1 43 package net.jforum.view.forum.common; 44 45 import java.awt.image.BufferedImage ; 46 import java.io.File ; 47 import java.io.FileNotFoundException ; 48 import java.io.IOException ; 49 import java.security.NoSuchAlgorithmException ; 50 import java.util.ArrayList ; 51 import java.util.List ; 52 53 import net.jforum.ActionServletRequest; 54 import net.jforum.JForumExecutionContext; 55 import net.jforum.SessionFacade; 56 import net.jforum.dao.DataAccessDriver; 57 import net.jforum.dao.UserDAO; 58 import net.jforum.entities.User; 59 import net.jforum.util.I18n; 60 import net.jforum.util.MD5; 61 import net.jforum.util.SafeHtml; 62 import net.jforum.util.image.ImageUtils; 63 import net.jforum.util.legacy.commons.fileupload.FileItem; 64 import net.jforum.util.preferences.ConfigKeys; 65 import net.jforum.util.preferences.SystemGlobals; 66 67 import org.apache.log4j.Logger; 68 69 73 public class UserCommon 74 { 75 private static final Logger logger = Logger.getLogger(UserCommon.class); 76 77 83 public static List saveUser(int userId) throws Exception 84 { 85 UserDAO um = DataAccessDriver.getInstance().newUserDAO(); 86 User u = um.selectById(userId); 87 88 ActionServletRequest request = JForumExecutionContext.getRequest(); 89 if (SessionFacade.getUserSession().isAdmin()) { 90 String username = request.getParameter("username"); 91 92 if (username != null) { 93 u.setUsername(username.trim()); 94 } 95 } 96 97 u.setId(userId); 98 u.setEmail(SafeHtml.makeSafe(request.getParameter("email"))); 99 u.setIcq(SafeHtml.makeSafe(request.getParameter("icq"))); 100 u.setAim(SafeHtml.makeSafe(request.getParameter("aim"))); 101 u.setMsnm(SafeHtml.makeSafe(request.getParameter("msn"))); 102 u.setYim(SafeHtml.makeSafe(request.getParameter("yim"))); 103 u.setFrom(SafeHtml.makeSafe(request.getParameter("location"))); 104 u.setOccupation(SafeHtml.makeSafe(request.getParameter("occupation"))); 105 u.setInterests(SafeHtml.makeSafe(request.getParameter("interests"))); 106 u.setBiography(SafeHtml.makeSafe(request.getParameter("biography"))); 107 u.setSignature(SafeHtml.makeSafe(request.getParameter("signature"))); 108 u.setViewEmailEnabled(request.getParameter("viewemail").equals("1")); 109 u.setViewOnlineEnabled(request.getParameter("hideonline").equals("0")); 110 u.setNotifyPrivateMessagesEnabled(request.getParameter("notifypm").equals("1")); 111 u.setNotifyOnMessagesEnabled(request.getParameter("notifyreply").equals("1")); 112 u.setAttachSignatureEnabled(request.getParameter("attachsig").equals("1")); 113 u.setHtmlEnabled(request.getParameter("allowhtml").equals("1")); 114 u.setLang(request.getParameter("language")); 115 116 String website = SafeHtml.makeSafe(request.getParameter("website")); 117 if (website != null && !"".equals(website.trim()) && !website.toLowerCase().startsWith("http://")) { 118 website = "http://" + website; 119 } 120 121 u.setWebSite(website); 122 123 if (request.getParameter("new_password") != null && request.getParameter("new_password").length() > 0) { 124 u.setPassword(MD5.crypt(request.getParameter("new_password"))); 125 } 126 127 if (request.getParameter("avatardel") != null) { 128 File f = new File (SystemGlobals.getApplicationPath() + "/images/avatar/"+ u.getAvatar()); 129 f.delete(); 130 131 u.setAvatar(null); 132 } 133 134 List warns = new ArrayList (); 135 if (request.getObjectParameter("avatar") != null) { 136 try { 137 UserCommon.handleAvatar(u); 138 } 139 catch (Exception e) { 140 UserCommon.logger.warn("Problems while uploading the avatar: " + e); 141 warns.add(I18n.getMessage("User.avatarUploadError")); 142 } 143 } 144 else { 145 String avatarUrl = request.getParameter("avatarUrl"); 146 if (avatarUrl != null && !"".equals(avatarUrl.trim())) { 147 if (avatarUrl.toLowerCase().startsWith("http://")) { 148 u.setAvatar(avatarUrl); 149 } 150 else { 151 warns.add(I18n.getMessage("User.avatarUrlShouldHaveHttp")); 152 } 153 } 154 } 155 156 um.update(u); 157 158 SessionFacade.getUserSession().setLang(u.getLang()); 159 160 return warns; 161 } 162 163 169 private static void handleAvatar(User u) throws Exception 170 { 171 String fileName = MD5.crypt(Integer.toString(u.getId())); 172 FileItem item = (FileItem)JForumExecutionContext.getRequest().getObjectParameter("avatar"); 173 UploadUtils uploadUtils = new UploadUtils(item); 174 175 String extension = uploadUtils.getExtension().toLowerCase(); 177 int type = ImageUtils.IMAGE_UNKNOWN; 178 179 if (extension.equals("jpg") || extension.equals("jpeg")) { 180 type = ImageUtils.IMAGE_JPEG; 181 } 182 else if (extension.equals("gif") || extension.equals("png")) { 183 type = ImageUtils.IMAGE_PNG; 184 } 185 186 if (type != ImageUtils.IMAGE_UNKNOWN) { 187 String avatarTmpFileName = SystemGlobals.getApplicationPath() 188 + "/images/avatar/" + fileName + "_tmp." + extension; 189 190 if (extension.toLowerCase().equals("gif")) { 192 extension = "png"; 193 } 194 195 String avatarFinalFileName = SystemGlobals.getApplicationPath() + 196 "/images/avatar/" + fileName + "." + extension; 197 198 uploadUtils.saveUploadedFile(avatarTmpFileName); 199 200 int maxWidth = SystemGlobals.getIntValue(ConfigKeys.AVATAR_MAX_WIDTH); 202 int maxHeight = SystemGlobals.getIntValue(ConfigKeys.AVATAR_MAX_HEIGHT); 203 204 BufferedImage image = ImageUtils.resizeImage(avatarTmpFileName, type, maxWidth, maxHeight); 205 ImageUtils.saveImage(image, avatarFinalFileName, type); 206 207 u.setAvatar(fileName + "." + extension); 208 209 new File (avatarTmpFileName).delete(); 211 } 212 } 213 } 214 | Popular Tags |