KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > view > forum > common > UserCommon


1 /*
2  * Copyright (c) 2003, 2004 Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * Created on 29/11/2004 23:07:10
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.forum.common;
44
45 import java.awt.image.BufferedImage JavaDoc;
46 import java.io.File JavaDoc;
47 import java.io.FileNotFoundException JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.security.NoSuchAlgorithmException JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import java.util.List JavaDoc;
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 /**
70  * @author Rafael Steil
71  * @version $Id: UserCommon.java,v 1.13 2006/02/12 17:25:58 rafaelsteil Exp $
72  */

73 public class UserCommon
74 {
75     private static final Logger logger = Logger.getLogger(UserCommon.class);
76
77     /**
78      * Updates the user information
79      *
80      * @param userId The user id we are saving
81      * @throws Exception
82      */

83     public static List JavaDoc saveUser(int userId) throws Exception JavaDoc
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 JavaDoc 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 JavaDoc 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 JavaDoc f = new File JavaDoc(SystemGlobals.getApplicationPath() + "/images/avatar/"+ u.getAvatar());
129             f.delete();
130             
131             u.setAvatar(null);
132         }
133     
134         List JavaDoc warns = new ArrayList JavaDoc();
135         if (request.getObjectParameter("avatar") != null) {
136             try {
137                 UserCommon.handleAvatar(u);
138             }
139             catch (Exception JavaDoc e) {
140                 UserCommon.logger.warn("Problems while uploading the avatar: " + e);
141                 warns.add(I18n.getMessage("User.avatarUploadError"));
142             }
143         }
144         else {
145             String JavaDoc 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     /**
164      * @param u
165      * @throws NoSuchAlgorithmException
166      * @throws FileNotFoundException
167      * @throws IOException
168      */

169     private static void handleAvatar(User u) throws Exception JavaDoc
170     {
171         String JavaDoc fileName = MD5.crypt(Integer.toString(u.getId()));
172         FileItem item = (FileItem)JForumExecutionContext.getRequest().getObjectParameter("avatar");
173         UploadUtils uploadUtils = new UploadUtils(item);
174         
175         // Gets file extension
176
String JavaDoc 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 JavaDoc avatarTmpFileName = SystemGlobals.getApplicationPath()
188                 + "/images/avatar/" + fileName + "_tmp." + extension;
189             
190             // We cannot handle gifs
191
if (extension.toLowerCase().equals("gif")) {
192                 extension = "png";
193             }
194     
195             String JavaDoc avatarFinalFileName = SystemGlobals.getApplicationPath() +
196                 "/images/avatar/" + fileName + "." + extension;
197     
198             uploadUtils.saveUploadedFile(avatarTmpFileName);
199             
200             // OK, time to check and process the avatar size
201
int maxWidth = SystemGlobals.getIntValue(ConfigKeys.AVATAR_MAX_WIDTH);
202             int maxHeight = SystemGlobals.getIntValue(ConfigKeys.AVATAR_MAX_HEIGHT);
203     
204             BufferedImage JavaDoc image = ImageUtils.resizeImage(avatarTmpFileName, type, maxWidth, maxHeight);
205             ImageUtils.saveImage(image, avatarFinalFileName, type);
206     
207             u.setAvatar(fileName + "." + extension);
208             
209             // Delete the temporary file
210
new File JavaDoc(avatarTmpFileName).delete();
211         }
212     }
213 }
214
Popular Tags