use of net.jforum.view.forum.common.AttachmentCommon in project jforum2 by rafaelsteil.
the class PostAction method insertSave.
public void insertSave() {
int forumId = this.request.getIntParameter("forum_id");
boolean firstPost = false;
if (!this.anonymousPost(forumId)) {
return;
}
Topic t = new Topic(-1);
t.setForumId(forumId);
boolean newTopic = (this.request.getParameter("topic_id") == null);
if (!TopicsCommon.isTopicAccessible(t.getForumId()) || this.isForumReadonly(t.getForumId(), newTopic)) {
return;
}
TopicDAO topicDao = DataAccessDriver.getInstance().newTopicDAO();
PostDAO postDao = DataAccessDriver.getInstance().newPostDAO();
PollDAO poolDao = DataAccessDriver.getInstance().newPollDAO();
ForumDAO forumDao = DataAccessDriver.getInstance().newForumDAO();
if (!newTopic) {
int topicId = this.request.getIntParameter("topic_id");
t = TopicRepository.getTopic(new Topic(topicId));
if (t == null) {
t = topicDao.selectById(topicId);
}
// Could not find the topic. The topicId sent was invalid
if (t == null || t.getId() == 0) {
newTopic = true;
} else {
if (!TopicsCommon.isTopicAccessible(t.getForumId())) {
return;
}
// Cannot insert new messages on locked topics
if (t.getStatus() == Topic.STATUS_LOCKED) {
this.topicLocked();
return;
}
}
}
// checking above set the newTopic var to true
if (newTopic) {
if (this.isReplyOnly(forumId)) {
this.replyOnly();
return;
}
if (this.request.getParameter("topic_type") != null) {
t.setType(this.request.getIntParameter("topic_type"));
if (t.getType() != Topic.TYPE_NORMAL && !SecurityRepository.canAccess(SecurityConstants.PERM_CREATE_STICKY_ANNOUNCEMENT_TOPICS)) {
t.setType(Topic.TYPE_NORMAL);
}
}
}
UserSession us = SessionFacade.getUserSession();
User u = DataAccessDriver.getInstance().newUserDAO().selectById(us.getUserId());
if ("1".equals(this.request.getParameter("quick")) && SessionFacade.isLogged()) {
this.request.addParameter("notify", u.isNotifyOnMessagesEnabled() ? "1" : null);
this.request.addParameter("attach_sig", u.getAttachSignatureEnabled() ? "1" : "0");
} else {
u.setId(us.getUserId());
u.setUsername(us.getUsername());
}
// Set the Post
Post p = PostCommon.fillPostFromRequest();
if (p.getText() == null || p.getText().trim().equals("")) {
this.insert();
return;
}
// Check the elapsed time since the last post from the user
int delay = SystemGlobals.getIntValue(ConfigKeys.POSTS_NEW_DELAY);
if (delay > 0) {
Long lastPostTime = (Long) SessionFacade.getAttribute(ConfigKeys.LAST_POST_TIME);
if (lastPostTime != null) {
if (System.currentTimeMillis() < (lastPostTime.longValue() + delay)) {
this.context.put("post", p);
this.context.put("start", this.request.getParameter("start"));
this.context.put("error", I18n.getMessage("PostForm.tooSoon"));
this.insert();
return;
}
}
}
p.setForumId(this.request.getIntParameter("forum_id"));
if (StringUtils.isBlank(p.getSubject())) {
p.setSubject(t.getTitle());
}
boolean needCaptcha = SystemGlobals.getBoolValue(ConfigKeys.CAPTCHA_POSTS) && request.getSessionContext().getAttribute(ConfigKeys.REQUEST_IGNORE_CAPTCHA) == null;
if (needCaptcha) {
if (!us.validateCaptchaResponse(this.request.getParameter("captcha_anwser"))) {
this.context.put("post", p);
this.context.put("start", this.request.getParameter("start"));
this.context.put("error", I18n.getMessage("CaptchaResponseFails"));
this.insert();
return;
}
}
boolean preview = "1".equals(this.request.getParameter("preview"));
if (!preview) {
AttachmentCommon attachments = new AttachmentCommon(this.request, forumId);
try {
attachments.preProcess();
} catch (AttachmentException e) {
JForumExecutionContext.enableRollback();
p.setText(this.request.getParameter("message"));
p.setId(0);
this.context.put("errorMessage", e.getMessage());
this.context.put("post", p);
this.insert();
return;
}
Forum forum = ForumRepository.getForum(forumId);
PermissionControl pc = SecurityRepository.get(us.getUserId());
// Moderators and admins don't need to have their messages moderated
boolean moderate = (forum.isModerated() && !pc.canAccess(SecurityConstants.PERM_MODERATION) && !pc.canAccess(SecurityConstants.PERM_ADMINISTRATION));
if (newTopic) {
t.setTime(new Date());
t.setTitle(this.request.getParameter("subject"));
t.setModerated(moderate);
t.setPostedBy(u);
t.setFirstPostTime(ViewCommon.formatDate(t.getTime()));
int topicId = topicDao.addNew(t);
t.setId(topicId);
firstPost = true;
}
if (!firstPost && pc.canAccess(SecurityConstants.PERM_REPLY_WITHOUT_MODERATION, Integer.toString(t.getForumId()))) {
moderate = false;
}
// Topic watch
if (this.request.getParameter("notify") != null) {
this.watch(topicDao, t.getId(), u.getId());
}
p.setTopicId(t.getId());
// add a poll
Poll poll = PollCommon.fillPollFromRequest();
if (poll != null && newTopic) {
poll.setTopicId(t.getId());
if (poll.getOptions().size() < 2) {
//it is not a valid poll, cancel the post
JForumExecutionContext.enableRollback();
p.setText(this.request.getParameter("message"));
p.setId(0);
this.context.put("errorMessage", I18n.getMessage("PostForm.needMorePollOptions"));
this.context.put("post", p);
this.context.put("poll", poll);
this.insert();
return;
}
poolDao.addNew(poll);
t.setVoteId(poll.getId());
}
// Save the remaining stuff
p.setModerate(moderate);
int postId = postDao.addNew(p);
if (newTopic) {
t.setFirstPostId(postId);
}
if (!moderate) {
t.setLastPostId(postId);
t.setLastPostBy(u);
t.setLastPostDate(p.getTime());
t.setLastPostTime(p.getFormatedTime());
}
topicDao.update(t);
attachments.insertAttachments(p);
if (!moderate) {
StringBuffer path = new StringBuffer(512);
path.append(this.request.getContextPath()).append("/posts/list/");
int start = ViewCommon.getStartPage();
path.append(this.startPage(t, start)).append("/").append(t.getId()).append(SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION)).append('#').append(postId);
JForumExecutionContext.setRedirect(path.toString());
if (newTopic) {
// Notify "forum new topic" users
ForumCommon.notifyUsers(forum, t, p);
} else {
t.setTotalReplies(t.getTotalReplies() + 1);
TopicsCommon.notifyUsers(t, p);
}
// Update forum stats, cache and etc
t.setTotalViews(t.getTotalViews() + 1);
DataAccessDriver.getInstance().newUserDAO().incrementPosts(p.getUserId());
TopicsCommon.updateBoardStatus(t, postId, firstPost, topicDao, forumDao);
ForumRepository.updateForumStats(t, u, p);
int anonymousUser = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
if (u.getId() != anonymousUser) {
SessionFacade.getTopicsReadTime().put(new Integer(t.getId()), new Long(p.getTime().getTime()));
}
if (SystemGlobals.getBoolValue(ConfigKeys.POSTS_CACHE_ENABLED)) {
SimpleDateFormat df = new SimpleDateFormat(SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT));
p.setFormatedTime(df.format(p.getTime()));
PostRepository.append(p.getTopicId(), PostCommon.preparePostForDisplay(p));
}
} else {
JForumExecutionContext.setRedirect(this.request.getContextPath() + "/posts/waitingModeration/" + (firstPost ? 0 : t.getId()) + "/" + t.getForumId() + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
}
if (delay > 0) {
SessionFacade.setAttribute(ConfigKeys.LAST_POST_TIME, new Long(System.currentTimeMillis()));
}
} else {
this.context.put("preview", true);
this.context.put("post", p);
this.context.put("start", this.request.getParameter("start"));
Post postPreview = new Post(p);
this.context.put("postPreview", PostCommon.preparePostForDisplay(postPreview));
this.insert();
}
}
use of net.jforum.view.forum.common.AttachmentCommon in project jforum2 by rafaelsteil.
the class PostAction method listByUser.
public void listByUser() {
PostDAO pm = DataAccessDriver.getInstance().newPostDAO();
UserDAO um = DataAccessDriver.getInstance().newUserDAO();
TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
User u = um.selectById(this.request.getIntParameter("user_id"));
if (u.getId() == 0) {
this.context.put("message", I18n.getMessage("User.notFound"));
this.setTemplateName(TemplateKeys.USER_NOT_FOUND);
return;
}
int count = SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
int start = ViewCommon.getStartPage();
int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
List posts = pm.selectByUserByLimit(u.getId(), start, postsPerPage);
int totalMessages = pm.countUserPosts(u.getId());
// get list of forums
Map topics = new HashMap();
Map forums = new HashMap();
for (Iterator iter = posts.iterator(); iter.hasNext(); ) {
Post p = (Post) iter.next();
if (!topics.containsKey(new Integer(p.getTopicId()))) {
Topic t = TopicRepository.getTopic(new Topic(p.getTopicId()));
if (t == null) {
t = tm.selectRaw(p.getTopicId());
}
this.context.put("attachmentsEnabled", SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_ENABLED, Integer.toString(t.getForumId())));
this.context.put("am", new AttachmentCommon(this.request, t.getForumId()));
topics.put(new Integer(t.getId()), t);
}
if (!forums.containsKey(new Integer(p.getForumId()))) {
Forum f = ForumRepository.getForum(p.getForumId());
if (f == null) {
// Ok, probably the user does not have permission to see this forum
iter.remove();
totalMessages--;
continue;
}
forums.put(new Integer(f.getId()), f);
}
PostCommon.preparePostForDisplay(p);
}
this.setTemplateName(TemplateKeys.POSTS_USER_POSTS_LIST);
this.context.put("canDownloadAttachments", SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_DOWNLOAD));
this.context.put("rssEnabled", SystemGlobals.getBoolValue(ConfigKeys.RSS_ENABLED));
this.context.put("allCategories", ForumCommon.getAllCategoriesAndForums(false));
this.context.put("posts", posts);
this.context.put("topics", topics);
this.context.put("forums", forums);
this.context.put("u", u);
this.context.put("pageTitle", I18n.getMessage("PostShow.userPosts") + " " + u.getUsername());
this.context.put("karmaMin", new Integer(SystemGlobals.getValue(ConfigKeys.KARMA_MIN_POINTS)));
this.context.put("karmaMax", new Integer(SystemGlobals.getValue(ConfigKeys.KARMA_MAX_POINTS)));
ViewCommon.contextToPagination(start, totalMessages, count);
}
use of net.jforum.view.forum.common.AttachmentCommon in project jforum2 by rafaelsteil.
the class PostAction method insert.
public void insert() {
int forumId;
// If we have a topic_id, then it should be a reply
if (this.request.getParameter("topic_id") != null) {
int topicId = this.request.getIntParameter("topic_id");
Topic t = TopicRepository.getTopic(new Topic(topicId));
if (t == null) {
t = DataAccessDriver.getInstance().newTopicDAO().selectRaw(topicId);
if (t == null) {
throw new ForumException("Could not find a topic with id #" + topicId);
}
}
forumId = t.getForumId();
if (!TopicsCommon.isTopicAccessible(t.getForumId())) {
return;
}
if (t.getStatus() == Topic.STATUS_LOCKED) {
this.topicLocked();
return;
}
this.context.put("topic", t);
this.context.put("setType", false);
this.context.put("pageTitle", I18n.getMessage("PostForm.reply") + " " + t.getTitle());
} else {
forumId = this.request.getIntParameter("forum_id");
if (this.isReplyOnly(forumId)) {
this.replyOnly();
return;
}
this.context.put("setType", true);
this.context.put("pageTitle", I18n.getMessage("PostForm.title"));
}
Forum forum = ForumRepository.getForum(forumId);
if (forum == null) {
throw new ForumException("Could not find a forum with id #" + forumId);
}
if (!TopicsCommon.isTopicAccessible(forumId)) {
return;
}
if (!this.anonymousPost(forumId) || this.isForumReadonly(forumId, this.request.getParameter("topic_id") != null)) {
return;
}
int userId = SessionFacade.getUserSession().getUserId();
this.setTemplateName(TemplateKeys.POSTS_INSERT);
// Attachments
boolean attachmentsEnabled = SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_ENABLED, Integer.toString(forumId));
if (attachmentsEnabled && !SessionFacade.isLogged() && !SystemGlobals.getBoolValue(ConfigKeys.ATTACHMENTS_ANONYMOUS)) {
attachmentsEnabled = false;
}
this.context.put("attachmentsEnabled", attachmentsEnabled);
if (attachmentsEnabled) {
QuotaLimit ql = new AttachmentCommon(this.request, forumId).getQuotaLimit(userId);
this.context.put("maxAttachmentsSize", new Long(ql != null ? ql.getSizeInBytes() : 1));
this.context.put("maxAttachments", SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_MAX_POST));
}
boolean needCaptcha = SystemGlobals.getBoolValue(ConfigKeys.CAPTCHA_POSTS);
this.context.put("moderationLoggingEnabled", SystemGlobals.getBoolValue(ConfigKeys.MODERATION_LOGGING_ENABLED));
this.context.put("smilies", SmiliesRepository.getSmilies());
this.context.put("forum", forum);
this.context.put("action", "insertSave");
this.context.put("start", this.request.getParameter("start"));
this.context.put("isNewPost", true);
this.context.put("needCaptcha", needCaptcha);
this.context.put("htmlAllowed", SecurityRepository.canAccess(SecurityConstants.PERM_HTML_DISABLED, Integer.toString(forumId)));
this.context.put("canCreateStickyOrAnnouncementTopics", SecurityRepository.canAccess(SecurityConstants.PERM_CREATE_STICKY_ANNOUNCEMENT_TOPICS));
this.context.put("canCreatePolls", SecurityRepository.canAccess(SecurityConstants.PERM_CREATE_POLL));
User user = DataAccessDriver.getInstance().newUserDAO().selectById(userId);
ViewCommon.prepareUserSignature(user);
if (this.request.getParameter("preview") != null) {
user.setNotifyOnMessagesEnabled(this.request.getParameter("notify") != null);
}
this.context.put("user", user);
}
use of net.jforum.view.forum.common.AttachmentCommon in project jforum2 by rafaelsteil.
the class PostAction method quote.
public void quote() {
PostDAO pm = DataAccessDriver.getInstance().newPostDAO();
Post p = pm.selectById(this.request.getIntParameter("post_id"));
if (p.getId() == 0) {
this.postNotFound();
return;
}
if (p.isModerationNeeded()) {
this.notModeratedYet();
return;
}
if (!this.anonymousPost(p.getForumId())) {
return;
}
Topic topic = TopicRepository.getTopic(new Topic(p.getTopicId()));
if (topic == null) {
topic = DataAccessDriver.getInstance().newTopicDAO().selectRaw(p.getTopicId());
}
if (!TopicsCommon.isTopicAccessible(topic.getForumId())) {
return;
}
if (topic.getStatus() == Topic.STATUS_LOCKED) {
this.topicLocked();
return;
}
this.setTemplateName(TemplateKeys.POSTS_QUOTE);
this.context.put("forum", ForumRepository.getForum(p.getForumId()));
this.context.put("action", "insertSave");
this.context.put("post", p);
UserDAO um = DataAccessDriver.getInstance().newUserDAO();
User u = um.selectById(p.getUserId());
int userId = SessionFacade.getUserSession().getUserId();
this.context.put("attachmentsEnabled", SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_ENABLED, Integer.toString(topic.getForumId())));
QuotaLimit ql = new AttachmentCommon(this.request, topic.getForumId()).getQuotaLimit(userId);
this.context.put("maxAttachmentsSize", new Long(ql != null ? ql.getSizeInBytes() : 1));
this.context.put("moderationLoggingEnabled", SystemGlobals.getBoolValue(ConfigKeys.MODERATION_LOGGING_ENABLED));
this.context.put("maxAttachments", SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_MAX_POST));
this.context.put("isNewPost", true);
this.context.put("topic", topic);
this.context.put("quote", "true");
this.context.put("quoteUser", u.getUsername());
this.context.put("setType", false);
this.context.put("htmlAllowed", SecurityRepository.canAccess(SecurityConstants.PERM_HTML_DISABLED, Integer.toString(topic.getForumId())));
this.context.put("start", this.request.getParameter("start"));
this.context.put("user", DataAccessDriver.getInstance().newUserDAO().selectById(userId));
this.context.put("pageTitle", I18n.getMessage("PostForm.reply") + " " + topic.getTitle());
this.context.put("smilies", SmiliesRepository.getSmilies());
boolean needCaptcha = SystemGlobals.getBoolValue(ConfigKeys.CAPTCHA_POSTS);
if (needCaptcha) {
SessionFacade.getUserSession().createNewCaptcha();
}
this.context.put("needCaptcha", needCaptcha);
}
Aggregations