1 29 30 package com.caucho.quercus.lib; 31 32 import com.caucho.quercus.annotation.Optional; 33 import com.caucho.quercus.env.ArrayValue; 34 import com.caucho.quercus.env.ConstArrayValue; 35 import com.caucho.quercus.env.Env; 36 import com.caucho.quercus.env.StringValue; 37 import com.caucho.quercus.env.StringValueImpl; 38 import com.caucho.quercus.env.Value; 39 import com.caucho.quercus.module.AbstractQuercusModule; 40 import com.caucho.util.L10N; 41 42 import java.util.HashMap ; 43 44 47 public class HtmlModule extends AbstractQuercusModule { 48 private static final L10N L = new L10N(HtmlModule.class); 49 50 public static final int HTML_SPECIALCHARS = 0; 51 public static final int HTML_ENTITIES = 1; 52 53 public static final int ENT_HTML_QUOTE_NONE = 0; 54 public static final int ENT_HTML_QUOTE_SINGLE = 1; 55 public static final int ENT_HTML_QUOTE_DOUBLE = 2; 56 57 public static final int ENT_COMPAT = ENT_HTML_QUOTE_DOUBLE; 58 public static final int ENT_QUOTES = ENT_HTML_QUOTE_SINGLE|ENT_HTML_QUOTE_DOUBLE; 59 public static final int ENT_NOQUOTES = ENT_HTML_QUOTE_NONE; 60 61 private final static ArrayValue HTML_SPECIALCHARS_ARRAY 62 = new ConstArrayValue(); 63 private final static ArrayValue HTML_ENTITIES_ARRAY 64 = new ConstArrayValue(); 65 66 private final static HashMap <String ,String > HTML_DECODE 67 = new HashMap <String ,String >(); 68 69 private final ArrayValue _htmlQuotesArray = new ConstArrayValue(); 70 private final ArrayValue _htmlNoQuotesArray = new ConstArrayValue(); 71 72 public HtmlModule() 73 { 74 } 75 76 79 public Value get_html_translation_table(@Optional("HTML_SPECIALCHARS") int table, 80 @Optional("ENT_COMPAT") int quoteStyle) 81 { 82 Value result; 83 84 if (table == HTML_ENTITIES) 85 result = HTML_ENTITIES_ARRAY.copy(); 86 else 87 result = HTML_SPECIALCHARS_ARRAY.copy(); 88 89 if ((quoteStyle & ENT_HTML_QUOTE_SINGLE) != 0) 90 result.put(new StringValueImpl("'"), new StringValueImpl("'")); 91 92 if ((quoteStyle & ENT_HTML_QUOTE_DOUBLE) != 0) 93 result.put(new StringValueImpl("\""), new StringValueImpl(""")); 94 95 return result; 96 } 97 98 107 public static Value htmlspecialchars(Env env, 108 Value stringV, 109 @Optional Value quoteStyleV, 110 @Optional Value charsetV) 111 { 112 String string = stringV.toString(); 114 StringBuilder sb = new StringBuilder (); 115 116 int len = string.length(); 117 for (int i = 0; i < len; i++) { 118 char ch = string.charAt(i); 119 120 switch (ch) { 121 case '&': 122 sb.append("&"); 123 break; 124 case '"': 125 sb.append("""); 126 break; 127 case '\'': 128 sb.append("'"); 129 break; 130 case '<': 131 sb.append("<"); 132 break; 133 case '>': 134 sb.append(">"); 135 break; 136 default: 137 sb.append(ch); 138 break; 139 } 140 } 141 142 return new StringValueImpl(sb.toString()); 143 } 144 145 154 public static Value htmlentities(Env env, 155 Value stringV, 156 @Optional Value quoteStyleV, 157 @Optional Value charsetV) 158 { 159 return htmlspecialchars(env, stringV, quoteStyleV, charsetV); 161 } 162 163 171 public static StringValue html_entity_decode(Env env, 172 StringValue string, 173 @Optional int quoteStyle, 174 @Optional String charset) 175 { 176 if (string.length() == 0) 177 return StringValue.EMPTY; 178 179 181 184 188 Value[] keys = HTML_SPECIALCHARS_ARRAY.getKeyArray(); 189 Value[] values = HTML_SPECIALCHARS_ARRAY.getValueArray(env); 190 int length = keys.length; 191 for (int i = 0; i < length; i++) { 192 string = RegexpModule.ereg_replace(env, 193 values[i].toStringValue(), 194 keys[i].toStringValue(), 195 string).toStringValue(); 196 } 197 204 return string; 205 } 206 207 212 public static Value nl2br(Env env, Value stringV) 213 { 214 String string = stringV.toString(); 215 216 int strLen = string.length(); 217 218 StringBuilder sb = new StringBuilder (); 219 220 for (int i = 0; i < strLen; i++) { 221 char ch = string.charAt(i); 222 223 if (ch == '\n') { 224 sb.append("<br />\n"); 225 } 226 else if (ch == '\r') { 227 if (i + 1 < strLen && string.charAt(i + 1) == '\n') { 228 sb.append("<br />\r\n"); 229 i++; 230 } 231 else { 232 sb.append("<br />\r"); 233 } 234 } 235 else { 236 sb.append(ch); 237 } 238 } 239 240 return new StringValueImpl(sb.toString()); 241 } 242 243 private static void entity(int ch, String entity) 244 { 245 HTML_ENTITIES_ARRAY.put("&" + (char) ch + ";", entity); 246 HTML_DECODE.put(entity, String.valueOf((char) ch)); 247 } 248 249 static { 250 HTML_SPECIALCHARS_ARRAY.put("<", "<"); 251 HTML_SPECIALCHARS_ARRAY.put(">", ">"); 252 HTML_SPECIALCHARS_ARRAY.put("&", "&"); 253 254 entity('<', "lt"); 255 entity('>', "gt"); 256 entity('&', "amp"); 257 258 entity(160, "nbsp"); 259 entity(161, "iexcl"); 260 entity(162, "cent"); 261 entity(163, "pound"); 262 entity(164, "curren"); 263 entity(165, "yen"); 264 entity(166, "brvbar"); 265 entity(167, "sect"); 266 entity(168, "uml"); 267 entity(169, "copy"); 268 entity(170, "ordf"); 269 entity(171, "laquo"); 270 entity(172, "not"); 271 entity(173, "shy"); 272 entity(174, "reg"); 273 entity(175, "macr"); 274 entity(176, "deg"); 275 entity(177, "plusmn"); 276 entity(178, "sup2"); 277 entity(179, "sup3"); 278 entity(180, "acute"); 279 entity(181, "micro"); 280 entity(182, "para"); 281 entity(183, "middot"); 282 entity(184, "cedil"); 283 entity(185, "sup1"); 284 entity(186, "ordm"); 285 entity(187, "raquo"); 286 entity(188, "frac14"); 287 entity(189, "frac12"); 288 entity(190, "frac34"); 289 entity(191, "iquest"); 290 entity(192, "Agrave"); 291 entity(193, "Aacute"); 292 entity(194, "Acirc"); 293 entity(195, "Atilde"); 294 entity(196, "Auml"); 295 entity(197, "Aring"); 296 entity(198, "AElig"); 297 entity(199, "Ccedil"); 298 entity(200, "Egrave"); 299 entity(201, "Eacute"); 300 entity(202, "Ecirc"); 301 entity(203, "Euml"); 302 entity(204, "Igrave"); 303 entity(205, "Iacute"); 304 entity(206, "Icirc"); 305 entity(207, "Iuml"); 306 entity(208, "ETH"); 307 entity(209, "Ntilde"); 308 entity(210, "Ograve"); 309 entity(211, "Oacute"); 310 entity(212, "Ocirc"); 311 entity(213, "Otilde"); 312 entity(214, "Ouml"); 313 entity(215, "times"); 314 entity(216, "Oslash"); 315 entity(217, "Ugrave"); 316 entity(218, "Uacute"); 317 entity(219, "Ucirc"); 318 entity(220, "Uuml"); 319 entity(221, "Yacute"); 320 entity(222, "THORN"); 321 entity(223, "szlig"); 322 entity(224, "agrave"); 323 entity(225, "aacute"); 324 entity(226, "acirc"); 325 entity(227, "atilde"); 326 entity(228, "auml"); 327 entity(229, "aring"); 328 entity(230, "aelig"); 329 entity(231, "ccedil"); 330 entity(232, "egrave"); 331 entity(233, "eacute"); 332 entity(234, "ecirc"); 333 entity(235, "euml"); 334 entity(236, "igrave"); 335 entity(237, "iacute"); 336 entity(238, "icirc"); 337 entity(239, "iuml"); 338 entity(240, "eth"); 339 entity(241, "ntilde"); 340 entity(242, "ograve"); 341 entity(243, "oacute"); 342 entity(244, "ocirc"); 343 entity(245, "otilde"); 344 entity(246, "ouml"); 345 entity(247, "divide"); 346 entity(248, "oslash"); 347 entity(249, "ugrave"); 348 entity(250, "uacute"); 349 entity(251, "ucirc"); 350 entity(252, "uuml"); 351 entity(253, "yacute"); 352 entity(254, "thorn"); 353 entity(255, "yuml"); 354 } 355 } 356 357 | Popular Tags |