1 53 54 106 107 package com.Yasna.forum.util; 108 109 import java.util.Date ; 110 import java.util.Iterator ; 111 import java.util.Locale ; 112 import java.text.SimpleDateFormat ; 113 import java.io.File ; 114 import java.util.TimeZone ; 115 116 import javax.servlet.*; 117 import javax.servlet.http.*; 118 119 import com.Yasna.forum.*; 120 import com.Yasna.forum.Tasks.SessionManager; 121 import com.Yasna.forum.locale.YazdLocale; 122 import com.Yasna.util.StringUtils; 123 124 171 public class SkinUtils { 172 173 174 public static final String YAZD_AUTH_TOKEN = "yazdAuthorization"; 175 176 177 public static final String YAZD_AUTOLOGIN_COOKIE = "yazdAutoLogin"; 178 179 180 public static final String YAZD_LASTVISITED_TOKEN = "yazdLastVisited"; 181 182 183 public static final String YAZD_LASTVISITED_COOKIE = "yazdLastVisited"; 184 185 187 public static final String YAZD_LASTVISITED_PROP = "Site.useLastVisited"; 188 189 private static final long SECOND = 1000; 191 private static final long MINUTE = 60 * SECOND; 192 private static final long HOUR = 60 * MINUTE; 193 private static final long DAY = 24 * HOUR; 194 private static final long WEEK = 7 * DAY; 195 196 private static final int MAX_COOKIE_AGE = (int)(WEEK / 1000) * 8; 198 199 private static SessionManager sessionManager = new SessionManager(); 200 201 private static final String [] DAYS_OF_WEEK = 203 { "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" }; 204 205 private static final SimpleDateFormat yesterdayFormatter = 207 new SimpleDateFormat ("'Yesterday at' h:mm a"); 208 209 private final static int ENCODE_XORMASK = 0x5A; 212 private final static char ENCODE_DELIMETER = '\002'; 213 private final static char ENCODE_CHAR_OFFSET1 = 'A'; 214 private final static char ENCODE_CHAR_OFFSET2 = 'h'; 215 216 240 public static Authorization getUserAuthorization(HttpServletRequest request, 241 HttpServletResponse response, boolean checkYazdCookie) 242 { 243 HttpSession session = request.getSession(); 245 247 Authorization authToken = (Authorization)session.getAttribute(YAZD_AUTH_TOKEN); 249 if (authToken != null) { 250 sessionManager.addMessage(session.getId(),request.getRemoteAddr(),authToken.getUserID()); 251 return authToken; 252 } 253 254 if( checkYazdCookie ) { 256 Cookie cookie = getCookie(request, YAZD_AUTOLOGIN_COOKIE); 257 try { 258 if( cookie != null ) { 259 String [] values = decodePasswordCookie(cookie.getValue()); 262 String username = values[0]; 263 String password = values[1]; 264 authToken = AuthorizationFactory.getAuthorization(username,password); 266 267 session.setAttribute( YAZD_AUTH_TOKEN, authToken ); 269 270 sessionManager.addMessage(session.getId(),request.getRemoteAddr(),authToken.getUserID()); 272 return authToken; 273 } 274 } 275 catch( Exception e ) { 276 cookie = new Cookie(YAZD_AUTOLOGIN_COOKIE,null); 282 cookie.setMaxAge(0); cookie.setPath("/"); 284 response.addCookie(cookie); 285 } 286 } 287 288 sessionManager.addMessage(session.getId(),request.getRemoteAddr(),-1); 290 return null; 291 } 292 293 306 public static Authorization getUserAuthorization 307 ( HttpServletRequest request, HttpServletResponse response ) 308 { 309 return getUserAuthorization(request, response, true); 310 } 311 312 326 public static Authorization setUserAuthorization(HttpServletRequest request, 327 HttpServletResponse response, String username, String password, 328 boolean autoLogin) throws UserNotFoundException, UnauthorizedException 329 { 330 HttpSession session = request.getSession(); 331 Authorization authToken = AuthorizationFactory.getAuthorization(username, password); 332 session.setAttribute(YAZD_AUTH_TOKEN, authToken); 333 334 if (autoLogin) { 335 Cookie cookie = new Cookie(YAZD_AUTOLOGIN_COOKIE, encodePasswordCookie(username, password)); 336 cookie.setMaxAge(MAX_COOKIE_AGE); 337 cookie.setPath("/"); 338 response.addCookie(cookie); 339 } 340 341 return authToken; 342 } 343 344 350 public static void removeUserAuthorization( HttpServletRequest request, HttpServletResponse response ) 351 { 352 HttpSession session = request.getSession(); 353 session.removeAttribute(YAZD_AUTH_TOKEN); 354 Cookie cookie = new Cookie(YAZD_AUTOLOGIN_COOKIE, null); 355 cookie.setMaxAge(0); 356 cookie.setPath("/"); 357 response.addCookie(cookie); 358 } 359 360 367 public static void invalidateCookie( HttpServletRequest request, HttpServletResponse response, String cookieName ) { 368 Cookie cookie = new Cookie( cookieName, null ); cookie.setMaxAge(0); cookie.setPath("/"); 371 response.addCookie(cookie); 372 } 373 374 379 public static void store( HttpServletRequest request, HttpServletResponse response, 380 String id, String value ) 381 { 382 store( request,response,id,value,0,false ); 383 } 384 385 390 public static void store( HttpServletRequest request, HttpServletResponse response, 391 String id, String value, int secsToLive ) 392 { 393 store( request,response,id,value,secsToLive,false ); 394 } 395 396 412 public static void store( HttpServletRequest request, HttpServletResponse response, 413 String id, String value, int secsToLive, boolean restoreInSession ) 414 { 415 419 if( id == null ) { 421 return; 422 } 423 424 HttpSession session = request.getSession(); 426 427 if( ((String )session.getAttribute(id)) != null && !restoreInSession ) { 430 return; 431 } 432 433 session.setAttribute(id,value); 437 438 if( secsToLive > 0 ) { 440 Cookie cookie = new Cookie(id,value); 441 cookie.setMaxAge(secsToLive); 442 cookie.setPath("/"); 443 response.addCookie(cookie); 444 } 445 } 446 447 456 public static String retrieve( HttpServletRequest request, HttpServletResponse response, String id ) { 457 return( retrieve( request,response,id,false ) ); 459 } 460 461 471 public static String retrieve( HttpServletRequest request, 472 HttpServletResponse response, String id, boolean remove ) 473 { 474 HttpSession session = request.getSession(); 476 String value = (String )session.getAttribute(id); 477 478 if( value == null ) { 480 value = getCookieValue(request,id); 481 } 482 483 if( remove ) { 485 remove( request,response,id ); 486 } 487 488 return value; 489 } 490 491 499 public static void remove( HttpServletRequest request, HttpServletResponse response, String id ) { 500 HttpSession session = request.getSession(); 502 session.removeAttribute(id); 503 504 Cookie cookie = new Cookie( id, null ); 506 cookie.setMaxAge(0); 507 cookie.setPath("/"); 508 response.addCookie(cookie); 509 } 510 511 519 public static long getLastVisited(HttpServletRequest request, 520 HttpServletResponse response) 521 { 522 return getLastVisited(request,response,true); 523 } 524 525 534 public static long getLastVisited(HttpServletRequest request, 535 HttpServletResponse response, boolean updateLastVisitedTime) 536 { 537 HttpSession session = request.getSession(); 539 540 long now = System.currentTimeMillis(); 542 543 String lastTime = (String )session.getAttribute(YAZD_LASTVISITED_TOKEN); 545 546 if(lastTime != null) { 548 try { 549 long time = Long.parseLong(lastTime); 550 Cookie cookie = new Cookie(YAZD_LASTVISITED_TOKEN, Long.toString(now)); 553 cookie.setMaxAge(60*60*24*30); 554 cookie.setPath("/"); 555 response.addCookie(cookie); 556 return time; 558 } 559 catch(NumberFormatException e) { 560 e.printStackTrace(); 561 } 562 } 563 564 long time = now; 567 lastTime = getCookieValue(request,YAZD_LASTVISITED_TOKEN); 568 if( lastTime != null ) { 569 try { 570 time = Long.parseLong(lastTime); 571 } catch( NumberFormatException e ) {} 572 } 573 574 session.setAttribute(YAZD_LASTVISITED_TOKEN, Long.toString(time)); 576 Cookie cookie = new Cookie(YAZD_LASTVISITED_TOKEN, Long.toString(now)); 577 cookie.setMaxAge(60*60*24*30); 578 cookie.setPath("/"); 579 response.addCookie(cookie); 580 581 return time; 582 } 583 584 593 public static boolean isNewMessage(ForumMessage message, long lastVisited) 594 { 595 if (message.getModifiedDate().getTime() > lastVisited) { 596 return true; 597 } 598 else { 599 return false; 600 } 601 } 602 603 611 public static Cookie getCookie( HttpServletRequest request, String name ) { 612 Cookie cookies[] = request.getCookies(); 613 if(cookies == null || name == null || name.length() == 0) { 614 return null; 615 } 616 for( int i = 0; i < cookies.length; i++ ) { 618 if(cookies[i].getName().equals(name) ) { 619 return cookies[i]; 620 } 621 } 622 return null; 623 } 624 625 634 public static String getCookieValue(HttpServletRequest request, String name) { 635 Cookie cookie = getCookie(request,name); 636 if(cookie != null) { 637 return cookie.getValue(); 638 } 639 return null; 640 } 641 642 664 public static String quoteOriginal(String body, String delimiter, 665 int lineLength) 666 { 667 if (body == null || body.length() == 0) { 668 return ""; 669 } 670 int length = body.length(); 671 StringBuffer buf = new StringBuffer (body.length()); 673 for (int i=0; i<length; ) { 675 String partialString = 676 StringUtils.chopAtWord( 677 body.substring(i), 678 lineLength 679 ); 680 i += partialString.length()+1; 682 buf.append(delimiter).append(partialString.trim()).append("\\n"); 683 } 684 return buf.toString(); 685 } 686 687 696 public static String dateToText( Date date,Locale locale,TimeZone timezone ) { 697 if( date == null ) { 698 return ""; 699 } 700 701 long delta = System.currentTimeMillis() - date.getTime(); 702 703 if( (delta / HOUR) < 1 ) { 705 long minutes = (delta/MINUTE); 706 if( minutes == 0 ) { 707 return YazdLocale.getLocaleKey("Less_than_1_min_ago",locale); 708 } 709 else if( minutes == 1 ) { 710 return YazdLocale.getLocaleKey("1_minute_ago",locale); 711 } 712 else { 713 return ( minutes +" "+ YazdLocale.getLocaleKey("minutes_ago",locale) ); 714 } 715 } 716 717 if( (delta / DAY) < 1 ) { 719 long hours = (delta/HOUR); 720 if( hours <= 1 ) { 721 return YazdLocale.getLocaleKey("1_hour_ago",locale); 722 } 723 else { 724 return ( hours + " "+YazdLocale.getLocaleKey("hours_ago",locale) ); 725 } 726 } 727 728 if( (delta / WEEK) < 1 ) { 730 double days = ((double)delta/(double)DAY); 731 if( days <= 1.0 ) { 732 SimpleDateFormat yestFormatter = new SimpleDateFormat ("'"+YazdLocale.getLocaleKey("Yesterday_at",locale)+"' h:mm a",locale); 733 return yestFormatter.format(date); 734 } 735 else { 736 SimpleDateFormat dateFormatter = new SimpleDateFormat ("EEEE, MMM d '"+YazdLocale.getLocaleKey("at",locale)+"' h:mm a",locale); 737 dateFormatter.setTimeZone(timezone); 738 return dateFormatter.format(date); 739 } 740 } 741 742 else { 744 SimpleDateFormat dateFormatter = new SimpleDateFormat ("EEEE, MMM d '"+YazdLocale.getLocaleKey("at",locale)+"' h:mm a",locale); 745 dateFormatter.setTimeZone(timezone); 746 return dateFormatter.format(date); 747 } 748 } 749 750 756 public static boolean isSystemAdmin( Authorization authToken ) { 757 ForumFactory forumFactory = ForumFactory.getInstance(authToken); 758 ForumPermissions permissions = forumFactory.getPermissions(authToken); 759 return permissions.get(ForumPermissions.SYSTEM_ADMIN); 760 } 761 762 775 public static boolean isForumAdmin( Authorization authToken ) { 776 ForumFactory forumFactory = ForumFactory.getInstance(authToken); 777 Iterator forumIterator = forumFactory.forums(); 778 if( !forumIterator.hasNext() ) { 779 return false; 780 } 781 while( forumIterator.hasNext() ) { 782 Forum forum = (Forum)forumIterator.next(); 783 if( forum.hasPermission(ForumPermissions.FORUM_ADMIN) ) { 784 return true; 785 } 786 } 787 return false; 788 } 789 790 803 public static boolean isForumModerator( Authorization authToken ) { 804 ForumFactory forumFactory = ForumFactory.getInstance(authToken); 805 Iterator forumIterator = forumFactory.forums(); 806 if( !forumIterator.hasNext() ) { 807 return false; 808 } 809 while( forumIterator.hasNext() ) { 810 Forum forum = (Forum)forumIterator.next(); 811 if( forum.hasPermission(ForumPermissions.MODERATOR) ) { 812 return true; 813 } 814 } 815 return false; 816 } 817 818 825 public static boolean isForumAdmin( Authorization authToken, Forum forum ) { 826 return( forum.hasPermission(ForumPermissions.FORUM_ADMIN) ); 827 } 828 835 public static boolean isForumModerator( Authorization authToken, Forum forum ) { 836 return( forum.hasPermission(ForumPermissions.MODERATOR) ); 837 } 838 839 850 public static boolean isGroupAdmin( Authorization authToken ) { 851 ForumFactory forumFactory = ForumFactory.getInstance(authToken); 852 ProfileManager manager = forumFactory.getProfileManager(); 853 Iterator groupIterator = manager.groups(); 854 if( !groupIterator.hasNext() ) { 855 return false; 856 } 857 while( groupIterator.hasNext() ) { 858 Group group = (Group)groupIterator.next(); 859 if( group.hasPermission(ForumPermissions.GROUP_ADMIN) ) { 860 return true; 861 } 862 } 863 return false; 864 } 865 866 873 public static boolean isGroupAdmin( Authorization authToken, Group group ) { 874 return( group.hasPermission(ForumPermissions.GROUP_ADMIN) ); 875 } 876 877 889 private static String encodePasswordCookie (String username, String password) 890 { 891 StringBuffer buf = new StringBuffer (); 892 if (username != null && password != null) { 893 byte[] bytes = (username + ENCODE_DELIMETER + password).getBytes(); 894 int b; 895 896 for (int n = 0; n < bytes.length; n++) { 897 b = bytes[n] ^ (ENCODE_XORMASK + n); 898 buf.append((char)(ENCODE_CHAR_OFFSET1 + (b & 0x0F))); 899 buf.append((char)(ENCODE_CHAR_OFFSET2 + ((b >> 4) & 0x0F))); 900 } 901 } 902 return buf.toString(); 903 } 904 905 912 private static String [] decodePasswordCookie( String cookieVal ) { 913 914 if( cookieVal == null || cookieVal.length() <= 0 ) { 916 return null; 917 } 918 919 char[] chars = cookieVal.toCharArray(); 921 byte[] bytes = new byte[chars.length / 2]; 922 int b; 923 for (int n = 0, m = 0; n < bytes.length; n++) { 924 b = chars[m++] - ENCODE_CHAR_OFFSET1; 925 b |= (chars[m++] - ENCODE_CHAR_OFFSET2) << 4; 926 bytes[n] = (byte)(b ^ (ENCODE_XORMASK + n)); 927 } 928 cookieVal = new String (bytes); 929 int pos = cookieVal.indexOf(ENCODE_DELIMETER); 930 String username = (pos < 0) ? "" : cookieVal.substring(0, pos); 931 String password = (pos < 0) ? "" : cookieVal.substring(pos + 1); 932 933 return new String [] { username, password }; 934 } 935 936 939 949 private static void UpdateSession(String sID,String ip, int uid){ 950 951 } 952 953 } 954 | Popular Tags |