1 53 54 106 107 package com.Yasna.forum.tags; 108 109 import java.util.*; 110 import javax.servlet.*; 111 import javax.servlet.jsp.*; 112 import javax.servlet.jsp.tagext.*; 113 import javax.servlet.http.*; 114 import com.Yasna.forum.*; 115 import com.Yasna.forum.Exceptions.RapidPostingException; 116 import com.Yasna.forum.Exceptions.UserBlackListedException; 117 import com.Yasna.forum.util.ClientIP; 118 import com.Yasna.forum.tags.*; 119 import java.net.URLDecoder ; 120 121 162 163 public class PostTag extends TagSupport implements YazdProperty, ChangeForum 164 { 165 private String subject = null; 167 private String localIP = null; 168 private int type =0; 169 private StringBuffer body = new StringBuffer (); 170 private boolean isreply = false; 171 private boolean submit = false; 172 private boolean isPrivate; 173 private boolean isPreview; 174 private YazdRequest jr = null; 176 private YazdState js = null; 177 private Forum pf = null; 178 private ForumMessage message = null; 179 private Map properties = new HashMap(); 180 181 190 public final int doStartTag() throws JspException 191 { 192 js = (YazdState)pageContext.getAttribute("yazdUserState", 194 PageContext.SESSION_SCOPE); 195 if( js == null ) 196 throw new JspException("Yazd post tag, could not find yazdUserState"); 197 198 jr = (YazdRequest)pageContext.getAttribute("yazdUserRequest", 200 PageContext.REQUEST_SCOPE); 201 if( jr == null ) 202 throw new JspException("Yazd post tag, could not find request"); 203 204 pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE); 206 207 ServletRequest req = pageContext.getRequest(); 209 String tmp; 210 tmp = req.getParameter("post"); 211 if( tmp != null && tmp.length() > 0 ) 212 submit=true; 213 tmp = req.getParameter("reply"); 214 if( tmp != null && tmp.length() > 0 ) { 215 submit=true; 216 isreply = true; 217 } 218 tmp = req.getParameter("preview"); 219 if( tmp != null && tmp.length() > 0 ) { 220 isPreview = true; 221 submit = false; 222 } else { 223 isPreview = false; 224 } 225 tmp = req.getParameter("isPrivate"); 226 if( tmp != null && "true".equals(tmp)) { 227 isPrivate = true; 228 } else { 229 isPrivate = false; 230 } 231 232 String b = req.getParameter("body"); 233 String localIP = req.getParameter("localeip"); 234 String s = req.getParameter("subject"); 235 int type = Integer.parseInt(req.getParameter("type")); 236 if (req.getParameter("isPreview") != null && b != null && s != null) { 237 try { 238 b = URLDecoder.decode(b, "UTF-8"); 239 s = URLDecoder.decode(s, "UTF-8"); 240 } catch(Exception e) { 241 e.printStackTrace(); 242 } 243 } 244 245 subject = s; 246 if (!isPreview) body.append( b ); 247 248 return EVAL_BODY_INCLUDE; 250 } 251 252 259 public final String getSubject() 260 { 261 if( subject == null ) 262 return ""; 263 return subject; 264 } 265 266 272 public final String getProperty(String name) 273 { 274 String tmp = (String )properties.get(name); 275 if( tmp != null )return tmp; 276 return ""; 277 } 278 279 283 public final void setProperty(String name, String value) 284 { 285 properties.put(name,value); 286 } 287 288 295 public final String getBody() 296 { 297 return body.toString(); 298 } 299 300 307 public final void appendBody(String str) 308 { 309 if( body.length() > 0 ) 310 body.append(str); 311 } 312 313 318 public final boolean postMessage() throws JspException 319 { 320 ForumMessage parentMessage = null; 321 ForumThread thread = null; 322 Forum cf = jr.getForum(); 323 User user = null; 324 String IP =""; 325 326 if( !submit && !isPreview) 328 return false; 329 330 if( body.length() == 0 || 331 subject == null || subject.length() == 0 ) { 332 jr.addError("A message must have both a Subject and a Message."); 333 pageContext.getRequest().removeAttribute("showPreview"); 334 return false; 335 } 336 IP=pageContext.getRequest().getRemoteAddr(); 337 if (isPreview) return false; 338 if( pf == null ) { 339 pf = cf; 340 } 341 342 try { 343 user = jr.getProfileManager().getUser( js.getAuthorization().getUserID() ); 344 } catch( UserNotFoundException e ) { 345 throw new JspException("Post Message, user account not found"); 346 } 347 try { 348 ClientIP clientIP = new ClientIP(localIP,IP); 349 message = pf.createMessage( user,clientIP ); 350 message.setSubject( subject ); 351 message.setBody( body.toString() ); 352 message.setProperty("IP",IP); 353 if(localIP !=null){ 354 message.setProperty("localIP",localIP); 355 } 356 String tmp; 357 for( Iterator it=properties.keySet().iterator(); it.hasNext(); ) { 358 tmp = (String )it.next(); 359 message.setProperty(tmp,(String )properties.get(tmp)); 360 } 361 if( isreply ) { 362 try { 363 thread = cf.getThread( js.getThreadID() ); 364 parentMessage = thread.getMessage( js.getMessageID() ); 365 } catch( ForumThreadNotFoundException e ) { 366 throw new JspException("Post Message, thread not found with id: " + 367 js.getThreadID() ); 368 } catch( ForumMessageNotFoundException e ) { 369 throw new JspException("Post Message, parent not found with id: " + 370 js.getMessageID() ); 371 } 372 if (isPrivate) { 373 message.setReplyPrivateUserId(parentMessage.getUser().getID()); 374 } 375 thread.addMessage( parentMessage, message ); 376 } else { 377 thread = pf.createThread( message ,jr.getThreadType(type)); 378 pf.addThread( thread ); 379 } 380 } catch( UnauthorizedException ue ) { 381 jr.addError(TagPropertyManager.getTagProperty("yazd.tag.post.authorize.failed")); 382 return false; 383 } catch( RapidPostingException ue ) { 384 jr.addError(TagPropertyManager.getTagProperty("yazd.tag.post.rapid.failed")); 385 return false; 386 } catch( UserBlackListedException ue ) { 387 jr.addError(TagPropertyManager.getTagProperty("yazd.tag.post.blacklist.failed")); 388 return false; 389 } 390 return true; 391 } 392 393 396 public final void changeForum(Forum apf) 397 { 398 pf = apf; 399 } 400 401 public final boolean isPreview() { 402 return isPreview; 403 } 404 405 } 406 | Popular Tags |