001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 29/11/2004 23:07:10
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.forum.common;
044:
045: import java.awt.image.BufferedImage;
046: import java.io.File;
047: import java.util.ArrayList;
048: import java.util.List;
049:
050: import net.jforum.JForumExecutionContext;
051: import net.jforum.SessionFacade;
052: import net.jforum.context.RequestContext;
053: import net.jforum.dao.DataAccessDriver;
054: import net.jforum.dao.UserDAO;
055: import net.jforum.entities.User;
056: import net.jforum.util.I18n;
057: import net.jforum.util.MD5;
058: import net.jforum.util.SafeHtml;
059: import net.jforum.util.image.ImageUtils;
060: import net.jforum.util.legacy.commons.fileupload.FileItem;
061: import net.jforum.util.preferences.ConfigKeys;
062: import net.jforum.util.preferences.SystemGlobals;
063:
064: import org.apache.commons.lang.StringUtils;
065: import org.apache.log4j.Logger;
066:
067: /**
068: * @author Rafael Steil
069: * @version $Id: UserCommon.java,v 1.29 2007/09/19 14:08:57 rafaelsteil Exp $
070: */
071: public class UserCommon {
072: private static final Logger logger = Logger
073: .getLogger(UserCommon.class);
074:
075: /**
076: * Updates the user information
077: *
078: * @param userId int The user id we are saving
079: * @return List
080: */
081: public static List saveUser(int userId) {
082: List errors = new ArrayList();
083:
084: UserDAO um = DataAccessDriver.getInstance().newUserDAO();
085: User u = um.selectById(userId);
086:
087: RequestContext request = JForumExecutionContext.getRequest();
088: boolean isAdmin = SessionFacade.getUserSession().isAdmin();
089:
090: if (isAdmin) {
091: String username = request.getParameter("username");
092:
093: if (username != null) {
094: u.setUsername(username.trim());
095: }
096:
097: if (request.getParameter("rank_special") != null) {
098: u.setRankId(request.getIntParameter("rank_special"));
099: }
100: }
101:
102: SafeHtml safeHtml = new SafeHtml();
103:
104: u.setId(userId);
105: u.setIcq(safeHtml.makeSafe(request.getParameter("icq")));
106: u.setAim(safeHtml.makeSafe(request.getParameter("aim")));
107: u.setMsnm(safeHtml.makeSafe(request.getParameter("msn")));
108: u.setYim(safeHtml.makeSafe(request.getParameter("yim")));
109: u.setFrom(safeHtml.makeSafe(request.getParameter("location")));
110: u.setOccupation(safeHtml.makeSafe(request
111: .getParameter("occupation")));
112: u.setInterests(safeHtml.makeSafe(request
113: .getParameter("interests")));
114: u.setBiography(safeHtml.makeSafe(request
115: .getParameter("biography")));
116: u.setSignature(safeHtml.makeSafe(request
117: .getParameter("signature")));
118: u.setViewEmailEnabled(request.getParameter("viewemail").equals(
119: "1"));
120: u.setViewOnlineEnabled(request.getParameter("hideonline")
121: .equals("0"));
122: u.setNotifyPrivateMessagesEnabled(request.getParameter(
123: "notifypm").equals("1"));
124: u.setNotifyOnMessagesEnabled(request
125: .getParameter("notifyreply").equals("1"));
126: u.setAttachSignatureEnabled(request.getParameter("attachsig")
127: .equals("1"));
128: u.setHtmlEnabled(request.getParameter("allowhtml").equals("1"));
129: u.setLang(request.getParameter("language"));
130: u.setBbCodeEnabled("1".equals(request
131: .getParameter("allowbbcode")));
132: u.setSmiliesEnabled("1".equals(request
133: .getParameter("allowsmilies")));
134: u.setNotifyAlways("1".equals(request
135: .getParameter("notify_always")));
136: u
137: .setNotifyText("1".equals(request
138: .getParameter("notify_text")));
139:
140: String website = safeHtml.makeSafe(request
141: .getParameter("website"));
142: if (!StringUtils.isEmpty(website)
143: && !website.toLowerCase().startsWith("http://")) {
144: website = "http://" + website;
145: }
146:
147: u.setWebSite(website);
148:
149: String currentPassword = request
150: .getParameter("current_password");
151: boolean isCurrentPasswordEmpty = currentPassword == null
152: || "".equals(currentPassword.trim());
153:
154: if (isAdmin || !isCurrentPasswordEmpty) {
155: if (!isCurrentPasswordEmpty) {
156: currentPassword = MD5.crypt(currentPassword);
157: }
158:
159: if (isAdmin || u.getPassword().equals(currentPassword)) {
160: u.setEmail(safeHtml.makeSafe(request
161: .getParameter("email")));
162:
163: String newPassword = request
164: .getParameter("new_password");
165:
166: if (newPassword != null && newPassword.length() > 0) {
167: u.setPassword(MD5.crypt(newPassword));
168: }
169: } else {
170: errors.add(I18n
171: .getMessage("User.currentPasswordInvalid"));
172: }
173: }
174:
175: if (request.getParameter("avatardel") != null) {
176: File f = new File(SystemGlobals.getApplicationPath()
177: + "/images/avatar/" + u.getAvatar());
178: f.delete();
179:
180: u.setAvatar(null);
181: }
182:
183: if (request.getObjectParameter("avatar") != null) {
184: try {
185: UserCommon.handleAvatar(u);
186: } catch (Exception e) {
187: UserCommon.logger
188: .warn("Problems while uploading the avatar: "
189: + e);
190: errors.add(I18n.getMessage("User.avatarUploadError"));
191: }
192: } else if (SystemGlobals
193: .getBoolValue(ConfigKeys.AVATAR_ALLOW_EXTERNAL_URL)) {
194: String avatarUrl = request.getParameter("avatarUrl");
195: if (!StringUtils.isEmpty(avatarUrl)) {
196: if (avatarUrl.toLowerCase().startsWith("http://")) {
197: u.setAvatar(avatarUrl);
198: } else {
199: errors
200: .add(I18n
201: .getMessage("User.avatarUrlShouldHaveHttp"));
202: }
203: }
204: }
205:
206: if (errors.size() == 0) {
207: um.update(u);
208: }
209:
210: if (SessionFacade.getUserSession().getUserId() == userId) {
211: SessionFacade.getUserSession().setLang(u.getLang());
212: }
213: return errors;
214: }
215:
216: /**
217: * @param u User
218: */
219: private static void handleAvatar(User u) {
220: String fileName = MD5.crypt(Integer.toString(u.getId()));
221: FileItem item = (FileItem) JForumExecutionContext.getRequest()
222: .getObjectParameter("avatar");
223: UploadUtils uploadUtils = new UploadUtils(item);
224:
225: // Gets file extension
226: String extension = uploadUtils.getExtension().toLowerCase();
227: int type = ImageUtils.IMAGE_UNKNOWN;
228:
229: if (extension.equals("jpg") || extension.equals("jpeg")) {
230: type = ImageUtils.IMAGE_JPEG;
231: } else if (extension.equals("gif") || extension.equals("png")) {
232: type = ImageUtils.IMAGE_PNG;
233: }
234:
235: if (type != ImageUtils.IMAGE_UNKNOWN) {
236: String avatarTmpFileName = SystemGlobals
237: .getApplicationPath()
238: + "/images/avatar/"
239: + fileName
240: + "_tmp."
241: + extension;
242:
243: // We cannot handle gifs
244: if (extension.toLowerCase().equals("gif")) {
245: extension = "png";
246: }
247:
248: String avatarFinalFileName = SystemGlobals
249: .getApplicationPath()
250: + "/images/avatar/" + fileName + "." + extension;
251:
252: uploadUtils.saveUploadedFile(avatarTmpFileName);
253:
254: // OK, time to check and process the avatar size
255: int maxWidth = SystemGlobals
256: .getIntValue(ConfigKeys.AVATAR_MAX_WIDTH);
257: int maxHeight = SystemGlobals
258: .getIntValue(ConfigKeys.AVATAR_MAX_HEIGHT);
259:
260: BufferedImage image = ImageUtils.resizeImage(
261: avatarTmpFileName, type, maxWidth, maxHeight);
262: ImageUtils.saveImage(image, avatarFinalFileName, type);
263:
264: u.setAvatar(fileName + "." + extension);
265:
266: // Delete the temporary file
267: new File(avatarTmpFileName).delete();
268: }
269: }
270: }
|