1 43 package net.jforum.view.forum.common; 44 45 import java.util.ArrayList ; 46 import java.util.Collection ; 47 import java.util.Date ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 import java.util.regex.Matcher ; 51 import java.util.regex.Pattern ; 52 53 import net.jforum.ActionServletRequest; 54 import net.jforum.JForumExecutionContext; 55 import net.jforum.SessionFacade; 56 import net.jforum.dao.PostDAO; 57 import net.jforum.entities.Post; 58 import net.jforum.entities.Smilie; 59 import net.jforum.repository.BBCodeRepository; 60 import net.jforum.repository.PostRepository; 61 import net.jforum.repository.SecurityRepository; 62 import net.jforum.repository.SmiliesRepository; 63 import net.jforum.security.SecurityConstants; 64 import net.jforum.util.SafeHtml; 65 import net.jforum.util.bbcode.BBCode; 66 import net.jforum.util.preferences.ConfigKeys; 67 import net.jforum.util.preferences.SystemGlobals; 68 69 73 public class PostCommon 74 { 75 private static PostCommon instance = new PostCommon(); 76 77 84 public static PostCommon getInstance() 85 { 86 return instance; 87 } 88 89 public static Post preparePostForDisplay(Post p) 90 { 91 if (p.getText() == null) { 92 return p; 93 } 94 95 if (!p.isHtmlEnabled()) { 96 p.setText(p.getText().replaceAll("<", "<").replaceAll(">", ">")); 97 } 98 99 p.setText(p.getText().replaceAll("\n", "<br/> ")); 101 102 p.setText(alwaysProcess(p.getText(), BBCodeRepository.getBBCollection().getAlwaysProcessList())); 103 104 if (p.isBbCodeEnabled()) { 106 p.setText(PostCommon.processText(p.getText())); 107 } 108 109 if (p.isSmiliesEnabled()) { 111 p.setText(processSmilies(p.getText(), SmiliesRepository.getSmilies())); 112 } 113 114 p.setText(SafeHtml.avoidJavascript(p.getText())); 115 116 return p; 117 } 118 119 private static String alwaysProcess(String text, Collection bbList) 120 { 121 for (Iterator iter = bbList.iterator(); iter.hasNext(); ) { 122 BBCode bb = (BBCode)iter.next(); 123 text = text.replaceAll(bb.getRegex(), bb.getReplace()); 124 } 125 126 return text; 127 } 128 129 public static String processText(String text) 130 { 131 if (text == null) { 132 return null; 133 } 134 135 if (text.indexOf('[') > -1 && text.indexOf(']') > -1) { 136 Iterator tmpIter = BBCodeRepository.getBBCollection().getBbList().iterator(); 137 138 while (tmpIter.hasNext()) { 139 BBCode bb = (BBCode) tmpIter.next(); 140 141 if (bb.getTagName().equals("openQuote") || bb.getTagName().equals("openSimpleQuote")) { 143 Matcher matcher = Pattern.compile(bb.getRegex()).matcher(text); 144 145 while (matcher.find()) { 146 text = text.replaceFirst(bb.getRegex(), bb.getReplace()); 147 } 148 } 149 else if (bb.getTagName().equals("closeQuote")) { 150 Matcher matcher = Pattern.compile(bb.getRegex()).matcher(text); 151 152 while (matcher.find()) { 153 text = text.replaceFirst(bb.getRegex(), bb.getReplace()); 154 } 155 } 156 else if (bb.getTagName().equals("code")) { 157 Matcher matcher = Pattern.compile(bb.getRegex()).matcher(text); 158 StringBuffer sb = new StringBuffer (text); 159 160 while (matcher.find()) { 161 String contents = matcher.group(1); 162 163 contents = contents.replaceAll("<br/>", "\n"); 166 167 contents = contents.replaceAll("\\[", "[").replaceAll("\\]", "]"); 169 170 contents = contents.replaceAll("\\(", "(").replaceAll("\\)", ")"); 172 173 contents = contents.replaceAll("<", "<").replaceAll(">", ">"); 175 176 StringBuffer replace = new StringBuffer (bb.getReplace()); 177 int index = replace.indexOf("$1"); 178 179 if (index > -1) { 180 replace.replace(index, index + 2, contents); 181 } 182 183 index = sb.indexOf("[code]"); 184 int lastIndex = sb.indexOf("[/code]") + "[/code]".length(); 185 186 if (lastIndex > index) { 187 sb.replace(index, lastIndex, replace.toString()); 188 text = sb.toString(); 189 } 190 } 191 } 192 else { 193 text = text.replaceAll(bb.getRegex(), bb.getReplace()); 194 } 195 } 196 } 197 198 return text; 199 } 200 201 public static String processSmilies(String text, List smilies) 202 { 203 if (text == null || text.equals("")) { 204 return text; 205 } 206 207 Iterator iter = smilies.iterator(); 208 while (iter.hasNext()) { 209 Smilie s = (Smilie) iter.next(); 210 211 int index = text.indexOf(s.getCode()); 212 if (index > -1) { 213 text = text.replaceAll("\\Q" + s.getCode() + "\\E", s.getUrl()); 214 } 215 } 216 217 return text; 218 } 219 220 public static Post fillPostFromRequest() throws Exception 221 { 222 Post p = new Post(); 223 p.setTime(new Date ()); 224 225 return fillPostFromRequest(p, false); 226 } 227 228 public static Post fillPostFromRequest(Post p, boolean isEdit) throws Exception 229 { 230 ActionServletRequest request = JForumExecutionContext.getRequest(); 231 232 p.setSubject(SafeHtml.makeSafe(request.getParameter("subject"))); 233 p.setBbCodeEnabled(request.getParameter("disable_bbcode") != null ? false : true); 234 p.setSmiliesEnabled(request.getParameter("disable_smilies") != null ? false : true); 235 p.setSignatureEnabled(request.getParameter("attach_sig") != null ? true : false); 236 237 if (!isEdit) { 238 p.setUserIp(request.getRemoteAddr()); 239 p.setUserId(SessionFacade.getUserSession().getUserId()); 240 } 241 242 boolean htmlEnabled = SecurityRepository.canAccess(SecurityConstants.PERM_HTML_DISABLED, 243 request.getParameter("forum_id")); 244 p.setHtmlEnabled(htmlEnabled && request.getParameter("disable_html") == null); 245 246 if (p.isHtmlEnabled()) { 247 p.setText(SafeHtml.makeSafe(request.getParameter("message"))); 248 } 249 else { 250 p.setText(request.getParameter("message")); 251 } 252 253 return p; 254 } 255 256 public static List topicPosts(PostDAO dao, boolean canEdit, int userId, int topicId, int start, int count) throws Exception 257 { 258 List posts = null; 259 boolean needPrepare = true; 260 261 if (SystemGlobals.getBoolValue(ConfigKeys.POSTS_CACHE_ENABLED)) { 262 posts = PostRepository.selectAllByTopicByLimit(topicId, start, count); 263 needPrepare = false; 264 } 265 else { 266 posts = dao.selectAllByTopicByLimit(topicId, start, count); 267 } 268 269 List helperList = new ArrayList (); 270 271 int anonymousUser = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID); 272 273 for (Iterator iter = posts.iterator(); iter.hasNext(); ) { 274 Post p; 275 276 if (needPrepare) { 277 p = (Post)iter.next(); 278 } 279 else { 280 p = new Post((Post)iter.next()); 281 } 282 283 if (canEdit || (p.getUserId() != anonymousUser && p.getUserId() == userId)) { 284 p.setCanEdit(true); 285 } 286 287 helperList.add(needPrepare ? PostCommon.preparePostForDisplay(p) : p); 288 } 289 290 return helperList; 291 } 292 } 293 | Popular Tags |