PageRenderTime 27ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/HtmLawed/htmLawed/htmLawedTest.php

https://github.com/Emaratilicious/Garden
PHP | 592 lines | 511 code | 42 blank | 39 comment | 118 complexity | 755466336c8e722ffa91a88f213f878c MD5 | raw file
  1. <?php
  2. /*
  3. htmLawedTest.php, 16 July 2009
  4. htmLawed 1.1.9.3, 17 May 2010
  5. Copyright Santosh Patnaik
  6. LGPL v3 license
  7. A PHP Labware internal utility - http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed
  8. Test htmLawed; user provides text input; input and processed input are shown as highlighted code and rendered HTML; also shown are execution time and peak memory usage
  9. */
  10. // config
  11. $_errs = 0; // display PHP errors
  12. $_limit = 8000; // input character limit
  13. // more config
  14. $_hlimit = 1000; // input character limit for showing hexdumps
  15. $_hilite = 1; // 0 turns off slow Javascript-based code-highlighting, e.g., if $_limit is high
  16. $_w3c_validate = 1; // 1 to show buttons to send input/output to w3c validator
  17. $_sid = 'sid'; // session name; alphanum.
  18. $_slife = 30; // session life in min.
  19. // errors
  20. error_reporting(E_ALL | (defined('E_STRICT') ? E_STRICT : 1));
  21. ini_set('display_errors', $_errs);
  22. // session
  23. session_name($_sid);
  24. session_cache_limiter('private');
  25. session_cache_expire($_slife);
  26. ini_set('session.gc_maxlifetime', $_slife * 60);
  27. ini_set('session.use_only_cookies', 1);
  28. ini_set('session.cookie_lifetime', 0);
  29. session_start();
  30. if(!isset($_SESSION['token'])){
  31. $_SESSION['token'] = md5(uniqid(rand(), 1));
  32. }
  33. // slashes
  34. if(get_magic_quotes_gpc()){
  35. foreach($_POST as $k => $v){
  36. $_POST[$k] = stripslashes($v);
  37. }
  38. ini_set('magic_quotes_gpc', 0);
  39. }
  40. set_magic_quotes_runtime(0);
  41. $_POST['enc'] = (isset($_POST['enc']) and preg_match('`^[-\w]+$`', $_POST['enc'])) ? $_POST['enc'] : 'utf-8';
  42. // token for anti-CSRF
  43. if(count($_POST)){
  44. if((empty($_GET['pre']) and ((!empty($_POST['token']) and !empty($_SESSION['token']) and $_POST['token'] != $_SESSION['token']) or empty($_POST[$_sid]) or $_POST[$_sid] != session_id() or empty($_COOKIE[$_sid]) or $_COOKIE[$_sid] != session_id())) or ($_POST[$_sid] != session_id())){
  45. $_POST = array('enc'=>'utf-8');
  46. }
  47. }
  48. if(empty($_GET['pre'])){
  49. $_SESSION['token'] = md5(uniqid(rand(), 1));
  50. $token = $_SESSION['token'];
  51. session_regenerate_id(1);
  52. }
  53. // compress
  54. if(function_exists('gzencode') && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && preg_match('`gzip|deflate`i', $_SERVER['HTTP_ACCEPT_ENCODING']) && !ini_get('zlib.output_compression')){
  55. ob_start('ob_gzhandler');
  56. }
  57. // HTM for unprocessed
  58. if(isset($_POST['inputH'])){
  59. echo '<html><head><title>htmLawed test: HTML view of unprocessed input</title></head><body style="margin:0; padding: 0;"><p style="background-color: black; color: white; padding: 2px;">&nbsp; Rendering of unprocessed input without an HTML doctype or charset declaration &nbsp; &nbsp; <small><a style="color: white; text-decoration: none;" href="1" onclick="javascript:window.close(this); return false;">close window</a> | <a style="color: white; text-decoration: none;" href="htmLawedTest.php" onclick="javascript: window.open(\'htmLawedTest.php\', \'hlmain\'); window.close(this); return false;">htmLawed test page</a></small></p><div>', $_POST['inputH'], '</div></body></html>';
  60. exit;
  61. }
  62. // main
  63. $_POST['text'] = isset($_POST['text']) ? $_POST['text'] : 'text to process; < '. $_limit. ' characters'. ($_hlimit ? ' (for binary hexdump view, < '. $_hlimit. ')' : '');
  64. $do = (!empty($_POST[$_sid]) && isset($_POST['text'][0]) && !isset($_POST['text'][$_limit])) ? 1 : 0;
  65. $limit_exceeded = isset($_POST['text'][$_limit]) ? 1 : 0;
  66. $pre_mem = memory_get_usage();
  67. $validation = (!empty($_POST[$_sid]) and isset($_POST['w3c_validate'][0])) ? 1 : 0;
  68. include './htmLawed.php';
  69. function format($t){
  70. $t = "\n". str_replace(array("\t", "\r\n", "\r", '&', '<', '>', "\n"), array(' ', "\n", "\n", '&amp;', '&lt;', '&gt;', "<span class=\"newline\">&#172;</span><br />\n"), $t);
  71. return str_replace(array('<br />', "\n ", ' '), array("\n<br />\n", "\n&nbsp;", ' &nbsp;'), $t);
  72. }
  73. function hexdump($d){
  74. // Mainly by Aidan Lister <aidan@php.net>, Peter Waller <iridum@php.net>
  75. $hexi = '';
  76. $ascii = '';
  77. ob_start();
  78. echo '<pre>';
  79. $offset = 0;
  80. $len = strlen($d);
  81. for($i=$j=0; $i<$len; $i++)
  82. {
  83. // Convert to hexidecimal
  84. $hexi .= sprintf("%02X ", ord($d[$i]));
  85. // Replace non-viewable bytes with '.'
  86. if(ord($d[$i]) >= 32){
  87. $ascii .= htmlspecialchars($d[$i]);
  88. }else{
  89. $ascii .= '.';
  90. }
  91. // Add extra column spacing
  92. if($j == 7){
  93. $hexi .= ' ';
  94. $ascii .= ' ';
  95. }
  96. // Add row
  97. if(++$j == 16 || $i == $len-1){
  98. // Join the hexi / ascii output
  99. echo sprintf("%04X %-49s %s", $offset, $hexi, $ascii);
  100. // Reset vars
  101. $hexi = $ascii = '';
  102. $offset += 16;
  103. $j = 0;
  104. // Add newline
  105. if ($i !== $len-1){
  106. echo "\n";
  107. }
  108. }
  109. }
  110. echo '</pre>';
  111. $o = ob_get_contents();
  112. ob_end_clean();
  113. return $o;
  114. }
  115. ?>
  116. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  117. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  118. <html lang="en" xml:lang="en">
  119. <head>
  120. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  121. <meta name="description" content="htmLawed <?php echo hl_version();?> test page" />
  122. <style type="text/css"><!--/*--><![CDATA[/*><!--*/
  123. a, a.resizer{text-decoration:none;}
  124. a:hover, a.resizer:hover{color:red;}
  125. a.resizer{color:green; float:right;}
  126. body{background-color:#efefef;}
  127. body, button, div, html, input, p{font-size:13px; font-family:'Lucida grande', Verdana, Arial, Helvetica, sans-serif;}
  128. button, input{font-size: 85%;}
  129. div.help{border-top: 1px dotted gray; margin-top: 15px; padding-top: 15px; color:#999999;}
  130. #inputC, #inputD, #inputF, #inputR, #outputD, #outputF, #outputH, #outputR, #settingF{display:block;}
  131. #inputC, #settingF{background-color:white; border:1px gray solid; padding:3px;}
  132. #inputC li{margin: 0; padding: 0;}
  133. #inputC ul{margin: 0; padding: 0; margin-left: 14px;}
  134. #inputC input{margin: 0; margin-left: 2px; margin-right: 2px; padding: 1px; vertical-align: middle;}
  135. #inputD{overflow:auto; background-color:#ffff99; border:1px #cc9966 solid; padding:3px;}
  136. #inputR{overflow:auto; background-color:#ffffcc; border:1px #ffcc99 solid; padding:3px;}
  137. #inputC, #settingF, #inputD, #inputR, #outputD, #outputR, textarea{font-size:100%; font-family:'Bitstream vera sans mono', 'courier new', 'courier', monospace;}
  138. #outputD{overflow:auto; background-color: #99ffcc; border:1px #66cc99 solid; padding:3px;}
  139. #outputH{overflow:auto; background-color:white; padding:3px; border:1px #dcdcdc solid;}
  140. #outputR{overflow:auto; background-color: #ccffcc; border:1px #99cc99 solid; padding:3px;}
  141. span.cmtcdata{color: orange;}
  142. span.ctag{color:red;}
  143. span.ent{border-bottom:1px dotted #999999;}
  144. span.etag{color:purple;}
  145. span.help{color:#999999;}
  146. span.newline{color:#dcdcdc;}
  147. span.notice{color:green;}
  148. span.otag{color:blue;}
  149. #topmost{margin:auto; width:98%;}
  150. /*]]>*/--></style>
  151. <script type="text/javascript"><!--//--><![CDATA[//><!--
  152. window.name = 'hlmain';
  153. function hl(i){
  154. <?php if(!$_hilite){echo 'return;'; }?>
  155. var e = document.getElementById(i);
  156. if(!e){return;}
  157. run(e, '</[a-z1-6]+>', 'ctag');
  158. run(e, '<[a-z]+(?:[^>]*)/>', 'etag');
  159. run(e, '<[a-z1-6]+(?:[^>]*)>', 'otag');
  160. run(e, '&[#a-z0-9]+;', 'ent');
  161. run(e, '<!(?:(?:--(?:.|\n)*?--)|(?:\\[CDATA\\[(?:.|\n)*?\\]\\]))>', 'cmtcdata');
  162. }
  163. function sndProc(){
  164. var f = document.getElementById('testform');
  165. if(!f){return;}
  166. var e = document.createElement('input');
  167. e.type = 'hidden';
  168. e.name = '<?php echo htmlspecialchars($_sid); ?>';
  169. e.id = '<?php echo htmlspecialchars($_sid); ?>';
  170. e.value = readCookie('<?php echo htmlspecialchars($_sid); ?>');
  171. f.appendChild(e);
  172. f.submit();
  173. }
  174. function readCookie(n){
  175. var ne = n + '=';
  176. var ca = document.cookie.split(';');
  177. for(var i=0;i < ca.length;i++){
  178. var c = ca[i];
  179. while(c.charAt(0)==' '){
  180. c = c.substring(1,c.length);
  181. }
  182. if(c.indexOf(ne) == 0){
  183. return c.substring(ne.length,c.length);
  184. }
  185. }
  186. return null;
  187. }
  188. function run(e, q, c){
  189. var q = new RegExp(q);
  190. if(e.firstChild == null){
  191. var m = q.exec(e.data);
  192. if(m){
  193. var v = m[0];
  194. var k2 = e.splitText(m.index);
  195. var k3 = k2.splitText(v.length);
  196. var s = e.ownerDocument.createElement('span');
  197. e.parentNode.replaceChild(s, k2);
  198. s.className = c; s.appendChild(k2);
  199. }
  200. }
  201. for(var k = e.firstChild; k != null; k = k.nextSibling){
  202. if(k.nodeType == 3){
  203. var m = q.exec(k.data);
  204. if(m){
  205. var v = m[0];
  206. var k2 = k.splitText(m.index);
  207. var k3 = k2.splitText(v.length);
  208. var s = k.ownerDocument.createElement('span');
  209. k.parentNode.replaceChild(s, k2);
  210. s.className = c; s.appendChild(k2);
  211. }
  212. }
  213. else if(c == 'ent' && k.nodeType == 1){
  214. var d = k.firstChild;
  215. if(d){
  216. var m = q.exec(d.data);
  217. if(m){
  218. var v = m[0];
  219. var d2 = d.splitText(m.index);
  220. var d3 = d2.splitText(v.length);
  221. var s = d.ownerDocument.createElement('span');
  222. d.parentNode.replaceChild(s, d2);
  223. s.className = c; s.appendChild(d2);
  224. }
  225. }
  226. }
  227. }
  228. }
  229. function toggle(i){
  230. var e = document.getElementById(i);
  231. if(!e){return;}
  232. if(e.style){
  233. var a = e.style.display;
  234. if(a == 'block'){e.style.display = 'none'; return;}
  235. if(a == 'none'){e.style.display = 'block';}
  236. else{e.style.display = 'none';}
  237. return;
  238. }
  239. var a = e.visibility;
  240. if(a == 'hidden'){e.visibility = 'show'; return;}
  241. if(a == 'show'){e.visibility = 'hidden';}
  242. }
  243. function sndUnproc(){
  244. var i = document.getElementById('text');
  245. if(!i){return;}
  246. i = i.value;
  247. i = i.replace(/>/g, '&gt;');
  248. i = i.replace(/</g, '&lt;');
  249. i = i.replace(/"/g, '&quot;');
  250. var w = window.open('htmLawedTest.php?pre=1', 'hlprehtm');
  251. var f = document.createElement('form');
  252. f.enctype = 'application/x-www-form-urlencoded';
  253. f.method = 'post';
  254. f.acceptCharset = '<?php echo htmlspecialchars($_POST['enc']); ?>';
  255. if(f.style){f.style.display = 'none';}
  256. else{f.visibility = 'hidden';}
  257. f.innerHTML = '<p style="display:none;"><input style="display:none;" type="hidden" name="token" id="token" value="<?php echo $token; ?>" /><input style="display:none;" type="hidden" name="<?php echo htmlspecialchars($_sid); ?>" id="<?php echo htmlspecialchars($_sid); ?>" value="' + readCookie('<?php echo htmlspecialchars($_sid); ?>') + '" /><input style="display:none;" type="hidden" name="inputH" id="inputH" value="'+ i+ '" /></p>';
  258. f.action = 'htmLawedTest.php?pre=1';
  259. f.target = 'hlprehtm';
  260. f.method = 'post';
  261. var b = document.getElementsByTagName('body')[0];
  262. b.appendChild(f);
  263. f.submit();
  264. w.focus;
  265. }
  266. function sndValidn(id, type){
  267. var i = document.getElementById(id);
  268. if(!i){return;}
  269. i = i.value;
  270. i = i.replace(/>/g, '&gt;');
  271. i = i.replace(/</g, '&lt;');
  272. i = i.replace(/"/g, '&quot;');
  273. var w = window.open('http://validator.w3.org/check', 'validate'+id+type);
  274. var f = document.createElement('form');
  275. f.enctype = 'application/x-www-form-urlencoded';
  276. f.method = 'post';
  277. f.acceptCharset = '<?php echo htmlspecialchars($_POST['enc']); ?>';
  278. if(f.style){f.style.display = 'none';}
  279. else{f.visibility = 'hidden';}
  280. f.innerHTML = '<p style="display:none;"><input style="display:none;" type="hidden" name="fragment" id="fragment" value="'+ i+ '" /><input style="display:none;" type="hidden" name="prefill" id="prefill" value="1" /><input style="display:none;" type="hidden" name="prefill_doctype" id="prefill_doctype" value="'+ type+ '" /><input style="display:none;" type="hidden" name="group" id="group" value="1" /><input type="hidden" name="ss" id="ss" value="1" /></p>';
  281. f.action = 'http://validator.w3.org/check';
  282. f.target = 'validate'+id+type;
  283. var b = document.getElementsByTagName('body')[0];
  284. b.appendChild(f);
  285. f.submit();
  286. w.focus;
  287. }
  288. tRs = {
  289. formEl: null,
  290. resizeClass: 'textarea',
  291. adEv: function(t,ev,fn){
  292. if(typeof document.addEventListener != 'undefined'){
  293. t.addEventListener(ev,fn,false);
  294. }else{
  295. t.attachEvent('on' + ev, fn);
  296. }
  297. },
  298. rmEv: function(t,ev,fn){
  299. if(typeof document.removeEventListener != 'undefined'){
  300. t.removeEventListener(ev,fn,false);
  301. }else
  302. {
  303. t.detachEvent('on' + ev, fn);
  304. }
  305. },
  306. adBtn: function(){
  307. var textareas = document.getElementsByTagName('textarea');
  308. for(var i = 0; i < textareas.length; i++){
  309. var txtclass=textareas[i].className;
  310. if(txtclass.substring(0,tRs.resizeClass.length)==tRs.resizeClass ||
  311. txtclass.substring(txtclass.length -tRs.resizeClass.length)==tRs.resizeClass){
  312. var a = document.createElement('a');
  313. a.appendChild(document.createTextNode("\u2195"));
  314. a.style.cursor = 'n-resize';
  315. a.className= 'resizer';
  316. a.title = 'click-drag to resize'
  317. tRs.adEv(a, 'mousedown', tRs.initResize);
  318. textareas[i].parentNode.appendChild(a);
  319. }
  320. }
  321. },
  322. initResize: function(event){
  323. if(typeof event == 'undefined'){
  324. event = window.event;
  325. }
  326. if(event.srcElement){
  327. var target = event.srcElement.previousSibling;
  328. }else{
  329. var target = event.target.previousSibling;
  330. }
  331. if(target.nodeName.toLowerCase() == 'textarea' || (target.nodeName.toLowerCase() == 'input' && target.type == 'text')){
  332. tRs.formEl = target;
  333. tRs.formEl.startHeight = tRs.formEl.clientHeight;
  334. tRs.formEl.startY = event.clientY;
  335. tRs.adEv(document, 'mousemove', tRs.resize);
  336. tRs.adEv(document, 'mouseup', tRs.stopResize);
  337. tRs.formEl.parentNode.style.cursor = 'n-resize';
  338. tRs.formEl.style.cursor = 'n-resize';
  339. try{
  340. event.preventDefault();
  341. }catch(e){
  342. }
  343. }
  344. },
  345. resize: function(event){
  346. if(typeof event == 'undefined'){
  347. event = window.event;
  348. }
  349. if(tRs.formEl.nodeName.toLowerCase() == 'textarea'){
  350. tRs.formEl.style.height = event.clientY - tRs.formEl.startY + tRs.formEl.startHeight + 'px';
  351. }
  352. },
  353. stopResize: function(event){
  354. tRs.rmEv(document, 'mousedown', tRs.initResize);
  355. tRs.rmEv(document, 'mousemove', tRs.resize);
  356. tRs.formEl.style.cursor = 'text';
  357. tRs.formEl.parentNode.style.cursor = 'auto';
  358. return false;
  359. }
  360. };
  361. tRs.adEv(window, 'load', tRs.adBtn);
  362. //--><!]]></script>
  363. <title>htmLawed (<?php echo hl_version();?>) test</title>
  364. </head>
  365. <body>
  366. <div id="topmost">
  367. <h5 style="float: left; display: inline; margin-top: 0; margin-bottom: 5px;"><a href="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/index.php" title="htmLawed home">HTM<big><big>L</big></big>AWED</a> <?php echo hl_version();?> <a href="htmLawedTest.php" title="test home">TEST</a></h5>
  368. <span style="float: right;" class="help"><a href="htmLawed_README.htm"><span class="notice">htm</span></a> / <a href="htmLawed_README.txt"><span class="notice">txt</span></a> documentation</span><br style="clear:both;" />
  369. <a href="htmLawedTest.php" title="[toggle visibility] type or copy-paste" onclick="javascript:toggle('inputF'); return false;"><span class="notice">Input &raquo;</span> <span class="help" title="limit lower with multibyte characters<?php echo (($_hlimit < $_limit && $_hlimit)? '; limit is '. $_hlimit. ' for viewing binaries' : ''); ?>"><small>(max. <?php echo htmlspecialchars($_limit);?> chars)</small></span></a>
  370. <form id="testform" name="testform" action="htmLawedTest.php" method="post" accept-charset="<?php echo htmlspecialchars($_POST['enc']); ?>" style="padding:0; margin: 0; display:inline;">
  371. <div id="inputF" style="display: block;">
  372. <input type="hidden" name="token" id="token" value="<?php echo $token; ?>" />
  373. <div><textarea id="text" class="textarea" name="text" rows="5" cols="100" style="width: 100%;"><?php echo htmlspecialchars($_POST['text']);?></textarea></div>
  374. <input type="submit" id="submitF" name="submitF" value="Process" style="float:left;" title="filter using htmLawed" onclick="javascript: sndProc(); return false;" onkeypress="javascript: sndProc(); return false;" />
  375. <?php
  376. if($do){
  377. if($validation){
  378. echo '<input type="hidden" value="1" name="w3c_validate" id="w3c_validate" />';
  379. }
  380. ?>
  381. <button type="button" title="rendered as web-page without a doctype or charset declaration" style="float: right;" onclick="javascript: sndUnproc(); return false;" onkeypress="javascript: sndUnproc(); return false;">View unprocessed</button>
  382. <button type="button" onclick="javascript:document.getElementById('text').focus();document.getElementById('text').select()" title="select all to copy" style="float:right;">Select all</button>
  383. <?php
  384. if($_w3c_validate && $validation){
  385. ?>
  386. <button type="button" title="HTML 4.01 W3C online validation" style="float: right;" onclick="javascript: sndValidn('text', 'html401'); return false;" onkeypress="javascript: sndValidn('text', 'html401'); return false;">Check HTML</button>
  387. <button type="button" title="XHTML 1.1 W3C online validation" style="float: right;" onclick="javascript: sndValidn('text', 'xhtml110'); return false;" onkeypress="javascript: sndValidn('text', 'xhtml110'); return false;">Check XHTML</button>
  388. <?php
  389. }
  390. }
  391. else{
  392. if($_w3c_validate){
  393. echo '<span style="float: right;" class="help" title="for direct submission of input or output code to W3C validator for (X)HTML validation"><span style="font-size: 85%;">&nbsp;Validator tools: </span><input type="checkbox" value="1" name="w3c_validate" id="w3c_validate" style="vertical-align: middle;"', ($validation ? ' checked="checked"' : ''), ' /></span>';
  394. }
  395. }
  396. ?>
  397. <span style="float:right;" class="help"><span style="font-size: 85%;">Encoding: </span><input type="text" size="8" id="enc" name="enc" style="vertical-align: middle;" value="<?php echo htmlspecialchars($_POST['enc']); ?>" title="IANA-recognized name of the input character-set; can be multiple ;- or space-separated values; may not work in some browsers" /></span>
  398. </div>
  399. <br style="clear:both;" />
  400. <?php
  401. if($limit_exceeded){
  402. echo '<br /><strong>Input text is too long!</strong><br />';
  403. }
  404. ?>
  405. <br />
  406. <a href="htmLawedTest.php" title="[toggle visibility] htmLawed configuration" onclick="javascript:toggle('inputC'); return false;"><span class="notice">Settings &raquo;</span></a>
  407. <div id="inputC" style="display: none;">
  408. <table summary="none">
  409. <tr>
  410. <td><span class="help" title="$config argument">Config:</span></td>
  411. <td><ul>
  412. <?php
  413. $cfg = array(
  414. 'abs_url'=>array('3', '0', 'absolute/relative URL conversion', '-1'),
  415. 'and_mark'=>array('2', '0', 'mark original <em>&amp;</em> chars', '0', 'd'=>1), // 'd' to disable
  416. 'anti_link_spam'=>array('1', '0', 'modify <em>href</em> values as an anti-link spam measure', '0', array(array('30', '1', '', 'regex for extra <em>rel</em>'), array('30', '2', '', 'regex for no <em>href</em>'))),
  417. 'anti_mail_spam'=>array('1', '0', 'replace <em>@</em> in <em>mailto:</em> URLs', '0', '8', 'NO@SPAM', 'replacement'),
  418. 'balance'=>array('2', '1', 'fix nestings and balance tags', '0'),
  419. 'base_url'=>array('', '', 'base URL', '25'),
  420. 'cdata'=>array('4', 'nil', 'allow <em>CDATA</em> sections', 'nil'),
  421. 'clean_ms_char'=>array('3', '0', 'replace bad characters introduced by Microsoft apps. like <em>Word</em>', '0'),
  422. 'comment'=>array('4', 'nil', 'allow HTML comments', 'nil'),
  423. 'css_expression'=>array('2', 'nil', 'allow dynamic expressions in CSS style properties', 'nil'),
  424. 'deny_attribute'=>array('1', '0', 'denied attributes', '0', '50', '', 'these'),
  425. 'elements'=>array('', '', 'allowed elements', '50'),
  426. 'hexdec_entity'=>array('3', '1', 'convert hexadecimal numeric entities to decimal ones, or vice versa', '0'),
  427. 'hook'=>array('', '', 'name of hook function', '25'),
  428. 'hook_tag'=>array('', '', 'name of custom function to further check attribute values', '25'),
  429. 'keep_bad'=>array('7', '6', 'keep, or remove <em>bad</em> tag content', '0'),
  430. 'lc_std_val'=>array('2', '1', 'lower-case std. attribute values like <em>radio</em>', '0'),
  431. 'make_tag_strict'=>array('3', 'nil', 'transform deprecated elements', 'nil'),
  432. 'named_entity'=>array('2', '1', 'allow named entities, or convert numeric ones', '0'),
  433. 'no_deprecated_attr'=>array('3', '1', 'allow deprecated attributes, or transform them', '0'),
  434. 'parent'=>array('', 'div', 'name of parent element', '25'),
  435. 'safe'=>array('2', '0', 'for most <em>safe</em> HTML', '0'),
  436. 'schemes'=>array('', 'href: aim, feed, file, ftp, gopher, http, https, irc, mailto, news, nntp, sftp, ssh, telnet; *:file, http, https', 'allowed URL protocols', '50'),
  437. 'show_setting'=>array('', 'htmLawed_setting', 'variable name to record <em>finalized</em> htmLawed settings', '25', 'd'=>1),
  438. 'style_pass'=>array('2', 'nil', 'do not look at <em>style</em> attribute values', 'nil'),
  439. 'tidy'=>array('3', '0', 'beautify/compact', '-1', '8', '1t1', 'format'),
  440. 'unique_ids'=>array('2', '1', 'unique <em>id</em> values', '0', '8', 'my_', 'prefix'),
  441. 'valid_xhtml'=>array('2', 'nil', 'auto-set various parameters for most valid XHTML', 'nil'),
  442. 'xml:lang'=>array('3', 'nil', 'auto-add <em>xml:lang</em> attribute', '0'),
  443. );
  444. foreach($cfg as $k=>$v){
  445. echo '<li>', $k, ': ';
  446. if(!empty($v[0])){ // input radio
  447. $j = $v[3];
  448. for($i = $j-1; ++$i < $v[0]+$v[3];++$j){
  449. echo '<input type="radio" name="h', $k, '" value="', $i, '"', (!isset($_POST['h'. $k]) ? ($v[1] == $i ? ' checked="checked"' : '') : ($_POST['h'. $k] == $i ? ' checked="checked"' : '')), (isset($v['d']) ? ' disabled="disabled"' : ''), ' />', $i, ' ';
  450. }
  451. if($v[1] == 'nil'){
  452. echo '<input type="radio" name="h', $k, '" value="nil"', ((!isset($_POST['h'. $k]) or $_POST['h'. $k] == 'nil') ? ' checked="checked"' : ''), (isset($v['d']) ? ' disabled="disabled"' : ''), ' />not set ';
  453. }
  454. if(!empty($v[4])){ // + input text box
  455. echo '<input type="radio" name="h', $k, '" value="', $j, '"', (((isset($_POST['h'. $k]) && $_POST['h'. $k] == $j) or (!isset($_POST['h'. $k]) && $j == $v[1])) ? ' checked="checked"' : ''), (isset($v['d']) ? ' disabled="disabled"' : ''), ' />';
  456. if(!is_array($v[4])){
  457. echo $v[6], ': <input type="text" size="', $v[4], '" name="h', $k. $j, '" value="', htmlspecialchars(isset($_POST['h'. $k. $j][0]) ? $_POST['h'. $k. $j] : $v[5]), '"', (isset($v['d']) ? ' disabled="disabled"' : ''), ' />';
  458. }
  459. else{
  460. foreach($v[4] as $z){
  461. echo ' ', $z[3], ': <input type="text" size="', $z[0], '" name="h', $k. $j. $z[1], '" value="', htmlspecialchars(isset($_POST['h'. $k. $j. $z[1]][0]) ? $_POST['h'. $k. $j. $z[1]] : $z[2]), '"', (isset($v['d']) ? ' disabled="disabled"' : ''), ' />';
  462. }
  463. }
  464. }
  465. }
  466. elseif(ctype_digit($v[3])){ // input text
  467. echo '<input type="text" size="', $v[3], '" name="h', $k, '" value="', htmlspecialchars(isset($_POST['h'. $k][0]) ? $_POST['h'. $k] : $v[1]), '"', (isset($v['d']) ? ' disabled="disabled"' : ''), ' />';
  468. }
  469. else{} // text-area
  470. echo ' <span class="help">', $v[2], '</span></li>';
  471. }
  472. echo '</ul></td></tr><tr><td><span style="vertical-align: top;" class="help" title="$spec argument: element-specific attribute rules">Spec:</span></td><td><textarea name="spec" id="spec" cols="70" rows="3" style="width:80%;">', htmlspecialchars((isset($_POST['spec']) ? $_POST['spec'] : '')), '</textarea></td></tr></table>';
  473. ?>
  474. </div>
  475. </form>
  476. <?php
  477. if($do){
  478. $cfg = array();
  479. foreach($_POST as $k=>$v){
  480. if($k[0] == 'h' && $v != 'nil'){
  481. $cfg[substr($k, 1)] = $v;
  482. }
  483. }
  484. if($cfg['anti_link_spam'] && (!empty($cfg['anti_link_spam11']) or !empty($cfg['anti_link_spam12']))){
  485. $cfg['anti_link_spam'] = array($cfg['anti_link_spam11'], $cfg['anti_link_spam12']);
  486. }
  487. unset($cfg['anti_link_spam11'], $cfg['anti_link_spam12']);
  488. if($cfg['anti_mail_spam'] == 1){
  489. $cfg['anti_mail_spam'] = isset($cfg['anti_mail_spam1'][0]) ? $cfg['anti_mail_spam1'] : 0;
  490. }
  491. unset($cfg['anti_mail_spam11']);
  492. if($cfg['deny_attribute'] == 1){
  493. $cfg['deny_attribute'] = isset($cfg['deny_attribute1'][0]) ? $cfg['deny_attribute1'] : 0;
  494. }
  495. unset($cfg['deny_attribute1']);
  496. if($cfg['tidy'] == 2){
  497. $cfg['tidy'] = isset($cfg['tidy2'][0]) ? $cfg['tidy2'] : 0;
  498. }
  499. unset($cfg['tidy2']);
  500. if($cfg['unique_ids'] == 2){
  501. $cfg['unique_ids'] = isset($cfg['unique_ids2'][0]) ? $cfg['unique_ids2'] : 1;
  502. }
  503. unset($cfg['unique_ids2']);
  504. unset($cfg['and_mark']); // disabling and_mark
  505. $cfg['show_setting'] = 'hlcfg';
  506. $st = microtime();
  507. $out = htmLawed($_POST['text'], $cfg, str_replace(array('$', '{'), '', $_POST['spec']));
  508. $et = microtime();
  509. echo '<br /><a href="htmLawedTest.php" title="[toggle visibility] syntax-highlighted" onclick="javascript:toggle(\'inputR\'); return false;"><span class="notice">Input code &raquo;</span></a> <span class="help" title="tags estimated as half of total &gt; and &lt; chars; values may be inaccurate for non-ASCII text"><small><big>', strlen($_POST['text']), '</big> chars, ~<big>', round((substr_count($_POST['text'], '>') + substr_count($_POST['text'], '<'))/2), '</big> tags</small>&nbsp;</span><div id="inputR" style="display: none;">', format($_POST['text']), '</div><script type="text/javascript">hl(\'inputR\');</script>', (!isset($_POST['text'][$_hlimit]) ? ' <a href="htmLawedTest.php" title="[toggle visibility] hexdump; non-viewable characters like line-returns are shown as dots" onclick="javascript:toggle(\'inputD\'); return false;"><span class="notice">Input binary &raquo;&nbsp;</span></a><div id="inputD" style="display: none;">'. hexdump($_POST['text']). '</div>' : ''), ' <a href="htmLawedTest.php" title="[toggle visibility] finalized internal settings as interpreted by htmLawed; for developers" onclick="javascript:toggle(\'settingF\'); return false;"><span class="notice">Finalized internal settings &raquo;&nbsp;</span></a> <div id="settingF" style="display: none;">', str_replace(array(' ', "\t", ' '), array(' ', '&nbsp; ', '&nbsp; '), nl2br(htmlspecialchars(print_r($GLOBALS['hlcfg']['config'], true)))), '</div><script type="text/javascript">hl(\'settingF\');</script>', '<br /><a href="htmLawedTest.php" title="[toggle visibility] suitable for copy-paste" onclick="javascript:toggle(\'outputF\'); return false;"><span class="notice">Output &raquo;</span></a> <span class="help" title="approx., server-specific value excluding the \'include()\' call"><small>htmLawed processing time <big>', number_format(((substr($et,0,9)) + (substr($et,-10)) - (substr($st,0,9)) - (substr($st,-10))),4), '</big> s</small></span>', (($mem = memory_get_peak_usage()) !== false ? '<span class="help"><small>, peak memory usage <big>'. round(($mem-$pre_mem)/1048576, 2). '</big> <small>MB</small>' : ''), '</small></span><div id="outputF" style="display: block;"><div><textarea id="text2" class="textarea" name="text2" rows="5" cols="100" style="width: 100%;">', htmlspecialchars($out), '</textarea></div><button type="button" onclick="javascript:document.getElementById(\'text2\').focus();document.getElementById(\'text2\').select()" title="select all to copy" style="float:right;">Select all</button>';
  510. if($_w3c_validate && $validation)
  511. {
  512. ?>
  513. <button type="button" title="HTML 4.01 W3C online validation" style="float: right;" onclick="javascript: sndValidn('text2', 'html401'); return false;" onkeypress="javascript: sndValidn('text2', 'html401'); return false;">Check HTML</button>
  514. <button type="button" title="XHTML 1.1 W3C online validation" style="float: right;" onclick="javascript: sndValidn('text2', 'xhtml110'); return false;" onkeypress="javascript: sndValidn('text2', 'xhtml110'); return false;">Check XHTML</button>
  515. <?php
  516. }
  517. echo '</div><br /><a href="htmLawedTest.php" title="[toggle visibility] syntax-highlighted" onclick="javascript:toggle(\'outputR\'); return false;"><span class="notice">Output code &raquo;</span></a><div id="outputR" style="display: block;">', format($out), '</div><script type="text/javascript">hl(\'outputR\');</script>', (!isset($_POST['text'][$_hlimit]) ? '<br /><a href="htmLawedTest.php" title="[toggle visibility] hexdump; non-viewable characters like line-returns are shown as dots" onclick="javascript:toggle(\'outputD\'); return false;"><span class="notice">Output binary &raquo;</span></a><div id="outputD" style="display: none;">'. hexdump($out). '</div>' : ''), '<br /><a href="htmLawedTest.php" title="[toggle visibility] XHTML 1 Transitional doctype" onclick="javascript:toggle(\'outputH\'); return false;"><span class="notice">Output rendered &raquo;</span></a><div id="outputH" style="display: block;">', $out, '</div>';
  518. }
  519. else{
  520. ?>
  521. <br />
  522. <div class="help">Use with a Javascript- and cookie-enabled, relatively new version of a common browser. <em>Submitted input will also be HTML-rendered (XHTML 1) after htmLawed-filtering.</em>
  523. <?php echo (file_exists('./htmLawed_TESTCASE.txt') ? '<br /><br />You can use text from <a href="htmLawed_TESTCASE.txt"><span class="notice">this collection of test-cases</span></a> in the input. Set the character encoding of the browser to Unicode/utf-8 before copying.' : ''); ?>
  524. <br /><br />For anti-XSS tests, try the <a href="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/htmLawedSafeModeTest.php"><span class="notice">special test-page</span></a> or see <a href="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/rsnake/RSnakeXSSTest.htm"><span class="notice">these results</span></a>.
  525. <br /><br /><small>Change <em>Encoding</em> to reflect the character encoding of the input text. Even then, it may not work or some characters may not display properly because of variable browser support and because of the form interface. Developers can write some PHP code to capture the filtered input to a file if this is important.
  526. <br /><br />Refer to the htmLawed documentation (<a href="htmLawed_README.htm"><span class="notice">htm</span></a>/<a href="htmLawed_README.txt"><span class="notice">txt</span></a>) for details about <em>Settings</em>, and htmLawed's behavior and limitations. For <em>Settings</em>, incorrectly-specified values like regular expressions are silently ignored. One or more settings form-fields may have been disabled. Some characters are not allowed in the <em>Spec</em> field.
  527. <br /><br />Hovering the mouse over some of the text can provide additional information in some browsers.</small>
  528. <?php
  529. if($_w3c_validate){
  530. ?>
  531. <small><br /><br />Because of character-encoding issues, the W3C validator (anyway not perfect) may reject validation requests or invalidate otherwise-valid code, esp. if text was copy-pasted in the input box. Local applications like the <em>HTML Validator</em> Firefox browser add-on may be useful in such cases.</small>
  532. <?php
  533. }
  534. ?>
  535. </div>
  536. <?php
  537. }
  538. ?>
  539. </div>
  540. </body>
  541. </html>