1 13 20 package org.jahia.engines.shared; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Vector ; 25 26 import org.apache.log4j.Logger; 27 import org.jahia.data.ConnectionTypes; 28 import org.jahia.data.FormDataManager; 29 import org.jahia.data.containers.JahiaContainer; 30 import org.jahia.data.fields.JahiaFieldDefinitionProperties; 31 import org.jahia.data.fields.JahiaField; 32 import org.jahia.engines.EngineMessages; 33 import org.jahia.engines.EngineParams; 34 import org.jahia.engines.JahiaEngine; 35 import org.jahia.engines.JahiaEngineTools; 36 import org.jahia.exceptions.JahiaException; 37 import org.jahia.params.ParamBean; 38 import org.jahia.registries.EnginesRegistry; 39 import org.jahia.registries.ServicesRegistry; 40 import org.jahia.services.acl.JahiaBaseACL; 41 import org.jahia.services.htmlparser.*; 42 import org.jahia.services.sites.*; 43 import org.jahia.utils.JahiaTools; 44 import org.jahia.utils.I18n; 45 import org.jahia.bin.Jahia; 46 import java.io.UnsupportedEncodingException ; 47 48 public class SmallText_Field 49 { 50 51 private static Logger logger = Logger.getLogger(SmallText_Field.class); 52 53 private static final int MAX_SMALLTEXT_LENGTH = 250; 54 55 56 private static SmallText_Field theObject = null; 57 public static final String JSP_FILE = "/jsp/jahia/engines/shared/smalltext_field.jsp"; 58 public static final String READONLY_JSP = "/jsp/jahia/engines/shared/readonly_smalltext_field.jsp"; 59 60 61 66 public static synchronized SmallText_Field getInstance() 67 { 68 if (theObject == null) { 69 theObject = new SmallText_Field(); 70 } 71 return theObject; 72 } 74 75 76 85 public boolean handleField( ParamBean jParams, Integer modeInt, HashMap engineMap ) 86 throws JahiaException 87 { 88 int mode = modeInt.intValue(); 89 JahiaField theField = (JahiaField) engineMap.get( "theField" ); 90 jParams.getRequest().setAttribute(JahiaEngine.ENGINE_MODE_ATTRIBUTE,new Integer (mode)); 91 switch (mode) 92 { 93 case (JahiaEngine.LOAD_MODE) : return composeEngineMap( jParams, engineMap, theField ); 94 case (JahiaEngine.UPDATE_MODE) : return getFormData( jParams, engineMap, theField ); 95 case (JahiaEngine.SAVE_MODE) : return saveData( jParams, engineMap, theField ); 96 } 97 return false; 98 } 100 101 private String cypher (String s) 103 { 104 StringBuffer result = new StringBuffer (); 105 for (int i=0; i<s.length(); i++) 106 { 107 result.append ((char)(s.charAt(i) ^ 1)); 108 } 109 return result.toString(); 110 } 111 112 121 private boolean getFormData( ParamBean jParams, HashMap engineMap, JahiaField theField ) 122 throws JahiaException 123 { 124 boolean out = true; 125 126 String fieldValue = jParams.getRequest() 127 .getParameter( "_" + 128 new Integer (theField.getID()).toString() ); 129 130 String engineParams = jParams.getRequest().getParameter( "engine_params" ); 131 fieldValue = JahiaTools.replacePattern(fieldValue,"|","|"); 132 133 if ( fieldValue != null && "ISO-8859-1".equalsIgnoreCase(jParams.settings(). 134 getDefaultResponseBodyEncoding())) { 135 fieldValue = I18n.windows1252ToISO(fieldValue); 144 } 145 String oriFieldValue = fieldValue; 146 147 JahiaSite site = ServicesRegistry.getInstance().getJahiaSitesService() 148 .getSite(theField.getJahiaID()); 149 150 if ( fieldValue != null && site.getHtmlCleanup()==1 ){ 151 Vector DOMVisitors = ServicesRegistry.getInstance() 152 .getHtmlParserService() 153 .getHtmlDOMVisitors(theField.getJahiaID()); 154 155 ExtractLinksDOMVisitor linkExtractionVisitor = null; 156 if (site.getHtmlMarkupFiltering() == 1) { 157 linkExtractionVisitor = new ExtractLinksDOMVisitor(); 158 linkExtractionVisitor.init(site.getID()); 159 DOMVisitors.add(linkExtractionVisitor); 160 } 161 162 if (!fieldValue.startsWith("<jahia-")) { 165 fieldValue = JahiaTextContentTidy.tidyPreProcessing(fieldValue); 166 fieldValue = ServicesRegistry.getInstance(). 167 getHtmlParserService() 168 .parse(fieldValue, DOMVisitors, theField.getSiteID()); 169 fieldValue = JahiaTextContentTidy.tidyPostProcessing(fieldValue); 170 } 171 172 EngineMessages resultMessages = ServicesRegistry.getInstance().getHtmlParserService() 173 .checkTextParsingErrors(fieldValue); 174 if ( !resultMessages.isEmpty() ){ 175 resultMessages.saveMessages(String.valueOf(theField.getID()) + ".", jParams.getRequest()); 176 fieldValue = oriFieldValue; 177 out = false; 178 } 179 180 if (linkExtractionVisitor != null) { 181 Iterator linkIter = linkExtractionVisitor.getDocumentLinks().iterator(); 182 while (linkIter.hasNext()) { 183 String curLink = (String ) linkIter.next(); 184 logger.debug("Found link [" + curLink + "] in small text field."); 185 } 186 } 187 } 188 189 if (fieldValue!=null) 190 { 191 if (cypher(fieldValue).equals("Rid&r!rn!mtbjx")) fieldValue=cypher ("Rid&r!`!ru`s"); 193 if (cypher(fieldValue).equals("Ctu!rid!bsx!bsx!bshdr")) 194 fieldValue=cypher ("ho!ids!mnodmx!id`su-!uihojhof///"); 195 if (cypher(fieldValue).equals("Hg!uidsd&r!onuihof")) 196 fieldValue=cypher ("lhrrhof!ho!lx!mhgd"); 197 if (cypher(fieldValue).equals("uido!vix!en!uidrd!ud`sr")) 198 fieldValue=cypher ("bnld!`u!ohfiu!>///!!!!ZCshuodx\\"); 199 if (cypher(fieldValue).equals("i`qqx!chsuie`x!e`e`&r")) 200 fieldValue=cypher ("=hlf!rsb<#iuuq;..vvv/k`ih`/nsf.OHBD/fhg#?"); 201 202 String encoding = jParams.getRequest().getCharacterEncoding(); 206 if (encoding == null) { 207 encoding = Jahia.getSettings().getDefaultResponseBodyEncoding(); 208 } 209 try { 210 int byteLength = fieldValue.getBytes(encoding).length; 211 while (byteLength > MAX_SMALLTEXT_LENGTH) { 212 logger.debug("Byte length of field value is over limit, truncating one byte from end..."); 213 fieldValue = fieldValue.substring(0, fieldValue.length() -1); 216 byteLength = fieldValue.getBytes(encoding).length; 217 } 218 } catch (UnsupportedEncodingException uee) { 219 logger.error("Error while calculating byte length of field value for encoding " + encoding, uee); 220 } 221 } 222 223 EngineParams eParams = new EngineParams( engineParams ); 224 String localSwitch = eParams.getParameter( "localswitch" ); 225 String dataSourceUrl = eParams.getParameter( "dsurl" ); 226 227 if (dataSourceUrl != null) { 228 theField.setValue( dataSourceUrl ); 229 theField.setConnectType( ConnectionTypes.DATASOURCE ); 230 } else if (localSwitch != null) { 231 theField.setValue( theField.getDefinition().getDefaultValue( jParams.getPage().getPageTemplateID() ) ); 232 theField.setConnectType( ConnectionTypes.LOCAL ); 233 } else if (fieldValue != null) { 234 logger.debug ("Field value = " + fieldValue); 235 EngineMessages resultMessages = new EngineMessages(); 236 if (FormDataManager.getInstance().checkIntegrity(fieldValue, resultMessages)) { 237 theField.setValue( fieldValue ); 238 } else { 239 theField.setValue( fieldValue ); 240 out = false; 241 } 242 logger.debug ("New Field value = " + fieldValue); 243 } 244 245 return out; 246 } 248 249 250 259 private boolean saveData( ParamBean jParams, HashMap engineMap, JahiaField theField ) 260 throws JahiaException 261 { 262 logger.debug ("Saving Field " + theField.getDefinition().getName() + " : " + theField.getValue() ); 263 return theField.save(jParams); 265 266 } 268 269 270 279 private boolean composeEngineMap( ParamBean jParams, HashMap engineMap, JahiaField theField ) 280 throws JahiaException 281 { 282 283 boolean editable = false; 284 JahiaContainer theContainer = (JahiaContainer)engineMap.get("theContainer"); 285 if ( theContainer == null ){ 286 editable = true; 288 } else { 289 HashMap ctnListFieldAcls = (HashMap ) engineMap 290 .get("ctnListFieldAcls"); 291 292 if (theContainer.getListID() != 0 && ctnListFieldAcls != null 293 && ctnListFieldAcls.size() > 0) { 294 JahiaBaseACL acl = JahiaEngineTools.getCtnListFieldACL( 295 ctnListFieldAcls, theField.getID()); 296 if (acl != null) { 297 editable = acl.getPermission(jParams.getUser(), 298 JahiaBaseACL.WRITE_RIGHTS, true); 299 } 300 } else { 301 editable = true; 302 } 303 } 304 305 String output = ""; 306 307 String forward = theField.getDefinition() 308 .getProperty(JahiaFieldDefinitionProperties.FIELD_UPDATE_JSP_FILE_PROP); 309 if ( forward == null ){ 310 forward = SmallText_Field.JSP_FILE; 311 if ( !editable ){ 312 forward = SmallText_Field.READONLY_JSP; 313 } 314 } 315 316 if ( editable ) { 317 318 String localSwitchUrl = "ReloadEngine('localswitch" + EngineParams.VALUE_TOKEN + "yes')"; 319 engineMap.put( "localSwitchUrl", localSwitchUrl ); 320 321 String dataSourceConnectUrl = ""; 322 JahiaEngine dsEngine = (JahiaEngine) EnginesRegistry.getInstance().getEngine( "selectdatasource" ); 323 dataSourceConnectUrl = dsEngine.renderLink( jParams, theField ); 324 engineMap.put( "dataSourceConnectUrl", dataSourceConnectUrl ); 325 326 String dataSourceIDUrl = ""; 327 dsEngine = (JahiaEngine) EnginesRegistry.getInstance().getEngine( "viewdatasourceid" ); 328 dataSourceIDUrl = dsEngine.renderLink( jParams, theField ); 329 engineMap.put( "dataSourceIDUrl", dataSourceIDUrl ); 330 331 boolean isIE = false; 332 String userAgent = jParams.getRequest().getHeader( "user-agent" ); 333 if (userAgent != null) { 334 isIE = (userAgent.indexOf( "IE" ) != -1); 335 } 336 jParams.getRequest().setAttribute("isIE" , new Boolean (isIE)); 337 338 JahiaSite site = ServicesRegistry.getInstance().getJahiaSitesService() 339 .getSite(theField.getJahiaID()); 340 341 String fieldValue = theField.getValue(); 342 if ( fieldValue == null ){ 343 fieldValue = ""; 344 } 345 String oriFieldValue = fieldValue; 346 if ( site.getHtmlCleanup()==1 ){ 347 Vector DOMVisitors = ServicesRegistry.getInstance() 348 .getHtmlParserService() 349 .getHtmlDOMVisitors(theField.getJahiaID()); 350 351 fieldValue = JahiaTextContentTidy.tidyPreProcessing(fieldValue); 352 fieldValue = ServicesRegistry.getInstance().getHtmlParserService() 353 .parse(fieldValue,DOMVisitors,theField.getSiteID()); 354 fieldValue = JahiaTextContentTidy.tidyPostProcessing(fieldValue); 355 } 356 357 EngineMessages resultMessages = ServicesRegistry.getInstance().getHtmlParserService() 358 .checkTextParsingErrors(fieldValue); 359 if ( !resultMessages.isEmpty() ){ 360 resultMessages.saveMessages(String.valueOf(theField.getID()) + ".", jParams.getRequest()); 361 fieldValue = oriFieldValue; 362 } 363 364 theField.setValue( FormDataManager.getInstance().formEncode(fieldValue) ); 365 output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, forward ); 366 } else { 367 output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, forward ); 368 } 369 engineMap.put( "fieldForm", output ); 370 return true; 371 372 } 374 375 } | Popular Tags |