1 43 package net.jforum.view.admin; 44 45 46 import java.io.ByteArrayOutputStream ; 47 import java.io.InputStream ; 48 import java.io.OutputStream ; 49 import java.net.URL ; 50 import java.net.URLConnection ; 51 52 import javax.servlet.http.HttpServletResponse ; 53 54 import net.jforum.ActionServletRequest; 55 import net.jforum.Command; 56 import net.jforum.JForumExecutionContext; 57 import net.jforum.SessionFacade; 58 import net.jforum.dao.DataAccessDriver; 59 import net.jforum.dao.ForumDAO; 60 import net.jforum.entities.UserSession; 61 import net.jforum.repository.ModulesRepository; 62 import net.jforum.repository.SecurityRepository; 63 import net.jforum.security.PermissionControl; 64 import net.jforum.security.SecurityConstants; 65 import net.jforum.util.preferences.ConfigKeys; 66 import net.jforum.util.preferences.SystemGlobals; 67 import net.jforum.util.preferences.TemplateKeys; 68 import freemarker.template.SimpleHash; 69 import freemarker.template.Template; 70 71 75 public class AdminAction extends Command { 76 77 80 public void list() throws Exception 81 { 82 this.login(); 83 } 84 85 public void login() throws Exception 86 { 87 String logged = (String )SessionFacade.getAttribute("logged"); 88 UserSession us = SessionFacade.getUserSession(); 89 90 PermissionControl pc = SecurityRepository.get(us.getUserId()); 91 92 if (logged == null || logged.toString().equals("0") 93 || pc == null || !pc.canAccess(SecurityConstants.PERM_ADMINISTRATION)) { 94 String returnPath = this.request.getContextPath() + "/admBase/login" 95 + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION); 96 97 JForumExecutionContext.setRedirect(this.request.getContextPath() 98 + "/jforum" 99 + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) 100 + "?module=user&action=login&returnPath=" 101 + returnPath); 102 } 103 else { 104 this.setTemplateName(TemplateKeys.ADMIN_INDEX); 105 } 106 } 107 108 public void menu() throws Exception 109 { 110 this.setTemplateName(TemplateKeys.ADMIN_MENU); 111 } 112 113 public void main() throws Exception 114 { 115 this.setTemplateName(TemplateKeys.ADMIN_MAIN); 116 117 this.context.put("installModuleExists", ModulesRepository.getModuleClass("install") != null); 119 this.context.put("sessions", SessionFacade.getAllSessions()); 120 121 ForumDAO dao = DataAccessDriver.getInstance().newForumDAO(); 122 this.context.put("stats", dao.getBoardStatus()); 123 124 this.checkBoardVersion(); 125 } 126 127 private void checkBoardVersion() 128 { 129 String data = this.readVersionFromSocket(); 130 131 if (data == null || data.trim().length() == 0) { 132 return; 133 } 134 135 int index = data.indexOf('\n'); 136 137 String version = data.substring(0, index).trim(); 138 String notes = data.substring(index + 1, data.length()); 139 140 this.matchVersion(version); 141 this.context.put("notes", notes); 142 } 143 144 private void matchVersion(String latest) 145 { 146 String current = SystemGlobals.getValue(ConfigKeys.VERSION); 147 148 String [] currentParts = current.split("\\."); 149 String [] latestParts = latest.split("\\."); 150 151 if (Integer.parseInt(latestParts[2]) > Integer.parseInt(currentParts[2]) || Integer.parseInt(latestParts[1]) > Integer.parseInt(currentParts[1]) || Integer.parseInt(latestParts[0]) > Integer.parseInt(currentParts[0])) { this.context.put("upToDate", false); 155 } 156 else { 157 this.context.put("upToDate", true); 158 } 159 160 this.context.put("latestVersion", latest); 161 this.context.put("currentVersion", current); 162 } 163 164 private String readVersionFromSocket() 165 { 166 InputStream is = null; 167 OutputStream os = null; 168 169 String data = null; 170 171 try { 172 URL url = new URL (SystemGlobals.getValue(ConfigKeys.JFORUM_VERSION_URL)); 173 URLConnection conn = url.openConnection(); 174 175 is = conn.getInputStream(); 176 os = new ByteArrayOutputStream (); 177 178 int available = is.available(); 179 180 while (available > 0) { 181 byte[] b = new byte[available]; 182 is.read(b); 183 os.write(b); 184 185 available = is.available(); 186 } 187 188 data = os.toString(); 189 } 190 catch (Exception e) { 191 e.printStackTrace(); 192 } 193 finally { 194 if (is != null) { 195 try { is.close(); os.close(); } catch (Exception e) {} 196 } 197 } 198 199 return data; 200 } 201 202 public boolean checkAdmin() 203 { 204 int userId = SessionFacade.getUserSession().getUserId(); 205 206 if (SecurityRepository.get(userId).canAccess(SecurityConstants.PERM_ADMINISTRATION)) { 207 return true; 208 } 209 210 JForumExecutionContext.setRedirect(JForumExecutionContext.getRequest().getContextPath() 211 + "/admBase/login" 212 + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION)); 213 214 super.ignoreAction(); 215 216 return false; 217 } 218 219 public Template process(ActionServletRequest request, 220 HttpServletResponse response, 221 SimpleHash context) throws Exception 222 { 223 return super.process(request, response, context); 224 } 225 } 226 | Popular Tags |