use of net.jforum.entities.Post in project jforum2 by rafaelsteil.
the class PostAction method downloadAttach.
public void downloadAttach() {
int id = this.request.getIntParameter("attach_id");
if (!SessionFacade.isLogged() && !SystemGlobals.getBoolValue(ConfigKeys.ATTACHMENTS_ANONYMOUS)) {
String referer = this.request.getHeader("Referer");
if (referer != null) {
this.setTemplateName(ViewCommon.contextToLogin(referer));
} else {
this.setTemplateName(ViewCommon.contextToLogin());
}
return;
}
AttachmentDAO am = DataAccessDriver.getInstance().newAttachmentDAO();
Attachment a = am.selectAttachmentById(id);
PostDAO postDao = DataAccessDriver.getInstance().newPostDAO();
Post post = postDao.selectById(a.getPostId());
String forumId = Integer.toString(post.getForumId());
boolean attachmentsEnabled = SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_ENABLED, forumId);
boolean attachmentsDownload = SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_DOWNLOAD, forumId);
if (!attachmentsEnabled && !attachmentsDownload) {
this.setTemplateName(TemplateKeys.POSTS_CANNOT_DOWNLOAD);
this.context.put("message", I18n.getMessage("Attachments.featureDisabled"));
return;
}
String filename = SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_STORE_DIR) + "/" + a.getInfo().getPhysicalFilename();
if (!new File(filename).exists()) {
this.setTemplateName(TemplateKeys.POSTS_ATTACH_NOTFOUND);
this.context.put("message", I18n.getMessage("Attachments.notFound"));
return;
}
FileInputStream fis = null;
OutputStream os = null;
try {
a.getInfo().setDownloadCount(a.getInfo().getDownloadCount() + 1);
am.updateAttachment(a);
fis = new FileInputStream(filename);
os = response.getOutputStream();
if (am.isPhysicalDownloadMode(a.getInfo().getExtension().getExtensionGroupId())) {
this.response.setContentType("application/octet-stream");
} else {
this.response.setContentType(a.getInfo().getMimetype());
}
if (this.request.getHeader("User-Agent").indexOf("Firefox") != -1) {
this.response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(a.getInfo().getRealFilename().getBytes(SystemGlobals.getValue(ConfigKeys.ENCODING)), SystemGlobals.getValue(ConfigKeys.DEFAULT_CONTAINER_ENCODING)) + "\";");
} else {
this.response.setHeader("Content-Disposition", "attachment; filename=\"" + ViewCommon.toUtf8String(a.getInfo().getRealFilename()) + "\";");
}
this.response.setContentLength((int) a.getInfo().getFilesize());
int c;
byte[] b = new byte[4096];
while ((c = fis.read(b)) != -1) {
os.write(b, 0, c);
}
JForumExecutionContext.enableCustomContent(true);
} catch (IOException e) {
throw new ForumException(e);
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
}
}
if (os != null) {
try {
os.close();
} catch (Exception e) {
}
}
}
}
use of net.jforum.entities.Post in project jforum2 by rafaelsteil.
the class PostAction method editSave.
public void editSave() {
PostDAO postDao = DataAccessDriver.getInstance().newPostDAO();
PollDAO pollDao = DataAccessDriver.getInstance().newPollDAO();
TopicDAO topicDao = DataAccessDriver.getInstance().newTopicDAO();
Post post = postDao.selectById(this.request.getIntParameter("post_id"));
if (!PostCommon.canEditPost(post)) {
this.cannotEdit();
return;
}
boolean isModerator = SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_EDIT);
String originalMessage = post.getText();
post = PostCommon.fillPostFromRequest(post, true);
// The user wants to preview the message before posting it?
if ("1".equals(this.request.getParameter("preview"))) {
this.context.put("preview", true);
Post postPreview = new Post(post);
this.context.put("postPreview", PostCommon.preparePostForDisplay(postPreview));
this.edit(true, post);
} else {
AttachmentCommon attachments = new AttachmentCommon(this.request, post.getForumId());
try {
attachments.preProcess();
} catch (AttachmentException e) {
JForumExecutionContext.enableRollback();
post.setText(this.request.getParameter("message"));
this.context.put("errorMessage", e.getMessage());
this.context.put("post", post);
this.edit(false, post);
return;
}
Topic t = TopicRepository.getTopic(new Topic(post.getTopicId()));
if (t == null) {
t = topicDao.selectById(post.getTopicId());
}
if (!TopicsCommon.isTopicAccessible(t.getForumId())) {
return;
}
if (t.getStatus() == Topic.STATUS_LOCKED && !SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_EDIT)) {
this.topicLocked();
return;
}
postDao.update(post);
// Attachments
attachments.editAttachments(post.getId(), post.getForumId());
attachments.insertAttachments(post);
// The first message (the one which originated the topic) was changed
if (t.getFirstPostId() == post.getId()) {
t.setTitle(post.getSubject());
int newType = this.request.getIntParameter("topic_type");
boolean changeType = SecurityRepository.canAccess(SecurityConstants.PERM_CREATE_STICKY_ANNOUNCEMENT_TOPICS) && newType != t.getType();
if (changeType) {
t.setType(newType);
}
// Poll
Poll poll = PollCommon.fillPollFromRequest();
if (poll != null && !t.isVote()) {
// They added a poll
poll.setTopicId(t.getId());
if (!this.ensurePollMinimumOptions(post, poll)) {
return;
}
pollDao.addNew(poll);
t.setVoteId(poll.getId());
} else if (poll != null) {
if (!this.ensurePollMinimumOptions(post, poll)) {
return;
}
// They edited the poll in the topic
Poll existing = pollDao.selectById(t.getVoteId());
PollChanges changes = new PollChanges(existing, poll);
if (changes.hasChanges()) {
poll.setId(existing.getId());
poll.setChanges(changes);
pollDao.update(poll);
}
} else if (t.isVote()) {
// They deleted the poll from the topic
pollDao.delete(t.getVoteId());
t.setVoteId(0);
}
topicDao.update(t);
if (changeType) {
TopicRepository.addTopic(t);
} else {
TopicRepository.updateTopic(t);
}
}
if (SystemGlobals.getBoolValue(ConfigKeys.MODERATION_LOGGING_ENABLED) && isModerator && post.getUserId() != SessionFacade.getUserSession().getUserId()) {
ModerationHelper helper = new ModerationHelper();
this.request.addParameter("log_original_message", originalMessage);
ModerationLog log = helper.buildModerationLogFromRequest();
log.getPosterUser().setId(post.getUserId());
helper.saveModerationLog(log);
}
if (this.request.getParameter("notify") == null) {
topicDao.removeSubscription(post.getTopicId(), SessionFacade.getUserSession().getUserId());
}
String path = this.request.getContextPath() + "/posts/list/";
int start = ViewCommon.getStartPage();
if (start > 0) {
path += start + "/";
}
path += post.getTopicId() + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) + "#" + post.getId();
JForumExecutionContext.setRedirect(path);
if (SystemGlobals.getBoolValue(ConfigKeys.POSTS_CACHE_ENABLED)) {
PostRepository.update(post.getTopicId(), PostCommon.preparePostForDisplay(post));
}
}
}
use of net.jforum.entities.Post in project jforum2 by rafaelsteil.
the class PostAction method delete.
public void delete() {
if (!SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_REMOVE)) {
this.setTemplateName(TemplateKeys.POSTS_CANNOT_DELETE);
this.context.put("message", I18n.getMessage("CannotRemovePost"));
return;
}
// Post
PostDAO postDao = DataAccessDriver.getInstance().newPostDAO();
Post p = postDao.selectById(this.request.getIntParameter("post_id"));
if (p.getId() == 0) {
this.postNotFound();
return;
}
TopicDAO topicDao = DataAccessDriver.getInstance().newTopicDAO();
Topic t = topicDao.selectRaw(p.getTopicId());
if (!TopicsCommon.isTopicAccessible(t.getForumId())) {
return;
}
postDao.delete(p);
DataAccessDriver.getInstance().newUserDAO().decrementPosts(p.getUserId());
// Karma
KarmaDAO karmaDao = DataAccessDriver.getInstance().newKarmaDAO();
karmaDao.updateUserKarma(p.getUserId());
// Attachments
new AttachmentCommon(this.request, p.getForumId()).deleteAttachments(p.getId(), p.getForumId());
// It was the last remaining post in the topic?
int totalPosts = topicDao.getTotalPosts(p.getTopicId());
if (totalPosts > 0) {
// Topic
topicDao.decrementTotalReplies(p.getTopicId());
int maxPostId = topicDao.getMaxPostId(p.getTopicId());
if (maxPostId > -1) {
topicDao.setLastPostId(p.getTopicId(), maxPostId);
}
int minPostId = topicDao.getMinPostId(p.getTopicId());
if (minPostId > -1) {
topicDao.setFirstPostId(p.getTopicId(), minPostId);
}
// Forum
ForumDAO fm = DataAccessDriver.getInstance().newForumDAO();
maxPostId = fm.getMaxPostId(p.getForumId());
if (maxPostId > -1) {
fm.setLastPost(p.getForumId(), maxPostId);
}
String returnPath = this.request.getContextPath() + "/posts/list/";
int page = ViewCommon.getStartPage();
if (page > 0) {
int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
if (totalPosts % postsPerPage == 0) {
page -= postsPerPage;
}
returnPath += page + "/";
}
JForumExecutionContext.setRedirect(returnPath + p.getTopicId() + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
// Update the cache
if (TopicRepository.isTopicCached(t)) {
t = topicDao.selectById(t.getId());
TopicRepository.updateTopic(t);
}
} else {
// Ok, all posts were removed. Time to say goodbye
TopicsCommon.deleteTopic(p.getTopicId(), p.getForumId(), false);
JForumExecutionContext.setRedirect(this.request.getContextPath() + "/forums/show/" + p.getForumId() + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
}
this.request.addOrReplaceParameter("log_original_message", p.getText());
ModerationHelper moderationHelper = new ModerationHelper();
ModerationLog moderationLog = moderationHelper.buildModerationLogFromRequest();
moderationLog.getPosterUser().setId(p.getUserId());
moderationHelper.saveModerationLog(moderationLog);
PostRepository.remove(t.getId(), p.getId());
TopicRepository.loadMostRecentTopics();
TopicRepository.loadHottestTopics();
ForumRepository.reloadForum(p.getForumId());
}
use of net.jforum.entities.Post 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.entities.Post in project jforum2 by rafaelsteil.
the class PostAction method preList.
/**
* Given a postId, sends the user to the right page
*/
public void preList() {
int postId = this.request.getIntParameter("post_id");
PostDAO dao = DataAccessDriver.getInstance().newPostDAO();
int count = dao.countPreviousPosts(postId);
int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
int topicId = 0;
if (this.request.getParameter("topic_id") != null) {
topicId = this.request.getIntParameter("topic_id");
}
if (topicId == 0) {
Post post = dao.selectById(postId);
topicId = post.getTopicId();
}
String page = "";
if (count > postsPerPage) {
page = Integer.toString(postsPerPage * ((count - 1) / postsPerPage)) + "/";
}
JForumExecutionContext.setRedirect(this.request.getContextPath() + "/posts/list/" + page + topicId + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) + "#" + postId);
}
Aggregations