⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 postaction.java

📁 个人认为是最好的Java论坛源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				firstPost = true;
			}
			
			// Moderators and admins don't need to have their messages moderated
			moderate = (t.isModerated() 
					&& !SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION)
					&& !SecurityRepository.canAccess(SecurityConstants.PERM_ADMINISTRATION));

			// Topic watch
			if (this.request.getParameter("notify") != null) {
				this.watch(tm, t.getId(), u.getId());
			}

			p.setTopicId(t.getId());

			// Save the remaining stuff
			p.setModerate(moderate);
			int postId = pm.addNew(p);

			if (this.request.getParameter("topic_id") == null) {
				t.setFirstPostId(postId);
			}
			
			tm.update(t);
			
			// Attachments
			try {
				new AttachmentCommon(this.request).insertAttachments(postId, forumId);
			}
			catch (AttachmentException e) {
				JForum.enableCancelCommit();
				p.setText(this.request.getParameter("message"));
				p.setId(0);
				this.context.put("errorMessage", e.getMessage());
				this.context.put("post", p);
				this.insert();
				return;
			}

			if (!moderate) {
				DataAccessDriver.getInstance().newUserModel().incrementPosts(p.getUserId());
				TopicsCommon.updateBoardStatus(t, postId, firstPost, tm, fm);
				TopicsCommon.notifyUsers(t, tm);
	
				String path = this.request.getContextPath() + "/posts/list/";
				int start = ViewCommon.getStartPage();
	
				path += this.startPage(t, start) + "/";
				path += t.getId() + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) + "#" + postId;
	
				JForum.setRedirect(path);
	
				int anonymousUser = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
				if (u.getId() != anonymousUser) {
					((Map) SessionFacade.getAttribute(ConfigKeys.TOPICS_TRACKING)).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 {
				JForum.setRedirect(this.request.getContextPath() + "/posts/waitingModeration/" + (firstPost ? 0 : t.getId())
						+ "/" + t.getForumId()
						+ SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
			}
		}
		else {
			this.context.put("preview", true);
			this.context.put("post", p);
			this.context.put("topic", t);
			this.context.put("start", this.request.getParameter("start"));

			Post postPreview = new Post(p);
			this.context.put("postPreview", PostCommon.preparePostForDisplay(postPreview));

			this.insert();
		}
	}

	private int startPage(Topic t, int currentStart) {
		int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POST_PER_PAGE);

		int newStart = (t.getTotalReplies() + 1) / postsPerPage * postsPerPage;
		if (newStart > currentStart) {
			return newStart;
		}
		
		return currentStart;
	}

	public void delete() throws Exception {
		if (!SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_REMOVE)) {
			this.context.put("moduleAction", "message.htm");
			this.context.put("message", I18n.getMessage("CannotRemovePost"));

			return;
		}

		// Post
		PostModel pm = DataAccessDriver.getInstance().newPostModel();
		Post p = pm.selectById(this.request.getIntParameter("post_id"));

		TopicModel tm = DataAccessDriver.getInstance().newTopicModel();
		Topic t = tm.selectById(p.getTopicId());

		if (!TopicsCommon.isTopicAccessible(t.getForumId())) {
			return;
		}

		if (p.getId() == 0) {
			this.postNotFound();
			return;
		}

		pm.delete(p);
		DataAccessDriver.getInstance().newUserModel().decrementPosts(p.getUserId());
		
		// Attachments
		new AttachmentCommon(this.request).deleteAttachments(p.getId(), p.getForumId());

		// Topic
		tm.decrementTotalReplies(p.getTopicId());

		int maxPostId = tm.getMaxPostId(p.getTopicId());
		if (maxPostId > -1) {
			tm.setLastPostId(p.getTopicId(), maxPostId);
		}

		int minPostId = tm.getMinPostId(p.getTopicId());
		if (minPostId > -1) {
		  tm.setFirstPostId(p.getTopicId(), minPostId);
		}
        
		// Forum
		ForumModel fm = DataAccessDriver.getInstance().newForumModel();

		maxPostId = fm.getMaxPostId(p.getForumId());
		if (maxPostId > -1) {
			fm.setLastPost(p.getForumId(), maxPostId);
		}

		// It was the last remaining post in the topic?
		int totalPosts = tm.getTotalPosts(p.getTopicId());
		if (totalPosts > 0) {
			String page = this.request.getParameter("start");
			String returnPath = this.request.getContextPath() + "/posts/list/";

			if (page != null && !page.equals("") && !page.equals("0")) {
				int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POST_PER_PAGE);
				int newPage = Integer.parseInt(page);

				if (totalPosts % postsPerPage == 0) {
					newPage -= postsPerPage;
				}

				returnPath += newPage + "/";
			}

			JForum.setRedirect(returnPath + p.getTopicId() + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
		}
		else {
			// Ok, all posts were removed. Time to say goodbye
			TopicsCommon.deleteTopic(p.getTopicId(), p.getForumId());

			JForum.setRedirect(this.request.getContextPath() + "/forums/show/" + p.getForumId()
					+ SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
		}

		ForumRepository.reloadForum(p.getForumId());
		TopicRepository.clearCache(p.getForumId());
		PostRepository.clearCache(p.getTopicId());
	}

	private void watch(TopicModel tm, int topicId, int userId) throws Exception {
		if (!tm.isUserSubscribed(topicId, userId)) {
			tm.subscribeUser(topicId, userId);
		}
	}

	public void watch() throws Exception {
		int topicId = this.request.getIntParameter("topic_id");
		int userId = SessionFacade.getUserSession().getUserId();

		this.watch(DataAccessDriver.getInstance().newTopicModel(), topicId, userId);
		this.list();
	}

	public void unwatch() throws Exception {
		if (this.isUserLogged()) {
			int topicId = this.request.getIntParameter("topic_id");
			int userId = SessionFacade.getUserSession().getUserId();
			String start = this.request.getParameter("start");

			DataAccessDriver.getInstance().newTopicModel().removeSubscription(topicId, userId);

			String returnPath = this.request.getContextPath() + "/posts/list/";
			if (start != null && !start.equals("")) {
				returnPath += start + "/";
			}

			returnPath += topicId + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION);

			this.context.put("moduleAction", "message.htm");
			this.context.put("message", I18n.getMessage("ForumBase.unwatched", new String[] { returnPath }));
		}
		else {
			ViewCommon.contextToLogin();
		}
	}

	public void downloadAttach() throws Exception
	{
		if (!SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_DOWNLOAD)) {
			this.context.put("moduleAction", "message.htm");
			this.context.put("message", I18n.getMessage("Attachments.featureDisabled"));
			return;
		}
		
		int id = this.request.getIntParameter("attach_id");
		
		AttachmentModel am = DataAccessDriver.getInstance().newAttachmentModel();
		Attachment a = am.selectAttachmentById(id);
		
		String filename = SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_STORE_DIR)
			+ "/"
			+ a.getInfo().getPhysicalFilename();
		
		if (!new File(filename).exists()) {
			this.context.put("moduleAction", "message.htm");
			this.context.put("message", I18n.getMessage("Attachments.notFound"));
			return;
		}
		
		a.getInfo().setDownloadCount(a.getInfo().getDownloadCount() + 1);
		am.updateAttachment(a);
		
		FileInputStream fis = new FileInputStream(filename);
		OutputStream os = response.getOutputStream();

		this.response.setContentType("application/octet-stream");
		this.response.setHeader("Content-Disposition", "attachment; filename=\"" + a.getInfo().getRealFilename() + "\";");
		this.response.setContentLength((int)a.getInfo().getFilesize());
		
		byte[] b = new byte[4096];
		int c = 0;
		while ((c = fis.read(b)) != -1) {
			os.write(b);
		}
		
		fis.close();
		os.close();
		
		JForum.enableBinaryContent(true);
	}
	
	private boolean isUserLogged() {
		return (SessionFacade.getAttribute("logged") != null && SessionFacade.getAttribute("logged").equals("1"));
	}

	private void topicLocked() {
		this.context.put("moduleAction", "message.htm");
		this.context.put("message", I18n.getMessage("PostShow.topicLocked"));
	}
	
	public void listSmilies()
	{
		this.setTemplateName(SystemGlobals.getValue(ConfigKeys.TEMPLATE_NAME) + "/empty.htm");
		this.context.put("moduleAction", "list_smilies.htm");
		this.context.put("smilies", SmiliesRepository.getSmilies());
	}

	private boolean isForumReadonly(int forumId, boolean isReply) throws Exception {
		if (!SecurityRepository.canAccess(SecurityConstants.PERM_READ_ONLY_FORUMS, Integer.toString(forumId))) {
			if (isReply) {
				this.list();
			}
			else {
				JForum.setRedirect(this.request.getContextPath() + "/forums/show/" + forumId
						+ SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
			}

			return true;
		}

		return false;
	}

	private boolean anonymousPost(int forumId) throws Exception {
		// Check if anonymous posts are allowed
		if (!this.isUserLogged()
				&& !SecurityRepository.canAccess(SecurityConstants.PERM_ANONYMOUS_POST, Integer.toString(forumId))) {
			ViewCommon.contextToLogin();

			return false;
		}

		return true;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -