Skip to content

Commit 9e04b04

Browse files
committed
Upgrade to 1.2.4.1
Fixes PHP 7.2 compatibility
1 parent 62a8d13 commit 9e04b04

4 files changed

Lines changed: 132 additions & 56 deletions

File tree

htmLawed.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
htmLawed 1.2, 11 February 2017
4+
htmLawed 1.2.4.1, 12 September 2017
55
Copyright Santosh Patnaik
66
Dual licensed with LGPL 3 and GPL 2+
77
A PHP Labware internal utility - www.bioinformatics.org/phplabware/internal_utilities/htmLawed
@@ -308,7 +308,7 @@ function hl_cmtcd($t){
308308
global $C;
309309
if(!($v = $C[$n = $t[3] == '-' ? 'comment' : 'cdata'])){return $t;}
310310
if($v == 1){return '';}
311-
if($n == 'comment'){
311+
if($n == 'comment' && $v < 4){
312312
if(substr(($t = preg_replace('`--+`', '-', substr($t, 4, -3))), -1) != ' '){$t .= ' ';}
313313
}
314314
else{$t = substr($t, 1, -1);}
@@ -366,28 +366,37 @@ function hl_prot($p, $c=null){
366366
function hl_regex($p){
367367
// check regex
368368
if(empty($p)){return 0;}
369-
if($t = ini_get('track_errors')){$o = isset($php_errormsg) ? $php_errormsg : null;}
370-
else{ini_set('track_errors', 1);}
371-
unset($php_errormsg);
369+
if($v = function_exists('error_clear_last') && function_exists('error_get_last')){error_clear_last();}
370+
else{
371+
if($t = ini_get('track_errors')){$o = isset($php_errormsg) ? $php_errormsg : null;}
372+
else{ini_set('track_errors', 1);}
373+
unset($php_errormsg);
374+
}
372375
if(($d = ini_get('display_errors'))){ini_set('display_errors', 0);}
373376
preg_match($p, '');
377+
if($v){$r = error_get_last() == null ? 1 : 0; }
378+
else{
379+
$r = isset($php_errormsg) ? 0 : 1;
380+
if($t){$php_errormsg = isset($o) ? $o : null;}
381+
else{ini_set('track_errors', 0);}
382+
}
374383
if($d){ini_set('display_errors', 1);}
375-
$r = isset($php_errormsg) ? 0 : 1;
376-
if($t){$php_errormsg = isset($o) ? $o : null;}
377-
else{ini_set('track_errors', 0);}
378384
return $r;
379385
}
380386

381387
function hl_spec($t){
382388
// final $spec
383389
$s = array();
384-
$t = str_replace(array("\t", "\r", "\n", ' '), '', preg_replace_callback('/"(?>(`.|[^"])*)"/sm', create_function('$m', 'return substr(str_replace(array(";", "|", "~", " ", ",", "/", "(", ")", \'`"\'), array("\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", "\x08", "\""), $m[0]), 1, -1);'), trim($t)));
390+
if(!function_exists('hl_aux1')){function hl_aux1($m){
391+
return substr(str_replace(array(";", "|", "~", " ", ",", "/", "(", ")", '`"'), array("\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", "\x08", '"'), $m[0]), 1, -1);
392+
}}
393+
$t = str_replace(array("\t", "\r", "\n", ' '), '', preg_replace_callback('/"(?>(`.|[^"])*)"/sm', 'hl_aux1', trim($t)));
385394
for($i = count(($t = explode(';', $t))); --$i>=0;){
386395
$w = $t[$i];
387396
if(empty($w) or ($e = strpos($w, '=')) === false or !strlen(($a = substr($w, $e+1)))){continue;}
388397
$y = $n = array();
389398
foreach(explode(',', $a) as $v){
390-
if(!preg_match('`^([a-z][^=/()]+)(?:\((.*?)\))?`i', $v, $m)){continue;}
399+
if(!preg_match('`^([a-z:\-\*]+)(?:\((.*?)\))?`i', $v, $m)){continue;}
391400
if(($x = strtolower($m[1])) == '-*'){$n['*'] = 1; continue;}
392401
if($x[0] == '-'){$n[substr($x, 1)] = 1; continue;}
393402
if(!isset($m[2])){$y[$x] = 1; continue;}
@@ -401,8 +410,8 @@ function hl_spec($t){
401410
if(!count($y) && !count($n)){continue;}
402411
foreach(explode(',', substr($w, 0, $e)) as $v){
403412
if(!strlen(($v = strtolower($v)))){continue;}
404-
if(count($y)){$s[$v] = $y;}
405-
if(count($n)){$s[$v]['n'] = $n;}
413+
if(count($y)){if(!isset($s[$v])){$s[$v] = $y;} else{$s[$v] = array_merge($s[$v], $y);}}
414+
if(count($n)){if(!isset($s[$v]['n'])){$s[$v]['n'] = $n;} else{$s[$v]['n'] = array_merge($s[$v]['n'], $n);}}
406415
}
407416
}
408417
return $s;
@@ -562,6 +571,7 @@ function hl_tag($t){
562571
$c = array();
563572
foreach($a as $k=>$v){
564573
if($k == 'style' or !isset($aND[$k][$e])){continue;}
574+
$v = str_replace(array('\\', ':', ';', '&#'), '', $v);
565575
if($k == 'align'){
566576
unset($a['align']);
567577
if($e == 'img' && ($v == 'left' or $v == 'right')){$c[] = 'float: '. $v;}
@@ -659,7 +669,10 @@ function hl_tag2(&$e, &$a, $t=1){
659669
function hl_tidy($t, $w, $p){
660670
// tidy/compact HTM
661671
if(strpos(' pre,script,textarea', "$p,")){return $t;}
662-
$t = 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));
672+
if(!function_exists('hl_aux2')){function hl_aux2($m){
673+
return $m[1]. str_replace(array("<", ">", "\n", "\r", "\t", ' '), array("\x01", "\x02", "\x03", "\x04", "\x05", "\x07"), $m[3]). $m[4];
674+
}}
675+
$t = preg_replace(array('`(<\w[^>]*(?<!/)>)\s+`', '`\s+`', '`(<\w[^>]*(?<!/)>) `'), array(' $1', ' ', '$1'), preg_replace_callback(array('`(<(!\[CDATA\[))(.+?)(\]\]>)`sm', '`(<(!--))(.+?)(-->)`sm', '`(<(pre|script|textarea)[^>]*?>)(.+?)(</\2>)`sm'), 'hl_aux2', $t));
663676
if(($w = strtolower($w)) == -1){
664677
return str_replace(array("\x01", "\x02", "\x03", "\x04", "\x05", "\x07"), array('<', '>', "\n", "\r", "\t", ' '), $t);
665678
}
@@ -712,5 +725,5 @@ function hl_tidy($t, $w, $p){
712725

713726
function hl_version(){
714727
// version
715-
return '1.2';
728+
return '1.2.4.1';
716729
}

htmLawedTest.php

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
htmLawedTest.php, 11 February 2017
4+
htmLawedTest.php, 17 May 2017
55
To test htmLawed
66
Copyright Santosh Patnaik
77
Dual licensed with LGPL 3 and GPL 2+
@@ -67,7 +67,13 @@
6767

6868
// HTM for unprocessed
6969
if(isset($_POST['inputH'])){
70-
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>';
70+
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 raw/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>';
71+
exit;
72+
}
73+
74+
// HTM for processed
75+
if(isset($_POST['outputH'])){
76+
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 filtered/processed 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['outputH'], '</div></body></html>';
7177
exit;
7278
}
7379

@@ -142,7 +148,7 @@ function hexdump($d){
142148
body, button, div, html, input, p{font-size:13px; font-family:'Lucida grande', Verdana, Arial, Helvetica, sans-serif;}
143149
button, input{font-size: 85%;}
144150
div.help{border-top: 1px dotted gray; margin-top: 15px; padding-top: 15px; color:#999999;}
145-
#inputC, #inputD, #inputF, #inputR, #outputD, #outputF, #outputH, #outputR, #settingF, #diff{display:block;}
151+
#inputC, #inputD, #inputF, #inputR, #outputD, #outputF, #outputR, #settingF, #diff{display:block;}
146152
#inputC, #settingF{background-color:white; border:1px gray solid; padding:3px;}
147153
#inputC li{margin: 0; padding: 0;}
148154
#inputC ul{margin: 0; padding: 0; margin-left: 14px;}
@@ -152,7 +158,6 @@ function hexdump($d){
152158
#inputC, #settingF, #inputD, #inputR, #outputD, #outputR, #diff, textarea{font-size:100%; font-family:'Bitstream vera sans mono', 'courier new', 'courier', monospace;}
153159
#outputD{overflow:auto; background-color: #99ffcc; border:1px #66cc99 solid; padding:3px;}
154160
#diff{overflow:auto; background-color: white; border:1px #dcdcdc solid; padding:3px;}
155-
#outputH{overflow:auto; background-color:white; padding:3px; border:1px #dcdcdc solid;}
156161
#outputR{overflow:auto; background-color: #ccffcc; border:1px #99cc99 solid; padding:3px;}
157162
span.cmtcdata{color: orange;}
158163
span.ctag{color:red;}
@@ -256,6 +261,30 @@ function toggle(i){
256261
if(a == 'hidden'){e.visibility = 'show'; return;}
257262
if(a == 'show'){e.visibility = 'hidden';}
258263
}
264+
function sndProc2(){
265+
var i = document.getElementById('text2');
266+
if(!i){return;}
267+
i = i.value;
268+
var w = window.open('htmLawedTest.php?pre=1', 'hlposthtm');
269+
var f = document.createElement('form');
270+
f.enctype = 'application/x-www-form-urlencoded';
271+
f.method = 'post';
272+
f.acceptCharset = '<?php echo htmlspecialchars($_POST['enc']); ?>';
273+
if(f.style){f.style.display = 'none';}
274+
else{f.visibility = 'hidden';}
275+
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); ?>') + '" /></p>';
276+
f.action = 'htmLawedTest.php?pre=1';
277+
f.target = 'hlposthtm';
278+
f.method = 'post';
279+
var t = document.createElement('textarea');
280+
t.name = 'outputH';
281+
t.value = i;
282+
f.appendChild(t);
283+
var b = document.getElementsByTagName('body')[0];
284+
b.appendChild(f);
285+
f.submit();
286+
w.focus;
287+
}
259288
function sndUnproc(){
260289
var i = document.getElementById('text');
261290
if(!i){return;}
@@ -455,7 +484,7 @@ function sndValidn(id, type){
455484
}
456485
?>
457486

458-
<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>
487+
<button type="button" title="Raw input rendered as web-page without a doctype or charset declaration" style="float: right;" onclick="javascript: sndUnproc(); return false;" onkeypress="javascript: sndUnproc(); return false;">Render in webpage</button>
459488
<button type="button" onclick="javascript:document.getElementById('text').focus();document.getElementById('text').select()" title="select all to copy" style="float:right;">Select all</button>
460489

461490
<?php
@@ -598,7 +627,7 @@ function sndValidn(id, type){
598627
$st = microtime();
599628
$out = htmLawed($_POST['text'], $cfg, $_POST['spec']);
600629
$et = microtime();
601-
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>', ($tag = round((substr_count($_POST['text'], '>') + substr_count($_POST['text'], '<'))/2)), '</big> tag', ($tag > 1 ? 's' : ''), '</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;">$config: ', str_replace(array(' ', "\t", ' '), array(' ', '&nbsp; ', '&nbsp; '), nl2br(htmlspecialchars(print_r($GLOBALS['hlcfg']['config'], true)))), '<br />$spec: ', str_replace(array(' ', "\t", ' '), array(' ', '&nbsp; ', '&nbsp; '), nl2br(htmlspecialchars(print_r($GLOBALS['hlcfg']['spec'], 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>';
630+
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>', ($tag = round((substr_count($_POST['text'], '>') + substr_count($_POST['text'], '<'))/2)), '</big> tag', ($tag > 1 ? 's' : ''), '</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;">$config: ', str_replace(array(' ', "\t", ' '), array(' ', '&nbsp; ', '&nbsp; '), nl2br(htmlspecialchars(print_r($GLOBALS['hlcfg']['config'], true)))), '<br />$spec: ', str_replace(array(' ', "\t", ' '), array(' ', '&nbsp; ', '&nbsp; '), nl2br(htmlspecialchars(print_r($GLOBALS['hlcfg']['spec'], 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" title="Filtered input rendered as web-page without a doctype or charset declaration" style="float: right;" onclick="javascript: sndProc2(); return false;" onkeypress="javascript: sndProc2(); return false;">Render in webpage</button><button type="button" onclick="javascript:document.getElementById(\'text2\').focus();document.getElementById(\'text2\').select()" title="select all to copy" style="float:right;">Select all</button>';
602631
if($_w3c_validate && $validation)
603632
{
604633
?>
@@ -608,14 +637,14 @@ function sndValidn(id, type){
608637

609638
<?php
610639
}
611-
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]) ? ' <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>' : ''), ' <a href="htmLawedTest.php" title="[toggle visibility] inline output-input diff; might not be perfectly accurate, semantically or otherwise " onclick="javascript:toggle(\'diff\'); diffLaunch(); return false;"><span class="notice">Diff &raquo;</span></a> <div id="diff" style="display: none;"></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>';
640+
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]) ? ' <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>' : ''), ' <a href="htmLawedTest.php" title="[toggle visibility] inline output-input diff; might not be perfectly accurate, semantically or otherwise " onclick="javascript:toggle(\'diff\'); diffLaunch(); return false;"><span class="notice">Diff &raquo;</span></a> <div id="diff" style="display: none;"></div><br /><a href="htmLawedTest.php" title="[toggle visibility] XHTML 1 Transitional doctype" onclick="javascript:toggle(\'outputH\'); return false;">';
612641
}
613642
else{
614643
?>
615644

616645
<br />
617646

618-
<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>
647+
<div class="help">Use with a Javascript- and cookie-enabled, relatively new version of a common browser.
619648

620649
<?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.' : ''); ?>
621650

0 commit comments

Comments
 (0)