1 13 18 package org.jahia.engines.template; 19 20 import java.io.File ; 21 import java.util.HashMap ; 22 23 import javax.servlet.http.HttpSession ; 24 25 import org.jahia.data.JahiaData; 26 import org.jahia.data.events.JahiaEvent; 27 import org.jahia.engines.EngineToolBox; 28 import org.jahia.engines.JahiaEngine; 29 import org.jahia.engines.audit.ManageLogs_Engine; 30 import org.jahia.engines.rights.ManageRights; 31 import org.jahia.exceptions.JahiaException; 32 import org.jahia.exceptions.JahiaForbiddenAccessException; 33 import org.jahia.exceptions.JahiaSessionExpirationException; 34 import org.jahia.params.ParamBean; 35 import org.jahia.registries.ServicesRegistry; 36 import org.jahia.services.acl.ACLResource; 37 import org.jahia.services.pages.JahiaPageDefinition; 38 import org.jahia.services.pages.JahiaPageDefinitionTemp; 39 import org.jahia.services.usermanager.JahiaUser; 40 import org.jahia.utils.JahiaObjectTool; 41 import org.jahia.utils.JahiaTools; 42 43 48 public class Template_Engine implements JahiaEngine { 49 50 private static final String TEMPLATE_JSP = "manage_template"; 51 public static final String TEMPLATE_SESSION_NAME = "theTemplate"; 52 53 private static final String TEMPORARY_TEMPLATE_SESSION_NAME = "theTemporaryTemplate"; 55 56 private static Template_Engine instance; 57 public static final String ENGINE_NAME = "template"; 58 private EngineToolBox toolBox; 59 60 private static final org.apache.log4j.Logger logger = 61 org.apache.log4j.Logger.getLogger (Template_Engine.class); 62 63 private final String WRITE_ACCESS_STR = "writeAccess"; 64 private final String ADMIN_ACCESS_STR = "adminAccess"; 65 66 private final String SCREEN_STR = "screen"; 67 68 private final String EDIT_STR = "edit"; 69 private final String SAVE_STR = "save"; 70 private final String CANCEL_STR = "cancel"; 71 private final String LOGS_STR = "logs"; 72 private final String APPLY_STR = "apply"; 73 private final String CLOSE_STR = "close"; 74 private final String LASTSCREEN_STR = "lastscreen"; 75 private final String JSPSOURCE_STR = "jspSource"; 76 77 78 81 private Template_Engine () { 82 logger.debug ("***** Starting " + Template_Engine.class.getName () + " engine *****"); 83 toolBox = EngineToolBox.getInstance (); 84 } 85 86 89 public static synchronized Template_Engine getInstance () { 90 if (instance == null) { 91 instance = new Template_Engine (); 92 } 93 return instance; 94 } 95 96 99 public boolean authoriseRender (ParamBean jParams) { 100 return true; } 102 103 106 public String renderLink (ParamBean jParams, Object theObj) 107 throws JahiaException { 108 JahiaPageDefinition theTemplate = (JahiaPageDefinition) theObj; 109 String params = "?mode=display&templateid=" + theTemplate.getID (); 110 111 return jParams.composeEngineUrl (ENGINE_NAME, params); 112 } 113 114 117 public boolean needsJahiaData (ParamBean jParams) { 118 return false; 119 } 120 121 127 public void handleActions (ParamBean jParams, JahiaData jData) 128 throws JahiaException, 129 JahiaSessionExpirationException, 130 JahiaForbiddenAccessException { 131 HashMap engineMap = initEngineMap (jData); 133 134 JahiaPageDefinition theTemplate = 136 (JahiaPageDefinition) engineMap.get (TEMPLATE_SESSION_NAME); 137 JahiaUser theUser = jParams.getUser (); 138 139 if (ACLResource.checkAdminAccess (theTemplate, theUser)) { 140 engineMap.put (ADMIN_ACCESS_STR, Boolean.TRUE); 141 engineMap.put ("enableRightView", Boolean.TRUE); 142 engineMap.put (WRITE_ACCESS_STR, Boolean.TRUE); 143 144 } else if (ACLResource.checkWriteAccess (theTemplate, theUser)) { 145 engineMap.put (WRITE_ACCESS_STR, Boolean.TRUE); 146 } 147 148 if (engineMap.get (WRITE_ACCESS_STR) != null) { 149 processLastScreen (jParams, engineMap); 150 processCurrentScreen (jParams, engineMap); 151 } else { 152 throw new JahiaForbiddenAccessException (); 153 } 154 155 toolBox.displayScreen (jParams, engineMap); 157 } 158 159 164 public final String getName () { 165 return ENGINE_NAME; 166 } 167 168 169 175 public void processLastScreen (ParamBean jParams, HashMap engineMap) 176 throws JahiaException, 177 JahiaForbiddenAccessException { 178 JahiaPageDefinition theTemplate = 179 (JahiaPageDefinition) engineMap.get (TEMPLATE_SESSION_NAME); 180 181 String lastScreen = jParams.getRequest ().getParameter (LASTSCREEN_STR); 184 if (lastScreen == null) { 185 lastScreen = EDIT_STR; 186 } 187 188 int mode = JahiaEngine.UPDATE_MODE; 189 190 if (lastScreen.equals (EDIT_STR)) { 191 if (!processTemplateEdit (jParams, mode, engineMap)) { 192 engineMap.put (SCREEN_STR, lastScreen); 194 engineMap.put (JSPSOURCE_STR, TEMPLATE_JSP); 195 } 196 } else if (lastScreen.equals ("rightsMgmt")) { 197 if (engineMap.get (ADMIN_ACCESS_STR) != null) { 198 ManageRights.getInstance () 199 .handleActions (jParams, mode, engineMap, theTemplate.getAclID ()); 200 } else { 201 throw new JahiaForbiddenAccessException (); 202 } 203 } else if (lastScreen.equals (LOGS_STR)) { 204 if (engineMap.get (ADMIN_ACCESS_STR) != null) { 205 ManageLogs_Engine.getInstance ().handleActions (jParams, mode, engineMap, null); 206 } else { 207 throw new JahiaForbiddenAccessException (); 208 } 209 } 210 } 211 212 213 218 public void processCurrentScreen (ParamBean jParams, HashMap engineMap) 219 throws JahiaException, 220 JahiaForbiddenAccessException { 221 String theScreen = (String ) engineMap.get (SCREEN_STR); 224 JahiaPageDefinition theTemplate = 225 (JahiaPageDefinition) engineMap.get (TEMPLATE_SESSION_NAME); 226 227 int mode = JahiaEngine.LOAD_MODE; 229 230 if (theScreen.equals (EDIT_STR)) { 232 processTemplateEdit (jParams, mode, engineMap); 233 } else if (theScreen.equals (LOGS_STR)) { 234 toolBox.loadLogData (jParams, JahiaObjectTool.TEMPLATE_TYPE, engineMap); 235 } else if (theScreen.equals ("rightsMgmt")) { 236 if (engineMap.get (ADMIN_ACCESS_STR) != null) { 237 ManageRights.getInstance () 238 .handleActions (jParams, mode, engineMap, theTemplate.getAclID ()); 239 } else { 240 throw new JahiaForbiddenAccessException (); 241 } 242 } else if (theScreen.equals (SAVE_STR) || theScreen.equals (APPLY_STR)) { 243 mode = JahiaEngine.SAVE_MODE; 244 if (processTemplateSave (jParams, engineMap)) { 245 if (engineMap.get (ADMIN_ACCESS_STR) != null) { 246 engineMap.put ("logObjectType", 247 Integer.toString (JahiaObjectTool.TEMPLATE_TYPE)); 248 engineMap.put ("logObject", theTemplate); 249 ManageRights.getInstance () 250 .handleActions (jParams, mode, engineMap, theTemplate.getAclID ()); 251 } 252 253 JahiaEvent theEvent = new JahiaEvent (this, jParams, theTemplate); 254 ServicesRegistry.getInstance ().getJahiaEventService ().fireUpdateTemplate ( 255 theEvent); 256 } else { 257 engineMap.put (SCREEN_STR, EDIT_STR); 259 engineMap.put (JSPSOURCE_STR, TEMPLATE_JSP); 260 } 261 if (theScreen.equals (APPLY_STR)) { 262 263 JahiaPageDefinitionTemp theTemporaryTemplate = (JahiaPageDefinitionTemp) 264 engineMap.get (TEMPORARY_TEMPLATE_SESSION_NAME); 265 266 String templateContext = jParams.settings ().getTemplatesContext () + 268 jParams.getSite ().getSiteKey () + 269 "/"; 270 if ((templateContext != null) 271 && 272 (theTemporaryTemplate.getSourcePath ().indexOf (templateContext) != -1)) { 273 theTemporaryTemplate.setSourcePath ( 274 theTemporaryTemplate.getSourcePath ().substring ( 275 templateContext.length (), 276 theTemporaryTemplate.getSourcePath ().length ())); 277 } 278 279 engineMap.put (SCREEN_STR, jParams.getRequest ().getParameter (LASTSCREEN_STR)); 280 } 281 logger.debug ("Saving !!"); 282 } else if (theScreen.equals (CANCEL_STR)) { 283 engineMap.put (ENGINE_OUTPUT_FILE_PARAM, JahiaEngine.CANCEL_JSP); 284 } 285 } 286 287 288 295 private HashMap initEngineMap (JahiaData jData) 296 throws JahiaException, 297 JahiaSessionExpirationException { 298 ParamBean jParams = jData.params (); 299 300 HashMap engineMap = new HashMap (); 301 JahiaPageDefinition theTemplate; 302 303 HttpSession theSession = jParams.getSession (); 305 306 String theScreen = jParams.getRequest ().getParameter (SCREEN_STR); 308 if (theScreen != null) { 309 engineMap = (HashMap ) theSession.getAttribute (ParamBean.SESSION_JAHIA_ENGINEMAP); 311 if (engineMap == null) { 312 throw new JahiaSessionExpirationException (); 313 } 314 theTemplate = (JahiaPageDefinition) engineMap.get (TEMPLATE_SESSION_NAME); 315 } else { 316 int templateID = -1; 318 String value = jParams.getRequest ().getParameter ("templateid"); 319 if (value != null) 320 templateID = Integer.parseInt (value); 321 322 theTemplate = ServicesRegistry.getInstance () 323 .getJahiaPageTemplateService () 324 .lookupPageTemplate (templateID); 325 326 theScreen = EDIT_STR; 327 328 JahiaPageDefinitionTemp theTemporaryTemplate = 330 new JahiaPageDefinitionTemp (theTemplate, 331 (jParams.getSite ().getDefaultTemplateID () == 332 theTemplate.getID ())); 333 334 String templateContext = jParams.settings ().getTemplatesContext () + 336 jParams.getSite ().getSiteKey () + 337 "/"; 338 if ((templateContext != null) 339 && 340 (theTemporaryTemplate.getSourcePath ().indexOf (templateContext) != -1)) { 341 theTemporaryTemplate.setSourcePath ( 342 theTemporaryTemplate.getSourcePath ().substring ( 343 templateContext.length (), 344 theTemporaryTemplate.getSourcePath ().length ())); 345 } 346 347 engineMap.put (TEMPLATE_SESSION_NAME, theTemplate); 349 engineMap.put (TEMPORARY_TEMPLATE_SESSION_NAME, theTemporaryTemplate); 350 } 351 352 engineMap.put (RENDER_TYPE_PARAM, new Integer (JahiaEngine.RENDERTYPE_FORWARD)); 353 engineMap.put (ENGINE_NAME_PARAM, ENGINE_NAME); 354 engineMap.put (ENGINE_URL_PARAM, 355 jParams.composeEngineUrl (ENGINE_NAME, "?templateid=" + theTemplate.getID ())); 356 357 theSession.setAttribute (ParamBean.SESSION_JAHIA_ENGINEMAP, engineMap); 358 359 engineMap.put (SCREEN_STR, theScreen); 361 if (theScreen.equals (SAVE_STR)) { 362 engineMap.put (JSPSOURCE_STR, CLOSE_STR); 363 } else if (theScreen.equals (APPLY_STR)) { 364 engineMap.put (JSPSOURCE_STR, APPLY_STR); 365 } else if (theScreen.equals (CANCEL_STR)) { 366 engineMap.put (JSPSOURCE_STR, CLOSE_STR); 367 } else { 368 engineMap.put (JSPSOURCE_STR, TEMPLATE_JSP); 369 } 370 371 jParams.getRequest ().setAttribute (ENGINE_NAME_PARAM, "Manage Templates"); 373 jParams.getRequest ().setAttribute ("org.jahia.engines.EngineHashMap", engineMap); 374 jParams.getRequest ().setAttribute ("Template_Engine.warningMsg", EMPTY_STRING); 375 return engineMap; 376 } 377 378 379 382 private boolean processTemplateEdit (ParamBean jParams, int mode, HashMap engineMap) { 383 384 if (mode == JahiaEngine.LOAD_MODE) { 385 return true; 387 } else if (mode == JahiaEngine.UPDATE_MODE) { 388 String lastScreen = jParams.getRequest ().getParameter (LASTSCREEN_STR); 390 if (lastScreen == null) 391 lastScreen = EMPTY_STRING; 392 393 if (lastScreen.equals (EDIT_STR)) { 394 JahiaPageDefinitionTemp theTemporaryTemplate = (JahiaPageDefinitionTemp) 396 engineMap.get (TEMPORARY_TEMPLATE_SESSION_NAME); 397 if (theTemporaryTemplate == null) 398 return false; 400 String value = jParams.getRequest ().getParameter ("templateName"); 402 if (value != null) 403 theTemporaryTemplate.setName (value); 404 405 value = jParams.getRequest ().getParameter ("sourcePath"); 407 if (value != null) 408 theTemporaryTemplate.setSourcePath (value); 409 410 value = jParams.getRequest ().getParameter ("templateAvailable"); 412 theTemporaryTemplate.setAvailable (value != null); 413 414 value = jParams.getRequest ().getParameter ("templateDefault"); 416 theTemporaryTemplate.setDefault (value != null); 417 } 418 return true; 419 } 420 return false; 421 } 422 423 424 427 private boolean processTemplateSave (ParamBean jParams, HashMap engineMap) 428 throws JahiaException { 429 430 StringBuffer warningMsg = new StringBuffer (EMPTY_STRING); 431 432 JahiaPageDefinitionTemp theTemporaryTemplate = (JahiaPageDefinitionTemp) 433 engineMap.get (TEMPORARY_TEMPLATE_SESSION_NAME); 434 435 JahiaPageDefinition theTemplate = (JahiaPageDefinition) 436 engineMap.get (TEMPLATE_SESSION_NAME); 437 438 if (theTemporaryTemplate.getName () == null 440 || theTemporaryTemplate.getName ().trim ().equals (EMPTY_STRING)) 441 warningMsg.append ("<lu><li>The name is required.<br>"); 442 443 warningMsg.append (checkSourcePath (warningMsg.toString (), jParams, 445 theTemporaryTemplate)); 446 447 if (!warningMsg.toString ().equals (EMPTY_STRING)) { 448 warningMsg.append ("</lu>"); 449 jParams.getRequest ().setAttribute ("Template_Engine.warningMsg", 450 warningMsg.toString ()); 451 return false; 452 } 453 454 theTemplate.setName (theTemporaryTemplate.getName ()); 456 theTemplate.setSourcePath (theTemporaryTemplate.getSourcePath ()); 457 logger.debug ("Source path :" + theTemporaryTemplate.getSourcePath ()); 458 theTemplate.setAvailable (theTemporaryTemplate.isAvailable ()); 459 theTemplate.commitChanges (); 460 461 boolean doUpdateSite = true; 463 if ((jParams.getSite ().getDefaultTemplateID () == theTemplate.getID ()) 464 && !theTemporaryTemplate.isDefault ()) { 465 jParams.getSite ().setDefaultTemplateID (-1); 466 } else if (theTemporaryTemplate.isDefault ()) { 467 jParams.getSite ().setDefaultTemplateID (theTemplate.getID ()); 468 } else { 469 doUpdateSite = false; 470 } 471 472 if (doUpdateSite) 473 ServicesRegistry.getInstance () 474 .getJahiaSitesService ().updateSite (jParams.getSite ()); 475 476 return true; 477 478 } 479 480 483 private String checkSourcePath (String warningMsg, ParamBean jParams, 484 JahiaPageDefinitionTemp tempoPageDef) { 485 486 if (tempoPageDef.getSourcePath () == null 487 || tempoPageDef.getSourcePath ().trim ().equals (EMPTY_STRING)) { 488 return "<li>The source path is required.<br>"; 489 } 490 491 String sourcePath = JahiaTools.replacePattern (tempoPageDef.getSourcePath (), "\\", 493 "/"); 494 495 while (sourcePath.startsWith ("/") || sourcePath.startsWith (".")) { 496 sourcePath = sourcePath.substring (1, sourcePath.length ()); 497 } 498 sourcePath = JahiaTools.replacePattern (sourcePath, "..", EMPTY_STRING); 499 sourcePath = JahiaTools.replacePattern (sourcePath, "./", "/"); 500 sourcePath = JahiaTools.replacePattern (sourcePath, "/.", "/"); 501 sourcePath = JahiaTools.replacePattern (sourcePath, "//", "/"); 502 503 504 505 String path = jParams.settings ().getJahiaTemplatesDiskPath (); 507 path = JahiaTools.replacePattern (path, "\\", "/"); 508 if (!path.endsWith ("/")) 509 path += "/"; 510 File f = new File ( 511 path + jParams.getSite ().getSiteKey () + File.separator + sourcePath); 512 logger.debug (" Template Path = " + f.getAbsolutePath ()); 513 514 if (!f.isFile ()) 515 return "<li>The source file does not exist.<br>"; 516 517 if (!f.canRead ()) 518 return "<li>No access allowed to the source file.<br>"; 519 520 if (warningMsg.equals (EMPTY_STRING)) { 522 String templateContext = jParams.settings ().getTemplatesContext () 523 + jParams.getSite ().getSiteKey () + "/"; 524 tempoPageDef.setSourcePath (templateContext + sourcePath); 525 } 526 return EMPTY_STRING; 527 } 528 529 } 530 | Popular Tags |