1 26 package org.jahia.data; 27 28 import org.jahia.engines.EngineMessage; 29 import org.jahia.engines.EngineMessages; 30 import org.jahia.utils.JahiaConsole; 31 import org.jahia.utils.JahiaTools; 32 33 43 public class FormDataManager { 44 45 private static FormDataManager theObject = null; 46 47 52 private FormDataManager () { 53 JahiaConsole.println("FormDataManager", 54 "***** Starting up Form Data Manager *****"); 55 } 57 62 public static synchronized FormDataManager getInstance () { 63 if (theObject == null) { 64 theObject = new FormDataManager(); 65 } 66 return theObject; 67 } 69 77 public String formEncode (String str) { 78 if (str == null) { 79 return null; 80 } 81 str = JahiaTools.replacePattern(str, "&", "&"); 82 str = JahiaTools.replacePattern(str, "<", "<"); 83 str = JahiaTools.text2html(str); 84 return str; 85 } 86 87 95 public String formDecode (String str) { 96 if (str == null) { 97 return null; 98 } 99 100 str = JahiaTools.replacePattern(str, "&", "&"); 101 str = JahiaTools.replacePattern(str, "<", "<"); 102 str = JahiaTools.html2text(str); 103 104 return str; 105 } 107 112 public String htmlEncode (String str) { 113 if (str == null) { 114 return null; 115 } 116 StringBuffer result = new StringBuffer (str.length() + 500); 117 String strLower = str.toLowerCase(); 118 int startOfIndex = 0; 119 int i = strLower.indexOf("<html>"); 120 while (i != -1) { 121 result.append(JahiaTools.replacePattern(str.substring(startOfIndex, 122 i), "\n", "\n<br/>")); 123 startOfIndex = i + 6; i = strLower.indexOf("</html>", startOfIndex); 125 if (i != -1) { 126 result.append(str.substring(startOfIndex, i)); 127 startOfIndex = i + 7; } 129 i = strLower.indexOf("<html>", startOfIndex); 130 } 131 str = result.append(JahiaTools.replacePattern(str.substring( 132 startOfIndex, str.length()), "\n", "\n<br/>")).toString(); 133 str = removeSpecialTags(str); 134 str = JahiaTools.text2html(str); 135 str = JahiaTools.replacePattern(str, """, "\""); 137 return str; 138 } 139 140 146 public String removeSpecialTags (String str) { 147 if (str == null) { 148 return null; 149 } 150 151 str = removeTags(str, "body"); 152 str = removeTagWithContent(str, "head"); 153 str = removeTagWithContent(str, "title"); 154 str = removeTagWithContent(str, "frame"); 155 str = removeTagWithContent(str, "frameset"); 156 157 return str; 158 } 159 160 169 private String removeTags (String str, String tag) { 170 str = removeTag(str, tag); 171 str = removeTag(str, "/" + tag); 172 return str; 173 } 174 175 184 private String removeTag (String str, String tag) { 185 if (str == null) { 186 return null; 187 } 188 StringBuffer result = new StringBuffer (str.length()); 189 String strLower = str.toLowerCase(); 190 tag = tag.toLowerCase(); 191 int startOfIndex = 0; 192 int i = strLower.indexOf("<" + tag); 193 while (i != -1) { 194 result.append(str.substring(startOfIndex, i)); 195 i = strLower.indexOf(">", i); 196 if (i != -1) { 197 startOfIndex = i + 1; 198 } 199 i = strLower.indexOf("<" + tag, startOfIndex); 200 } 201 str = result.append(str.substring(startOfIndex, str.length())).toString(); 202 return str; 203 } 204 205 private String removeTagWithContent (String str, String tag) { 214 if (str == null) { 215 return null; 216 } 217 StringBuffer result = new StringBuffer (str.length()); 218 String strLower = str.toLowerCase(); 219 tag = tag.toLowerCase(); 220 int startOfIndex = 0; 221 int i = strLower.indexOf("<" + tag); 222 while (i != -1) { 223 result.append(str.substring(startOfIndex, i)); 224 startOfIndex = i + tag.length() + 1; 225 i = strLower.indexOf(tag + ">", startOfIndex); 226 if (i != -1) { 227 startOfIndex = i + tag.length() + 1; 228 } 229 i = strLower.indexOf("<" + tag, startOfIndex); 230 } 231 str = result.append(str.substring(startOfIndex, str.length())).toString(); 232 return str; 233 } 234 235 241 public boolean checkIntegrity (String str, EngineMessages resultMessages) { 242 if (str == null) { 243 return false; 244 } 245 246 String testStr = str; 247 int pos = 0; 248 int pos2 = 0; 249 int qtCount = 0; 250 testStr = JahiaTools.replacePattern(str, "\\<", ""); 251 252 int searchIndex = 0; 254 pos = testStr.indexOf("<", searchIndex); 255 while (pos != -1) { 256 pos2 = testStr.indexOf(">", searchIndex); 257 pos = testStr.indexOf("<", pos2); 258 if ( (pos2 != -1) && ( (pos > pos2) || (pos == -1))) { 259 searchIndex = pos2 + 1; 261 } else { 262 EngineMessage errorMessage = new EngineMessage( 263 "org.jahia.data.FormDataManager.tagMissingLtOrGt"); 264 resultMessages.add("htmlParser", errorMessage); 265 return false; 266 } 267 } 268 269 if (! ( 271 273 isTagClosed("b>", str, resultMessages) && 274 isTagClosed("font", str, resultMessages) && 275 isTagClosed("em>", str, resultMessages) && 276 isTagClosed("i>", str, resultMessages) && 277 isTagClosed("h1", str, resultMessages) && 278 isTagClosed("h2", str, resultMessages) && 279 isTagClosed("h3", str, resultMessages) && 280 isTagClosed("table", str, resultMessages) && 281 isTagClosed("script", str, resultMessages) && 282 isTagClosed("span", str, resultMessages) && 283 isTagClosed("div", str, resultMessages) && 284 isTagClosed("layer", str, resultMessages) && 285 isTagClosed("blockquote", str, resultMessages) && 286 isTagClosed("ul", str, resultMessages) && 287 isTagClosed("ol", str, resultMessages) && 288 isTagClosed("form", str, resultMessages) && 289 isTagClosed("comment", str, resultMessages) && 290 isTagClosed("center", str, resultMessages) && 291 isTagClosed("textarea", str, resultMessages) && 292 isTagClosed("select", str, resultMessages) && 293 isTagClosed("xmp", str, resultMessages) && 294 isTagClosed("style", str, resultMessages) && 295 isTagClosed("pre", str, resultMessages) && 296 isTagClosed("u>", str, resultMessages) && 297 isTagClosed("small", str, resultMessages) && 298 isTagClosed("sub", str, resultMessages) && 299 isTagClosed("sup", str, resultMessages) && 300 isTagClosed("s>", str, resultMessages) && 301 isTagClosed("strike", str, resultMessages) && 302 isTagClosed("strong", str, resultMessages) && 303 isTagClosed("tbody", str, resultMessages) && 304 isTagClosed("caption", str, resultMessages) && 305 isTagClosed("colgroup", str, resultMessages) && 306 isTagClosed("col", str, resultMessages) && 307 isTagClosed("tfoot", str, resultMessages) && 308 isTagClosed("th", str, resultMessages) && 309 isTagClosed("thread", str, resultMessages) && 310 isTagClosed("del", str, resultMessages) && 311 isTagClosed("dt", str, resultMessages) && 312 isTagClosed("dd", str, resultMessages) && 313 isTagClosed("dl", str, resultMessages) && 314 isTagClosed("dir", str, resultMessages) && 315 isTagClosed("cite", str, resultMessages) && 316 isTagClosed("code", str, resultMessages) && 317 isTagClosed("fieldset", str, resultMessages) && 318 isTagClosed("label", str, resultMessages) && 319 isTagClosed("object", str, resultMessages) && 320 isTagClosed("map", str, resultMessages) && 321 isTagClosed("dfn", str, resultMessages) && 322 isTagClosed("listing", str, resultMessages) && 323 isTagClosed("marquee", str, resultMessages) && 324 isTagClosed("menu", str, resultMessages) && 325 isTagClosed("multicol", str, resultMessages) && 326 isTagClosed("nobr", str, resultMessages) && 327 isTagClosed("note", str, resultMessages) && 328 isTagClosed("optgroup", str, resultMessages) && 329 isTagClosed("samp", str, resultMessages) && 330 isTagClosed("tt", str, resultMessages) && 331 isTagClosed("kbd", str, resultMessages) && 332 isTagClosed("var", str, resultMessages) && 333 isTagClosed("noembed", str, resultMessages) && 334 isTagClosed("html", str, resultMessages) 335 ) 336 ) { 337 return false; 338 } 339 340 searchIndex = 0; int endTagIndex = 0; 343 int startTagIndex = str.indexOf("<", searchIndex); 344 while (startTagIndex != -1) { 345 endTagIndex = str.indexOf(">", startTagIndex); 346 if (endTagIndex != -1) { 347 String tag = str.substring(startTagIndex, endTagIndex); 348 testStr = str; 349 pos = testStr.indexOf("\"", searchIndex); 350 while (pos != -1) { 351 qtCount++; 352 searchIndex = pos + 1; 353 pos = testStr.indexOf("\"", searchIndex); 354 } 355 359 368 } else { 369 String tag; 370 if (startTagIndex + 5 > str.length()) { 371 tag = str.substring(startTagIndex); 372 } else { 373 tag = str.substring(startTagIndex, startTagIndex+5) + "..."; 374 } 375 EngineMessage errorMessage = new EngineMessage( 376 "org.jahia.data.FormDataManager.tagNotClosed", tag); 377 resultMessages.add("htmlParser", errorMessage); 378 JahiaConsole.println("FormDataManagerager", "Tag not closed"); 379 return false; 380 } 381 startTagIndex = str.indexOf("<", endTagIndex); 382 } 383 return true; 385 386 } 388 398 public boolean isTagClosed (String tag, String inputStr, 399 EngineMessages resultMessages) { 400 tag = tag.toLowerCase(); 401 String testStr = inputStr.toLowerCase(); 402 String openTag = "<" + tag; 403 String closeTag = "</" + tag; 404 int pos1 = 0; 405 int pos2 = 0; 406 int searchIndex = 0; 407 pos1 = testStr.indexOf(openTag, searchIndex); 408 while (pos1 != -1) { 409 pos2 = testStr.indexOf(closeTag, searchIndex); 410 pos1 = testStr.indexOf(openTag, pos2); 411 if ( (pos2 != -1) && ( (pos1 > pos2) || (pos1 == -1))) { 412 searchIndex = pos2 + tag.length() + 2; 413 } else { 414 EngineMessage errorMessage = new EngineMessage( 415 "org.jahia.data.FormDataManager.tagNotClosed", tag); 416 resultMessages.add("htmlParser", errorMessage); 417 JahiaConsole.println("FormDataManagerager", 418 "ERROR: the HTML tag '" + tag + 419 "' should be closed."); 420 return false; 421 } 422 } 423 return true; 424 } 426 } | Popular Tags |