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: * This file creation date: 17/01/2004 / 19:34:01
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.admin;
044:
045: import java.io.ByteArrayOutputStream;
046: import java.io.InputStream;
047: import java.io.OutputStream;
048: import java.net.URL;
049: import java.net.URLConnection;
050:
051: import net.jforum.Command;
052: import net.jforum.JForumExecutionContext;
053: import net.jforum.SessionFacade;
054: import net.jforum.api.integration.mail.pop.POPListener;
055: import net.jforum.context.RequestContext;
056: import net.jforum.context.ResponseContext;
057: import net.jforum.dao.DataAccessDriver;
058: import net.jforum.dao.ForumDAO;
059: import net.jforum.entities.UserSession;
060: import net.jforum.repository.ModulesRepository;
061: import net.jforum.repository.SecurityRepository;
062: import net.jforum.security.PermissionControl;
063: import net.jforum.security.SecurityConstants;
064: import net.jforum.util.preferences.ConfigKeys;
065: import net.jforum.util.preferences.SystemGlobals;
066: import net.jforum.util.preferences.TemplateKeys;
067: import freemarker.template.SimpleHash;
068: import freemarker.template.Template;
069:
070: /**
071: * @author Rafael Steil
072: * @version $Id: AdminAction.java,v 1.23 2007/02/25 13:48:34 rafaelsteil Exp $
073: */
074: public class AdminAction extends Command {
075:
076: /**
077: * @see net.jforum.Command#list()
078: */
079: public void list() {
080: this .login();
081: }
082:
083: public void login() {
084: UserSession us = SessionFacade.getUserSession();
085: PermissionControl pc = SecurityRepository.get(us.getUserId());
086:
087: if (!SessionFacade.isLogged() || pc == null
088: || !pc.canAccess(SecurityConstants.PERM_ADMINISTRATION)) {
089: String returnPath = this .request.getContextPath()
090: + "/admBase/login"
091: + SystemGlobals
092: .getValue(ConfigKeys.SERVLET_EXTENSION);
093:
094: JForumExecutionContext.setRedirect(this .request
095: .getContextPath()
096: + "/jforum"
097: + SystemGlobals
098: .getValue(ConfigKeys.SERVLET_EXTENSION)
099: + "?module=user&action=login&returnPath="
100: + returnPath);
101: } else {
102: this .setTemplateName(TemplateKeys.ADMIN_INDEX);
103: }
104: }
105:
106: public void menu() {
107: if (this .checkAdmin()) {
108: this .setTemplateName(TemplateKeys.ADMIN_MENU);
109: }
110: }
111:
112: public void main() throws Exception {
113: if (this .checkAdmin()) {
114: this .setTemplateName(TemplateKeys.ADMIN_MAIN);
115:
116: // Checks if the install module is still active
117: this .context.put("installModuleExists", ModulesRepository
118: .getModuleClass("install") != null);
119: this .context
120: .put("sessions", SessionFacade.getAllSessions());
121:
122: ForumDAO dao = DataAccessDriver.getInstance().newForumDAO();
123: this .context.put("stats", dao.getBoardStatus());
124:
125: this .checkBoardVersion();
126: }
127: }
128:
129: public void fetchMail() throws Exception {
130: new Thread(new Runnable() {
131: public void run() {
132: try {
133: new POPListener().execute(null);
134: } catch (Exception e) {
135: e.printStackTrace();
136: }
137: }
138: }).start();
139:
140: this .main();
141: }
142:
143: private void checkBoardVersion() {
144: String data = this .readVersionFromSocket();
145:
146: if (data == null || data.trim().length() == 0) {
147: this .context.put("developmentVersion", false);
148: return;
149: }
150:
151: int index = data.indexOf('\n');
152:
153: String version = data.substring(0, index).trim();
154: String notes = data.substring(index + 1, data.length());
155:
156: this .matchVersion(version);
157: this .context.put("notes", notes);
158: }
159:
160: private void matchVersion(String latest) {
161: String current = SystemGlobals.getValue(ConfigKeys.VERSION);
162:
163: String[] currentParts = current.split("\\.");
164: String[] latestParts = latest.split("\\.");
165:
166: if (currentParts[2].indexOf('-') > -1) {
167: currentParts[2] = currentParts[2].substring(0,
168: currentParts[2].indexOf('-'));
169: }
170:
171: if (Integer.parseInt(latestParts[2]) > Integer
172: .parseInt(currentParts[2]) // Revision
173: || Integer.parseInt(latestParts[1]) > Integer
174: .parseInt(currentParts[1]) // Minor
175: || Integer.parseInt(latestParts[0]) > Integer
176: .parseInt(currentParts[0])) { // Major
177: this .context.put("upToDate", false);
178: } else {
179: this .context.put("upToDate", true);
180: }
181:
182: this .context.put("latestVersion", latest);
183: this .context.put("currentVersion", current);
184: this .context.put("developmentVersion",
185: current.indexOf("-dev") > -1);
186: }
187:
188: private String readVersionFromSocket() {
189: InputStream is = null;
190: OutputStream os = null;
191:
192: String data = null;
193:
194: try {
195: URL url = new URL(SystemGlobals
196: .getValue(ConfigKeys.JFORUM_VERSION_URL));
197: URLConnection conn = url.openConnection();
198:
199: is = conn.getInputStream();
200: os = new ByteArrayOutputStream();
201:
202: int available = is.available();
203:
204: while (available > 0) {
205: byte[] b = new byte[available];
206: is.read(b);
207: os.write(b);
208:
209: available = is.available();
210: }
211:
212: data = os.toString();
213: } catch (Exception e) {
214: e.printStackTrace();
215: } finally {
216: if (is != null) {
217: try {
218: is.close();
219: } catch (Exception e) {
220: }
221: }
222:
223: if (os != null) {
224: try {
225: os.close();
226: } catch (Exception e) {
227: }
228: }
229: }
230:
231: return data;
232: }
233:
234: public boolean checkAdmin() {
235: int userId = SessionFacade.getUserSession().getUserId();
236:
237: if (SecurityRepository.get(userId).canAccess(
238: SecurityConstants.PERM_ADMINISTRATION)) {
239: return true;
240: }
241:
242: JForumExecutionContext.setRedirect(JForumExecutionContext
243: .getRequest().getContextPath()
244: + "/admBase/login"
245: + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
246:
247: super .ignoreAction();
248:
249: return false;
250: }
251:
252: public Template process(RequestContext request,
253: ResponseContext response, SimpleHash context) {
254: return super.process(request, response, context);
255: }
256: }
|