PageRenderTime 67ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 1ms

/administrator/components/com_virtuemart/helpers/vmfilter.php

https://github.com/srgg6701/auction-ruseasons
PHP | 1060 lines | 927 code | 40 blank | 93 comment | 328 complexity | bc2a7b21d3c9d9f167d0848542931241 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-3-Clause, JSON

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. defined('_JEXEC') or die('');
  3. /*
  4. htmLawed 1.1.10, 5 April 2012
  5. OOP code, 5 April 2012
  6. Copyright Santosh Patnaik
  7. Dual LGPL v3 and GPL v2+ license
  8. Modified for Virtuemart 2 by vm2 dev team(Patrick Kohl)
  9. A PHP Labware internal utility; www.bioinformatics.org/phplabware/internal_utilities/htmLawed
  10. $out = vmFilter::hl($in, array('safe'=>1, 'elements'=>'a, b, strong, i, em, li, ol, ul'));
  11. See htmLawed_README.txt/htm
  12. only good for HTML filtering needed in MultiVendor editing Product/category ...
  13. */
  14. class vmFilter{
  15. private static $C=null;
  16. private static $E=null;
  17. private static $S=null;
  18. private static $hl_Ids=null;
  19. // begin class
  20. public static function hl($t, $C=null, $S=array()){
  21. if ( is_string($C) ) $C = vmFilter::loadconfig($C);
  22. $C = is_array($C) ? $C : array();
  23. if(!empty($C['valid_xhtml'])){
  24. $C['elements'] = empty($C['elements']) ? '*-center-dir-font-isindex-menu-s-strike-u' : $C['elements'];
  25. $C['make_tag_strict'] = isset($C['make_tag_strict']) ? $C['make_tag_strict'] : 2;
  26. $C['xml:lang'] = isset($C['xml:lang']) ? $C['xml:lang'] : 2;
  27. }
  28. // config eles
  29. $e = array('a'=>1, 'abbr'=>1, 'acronym'=>1, 'address'=>1, 'applet'=>1, 'area'=>1, 'b'=>1, 'bdo'=>1, 'big'=>1, 'blockquote'=>1, 'br'=>1, 'button'=>1, 'caption'=>1, 'center'=>1, 'cite'=>1, 'code'=>1, 'col'=>1, 'colgroup'=>1, 'dd'=>1, 'del'=>1, 'dfn'=>1, 'dir'=>1, 'div'=>1, 'dl'=>1, 'dt'=>1, 'em'=>1, 'embed'=>1, 'fieldset'=>1, 'font'=>1, 'form'=>1, 'h1'=>1, 'h2'=>1, 'h3'=>1, 'h4'=>1, 'h5'=>1, 'h6'=>1, 'hr'=>1, 'i'=>1, 'iframe'=>1, 'img'=>1, 'input'=>1, 'ins'=>1, 'isindex'=>1, 'kbd'=>1, 'label'=>1, 'legend'=>1, 'li'=>1, 'map'=>1, 'menu'=>1, 'noscript'=>1, 'object'=>1, 'ol'=>1, 'optgroup'=>1, 'option'=>1, 'p'=>1, 'param'=>1, 'pre'=>1, 'q'=>1, 'rb'=>1, 'rbc'=>1, 'rp'=>1, 'rt'=>1, 'rtc'=>1, 'ruby'=>1, 's'=>1, 'samp'=>1, 'script'=>1, 'select'=>1, 'small'=>1, 'span'=>1, 'strike'=>1, 'strong'=>1, 'sub'=>1, 'sup'=>1, 'table'=>1, 'tbody'=>1, 'td'=>1, 'textarea'=>1, 'tfoot'=>1, 'th'=>1, 'thead'=>1, 'tr'=>1, 'tt'=>1, 'u'=>1, 'ul'=>1, 'var'=>1); // 86/deprecated+embed+ruby
  30. if(!empty($C['safe'])){
  31. unset($e['applet'], $e['embed'], $e['iframe'], $e['object'], $e['script']);
  32. }
  33. $x = !empty($C['elements']) ? str_replace(array("\n", "\r", "\t", ' '), '', $C['elements']) : '*';
  34. if($x == '-*'){
  35. $e = array();
  36. }
  37. elseif(strpos($x, '*') === false){
  38. $e = array_flip(explode(',', $x));
  39. }
  40. else{
  41. if(isset($x[1])){
  42. preg_match_all('`(?:^|-|\+)[^\-+]+?(?=-|\+|$)`', $x, $m, PREG_SET_ORDER);
  43. for($i=count($m); --$i>=0;){
  44. $m[$i] = $m[$i][0];
  45. }
  46. foreach($m as $v){
  47. if($v[0] == '+'){
  48. $e[substr($v, 1)] = 1;
  49. }
  50. if($v[0] == '-' && isset($e[($v = substr($v, 1))]) && !in_array('+'. $v, $m)){
  51. unset($e[$v]);
  52. }
  53. }
  54. }
  55. }
  56. $C['elements'] =& $e;
  57. // config attrs
  58. $x = !empty($C['deny_attribute']) ? str_replace(array("\n", "\r", "\t", ' '), '', $C['deny_attribute']) : '';
  59. $x = array_flip((isset($x[0]) && $x[0] == '*') ? explode('-', $x) : explode(',', $x. (!empty($C['safe']) ? ',on*' : '')));
  60. if(isset($x['on*'])){
  61. unset($x['on*']);
  62. $x += array('onblur'=>1, 'onchange'=>1, 'onclick'=>1, 'ondblclick'=>1, 'onfocus'=>1, 'onkeydown'=>1, 'onkeypress'=>1, 'onkeyup'=>1, 'onmousedown'=>1, 'onmousemove'=>1, 'onmouseout'=>1, 'onmouseover'=>1, 'onmouseup'=>1, 'onreset'=>1, 'onselect'=>1, 'onsubmit'=>1);
  63. }
  64. $C['deny_attribute'] = $x;
  65. // config URL
  66. $x = (isset($C['schemes'][2]) && strpos($C['schemes'], ':')) ? strtolower($C['schemes']) : 'href: aim, feed, file, ftp, gopher, http, https, irc, mailto, news, nntp, sftp, ssh, telnet; *:file, http, https';
  67. $C['schemes'] = array();
  68. foreach(explode(';', str_replace(array(' ', "\t", "\r", "\n"), '', $x)) as $v){
  69. $x = $x2 = null; list($x, $x2) = explode(':', $v, 2);
  70. if($x2){
  71. $C['schemes'][$x] = array_flip(explode(',', $x2));
  72. }
  73. }
  74. if(!isset($C['schemes']['*'])){
  75. $C['schemes']['*'] = array('file'=>1, 'http'=>1, 'https'=>1,);
  76. }
  77. if(!empty($C['safe']) && empty($C['schemes']['style'])){
  78. $C['schemes']['style'] = array('!'=>1);
  79. }
  80. $C['abs_url'] = isset($C['abs_url']) ? $C['abs_url'] : 0;
  81. if(!isset($C['base_url']) or !preg_match('`^[a-zA-Z\d.+\-]+://[^/]+/(.+?/)?$`', $C['base_url'])){
  82. $C['base_url'] = $C['abs_url'] = 0;
  83. }
  84. // config rest
  85. $C['and_mark'] = empty($C['and_mark']) ? 0 : 1;
  86. $C['anti_link_spam'] = (isset($C['anti_link_spam']) && is_array($C['anti_link_spam']) && count($C['anti_link_spam']) == 2 && (empty($C['anti_link_spam'][0]) or vmFilter::hl_regex($C['anti_link_spam'][0])) && (empty($C['anti_link_spam'][1]) or vmFilter::hl_regex($C['anti_link_spam'][1]))) ? $C['anti_link_spam'] : 0;
  87. $C['anti_mail_spam'] = isset($C['anti_mail_spam']) ? $C['anti_mail_spam'] : 0;
  88. $C['balance'] = isset($C['balance']) ? (bool)$C['balance'] : 1;
  89. $C['cdata'] = isset($C['cdata']) ? $C['cdata'] : (empty($C['safe']) ? 3 : 0);
  90. $C['clean_ms_char'] = empty($C['clean_ms_char']) ? 0 : $C['clean_ms_char'];
  91. $C['comment'] = isset($C['comment']) ? $C['comment'] : (empty($C['safe']) ? 3 : 0);
  92. $C['css_expression'] = empty($C['css_expression']) ? 0 : 1;
  93. $C['direct_list_nest'] = empty($C['direct_list_nest']) ? 0 : 1;
  94. $C['hexdec_entity'] = isset($C['hexdec_entity']) ? $C['hexdec_entity'] : 1;
  95. $C['hook'] = (!empty($C['hook']) && function_exists($C['hook'])) ? $C['hook'] : 0;
  96. $C['hook_tag'] = (!empty($C['hook_tag']) && function_exists($C['hook_tag'])) ? $C['hook_tag'] : 0;
  97. $C['keep_bad'] = isset($C['keep_bad']) ? $C['keep_bad'] : 6;
  98. $C['lc_std_val'] = isset($C['lc_std_val']) ? (bool)$C['lc_std_val'] : 1;
  99. $C['make_tag_strict'] = isset($C['make_tag_strict']) ? $C['make_tag_strict'] : 1;
  100. $C['named_entity'] = isset($C['named_entity']) ? (bool)$C['named_entity'] : 1;
  101. $C['no_deprecated_attr'] = isset($C['no_deprecated_attr']) ? $C['no_deprecated_attr'] : 1;
  102. $C['parent'] = isset($C['parent'][0]) ? strtolower($C['parent']) : 'body';
  103. $C['show_setting'] = !empty($C['show_setting']) ? $C['show_setting'] : 0;
  104. $C['style_pass'] = empty($C['style_pass']) ? 0 : 1;
  105. $C['tidy'] = empty($C['tidy']) ? 0 : $C['tidy'];
  106. $C['unique_ids'] = isset($C['unique_ids']) ? $C['unique_ids'] : 1;
  107. $C['xml:lang'] = isset($C['xml:lang']) ? $C['xml:lang'] : 0;
  108. if(!is_null(vmFilter::$C)){
  109. $reC = vmFilter::$C;
  110. }
  111. vmFilter::$C = $C;
  112. $S = is_array($S) ? $S : vmFilter::$hl_spec($S);
  113. if(!is_null(vmFilter::$S)){
  114. $reS = vmFilter::$S;
  115. }
  116. vmFilter::$S = $S;
  117. $t = preg_replace('`[\x00-\x08\x0b-\x0c\x0e-\x1f]`', '', $t);
  118. if($C['clean_ms_char']){
  119. $x = array("\x7f"=>'', "\x80"=>'&#8364;', "\x81"=>'', "\x83"=>'&#402;', "\x85"=>'&#8230;', "\x86"=>'&#8224;', "\x87"=>'&#8225;', "\x88"=>'&#710;', "\x89"=>'&#8240;', "\x8a"=>'&#352;', "\x8b"=>'&#8249;', "\x8c"=>'&#338;', "\x8d"=>'', "\x8e"=>'&#381;', "\x8f"=>'', "\x90"=>'', "\x95"=>'&#8226;', "\x96"=>'&#8211;', "\x97"=>'&#8212;', "\x98"=>'&#732;', "\x99"=>'&#8482;', "\x9a"=>'&#353;', "\x9b"=>'&#8250;', "\x9c"=>'&#339;', "\x9d"=>'', "\x9e"=>'&#382;', "\x9f"=>'&#376;');
  120. $x = $x + ($C['clean_ms_char'] == 1 ? array("\x82"=>'&#8218;', "\x84"=>'&#8222;', "\x91"=>'&#8216;', "\x92"=>'&#8217;', "\x93"=>'&#8220;', "\x94"=>'&#8221;') : array("\x82"=>'\'', "\x84"=>'"', "\x91"=>'\'', "\x92"=>'\'', "\x93"=>'"', "\x94"=>'"'));
  121. $t = strtr($t, $x);
  122. }
  123. if($C['cdata'] or $C['comment']){
  124. $t = preg_replace_callback('`<!(?:(?:--.*?--)|(?:\[CDATA\[.*?\]\]))>`sm', 'vmFilter::hl_cmtcd', $t);
  125. }
  126. $t = preg_replace_callback('`&amp;([A-Za-z][A-Za-z0-9]{1,30}|#(?:[0-9]{1,8}|[Xx][0-9A-Fa-f]{1,7}));`', 'vmFilter::hl_ent', str_replace('&', '&amp;', $t));
  127. if($C['unique_ids'] && is_null(vmFilter::$hl_Ids)){
  128. vmFilter::$hl_Ids = array();
  129. }
  130. if($C['hook']){
  131. $t = $C['hook']($t, $C, $S);
  132. }
  133. if($C['show_setting'] && preg_match('`^[a-z][a-z0-9_]*$`i', $C['show_setting'])){
  134. vmFilter::$C['show_setting'] = array('config'=>$C, 'spec'=>$S, 'time'=>microtime());
  135. }
  136. // main
  137. $t = preg_replace_callback('`<(?:(?:\s|$)|(?:[^>]*(?:>|$)))|>`m', 'vmFilter::hl_tag', $t);
  138. $t = $C['balance'] ? vmFilter::hl_bal($t, $C['keep_bad'], $C['parent']) : $t;
  139. $t = (($C['cdata'] or $C['comment']) && strpos($t, "\x01") !== false) ? str_replace(array("\x01", "\x02", "\x03", "\x04", "\x05"), array('', '', '&', '<', '>'), $t) : $t;
  140. $t = $C['tidy'] ? vmFilter::hl_tidy($t, $C['tidy'], $C['parent']) : $t;
  141. unset($C, $e);
  142. if(isset($reC)){
  143. vmFilter::$C = $reC;
  144. }
  145. if(isset($reS)){
  146. vmFilter::$S = $reS;
  147. }
  148. return $t;
  149. // eof
  150. }
  151. public static function hl_attrval($t, $p){
  152. // check attr val against $S
  153. $o = 1; $l = strlen($t);
  154. foreach($p as $k=>$v){
  155. switch($k){
  156. case 'maxlen':if($l > $v){
  157. $o = 0;
  158. }
  159. break; case 'minlen': if($l < $v){
  160. $o = 0;
  161. }
  162. break; case 'maxval': if((float)($t) > $v){
  163. $o = 0;
  164. }
  165. break; case 'minval': if((float)($t) < $v){
  166. $o = 0;
  167. }
  168. break; case 'match': if(!preg_match($v, $t)){
  169. $o = 0;
  170. }
  171. break; case 'nomatch': if(preg_match($v, $t)){
  172. $o = 0;
  173. }
  174. break; case 'oneof':
  175. $m = 0;
  176. foreach(explode('|', $v) as $n){
  177. if($t == $n){
  178. $m = 1; break;
  179. }
  180. }
  181. $o = $m;
  182. break; case 'noneof':
  183. $m = 1;
  184. foreach(explode('|', $v) as $n){
  185. if($t == $n){
  186. $m = 0; break;
  187. }
  188. }
  189. $o = $m;
  190. break; default:
  191. break;
  192. }
  193. if(!$o){
  194. break;
  195. }
  196. }
  197. return ($o ? $t : (isset($p['default']) ? $p['default'] : 0));
  198. // eof
  199. }
  200. public static function hl_bal($t, $do=1, $in='div'){
  201. // balance tags
  202. // by content
  203. $cB = array('blockquote'=>1, 'form'=>1, 'map'=>1, 'noscript'=>1); // Block
  204. $cE = array('area'=>1, 'br'=>1, 'col'=>1, 'embed'=>1, 'hr'=>1, 'img'=>1, 'input'=>1, 'isindex'=>1, 'param'=>1); // Empty
  205. $cF = array('button'=>1, 'del'=>1, 'div'=>1, 'dd'=>1, 'fieldset'=>1, 'iframe'=>1, 'ins'=>1, 'li'=>1, 'noscript'=>1, 'object'=>1, 'td'=>1, 'th'=>1); // Flow; later context-wise dynamic move of ins & del to $cI
  206. $cI = array('a'=>1, 'abbr'=>1, 'acronym'=>1, 'address'=>1, 'b'=>1, 'bdo'=>1, 'big'=>1, 'caption'=>1, 'cite'=>1, 'code'=>1, 'dfn'=>1, 'dt'=>1, 'em'=>1, 'font'=>1, 'h1'=>1, 'h2'=>1, 'h3'=>1, 'h4'=>1, 'h5'=>1, 'h6'=>1, 'i'=>1, 'kbd'=>1, 'label'=>1, 'legend'=>1, 'p'=>1, 'pre'=>1, 'q'=>1, 'rb'=>1, 'rt'=>1, 's'=>1, 'samp'=>1, 'small'=>1, 'span'=>1, 'strike'=>1, 'strong'=>1, 'sub'=>1, 'sup'=>1, 'tt'=>1, 'u'=>1, 'var'=>1); // Inline
  207. $cN = array('a'=>array('a'=>1), 'button'=>array('a'=>1, 'button'=>1, 'fieldset'=>1, 'form'=>1, 'iframe'=>1, 'input'=>1, 'label'=>1, 'select'=>1, 'textarea'=>1), 'fieldset'=>array('fieldset'=>1), 'form'=>array('form'=>1), 'label'=>array('label'=>1), 'noscript'=>array('script'=>1), 'pre'=>array('big'=>1, 'font'=>1, 'img'=>1, 'object'=>1, 'script'=>1, 'small'=>1, 'sub'=>1, 'sup'=>1), 'rb'=>array('ruby'=>1), 'rt'=>array('ruby'=>1)); // Illegal
  208. $cN2 = array_keys($cN);
  209. $cR = array('blockquote'=>1, 'dir'=>1, 'dl'=>1, 'form'=>1, 'map'=>1, 'menu'=>1, 'noscript'=>1, 'ol'=>1, 'optgroup'=>1, 'rbc'=>1, 'rtc'=>1, 'ruby'=>1, 'select'=>1, 'table'=>1, 'tbody'=>1, 'tfoot'=>1, 'thead'=>1, 'tr'=>1, 'ul'=>1);
  210. $cS = array('colgroup'=>array('col'=>1), 'dir'=>array('li'=>1), 'dl'=>array('dd'=>1, 'dt'=>1), 'menu'=>array('li'=>1), 'ol'=>array('li'=>1), 'optgroup'=>array('option'=>1), 'option'=>array('#pcdata'=>1), 'rbc'=>array('rb'=>1), 'rp'=>array('#pcdata'=>1), 'rtc'=>array('rt'=>1), 'ruby'=>array('rb'=>1, 'rbc'=>1, 'rp'=>1, 'rt'=>1, 'rtc'=>1), 'select'=>array('optgroup'=>1, 'option'=>1), 'script'=>array('#pcdata'=>1), 'table'=>array('caption'=>1, 'col'=>1, 'colgroup'=>1, 'tfoot'=>1, 'tbody'=>1, 'tr'=>1, 'thead'=>1), 'tbody'=>array('tr'=>1), 'tfoot'=>array('tr'=>1), 'textarea'=>array('#pcdata'=>1), 'thead'=>array('tr'=>1), 'tr'=>array('td'=>1, 'th'=>1), 'ul'=>array('li'=>1)); // Specific - immediate parent-child
  211. if(vmFilter::$C['direct_list_nest']){
  212. $cS['ol'] = $cS['ul'] += array('ol'=>1, 'ul'=>1);
  213. }
  214. $cO = array('address'=>array('p'=>1), 'applet'=>array('param'=>1), 'blockquote'=>array('script'=>1), 'fieldset'=>array('legend'=>1, '#pcdata'=>1), 'form'=>array('script'=>1), 'map'=>array('area'=>1), 'object'=>array('param'=>1, 'embed'=>1)); // Other
  215. $cT = array('colgroup'=>1, 'dd'=>1, 'dt'=>1, 'li'=>1, 'option'=>1, 'p'=>1, 'td'=>1, 'tfoot'=>1, 'th'=>1, 'thead'=>1, 'tr'=>1); // Omitable closing
  216. // block/inline type; ins & del both type; #pcdata: text
  217. $eB = array('address'=>1, 'blockquote'=>1, 'center'=>1, 'del'=>1, 'dir'=>1, 'dl'=>1, 'div'=>1, 'fieldset'=>1, 'form'=>1, 'ins'=>1, 'h1'=>1, 'h2'=>1, 'h3'=>1, 'h4'=>1, 'h5'=>1, 'h6'=>1, 'hr'=>1, 'isindex'=>1, 'menu'=>1, 'noscript'=>1, 'ol'=>1, 'p'=>1, 'pre'=>1, 'table'=>1, 'ul'=>1);
  218. $eI = array('#pcdata'=>1, 'a'=>1, 'abbr'=>1, 'acronym'=>1, 'applet'=>1, 'b'=>1, 'bdo'=>1, 'big'=>1, 'br'=>1, 'button'=>1, 'cite'=>1, 'code'=>1, 'del'=>1, 'dfn'=>1, 'em'=>1, 'embed'=>1, 'font'=>1, 'i'=>1, 'iframe'=>1, 'img'=>1, 'input'=>1, 'ins'=>1, 'kbd'=>1, 'label'=>1, 'map'=>1, 'object'=>1, 'q'=>1, 'ruby'=>1, 's'=>1, 'samp'=>1, 'select'=>1, 'script'=>1, 'small'=>1, 'span'=>1, 'strike'=>1, 'strong'=>1, 'sub'=>1, 'sup'=>1, 'textarea'=>1, 'tt'=>1, 'u'=>1, 'var'=>1);
  219. $eN = array('a'=>1, 'big'=>1, 'button'=>1, 'fieldset'=>1, 'font'=>1, 'form'=>1, 'iframe'=>1, 'img'=>1, 'input'=>1, 'label'=>1, 'object'=>1, 'ruby'=>1, 'script'=>1, 'select'=>1, 'small'=>1, 'sub'=>1, 'sup'=>1, 'textarea'=>1); // Exclude from specific ele; $cN values
  220. $eO = array('area'=>1, 'caption'=>1, 'col'=>1, 'colgroup'=>1, 'dd'=>1, 'dt'=>1, 'legend'=>1, 'li'=>1, 'optgroup'=>1, 'option'=>1, 'param'=>1, 'rb'=>1, 'rbc'=>1, 'rp'=>1, 'rt'=>1, 'rtc'=>1, 'script'=>1, 'tbody'=>1, 'td'=>1, 'tfoot'=>1, 'thead'=>1, 'th'=>1, 'tr'=>1); // Missing in $eB & $eI
  221. $eF = $eB + $eI;
  222. // $in sets allowed child
  223. $in = ((isset($eF[$in]) && $in != '#pcdata') or isset($eO[$in])) ? $in : 'div';
  224. if(isset($cE[$in])){
  225. return (!$do ? '' : str_replace(array('<', '>'), array('&lt;', '&gt;'), $t));
  226. }
  227. if(isset($cS[$in])){
  228. $inOk = $cS[$in];
  229. }
  230. elseif(isset($cI[$in])){
  231. $inOk = $eI; $cI['del'] = 1; $cI['ins'] = 1;
  232. }
  233. elseif(isset($cF[$in])){
  234. $inOk = $eF; unset($cI['del'], $cI['ins']);
  235. }
  236. elseif(isset($cB[$in])){
  237. $inOk = $eB; unset($cI['del'], $cI['ins']);
  238. }
  239. if(isset($cO[$in])){
  240. $inOk = $inOk + $cO[$in];
  241. }
  242. if(isset($cN[$in])){
  243. $inOk = array_diff_assoc($inOk, $cN[$in]);
  244. }
  245. $t = explode('<', $t);
  246. $ok = $q = array(); // $q seq list of open non-empty ele
  247. ob_start();
  248. for($i=-1, $ci=count($t); ++$i<$ci;){
  249. // allowed $ok in parent $p
  250. if($ql = count($q)){
  251. $p = array_pop($q);
  252. $q[] = $p;
  253. if(isset($cS[$p])){
  254. $ok = $cS[$p];
  255. }
  256. elseif(isset($cI[$p])){
  257. $ok = $eI; $cI['del'] = 1; $cI['ins'] = 1;
  258. }
  259. elseif(isset($cF[$p])){
  260. $ok = $eF; unset($cI['del'], $cI['ins']);
  261. }
  262. elseif(isset($cB[$p])){
  263. $ok = $eB; unset($cI['del'], $cI['ins']);
  264. }
  265. if(isset($cO[$p])){
  266. $ok = $ok + $cO[$p];
  267. }
  268. if(isset($cN[$p])){
  269. $ok = array_diff_assoc($ok, $cN[$p]);
  270. }
  271. }else{$ok = $inOk; unset($cI['del'], $cI['ins']);
  272. }
  273. // bad tags, & ele content
  274. if(isset($e) && ($do == 1 or (isset($ok['#pcdata']) && ($do == 3 or $do == 5)))){
  275. echo '&lt;', $s, $e, $a, '&gt;';
  276. }
  277. if(isset($x[0])){
  278. if($do < 3 or isset($ok['#pcdata'])){
  279. echo $x;
  280. }
  281. elseif(strpos($x, "\x02\x04")){
  282. foreach(preg_split('`(\x01\x02[^\x01\x02]+\x02\x01)`', $x, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) as $v){
  283. echo (substr($v, 0, 2) == "\x01\x02" ? $v : ($do > 4 ? preg_replace('`\S`', '', $v) : ''));
  284. }
  285. }elseif($do > 4){
  286. echo preg_replace('`\S`', '', $x);
  287. }
  288. }
  289. // get markup
  290. if(!preg_match('`^(/?)([a-zA-Z1-6]+)([^>]*)>(.*)`sm', $t[$i], $r)){
  291. $x = $t[$i]; continue;
  292. }
  293. $s = null; $e = null; $a = null; $x = null; list($all, $s, $e, $a, $x) = $r;
  294. // close tag
  295. if($s){
  296. if(isset($cE[$e]) or !in_array($e, $q)){
  297. continue;
  298. } // Empty/unopen
  299. if($p == $e){
  300. array_pop($q); echo '</', $e, '>'; unset($e); continue;
  301. } // Last open
  302. $add = ''; // Nesting - close open tags that need to be
  303. for($j=-1, $cj=count($q); ++$j<$cj;){
  304. if(($d = array_pop($q)) == $e){
  305. break;
  306. }
  307. else{$add .= "</{$d}>";
  308. }
  309. }
  310. echo $add, '</', $e, '>'; unset($e); continue;
  311. }
  312. // open tag
  313. // $cB ele needs $eB ele as child
  314. if(isset($cB[$e]) && strlen(trim($x))){
  315. $t[$i] = "{$e}{$a}>";
  316. array_splice($t, $i+1, 0, 'div>'. $x); unset($e, $x); ++$ci; --$i; continue;
  317. }
  318. if((($ql && isset($cB[$p])) or (isset($cB[$in]) && !$ql)) && !isset($eB[$e]) && !isset($ok[$e])){
  319. array_splice($t, $i, 0, 'div>'); unset($e, $x); ++$ci; --$i; continue;
  320. }
  321. // if no open ele, $in = parent; mostly immediate parent-child relation should hold
  322. if(!$ql or !isset($eN[$e]) or !array_intersect($q, $cN2)){
  323. if(!isset($ok[$e])){
  324. if($ql && isset($cT[$p])){
  325. echo '</', array_pop($q), '>'; unset($e, $x); --$i;
  326. }
  327. continue;
  328. }
  329. if(!isset($cE[$e])){
  330. $q[] = $e;
  331. }
  332. echo '<', $e, $a, '>'; unset($e); continue;
  333. }
  334. // specific parent-child
  335. if(isset($cS[$p][$e])){
  336. if(!isset($cE[$e])){
  337. $q[] = $e;
  338. }
  339. echo '<', $e, $a, '>'; unset($e); continue;
  340. }
  341. // nesting
  342. $add = '';
  343. $q2 = array();
  344. for($k=-1, $kc=count($q); ++$k<$kc;){
  345. $d = $q[$k];
  346. $ok2 = array();
  347. if(isset($cS[$d])){
  348. $q2[] = $d; continue;
  349. }
  350. $ok2 = isset($cI[$d]) ? $eI : $eF;
  351. if(isset($cO[$d])){
  352. $ok2 = $ok2 + $cO[$d];
  353. }
  354. if(isset($cN[$d])){
  355. $ok2 = array_diff_assoc($ok2, $cN[$d]);
  356. }
  357. if(!isset($ok2[$e])){
  358. if(!$k && !isset($inOk[$e])){
  359. continue 2;
  360. }
  361. $add = "</{$d}>";
  362. for(;++$k<$kc;){
  363. $add = "</{$q[$k]}>{$add}";
  364. }
  365. break;
  366. }
  367. else{$q2[] = $d;
  368. }
  369. }
  370. $q = $q2;
  371. if(!isset($cE[$e])){
  372. $q[] = $e;
  373. }
  374. echo $add, '<', $e, $a, '>'; unset($e); continue;
  375. }
  376. // end
  377. if($ql = count($q)){
  378. $p = array_pop($q);
  379. $q[] = $p;
  380. if(isset($cS[$p])){
  381. $ok = $cS[$p];
  382. }
  383. elseif(isset($cI[$p])){
  384. $ok = $eI; $cI['del'] = 1; $cI['ins'] = 1;
  385. }
  386. elseif(isset($cF[$p])){
  387. $ok = $eF; unset($cI['del'], $cI['ins']);
  388. }
  389. elseif(isset($cB[$p])){
  390. $ok = $eB; unset($cI['del'], $cI['ins']);
  391. }
  392. if(isset($cO[$p])){
  393. $ok = $ok + $cO[$p];
  394. }
  395. if(isset($cN[$p])){
  396. $ok = array_diff_assoc($ok, $cN[$p]);
  397. }
  398. }else{$ok = $inOk; unset($cI['del'], $cI['ins']);
  399. }
  400. if(isset($e) && ($do == 1 or (isset($ok['#pcdata']) && ($do == 3 or $do == 5)))){
  401. echo '&lt;', $s, $e, $a, '&gt;';
  402. }
  403. if(isset($x[0])){
  404. if(strlen(trim($x)) && (($ql && isset($cB[$p])) or (isset($cB[$in]) && !$ql))){
  405. echo '<div>', $x, '</div>';
  406. }
  407. elseif($do < 3 or isset($ok['#pcdata'])){
  408. echo $x;
  409. }
  410. elseif(strpos($x, "\x02\x04")){
  411. foreach(preg_split('`(\x01\x02[^\x01\x02]+\x02\x01)`', $x, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) as $v){
  412. echo (substr($v, 0, 2) == "\x01\x02" ? $v : ($do > 4 ? preg_replace('`\S`', '', $v) : ''));
  413. }
  414. }elseif($do > 4){
  415. echo preg_replace('`\S`', '', $x);
  416. }
  417. }
  418. while(!empty($q) && ($e = array_pop($q))){
  419. echo '</', $e, '>';
  420. }
  421. $o = ob_get_contents();
  422. ob_end_clean();
  423. return $o;
  424. // eof
  425. }
  426. public static function hl_cmtcd($t){
  427. // comment/CDATA sec handler
  428. $t = $t[0];
  429. $C = vmFilter::$C;
  430. if(!($v = $C[$n = $t[3] == '-' ? 'comment' : 'cdata'])){
  431. return $t;
  432. }
  433. if($v == 1){
  434. return '';
  435. }
  436. if($n == 'comment'){
  437. if(substr(($t = preg_replace('`--+`', '-', substr($t, 4, -3))), -1) != ' '){
  438. $t .= ' ';
  439. }
  440. }
  441. else{$t = substr($t, 1, -1);
  442. }
  443. $t = $v == 2 ? str_replace(array('&', '<', '>'), array('&amp;', '&lt;', '&gt;'), $t) : $t;
  444. return str_replace(array('&', '<', '>'), array("\x03", "\x04", "\x05"), ($n == 'comment' ? "\x01\x02\x04!--$t--\x05\x02\x01" : "\x01\x01\x04$t\x05\x01\x01"));
  445. // eof
  446. }
  447. public static function hl_ent($t){
  448. // entitity handler
  449. $C = vmFilter::$C;
  450. $t = $t[1];
  451. static $U = array('quot'=>1,'amp'=>1,'lt'=>1,'gt'=>1);
  452. static $N = array('fnof'=>'402', 'Alpha'=>'913', 'Beta'=>'914', 'Gamma'=>'915', 'Delta'=>'916', 'Epsilon'=>'917', 'Zeta'=>'918', 'Eta'=>'919', 'Theta'=>'920', 'Iota'=>'921', 'Kappa'=>'922', 'Lambda'=>'923', 'Mu'=>'924', 'Nu'=>'925', 'Xi'=>'926', 'Omicron'=>'927', 'Pi'=>'928', 'Rho'=>'929', 'Sigma'=>'931', 'Tau'=>'932', 'Upsilon'=>'933', 'Phi'=>'934', 'Chi'=>'935', 'Psi'=>'936', 'Omega'=>'937', 'alpha'=>'945', 'beta'=>'946', 'gamma'=>'947', 'delta'=>'948', 'epsilon'=>'949', 'zeta'=>'950', 'eta'=>'951', 'theta'=>'952', 'iota'=>'953', 'kappa'=>'954', 'lambda'=>'955', 'mu'=>'956', 'nu'=>'957', 'xi'=>'958', 'omicron'=>'959', 'pi'=>'960', 'rho'=>'961', 'sigmaf'=>'962', 'sigma'=>'963', 'tau'=>'964', 'upsilon'=>'965', 'phi'=>'966', 'chi'=>'967', 'psi'=>'968', 'omega'=>'969', 'thetasym'=>'977', 'upsih'=>'978', 'piv'=>'982', 'bull'=>'8226', 'hellip'=>'8230', 'prime'=>'8242', 'Prime'=>'8243', 'oline'=>'8254', 'frasl'=>'8260', 'weierp'=>'8472', 'image'=>'8465', 'real'=>'8476', 'trade'=>'8482', 'alefsym'=>'8501', 'larr'=>'8592', 'uarr'=>'8593', 'rarr'=>'8594', 'darr'=>'8595', 'harr'=>'8596', 'crarr'=>'8629', 'lArr'=>'8656', 'uArr'=>'8657', 'rArr'=>'8658', 'dArr'=>'8659', 'hArr'=>'8660', 'forall'=>'8704', 'part'=>'8706', 'exist'=>'8707', 'empty'=>'8709', 'nabla'=>'8711', 'isin'=>'8712', 'notin'=>'8713', 'ni'=>'8715', 'prod'=>'8719', 'sum'=>'8721', 'minus'=>'8722', 'lowast'=>'8727', 'radic'=>'8730', 'prop'=>'8733', 'infin'=>'8734', 'ang'=>'8736', 'and'=>'8743', 'or'=>'8744', 'cap'=>'8745', 'cup'=>'8746', 'int'=>'8747', 'there4'=>'8756', 'sim'=>'8764', 'cong'=>'8773', 'asymp'=>'8776', 'ne'=>'8800', 'equiv'=>'8801', 'le'=>'8804', 'ge'=>'8805', 'sub'=>'8834', 'sup'=>'8835', 'nsub'=>'8836', 'sube'=>'8838', 'supe'=>'8839', 'oplus'=>'8853', 'otimes'=>'8855', 'perp'=>'8869', 'sdot'=>'8901', 'lceil'=>'8968', 'rceil'=>'8969', 'lfloor'=>'8970', 'rfloor'=>'8971', 'lang'=>'9001', 'rang'=>'9002', 'loz'=>'9674', 'spades'=>'9824', 'clubs'=>'9827', 'hearts'=>'9829', 'diams'=>'9830', 'apos'=>'39', 'OElig'=>'338', 'oelig'=>'339', 'Scaron'=>'352', 'scaron'=>'353', 'Yuml'=>'376', 'circ'=>'710', 'tilde'=>'732', 'ensp'=>'8194', 'emsp'=>'8195', 'thinsp'=>'8201', 'zwnj'=>'8204', 'zwj'=>'8205', 'lrm'=>'8206', 'rlm'=>'8207', 'ndash'=>'8211', 'mdash'=>'8212', 'lsquo'=>'8216', 'rsquo'=>'8217', 'sbquo'=>'8218', 'ldquo'=>'8220', 'rdquo'=>'8221', 'bdquo'=>'8222', 'dagger'=>'8224', 'Dagger'=>'8225', 'permil'=>'8240', 'lsaquo'=>'8249', 'rsaquo'=>'8250', 'euro'=>'8364', 'nbsp'=>'160', 'iexcl'=>'161', 'cent'=>'162', 'pound'=>'163', 'curren'=>'164', 'yen'=>'165', 'brvbar'=>'166', 'sect'=>'167', 'uml'=>'168', 'copy'=>'169', 'ordf'=>'170', 'laquo'=>'171', 'not'=>'172', 'shy'=>'173', 'reg'=>'174', 'macr'=>'175', 'deg'=>'176', 'plusmn'=>'177', 'sup2'=>'178', 'sup3'=>'179', 'acute'=>'180', 'micro'=>'181', 'para'=>'182', 'middot'=>'183', 'cedil'=>'184', 'sup1'=>'185', 'ordm'=>'186', 'raquo'=>'187', 'frac14'=>'188', 'frac12'=>'189', 'frac34'=>'190', 'iquest'=>'191', 'Agrave'=>'192', 'Aacute'=>'193', 'Acirc'=>'194', 'Atilde'=>'195', 'Auml'=>'196', 'Aring'=>'197', 'AElig'=>'198', 'Ccedil'=>'199', 'Egrave'=>'200', 'Eacute'=>'201', 'Ecirc'=>'202', 'Euml'=>'203', 'Igrave'=>'204', 'Iacute'=>'205', 'Icirc'=>'206', 'Iuml'=>'207', 'ETH'=>'208', 'Ntilde'=>'209', 'Ograve'=>'210', 'Oacute'=>'211', 'Ocirc'=>'212', 'Otilde'=>'213', 'Ouml'=>'214', 'times'=>'215', 'Oslash'=>'216', 'Ugrave'=>'217', 'Uacute'=>'218', 'Ucirc'=>'219', 'Uuml'=>'220', 'Yacute'=>'221', 'THORN'=>'222', 'szlig'=>'223', 'agrave'=>'224', 'aacute'=>'225', 'acirc'=>'226', 'atilde'=>'227', 'auml'=>'228', 'aring'=>'229', 'aelig'=>'230', 'ccedil'=>'231', 'egrave'=>'232', 'eacute'=>'233', 'ecirc'=>'234', 'euml'=>'235', 'igrave'=>'236', 'iacute'=>'237', 'icirc'=>'238', 'iuml'=>'239', 'eth'=>'240', 'ntilde'=>'241', 'ograve'=>'242', 'oacute'=>'243', 'ocirc'=>'244', 'otilde'=>'245', 'ouml'=>'246', 'divide'=>'247', 'oslash'=>'248', 'ugrave'=>'249', 'uacute'=>'250', 'ucirc'=>'251', 'uuml'=>'252', 'yacute'=>'253', 'thorn'=>'254', 'yuml'=>'255');
  453. if($t[0] != '#'){
  454. return ($C['and_mark'] ? "\x06" : '&'). (isset($U[$t]) ? $t : (isset($N[$t]) ? (!$C['named_entity'] ? '#'. ($C['hexdec_entity'] > 1 ? 'x'. dechex($N[$t]) : $N[$t]) : $t) : 'amp;'. $t)). ';';
  455. }
  456. if(($n = ctype_digit($t = substr($t, 1)) ? intval($t) : hexdec(substr($t, 1))) < 9 or ($n > 13 && $n < 32) or $n == 11 or $n == 12 or ($n > 126 && $n < 160 && $n != 133) or ($n > 55295 && ($n < 57344 or ($n > 64975 && $n < 64992) or $n == 65534 or $n == 65535 or $n > 1114111))){
  457. return ($C['and_mark'] ? "\x06" : '&'). "amp;#{$t};";
  458. }
  459. return ($C['and_mark'] ? "\x06" : '&'). '#'. (((ctype_digit($t) && $C['hexdec_entity'] < 2) or !$C['hexdec_entity']) ? $n : 'x'. dechex($n)). ';';
  460. // eof
  461. }
  462. public static function hl_prot($p, $c=null){
  463. // check URL scheme
  464. $C = vmFilter::$C;
  465. $b = $a = '';
  466. if($c == null){
  467. $c = 'style'; $b = $p[1]; $a = $p[3]; $p = trim($p[2]);
  468. }
  469. $c = isset($C['schemes'][$c]) ? $C['schemes'][$c] : $C['schemes']['*'];
  470. static $d = 'denied:';
  471. if(isset($c['!']) && substr($p, 0, 7) != $d){
  472. $p = "$d$p";
  473. }
  474. if(isset($c['*']) or !strcspn($p, '#?;') or (substr($p, 0, 7) == $d)){
  475. return "{$b}{$p}{$a}";
  476. } // All ok, frag, query, param
  477. if(preg_match('`^([a-z\d\-+.&#; ]+?)(:|&#(58|x3a);|%3a|\\\\0{0,4}3a).`i', $p, $m) && !isset($c[strtolower($m[1])])){
  478. // Denied prot
  479. return "{$b}{$d}{$p}{$a}";
  480. }
  481. if($C['abs_url']){
  482. if($C['abs_url'] == -1 && strpos($p, $C['base_url']) === 0){
  483. // Make url rel
  484. $p = substr($p, strlen($C['base_url']));
  485. }elseif(empty($m[1])){
  486. // Make URL abs
  487. if(substr($p, 0, 2) == '//'){
  488. $p = substr($C['base_url'], 0, strpos($C['base_url'], ':')+1). $p;
  489. }
  490. elseif($p[0] == '/'){
  491. $p = preg_replace('`(^.+?://[^/]+)(.*)`', '$1', $C['base_url']). $p;
  492. }
  493. elseif(strcspn($p, './')){
  494. $p = $C['base_url']. $p;
  495. }
  496. else{
  497. preg_match('`^([a-zA-Z\d\-+.]+://[^/]+)(.*)`', $C['base_url'], $m);
  498. $p = preg_replace('`(?<=/)\./`', '', $m[2]. $p);
  499. while(preg_match('`(?<=/)([^/]{3,}|[^/.]+?|\.[^/.]|[^/.]\.)/\.\./`', $p)){
  500. $p = preg_replace('`(?<=/)([^/]{3,}|[^/.]+?|\.[^/.]|[^/.]\.)/\.\./`', '', $p);
  501. }
  502. $p = $m[1]. $p;
  503. }
  504. }
  505. }
  506. return "{$b}{$p}{$a}";
  507. // eof
  508. }
  509. public static function hl_regex($p){
  510. // ?regex
  511. if(empty($p)){
  512. return 0;
  513. }
  514. if($t = ini_get('track_errors')){
  515. $o = isset($php_errormsg) ? $php_errormsg : null;
  516. }
  517. else{ini_set('track_errors', 1);
  518. }
  519. unset($php_errormsg);
  520. if(($d = ini_get('display_errors'))){
  521. ini_set('display_errors', 0);
  522. }
  523. preg_match($p, '');
  524. if($d){
  525. ini_set('display_errors', 1);
  526. }
  527. $r = isset($php_errormsg) ? 0 : 1;
  528. if($t){
  529. $php_errormsg = isset($o) ? $o : null;
  530. }
  531. else{ini_set('track_errors', 0);
  532. }
  533. return $r;
  534. // eof
  535. }
  536. public static function hl_spec($t){
  537. // final $spec
  538. $s = array();
  539. $t = str_replace(array("\t", "\r", "\n", ' '), '', preg_replace('/"(?>(`.|[^"])*)"/sme', 'substr(str_replace(array(";", "|", "~", " ", ",", "/", "(", ")", \'`"\'), array("\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", "\x08", "\""), "$0"), 1, -1)', trim($t)));
  540. for($i = count(($t = explode(';', $t))); --$i>=0;){
  541. $w = $t[$i];
  542. if(empty($w) or ($e = strpos($w, '=')) === false or !strlen(($a = substr($w, $e+1)))){
  543. continue;
  544. }
  545. $y = $n = array();
  546. foreach(explode(',', $a) as $v){
  547. if(!preg_match('`^([a-z:\-\*]+)(?:\((.*?)\))?`i', $v, $m)){
  548. continue;
  549. }
  550. if(($x = strtolower($m[1])) == '-*'){
  551. $n['*'] = 1; continue;
  552. }
  553. if($x[0] == '-'){
  554. $n[substr($x, 1)] = 1; continue;
  555. }
  556. if(!isset($m[2])){
  557. $y[$x] = 1; continue;
  558. }
  559. foreach(explode('/', $m[2]) as $m){
  560. if(empty($m) or ($p = strpos($m, '=')) == 0 or $p < 5){
  561. $y[$x] = 1; continue;
  562. }
  563. $y[$x][strtolower(substr($m, 0, $p))] = str_replace(array("\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", "\x08"), array(";", "|", "~", " ", ",", "/", "(", ")"), substr($m, $p+1));
  564. }
  565. if(isset($y[$x]['match']) && !vmFilter::hl_regex($y[$x]['match'])){
  566. unset($y[$x]['match']);
  567. }
  568. if(isset($y[$x]['nomatch']) && !vmFilter::hl_regex($y[$x]['nomatch'])){
  569. unset($y[$x]['nomatch']);
  570. }
  571. }
  572. if(!count($y) && !count($n)){
  573. continue;
  574. }
  575. foreach(explode(',', substr($w, 0, $e)) as $v){
  576. if(!strlen(($v = strtolower($v)))){
  577. continue;
  578. }
  579. if(count($y)){
  580. $s[$v] = $y;
  581. }
  582. if(count($n)){
  583. $s[$v]['n'] = $n;
  584. }
  585. }
  586. }
  587. return $s;
  588. // eof
  589. }
  590. public static function hl_tag($t){
  591. // tag/attribute handler
  592. $C = vmFilter::$C;
  593. $t = $t[0];
  594. // invalid < >
  595. if($t == '< '){
  596. return '&lt; ';
  597. }
  598. if($t == '>'){
  599. return '&gt;';
  600. }
  601. if(!preg_match('`^<(/?)([a-zA-Z][a-zA-Z1-6]*)([^>]*?)\s?>$`m', $t, $m)){
  602. return str_replace(array('<', '>'), array('&lt;', '&gt;'), $t);
  603. }elseif(!isset($C['elements'][($e = strtolower($m[2]))])){
  604. return (($C['keep_bad']%2) ? str_replace(array('<', '>'), array('&lt;', '&gt;'), $t) : '');
  605. }
  606. // attr string
  607. $a = str_replace(array("\n", "\r", "\t"), ' ', trim($m[3]));
  608. // tag transform
  609. static $eD = array('applet'=>1, 'center'=>1, 'dir'=>1, 'embed'=>1, 'font'=>1, 'isindex'=>1, 'menu'=>1, 's'=>1, 'strike'=>1, 'u'=>1); // Deprecated
  610. if($C['make_tag_strict'] && isset($eD[$e])){
  611. $trt = vmFilter::hl_tag2($e, $a, $C['make_tag_strict']);
  612. if(!$e){
  613. return (($C['keep_bad']%2) ? str_replace(array('<', '>'), array('&lt;', '&gt;'), $t) : '');
  614. }
  615. }
  616. // close tag
  617. static $eE = array('area'=>1, 'br'=>1, 'col'=>1, 'embed'=>1, 'hr'=>1, 'img'=>1, 'input'=>1, 'isindex'=>1, 'param'=>1); // Empty ele
  618. if(!empty($m[1])){
  619. return (!isset($eE[$e]) ? "</$e>" : (($C['keep_bad'])%2 ? str_replace(array('<', '>'), array('&lt;', '&gt;'), $t) : ''));
  620. }
  621. // open tag & attr
  622. static $aN = array('abbr'=>array('td'=>1, 'th'=>1), 'accept-charset'=>array('form'=>1), 'accept'=>array('form'=>1, 'input'=>1), 'accesskey'=>array('a'=>1, 'area'=>1, 'button'=>1, 'input'=>1, 'label'=>1, 'legend'=>1, 'textarea'=>1), 'action'=>array('form'=>1), 'align'=>array('caption'=>1, 'embed'=>1, 'applet'=>1, 'iframe'=>1, 'img'=>1, 'input'=>1, 'object'=>1, 'legend'=>1, 'table'=>1, 'hr'=>1, 'div'=>1, 'h1'=>1, 'h2'=>1, 'h3'=>1, 'h4'=>1, 'h5'=>1, 'h6'=>1, 'p'=>1, 'col'=>1, 'colgroup'=>1, 'tbody'=>1, 'td'=>1, 'tfoot'=>1, 'th'=>1, 'thead'=>1, 'tr'=>1), 'alt'=>array('applet'=>1, 'area'=>1, 'img'=>1, 'input'=>1), 'archive'=>array('applet'=>1, 'object'=>1), 'axis'=>array('td'=>1, 'th'=>1), 'bgcolor'=>array('embed'=>1, 'table'=>1, 'tr'=>1, 'td'=>1, 'th'=>1), 'border'=>array('table'=>1, 'img'=>1, 'object'=>1), 'bordercolor'=>array('table'=>1, 'td'=>1, 'tr'=>1), 'cellpadding'=>array('table'=>1), 'cellspacing'=>array('table'=>1), 'char'=>array('col'=>1, 'colgroup'=>1, 'tbody'=>1, 'td'=>1, 'tfoot'=>1, 'th'=>1, 'thead'=>1, 'tr'=>1), 'charoff'=>array('col'=>1, 'colgroup'=>1, 'tbody'=>1, 'td'=>1, 'tfoot'=>1, 'th'=>1, 'thead'=>1, 'tr'=>1), 'charset'=>array('a'=>1, 'script'=>1), 'checked'=>array('input'=>1), 'cite'=>array('blockquote'=>1, 'q'=>1, 'del'=>1, 'ins'=>1), 'classid'=>array('object'=>1), 'clear'=>array('br'=>1), 'code'=>array('applet'=>1), 'codebase'=>array('object'=>1, 'applet'=>1), 'codetype'=>array('object'=>1), 'color'=>array('font'=>1), 'cols'=>array('textarea'=>1), 'colspan'=>array('td'=>1, 'th'=>1), 'compact'=>array('dir'=>1, 'dl'=>1, 'menu'=>1, 'ol'=>1, 'ul'=>1), 'coords'=>array('area'=>1, 'a'=>1), 'data'=>array('object'=>1), 'datetime'=>array('del'=>1, 'ins'=>1), 'declare'=>array('object'=>1), 'defer'=>array('script'=>1), 'dir'=>array('bdo'=>1), 'disabled'=>array('button'=>1, 'input'=>1, 'optgroup'=>1, 'option'=>1, 'select'=>1, 'textarea'=>1), 'enctype'=>array('form'=>1), 'face'=>array('font'=>1), 'for'=>array('label'=>1), 'frame'=>array('table'=>1), 'frameborder'=>array('iframe'=>1), 'headers'=>array('td'=>1, 'th'=>1), 'height'=>array('embed'=>1, 'iframe'=>1, 'td'=>1, 'th'=>1, 'img'=>1, 'object'=>1, 'applet'=>1), 'href'=>array('a'=>1, 'area'=>1), 'hreflang'=>array('a'=>1), 'hspace'=>array('applet'=>1, 'img'=>1, 'object'=>1), 'ismap'=>array('img'=>1, 'input'=>1), 'label'=>array('option'=>1, 'optgroup'=>1), 'language'=>array('script'=>1), 'longdesc'=>array('img'=>1, 'iframe'=>1), 'marginheight'=>array('iframe'=>1), 'marginwidth'=>array('iframe'=>1), 'maxlength'=>array('input'=>1), 'method'=>array('form'=>1), 'model'=>array('embed'=>1), 'multiple'=>array('select'=>1), 'name'=>array('button'=>1, 'embed'=>1, 'textarea'=>1, 'applet'=>1, 'select'=>1, 'form'=>1, 'iframe'=>1, 'img'=>1, 'a'=>1, 'input'=>1, 'object'=>1, 'map'=>1, 'param'=>1), 'nohref'=>array('area'=>1), 'noshade'=>array('hr'=>1), 'nowrap'=>array('td'=>1, 'th'=>1), 'object'=>array('applet'=>1), 'onblur'=>array('a'=>1, 'area'=>1, 'button'=>1, 'input'=>1, 'label'=>1, 'select'=>1, 'textarea'=>1), 'onchange'=>array('input'=>1, 'select'=>1, 'textarea'=>1), 'onfocus'=>array('a'=>1, 'area'=>1, 'button'=>1, 'input'=>1, 'label'=>1, 'select'=>1, 'textarea'=>1), 'onreset'=>array('form'=>1), 'onselect'=>array('input'=>1, 'textarea'=>1), 'onsubmit'=>array('form'=>1), 'pluginspage'=>array('embed'=>1), 'pluginurl'=>array('embed'=>1), 'prompt'=>array('isindex'=>1), 'readonly'=>array('textarea'=>1, 'input'=>1), 'rel'=>array('a'=>1), 'rev'=>array('a'=>1), 'rows'=>array('textarea'=>1), 'rowspan'=>array('td'=>1, 'th'=>1), 'rules'=>array('table'=>1), 'scope'=>array('td'=>1, 'th'=>1), 'scrolling'=>array('iframe'=>1), 'selected'=>array('option'=>1), 'shape'=>array('area'=>1, 'a'=>1), 'size'=>array('hr'=>1, 'font'=>1, 'input'=>1, 'select'=>1), 'span'=>array('col'=>1, 'colgroup'=>1), 'src'=>array('embed'=>1, 'script'=>1, 'input'=>1, 'iframe'=>1, 'img'=>1), 'standby'=>array('object'=>1), 'start'=>array('ol'=>1), 'summary'=>array('table'=>1), 'tabindex'=>array('a'=>1, 'area'=>1, 'button'=>1, 'input'=>1, 'object'=>1, 'select'=>1, 'textarea'=>1), 'target'=>array('a'=>1, 'area'=>1, 'form'=>1), 'type'=>array('a'=>1, 'embed'=>1, 'object'=>1, 'param'=>1, 'script'=>1, 'input'=>1, 'li'=>1, 'ol'=>1, 'ul'=>1, 'button'=>1), 'usemap'=>array('img'=>1, 'input'=>1, 'object'=>1), 'valign'=>array('col'=>1, 'colgroup'=>1, 'tbody'=>1, 'td'=>1, 'tfoot'=>1, 'th'=>1, 'thead'=>1, 'tr'=>1), 'value'=>array('input'=>1, 'option'=>1, 'param'=>1, 'button'=>1, 'li'=>1), 'valuetype'=>array('param'=>1), 'vspace'=>array('applet'=>1, 'img'=>1, 'object'=>1), 'width'=>array('embed'=>1, 'hr'=>1, 'iframe'=>1, 'img'=>1, 'object'=>1, 'table'=>1, 'td'=>1, 'th'=>1, 'applet'=>1, 'col'=>1, 'colgroup'=>1, 'pre'=>1), 'wmode'=>array('embed'=>1), 'xml:space'=>array('pre'=>1, 'script'=>1, 'style'=>1)); // Ele-specific
  623. static $aNE = array('checked'=>1, 'compact'=>1, 'declare'=>1, 'defer'=>1, 'disabled'=>1, 'ismap'=>1, 'multiple'=>1, 'nohref'=>1, 'noresize'=>1, 'noshade'=>1, 'nowrap'=>1, 'readonly'=>1, 'selected'=>1); // Empty
  624. static $aNP = array('action'=>1, 'cite'=>1, 'classid'=>1, 'codebase'=>1, 'data'=>1, 'href'=>1, 'longdesc'=>1, 'model'=>1, 'pluginspage'=>1, 'pluginurl'=>1, 'usemap'=>1); // Need scheme check; excludes style, on* & src
  625. static $aNU = array('class'=>array('param'=>1, 'script'=>1), 'dir'=>array('applet'=>1, 'bdo'=>1, 'br'=>1, 'iframe'=>1, 'param'=>1, 'script'=>1), 'id'=>array('script'=>1), 'lang'=>array('applet'=>1, 'br'=>1, 'iframe'=>1, 'param'=>1, 'script'=>1), 'xml:lang'=>array('applet'=>1, 'br'=>1, 'iframe'=>1, 'param'=>1, 'script'=>1), 'onclick'=>array('applet'=>1, 'bdo'=>1, 'br'=>1, 'font'=>1, 'iframe'=>1, 'isindex'=>1, 'param'=>1, 'script'=>1), 'ondblclick'=>array('applet'=>1, 'bdo'=>1, 'br'=>1, 'font'=>1, 'iframe'=>1, 'isindex'=>1, 'param'=>1, 'script'=>1), 'onkeydown'=>array('applet'=>1, 'bdo'=>1, 'br'=>1, 'font'=>1, 'iframe'=>1, 'isindex'=>1, 'param'=>1, 'script'=>1), 'onkeypress'=>array('applet'=>1, 'bdo'=>1, 'br'=>1, 'font'=>1, 'iframe'=>1, 'isindex'=>1, 'param'=>1, 'script'=>1), 'onkeyup'=>array('applet'=>1, 'bdo'=>1, 'br'=>1, 'font'=>1, 'iframe'=>1, 'isindex'=>1, 'param'=>1, 'script'=>1), 'onmousedown'=>array('applet'=>1, 'bdo'=>1, 'br'=>1, 'font'=>1, 'iframe'=>1, 'isindex'=>1, 'param'=>1, 'script'=>1), 'onmousemove'=>array('applet'=>1, 'bdo'=>1, 'br'=>1, 'font'=>1, 'iframe'=>1, 'isindex'=>1, 'param'=>1, 'script'=>1), 'onmouseout'=>array('applet'=>1, 'bdo'=>1, 'br'=>1, 'font'=>1, 'iframe'=>1, 'isindex'=>1, 'param'=>1, 'script'=>1), 'onmouseover'=>array('applet'=>1, 'bdo'=>1, 'br'=>1, 'font'=>1, 'iframe'=>1, 'isindex'=>1, 'param'=>1, 'script'=>1), 'onmouseup'=>array('applet'=>1, 'bdo'=>1, 'br'=>1, 'font'=>1, 'iframe'=>1, 'isindex'=>1, 'param'=>1, 'script'=>1), 'style'=>array('param'=>1, 'script'=>1), 'title'=>array('param'=>1, 'script'=>1)); // Univ & exceptions
  626. if($C['lc_std_val']){
  627. // predef attr vals for $eAL & $aNE ele
  628. static $aNL = array('all'=>1, 'baseline'=>1, 'bottom'=>1, 'button'=>1, 'center'=>1, 'char'=>1, 'checkbox'=>1, 'circle'=>1, 'col'=>1, 'colgroup'=>1, 'cols'=>1, 'data'=>1, 'default'=>1, 'file'=>1, 'get'=>1, 'groups'=>1, 'hidden'=>1, 'image'=>1, 'justify'=>1, 'left'=>1, 'ltr'=>1, 'middle'=>1, 'none'=>1, 'object'=>1, 'password'=>1, 'poly'=>1, 'post'=>1, 'preserve'=>1, 'radio'=>1, 'rect'=>1, 'ref'=>1, 'reset'=>1, 'right'=>1, 'row'=>1, 'rowgroup'=>1, 'rows'=>1, 'rtl'=>1, 'submit'=>1, 'text'=>1, 'top'=>1);
  629. static $eAL = array('a'=>1, 'area'=>1, 'bdo'=>1, 'button'=>1, 'col'=>1, 'form'=>1, 'img'=>1, 'input'=>1, 'object'=>1, 'optgroup'=>1, 'option'=>1, 'param'=>1, 'script'=>1, 'select'=>1, 'table'=>1, 'td'=>1, 'tfoot'=>1, 'th'=>1, 'thead'=>1, 'tr'=>1, 'xml:space'=>1);
  630. $lcase = isset($eAL[$e]) ? 1 : 0;
  631. }
  632. $depTr = 0;
  633. if($C['no_deprecated_attr']){
  634. // dep attr:applicable ele
  635. static $aND = array('align'=>array('caption'=>1, 'div'=>1, 'h1'=>1, 'h2'=>1, 'h3'=>1, 'h4'=>1, 'h5'=>1, 'h6'=>1, 'hr'=>1, 'img'=>1, 'input'=>1, 'legend'=>1, 'object'=>1, 'p'=>1, 'table'=>1), 'bgcolor'=>array('table'=>1, 'td'=>1, 'th'=>1, 'tr'=>1), 'border'=>array('img'=>1, 'object'=>1), 'bordercolor'=>array('table'=>1, 'td'=>1, 'tr'=>1), 'clear'=>array('br'=>1), 'compact'=>array('dl'=>1, 'ol'=>1, 'ul'=>1), 'height'=>array('td'=>1, 'th'=>1), 'hspace'=>array('img'=>1, 'object'=>1), 'language'=>array('script'=>1), 'name'=>array('a'=>1, 'form'=>1, 'iframe'=>1, 'img'=>1, 'map'=>1), 'noshade'=>array('hr'=>1), 'nowrap'=>array('td'=>1, 'th'=>1), 'size'=>array('hr'=>1), 'start'=>array('ol'=>1), 'type'=>array('li'=>1, 'ol'=>1, 'ul'=>1), 'value'=>array('li'=>1), 'vspace'=>array('img'=>1, 'object'=>1), 'width'=>array('hr'=>1, 'pre'=>1, 'td'=>1, 'th'=>1));
  636. static $eAD = array('a'=>1, 'br'=>1, 'caption'=>1, 'div'=>1, 'dl'=>1, 'form'=>1, 'h1'=>1, 'h2'=>1, 'h3'=>1, 'h4'=>1, 'h5'=>1, 'h6'=>1, 'hr'=>1, 'iframe'=>1, 'img'=>1, 'input'=>1, 'legend'=>1, 'li'=>1, 'map'=>1, 'object'=>1, 'ol'=>1, 'p'=>1, 'pre'=>1, 'script'=>1, 'table'=>1, 'td'=>1, 'th'=>1, 'tr'=>1, 'ul'=>1);
  637. $depTr = isset($eAD[$e]) ? 1 : 0;
  638. }
  639. // attr name-vals
  640. if(strpos($a, "\x01") !== false){
  641. $a = preg_replace('`\x01[^\x01]*\x01`', '', $a);
  642. } // No comment/CDATA sec
  643. $mode = 0; $a = trim($a, ' /'); $aA = array();
  644. while(strlen($a)){
  645. $w = 0;
  646. switch($mode){
  647. case 0: // Name
  648. if(preg_match('`^[a-zA-Z][\-a-zA-Z:]+`', $a, $m)){
  649. $nm = strtolower($m[0]);
  650. $w = $mode = 1; $a = ltrim(substr_replace($a, '', 0, strlen($m[0])));
  651. }
  652. break; case 1:
  653. if($a[0] == '='){
  654. // =
  655. $w = 1; $mode = 2; $a = ltrim($a, '= ');
  656. }else{ // No val
  657. $w = 1; $mode = 0; $a = ltrim($a);
  658. $aA[$nm] = '';
  659. }
  660. break; case 2: // Val
  661. if(preg_match('`^"[^"]*"`', $a, $m) or preg_match("`^'[^']*'`", $a, $m) or preg_match("`^\s*[^\s\"']+`", $a, $m)){
  662. $m = $m[0]; $w = 1; $mode = 0; $a = ltrim(substr_replace($a, '', 0, strlen($m)));
  663. $aA[$nm] = trim(($m[0] == '"' or $m[0] == '\'') ? substr($m, 1, -1) : $m);
  664. }
  665. break;
  666. }
  667. if($w == 0){
  668. // Parse errs, deal with space, " & '
  669. $a = preg_replace('`^(?:"[^"]*("|$)|\'[^\']*(\'|$)|\S)*\s*`', '', $a);
  670. $mode = 0;
  671. }
  672. }
  673. if($mode == 1){
  674. $aA[$nm] = '';
  675. }
  676. // clean attrs
  677. $S = vmFilter::$S;
  678. $rl = isset($S[$e]) ? $S[$e] : array();
  679. $a = array(); $nfr = 0;
  680. foreach($aA as $k=>$v){
  681. if(((isset($C['deny_attribute']['*']) ? isset($C['deny_attribute'][$k]) : !isset($C['deny_attribute'][$k])) or isset($rl[$k])) && ((!isset($rl['n'][$k]) && !isset($rl['n']['*'])) or isset($rl[$k])) && (isset($aN[$k][$e]) or (isset($aNU[$k]) && !isset($aNU[$k][$e])))){
  682. if(isset($aNE[$k])){
  683. $v = $k;
  684. }
  685. elseif(!empty($lcase) && (($e != 'button' or $e != 'input') or $k == 'type')){
  686. // Rather loose but ?not cause issues
  687. $v = (isset($aNL[($v2 = strtolower($v))])) ? $v2 : $v;
  688. }
  689. if($k == 'style' && !$C['style_pass']){
  690. if(false !== strpos($v, '&#')){
  691. static $sC = array('&#x20;'=>' ', '&#32;'=>' ', '&#x45;'=>'e', '&#69;'=>'e', '&#x65;'=>'e', '&#101;'=>'e', '&#x58;'=>'x', '&#88;'=>'x', '&#x78;'=>'x', '&#120;'=>'x', '&#x50;'=>'p', '&#80;'=>'p', '&#x70;'=>'p', '&#112;'=>'p', '&#x53;'=>'s', '&#83;'=>'s', '&#x73;'=>'s', '&#115;'=>'s', '&#x49;'=>'i', '&#73;'=>'i', '&#x69;'=>'i', '&#105;'=>'i', '&#x4f;'=>'o', '&#79;'=>'o', '&#x6f;'=>'o', '&#111;'=>'o', '&#x4e;'=>'n', '&#78;'=>'n', '&#x6e;'=>'n', '&#110;'=>'n', '&#x55;'=>'u', '&#85;'=>'u', '&#x75;'=>'u', '&#117;'=>'u', '&#x52;'=>'r', '&#82;'=>'r', '&#x72;'=>'r', '&#114;'=>'r', '&#x4c;'=>'l', '&#76;'=>'l', '&#x6c;'=>'l', '&#108;'=>'l', '&#x28;'=>'(', '&#40;'=>'(', '&#x29;'=>')', '&#41;'=>')', '&#x20;'=>':', '&#32;'=>':', '&#x22;'=>'"', '&#34;'=>'"', '&#x27;'=>"'", '&#39;'=>"'", '&#x2f;'=>'/', '&#47;'=>'/', '&#x2a;'=>'*', '&#42;'=>'*', '&#x5c;'=>'\\', '&#92;'=>'\\');
  692. $v = strtr($v, $sC);
  693. }
  694. $v = preg_replace_callback('`(url(?:\()(?: )*(?:\'|"|&(?:quot|apos);)?)(.+?)((?:\'|"|&(?:quot|apos);)?(?: )*(?:\)))`iS', 'vmFilter::hl_prot', $v);
  695. $v = !$C['css_expression'] ? preg_replace('`expression`i', ' ', preg_replace('`\\\\\S|(/|(%2f))(\*|(%2a))`i', ' ', $v)) : $v;
  696. }elseif(isset($aNP[$k]) or strpos($k, 'src') !== false or $k[0] == 'o'){
  697. $v = str_replace("\xad", ' ', (strpos($v, '&') !== false ? str_replace(array('&#xad;', '&#173;', '&shy;'), ' ', $v) : $v));
  698. $v = vmFilter::hl_prot($v, $k);
  699. if($k == 'href'){
  700. // X-spam
  701. if($C['anti_mail_spam'] && strpos($v, 'mailto:') === 0){
  702. $v = str_replace('@', htmlspecialchars($C['anti_mail_spam']), $v);
  703. }elseif($C['anti_link_spam']){
  704. $r1 = $C['anti_link_spam'][1];
  705. if(!empty($r1) && preg_match($r1, $v)){
  706. continue;
  707. }
  708. $r0 = $C['anti_link_spam'][0];
  709. if(!empty($r0) && preg_match($r0, $v)){
  710. if(isset($a['rel'])){
  711. if(!preg_match('`\bnofollow\b`i', $a['rel'])){
  712. $a['rel'] .= ' nofollow';
  713. }
  714. }elseif(isset($aA['rel'])){
  715. if(!preg_match('`\bnofollow\b`i', $aA['rel'])){
  716. $nfr = 1;
  717. }
  718. }else{$a['rel'] = 'nofollow';
  719. }
  720. }
  721. }
  722. }
  723. }
  724. if(isset($rl[$k]) && is_array($rl[$k]) && ($v = vmFilter::hl_attrval($v, $rl[$k])) === 0){
  725. continue;
  726. }
  727. $a[$k] = str_replace('"', '&quot;', $v);
  728. }
  729. }
  730. if($nfr){
  731. $a['rel'] = isset($a['rel']) ? $a['rel']. ' nofollow' : 'nofollow';
  732. }
  733. // rqd attr
  734. static $eAR = array('area'=>array('alt'=>'area'), 'bdo'=>array('dir'=>'ltr'), 'form'=>array('action'=>''), 'img'=>array('src'=>'', 'alt'=>'image'), 'map'=>array('name'=>''), 'optgroup'=>array('label'=>''), 'param'=>array('name'=>''), 'script'=>array('type'=>'text/javascript'), 'textarea'=>array('rows'=>'10', 'cols'=>'50'));
  735. if(isset($eAR[$e])){
  736. foreach($eAR[$e] as $k=>$v){
  737. if(!isset($a[$k])){
  738. $a[$k] = isset($v[0]) ? $v : $k;
  739. }
  740. }
  741. }
  742. // depr attrs
  743. if($depTr){
  744. $c = array();
  745. foreach($a as $k=>$v){
  746. if($k == 'style' or !isset($aND[$k][$e])){
  747. continue;
  748. }
  749. if($k == 'align'){
  750. unset($a['align']);
  751. if($e == 'img' && ($v == 'left' or $v == 'right')){
  752. $c[] = 'float: '. $v;
  753. }
  754. elseif(($e == 'div' or $e == 'table') && $v == 'center'){
  755. $c[] = 'margin: auto';
  756. }
  757. else{$c[] = 'text-align: '. $v;
  758. }
  759. }elseif($k == 'bgcolor'){
  760. unset($a['bgcolor']);
  761. $c[] = 'background-color: '. $v;
  762. }elseif($k == 'border'){
  763. unset($a['border']); $c[] = "border: {$v}px";
  764. }elseif($k == 'bordercolor'){
  765. unset($a['bordercolor']); $c[] = 'border-color: '. $v;
  766. }elseif($k == 'clear'){
  767. unset($a['clear']); $c[] = 'clear: '. ($v != 'all' ? $v : 'both');
  768. }elseif($k == 'compact'){
  769. unset($a['compact']); $c[] = 'font-size: 85%';
  770. }elseif($k == 'height' or $k == 'width'){
  771. unset($a[$k]); $c[] = $k. ': '. ($v[0] != '*' ? $v. (ctype_digit($v) ? 'px' : '') : 'auto');
  772. }elseif($k == 'hspace'){
  773. unset($a['hspace']); $c[] = "margin-left: {$v}px; margin-right: {$v}px";
  774. }elseif($k == 'language' && !isset($a['type'])){
  775. unset($a['language']);
  776. $a['type'] = 'text/'. strtolower($v);
  777. }elseif($k == 'name'){
  778. if($C['no_deprecated_attr'] == 2 or ($e != 'a' && $e != 'map')){
  779. unset($a['name']);
  780. }
  781. if(!isset($a['id']) && preg_match('`[a-zA-Z][a-zA-Z\d.:_\-]*`', $v)){
  782. $a['id'] = $v;
  783. }
  784. }elseif($k == 'noshade'){
  785. unset($a['noshade']); $c[] = 'border-style: none; border: 0; background-color: gray; color: gray';
  786. }elseif($k == 'nowrap'){
  787. unset($a['nowrap']); $c[] = 'white-space: nowrap';
  788. }elseif($k == 'size'){
  789. unset($a['size']); $c[] = 'size: '. $v. 'px';
  790. }elseif($k == 'start' or $k == 'value'){
  791. unset($a[$k]);
  792. }elseif($k == 'type'){
  793. unset($a['type']);
  794. static $ol_type = array('i'=>'lower-roman', 'I'=>'upper-roman', 'a'=>'lower-latin', 'A'=>'upper-latin', '1'=>'decimal');
  795. $c[] = 'list-style-type: '. (isset($ol_type[$v]) ? $ol_type[$v] : 'decimal');
  796. }elseif($k == 'vspace'){
  797. unset($a['vspace']); $c[] = "margin-top: {$v}px; margin-bottom: {$v}px";
  798. }
  799. }
  800. if(count($c)){
  801. $c = implode('; ', $c);
  802. $a['style'] = isset($a['style']) ? rtrim($a['style'], ' ;'). '; '. $c. ';': $c. ';';
  803. }
  804. }
  805. // unique ID
  806. if($C['unique_ids'] && isset($a['id'])){
  807. if(!preg_match('`^[A-Za-z][A-Za-z0-9_\-.:]*$`', ($id = $a['id'])) or (!is_null(vmFilter::$hl_Ids[$id]) && $C['unique_ids'] == 1)){
  808. unset($a['id']);
  809. }else{
  810. while(!is_null(vmFilter::$hl_Ids[$id])){
  811. $id = $C['unique_ids']. $id;
  812. }
  813. vmFilter::$hl_Ids[($a['id'] = $id)] = 1;
  814. }
  815. }
  816. // xml:lang
  817. if($C['xml:lang'] && isset($a['lang'])){
  818. $a['xml:lang'] = isset($a['xml:lang']) ? $a['xml:lang'] : $a['lang'];
  819. if($C['xml:lang'] == 2){
  820. unset($a['lang']);
  821. }
  822. }
  823. // for transformed tag
  824. if(!empty($trt)){
  825. $a['style'] = isset($a['style']) ? rtrim($a['style'], ' ;'). '; '. $trt : $trt;
  826. }
  827. // return with empty ele /
  828. if(empty($C['hook_tag'])){
  829. $aA = '';
  830. foreach($a as $k=>$v){
  831. $aA .= " {$k}=\"{$v}\"";
  832. }
  833. return "<{$e}{$aA}". (isset($eE[$e]) ? ' /' : ''). '>';
  834. }
  835. else{return $C['hook_tag']($e, $a);
  836. }
  837. // eof
  838. }
  839. public static function hl_tag2(&$e, &$a, $t=1){
  840. // transform tag
  841. if($e == 'center'){
  842. $e = 'div'; return 'text-align: center;';
  843. }
  844. if($e == 'dir' or $e == 'menu'){
  845. $e = 'ul'; return '';
  846. }
  847. if($e == 's' or $e == 'strike'){
  848. $e = 'span'; return 'text-decoration: line-through;';
  849. }
  850. if($e == 'u'){
  851. $e = 'span'; return 'text-decoration: underline;';
  852. }
  853. static $fs = array('0'=>'xx-small', '1'=>'xx-small', '2'=>'small', '3'=>'medium', '4'=>'large', '5'=>'x-large', '6'=>'xx-large', '7'=>'300%', '-1'=>'smaller', '-2'=>'60%', '+1'=>'larger', '+2'=>'150%', '+3'=>'200%', '+4'=>'300%');
  854. if($e == 'font'){
  855. $a2 = '';
  856. if(preg_match('`face\s*=\s*(\'|")([^=]+?)\\1`i', $a, $m) or preg_match('`face\s*=\s*([^"])(\S+)`i', $a, $m)){
  857. $a2 .= ' font-family: '. str_replace('"', '\'', trim($m[2])). ';';
  858. }
  859. if(preg_match('`color\s*=\s*(\'|")?(.+?)(\\1|\s|$)`i', $a, $m)){
  860. $a2 .= ' color: '. trim($m[2]). ';';
  861. }
  862. if(preg_match('`size\s*=\s*(\'|")?(.+?)(\\1|\s|$)`i', $a, $m) && isset($fs[($m = trim($m[2]))])){
  863. $a2 .= ' font-size: '. $fs[$m]. ';';
  864. }
  865. $e = 'span'; return ltrim($a2);
  866. }
  867. if($t == 2){
  868. $e = 0; return 0;
  869. }
  870. return '';
  871. // eof
  872. }
  873. public static function hl_tidy($t, $w, $p){
  874. // Tidy/compact HTM
  875. if(strpos(' pre,script,textarea', "$p,")){
  876. return $t;
  877. }
  878. $t = str_replace(' </', '</', preg_replace(array('`(<\w[^>]*(?<!/)>)\s+`', '`\s+`', '`(<\w[^>]*(?<!/)>) `'), array(' $1', ' ', '$1'), preg_replace_callback(array('`(<(!\[CDATA\[))(.+?)(\]\]>)`sm', '`(<(!--))(.+?)(-->)`sm', '`(<(pre|script|textarea)[^>]*?>)(.+?)(</\2>)`sm'), create_function('$m', 'return $m[1]. str_replace(array("<", ">", "\n", "\r", "\t", " "), array("\x01", "\x02", "\x03", "\x04", "\x05", "\x07"), $m[3]). $m[4];'), $t)));
  879. if(($w = strtolower($w)) == -1){
  880. return str_replace(array("\x01", "\x02", "\x03", "\x04", "\x05", "\x07"), array('<', '>', "\n", "\r", "\t", ' '), $t);
  881. }
  882. $s = strpos(" $w", 't') ? "\t" : ' ';
  883. $s = preg_match('`\d`', $w, $m) ? str_repeat($s, $m[0]) : str_repeat($s, ($s == "\t" ? 1 : 2));
  884. $n = preg_match('`[ts]([1-9])`', $w, $m) ? $m[1] : 0;
  885. $a = array('br'=>1);
  886. $b = array('button'=>1, 'input'=>1, 'option'=>1);
  887. $c = array('caption'=>1, 'dd'=>1, 'dt'=>1, 'h1'=>1, 'h2'=>1, 'h3'=>1, 'h4'=>1, 'h5'=>1, 'h6'=>1, 'isindex'=>1, 'label'=>1, 'legend'=>1, 'li'=>1, 'object'=>1, 'p'=>1, 'pre'=>1, 'td'=>1, 'textarea'=>1, 'th'=>1);
  888. $d = array('address'=>1, 'blockquote'=>1, 'center'=>1, 'colgroup'=>1, 'dir'=>1, 'div'=>1, 'dl'=>1, 'fieldset'=>1, 'form'=>1, 'hr'=>1, 'iframe'=>1, 'map'=>1, 'menu'=>1, 'noscript'=>1, 'ol'=>1, 'optgroup'=>1, 'rbc'=>1, 'rtc'=>1, 'ruby'=>1, 'script'=>1, 'select'=>1, 'table'=>1, 'tfoot'=>1, 'thead'=>1, 'tr'=>1, 'ul'=>1);
  889. ob_start();
  890. if(isset($d[$p])){
  891. echo str_repeat($s, ++$n);
  892. }
  893. $t = explode('<', $t);
  894. echo ltrim(array_shift($t));
  895. for($i=-1, $j=count($t); ++$i<$j;){
  896. $r = ''; list($e, $r) = explode('>', $t[$i]);
  897. $x = $e[0] == '/' ? 0 : (substr($e, -1) == '/' ? 1 : ($e[0] != '!' ? 2 : -1));
  898. $y = !$x ? ltrim($e, '/')

Large files files are truncated, but you can click here to view the full file