3eb07a57323f949f86a84add06b10331ea1ec060
[yaffs-website] / web / modules / contrib / htmlawed / htmLawed / htmLawed_README.htm
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <meta http-equiv="Content-Language" content="en" />
6 <meta name="description" content="htmLawed PHP software is a free, open-source, customizable HTML input purifier and filter - htmLawed_README.txt - presented with rTxt2htm, a PHP Labware utility" />
7 <meta name="keywords" content="htmLawed, HTM, HTML, HTML5, HTML 5, XHTML, XHTML5, HTML Tidy, converter, filter, formatter, purifier, sanitizer, XSS, input, PHP, software, code, script, security, cross-site scripting, hack, sanitize, remove, standards, tags, attributes, elements, Aria, Ruby, data attributes, tidy, indent, auto-indent, prettify, pretty print, htmLawed_README.txt, rTxt2htm, PHP Labware" />
8 <style type="text/css" media="all">
9 <!--/*--><![CDATA[/*><!--*/
10 a {text-decoration:none; color: blue;}\r
11 a:hover {color: red;}\r
12 a:visited {color: blue;}\r
13 body {margin: 0; padding: 0;}\r
14 body, div, html, p {font-family: Georgia, 'Times new roman', Times;}\r
15 code.code {font-family: 'Bitstream vera sans mono', 'Courier New', 'Courier', monospace;}\r
16 div.comment {padding: 5px; color: #999999; font-size: 80%;}\r
17 div.comment a {color: #6699cc;}\r
18 div#body {width: 70%; margin: 5px; padding: 5px;} /* holds non-toc content */\r
19 div#toc {position: fixed; top: 5px; left: 73%; z-index: 2; margin-top: 5px; margin-left: 5px; border: 1px solid gray; padding: 5px; background-color: #ededed; width: 23%; overflow: auto; max-height:94%; font-size: 90%;} /* holds content table (toc) */\r
20 div#top {font-size: 14px; margin: 5px; padding: 5px;} /* holds all content */\r
21 div.monospace {overflow: auto; font-family: 'Bitstream vera sans mono', 'Courier New', 'Courier', monospace;}\r
22 div.sub-section {padding-left: 15px;}\r
23 div.sub-sub-section {padding-left: 30px;}\r
24 h1 {font-size: 22px; margin-top: 5px; margin-bottom: 5px;}\r
25 h2 {font-size: 20px; float: left; margin-top: 15px; margin-bottom: 5px;}\r
26 h3 {font-size: 18px; float: left; margin-top: 15px; margin-bottom: 5px;}\r
27 h4 {font-size: 16px; float: left; margin-top: 15px; margin-bottom: 5px;}\r
28 hr {margin-top: 15px; margin-bottom: 5px;}\r
29 input, textarea {font-family: 'Bitstream vera sans mono', 'Courier New', 'Courier', monospace;}\r
30 p.subtle {color: gray; padding: 0; padding-top: 10px; margin: 0;}\r
31 p.subtle a, p.subtle a:visited {color: #6699cc;}\r
32 span.item-no {color: black;}\r
33 span.subtle {color: gray; margin: 0; padding:0;}\r
34 span.subtle a, span.subtle a:visited {color: #6699cc;}\r
35 span.term {font-family: 'Bitstream vera sans mono', 'Courier New', 'Courier', monospace;}\r
36 span.toc-item {color: black;}\r
37 span.totop {float: right; margin-top: 15px; margin-bottom: 5px;}\r
38 span.totop a, span.totop a:visited {color: #6699cc;}\r
39 @media screen { /* fixes for old IE */\r
40  * html, * html body {overflow-y: auto!important; height: 100%; margin: 0; padding: 0;}\r
41  * html div#body {height: 100%; overflow-y: auto; position: relative;}\r
42  * html div#toc {position: absolute;}\r
43 }\r
44 /*]]>*/-->
45 </style>
46 <title>htmLawed documentation | htmLawed PHP software is a free, open-source, customizable HTML input purifier and filter</title>
47 </head>
48 <body>
49 <div id="top">
50 <h1><a id="peak" name="peak"></a>htmLawed documentation</h1>
51
52 <div id="toc"><span class="toc-item"><a href="#s1"><span class="item-no">1</span>&#160; About htmLawed</a></span><br />
53 &#160; <span class="toc-item"><a href="#s1.1"><span class="item-no">1.1</span>&#160; Example uses</a></span><br />
54 &#160; <span class="toc-item"><a href="#s1.2"><span class="item-no">1.2</span>&#160; Features</a></span><br />
55 &#160; <span class="toc-item"><a href="#s1.3"><span class="item-no">1.3</span>&#160; History</a></span><br />
56 &#160; <span class="toc-item"><a href="#s1.4"><span class="item-no">1.4</span>&#160; License &amp; copyright</a></span><br />
57 &#160; <span class="toc-item"><a href="#s1.5"><span class="item-no">1.5</span>&#160; Terms used here</a></span><br />
58 &#160; <span class="toc-item"><a href="#s1.6"><span class="item-no">1.6</span>&#160; Availability</a></span><br />
59 <span class="toc-item"><a href="#s2"><span class="item-no">2</span>&#160; Usage</a></span><br />
60 &#160; <span class="toc-item"><a href="#s2.1"><span class="item-no">2.1</span>&#160; Simple</a></span><br />
61 &#160; <span class="toc-item"><a href="#s2.2"><span class="item-no">2.2</span>&#160; Configuring htmLawed using the <span class="term">$config</span>&#160;argument</a></span><br />
62 &#160; <span class="toc-item"><a href="#s2.3"><span class="item-no">2.3</span>&#160; Extra HTML specifications using the <span class="term">$spec</span>&#160;argument</a></span><br />
63 &#160; <span class="toc-item"><a href="#s2.4"><span class="item-no">2.4</span>&#160; Performance time &amp; memory usage</a></span><br />
64 &#160; <span class="toc-item"><a href="#s2.5"><span class="item-no">2.5</span>&#160; Some security risks to keep in mind</a></span><br />
65 &#160; <span class="toc-item"><a href="#s2.6"><span class="item-no">2.6</span>&#160; Use with <span class="term">kses()</span>&#160;code</a></span><br />
66 &#160; <span class="toc-item"><a href="#s2.7"><span class="item-no">2.7</span>&#160; Tolerance for ill-written HTML</a></span><br />
67 &#160; <span class="toc-item"><a href="#s2.8"><span class="item-no">2.8</span>&#160; Limitations &amp; work-arounds</a></span><br />
68 &#160; <span class="toc-item"><a href="#s2.9"><span class="item-no">2.9</span>&#160; Examples of usage</a></span><br />
69 <span class="toc-item"><a href="#s3"><span class="item-no">3</span>&#160; Details</a></span><br />
70 &#160; <span class="toc-item"><a href="#s3.1"><span class="item-no">3.1</span>&#160; Invalid/dangerous characters</a></span><br />
71 &#160; <span class="toc-item"><a href="#s3.2"><span class="item-no">3.2</span>&#160; Character references/entities</a></span><br />
72 &#160; <span class="toc-item"><a href="#s3.3"><span class="item-no">3.3</span>&#160; HTML elements</a></span><br />
73 &#160; &#160; <span class="toc-item"><a href="#s3.3.1"><span class="item-no">3.3.1</span>&#160; HTML comments &amp; <span class="term">CDATA</span>&#160;sections</a></span><br />
74 &#160; &#160; <span class="toc-item"><a href="#s3.3.2"><span class="item-no">3.3.2</span>&#160; Tag-transformation for better compliance with standards</a></span><br />
75 &#160; &#160; <span class="toc-item"><a href="#s3.3.3"><span class="item-no">3.3.3</span>&#160; Tag balancing &amp; proper nesting</a></span><br />
76 &#160; &#160; <span class="toc-item"><a href="#s3.3.4"><span class="item-no">3.3.4</span>&#160; Elements requiring child elements</a></span><br />
77 &#160; &#160; <span class="toc-item"><a href="#s3.3.5"><span class="item-no">3.3.5</span>&#160; Beautify or compact HTML</a></span><br />
78 &#160; <span class="toc-item"><a href="#s3.4"><span class="item-no">3.4</span>&#160; Attributes</a></span><br />
79 &#160; &#160; <span class="toc-item"><a href="#s3.4.1"><span class="item-no">3.4.1</span>&#160; Auto-addition of XHTML-required attributes</a></span><br />
80 &#160; &#160; <span class="toc-item"><a href="#s3.4.2"><span class="item-no">3.4.2</span>&#160; Duplicate/invalid <span class="term">id</span>&#160;values</a></span><br />
81 &#160; &#160; <span class="toc-item"><a href="#s3.4.3"><span class="item-no">3.4.3</span>&#160; URL schemes &amp; scripts in attribute values</a></span><br />
82 &#160; &#160; <span class="toc-item"><a href="#s3.4.4"><span class="item-no">3.4.4</span>&#160; Absolute &amp; relative URLs</a></span><br />
83 &#160; &#160; <span class="toc-item"><a href="#s3.4.5"><span class="item-no">3.4.5</span>&#160; Lower-cased, standard attribute values</a></span><br />
84 &#160; &#160; <span class="toc-item"><a href="#s3.4.6"><span class="item-no">3.4.6</span>&#160; Transformation of deprecated attributes</a></span><br />
85 &#160; &#160; <span class="toc-item"><a href="#s3.4.7"><span class="item-no">3.4.7</span>&#160; Anti-spam &amp; <span class="term">href</span></a></span><br />
86 &#160; &#160; <span class="toc-item"><a href="#s3.4.8"><span class="item-no">3.4.8</span>&#160; Inline style properties</a></span><br />
87 &#160; &#160; <span class="toc-item"><a href="#s3.4.9"><span class="item-no">3.4.9</span>&#160; Hook function for tag content</a></span><br />
88 &#160; <span class="toc-item"><a href="#s3.5"><span class="item-no">3.5</span>&#160; Simple configuration directive for most valid XHTML</a></span><br />
89 &#160; <span class="toc-item"><a href="#s3.6"><span class="item-no">3.6</span>&#160; Simple configuration directive for most <em>safe</em>&#160;HTML</a></span><br />
90 &#160; <span class="toc-item"><a href="#s3.7"><span class="item-no">3.7</span>&#160; Using a hook function</a></span><br />
91 &#160; <span class="toc-item"><a href="#s3.8"><span class="item-no">3.8</span>&#160; Obtaining <em>finalized</em>&#160;parameter values</a></span><br />
92 &#160; <span class="toc-item"><a href="#s3.9"><span class="item-no">3.9</span>&#160; Retaining non-HTML tags in input with mixed markup</a></span><br />
93 <span class="toc-item"><a href="#s4"><span class="item-no">4</span>&#160; Other</a></span><br />
94 &#160; <span class="toc-item"><a href="#s4.1"><span class="item-no">4.1</span>&#160; Support</a></span><br />
95 &#160; <span class="toc-item"><a href="#s4.2"><span class="item-no">4.2</span>&#160; Known issues</a></span><br />
96 &#160; <span class="toc-item"><a href="#s4.3"><span class="item-no">4.3</span>&#160; Change-log</a></span><br />
97 &#160; <span class="toc-item"><a href="#s4.4"><span class="item-no">4.4</span>&#160; Testing</a></span><br />
98 &#160; <span class="toc-item"><a href="#s4.5"><span class="item-no">4.5</span>&#160; Upgrade, &amp; old versions</a></span><br />
99 &#160; <span class="toc-item"><a href="#s4.6"><span class="item-no">4.6</span>&#160; Comparison with <span class="term">HTMLPurifier</span></a></span><br />
100 &#160; <span class="toc-item"><a href="#s4.7"><span class="item-no">4.7</span>&#160; Use through application plug-ins/modules</a></span><br />
101 &#160; <span class="toc-item"><a href="#s4.8"><span class="item-no">4.8</span>&#160; Use in non-PHP applications</a></span><br />
102 &#160; <span class="toc-item"><a href="#s4.9"><span class="item-no">4.9</span>&#160; Donate</a></span><br />
103 &#160; <span class="toc-item"><a href="#s4.10"><span class="item-no">4.10</span>&#160; Acknowledgements</a></span><br />
104 <span class="toc-item"><a href="#s5"><span class="item-no">5</span>&#160; Appendices</a></span><br />
105 &#160; <span class="toc-item"><a href="#s5.1"><span class="item-no">5.1</span>&#160; Characters discouraged in HTML</a></span><br />
106 &#160; <span class="toc-item"><a href="#s5.2"><span class="item-no">5.2</span>&#160; Valid attribute-element combinations</a></span><br />
107 &#160; <span class="toc-item"><a href="#s5.3"><span class="item-no">5.3</span>&#160; CSS 2.1 properties accepting URLs</a></span><br />
108 &#160; <span class="toc-item"><a href="#s5.4"><span class="item-no">5.4</span>&#160; Microsoft Windows 1252 character replacements</a></span><br />
109 &#160; <span class="toc-item"><a href="#s5.5"><span class="item-no">5.5</span>&#160; URL format</a></span><br />
110 &#160; <span class="toc-item"><a href="#s5.6"><span class="item-no">5.6</span>&#160; Brief on htmLawed code</a></span></div><!-- ended div toc -->
111
112 <div id="body">
113 <br />
114 <div class="comment">htmLawed_README.txt, 12 September 2017<br />
115 htmLawed 1.2.4.1, 12 September 2017<br />
116 Copyright Santosh Patnaik<br />
117 Dual licensed with LGPL 3 and GPL 2+<br />
118 A PHP Labware internal utility &#45; <a href="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed">http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed</a>&#160;</div>
119 <br />
120
121 <div class="section"><h2>
122 <a name="s1" id="s1"></a><span class="item-no">1</span>&#160; About htmLawed
123 </h2><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
124 <br />
125 &#160; htmLawed is a PHP script to process text with HTML markup to make it more compliant with HTML standards and with administrative policies. It works by making HTML well-formed with balanced and properly nested tags, neutralizing code that introduces a security vulnerability or is used for cross-site scripting (XSS) attacks, allowing only specified HTML tags and attributes, and so on. Such <em>lawing in</em>&#160;of HTML code ensures that it is in accordance with the aesthetics, safety and usability requirements set by administrators.<br />
126 <br />
127 &#160; htmLawed is highly customizable, and fast with low memory usage. Its free and open-source code is in one small file. It does not require extensions or libraries, and works in older versions of PHP as well. It is a good alternative to the HTML <a href="http://tidy.sourceforge.net">Tidy</a>&#160;application.<br />
128
129 <div class="sub-section"><h3>
130 <a name="s1.1" id="s1.1"></a><span class="item-no">1.1</span>&#160; Example uses
131 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
132 <br />
133 &#160; * &#160;Filtering of text submitted as comments on blogs to allow only certain HTML elements<br />
134 <br />
135 &#160; * &#160;Making RSS newsfeed items standard-compliant: often one uses an excerpt from an HTML document for the content, and with unbalanced tags, non-numerical entities, etc., such excerpts may not be XML-compliant<br />
136 <br />
137 &#160; * &#160;Beautifying or pretty-printing HTML code<br />
138 <br />
139 &#160; * &#160;Text processing for stricter XML standard-compliance: e.g., to have lowercased <span class="term">x</span>&#160;in hexadecimal numeric entities becomes necessary if an HTML document with MathML content needs to be served as <span class="term">application/xml</span><br />
140 <br />
141 &#160; * &#160;Scraping text from web-pages<br />
142 <br />
143 &#160; * &#160;Transforming an HTML element to another<br />
144
145 </div>
146 <div class="sub-section"><h3>
147 <a name="s1.2" id="s1.2"></a><span class="item-no">1.2</span>&#160; Features
148 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
149 <br />
150 &#160; Key: <span class="term">&#42;</span>&#160;security feature, <span class="term">^</span>&#160;standard compliance, <span class="term">~</span>&#160;requires setting right options<br />
151 <br />
152 &#160; htmLawed:<br />
153 <br />
154 &#160; * &#160;makes input more <strong>secure</strong>&#160;and <strong>standard-compliant</strong>&#160;for HTML as well as generic <strong>XML</strong>&#160;documents &#160;^<br />
155 &#160; * &#160;supports markup for <strong>HTML 5</strong>&#160;and <strong>microdata, ARIA, Ruby, custom attributes</strong>, etc. &#160;^<br />
156 &#160; * &#160;can <strong>beautify</strong>&#160;or <strong>compact</strong>&#160;HTML &#160;~<br />
157 &#160; * &#160;works with input of almost any <strong>character encoding</strong>&#160;and does not affect it<br />
158 &#160; * &#160;has good <strong>tolerance for ill-written HTML</strong><br />
159 <br />
160 &#160; * &#160;can enforce <strong>restricted use of elements</strong>&#160; *~<br />
161 &#160; * &#160;ensures proper closure of empty elements like <span class="term">img</span>&#160; ^<br />
162 &#160; * &#160;<strong>transforms deprecated elements</strong>&#160;like <span class="term">font</span>&#160; ^~<br />
163 &#160; * &#160;can permit HTML <strong>comments</strong>&#160;and <strong>CDATA</strong>&#160;sections &#160;^~<br />
164 &#160; * &#160;can permit all elements, including <span class="term">script</span>, <span class="term">object</span>&#160;and <span class="term">form</span>&#160; ~<br />
165 <br />
166 &#160; * &#160;can <strong>restrict attributes by element</strong>&#160; ^~<br />
167 &#160; * &#160;removes <strong>invalid attributes</strong>&#160; ^<br />
168 &#160; * &#160;lower-cases element and attribute names &#160;^<br />
169 &#160; * &#160;provides <strong>required attributes</strong>, like <span class="term">alt</span>&#160;for <span class="term">image</span>&#160; ^<br />
170 &#160; * &#160;<strong>transforms deprecated attributes</strong>&#160; ^~<br />
171 &#160; * &#160;ensures attributes are <strong>declared only once</strong>&#160; ^<br />
172 &#160; * &#160;permits <strong>custom</strong>, non-standard attributes as well as custom rules for standard attributes &#160;~<br />
173 <br />
174 &#160; * &#160;declares value for <em>empty</em>&#160;(<em>minimized</em>&#160;or <em>boolean</em>) attributes like <span class="term">checked</span>&#160; ^<br />
175 &#160; * &#160;checks for potentially dangerous attribute values &#160;*~<br />
176 &#160; * &#160;ensures <strong>unique</strong>&#160;<span class="term">id</span>&#160;attribute values &#160;^~<br />
177 &#160; * &#160;<strong>double-quotes</strong>&#160;attribute values &#160;^<br />
178 &#160; * &#160;lower-cases <strong>standard attribute values</strong>&#160;like <span class="term">password</span>&#160; ^<br />
179 <br />
180 &#160; * &#160;can restrict <strong>URL protocol/scheme by attribute</strong>&#160; *~<br />
181 &#160; * &#160;can disable <strong>dynamic expressions</strong>&#160;in <span class="term">style</span>&#160;values &#160;*~<br />
182 <br />
183 &#160; * &#160;neutralizes invalid named <strong>character entities</strong>&#160; ^<br />
184 &#160; * &#160;converts hexadecimal numeric entities to decimal ones, or vice versa &#160;^~<br />
185 &#160; * &#160;converts named entities to numeric ones for generic XML use &#160;^~<br />
186 <br />
187 &#160; * &#160;removes <strong>null</strong>&#160;characters &#160;*<br />
188 &#160; * &#160;neutralizes potentially dangerous proprietary Netscape <strong>Javascript entities</strong>&#160; *<br />
189 &#160; * &#160;replaces potentially dangerous <strong>soft-hyphen</strong>&#160;character in URL-accepting attribute values with spaces &#160;*<br />
190 <br />
191 &#160; * &#160;removes common <strong>invalid characters</strong>&#160;not allowed in HTML or XML &#160;^<br />
192 &#160; * &#160;replaces <strong>characters from Microsoft applications</strong>&#160;like <span class="term">Word</span>&#160;that are discouraged in HTML or XML &#160;^~<br />
193 &#160; * &#160;neutralize entities for characters invalid or discouraged in HTML or XML &#160;^<br />
194 &#160; * &#160;appropriately neutralize <span class="term">&lt;</span>, <span class="term">&amp;</span>, <span class="term">"</span>, and <span class="term">&gt;</span>&#160;characters &#160;^*<br />
195 <br />
196 &#160; * &#160;understands improperly spaced tag content (e.g., spread over more than a line) and properly spaces them<br />
197 &#160; * &#160;attempts to <strong>balance tags</strong>&#160;for well-formedness &#160;^~<br />
198 &#160; * &#160;understands when <strong>omitable closing tags</strong>&#160;like <span class="term">&lt;/p&gt;</span>&#160;are missing &#160;^~<br />
199 &#160; * &#160;attempts to permit only <strong>validly nested tags</strong>&#160; ^~<br />
200 &#160; * &#160;can <strong>either remove or neutralize bad content</strong>&#160;^~<br />
201 &#160; * &#160;attempts to <strong>rectify common errors of plain-text misplacement</strong>&#160;(e.g., directly inside <span class="term">blockquote</span>) ^~<br />
202 <br />
203 &#160; * &#160;has optional <strong>anti-spam</strong>&#160;measures such as addition of <span class="term">rel="nofollow"</span>&#160;and link-disabling &#160;~<br />
204 &#160; * &#160;optionally makes <strong>relative URLs absolute</strong>, and vice versa &#160;~<br />
205 <br />
206 &#160; * &#160;optionally marks <span class="term">&amp;</span>&#160;to identify the entities for <span class="term">&amp;</span>, <span class="term">&lt;</span>&#160;and <span class="term">&gt;</span>&#160;introduced by it &#160;~<br />
207 <br />
208 &#160; * &#160;allows deployment of powerful <strong>hook functions</strong>&#160;to <strong>inject</strong>&#160;HTML, <strong>consolidate</strong>&#160;<span class="term">style</span>&#160;attributes to <span class="term">class</span>, finely check attribute values, etc. &#160;~<br />
209
210 </div>
211 <div class="sub-section"><h3>
212 <a name="s1.3" id="s1.3"></a><span class="item-no">1.3</span>&#160; History
213 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
214 <br />
215 &#160; htmLawed was created in 2007 for use with <span class="term">LabWiki</span>, a wiki software developed at PHP Labware, as a suitable software could not be found. Existing PHP software like <span class="term">Kses</span>&#160;and <span class="term">HTMLPurifier</span>&#160;were deemed inadequate, slow, resource-intensive, or dependent on an extension or external application like <span class="term">HTML Tidy</span>. The core logic of htmLawed, that of identifying HTML elements and attributes, was based on the <span class="term">Kses</span>&#160;(version 0.2.2) HTML filter software of Ulf Harnhammar (it can still be used with code that uses <span class="term">Kses</span>; see <a href="#s2.6">section 2.6</a>.). Support for HTML version 5 was added in May 2013 in a beta and in February 2017 in a production release.<br />
216 <br />
217 &#160; See <a href="#s4.3">section 4.3</a>&#160;for a detailed log of changes in htmLawed over the years, and <a href="#s4.10">section 4.10</a>&#160;for acknowledgements.<br />
218
219 </div>
220 <div class="sub-section"><h3>
221 <a name="s1.4" id="s1.4"></a><span class="item-no">1.4</span>&#160; License &amp; copyright
222 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
223 <br />
224 &#160; htmLawed is free and open-source software, copyrighted by Santosh Patnaik, MD, PhD, and dual-licensed with LGPL version <a href="http://www.gnu.org/licenses/lgpl-3.0.txt">3</a>, and GPL version <a href="http://www.gnu.org/licenses/gpl-2.0.txt">2</a>&#160;(or later) licenses.<br />
225
226 </div>
227 <div class="sub-section"><h3>
228 <a name="s1.5" id="s1.5"></a><span class="item-no">1.5</span>&#160; Terms used here
229 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
230 <br />
231 &#160; In this document, only HTML body-level elements are considered. htmLawed does not have support for head-level elements, <span class="term">body</span>, and the frame-level elements, <span class="term">frameset</span>, <span class="term">frame</span>&#160;and <span class="term">noframes</span>, and these elements are ignored here.<br />
232 <br />
233 &#160; * &#160;<em>administrator</em>&#160;- or admin; person setting up the code that utilizes htmLawed; also, <em>user</em><br />
234 &#160; * &#160;<em>attributes</em>&#160;- name-value pairs like <span class="term">href="http&#58;//x.com"</span>&#160;in opening tags<br />
235 &#160; * &#160;<em>author</em>&#160;- see <em>writer</em><br />
236 &#160; * &#160;<em>character</em>&#160;- atomic unit of text; internally represented by a numeric <em>code-point</em>&#160;as specified by the <em>encoding</em>&#160;or <em>charset</em>&#160;in use<br />
237 &#160; * &#160;<em>entity</em>&#160;- markup like <span class="term">&amp;gt;</span>&#160;and <span class="term">&amp;#160;</span>&#160;used to refer to a character<br />
238 &#160; * &#160;<em>element</em>&#160;- HTML element like <span class="term">a</span>&#160;and <span class="term">img</span><br />
239 &#160; * &#160;<em>element content</em>&#160;- &#160;content between the opening and closing tags of an element, like <span class="term">click</span>&#160;of the <span class="term">&lt;a href="x"&gt;click&lt;/a&gt;</span>&#160;element<br />
240 &#160; * &#160;<em>HTML</em>&#160;- implies XHTML unless specified otherwise<br />
241 &#160; * &#160;<em>HTML body</em>&#160;- content in the <em>body</em>&#160;container of an HTML document<br />
242 &#160; * &#160;<em>input</em>&#160;- text given to htmLawed to process<br />
243 &#160; * &#160;<em>legal</em>&#160;– standard-compliant; also, <em>valid</em><br />
244 &#160; * &#160;<em>processing</em>&#160;- involves filtering, correction, etc., of input<br />
245 &#160; * &#160;<em>safe</em>&#160;- absence or reduction of certain characters and HTML elements and attributes in HTML of text that can otherwise potentially, and circumstantially, expose text readers to security vulnerabilities like cross-site scripting attacks (XSS)<br />
246 &#160; * &#160;<em>scheme</em>&#160;- a URL protocol like <span class="term">http</span>&#160;and <span class="term">ftp</span><br />
247 &#160; * &#160;<em>specification</em>&#160;- detailed description including rules that define HTML<br />
248 &#160; * &#160;<em>standard</em>&#160;– widely accepted specification<br />
249 &#160; * &#160;<em>style property</em>&#160;- terms like <span class="term">border</span>&#160;and <span class="term">height</span>&#160;for which declarations are made in values for the <span class="term">style</span>&#160;attribute of elements<br />
250 &#160; * &#160;<em>tag</em>&#160;- markers like <span class="term">&lt;a href="x"&gt;</span>&#160;and <span class="term">&lt;/a&gt;</span>&#160;delineating element content; the opening tag can contain attributes<br />
251 &#160; * &#160;<em>tag content</em>&#160;- consists of tag markers <span class="term">&lt;</span>&#160;and <span class="term">&gt;</span>, element names like <span class="term">div</span>, and possibly attributes<br />
252 &#160; * &#160;<em>user</em>&#160;- administrator<br />
253 &#160; * &#160;<em>valid</em>&#160;- see <em>legal</em><br />
254 &#160; * &#160;<em>writer</em>&#160;- end-user like a blog commenter providing the input that is to be processed; also, <em>author</em><br />
255 &#160; * &#160;<em>XHTML</em>&#160;- XML-compliant HTML; parsing rules for XHTML are more strict than for regular HTML<br />
256
257 </div>
258 <div class="sub-section"><h3>
259 <a name="s1.6" id="s1.6"></a><span class="item-no">1.6</span>&#160; Availability
260 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
261 <br />
262 &#160; htmLawed can be downloaded for free at its <a href="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed">website</a>. Besides the <span class="term">htmLawed.php</span>&#160;file, the download has the htmLawed documentation (this document) in plain <a href="htmLawed_README.txt">text</a>&#160;and <a href="htmLawed_README.htm">HTML</a>&#160;formats, a script for <a href="htmLawedTest.php">testing</a>, and a text file for <a href="htmLawed_TESTCASE.txt">test-cases</a>. htmLawed is also available as a PHP class (OOP code) at its website.<br />
263
264 </div>
265 </div>
266 <div class="section"><h2>
267 <a name="s2" id="s2"></a><span class="item-no">2</span>&#160; Usage
268 </h2><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
269 <br />
270 &#160; htmLawed works in PHP version 4.4 or higher. Either <span class="term">include()</span>&#160;the <span class="term">htmLawed.php</span>&#160;file, or copy-paste the entire code.<br />
271 <br />
272 &#160; To use with PHP 4.3, have the following code included:<br />
273 <br />
274
275 <code class="code">&#160; &#160; if(!function_exists(&#39;ctype_digit&#39;)){</code>
276 <br />
277
278 <code class="code">&#160; &#160; &#160;function ctype_digit($var){</code>
279 <br />
280
281 <code class="code">&#160; &#160; &#160; return ((int) $var == $var);</code>
282 <br />
283
284 <code class="code">&#160; &#160; &#160;}</code>
285 <br />
286
287 <code class="code">&#160; &#160; }</code>
288 <br />
289
290 <div class="sub-section"><h3>
291 <a name="s2.1" id="s2.1"></a><span class="item-no">2.1</span>&#160; Simple
292 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
293 <br />
294 &#160; The input text to be processed, <span class="term">$text</span>, is passed as an argument of type string; <span class="term">htmLawed()</span>&#160;returns the processed string:<br />
295 <br />
296
297 <code class="code">&#160; &#160; $processed = htmLawed($text);</code>
298 <br />
299 <br />
300 &#160; With the <span class="term">htmLawed class</span>&#160;(<a href="#s1.6">section 1.6</a>), usage is:<br />
301 <br />
302
303 <code class="code">&#160; &#160; $processed = htmLawed&#58;&#58;hl($text);</code>
304 <br />
305 <br />
306 &#160; <strong>Notes</strong>: (1) If input is from a <span class="term">$_GET</span>&#160;or <span class="term">$_POST</span>&#160;value, and <span class="term">magic quotes</span>&#160;are enabled on the PHP setup, run <span class="term">stripslashes()</span>&#160;on the input before passing to htmLawed. (2) htmLawed does not have support for head-level elements, <span class="term">body</span>, and the frame-level elements, <span class="term">frameset</span>, <span class="term">frame</span>&#160;and <span class="term">noframes</span>.<br />
307 <br />
308 &#160; By default, htmLawed will process the text allowing all valid HTML elements/tags and commonly used URL schemes and CSS style properties. It will allow Javascript code, <span class="term">CDATA</span>&#160;sections and HTML comments, balance tags, and ensure proper nesting of elements. Such actions can be configured using two other optional arguments -- <span class="term">$config</span>&#160;and <span class="term">$spec</span>:<br />
309 <br />
310
311 <code class="code">&#160; &#160; $processed = htmLawed($text, $config, $spec);</code>
312 <br />
313 <br />
314 &#160; The <span class="term">$config</span>&#160;and <span class="term">$spec</span>&#160;arguments are detailed below. Some examples are shown in <a href="#s2.9">section 2.9</a>. For maximum protection against <span class="term">XSS</span>&#160;and other security vulnerabilities, consider using the <span class="term">safe</span>&#160;parameter; see <a href="#s3.6">section 3.6</a>.<br />
315
316 </div>
317 <div class="sub-section"><h3>
318 <a name="s2.2" id="s2.2"></a><span class="item-no">2.2</span>&#160; Configuring htmLawed using the <span class="term">$config</span>&#160;argument
319 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
320 <br />
321 &#160; <span class="term">$config</span>&#160;instructs htmLawed on how to tackle certain tasks. When <span class="term">$config</span>&#160;is not specified, or not set as an array (e.g., <span class="term">$config = 1</span>), htmLawed will take default actions. One or many of the task-action or parameter-value pairs can be specified in <span class="term">$config</span>&#160;as array key-value pairs. If a parameter is not specified, htmLawed will use the default value for it, indicated further below. In PHP code, parameter values that are integers should not be quoted and should be used as numeric types (unless meant as string/text). Thus, for instance:<br />
322 <br />
323
324 <code class="code">&#160; &#160; $config = array(&#39;comment&#39;=&gt;0, &#39;cdata&#39;=&gt;1, &#39;elements&#39;=&gt;&#39;a, b, strong&#39;);</code>
325 <br />
326
327 <code class="code">&#160; &#160; $processed = htmLawed($text, $config);</code>
328 <br />
329 <br />
330 &#160; Below are the various parameters that can be specified in <span class="term">$config</span>.<br />
331 <br />
332 &#160; Key: <span class="term">&#42;</span>&#160;default, <span class="term">^</span>&#160;different from htmLawed versions below 1.2, <span class="term">~</span>&#160;different default when <span class="term">valid_xhtml</span>&#160;is set to <span class="term">1</span>&#160;(see <a href="#s3.5">section 3.5</a>), <span class="term">"</span>&#160;different default when <span class="term">safe</span>&#160;is set to <span class="term">1</span>&#160;(see <a href="#s3.6">section 3.6</a>)<br />
333 <br />
334 &#160; <strong>abs_url</strong><br />
335 &#160; Make URLs absolute or relative; <span class="term">$config["base_url"]</span>&#160;needs to be set; see <a href="#s3.4.4">section 3.4.4</a><br />
336 <br />
337 &#160; <span class="term">-1</span>&#160;- make relative<br />
338 &#160; <span class="term">0</span>&#160;- no action &#160;*<br />
339 &#160; <span class="term">1</span>&#160;- make absolute<br />
340 <br />
341 &#160; <strong>and_mark</strong><br />
342 &#160; Mark <span class="term">&amp;</span>&#160;characters in the original input; see <a href="#s3.2">section 3.2</a><br />
343 <br />
344 &#160; <strong>anti_link_spam</strong><br />
345 &#160; Anti-link-spam measure; see <a href="#s3.4.7">section 3.4.7</a><br />
346 <br />
347 &#160; <span class="term">0</span>&#160;- no measure taken &#160;*<br />
348 &#160; <em>array("regex1", "regex2")</em>&#160;- will ensure a <span class="term">rel</span>&#160;attribute with <span class="term">nofollow</span>&#160;in its value in case the <span class="term">href</span>&#160;attribute value matches the regular expression pattern <span class="term">regex1</span>, and/or will remove <span class="term">href</span>&#160;if its value matches the regular expression pattern <span class="term">regex2</span>. E.g., <span class="term">array("/./", "/&#58;//\W&#42;(?!(abc\.com|xyz\.org))/")</span>; see <a href="#s3.4.7">section 3.4.7</a>&#160;for more.<br />
349 <br />
350 &#160; <strong>anti_mail_spam</strong><br />
351 &#160; Anti-mail-spam measure; see <a href="#s3.4.7">section 3.4.7</a><br />
352 <br />
353 &#160; <span class="term">0</span>&#160;- no measure taken &#160;*<br />
354 &#160; <em>word</em>&#160;- <span class="term">@</span>&#160;in mail address in <span class="term">href</span>&#160;attribute value is replaced with specified <em>word</em><br />
355 <br />
356 &#160; <strong>balance</strong><br />
357 &#160; Balance tags for well-formedness and proper nesting; see <a href="#s3.3.3">section 3.3.3</a><br />
358 <br />
359 &#160; <span class="term">0</span>&#160;- no<br />
360 &#160; <span class="term">1</span>&#160;- yes &#160;*<br />
361 <br />
362 &#160; <strong>base_url</strong><br />
363 &#160; Base URL value that needs to be set if <span class="term">$config["abs_url"]</span>&#160;is not <span class="term">0</span>; see <a href="#s3.4.4">section 3.4.4</a><br />
364 <br />
365 &#160; <strong>cdata</strong><br />
366 &#160; Handling of <span class="term">CDATA</span>&#160;sections; see <a href="#s3.3.1">section 3.3.1</a><br />
367 <br />
368 &#160; <span class="term">0</span>&#160;- don't consider <span class="term">CDATA</span>&#160;sections as markup and proceed as if plain text &#160;"<br />
369 &#160; <span class="term">1</span>&#160;- remove<br />
370 &#160; <span class="term">2</span>&#160;- allow, but neutralize any <span class="term">&lt;</span>, <span class="term">&gt;</span>, and <span class="term">&amp;</span>&#160;inside by converting them to named entities<br />
371 &#160; <span class="term">3</span>&#160;- allow &#160;*<br />
372 <br />
373 &#160; <strong>clean_ms_char</strong><br />
374 &#160; Replace <em>discouraged</em>&#160;characters introduced by Microsoft Word, etc.; see <a href="#s3.1">section 3.1</a><br />
375 <br />
376 &#160; <span class="term">0</span>&#160;- no &#160;*<br />
377 &#160; <span class="term">1</span>&#160;- yes<br />
378 &#160; <span class="term">2</span>&#160;- yes, but replace special single &amp; double quotes with ordinary ones<br />
379 <br />
380 &#160; <strong>comment</strong><br />
381 &#160; Handling of HTML comments; see <a href="#s3.3.1">section 3.3.1</a><br />
382 <br />
383 &#160; <span class="term">0</span>&#160;- don't consider comments as markup and proceed as if plain text &#160;"<br />
384 &#160; <span class="term">1</span>&#160;- remove<br />
385 &#160; <span class="term">2</span>&#160;- allow, but neutralize any <span class="term">&lt;</span>, <span class="term">&gt;</span>, and <span class="term">&amp;</span>&#160;inside by converting to named entities<br />
386 &#160; <span class="term">3</span>&#160;- allow &#160;*<br />
387 <br />
388 &#160; <strong>css_expression</strong><br />
389 &#160; Allow dynamic CSS expression by not removing the expression from CSS property values in <span class="term">style</span>&#160;attributes; see <a href="#s3.4.8">section 3.4.8</a><br />
390 <br />
391 &#160; <span class="term">0</span>&#160;- remove &#160;*<br />
392 &#160; <span class="term">1</span>&#160;- allow<br />
393 <br />
394 &#160; <strong>deny_attribute</strong><br />
395 &#160; Denied HTML attributes; see <a href="#s3.4">section 3.4</a><br />
396 <br />
397 &#160; <span class="term">0</span>&#160;- none &#160;*<br />
398 &#160; <em>string</em>&#160;- dictated by values in <em>string</em><br />
399 &#160; <span class="term">on&#42;</span>&#160;- on* event attributes like <span class="term">onfocus</span>&#160;not allowed &#160;"<br />
400 <br />
401 &#160; <strong>direct_nest_list</strong><br />
402 &#160; Allow direct nesting of a list within another without requiring it to be a list item; see <a href="#s3.3.4">section 3.3.4</a><br />
403 <br />
404 &#160; <span class="term">0</span>&#160;- no &#160;*<br />
405 &#160; <span class="term">1</span>&#160;- yes<br />
406 <br />
407 &#160; <strong>elements</strong><br />
408 &#160; Allowed HTML elements; see <a href="#s3.3">section 3.3</a><br />
409 <br />
410 &#160; <em>all</em>&#160;- *^<br />
411 &#160; <span class="term">&#42; -acronym -big -center -dir -font -isindex -s -strike -tt</span>&#160;- &#160;~^<br />
412 &#160; <em>applet, audio, canvas, embed, iframe, object, script, and video elements not allowed</em>&#160;- &#160;"^<br />
413 <br />
414 &#160; <strong>hexdec_entity</strong><br />
415 &#160; Allow hexadecimal numeric entities and do not convert to the more widely accepted decimal ones, or convert decimal to hexadecimal ones; see <a href="#s3.2">section 3.2</a><br />
416 <br />
417 &#160; <span class="term">0</span>&#160;- no<br />
418 &#160; <span class="term">1</span>&#160;- yes &#160;*<br />
419 &#160; <span class="term">2</span>&#160;- convert decimal to hexadecimal ones<br />
420 <br />
421 &#160; <strong>hook</strong><br />
422 &#160; Name of an optional hook function to alter the input string, <span class="term">$config</span>&#160;or <span class="term">$spec</span>&#160;before htmLawed enters the main phase of its work; see <a href="#s3.7">section 3.7</a><br />
423 <br />
424 &#160; <span class="term">0</span>&#160;- no hook function &#160;*<br />
425 &#160; <em>name</em>&#160;- <em>name</em>&#160;is name of the hook function<br />
426 <br />
427 &#160; <strong>hook_tag</strong><br />
428 &#160; Name of an optional hook function to alter tag content finalized by htmLawed; see <a href="#s3.4.9">section 3.4.9</a><br />
429 <br />
430 &#160; <span class="term">0</span>&#160;- no hook function &#160;*<br />
431 &#160; <em>name</em>&#160;- <em>name</em>&#160;is name of the hook function<br />
432 <br />
433 &#160; <strong>keep_bad</strong><br />
434 &#160; Neutralize <em>bad</em>&#160;tags by converting their <span class="term">&lt;</span>&#160;and <span class="term">&gt;</span>&#160;characters to entities, or remove them; see <a href="#s3.3.3">section 3.3.3</a><br />
435 <br />
436 &#160; <span class="term">0</span>&#160;- remove<br />
437 &#160; <span class="term">1</span>&#160;- neutralize both tags and element content<br />
438 &#160; <span class="term">2</span>&#160;- remove tags but neutralize element content<br />
439 &#160; <span class="term">3</span>&#160;and <span class="term">4</span>&#160;- like <span class="term">1</span>&#160;and <span class="term">2</span>&#160;but remove if text (<span class="term">pcdata</span>) is invalid in parent element<br />
440 &#160; <span class="term">5</span>&#160;and <span class="term">6</span>&#160;* - &#160;like <span class="term">3</span>&#160;and <span class="term">4</span>&#160;but line-breaks, tabs and spaces are left<br />
441 <br />
442 &#160; <strong>lc_std_val</strong><br />
443 &#160; For XHTML compliance, predefined, standard attribute values, like <span class="term">get</span>&#160;for the <span class="term">method</span>&#160;attribute of <span class="term">form</span>, must be lowercased; see <a href="#s3.4.5">section 3.4.5</a><br />
444 <br />
445 &#160; <span class="term">0</span>&#160;- no<br />
446 &#160; <span class="term">1</span>&#160;- yes &#160;*<br />
447 <br />
448 &#160; <strong>make_tag_strict</strong><br />
449 &#160; Transform or remove these deprecated HTML elements, even if they are allowed by the admin: acronym, applet, big, center, dir, font, isindex, s, strike, tt; see <a href="#s3.3.2">section 3.3.2</a><br />
450 <br />
451 &#160; <span class="term">0</span>&#160;- no<br />
452 &#160; <span class="term">1</span>&#160;- yes, but leave <span class="term">applet</span>&#160;and <span class="term">isindex</span>&#160;that currently cannot be transformed &#160;*^<br />
453 &#160; <span class="term">2</span>&#160;- yes, removing <span class="term">applet</span>&#160;and <span class="term">isindex</span>&#160;elements and their contents (nested elements remain) &#160;~^<br />
454 <br />
455 &#160; <strong>named_entity</strong><br />
456 &#160; Allow non-universal named HTML entities, or convert to numeric ones; see <a href="#s3.2">section 3.2</a><br />
457 <br />
458 &#160; <span class="term">0</span>&#160;- convert<br />
459 &#160; <span class="term">1</span>&#160;- allow &#160;*<br />
460 <br />
461 &#160; <strong>no_deprecated_attr</strong><br />
462 &#160; Allow deprecated attributes or transform them; see <a href="#s3.4.6">section 3.4.6</a><br />
463 <br />
464 &#160; <span class="term">0</span>&#160;- allow<br />
465 &#160; <span class="term">1</span>&#160;- transform, but <span class="term">name</span>&#160;attributes for <span class="term">a</span>&#160;and <span class="term">map</span>&#160;are retained &#160;*<br />
466 &#160; <span class="term">2</span>&#160;- transform<br />
467 <br />
468 &#160; <strong>parent</strong><br />
469 &#160; Name of the parent element, possibly imagined, that will hold the input; see <a href="#s3.3">section 3.3</a><br />
470 <br />
471 &#160; <strong>safe</strong><br />
472 &#160; Magic parameter to make input the most secure against vulnerabilities like XSS without needing to specify other relevant <span class="term">$config</span>&#160;parameters; see <a href="#s3.6">section 3.6</a><br />
473 <br />
474 &#160; <span class="term">0</span>&#160;- no &#160;*<br />
475 &#160; <span class="term">1</span>&#160;- will auto-adjust other relevant <span class="term">$config</span>&#160;parameters (indicated by <span class="term">"</span>&#160;in this list) &#160;^<br />
476 <br />
477 &#160; <strong>schemes</strong><br />
478 &#160; Array of attribute-specific, comma-separated, lower-cased list of schemes (protocols) allowed in attributes accepting URLs (or <span class="term">!</span>&#160;to <em>deny</em>&#160;any URL); <span class="term">&#42;</span>&#160;covers all unspecified attributes; see <a href="#s3.4.3">section 3.4.3</a><br />
479 <br />
480 &#160; <span class="term">href&#58; aim, app, feed, file, ftp, gopher, http, https, javascript, irc, mailto, news, nntp, sftp, ssh, tel, telnet; &#42;&#58;data, file, http, https, javascript</span>&#160; *^<br />
481 &#160; <span class="term">href&#58; aim, feed, file, ftp, gopher, http, https, irc, mailto, news, nntp, sftp, ssh, tel, telnet; style&#58; !; &#42;&#58;file, http, https</span>&#160; "<br />
482 <br />
483 &#160; <strong>show_setting</strong><br />
484 &#160; Name of a PHP variable to assign the <em>finalized</em>&#160;<span class="term">$config</span>&#160;and <span class="term">$spec</span>&#160;values; see <a href="#s3.8">section 3.8</a><br />
485 <br />
486 &#160; <strong>style_pass</strong><br />
487 &#160; Ignore <span class="term">style</span>&#160;attribute values, letting them through without any alteration<br />
488 <br />
489 &#160; <span class="term">0</span>&#160;- no *<br />
490 &#160; <span class="term">1</span>&#160;- htmLawed will let through any <span class="term">style</span>&#160;value; see <a href="#s3.4.8">section 3.4.8</a><br />
491 <br />
492 &#160; <strong>tidy</strong><br />
493 &#160; Beautify or compact HTML code; see <a href="#s3.3.5">section 3.3.5</a><br />
494 <br />
495 &#160; <span class="term">-1</span>&#160;- compact<br />
496 &#160; <span class="term">0</span>&#160;- no &#160;*<br />
497 &#160; <span class="term">1</span>&#160;or <em>string</em>&#160;- beautify (custom format specified by <span class="term">string</span>)<br />
498 <br />
499 &#160; <strong>unique_ids</strong><br />
500 &#160; <span class="term">id</span>&#160;attribute value checks; see <a href="#s3.4.2">section 3.4.2</a><br />
501 <br />
502 &#160; <span class="term">0</span>&#160;- no<br />
503 &#160; <span class="term">1</span>&#160;- remove duplicate and/or invalid ones &#160;*<br />
504 &#160; <em>word</em>&#160;- remove invalid ones and replace duplicate ones with new and unique ones based on the <em>word</em>; the admin-specified <em>word</em>&#160;cannot contain a space character<br />
505 <br />
506 &#160; <strong>valid_xhtml</strong><br />
507 &#160; Magic parameter to make input the most valid XHTML without needing to specify other relevant <span class="term">$config</span>&#160;parameters; see <a href="#s3.5">section 3.5</a><br />
508 <br />
509 &#160; <span class="term">0</span>&#160;- no &#160;*<br />
510 &#160; <span class="term">1</span>&#160;- will auto-adjust other relevant <span class="term">$config</span>&#160;parameters (indicated by <span class="term">~</span>&#160;in this list)<br />
511 <br />
512 &#160; <strong>xml:lang</strong><br />
513 &#160; Auto-add <span class="term">xml&#58;lang</span>&#160;attribute; see <a href="#s3.4.1">section 3.4.1</a><br />
514 <br />
515 &#160; <span class="term">0</span>&#160;- no &#160;*<br />
516 &#160; <span class="term">1</span>&#160;- add if <span class="term">lang</span>&#160;attribute is present<br />
517 &#160; <span class="term">2</span>&#160;- add if <span class="term">lang</span>&#160;attribute is present, and remove <span class="term">lang</span>&#160; ~<br />
518
519 </div>
520 <div class="sub-section"><h3>
521 <a name="s2.3" id="s2.3"></a><span class="item-no">2.3</span>&#160; Extra HTML specifications using the $spec parameter
522 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
523 <br />
524 &#160; The <span class="term">$spec</span>&#160;argument of htmLawed can be used to disallow an otherwise legal attribute for an element, or to restrict the attribute's values. This can also be helpful as a security measure (e.g., in certain versions of browsers, certain values can cause buffer overflows and denial of service attacks), or in enforcing admin policies. <span class="term">$spec</span>&#160;is specified as a string of text containing one or more <em>rules</em>, with multiple rules separated from each other by a semi-colon (<span class="term">;</span>). E.g.,<br />
525 <br />
526
527 <code class="code">&#160; &#160; $spec = &#39;i=-&#42;; td, tr=style, id, -&#42;; a=id(match="/[a-z][a-z\d.&#58;\-&#96;"]&#42;/i"/minval=2), href(maxlen=100/minlen=34); img=-width,-alt&#39;;</code>
528 <br />
529
530 <code class="code">&#160; &#160; $processed = htmLawed($text, $config, $spec);</code>
531 <br />
532 <br />
533 &#160; Or,<br />
534 <br />
535
536 <code class="code">&#160; &#160; $processed = htmLawed($text, $config, &#39;i=-&#42;; td, tr=style, id, -&#42;; a=id(match="/[a-z][a-z\d.&#58;\-&#96;"]&#42;/i"/minval=2), href(maxlen=100/minlen=34); img=-width,-alt&#39;);</code>
537 <br />
538 <br />
539 &#160; A rule begins with an HTML <strong>element</strong>&#160;name(s) (<em>rule-element</em>), for which the rule applies, followed by an equal-to (=) sign. A rule-element may represent multiple elements if comma (,)-separated element names are used. E.g., <span class="term">th,td,tr=</span>.<br />
540 <br />
541 &#160; Rest of the rule consists of comma-separated HTML <strong>attribute names</strong>. A minus (-) character before an attribute means that the attribute is not permitted inside the rule-element. E.g., <span class="term">-width</span>. To deny all attributes, <span class="term">-&#42;</span>&#160;can be used.<br />
542 <br />
543 &#160; Following shows examples of rule excerpts with rule-element <span class="term">a</span>&#160;and the attributes that are being permitted:<br />
544 <br />
545 &#160; * &#160;<span class="term">a=</span>&#160;- all<br />
546 &#160; * &#160;<span class="term">a=id</span>&#160;- all<br />
547 &#160; * &#160;<span class="term">a=href, title, -id, -onclick</span>&#160;- all except <span class="term">id</span>&#160;and <span class="term">onclick</span><br />
548 &#160; * &#160;<span class="term">a=&#42;, id, -id</span>&#160;- all except <span class="term">id</span><br />
549 &#160; * &#160;<span class="term">a=-&#42;</span>&#160;- none<br />
550 &#160; * &#160;<span class="term">a=-&#42;, href, title</span>&#160;- none except <span class="term">href</span>&#160;and <span class="term">title</span><br />
551 &#160; * &#160;<span class="term">a=-&#42;, -id, href, title</span>&#160;- none except <span class="term">href</span>&#160;and <span class="term">title</span><br />
552 <br />
553 &#160; Rules regarding <strong>attribute values</strong>&#160;are optionally specified inside round brackets after attribute names in solidus (/)-separated <em>parameter = value</em>&#160;pairs. E.g., <span class="term">title(maxlen=30/minlen=5)</span>. None or one or more of the following parameters may be specified:<br />
554 <br />
555 &#160; * &#160;<span class="term">oneof</span>&#160;- one or more choices separated by <span class="term">|</span>&#160;that the value should match; if only one choice is provided, then the value must match that choice; matching is case-sensitive<br />
556 <br />
557 &#160; * &#160;<span class="term">noneof</span>&#160;- one or more choices separated by <span class="term">|</span>&#160;that the value should not match; matching is case-sensitive<br />
558 <br />
559 &#160; * &#160;<span class="term">maxlen</span>&#160;and <span class="term">minlen</span>&#160;- upper and lower limits for the number of characters in the attribute value; specified in numbers<br />
560 <br />
561 &#160; * &#160;<span class="term">maxval</span>&#160;and <span class="term">minval</span>&#160;- upper and lower limits for the numerical value specified in the attribute value; specified in numbers<br />
562 <br />
563 &#160; * &#160;<span class="term">match</span>&#160;and <span class="term">nomatch</span>&#160;- pattern that the attribute value should or should not match; specified as PHP/PCRE-compatible regular expressions with delimiters and possibly modifiers (e.g., to specify case-sensitivity for matching)<br />
564 <br />
565 &#160; * &#160;<span class="term">default</span>&#160;- a value to force on the attribute if the value provided by the writer does not fit any of the specified parameters<br />
566 <br />
567 &#160; If <span class="term">default</span>&#160;is not set and the attribute value does not satisfy any of the specified parameters, then the attribute is removed. The <span class="term">default</span>&#160;value can also be used to force all attribute declarations to take the same value (by getting the values declared illegal by setting, e.g., <span class="term">maxlen</span>&#160;to <span class="term">-1</span>).<br />
568 <br />
569 &#160; Examples with <em>input</em>&#160;<span class="term">&lt;input title="WIDTH" value="10em" /&gt;&lt;input title="length" value="5" class="ic1 ic2" /&gt;</span>&#160;are shown below.<br />
570 <br />
571 &#160; <em>Rule</em>: <span class="term">input=title(maxlen=60/minlen=6), value</span><br />
572 &#160; <em>Output</em>: <span class="term">&lt;input value="10em" /&gt;&lt;input title="length" value="5" class="ic1 ic2" /&gt;</span><br />
573 <br />
574 &#160; <em>Rule</em>: <span class="term">input=title(), value(maxval=8/default=6)</span><br />
575 &#160; <em>Output</em>: <span class="term">&lt;input title="WIDTH" value="6" /&gt;&lt;input title="length" value="5" class="ic1 ic2" /&gt;</span><br />
576 <br />
577 &#160; <em>Rule</em>: <span class="term">input=title(nomatch=%w.d%i), value(match=%em%/default=6em)</span><br />
578 &#160; <em>Output</em>: <span class="term">&lt;input value="10em" /&gt;&lt;input title="length" value="6em" class="ic1 ic2" /&gt;</span><br />
579 <br />
580 &#160; <em>Rule</em>: <span class="term">input=class(noneof=ic2|ic3/oneof=ic1|ic4), title(oneof=height|depth/default=depth), value(noneof=5|6)</span><br />
581 &#160; <em>Output</em>: <span class="term">&lt;input title="depth" value="10em" /&gt;&lt;input title="depth" class="ic1" /&gt;</span><br />
582 <br />
583 &#160; <strong>Special characters</strong>: The characters <span class="term">;</span>, <span class="term">,</span>, <span class="term">/</span>, <span class="term">(</span>, <span class="term">)</span>, <span class="term">|</span>, <span class="term">~</span>&#160;and space have special meanings in the rules. Words in the rules that use such characters, or the characters themselves, should be <em>escaped</em>&#160;by enclosing in pairs of double-quotes (<span class="term">"</span>). A back-tick (<span class="term">&#96;</span>) can be used to escape a literal <span class="term">"</span>. An example rule illustrating this is <span class="term">input=value(maxlen=30/match="/^\w/"/default="your &#96;"ID&#96;"")</span>.<br />
584 <br />
585 &#160; <strong>Attributes that accept multiple values</strong>: If an attribute is <span class="term">accesskey</span>, <span class="term">class</span>, <span class="term">itemtype</span>&#160;or <span class="term">rel</span>, which can have multiple, space-separated values, or <span class="term">srcset</span>, which can have multiple, comma-separated values, htmLawed will parse the attribute value for such multiple values and will individually test each of them.<br />
586 <br />
587 &#160; <strong>Note</strong>: To deny an attribute for all elements for which it is legal, <span class="term">$config["deny_attribute"]</span>&#160;(see <a href="#s3.4">section 3.4</a>) can be used instead of <span class="term">$spec</span>. Also, attributes can be allowed element-specifically through <span class="term">$spec</span>&#160;while being denied globally through <span class="term">$config["deny_attribute"]</span>. The <span class="term">hook_tag</span>&#160;parameter (<a href="#s3.4.9">section 3.4.9</a>) can also be possibly used to implement a functionality like that achieved using <span class="term">$spec</span>&#160;functionality.<br />
588 <br />
589 &#160; <strong>Note</strong>: Attributes' specifications for an element may be set through multiple rules. In case of conflict, the attribute specification in the first rule will get precedence.<br />
590 <br />
591 &#160; <span class="term">$spec</span>&#160;can also be used to permit custom, non-standard attributes as well as custom rules for standard attributes. Thus, the following value of <span class="term">$spec</span>&#160;will permit the custom uses of the standard <span class="term">rel</span>&#160;attribute in <span class="term">input</span>&#160;(not permitted as per standards) and of a non-standard attribute, <span class="term">vFlag</span>, in <span class="term">img</span>.<br />
592 <br />
593
594 <code class="code">&#160; &#160; $spec = &#39;img=vFlag; input=rel&#39;</code>
595 <br />
596 <br />
597 &#160; The attribute names must begin with an alphabet and cannot have space, equal-to (=) and solidus (/) characters.<br />
598
599 </div>
600 <div class="sub-section"><h3>
601 <a name="s2.4" id="s2.4"></a><span class="item-no">2.4</span>&#160; Performance time &amp; memory usage
602 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
603 <br />
604 &#160; The time and memory consumed during text processing by htmLawed depends on its configuration, the size of the input, and the amount, nestedness and well-formedness of the HTML markup within the input. In particular, tag balancing and beautification each can increase the processing time by about a quarter.<br />
605 <br />
606 &#160; The htmLawed <a href="htmLawedTest.php">demo</a>&#160;can be used to evaluate the performance and effects of different types of input and <span class="term">$config</span>.<br />
607
608 </div>
609 <div class="sub-section"><h3>
610 <a name="s2.5" id="s2.5"></a><span class="item-no">2.5</span>&#160; Some security risks to keep in mind
611 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
612 <br />
613 &#160; When setting the parameters/arguments (like those to allow certain HTML elements) for use with htmLawed, one should bear in mind that the setting may let through potentially <em>dangerous</em>&#160;HTML code which is meant to steal user-data, deface a website, render a page non-functional, etc. Unless end-users, either people or software, supplying the content are completely trusted, security issues arising from the degree of HTML usage permitted through htmLawed's setting should be considered. For example, following increase security risks:<br />
614 <br />
615 &#160; * &#160;Allowing <span class="term">script</span>, <span class="term">applet</span>, <span class="term">embed</span>, <span class="term">iframe</span>, <span class="term">canvas</span>, <span class="term">audio</span>, <span class="term">video</span>&#160;or <span class="term">object</span>&#160;elements, or certain of their attributes like <span class="term">allowscriptaccess</span><br />
616 <br />
617 &#160; * &#160;Allowing HTML comments (some Internet Explorer versions are vulnerable with, e.g., <span class="term">&lt;!--[if gte IE 4]&gt;&lt;script&gt;alert("xss");&lt;/script&gt;&lt;![endif]--&gt;</span><br />
618 <br />
619 &#160; * &#160;Allowing dynamic CSS expressions (some Internet Explorer versions are vulnerable)<br />
620 <br />
621 &#160; * &#160;Allowing the <span class="term">style</span>&#160;attribute<br />
622 <br />
623 &#160; To remove <em>unsecure</em>&#160;HTML, code-developers using htmLawed must set <span class="term">$config</span>&#160;appropriately. E.g., <span class="term">$config["elements"] = "&#42; -script"</span>&#160;to deny the <span class="term">script</span>&#160;element (<a href="#s3.3">section 3.3</a>), <span class="term">$config["safe"] = 1</span>&#160;to auto-configure ceratin htmLawed parameters for maximizing security (<a href="#s3.6">section 3.6</a>), etc.<br />
624 <br />
625 &#160; Permitting the <span class="term">&#42;style&#42;</span>&#160;attribute brings in risks of <em>click-jacking</em>, <em>phishing</em>, web-page overlays, etc., <em>even</em>&#160;when the <span class="term">safe</span>&#160;parameter is enabled (see <a href="#s3.6">section 3.6</a>). Except for URLs and a few other things like CSS dynamic expressions, htmLawed currently does not check every CSS style property. It does provide ways for the code-developer implementing htmLawed to do such checks through htmLawed's <span class="term">$spec</span>&#160;argument, and through the <span class="term">hook_tag</span>&#160;parameter (see <a href="#s3.4.8">section 3.4.8</a>&#160;for more). Disallowing <span class="term">style</span>&#160;completely and relying on CSS classes and stylesheet files is recommended.<br />
626 <br />
627 &#160; htmLawed does not check or correct the character <strong>encoding</strong>&#160;of the input it receives. In conjunction with permissive circumstances, such as when the character encoding is left undefined through HTTP headers or HTML <span class="term">meta</span>&#160;tags, this can allow for an exploit (like Google's <em>UTF-7/XSS</em>&#160;vulnerability of the past).<br />
628 <br />
629 &#160; Ocassionally, though very rarely, the default settings with which htmLawed runs may change between different versions of htmLawed. Admins should keep this in mind when upgrading htmLawed. Important changes in htmLawed's default behavior in new releases of the software are noted in <a href="#s4.5">section 4.5</a>&#160;on upgrades.<br />
630
631 </div>
632 <div class="sub-section"><h3>
633 <a name="s2.6" id="s2.6"></a><span class="item-no">2.6</span>&#160; Use with <span class="term">kses()</span>&#160;code
634 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
635 <br />
636 &#160; The <span class="term">Kses</span>&#160;PHP script for HTML filtering is used by many applications (like <span class="term">WordPress</span>, as in year 2012). It is possible to have such applications use htmLawed instead, since it is compatible with code that calls the <span class="term">kses()</span>&#160;function declared in the <span class="term">Kses</span>&#160;file (usually named <span class="term">kses.php</span>). E.g., application code like this will continue to work after replacing <span class="term">Kses</span>&#160;with htmLawed:<br />
637 <br />
638
639 <code class="code">&#160; &#160; $comment_filtered = kses($comment_input, array(&#39;a&#39;=&gt;array(), &#39;b&#39;=&gt;array(), &#39;i&#39;=&gt;array()));</code>
640 <br />
641 <br />
642 &#160; If the application uses a <span class="term">Kses</span>&#160;file that has the <span class="term">kses()</span>&#160;function declared, then, to have the application use htmLawed instead of <span class="term">Kses</span>, rename <span class="term">htmLawed.php</span>&#160;(to <span class="term">kses.php</span>, e.g.) and replace the <span class="term">Kses</span>&#160;file (or just replace the code in the <span class="term">Kses</span>&#160;file with the htmLawed code). If the <span class="term">kses()</span>&#160;function in the <span class="term">Kses</span>&#160;file had been renamed by the application developer (e.g., in <span class="term">WordPress</span>, it is named <span class="term">wp_kses()</span>), then appropriately rename the <span class="term">kses()</span>&#160;function in the htmLawed code. Then, add the following code (which was a part of htmLawed prior to version 1.2):<br />
643 <br />
644
645 <code class="code">&#160; &#160; // kses compatibility</code>
646 <br />
647
648 <code class="code">&#160; &#160; function kses($t, $h, $p=array(&#39;http&#39;, &#39;https&#39;, &#39;ftp&#39;, &#39;news&#39;, &#39;nntp&#39;, &#39;telnet&#39;, &#39;gopher&#39;, &#39;mailto&#39;)){</code>
649 <br />
650
651 <code class="code">&#160; &#160; &#160;foreach($h as $k=&gt;$v){</code>
652 <br />
653
654 <code class="code">&#160; &#160; &#160; $h[$k][&#39;n&#39;][&#39;&#42;&#39;] = 1;</code>
655 <br />
656
657 <code class="code">&#160; &#160; &#160;}</code>
658 <br />
659
660 <code class="code">&#160; &#160; &#160;$C[&#39;cdata&#39;] = $C[&#39;comment&#39;] = $C[&#39;make_tag_strict&#39;] = $C[&#39;no_deprecated_attr&#39;] = $C[&#39;unique_ids&#39;] = 0;</code>
661 <br />
662
663 <code class="code">&#160; &#160; &#160;$C[&#39;keep_bad&#39;] = 1;</code>
664 <br />
665
666 <code class="code">&#160; &#160; &#160;$C[&#39;elements&#39;] = count($h) ? strtolower(implode(&#39;,&#39;, array_keys($h))) &#58; &#39;-&#42;&#39;;</code>
667 <br />
668
669 <code class="code">&#160; &#160; &#160;$C[&#39;hook&#39;] = &#39;kses_hook&#39;;</code>
670 <br />
671
672 <code class="code">&#160; &#160; &#160;$C[&#39;schemes&#39;] = &#39;&#42;&#58;&#39;. implode(&#39;,&#39;, $p);</code>
673 <br />
674
675 <code class="code">&#160; &#160; &#160;return htmLawed($t, $C, $h);</code>
676 <br />
677
678 <code class="code">&#160; &#160; &#160;}</code>
679 <br />
680 <br />
681
682 <code class="code">&#160; &#160; function kses_hook($t, &amp;$C, &amp;$S){</code>
683 <br />
684
685 <code class="code">&#160; &#160; &#160;return $t;</code>
686 <br />
687
688 <code class="code">&#160; &#160; }</code>
689 <br />
690 <br />
691 &#160; If the <span class="term">Kses</span>&#160;file used by the application has been significantly altered by the application developers, then one may need a different approach. E.g., with <span class="term">WordPress</span>&#160;(as in the year 2012), it is best to copy the htmLawed code, along with the above-mentioned additions, to <span class="term">wp_includes/kses.php</span>, rename the newly added function <span class="term">kses()</span>&#160;to <span class="term">wp_kses()</span>, and delete the code for the original <span class="term">wp_kses()</span>&#160;function.<br />
692 <br />
693 &#160; If the <span class="term">Kses</span>&#160;code has a non-empty hook function (e.g., <span class="term">wp_kses_hook()</span>&#160;in case of <span class="term">WordPress</span>), then the code for htmLawed's <span class="term">kses_hook()</span>&#160;function should be appropriately edited. However, the requirement of the hook function should be re-evaluated considering that htmLawed has extra capabilities. With <span class="term">WordPress</span>, the hook function is an essential one. The following code is suggested for the htmLawed <span class="term">kses_hook()</span>&#160;in case of <span class="term">WordPress</span>:<br />
694 <br />
695
696 <code class="code">&#160; &#160; // kses compatibility</code>
697 <br />
698
699 <code class="code">&#160; &#160; function kses_hook($string, &amp;$cf, &amp;$spec){</code>
700 <br />
701
702 <code class="code">&#160; &#160; &#160;$allowed_html = $spec;</code>
703 <br />
704
705 <code class="code">&#160; &#160; &#160;$allowed_protocols = array();</code>
706 <br />
707
708 <code class="code">&#160; &#160; &#160;foreach($cf[&#39;schemes&#39;] as $v){</code>
709 <br />
710
711 <code class="code">&#160; &#160; &#160; foreach($v as $k2=&gt;$v2){</code>
712 <br />
713
714 <code class="code">&#160; &#160; &#160; &#160;if(!in_array($k2, $allowed_protocols)){</code>
715 <br />
716
717 <code class="code">&#160; &#160; &#160; &#160; $allowed_protocols[] = $k2;</code>
718 <br />
719
720 <code class="code">&#160; &#160; &#160; &#160;}</code>
721 <br />
722
723 <code class="code">&#160; &#160; &#160; }</code>
724 <br />
725
726 <code class="code">&#160; &#160; &#160;}</code>
727 <br />
728
729 <code class="code">&#160; &#160; &#160;return wp_kses_hook($string, $allowed_html, $allowed_protocols);</code>
730 <br />
731
732 <code class="code">&#160; &#160; }</code>
733 <br />
734
735 </div>
736 <div class="sub-section"><h3>
737 <a name="s2.7" id="s2.7"></a><span class="item-no">2.7</span>&#160; Tolerance for ill-written HTML
738 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
739 <br />
740 &#160; htmLawed can work with ill-written HTML code in the input. However, HTML that is too ill-written may not be <em>read</em>&#160;as HTML, and may therefore get identified as mere plain text. Following statements indicate the degree of <em>looseness</em>&#160;that htmLawed can work with, and can be provided in instructions to writers:<br />
741 <br />
742 &#160; * &#160;Tags must be flanked by <span class="term">&lt;</span>&#160;and <span class="term">&gt;</span>&#160;with no <span class="term">&gt;</span>&#160;inside -- any needed <span class="term">&gt;</span>&#160;should be put in as <span class="term">&amp;gt;</span>. It is possible for tag content (element name and attributes) to be spread over many lines instead of being on one. A space may be present between the tag content and <span class="term">&gt;</span>, like <span class="term">&lt;div &gt;</span>&#160;and <span class="term">&lt;img / &gt;</span>, but not after the <span class="term">&lt;</span>.<br />
743 <br />
744 &#160; * &#160;Element and attribute names need not be lower-cased.<br />
745 <br />
746 &#160; * &#160;Attribute string of elements may be liberally spaced with tabs, line-breaks, etc.<br />
747 <br />
748 &#160; * &#160;Attribute values may be single- and not double-quoted.<br />
749 <br />
750 &#160; * &#160;Left-padding of numeric entities (like, <span class="term">&amp;#0160;</span>, <span class="term">&amp;x07ff;</span>) with <span class="term">0</span>&#160;is okay as long as the number of characters between between the <span class="term">&amp;</span>&#160;and the <span class="term">;</span>&#160;does not exceed 8. All entities must end with <span class="term">;</span>&#160;though.<br />
751 <br />
752 &#160; * &#160;Named character entities must be properly cased. Thus, <span class="term">&amp;Lt;</span>&#160;or <span class="term">&amp;TILDE;</span>&#160;will not be recognized as entities and will be <em>neutralized</em>.<br />
753 <br />
754 &#160; * &#160;HTML comments should not be inside element tags (they can be between tags), and should begin with <span class="term">&lt;!--</span>&#160;and end with <span class="term">--&gt;</span>. Characters like <span class="term">&lt;</span>, <span class="term">&gt;</span>, and <span class="term">&amp;</span>&#160;may be allowed inside depending on <span class="term">$config</span>, but any <span class="term">--&gt;</span>&#160;inside should be put in as <span class="term">--&amp;gt;</span>. Any <span class="term">--</span>&#160;inside will be automatically converted to <span class="term">-</span>, and a space will be added before the <span class="term">--&gt;</span>&#160;comment-closing marker &#160;unless <span class="term">$config["comments"]</span>&#160;is set to <span class="term">4</span>&#160;(<a href="#s3.3.1">section 3.3.1</a>).<br />
755 <br />
756 &#160; * &#160;<span class="term">CDATA</span>&#160;sections should not be inside element tags, and can be in element content only if plain text is allowed for that element. They should begin with <span class="term">&lt;[CDATA[</span>&#160;and end with <span class="term">]]&gt;</span>. Characters like <span class="term">&lt;</span>, <span class="term">&gt;</span>, and <span class="term">&amp;</span>&#160;may be allowed inside depending on <span class="term">$config</span>, but any <span class="term">]]&gt;</span>&#160;inside should be put in as <span class="term">]]&amp;gt;</span>.<br />
757 <br />
758 &#160; * &#160;For attribute values, character entities <span class="term">&amp;lt;</span>, <span class="term">&amp;gt;</span>&#160;and <span class="term">&amp;amp;</span>&#160;should be used instead of characters <span class="term">&lt;</span>&#160;and <span class="term">&gt;</span>, and <span class="term">&amp;</span>&#160;(when <span class="term">&amp;</span>&#160;is not part of a character entity). This applies even for Javascript code in values of attributes like <span class="term">onclick</span>.<br />
759 <br />
760 &#160; * &#160;Characters <span class="term">&lt;</span>, <span class="term">&gt;</span>, <span class="term">&amp;</span>&#160;and <span class="term">"</span>&#160;that are part of actual Javascript, etc., code in <span class="term">script</span>&#160;elements should be used as such and not be put in as entities like <span class="term">&amp;gt;</span>. Otherwise, though the HTML will be valid, the code may fail to work. Further, if such characters have to be used, then they should be put inside <span class="term">CDATA</span>&#160;sections.<br />
761 <br />
762 &#160; * &#160;Simple instructions like "an opening tag cannot be present between two closing tags" and "nested elements should be closed in the reverse order of how they were opened" can help authors write balanced HTML. If tags are imbalanced, htmLawed will try to balance them, but in the process, depending on <span class="term">$config["keep_bad"]</span>, some code/text may be lost.<br />
763 <br />
764 &#160; * &#160;Input authors should be notified of admin-specified allowed elements, attributes, configuration values (like conversion of named entities to numeric ones), etc.<br />
765 <br />
766 &#160; * &#160;With <span class="term">$config["unique_ids"]</span>&#160;not <span class="term">0</span>&#160;and the <span class="term">id</span>&#160;attribute being permitted, writers should carefully avoid using duplicate or invalid <span class="term">id</span>&#160;values as even though htmLawed will correct/remove the values, the final output may not be the one desired. E.g., when <span class="term">&lt;a id="home"&gt;&lt;/a&gt;&lt;input id="home" /&gt;&lt;label for="home"&gt;&lt;/label&gt;</span>&#160;is processed into<br />
767 <span class="term">&lt;a id="home"&gt;&lt;/a&gt;&lt;input id="prefix_home" /&gt;&lt;label for="home"&gt;&lt;/label&gt;</span>.<br />
768 <br />
769 &#160; * &#160;Even if intended HTML is lost from an ill-written input, the processed output will be more secure and standard-compliant.<br />
770 <br />
771 &#160; * &#160;For URLs, unless <span class="term">$config["scheme"]</span>&#160;is appropriately set, writers should avoid using escape characters or entities in schemes. E.g., <span class="term">htt&amp;#112;</span>&#160;(which many browsers will read as the harmless <span class="term">http</span>) may be considered bad by htmLawed.<br />
772 <br />
773 &#160; * &#160;htmLawed will attempt to put plain text present directly inside <span class="term">blockquote</span>, <span class="term">form</span>, <span class="term">map</span>&#160;and <span class="term">noscript</span>&#160;elements (illegal as per the specifications) inside auto-generated <span class="term">div</span>&#160;elements during tag balancing (<a href="#s3.3.3">section 3.3.3</a>).<br />
774
775 </div>
776 <div class="sub-section"><h3>
777 <a name="s2.8" id="s2.8"></a><span class="item-no">2.8</span>&#160; Limitations &amp; work-arounds
778 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
779 <br />
780 &#160; htmLawed's main objective is to make the input text <em>more</em>&#160;standard-compliant, secure for readers, and free of HTML elements and attributes considered undesirable by the administrator. Some of its current limitations, regardless of this objective, are noted below along with possible work-arounds.<br />
781 <br />
782 &#160; It should be borne in mind that no browser application is 100% standard-compliant, standard specifications continue to evolve, and many browsers accept commonly used non-standard HTML. Regarding security, note that <em>unsafe</em>&#160;HTML code is not legally invalid per se.<br />
783 <br />
784 &#160; * &#160;By default, htmLawed will not strictly adhere to the <em>current</em>&#160;HTML standard. Admins can configure htmLawed to be more strict about standard compliance. Standard specification for HTML is continuously evolving. There are two bodies (<a href="http://www.w3c.org">W3C</a>&#160;and <a href="http://www.whatwg.org">WHATWG</a>) that specify the standard and their specifications are not identical. E.g., as in mid-2013, the <span class="term">border</span>&#160;attribute is valid in <span class="term">table</span>&#160;as per W3C but not WHATWG. Thus, htmLawed may not be fully compliant with the standard of a specific group. The HTML standards/rules that htmLawed uses in its logic are a mix of the W3C and WHATWG standards, and can be lax because of the laxity of HTML interpreters (browsers) regarding standards.<br />
785 <br />
786 &#160; * &#160;In general, htmLawed processes input to generate output that is most likely to be standard-compatible in most users' browsers. Thus, for example, it does not enforce the required value of <span class="term">0</span>&#160;on <span class="term">border</span>&#160;attribute of <span class="term">img</span>&#160;(an HTML version 5 specification).<br />
787 <br />
788 &#160; * &#160;htmLawed is meant for input that goes into the <span class="term">body</span>&#160;of HTML documents. HTML's head-level elements are not supported, nor are the frame-specific elements <span class="term">frameset</span>, <span class="term">frame</span>&#160;and <span class="term">noframes</span>. However, content of the latter elements can be individually filtered through htmLawed.<br />
789 <br />
790 &#160; * &#160;It cannot handle input that has non-HTML code like <span class="term">SVG</span>&#160;and <span class="term">MathML</span>. One way around is to break the input into pieces and passing only those without non-HTML code to htmLawed. Another is described in <a href="#s3.9">section 3.9</a>. A third way may be to some how take advantage of the <span class="term">$config["and_mark"]</span>&#160;parameter (see <a href="#s3.2">section 3.2</a>).<br />
791 <br />
792 &#160; * &#160;By default, htmLawed won't check many attribute values for standard compliance. E.g., <span class="term">width="20m"</span>&#160;with the dimension in non-standard <span class="term">m</span>&#160;is let through. Implementing universal and strict attribute value checks can make htmLawed slow and resource-intensive. Admins should look at the <span class="term">hook_tag</span>&#160;parameter (<a href="#s3.4.9">section 3.4.9</a>) or <span class="term">$spec</span>&#160;to enforce finer checks on attribute values.<br />
793 <br />
794 &#160; * &#160;By default, htmLawed considers all ARIA, data-*, event and microdata attributes as global attributes and permits them in all elements. This is not strictly standard-compliant. E.g., the <span class="term">itemtype</span>&#160;microdata attribute is permitted only in elements that also have the <span class="term">itemscope</span>&#160;attribute. Admins can configure htmLawed to be more strict about this (<a href="#s2.3">section 2.3</a>).<br />
795 <br />
796 &#160; * &#160;The attributes, deprecated (which can be transformed too) or not, that it supports are largely those that are in the specifications. Only a few of the proprietary attributes are supported. However, <span class="term">$spec</span>&#160;can be used to allow custom attributes (<a href="#s2.3">section 2.3</a>).<br />
797 <br />
798 &#160; * &#160;Except for contained URLs and dynamic expressions (also optional), htmLawed does not check CSS style property values. Admins should look at using the <span class="term">hook_tag</span>&#160;parameter (<a href="#s3.4.9">section 3.4.9</a>) or <span class="term">$spec</span>&#160;for finer checks. Perhaps the best option is to disallow <span class="term">style</span>&#160;but allow <span class="term">class</span>&#160;attributes with the right <span class="term">oneof</span>&#160;or <span class="term">match</span>&#160;values for <span class="term">class</span>, and have the various class style properties in <span class="term">.css</span>&#160;CSS stylesheet files.<br />
799 <br />
800 &#160; * &#160;htmLawed does not parse emoticons, decode <em>BBcode</em>, or <em>wikify</em>, auto-converting text to proper HTML. Similarly, it won't convert line-breaks to <span class="term">br</span>&#160;elements. Such functions are beyond its purview. Admins should use other code to pre- or post-process the input for such purposes.<br />
801 <br />
802 &#160; * &#160;htmLawed cannot be used to have links force-opened in new windows (by auto-adding appropriate <span class="term">target</span>&#160;and <span class="term">onclick</span>&#160;attributes to <span class="term">a</span>). Admins should look at Javascript-based DOM-modifying solutions for this. Admins may also be able to use a custom hook function to enforce such checks (<span class="term">hook_tag</span>&#160;parameter; see <a href="#s3.4.9">section 3.4.9</a>).<br />
803 <br />
804 &#160; * &#160;Nesting-based checks are not possible. E.g., one cannot disallow <span class="term">p</span>&#160;elements specifically inside <span class="term">td</span>&#160;while permitting it elsewhere. Admins may be able to use a custom hook function to enforce such checks (<span class="term">hook_tag</span>&#160;parameter; see <a href="#s3.4.9">section 3.4.9</a>).<br />
805 <br />
806 &#160; * &#160;Except for optionally converting absolute or relative URLs to the other type, htmLawed will not alter URLs (e.g., to change the value of query strings or to convert <span class="term">http</span>&#160;to <span class="term">https</span>. Having absolute URLs may be a standard-requirement, e.g., when HTML is embedded in email messages, whereas altering URLs for other purposes is beyond htmLawed's goals. Admins may be able to use a custom hook function to enforce such checks (<span class="term">hook_tag</span>&#160;parameter; see <a href="#s3.4.9">section 3.4.9</a>).<br />
807 <br />
808 &#160; * &#160;Pairs of opening and closing tags that do not enclose any content (like <span class="term">&lt;em&gt;&lt;/em&gt;</span>) are not removed. This may be against the standard specification for certain elements (e.g., <span class="term">table</span>). However, presence of such standard-incompliant code will not break the display or layout of content. Admins can also use simple regex-based code to filter out such code.<br />
809 <br />
810 &#160; * &#160;htmLawed does not check for certain element orderings described in the standard specifications (e.g., in a <span class="term">table</span>, <span class="term">tbody</span>&#160;is allowed before <span class="term">tfoot</span>). Admins may be able to use a custom hook function to enforce such checks (<span class="term">hook_tag</span>&#160;parameter; see <a href="#s3.4.9">section 3.4.9</a>).<br />
811 <br />
812 &#160; * &#160;htmLawed does not check the number of nested elements. E.g., it will allow two <span class="term">caption</span>&#160;elements in a <span class="term">table</span>&#160;element, illegal as per standard specifications. Admins may be able to use a custom hook function to enforce such checks (<span class="term">hook_tag</span>&#160;parameter; see <a href="#s3.4.9">section 3.4.9</a>).<br />
813 <br />
814 &#160; * &#160;There are multiple ways to interpret ill-written HTML. E.g., in <span class="term">&lt;small&gt;&lt;small&gt;text&lt;/small&gt;</span>, is it that the second closing tag for <span class="term">small</span>&#160;is missing or is it that the second opening tag for <span class="term">small</span>&#160;was put in by mistake? htmLawed corrects the HTML in the string assuming the former, while the user may have intended the string for the latter. This is an issue that is impossible to address perfectly.<br />
815 <br />
816 &#160; * &#160;htmLawed might convert certain entities to actual characters and remove backslashes and CSS comment-markers (<span class="term">/&#42;</span>) in <span class="term">style</span>&#160;attribute values in order to detect malicious HTML like crafted, Internet Explorer browser-specific dynamic expressions like <span class="term">&amp;#101;xpression...</span>. If this is too harsh, admins can allow CSS expressions through htmLawed core but then use a custom function through the <span class="term">hook_tag</span>&#160;parameter (<a href="#s3.4.9">section 3.4.9</a>) to more specifically identify CSS expressions in the <span class="term">style</span>&#160;attribute values. Also, using <span class="term">$config["style_pass"]</span>, it is possible to have htmLawed pass <span class="term">style</span>&#160;attribute values without even looking at them (<a href="#s3.4.8">section 3.4.8</a>).<br />
817 <br />
818 &#160; * &#160;htmLawed does not correct certain possible attribute-based security vulnerabilities (e.g., <span class="term">&lt;a href="http&#58;//x%22+style=%22background-image&#58;xss"&gt;x&lt;/a&gt;</span>). These arise when browsers mis-identify markup in <em>escaped</em>&#160;text, defeating the very purpose of escaping text (a bad browser will read the given example as <span class="term">&lt;a href="http&#58;//x" style="background-image&#58;xss"&gt;x&lt;/a&gt;</span>).<br />
819 <br />
820 &#160; * &#160;Because of poor Unicode support in PHP, htmLawed does not remove the <em>high value</em>&#160;HTML-invalid characters with multi-byte code-points. Such characters however are extremely unlikely to be in the input. (see <a href="#s3.1">section 3.1</a>).<br />
821 <br />
822 &#160; * &#160;htmLawed does not check or correct the character encoding of the input it receives. In conjunction with permitting circumstances such as when the character encoding is left undefined through HTTP headers or HTML <span class="term">meta</span>&#160;tags, this can permit an exploit (like Google's <em>UTF-7/XSS</em>&#160;vulnerability of the past). Also, htmLawed can mangle input text if it is not well-formed in terms of character encoding. Administrators can consider using code available elsewhere to check well-formedness of input text characters to correct any defect.<br />
823 <br />
824 &#160; * &#160;htmLawed is expected to work with input texts in ASCII standard-compatible single-byte encodings such as national variants of ASCII (like ISO-646-DE/German of the ISO 646 standard), extended ASCII variants (like ISO 8859-10/Turkish of the ISO 8859/ISO Latin standard), ISO 8859-based Windows variants (like Windows 1252), EBCDIC, Shift JIS (Japanese), GB-Roman (Chinese), and KS-Roman (Korean). It should also properly handle texts with variable-byte encodings like UTF-7 (Unicode) and UTF-8 (Unicode). However, htmLawed may mangle input texts with double-byte encodings like UTF-16 (Unicode), JIS X 0208:1997 (Japanese) and K SX 1001:1992 (Korean), or the UTF-32 (Unicode) quadruple-byte encoding. If an input text has such an encoding, administrators can use PHP's <a href="http://php.net/manual/en/book.iconv.php">iconv</a>&#160;functions, or some other mean, to convert text to UTF-8 before passing it to htmLawed.<br />
825 <br />
826 &#160; * &#160;Like any script using PHP's PCRE regex functions, PHP setup-specific low PCRE limit values can cause htmLawed to at least partially fail with very long input texts.<br />
827
828 </div>
829 <div class="sub-section"><h3>
830 <a name="s2.9" id="s2.9"></a><span class="item-no">2.9</span>&#160; Examples of usage
831 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
832 <br />
833 &#160; Safest, allowing only <em>safe</em>&#160;HTML markup --<br />
834 <br />
835
836 <code class="code">&#160; &#160; $config = array(&#39;safe&#39;=&gt;1);</code>
837 <br />
838
839 <code class="code">&#160; &#160; $out = htmLawed($in, $config);</code>
840 <br />
841 <br />
842 &#160; Simplest, allowing all valid HTML markup including Javascript --<br />
843 <br />
844
845 <code class="code">&#160; &#160; $out = htmLawed($in);</code>
846 <br />
847 <br />
848 &#160; Allowing all valid HTML markup but restricting URL schemes in <span class="term">src</span>&#160;attribute values to <span class="term">http</span>&#160;and <span class="term">https</span>&#160;--<br />
849 <br />
850
851 <code class="code">&#160; &#160; $config = array(&#39;schemes&#39;=&gt;&#39;&#42;&#58;&#42;; src&#58;http, https&#39;);</code>
852 <br />
853
854 <code class="code">&#160; &#160; $out = htmLawed($in, $config);</code>
855 <br />
856 <br />
857 &#160; Allowing only <span class="term">safe</span>&#160;HTML and the elements <span class="term">a</span>, <span class="term">em</span>, and <span class="term">strong</span>&#160;--<br />
858 <br />
859
860 <code class="code">&#160; &#160; $config = array(&#39;safe&#39;=&gt;1, &#39;elements&#39;=&gt;&#39;a, em, strong&#39;);</code>
861 <br />
862
863 <code class="code">&#160; &#160; $out = htmLawed($in, $config);</code>
864 <br />
865 <br />
866 &#160; Not allowing elements <span class="term">script</span>&#160;and <span class="term">object</span>&#160;--<br />
867 <br />
868
869 <code class="code">&#160; &#160; $config = array(&#39;elements&#39;=&gt;&#39;&#42; -script -object&#39;);</code>
870 <br />
871
872 <code class="code">&#160; &#160; $out = htmLawed($in, $config);</code>
873 <br />
874 <br />
875 &#160; Not allowing attributes <span class="term">id</span>&#160;and <span class="term">style</span>&#160;--<br />
876 <br />
877
878 <code class="code">&#160; &#160; $config = array(&#39;deny_attribute&#39;=&gt;&#39;id, style&#39;);</code>
879 <br />
880
881 <code class="code">&#160; &#160; $out = htmLawed($in, $config);</code>
882 <br />
883 <br />
884 &#160; Permitting only attributes <span class="term">title</span>&#160;and <span class="term">href</span>&#160;--<br />
885 <br />
886
887 <code class="code">&#160; &#160; $config = array(&#39;deny_attribute&#39;=&gt;&#39;&#42; -title -href&#39;);</code>
888 <br />
889
890 <code class="code">&#160; &#160; $out = htmLawed($in, $config);</code>
891 <br />
892 <br />
893 &#160; Remove bad/disallowed tags altogether instead of converting them to entities --<br />
894 <br />
895
896 <code class="code">&#160; &#160; $config = array(&#39;keep_bad&#39;=&gt;0);</code>
897 <br />
898
899 <code class="code">&#160; &#160; $out = htmLawed($in, $config);</code>
900 <br />
901 <br />
902 &#160; Allowing attribute <span class="term">title</span>&#160;only in <span class="term">a</span>&#160;and not allowing attributes <span class="term">id</span>, <span class="term">style</span>, or scriptable <em>on*</em>&#160;attributes like <span class="term">onclick</span>&#160;--<br />
903 <br />
904
905 <code class="code">&#160; &#160; $config = array(&#39;deny_attribute&#39;=&gt;&#39;title, id, style, on&#42;&#39;);</code>
906 <br />
907
908 <code class="code">&#160; &#160; $spec = &#39;a=title&#39;;</code>
909 <br />
910
911 <code class="code">&#160; &#160; $out = htmLawed($in, $config, $spec);</code>
912 <br />
913 <br />
914 &#160; Allowing a custom attribute, <span class="term">vFlag</span>, in <span class="term">img</span>&#160;and permitting custom use of the standard attribute, <span class="term">rel</span>, in <span class="term">input</span>&#160;--<br />
915 <br />
916
917 <code class="code">&#160; &#160; $spec = &#39;img=vFlag; input=rel&#39;;</code>
918 <br />
919
920 <code class="code">&#160; &#160; $out = htmLawed($in, $config, $spec);</code>
921 <br />
922 <br />
923 &#160; Some case-studies are presented below.<br />
924 <br />
925 &#160; <strong>1.</strong>&#160;A blog administrator wants to allow only <span class="term">a</span>, <span class="term">em</span>, <span class="term">strike</span>, <span class="term">strong</span>&#160;and <span class="term">u</span>&#160;in comments, but needs <span class="term">strike</span>&#160;and <span class="term">u</span>&#160;transformed to <span class="term">span</span>&#160;for better XHTML 1-strict compliance, and, he wants the <span class="term">a</span>&#160;links to point only to <span class="term">http</span>&#160;or <span class="term">https</span>&#160;resources:<br />
926 <br />
927
928 <code class="code">&#160; &#160; $processed = htmLawed($in, array(&#39;elements&#39;=&gt;&#39;a, em, strike, strong, u&#39;, &#39;make_tag_strict&#39;=&gt;1, &#39;safe&#39;=&gt;1, &#39;schemes&#39;=&gt;&#39;&#42;&#58;http, https&#39;), &#39;a=href&#39;);</code>
929 <br />
930 <br />
931 &#160; <strong>2.</strong>&#160;An author uses a custom-made web application to load content on his website. He is the only one using that application and the content he generates has all types of HTML, including scripts. The web application uses htmLawed primarily as a tool to correct errors that creep in while writing HTML and to take care of the occasional <em>bad</em>&#160;characters in copy-paste text introduced by Microsoft Office. The web application provides a preview before submitted input is added to the content. For the previewing process, htmLawed is set up as follows:<br />
932 <br />
933
934 <code class="code">&#160; &#160; $processed = htmLawed($in, array(&#39;css_expression&#39;=&gt;1, &#39;keep_bad&#39;=&gt;1, &#39;make_tag_strict&#39;=&gt;1, &#39;schemes&#39;=&gt;&#39;&#42;&#58;&#42;&#39;, &#39;valid_xhtml&#39;=&gt;1));</code>
935 <br />
936 <br />
937 &#160; For the final submission process, <span class="term">keep_bad</span>&#160;is set to <span class="term">6</span>. A value of <span class="term">1</span>&#160;for the preview process allows the author to note and correct any HTML mistake without losing any of the typed text.<br />
938 <br />
939 &#160; <strong>3.</strong>&#160;A data-miner is scraping information in a specific table of similar web-pages and is collating the data rows, and uses htmLawed to reduce unnecessary markup and white-spaces:<br />
940 <br />
941
942 <code class="code">&#160; &#160; $processed = htmLawed($in, array(&#39;elements&#39;=&gt;&#39;tr, td&#39;, &#39;tidy&#39;=&gt;-1), &#39;tr, td =&#39;);</code>
943 <br />
944
945 </div>
946 </div>
947 <div class="section"><h2>
948 <a name="s3" id="s3"></a><span class="item-no">3</span>&#160; Details
949 </h2><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
950 <div class="sub-section"><h3>
951 <a name="s3.1" id="s3.1"></a><span class="item-no">3.1</span>&#160; Invalid/dangerous characters
952 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
953 <br />
954 &#160; Valid characters (more correctly, their code-points) in HTML or XML are, hexadecimally, <span class="term">9</span>, <span class="term">a</span>, <span class="term">d</span>, <span class="term">20</span>&#160;to <span class="term">d7ff</span>, and <span class="term">e000</span>&#160;to <span class="term">10ffff</span>, except <span class="term">fffe</span>&#160;and <span class="term">ffff</span>&#160;(decimally, <span class="term">9</span>, <span class="term">10</span>, <span class="term">13</span>, <span class="term">32</span>&#160;to <span class="term">55295</span>, and <span class="term">57344</span>&#160;to <span class="term">1114111</span>, except <span class="term">65534</span>&#160;and <span class="term">65535</span>). htmLawed removes the invalid characters <span class="term">0</span>&#160;to <span class="term">8</span>, <span class="term">b</span>, <span class="term">c</span>, and <span class="term">e</span>&#160;to <span class="term">1f</span>.<br />
955 <br />
956 &#160; Because of PHP's poor native support for multi-byte characters, htmLawed cannot check for the remaining invalid code-points. However, for various reasons, it is very unlikely for any of those characters to be in the input.<br />
957 <br />
958 &#160; Characters that are discouraged (see <a href="#s5.1">section 5.1</a>) but not invalid are not removed by htmLawed.<br />
959 <br />
960 &#160; It (function <span class="term">hl_tag()</span>) also replaces the potentially dangerous (in some Mozilla [Firefox] and Opera browsers) soft-hyphen character (code-point, hexadecimally, <span class="term">ad</span>, or decimally, <span class="term">173</span>) in attribute values with spaces. Where required, the characters <span class="term">&lt;</span>, <span class="term">&gt;</span>, <span class="term">&amp;</span>, and <span class="term">"</span>&#160;are converted to entities.<br />
961 <br />
962 &#160; With <span class="term">$config["clean_ms_char"]</span>&#160;set as <span class="term">1</span>&#160;or <span class="term">2</span>, many of the discouraged characters (decimal code-points <span class="term">127</span>&#160;to <span class="term">159</span>&#160;except <span class="term">133</span>) that many Microsoft applications incorrectly use (as per the <span class="term">Windows 1252</span>&#160;[<span class="term">Cp-1252</span>] or a similar encoding system), and the character for decimal code-point <span class="term">133</span>, are converted to appropriate decimal numerical entities (or removed for a few cases)-- see appendix in <a href="#s5.4">section 5.4</a>. This can help avoid some display issues arising from copying-pasting of content.<br />
963 <br />
964 &#160; With <span class="term">$config["clean_ms_char"]</span>&#160;set as <span class="term">2</span>, characters for the hexadecimal code-points <span class="term">82</span>, <span class="term">91</span>, and <span class="term">92</span>&#160;(for special single-quotes), and <span class="term">84</span>, <span class="term">93</span>, and <span class="term">94</span>&#160;(for special double-quotes) are converted to ordinary single and double quotes respectively and not to entities.<br />
965 <br />
966 &#160; The character values are replaced with entities/characters and not character values referred to by the entities/characters to keep this task independent of the character-encoding of input text.<br />
967 <br />
968 &#160; The <span class="term">$config["clean_ms_char"]</span>&#160;parameter should not be used if authors do not copy-paste Microsoft-created text, or if the input text is not believed to use the <span class="term">Windows 1252</span>&#160;(<span class="term">Cp-1252</span>) or a similar encoding like <span class="term">Cp-1251</span>&#160;(otherwise, for example when UTF-8 encoding is in use, Japanese or Korean characters can get mangled). Further, the input form and the web-pages displaying it or its content should have the character encoding appropriately marked-up.<br />
969
970 </div>
971 <div class="sub-section"><h3>
972 <a name="s3.2" id="s3.2"></a><span class="item-no">3.2</span>&#160; Character references/entities
973 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
974 <br />
975 &#160; Valid character entities take the form <span class="term">&amp;&#42;;</span>&#160;where <span class="term">&#42;</span>&#160;is <span class="term">#x</span>&#160;followed by a hexadecimal number (hexadecimal numeric entity; like <span class="term">&amp;#xA0;</span>&#160;for non-breaking space), or alphanumeric like <span class="term">gt</span>&#160;(external or named entity; like <span class="term">&amp;nbsp;</span>&#160;for non-breaking space), or <span class="term">#</span>&#160;followed by a number (decimal numeric entity; like <span class="term">&amp;#160;</span>&#160;for non-breaking space). Character entities referring to the soft-hyphen character (the <span class="term">&amp;shy;</span>&#160;or <span class="term">\xad</span>&#160;character; hexadecimal code-point <span class="term">ad</span>&#160;[decimal <span class="term">173</span>]) in URL-accepting attribute values are always replaced with spaces; soft-hyphens in attribute values introduce vulnerabilities in some older versions of the Opera and Mozilla [Firefox] browsers.<br />
976 <br />
977 &#160; htmLawed (function <span class="term">hl_ent()</span>):<br />
978 <br />
979 &#160; * &#160;Neutralizes entities with multiple leading zeroes or missing semi-colons (potentially dangerous)<br />
980 <br />
981 &#160; * &#160;Lowercases the <span class="term">X</span>&#160;(for XML-compliance) and <span class="term">A-F</span>&#160;of hexadecimal numeric entities<br />
982 <br />
983 &#160; * &#160;Neutralizes entities referring to characters that are HTML-invalid (see <a href="#s3.1">section 3.1</a>)<br />
984 <br />
985 &#160; * &#160;Neutralizes entities referring to characters that are HTML-discouraged (code-points, hexadecimally, <span class="term">7f</span>&#160;to <span class="term">84</span>, <span class="term">86</span>&#160;to <span class="term">9f</span>, and <span class="term">fdd0</span>&#160;to <span class="term">fddf</span>, or decimally, <span class="term">127</span>&#160;to <span class="term">132</span>, <span class="term">134</span>&#160;to <span class="term">159</span>, and <span class="term">64991</span>&#160;to <span class="term">64976</span>). Entities referring to the remaining discouraged characters (see <a href="#s5.1">section 5.1</a>&#160;for a full list) are let through.<br />
986 <br />
987 &#160; * &#160;Neutralizes named entities that are not in the specifications<br />
988 <br />
989 &#160; * &#160;Optionally converts valid HTML-specific named entities except <span class="term">&amp;gt;</span>, <span class="term">&amp;lt;</span>, <span class="term">&amp;quot;</span>, and <span class="term">&amp;amp;</span>&#160;to decimal numeric ones (hexadecimal if $config["hexdec_entity"] is <span class="term">2</span>) for generic XML-compliance. For this, <span class="term">$config["named_entity"]</span>&#160;should be <span class="term">1</span>.<br />
990 <br />
991 &#160; * &#160;Optionally converts hexadecimal numeric entities to the more widely supported decimal ones. For this, <span class="term">$config["hexdec_entity"]</span>&#160;should be <span class="term">0</span>.<br />
992 <br />
993 &#160; * &#160;Optionally converts decimal numeric entities to the hexadecimal ones. For this, <span class="term">$config["hexdec_entity"]</span>&#160;should be <span class="term">2</span>.<br />
994 <br />
995 &#160; <em>Neutralization</em>&#160;refers to the <em>entitification</em>&#160;of <span class="term">&amp;</span>&#160;to <span class="term">&amp;amp;</span>.<br />
996 <br />
997 &#160; <strong>Note</strong>: htmLawed does not convert entities to the actual characters represented by them; one can pass the htmLawed output through PHP's <span class="term">html_entity_decode</span>&#160;<a href="http://www.php.net/html_entity_decode">function</a>&#160;for that.<br />
998 <br />
999 &#160; <strong>Note</strong>: If <span class="term">$config["and_mark"]</span>&#160;is set, and set to a value other than <span class="term">0</span>, then the <span class="term">&amp;</span>&#160;characters in the original input are replaced with the control character for the hexadecimal code-point <span class="term">6</span>&#160;(<span class="term">\x06</span>; <span class="term">&amp;</span>&#160;characters introduced by htmLawed, e.g., after converting <span class="term">&lt;</span>&#160;to <span class="term">&amp;lt;</span>, are not affected). This allows one to distinguish, say, an <span class="term">&amp;gt;</span>&#160;introduced by htmLawed and an <span class="term">&amp;gt;</span>&#160;put in by the input writer, and can be helpful in further processing of the htmLawed-processed text (e.g., to identify the character sequence <span class="term">o(&gt;&lt;)o</span>&#160;to generate an emoticon image). When this feature is active, admins should ensure that the htmLawed output is not directly used in web pages or XML documents as the presence of the <span class="term">\x06</span>&#160;can break documents. Before use in such documents, and preferably before any storage, any remaining <span class="term">\x06</span>&#160;should be changed back to <span class="term">&amp;</span>, e.g., with:<br />
1000 <br />
1001
1002 <code class="code">&#160; &#160; $final = str_replace("\x06", &#39;&amp;&#39;, $prelim);</code>
1003 <br />
1004 <br />
1005 &#160; Also, see <a href="#s3.9">section 3.9</a>.<br />
1006
1007 </div>
1008 <div class="sub-section"><h3>
1009 <a name="s3.3" id="s3.3"></a><span class="item-no">3.3</span>&#160; HTML elements
1010 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1011 <br />
1012 &#160; htmLawed can be configured to allow only certain HTML elements (tags) in the input. Disallowed elements (just tag-content, and not element-content), based on <span class="term">$config["keep_bad"]</span>, are either <em>neutralized</em>&#160;(converted to plain text by entitification of <span class="term">&lt;</span>&#160;and <span class="term">&gt;</span>) or removed.<br />
1013 <br />
1014 &#160; E.g., with only <span class="term">em</span>&#160;permitted:<br />
1015 <br />
1016 &#160; Input:<br />
1017 <br />
1018
1019 <code class="code">&#160; &#160; &#160; &lt;em&gt;My&lt;/em&gt; website is &lt;a href="http&#58;//a.com&gt;a.com&lt;/a&gt;.</code>
1020 <br />
1021 <br />
1022 &#160; Output, with <span class="term">$config["keep_bad"] = 0</span>:<br />
1023 <br />
1024
1025 <code class="code">&#160; &#160; &#160; &lt;em&gt;My&lt;/em&gt; website is a.com.</code>
1026 <br />
1027 <br />
1028 &#160; Output, with <span class="term">$config["keep_bad"]</span>&#160;not <span class="term">0</span>:<br />
1029 <br />
1030
1031 <code class="code">&#160; &#160; &#160; &lt;em&gt;My&lt;/em&gt; website is &amp;lt;a href=""&amp;gt;a.com&amp;lt;/a&amp;gt;.</code>
1032 <br />
1033 <br />
1034 &#160; See <a href="#s3.3.3">section 3.3.3</a>&#160;for differences between the various non-zero <span class="term">$config["keep_bad"]</span>&#160;values.<br />
1035 <br />
1036 &#160; htmLawed by default permits these 118 HTML elements:<br />
1037 <br />
1038
1039 <code class="code">&#160; &#160; a, abbr, acronym, address, applet, area, article, aside, audio, b, bdi, bdo, big, blockquote, br, button, canvas, caption, center, cite, code, col, colgroup, command, data, datalist, dd, del, details, dfn, dir, div, dl, dt, em, embed, fieldset, figcaption, figure, font, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup, hr, i, iframe, img, input, ins, isindex, kbd, keygen, label, legend, li, link, main, map, mark, menu, meta, meter, nav, noscript, object, ol, optgroup, option, output, p, param, pre, progress, q, rb, rbc, rp, rt, rtc, ruby, s, samp, script, section, select, small, source, span, strike, strong, style, sub, summary, sup, table, tbody, td, textarea, tfoot, th, thead, time, tr, track, tt, u, ul, var, video, wbr</code>
1040 <br />
1041 <br />
1042 &#160; The HTML version 4 elements <span class="term">acronym</span>, <span class="term">applet</span>, <span class="term">big</span>, <span class="term">center</span>, <span class="term">dir</span>, <span class="term">font</span>, <span class="term">strike</span>, and <span class="term">tt</span>&#160;are obsolete/deprecated in HTML version 5. On the other hand, the obsolete/deprecated HTML 4 elements <span class="term">embed</span>, <span class="term">menu</span>&#160;and <span class="term">u</span>&#160;are no longer so in HTML 5. Elements new to HTML 5 are <span class="term">article</span>, <span class="term">aside</span>, <span class="term">audio</span>, <span class="term">bdi</span>, <span class="term">canvas</span>, <span class="term">command</span>, <span class="term">data</span>, <span class="term">datalist</span>, <span class="term">details</span>, <span class="term">figure</span>, <span class="term">figcaption</span>, <span class="term">footer</span>, <span class="term">header</span>, <span class="term">hgroup</span>, <span class="term">keygen</span>, <span class="term">link</span>, <span class="term">main</span>, <span class="term">mark</span>, <span class="term">meta</span>, <span class="term">meter</span>, <span class="term">nav</span>, <span class="term">output</span>, <span class="term">progress</span>, <span class="term">section</span>, <span class="term">source</span>, <span class="term">style</span>, <span class="term">summary</span>, <span class="term">time</span>, <span class="term">track</span>, <span class="term">video</span>, and <span class="term">wbr</span>. The <span class="term">link</span>, <span class="term">meta</span>&#160;and <span class="term">style</span>&#160;elements exist in HTML 4 but are not allowed in the HTML body. These 16 elements are <em>empty</em>&#160;elements that have an opening tag with possible content but no element content (thus, no closing tag): <span class="term">area</span>, <span class="term">br</span>, <span class="term">col</span>, <span class="term">command</span>, <span class="term">embed</span>, <span class="term">hr</span>, <span class="term">img</span>, <span class="term">input</span>, <span class="term">isindex</span>, <span class="term">keygen</span>, <span class="term">link</span>, <span class="term">meta</span>, <span class="term">param</span>, <span class="term">source</span>, <span class="term">track</span>, and <span class="term">wbr</span>.<br />
1043 <br />
1044 &#160; With <span class="term">$config["safe"] = 1</span>, the default set will exclude <span class="term">applet</span>, <span class="term">audio</span>, <span class="term">canvas</span>, <span class="term">embed</span>, <span class="term">iframe</span>, <span class="term">object</span>, <span class="term">script</span>&#160;and <span class="term">video</span>; see <a href="#s3.6">section 3.6</a>.<br />
1045 <br />
1046 &#160; When <span class="term">$config["elements"]</span>, which specifies allowed elements, is <em>properly</em>&#160;defined, and neither empty nor set to <span class="term">0</span>&#160;or <span class="term">&#42;</span>, the default set is not used. To have elements added to or removed from the default set, a <span class="term">+/-</span>&#160;notation is used. E.g., <span class="term">&#42;-script-object</span>&#160;implies that only <span class="term">script</span>&#160;and <span class="term">object</span>&#160;are disallowed, whereas <span class="term">&#42;+embed</span>&#160;means that <span class="term">noembed</span>&#160;is also allowed. Elements can also be specified as comma separated names. E.g., <span class="term">a, b, i</span>&#160;means only <span class="term">a</span>, <span class="term">b</span>&#160;and <span class="term">i</span>&#160;are permitted. In this notation, <span class="term">&#42;</span>, <span class="term">+</span>&#160;and <span class="term">-</span>&#160;have no significance and can actually cause a mis-reading.<br />
1047 <br />
1048 &#160; Some more examples of <span class="term">$config["elements"]</span>&#160;values indicating permitted elements (note that empty spaces are liberally allowed for clarity):<br />
1049 <br />
1050 &#160; * &#160;<span class="term">a, blockquote, code, em, strong</span>&#160;-- only <span class="term">a</span>, <span class="term">blockquote</span>, <span class="term">code</span>, <span class="term">em</span>, and <span class="term">strong</span><br />
1051 &#160; * &#160;<span class="term">&#42;-script</span>&#160;-- all excluding <span class="term">script</span><br />
1052 &#160; * &#160;<span class="term">&#42; -acronym -big -center -dir -font -isindex -s -strike -tt</span>&#160;-- only non-obsolete/deprecated elements of HTML5<br />
1053 &#160; * &#160;<span class="term">&#42;+noembed-script</span>&#160;-- all including <span class="term">noembed</span>&#160;excluding <span class="term">script</span><br />
1054 <br />
1055 &#160; Some mis-usages (and the resulting permitted elements) that can be avoided:<br />
1056 <br />
1057 &#160; * &#160;<span class="term">-&#42;</span>&#160;-- none; instead of htmLawed, one might just use, e.g., the <span class="term">htmlspecialchars()</span>&#160;PHP function<br />
1058 &#160; * &#160;<span class="term">&#42;, -script</span>&#160;-- all except <span class="term">script</span>; admin probably meant <span class="term">&#42;-script</span><br />
1059 &#160; * &#160;<span class="term">-&#42;, a, em, strong</span>&#160;-- all; admin probably meant <span class="term">a, em, strong</span><br />
1060 &#160; * &#160;<span class="term">&#42;</span>&#160;-- all; admin need not have set <span class="term">elements</span><br />
1061 &#160; * &#160;<span class="term">&#42;-form+form</span>&#160;-- all; a <span class="term">+</span>&#160;will always over-ride any <span class="term">-</span><br />
1062 &#160; * &#160;<span class="term">&#42;, noembed</span>&#160;-- only <span class="term">noembed</span>; admin probably meant <span class="term">&#42;+noembed</span><br />
1063 &#160; * &#160;<span class="term">a, +b, i</span>&#160;-- only <span class="term">a</span>&#160;and <span class="term">i</span>; admin probably meant <span class="term">a, b, i</span><br />
1064 <br />
1065 &#160; Basically, when using the <span class="term">+/-</span>&#160;notation, commas (<span class="term">,</span>) should not be used, and vice versa, and <span class="term">&#42;</span>&#160;should be used with the former but not the latter.<br />
1066 <br />
1067 &#160; <strong>Note</strong>: Even if an element that is not in the default set is allowed through <span class="term">$config["elements"]</span>, like <span class="term">noembed</span>&#160;in the last example, it will eventually be removed during tag balancing unless such balancing is turned off (<span class="term">$config["balance"]</span>&#160;set to <span class="term">0</span>). Currently, the only way around this, which actually is simple, is to edit htmLawed's PHP code which define various arrays in the function <span class="term">hl_bal()</span>&#160;to accommodate the element and its nesting properties.<br />
1068 <br />
1069 &#160; A possible second way to specify allowed elements is to set <span class="term">$config["parent"]</span>&#160;to an element name that supposedly will hold the input, and to set <span class="term">$config["balance"]</span>&#160;to <span class="term">1</span>. During tag balancing (see <a href="#s3.3.3">section 3.3.3</a>), all elements that cannot legally nest inside the parent element will be removed. The parent element is auto-reset to <span class="term">div</span>&#160;if <span class="term">$config["parent"]</span>&#160;is empty, <span class="term">body</span>, or an element not in htmLawed's default set of 118 elements.<br />
1070 <br />
1071 &#160; <em>Tag transformation</em>&#160;is possible for improving compliance with HTML standards -- most of the obsolete/deprecated elements of HTML version 5 are converted to valid &#160;ones; see <a href="#s3.3.2">section 3.3.2</a>.<br />
1072
1073 <div class="sub-sub-section"><h4>
1074 <a name="s3.3.1" id="s3.3.1"></a><span class="item-no">3.3.1</span>&#160; Handling of comments &amp; CDATA sections
1075 </h4><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1076 <br />
1077 &#160; <span class="term">CDATA</span>&#160;sections have the format <span class="term">&lt;![CDATA[...anything but not "]]&gt;"...]]&gt;</span>, and HTML comments, <span class="term">&lt;!--...anything but not "--&gt;"... --&gt;</span>. Neither HTML comments nor <span class="term">CDATA</span>&#160;sections can reside inside tags. HTML comments can exist anywhere else, but <span class="term">CDATA</span>&#160;sections can exist only where plain text is allowed (e.g., immediately inside <span class="term">td</span>&#160;element content but not immediately inside <span class="term">tr</span>&#160;element content).<br />
1078 <br />
1079 &#160; htmLawed (function <span class="term">hl_cmtcd()</span>) handles HTML comments or <span class="term">CDATA</span>&#160;sections depending on the values of <span class="term">$config["comment"]</span>&#160;or <span class="term">$config["cdata"]</span>. If <span class="term">0</span>, such markup is not looked for and the text is processed like plain text. If <span class="term">1</span>, it is removed completely. If <span class="term">2</span>, it is preserved but any <span class="term">&lt;</span>, <span class="term">&gt;</span>&#160;and <span class="term">&amp;</span>&#160;inside are changed to entities. If <span class="term">3</span>&#160;for <span class="term">$config["cdata"]</span>, or <span class="term">3</span>&#160;or <span class="term">4</span>&#160;for <span class="term">$config["comment"]</span>, they are left as such. When <span class="term">$config["comment"]</span>&#160;is set to <span class="term">4</span>, htmLawed will not force a space character before the <span class="term">--&gt;</span>&#160;comment-closing marker. While such a space is required for standard-compliance, it can corrupt marker code put in HTML by some software (such as Microsoft Outlook).<br />
1080 <br />
1081 &#160; Note that for the last two cases, HTML comments and <span class="term">CDATA</span>&#160;sections will always be removed from tag content (function <span class="term">hl_tag()</span>).<br />
1082 <br />
1083 &#160; Examples:<br />
1084 <br />
1085 &#160; Input:<br />
1086
1087 <code class="code">&#160; &#160; &lt;!-- home link--&gt;&lt;a href="home.htm"&gt;&lt;![CDATA[x=&amp;y]]&gt;Home&lt;/a&gt;</code>
1088 <br />
1089 &#160; Output (<span class="term">$config["comment"] = 0, $config["cdata"] = 2</span>):<br />
1090
1091 <code class="code">&#160; &#160; &amp;lt;-- home link--&amp;gt;&lt;a href="home.htm"&gt;&lt;![CDATA[x=&amp;amp;y]]&gt;Home&lt;/a&gt;</code>
1092 <br />
1093 &#160; Output (<span class="term">$config["comment"] = 1, $config["cdata"] = 2</span>):<br />
1094
1095 <code class="code">&#160; &#160; &lt;a href="home.htm"&gt;&lt;![CDATA[x=&amp;amp;y]]&gt;Home&lt;/a&gt;</code>
1096 <br />
1097 &#160; Output (<span class="term">$config["comment"] = 2, $config["cdata"] = 2</span>):<br />
1098
1099 <code class="code">&#160; &#160; &lt;!-- home link --&gt;&lt;a href="home.htm"&gt;&lt;![CDATA[x=&amp;amp;y]]&gt;Home&lt;/a&gt;</code>
1100 <br />
1101 &#160; Output (<span class="term">$config["comment"] = 2, $config["cdata"] = 1</span>):<br />
1102
1103 <code class="code">&#160; &#160; &lt;!-- home link --&gt;&lt;a href="home.htm"&gt;Home&lt;/a&gt;</code>
1104 <br />
1105 &#160; Output (<span class="term">$config["comment"] = 3, $config["cdata"] = 3</span>):<br />
1106
1107 <code class="code">&#160; &#160; &lt;!-- home link --&gt;&lt;a href="home.htm"&gt;&lt;![CDATA[x=&amp;y]]&gt;Home&lt;/a&gt;</code>
1108 <br />
1109 &#160; Output (<span class="term">$config["comment"] = 4, $config["cdata"] = 3</span>):<br />
1110
1111 <code class="code">&#160; &#160; &lt;!-- home link--&gt;&lt;a href="home.htm"&gt;&lt;![CDATA[x=&amp;y]]&gt;Home&lt;/a&gt;</code>
1112 <br />
1113 <br />
1114 &#160; For standard-compliance, comments are given the form <span class="term">&lt;!--comment --&gt;</span>, and any <span class="term">--</span>&#160;in the content is made <span class="term">-</span>. When <span class="term">$config["comment"]</span>&#160;is set to <span class="term">4</span>, htmLawed will not force a space character before the <span class="term">--&gt;</span>&#160;comment-closing marker.<br />
1115 <br />
1116 &#160; When <span class="term">$config["safe"] = 1</span>, CDATA sections and comments are considered plain text unless <span class="term">$config["comment"]</span>&#160;or <span class="term">$config["cdata"]</span>&#160;is explicitly specified; see <a href="#s3.6">section 3.6</a>.<br />
1117
1118 </div>
1119 <div class="sub-sub-section"><h4>
1120 <a name="s3.3.2" id="s3.3.2"></a><span class="item-no">3.3.2</span>&#160; Tag-transformation for better compliance with standards
1121 </h4><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1122 <br />
1123 &#160; If <span class="term">$config["make_tag_strict"]</span>&#160;is set and not <span class="term">0</span>, following deprecated elements (and attributes), as per HTML 5 specification, even if admin-permitted, are mutated as indicated (element content remains intact; function <span class="term">hl_tag2()</span>):<br />
1124 <br />
1125 &#160; * &#160;acronym - <span class="term">abbr</span><br />
1126 &#160; * &#160;applet - based on <span class="term">$config["make_tag_strict"]</span>, unchanged (<span class="term">1</span>) or removed (<span class="term">2</span>)<br />
1127 &#160; * &#160;big - <span class="term">span style="font-size&#58; larger;"</span><br />
1128 &#160; * &#160;center - <span class="term">div style="text-align&#58; center;"</span><br />
1129 &#160; * &#160;dir - <span class="term">ul</span><br />
1130 &#160; * &#160;font (face, size, color) - &#160; &#160;<span class="term">span style="font-family&#58; ; font-size&#58; ; color&#58; ;"</span>&#160;(size transformation <a href="http://style.cleverchimp.com/font_size_intervals/altintervals.html">reference</a>)<br />
1131 &#160; * &#160;isindex - based on <span class="term">$config["make_tag_strict"]</span>, unchanged (<span class="term">1</span>) or removed (<span class="term">2</span>)<br />
1132 &#160; * &#160;s - <span class="term">span style="text-decoration&#58; line-through;"</span><br />
1133 &#160; * &#160;strike - <span class="term">span style="text-decoration&#58; line-through;"</span><br />
1134 &#160; * &#160;tt - <span class="term">code</span><br />
1135 <br />
1136 &#160; For an element with a pre-existing <span class="term">style</span>&#160;attribute value, the extra style properties are appended.<br />
1137 <br />
1138 &#160; Example input:<br />
1139 <br />
1140
1141 <code class="code">&#160; &#160; &lt;center&gt;</code>
1142 <br />
1143
1144 <code class="code">&#160; &#160; &#160;The PHP &lt;s&gt;software&lt;/s&gt; script used for this &lt;strike&gt;web-page&lt;/strike&gt; web-page is &lt;font style="font-weight&#58; bold " face=arial size=&#39;+3&#39; color &#160; = &#160;"red &#160;"&gt;htmLawedTest.php&lt;/font&gt;, from &lt;u style= &#39;color&#58;green&#39;&gt;PHP Labware&lt;/u&gt;.</code>
1145 <br />
1146
1147 <code class="code">&#160; &#160; &lt;/center&gt;</code>
1148 <br />
1149 <br />
1150 &#160; The output:<br />
1151 <br />
1152
1153 <code class="code">&#160; &#160; &lt;div style="text-align&#58; center;"&gt;</code>
1154 <br />
1155
1156 <code class="code">&#160; &#160; &#160;The PHP &lt;span style="text-decoration&#58; line-through;"&gt;software&lt;/span&gt; script used for this &lt;span style="text-decoration&#58; line-through;"&gt;web-page&lt;/span&gt; web-page is &lt;span style="font-weight&#58; bold; font-family&#58; arial; color&#58; red; font-size&#58; 200%;"&gt;htmLawedTest.php&lt;/span&gt;, from &lt;span style="color&#58;green; text-decoration&#58; underline;"&gt;PHP Labware&lt;/span&gt;.</code>
1157 <br />
1158
1159 <code class="code">&#160; &#160; &lt;/div&gt;</code>
1160 <br />
1161
1162 </div>
1163 <div class="sub-sub-section"><h4>
1164 <a name="s3.3.3" id="s3.3.3"></a><span class="item-no">3.3.3</span>&#160; Tag balancing &amp; proper nesting
1165 </h4><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1166 <br />
1167 &#160; If <span class="term">$config["balance"]</span>&#160;is set to <span class="term">1</span>, htmLawed (function <span class="term">hl_bal()</span>) checks and corrects the input to have properly balanced tags and legal element content (i.e., any element nesting should be valid, and plain text may be present only in the content of elements that allow them).<br />
1168 <br />
1169 &#160; Depending on the value of <span class="term">$config["keep_bad"]</span>&#160;(see <a href="#s2.2">section 2.2</a>&#160;and <a href="#s3.3">section 3.3</a>), illegal content may be removed or neutralized to plain text by converting &lt; and &gt; to entities:<br />
1170 <br />
1171 &#160; <span class="term">0</span>&#160;- remove; this option is available only to maintain Kses-compatibility and should not be used otherwise (see <a href="#s2.6">section 2.6</a>)<br />
1172 &#160; <span class="term">1</span>&#160;- neutralize tags and keep element content<br />
1173 &#160; <span class="term">2</span>&#160;- remove tags but keep element content<br />
1174 &#160; <span class="term">3</span>&#160;and <span class="term">4</span>&#160;- like <span class="term">1</span>&#160;and <span class="term">2</span>, but keep element content only if text (<span class="term">pcdata</span>) is valid in parent element as per specs<br />
1175 &#160; <span class="term">5</span>&#160;and <span class="term">6</span>&#160;- &#160;like <span class="term">3</span>&#160;and <span class="term">4</span>, but line-breaks, tabs and spaces are left<br />
1176 <br />
1177 &#160; Example input (disallowing the <span class="term">p</span>&#160;element):<br />
1178 <br />
1179
1180 <code class="code">&#160; &#160; &lt;&#42;&gt; Pseudo-tags &lt;&#42;&gt;</code>
1181 <br />
1182
1183 <code class="code">&#160; &#160; &lt;xml&gt;Non-HTML tag xml&lt;/xml&gt;</code>
1184 <br />
1185
1186 <code class="code">&#160; &#160; &lt;p&gt;</code>
1187 <br />
1188
1189 <code class="code">&#160; &#160; Disallowed tag p</code>
1190 <br />
1191
1192 <code class="code">&#160; &#160; &lt;/p&gt;</code>
1193 <br />
1194
1195 <code class="code">&#160; &#160; &lt;ul&gt;Bad&lt;li&gt;OK&lt;/li&gt;&lt;/ul&gt;</code>
1196 <br />
1197 <br />
1198 &#160; The output with <span class="term">$config["keep_bad"] = 1</span>:<br />
1199 <br />
1200
1201 <code class="code">&#160; &#160; &amp;lt;&#42;&amp;gt; Pseudo-tags &amp;lt;&#42;&amp;gt;</code>
1202 <br />
1203
1204 <code class="code">&#160; &#160; &amp;lt;xml&amp;gt;Non-HTML tag xml&amp;lt;/xml&amp;gt;</code>
1205 <br />
1206
1207 <code class="code">&#160; &#160; &amp;lt;p&amp;gt;</code>
1208 <br />
1209
1210 <code class="code">&#160; &#160; Disallowed tag p</code>
1211 <br />
1212
1213 <code class="code">&#160; &#160; &amp;lt;/p&amp;gt;</code>
1214 <br />
1215
1216 <code class="code">&#160; &#160; &lt;ul&gt;Bad&lt;li&gt;OK&lt;/li&gt;&lt;/ul&gt;</code>
1217 <br />
1218 <br />
1219 &#160; The output with <span class="term">$config["keep_bad"] = 3</span>:<br />
1220 <br />
1221
1222 <code class="code">&#160; &#160; &amp;lt;&#42;&amp;gt; Pseudo-tags &amp;lt;&#42;&amp;gt;</code>
1223 <br />
1224
1225 <code class="code">&#160; &#160; &amp;lt;xml&amp;gt;Non-HTML tag xml&amp;lt;/xml&amp;gt;</code>
1226 <br />
1227
1228 <code class="code">&#160; &#160; &amp;lt;p&amp;gt;</code>
1229 <br />
1230
1231 <code class="code">&#160; &#160; Disallowed tag p</code>
1232 <br />
1233
1234 <code class="code">&#160; &#160; &amp;lt;/p&amp;gt;</code>
1235 <br />
1236
1237 <code class="code">&#160; &#160; &lt;ul&gt;&lt;li&gt;OK&lt;/li&gt;&lt;/ul&gt;</code>
1238 <br />
1239 <br />
1240 &#160; The output with <span class="term">$config["keep_bad"] = 6</span>:<br />
1241 <br />
1242
1243 <code class="code">&#160; &#160; &amp;lt;&#42;&amp;gt; Pseudo-tags &amp;lt;&#42;&amp;gt;</code>
1244 <br />
1245
1246 <code class="code">&#160; &#160; Non-HTML tag xml</code>
1247 <br />
1248 <br />
1249
1250 <code class="code">&#160; &#160; Disallowed tag p</code>
1251 <br />
1252 <br />
1253
1254 <code class="code">&#160; &#160; &lt;ul&gt;&lt;li&gt;OK&lt;/li&gt;&lt;/ul&gt;</code>
1255 <br />
1256 <br />
1257 &#160; An option like <span class="term">1</span>&#160;is useful, e.g., when a writer previews his submission, whereas one like <span class="term">3</span>&#160;is useful before content is finalized and made available to all.<br />
1258 <br />
1259 &#160; <strong>Note:</strong>&#160;In the example above, unlike <span class="term">&lt;&#42;&gt;</span>, <span class="term">&lt;xml&gt;</span>&#160;gets considered as a tag (even though there is no HTML element named <span class="term">xml</span>). Thus, the <span class="term">keep_bad</span>&#160;parameter's value affects <span class="term">&lt;xml&gt;</span>&#160;but not <span class="term">&lt;&#42;&gt;</span>. In general, text matching the regular expression pattern <span class="term">&lt;(/?)([a-zA-Z][a-zA-Z1-6]&#42;)([^&gt;]&#42;?)\s?&gt;</span>&#160;is considered a tag (phrase enclosed by the angled brackets <span class="term">&lt;</span>&#160;and <span class="term">&gt;</span>, and starting [with an optional slash preceding] with an alphanumeric word that starts with an alphabet...), and is subjected to the <span class="term">keep_bad</span>&#160;value.<br />
1260 <br />
1261 &#160; Nesting/content rules for each of the 118 elements in htmLawed's default set (see <a href="#s3.3">section 3.3</a>) are defined in function <span class="term">hl_bal()</span>. This means that if a non-standard element besides <span class="term">embed</span>&#160;is being permitted through <span class="term">$config["elements"]</span>, the element's tag content will end up getting removed if <span class="term">$config["balance"]</span>&#160;is set to <span class="term">1</span>.<br />
1262 <br />
1263 &#160; Plain text and/or certain elements nested inside <span class="term">blockquote</span>, <span class="term">form</span>, <span class="term">map</span>&#160;and <span class="term">noscript</span>&#160;need to be in block-level elements. This point is often missed during manual writing of HTML code. htmLawed attempts to address this during balancing. E.g., if the parent container is set as <span class="term">form</span>, the input <span class="term">B&#58;&lt;input type="text" value="b" /&gt;C&#58;&lt;input type="text" value="c" /&gt;</span>&#160;is converted to <span class="term">&lt;div&gt;B&#58;&lt;input type="text" value="b" /&gt;C&#58;&lt;input type="text" value="c" /&gt;&lt;/div&gt;</span>.<br />
1264
1265 </div>
1266 <div class="sub-sub-section"><h4>
1267 <a name="s3.3.4" id="s3.3.4"></a><span class="item-no">3.3.4</span>&#160; Elements requiring child elements
1268 </h4><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1269 <br />
1270 &#160; As per HTML specifications, elements such as those below require legal child elements nested inside them:<br />
1271 <br />
1272
1273 <code class="code">&#160; &#160; blockquote, dir, dl, form, map, menu, noscript, ol, optgroup, rbc, rtc, ruby, select, table, tbody, tfoot, thead, tr, ul</code>
1274 <br />
1275 <br />
1276 &#160; In some cases, the specifications stipulate the number and/or the ordering of the child elements. A <span class="term">table</span>&#160;can have 0 or 1 <span class="term">caption</span>, <span class="term">tbody</span>, <span class="term">tfoot</span>, and <span class="term">thead</span>, but they must be in this order: <span class="term">caption</span>, <span class="term">thead</span>, <span class="term">tfoot</span>, <span class="term">tbody</span>.<br />
1277 <br />
1278 &#160; htmLawed currently does not check for conformance to these rules. Note that any non-compliance in this regard will not introduce security vulnerabilities, crash browser applications, or affect the rendering of web-pages.<br />
1279 <br />
1280 &#160; With <span class="term">$config["direct_list_nest"]</span>&#160;set to <span class="term">1</span>, htmLawed will allow direct nesting of <span class="term">ol</span>, <span class="term">ul</span>, or <span class="term">menu</span>&#160;list within another <span class="term">ol</span>, <span class="term">ul</span>, or <span class="term">menu</span>&#160;without requiring the child list to be within an <span class="term">li</span>&#160;of the parent list. While this may not be standard-compliant, directly nested lists are rendered properly by almost all browsers. The parameter <span class="term">$config["direct_list_nest"]</span>&#160;has no effect if tag balancing (<a href="#s3.3.3">section 3.3.3</a>) is turned off.<br />
1281
1282 </div>
1283 <div class="sub-sub-section"><h4>
1284 <a name="s3.3.5" id="s3.3.5"></a><span class="item-no">3.3.5</span>&#160; Beautify or compact HTML
1285 </h4><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1286 <br />
1287 &#160; By default, htmLawed will neither <em>beautify</em>&#160;HTML code by formatting it with indentations, etc., nor will it make it compact by removing un-needed white-space.(It does always properly white-space tag content.)<br />
1288 <br />
1289 &#160; As per the HTML standards, spaces, tabs and line-breaks in web-pages (except those inside <span class="term">pre</span>&#160;elements) are all considered equivalent, and referred to as <em>white-spaces</em>. Browser applications are supposed to consider contiguous white-spaces as just a single space, and to disregard white-spaces trailing opening tags or preceding closing tags. This white-space <em>normalization</em>&#160;allows the use of text/code beautifully formatted with indentations and line-spacings for readability. Such <em>pretty</em>&#160;HTML can, however, increase the size of web-pages, or make the extraction or scraping of plain text cumbersome.<br />
1290 <br />
1291 &#160; With the <span class="term">$config</span>&#160;parameter <span class="term">tidy</span>, htmLawed can be used to beautify or compact the input text. Input with just plain text and no HTML markup is also subject to this. Besides <span class="term">pre</span>, the <span class="term">script</span>&#160;and <span class="term">textarea</span>&#160;elements, CDATA sections, and HTML comments are not subjected to the tidying process.<br />
1292 <br />
1293 &#160; To <em>compact</em>, use <span class="term">$config["tidy"] = -1</span>; single instances or runs of white-spaces are replaced with a single space, and white-spaces trailing and leading open and closing tags, respectively, are removed.<br />
1294 <br />
1295 &#160; To <em>beautify</em>, <span class="term">$config["tidy"]</span>&#160;is set as <span class="term">1</span>, or for customized tidying, as a string like <span class="term">2s2n</span>. The <span class="term">s</span>&#160;or <span class="term">t</span>&#160;character specifies the use of spaces or tabs for indentation. The first and third characters, any of the digits 0-9, specify the number of spaces or tabs per indentation, and any parental lead spacing (extra indenting of the whole block of input text). The <span class="term">r</span>&#160;and <span class="term">n</span>&#160;characters are used to specify line-break characters: <span class="term">n</span>&#160;for <span class="term">\n</span>&#160;(Unix/Mac OS X line-breaks), <span class="term">rn</span>&#160;or <span class="term">nr</span>&#160;for <span class="term">\r\n</span>&#160;(Windows/DOS line-breaks), or <span class="term">r</span>&#160;for <span class="term">\r</span>.<br />
1296 <br />
1297 &#160; The <span class="term">$config["tidy"]</span>&#160;value of <span class="term">1</span>&#160;is equivalent to <span class="term">2s0n</span>. Other <span class="term">$config["tidy"]</span>&#160;values are read loosely: a value of <span class="term">4</span>&#160;is equivalent to <span class="term">4s0n</span>; <span class="term">t2</span>, to <span class="term">1t2n</span>; <span class="term">s</span>, to <span class="term">2s0n</span>; <span class="term">2TR</span>, to <span class="term">2t0r</span>; <span class="term">T1</span>, to <span class="term">1t1n</span>; <span class="term">nr3</span>, to <span class="term">3s0nr</span>, and so on. Except in the indentations and line-spacings, runs of white-spaces are replaced with a single space during beautification.<br />
1298 <br />
1299 &#160; Input formatting using <span class="term">$config["tidy"]</span>&#160;is not recommended when input text has mixed markup (like HTML + PHP).<br />
1300
1301 </div>
1302 <div class="sub-section"><h3>
1303 <a name="s3.4" id="s3.4"></a><span class="item-no">3.4</span>&#160; Attributes
1304 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1305 <br />
1306 &#160; In its default setting, htmLawed will only permit attributes described in the HTML specifications (including deprecated ones). A list of the attributes and the elements they are allowed in is in <a href="#s5.2">section 5.2</a>. Using the <span class="term">$spec</span>&#160;argument, htmLawed can be forced to permit custom, non-standard attributes as well as custom rules for standard attributes (<a href="#s2.3">section 2.3</a>).<br />
1307 <br />
1308 &#160; Custom <em>data-*</em>&#160;(<em>data-star</em>) attributes, where the first three characters of the value of <em>star</em>&#160;(*) after lower-casing do not equal <span class="term">xml</span>, and the value of <em>star</em>&#160;does not have a colon (:), equal-to (=), newline, solidus (/), space or tab character, or any upper-case A-Z character are allowed in all elements. ARIA, event and microdata attributes like <span class="term">aria-live</span>, <span class="term">onclick</span>&#160;and <span class="term">itemid</span>&#160;are also considered global attributes (<a href="#s5.2">section 5.2</a>).<br />
1309 <br />
1310 &#160; When <span class="term">$config["deny_attribute"]</span>&#160;is not set, or set to <span class="term">0</span>, or empty (<span class="term">""</span>), all attributes are permitted. Otherwise, <span class="term">$config["deny_attribute"]</span>&#160;can be set as a list of comma-separated names of the denied attributes. <span class="term">on&#42;</span>&#160;can be used to refer to the group of potentially dangerous, script-accepting event attributes like <span class="term">onblur</span>&#160;and <span class="term">onchange</span>&#160;that have <span class="term">on</span>&#160;at the beginning of their names. Similarly, <span class="term">aria&#42;</span>&#160;and <span class="term">data&#42;</span>&#160;can be used to respectively refer to the set of all ARIA and data-* attributes.<br />
1311 <br />
1312 &#160; With <span class="term">$config["safe"] = 1</span>&#160;(<a href="#s3.6">section 3.6</a>), the <span class="term">on&#42;</span>&#160;event attributes are automatically disallowed even if a value for <span class="term">$config["deny_attribute"]</span>&#160;has been manually provided.<br />
1313 <br />
1314 &#160; Note that attributes specified in <span class="term">$config["deny_attribute"]</span>&#160;are denied globally, for all elements. To deny attributes for only specific elements, <span class="term">$spec</span>&#160;(see <a href="#s2.3">section 2.3</a>) can be used. <span class="term">$spec</span>&#160;can also be used to element-specifically permit an attribute otherwise denied through <span class="term">$config["deny_attribute"]</span>.<br />
1315 <br />
1316 &#160; Finer restrictions on attributes can also be put into effect through <span class="term">$config["deny_attribute"]</span>&#160;(<a href="3.4.9">section</a>).<br />
1317 <br />
1318 &#160; <strong>Note</strong>: To deny all but a few attributes globally, a simpler way to specify <span class="term">$config["deny_attribute"]</span>&#160;would be to use the notation <span class="term">&#42; -attribute1 -attribute2 ...</span>. Thus, a value of <span class="term">&#42; -title -href</span>&#160;implies that except <span class="term">href</span>&#160;and <span class="term">title</span>&#160;(where allowed as per standards) all other attributes are to be removed. With this notation, the value for the parameter <span class="term">safe</span>&#160;(<a href="#s3.6">section 3.6</a>) will have no effect on <span class="term">deny_attribute</span>. Values of <span class="term">aria&#42;</span>&#160;<span class="term">data&#42;</span>, and <span class="term">on&#42;</span>&#160;cannot be used in this notation to refer to the sets of all ARIA, data-*, and on* attributes respectively.<br />
1319 <br />
1320 &#160; htmLawed (function <span class="term">hl_tag()</span>) also:<br />
1321 <br />
1322 &#160; * &#160;Lower-cases attribute names<br />
1323 &#160; * &#160;Removes duplicate attributes (last one stays)<br />
1324 &#160; * &#160;Gives attributes the form <span class="term">name="value"</span>&#160;and single-spaces them, removing unnecessary white-spacing<br />
1325 &#160; * &#160;Provides <em>required</em>&#160;attributes (see <a href="#s3.4.1">section 3.4.1</a>)<br />
1326 &#160; * &#160;Double-quotes values and escapes any <span class="term">"</span>&#160;inside them<br />
1327 &#160; * &#160;Replaces the possibly dangerous soft-hyphen characters (hexadecimal code-point <span class="term">ad</span>) in the values with spaces<br />
1328 &#160; * &#160;Allows custom function to additionally filter/modify attribute values (see <a href="#s3.4.9">section 3.4.9</a>)<br />
1329
1330 <div class="sub-sub-section"><h4>
1331 <a name="s3.4.1" id="s3.4.1"></a><span class="item-no">3.4.1</span>&#160; Auto-addition of XHTML-required attributes
1332 </h4><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1333 <br />
1334 &#160; If indicated attributes for the following elements are found missing, htmLawed (function <span class="term">hl_tag()</span>) will add them (with values same as attribute names unless indicated otherwise below):<br />
1335 <br />
1336 &#160; * &#160;area - alt (<span class="term">area</span>)<br />
1337 &#160; * &#160;area, img - src, alt (<span class="term">image</span>)<br />
1338 &#160; * &#160;bdo - dir (<span class="term">ltr</span>)<br />
1339 &#160; * &#160;form - action<br />
1340 &#160; * &#160;label - command<br />
1341 &#160; * &#160;map - name<br />
1342 &#160; * &#160;optgroup - label<br />
1343 &#160; * &#160;param - name<br />
1344 &#160; * &#160;style - scoped<br />
1345 &#160; * &#160;textarea - rows (<span class="term">10</span>), cols (<span class="term">50</span>)<br />
1346 <br />
1347 &#160; Additionally, with <span class="term">$config["xml&#58;lang"]</span>&#160;set to <span class="term">1</span>&#160;or <span class="term">2</span>, if the <span class="term">lang</span>&#160;but not the <span class="term">xml&#58;lang</span>&#160;attribute is declared, then the latter is added too, with a value copied from that of <span class="term">lang</span>. This is for better standard-compliance. With <span class="term">$config["xml&#58;lang"]</span>&#160;set to <span class="term">2</span>, the <span class="term">lang</span>&#160;attribute is removed (XHTML specification).<br />
1348 <br />
1349 &#160; Note that the <span class="term">name</span>&#160;attribute for <span class="term">map</span>, invalid in XHTML, is also transformed if required -- see <a href="#s3.4.6">section 3.4.6</a>.<br />
1350
1351 </div>
1352 <div class="sub-sub-section"><h4>
1353 <a name="s3.4.2" id="s3.4.2"></a><span class="item-no">3.4.2</span>&#160; Duplicate/invalid <span class="term">id</span>&#160;values
1354 </h4><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1355 <br />
1356 &#160; If <span class="term">$config["unique_ids"]</span>&#160;is <span class="term">1</span>, htmLawed (function <span class="term">hl_tag()</span>) removes <span class="term">id</span>&#160;attributes with values that are not standards-compliant (must not have a space character) or duplicate. If <span class="term">$config["unique_ids"]</span>&#160;is a word (without a non-word character like space), any duplicate but otherwise valid value will be appropriately prefixed with the word to ensure its uniqueness.<br />
1357 <br />
1358 &#160; Even if multiple inputs need to be filtered (through multiple calls to htmLawed), htmLawed ensures uniqueness of <span class="term">id</span>&#160;values as it uses a global variable (<span class="term">$GLOBALS["hl_Ids"]</span>&#160;array). Further, an admin can restrict the use of certain <span class="term">id</span>&#160;values by presetting this variable before htmLawed is called into use. E.g.:<br />
1359 <br />
1360
1361 <code class="code">&#160; &#160; $GLOBALS[&#39;hl_Ids&#39;] = array(&#39;top&#39;=&gt;1, &#39;bottom&#39;=&gt;1, &#39;myform&#39;=&gt;1); // id values not allowed in input</code>
1362 <br />
1363
1364 <code class="code">&#160; &#160; $processed = htmLawed($text); // filter input</code>
1365 <br />
1366
1367 </div>
1368 <div class="sub-sub-section"><h4>
1369 <a name="s3.4.3" id="s3.4.3"></a><span class="item-no">3.4.3</span>&#160; URL schemes &amp; scripts in attribute values
1370 </h4><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1371 <br />
1372 &#160; htmLawed edits attributes that take URLs as values if they are found to contain un-permitted schemes. E.g., if the <span class="term">afp</span>&#160;scheme is not permitted, then <span class="term">&lt;a href="afp&#58;//domain.org"&gt;</span>&#160;becomes <span class="term">&lt;a href="denied&#58;afp&#58;//domain.org"&gt;</span>, and if Javascript is not permitted <span class="term">&lt;a onclick="javascript&#58;xss();"&gt;</span>&#160;becomes <span class="term">&lt;a onclick="denied&#58;javascript&#58;xss();"&gt;</span>.<br />
1373 <br />
1374 &#160; By default htmLawed permits these schemes in URLs for the <span class="term">href</span>&#160;attribute:<br />
1375 <br />
1376
1377 <code class="code">&#160; &#160; aim, app, feed, file, ftp, gopher, http, https, javascript, irc, mailto, news, nntp, sftp, ssh, tel, telnet</code>
1378 <br />
1379 <br />
1380 &#160; Also, only <span class="term">data</span>, <span class="term">file</span>, <span class="term">http</span>, <span class="term">https</span>&#160;and <span class="term">javascript</span>&#160;are permitted in these attributes that accept URLs:<br />
1381 <br />
1382
1383 <code class="code">&#160; &#160; action, cite, classid, codebase, data, itemtype, longdesc, model, pluginspage, pluginurl, src, srcset, style, usemap, and event attributes like onclick</code>
1384 <br />
1385 <br />
1386 &#160; With <span class="term">$config["safe"] = 1</span>&#160;(<a href="#s3.6">section 3.6</a>), the above is changed to disallow <span class="term">app</span>, <span class="term">data</span>&#160;and <span class="term">javascript</span>.<br />
1387 <br />
1388 &#160; These default sets are used when <span class="term">$config["schemes"]</span>&#160;is not set (see <a href="#s2.2">section 2.2</a>). To over-ride the defaults, <span class="term">$config["schemes"]</span>&#160;is defined as a string of semi-colon-separated sub-strings of type <span class="term">attribute&#58; comma-separated schemes</span>. E.g., <span class="term">href&#58; mailto, http, https; onclick&#58; javascript; src&#58; http, https</span>. For unspecified attributes, <span class="term">data</span>, <span class="term">file</span>, <span class="term">http</span>, <span class="term">https</span>&#160;and <span class="term">javascript</span>&#160;are permitted. This can be changed by passing schemes for <span class="term">&#42;</span>&#160;in <span class="term">$config["schemes"]</span>. E.g., <span class="term">href&#58; mailto, http, https; &#42;&#58; https, https</span>.<br />
1389 <br />
1390 &#160; <span class="term">&#42;</span>&#160;(asterisk) can be put in the list of schemes to permit all protocols. E.g., <span class="term">style&#58; &#42;; img&#58; http, https</span>&#160;results in protocols not being checked in <span class="term">style</span>&#160;attribute values. However, in such cases, any relative-to-absolute URL conversion, or vice versa, (<a href="#s3.4.4">section 3.4.4</a>) is not done. When an attribute is explicitly listed in <span class="term">$config["schemes"]</span>, then filtering is dictated by the setting for the attribute, with no effect of the setting for asterisk. That is, the set of attributes that asterisk refers to no longer includes the listed attribute.<br />
1391 <br />
1392 &#160; Thus, <em>to allow the xmpp scheme</em>, one can set <span class="term">$config["schemes"]</span>&#160;as <span class="term">href&#58; mailto, http, https; &#42;&#58; http, https, xmpp</span>, or <span class="term">href&#58; mailto, http, https, xmpp; &#42;&#58; http, https, xmpp</span>, or <span class="term">&#42;&#58; &#42;</span>, and so on. The consequence of each of these example values will be different (e.g., only the last two but not the first will allow <span class="term">xmpp</span>&#160;in <span class="term">href</span>)<br />
1393 <br />
1394 &#160; As a side-note, one may find <span class="term">style&#58; &#42;</span>&#160;useful as URLs in <span class="term">style</span>&#160;attributes can be specified in a variety of ways, and the patterns that htmLawed uses to identify URLs may mistakenly identify non-URL text.<br />
1395 <br />
1396 &#160; <span class="term">!</span>&#160;can be put in the list of schemes to disallow all protocols as well as <em>local</em>&#160;URLs. Thus, with <span class="term">href&#58; http, style&#58; !</span>, <span class="term">&lt;a href="http&#58;//cnn.com" style="background-image&#58; url(local.jpg);"&gt;CNN&lt;/a&gt;</span>&#160;will become <span class="term">&lt;a href="http&#58;//cnn.com" style="background-image&#58; url(denied&#58;local.jpg);"&gt;CNN&lt;/a&gt;</span><br />
1397 <br />
1398 &#160; <strong>Note</strong>: If URL-accepting attributes other than those listed above are being allowed, then the scheme will not be checked unless the attribute name contains the string <span class="term">src</span>&#160;(e.g., <span class="term">dynsrc</span>) or starts with <span class="term">o</span>&#160;(e.g., <span class="term">onbeforecopy</span>).<br />
1399 <br />
1400 &#160; With <span class="term">$config["safe"] = 1</span>, all URLs are disallowed in the <span class="term">style</span>&#160;attribute values.<br />
1401
1402 </div>
1403 <div class="sub-sub-section"><h4>
1404 <a name="s3.4.4" id="s3.4.4"></a><span class="item-no">3.4.4</span>&#160; Absolute &amp; relative URLs in attribute values
1405 </h4><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1406 <br />
1407 &#160; htmLawed can make absolute URLs in attributes like <span class="term">href</span>&#160;relative (<span class="term">$config["abs_url"]</span>&#160;is <span class="term">-1</span>), and vice versa (<span class="term">$config["abs_url"]</span>&#160;is <span class="term">1</span>). URLs in scripts are not considered for this, and so are URLs like <span class="term">#section_6</span>&#160;(fragment), <span class="term">?name=Tim#show</span>&#160;(starting with query string), and <span class="term">;var=1?name=Tim#show</span>&#160;(starting with parameters). Further, this requires that <span class="term">$config["base_url"]</span>&#160;be set properly, with the <span class="term">&#58;//</span>&#160;and a trailing slash (<span class="term">/</span>), with no query string, etc. E.g., <span class="term">file&#58;///D&#58;/page/</span>, <span class="term">https&#58;//abc.com/x/y/</span>, or <span class="term">http&#58;//localhost/demo/</span>&#160;are okay, but <span class="term">file&#58;///D&#58;/page/?help=1</span>, <span class="term">abc.com/x/y/</span>&#160;and <span class="term">http&#58;//localhost/demo/index.htm</span>&#160;are not.<br />
1408 <br />
1409 &#160; For making absolute URLs relative, only those URLs that have the <span class="term">$config["base_url"]</span>&#160;string at the beginning are converted. E.g., with <span class="term">$config["base_url"] = "https&#58;//abc.com/x/y/"</span>, <span class="term">https&#58;//abc.com/x/y/a.gif</span>&#160;and <span class="term">https&#58;//abc.com/x/y/z/b.gif</span>&#160;become <span class="term">a.gif</span>&#160;and <span class="term">z/b.gif</span>&#160;respectively, while <span class="term">https&#58;//abc.com/x/c.gif</span>&#160;is not changed.<br />
1410 <br />
1411 &#160; When making relative URLs absolute, only values for scheme, network location (host-name) and path values in the base URL are inherited. See <a href="#s5.5">section 5.5</a>&#160;for more about the URL specification as per RFC <a href="http://www.ietf.org/rfc/rfc1808.txt">1808</a>.<br />
1412
1413 </div>
1414 <div class="sub-sub-section"><h4>
1415 <a name="s3.4.5" id="s3.4.5"></a><span class="item-no">3.4.5</span>&#160; Lower-cased, standard attribute values
1416 </h4><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1417 <br />
1418 &#160; Optionally, for standard-compliance, htmLawed (function <span class="term">hl_tag()</span>) lower-cases standard attribute values to give, e.g., <span class="term">input type="password"</span>&#160;instead of <span class="term">input type="Password"</span>, if <span class="term">$config["lc_std_val"]</span>&#160;is <span class="term">1</span>. Attribute values matching those listed below for any of the elements listed further below (plus those for the <span class="term">type</span>&#160;attribute of <span class="term">button</span>&#160;or <span class="term">input</span>) are lower-cased:<br />
1419 <br />
1420
1421 <code class="code">&#160; &#160; all, auto, baseline, bottom, button, captions, center, chapters, char, checkbox, circle, col, colgroup, color, cols, data, date, datetime, datetime-local, default, descriptions, email, file, get, groups, hidden, image, justify, left, ltr, metadata, middle, month, none, number, object, password, poly, post, preserve, radio, range, rect, ref, reset, right, row, rowgroup, rows, rtl, search, submit, subtitles, tel, text, time, top, url, week</code>
1422 <br />
1423 <br />
1424
1425 <code class="code">&#160; &#160; a, area, bdo, button, col, fieldset, form, img, input, object, ol, optgroup, option, param, script, select, table, td, textarea, tfoot, th, thead, tr, track, xml&#58;space</code>
1426 <br />
1427 <br />
1428 &#160; The following <em>empty</em>&#160;(<em>minimized</em>) attributes are always assigned lower-cased values (same as the attribute names):<br />
1429 <br />
1430
1431 <code class="code">&#160; &#160; checkbox, checked, command, compact, declare, defer, default, disabled, hidden, inert, ismap, itemscope, multiple, nohref, noresize, noshade, nowrap, open, radio, readonly, required, reversed, selected</code>
1432 <br />
1433
1434 </div>
1435 <div class="sub-sub-section"><h4>
1436 <a name="s3.4.6" id="s3.4.6"></a><span class="item-no">3.4.6</span>&#160; Transformation of deprecated attributes
1437 </h4><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1438 <br />
1439 &#160; If <span class="term">$config["no_deprecated_attr"]</span>&#160;is <span class="term">0</span>, then deprecated attributes are removed and, in most cases, their values are transformed to CSS style properties and added to the <span class="term">style</span>&#160;attributes (function <span class="term">hl_tag()</span>). Except for <span class="term">bordercolor</span>&#160;for <span class="term">table</span>, <span class="term">tr</span>&#160;and <span class="term">td</span>, the scores of proprietary attributes that were never part of any cross-browser standard are not supported in this functionality.<br />
1440 <br />
1441 &#160; * &#160;align in caption, div, h, h2, h3, h4, h5, h6, hr, img, input, legend, object, p, table - for <span class="term">img</span>&#160;with value of <span class="term">left</span>&#160;or <span class="term">right</span>, becomes, e.g., <span class="term">float&#58; left</span>; for <span class="term">div</span>&#160;and <span class="term">table</span>&#160;with value <span class="term">center</span>, becomes <span class="term">margin&#58; auto</span>; all others become, e.g., <span class="term">text-align&#58; right</span><br />
1442 &#160; * &#160;bgcolor in table, td, th and tr - E.g., <span class="term">bgcolor="#ffffff"</span>&#160;becomes <span class="term">background-color&#58; #ffffff</span><br />
1443 &#160; * &#160;border in object - E.g., <span class="term">height="10"</span>&#160;becomes <span class="term">height&#58; 10px</span><br />
1444 &#160; * &#160;bordercolor in table, td and tr - E.g., <span class="term">bordercolor=#999999</span>&#160;becomes <span class="term">border-color&#58; #999999;</span><br />
1445 &#160; * &#160;compact in dl, ol and ul - <span class="term">font-size&#58; 85%</span><br />
1446 &#160; * &#160;cellspacing in table - <span class="term">cellspacing="10"</span>&#160;becomes <span class="term">border-spacing&#58; 10px</span><br />
1447 &#160; * &#160;clear in br - E.g., 'clear="all" becomes <span class="term">clear&#58; both</span><br />
1448 &#160; * &#160;height in td and th - E.g., <span class="term">height= "10"</span>&#160;becomes <span class="term">height&#58; 10px</span>&#160;and <span class="term">height="&#42;"</span>&#160;becomes <span class="term">height&#58; auto</span><br />
1449 &#160; * &#160;hspace in img and object - E.g., <span class="term">hspace="10"</span>&#160;becomes <span class="term">margin-left&#58; 10px; margin-right&#58; 10px</span><br />
1450 &#160; * &#160;language in script - <span class="term">language="VBScript"</span>&#160;becomes <span class="term">type="text/vbscript"</span><br />
1451 &#160; * &#160;name in a, form, iframe, img and map - E.g., <span class="term">name="xx"</span>&#160;becomes <span class="term">id="xx"</span><br />
1452 &#160; * &#160;noshade in hr - <span class="term">border-style&#58; none; border&#58; 0; background-color&#58; gray; color&#58; gray</span><br />
1453 &#160; * &#160;nowrap in td and th - <span class="term">white-space&#58; nowrap</span><br />
1454 &#160; * &#160;size in hr - E.g., <span class="term">size="10"</span>&#160;becomes <span class="term">height&#58; 10px</span><br />
1455 &#160; * &#160;vspace in img and object - E.g., <span class="term">vspace="10"</span>&#160;becomes <span class="term">margin-top&#58; 10px; margin-bottom&#58; 10px</span><br />
1456 &#160; * &#160;width in hr, pre, table, td and th - like <span class="term">height</span><br />
1457 <br />
1458 &#160; Example input:<br />
1459 <br />
1460
1461 <code class="code">&#160; &#160; &lt;img src="j.gif" alt="image" name="dad&#39;s" /&gt;&lt;img src="k.gif" alt="image" id="dad_off" name="dad" /&gt;</code>
1462 <br />
1463
1464 <code class="code">&#160; &#160; &lt;br clear="left" /&gt;</code>
1465 <br />
1466
1467 <code class="code">&#160; &#160; &lt;hr noshade size="1" /&gt;</code>
1468 <br />
1469
1470 <code class="code">&#160; &#160; &lt;img name="img" src="i.gif" align="left" alt="image" hspace="10" vspace="10" width="10em" height="20" border="1" style="padding&#58;5px;" /&gt;</code>
1471 <br />
1472
1473 <code class="code">&#160; &#160; &lt;table width="50em" align="center" bgcolor="red"&gt;</code>
1474 <br />
1475
1476 <code class="code">&#160; &#160; &#160;&lt;tr&gt;</code>
1477 <br />
1478
1479 <code class="code">&#160; &#160; &#160; &lt;td width="20%"&gt;</code>
1480 <br />
1481
1482 <code class="code">&#160; &#160; &#160; &#160;&lt;div align="center"&gt;</code>
1483 <br />
1484
1485 <code class="code">&#160; &#160; &#160; &#160; &lt;h3 align="right"&gt;Section&lt;/h3&gt;</code>
1486 <br />
1487
1488 <code class="code">&#160; &#160; &#160; &#160; &lt;p align="right"&gt;Para&lt;/p&gt;</code>
1489 <br />
1490
1491 <code class="code">&#160; &#160; &#160; &#160;&lt;/div&gt;</code>
1492 <br />
1493
1494 <code class="code">&#160; &#160; &#160; &lt;/td&gt;</code>
1495 <br />
1496
1497 <code class="code">&#160; &#160; &#160; &lt;td width="&#42;"&gt;</code>
1498 <br />
1499
1500 <code class="code">&#160; &#160; &#160; &lt;/td&gt;</code>
1501 <br />
1502
1503 <code class="code">&#160; &#160; &#160;&lt;/tr&gt;</code>
1504 <br />
1505
1506 <code class="code">&#160; &#160; &lt;/table&gt;</code>
1507 <br />
1508
1509 <code class="code">&#160; &#160; &lt;br clear="all" /&gt;</code>
1510 <br />
1511 <br />
1512 &#160; And the output with <span class="term">$config["no_deprecated_attr"] = 1</span>:<br />
1513 <br />
1514
1515 <code class="code">&#160; &#160; &lt;img src="j.gif" alt="image" id="dad&#39;s" /&gt;&lt;img src="k.gif" alt="image" id="dad_off" /&gt;</code>
1516 <br />
1517
1518 <code class="code">&#160; &#160; &lt;br style="clear&#58; left;" /&gt;</code>
1519 <br />
1520
1521 <code class="code">&#160; &#160; &lt;hr style="border-style&#58; none; border&#58; 0; background-color&#58; gray; color&#58; gray; size&#58; 1px;" /&gt;</code>
1522 <br />
1523
1524 <code class="code">&#160; &#160; &lt;img src="i.gif" alt="image" width="10em" height="20" style="padding&#58;5px; float&#58; left; margin-left&#58; 10px; margin-right&#58; 10px; margin-top&#58; 10px; margin-bottom&#58; 10px; border&#58; 1px;" id="img" /&gt;</code>
1525 <br />
1526
1527 <code class="code">&#160; &#160; &lt;table width="50em" style="margin&#58; auto; background-color&#58; red;"&gt;</code>
1528 <br />
1529
1530 <code class="code">&#160; &#160; &#160;&lt;tr&gt;</code>
1531 <br />
1532
1533 <code class="code">&#160; &#160; &#160; &lt;td style="width&#58; 20%;"&gt;</code>
1534 <br />
1535
1536 <code class="code">&#160; &#160; &#160; &#160;&lt;div style="margin&#58; auto;"&gt;</code>
1537 <br />
1538
1539 <code class="code">&#160; &#160; &#160; &#160; &lt;h3 style="text-align&#58; right;"&gt;Section&lt;/h3&gt;</code>
1540 <br />
1541
1542 <code class="code">&#160; &#160; &#160; &#160; &lt;p style="text-align&#58; right;"&gt;Para&lt;/p&gt;</code>
1543 <br />
1544
1545 <code class="code">&#160; &#160; &#160; &#160;&lt;/div&gt;</code>
1546 <br />
1547
1548 <code class="code">&#160; &#160; &#160; &lt;/td&gt;</code>
1549 <br />
1550
1551 <code class="code">&#160; &#160; &#160; &lt;td style="width&#58; auto;"&gt;</code>
1552 <br />
1553
1554 <code class="code">&#160; &#160; &#160; &lt;/td&gt;</code>
1555 <br />
1556
1557 <code class="code">&#160; &#160; &#160;&lt;/tr&gt;</code>
1558 <br />
1559
1560 <code class="code">&#160; &#160; &lt;/table&gt;</code>
1561 <br />
1562
1563 <code class="code">&#160; &#160; &lt;br style="clear&#58; both;" /&gt;</code>
1564 <br />
1565 <br />
1566 &#160; For <span class="term">lang</span>, deprecated in XHTML 1.1, transformation is taken care of through <span class="term">$config["xml&#58;lang"]</span>; see <a href="#s3.4.1">section 3.4.1</a>.<br />
1567 <br />
1568 &#160; The attribute <span class="term">name</span>&#160;is deprecated in <span class="term">form</span>, <span class="term">iframe</span>, and <span class="term">img</span>, and is replaced with <span class="term">id</span>&#160;if an <span class="term">id</span>&#160;attribute doesn't exist and if the <span class="term">name</span>&#160;value is appropriate for <span class="term">id</span>&#160;(i.e., doesn't have a non-word character like space). For such replacements for <span class="term">a</span>&#160;and <span class="term">map</span>, for which the <span class="term">name</span>&#160;attribute is deprecated in XHTML 1.1, <span class="term">$config["no_deprecated_attr"]</span>&#160;should be set to <span class="term">2</span>&#160;(when set to <span class="term">1</span>, for these two elements, the <span class="term">name</span>&#160;attribute is retained).<br />
1569
1570 </div>
1571 <div class="sub-sub-section"><h4>
1572 <a name="s3.4.7" id="s3.4.7"></a><span class="item-no">3.4.7</span>&#160; Anti-spam &amp; <span class="term">href</span>
1573 </h4><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1574 <br />
1575 &#160; htmLawed (function <span class="term">hl_tag()</span>) can check the <span class="term">href</span>&#160;attribute values (link addresses) as an anti-spam (email or link spam) measure.<br />
1576 <br />
1577 &#160; If <span class="term">$config["anti_mail_spam"]</span>&#160;is not <span class="term">0</span>, the <span class="term">@</span>&#160;of email addresses in <span class="term">href</span>&#160;values like <span class="term">mailto&#58;a@b.com</span>&#160;is replaced with text specified by <span class="term">$config["anti_mail_spam"]</span>. The text should be of a form that makes it clear to others that the address needs to be edited before a mail is sent; e.g., <span class="term">&lt;remove_this_antispam&gt;@</span>&#160;(makes the example address <span class="term">a&lt;remove_this_antispam&gt;@b.com</span>).<br />
1578 <br />
1579 &#160; For regular links, one can choose to have a <span class="term">rel</span>&#160;attribute with <span class="term">nofollow</span>&#160;in its value (which tells some search engines to not follow a link). This can discourage link spammers. Additionally, or as an alternative, one can choose to empty the <span class="term">href</span>&#160;value altogether (disable the link).<br />
1580 <br />
1581 &#160; For use of these options, <span class="term">$config["anti_link_spam"]</span>&#160;should be set as an array with values <span class="term">regex1</span>&#160;and <span class="term">regex2</span>, both or one of which can be empty (like <span class="term">array("", "regex2")</span>) to indicate that that option is not to be used. Otherwise, <span class="term">regex1</span>&#160;or <span class="term">regex2</span>&#160;should be PHP- and PCRE-compatible regular expression patterns: <span class="term">href</span>&#160;values will be matched against them and those matching the pattern will accordingly be treated.<br />
1582 <br />
1583 &#160; Note that the regular expressions should have <em>delimiters</em>, and be well-formed and preferably fast. Absolute efficiency/accuracy is often not needed.<br />
1584 <br />
1585 &#160; An example, to have a <span class="term">rel</span>&#160;attribute with <span class="term">nofollow</span>&#160;for all links, and to disable links that do not point to domains <span class="term">abc.com</span>&#160;and <span class="term">xyz.org</span>:<br />
1586 <br />
1587
1588 <code class="code">&#160; &#160; $config["anti_link_spam"] = array(&#39;&#96;.&#96;&#39;, &#39;&#96;&#58;//\W&#42;(?!(abc\.com|xyz\.org))&#96;&#39;);</code>
1589 <br />
1590
1591 </div>
1592 <div class="sub-sub-section"><h4>
1593 <a name="s3.4.8" id="s3.4.8"></a><span class="item-no">3.4.8</span>&#160; Inline style properties
1594 </h4><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1595 <br />
1596 &#160; htmLawed can check URL schemes and dynamic expressions (to guard against Javascript, etc., script-based insecurities) in inline CSS style property values in the <span class="term">style</span>&#160;attributes. (CSS properties like <span class="term">background-image</span>&#160;that accept URLs in their values are noted in <a href="#s5.3">section 5.3</a>.) Dynamic CSS expressions that allow scripting in the IE browser, and can be a vulnerability, can be removed from property values by setting <span class="term">$config["css_expression"]</span>&#160;to <span class="term">1</span>&#160;(default setting). Note that when <span class="term">$config["css_expression"]</span>&#160;is set to <span class="term">1</span>, htmLawed will remove <span class="term">/&#42;</span>&#160;from the <span class="term">style</span>&#160;values.<br />
1597 <br />
1598 &#160; <strong>Note</strong>: Because of the various ways of representing characters in attribute values (URL-escapement, entitification, etc.), htmLawed might alter the values of the <span class="term">style</span>&#160;attribute values, and may even falsely identify dynamic CSS expressions and URL schemes in them. If this is an important issue, checking of URLs and dynamic expressions can be turned off (<span class="term">$config["schemes"] = "...style&#58;&#42;..."</span>, see <a href="#s3.4.3">section 3.4.3</a>, and <span class="term">$config["css_expression"] = 0</span>). Alternately, admins can use their own custom function for finer handling of <span class="term">style</span>&#160;values through the <span class="term">hook_tag</span>&#160;parameter (see <a href="#s3.4.9">section 3.4.9</a>).<br />
1599 <br />
1600 &#160; It is also possible to have htmLawed let through any <span class="term">style</span>&#160;value by setting <span class="term">$config["style_pass"]</span>&#160;to <span class="term">1</span>.<br />
1601 <br />
1602 &#160; As such, it is better to set up a CSS file with class declarations, disallow the <span class="term">style</span>&#160;attribute, set a <span class="term">$spec</span>&#160;rule (see <a href="#s2.3">section 2.3</a>) for <span class="term">class</span>&#160;for the <span class="term">oneof</span>&#160;or <span class="term">match</span>&#160;parameter, and ask writers to make use of the <span class="term">class</span>&#160;attribute.<br />
1603
1604 </div>
1605 <div class="sub-sub-section"><h4>
1606 <a name="s3.4.9" id="s3.4.9"></a><span class="item-no">3.4.9</span>&#160; Hook function for tag content
1607 </h4><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1608 <br />
1609 &#160; It is possible to utilize a custom hook function to alter the tag content htmLawed has finalized (i.e., after it has checked/corrected for required attributes, transformed attributes, lower-cased attribute names, etc.).<br />
1610 <br />
1611 &#160; When <span class="term">$config</span>&#160;parameter <span class="term">hook_tag</span>&#160;is set to the name of a function, htmLawed (function <span class="term">hl_tag()</span>) will pass on the element name, and the <em>finalized</em>&#160;attribute name-value pairs as array elements to the function. The function, after completing a task such as filtering or tag transformation, will typically return an empty string, the full opening tag string like <span class="term">&lt;element_name attribute_1_name="attribute_1_value"...&gt;</span>&#160;(for empty elements like <span class="term">img</span>&#160;and <span class="term">input</span>, the element-closing slash <span class="term">/</span>&#160;should also be included), etc.<br />
1612 <br />
1613 &#160; Any <span class="term">hook_tag</span>&#160;function, since htmLawed version 1.1.11, also receives names of elements in closing tags, such as <span class="term">a</span>&#160;in the closing <span class="term">&lt;/a&gt;</span>&#160;tag of the element <span class="term">&lt;a href="http&#58;//cnn.com"&gt;CNN&lt;/a&gt;</span>. No other value is passed to the function since a closing tag contains only element names. Typically, the function will return an empty string or a full closing tag (like <span class="term">&lt;/a&gt;</span>).<br />
1614 <br />
1615 &#160; This is a <strong>powerful functionality</strong>&#160;that can be exploited for various objectives: consolidate-and-convert inline <span class="term">style</span>&#160;attributes to <span class="term">class</span>, convert <span class="term">embed</span>&#160;elements to <span class="term">object</span>, permit only one <span class="term">caption</span>&#160;element in a <span class="term">table</span>&#160;element, disallow embedding of certain types of media, <strong>inject HTML</strong>, use <a href="http://csstidy.sourceforge.net">CSSTidy</a>&#160;to sanitize <span class="term">style</span>&#160;attribute values, etc.<br />
1616 <br />
1617 &#160; As an example, the custom hook code below can be used to force a series of specifically ordered <span class="term">id</span>&#160;attributes on all elements, and a specific <span class="term">param</span>&#160;element inside all <span class="term">object</span>&#160;elements:<br />
1618 <br />
1619
1620 <code class="code">&#160; &#160; function my_tag_function($element, $attribute_array=0){</code>
1621 <br />
1622 <br />
1623
1624 <code class="code">&#160; &#160; &#160; // If second argument is not received, it means a closing tag is being handled</code>
1625 <br />
1626
1627 <code class="code">&#160; &#160; &#160; if(is_numeric($attribute_array)){</code>
1628 <br />
1629
1630 <code class="code">&#160; &#160; &#160; &#160; return "&lt;/$element&gt;";</code>
1631 <br />
1632
1633 <code class="code">&#160; &#160; &#160; }</code>
1634 <br />
1635 <br />
1636
1637 <code class="code">&#160; &#160; &#160; static $id = 0;</code>
1638 <br />
1639
1640 <code class="code">&#160; &#160; &#160; // Remove any duplicate element</code>
1641 <br />
1642
1643 <code class="code">&#160; &#160; &#160; if($element == &#39;param&#39; &amp;&amp; isset($attribute_array[&#39;allowscriptaccess&#39;])){</code>
1644 <br />
1645
1646 <code class="code">&#160; &#160; &#160; &#160; return &#39;&#39;;</code>
1647 <br />
1648
1649 <code class="code">&#160; &#160; &#160; }</code>
1650 <br />
1651 <br />
1652
1653 <code class="code">&#160; &#160; &#160; $new_element = &#39;&#39;;</code>
1654 <br />
1655 <br />
1656
1657 <code class="code">&#160; &#160; &#160; // Force a serialized ID number</code>
1658 <br />
1659
1660 <code class="code">&#160; &#160; &#160; $attribute_array[&#39;id&#39;] = &#39;my_&#39;. $id;</code>
1661 <br />
1662
1663 <code class="code">&#160; &#160; &#160; ++$id;</code>
1664 <br />
1665 <br />
1666
1667 <code class="code">&#160; &#160; &#160; // Inject param for allowscriptaccess</code>
1668 <br />
1669
1670 <code class="code">&#160; &#160; &#160; if($element == &#39;object&#39;){</code>
1671 <br />
1672
1673 <code class="code">&#160; &#160; &#160; &#160; $new_element = &#39;&lt;param id=&#39;my_&#39;. $id; allowscriptaccess="never" /&gt;&#39;;</code>
1674 <br />
1675
1676 <code class="code">&#160; &#160; &#160; &#160; ++$id;</code>
1677 <br />
1678
1679 <code class="code">&#160; &#160; &#160; }</code>
1680 <br />
1681 <br />
1682
1683 <code class="code">&#160; &#160; &#160; $string = &#39;&#39;;</code>
1684 <br />
1685
1686 <code class="code">&#160; &#160; &#160; foreach($attribute_array as $k=&gt;$v){</code>
1687 <br />
1688
1689 <code class="code">&#160; &#160; &#160; &#160; $string .= " {$k}=\"{$v}\"";</code>
1690 <br />
1691
1692 <code class="code">&#160; &#160; &#160; }</code>
1693 <br />
1694 <br />
1695
1696 <code class="code">&#160; &#160; &#160; static $empty_elements = array(&#39;area&#39;=&gt;1, &#39;br&#39;=&gt;1, &#39;col&#39;=&gt;1, &#39;command&#39;=&gt;1, &#39;embed&#39;=&gt;1, &#39;hr&#39;=&gt;1, &#39;img&#39;=&gt;1, &#39;input&#39;=&gt;1, &#39;isindex&#39;=&gt;1, &#39;keygen&#39;=&gt;1, &#39;link&#39;=&gt;1, &#39;meta&#39;=&gt;1, &#39;param&#39;=&gt;1, &#39;source&#39;=&gt;1, &#39;track&#39;=&gt;1, &#39;wbr&#39;=&gt;1);</code>
1697 <br />
1698 <br />
1699
1700 <code class="code">&#160; &#160; &#160; return "&lt;{$element}{$string}". (array_key_exists($element, $empty_elements) ? &#39; /&#39; &#58; &#39;&#39;). &#39;&gt;&#39;. $new_element;</code>
1701 <br />
1702
1703 <code class="code">&#160; &#160; }</code>
1704 <br />
1705 <br />
1706 &#160; The <span class="term">hook_tag</span>&#160;parameter is different from the <span class="term">hook</span>&#160;parameter (<a href="#s3.7">section 3.7</a>).<br />
1707 <br />
1708 &#160; Snippets of hook function code developed by others may be available on the <a href="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed">htmLawed</a>&#160;website.<br />
1709
1710 </div>
1711 <div class="sub-section"><h3>
1712 <a name="s3.5" id="s3.5"></a><span class="item-no">3.5</span>&#160; Simple configuration directive for most valid XHTML
1713 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1714 <br />
1715 &#160; If <span class="term">$config["valid_xhtml"]</span>&#160;is set to <span class="term">1</span>, some relevant <span class="term">$config</span>&#160;parameters (indicated by <span class="term">~</span>&#160;in <a href="#s2.2">section 2.2</a>) are auto-adjusted. This allows one to pass the <span class="term">$config</span>&#160;argument with a simpler value. If a value for a parameter auto-set through <span class="term">valid_xhtml</span>&#160;is still manually provided, then that value will over-ride the auto-set value.<br />
1716
1717 </div>
1718 <div class="sub-section"><h3>
1719 <a name="s3.6" id="s3.6"></a><span class="item-no">3.6</span>&#160; Simple configuration directive for most <em>safe</em>&#160;HTML
1720 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1721 <br />
1722 &#160; <em>Safe</em>&#160;HTML refers to HTML that is restricted to reduce the vulnerability for scripting attacks (such as XSS) based on HTML code which otherwise may still be legal and compliant with the HTML standard specifications. When elements such as <span class="term">script</span>&#160;and <span class="term">object</span>, and attributes such as <span class="term">onmouseover</span>&#160;and <span class="term">style</span>&#160;are allowed in the input text, an input writer can introduce malevolent HTML code. Note that what is considered <span class="term">safe</span>&#160;depends on the nature of the web application and the trust-level accorded to its users.<br />
1723 <br />
1724 &#160; htmLawed allows an admin to use <span class="term">$config["safe"]</span>&#160;to auto-adjust multiple <span class="term">$config</span>&#160;parameters (such as <span class="term">elements</span>&#160;which declares the allowed element-set), which otherwise would have to be manually set. The relevant parameters are indicated by <span class="term">"</span>&#160;in <a href="#s2.2">section 2.2</a>). Thus, one can pass the <span class="term">$config</span>&#160;argument with a simpler value. Having the <span class="term">safe</span>&#160;parameter set to <span class="term">1</span>&#160;is equivalent to setting the following <span class="term">$config</span>&#160;parameters to the noted values :<br />
1725 <br />
1726
1727 <code class="code">&#160; &#160; cdata - 0</code>
1728 <br />
1729
1730 <code class="code">&#160; &#160; comment - 0</code>
1731 <br />
1732
1733 <code class="code">&#160; &#160; deny_attribute - on&#42;</code>
1734 <br />
1735
1736 <code class="code">&#160; &#160; elements - &#42; -applet -audio -canvas -embed -iframe -object -script -video</code>
1737 <br />
1738
1739 <code class="code">&#160; &#160; schemes - href&#58; aim, feed, file, ftp, gopher, http, https, irc, mailto, news, nntp, sftp, ssh, tel, telnet; style&#58; !; &#42;&#58;file, http, https</code>
1740 <br />
1741 <br />
1742 &#160; With <span class="term">safe</span>&#160;set to <span class="term">1</span>, htmLawed considers <span class="term">CDATA</span>&#160;sections and HTML comments as plain text, and prohibits the <span class="term">applet</span>, <span class="term">audio</span>, <span class="term">canvas</span>, <span class="term">embed</span>, <span class="term">iframe</span>, <span class="term">object</span>, <span class="term">script</span>&#160;and <span class="term">video</span>&#160;elements, and the <span class="term">on&#42;</span>&#160;attributes like <span class="term">onclick</span>. ( There are <span class="term">$config</span>&#160;parameters like <span class="term">css_expression</span>&#160;that are not affected by the value set for <span class="term">safe</span>&#160;but whose default values still contribute towards a more <em>safe</em>&#160;output.) Further, unless overridden by the value for parameter <span class="term">schemes</span>&#160;(see <a href="#s3.4.3">section 3.4.3</a>), the schemes <span class="term">app</span>, <span class="term">data</span>&#160;and <span class="term">javascript</span>&#160;are not permitted, and URLs with schemes are neutralized so that, e.g., <span class="term">style="moz-binding&#58;url(http&#58;//danger)"</span>&#160;becomes <span class="term">style="moz-binding&#58;url(denied&#58;http&#58;//danger)"</span>.<br />
1743 <br />
1744 &#160; Admins, however, may still want to completely deny the <span class="term">style</span>&#160;attribute, e.g., with code like<br />
1745 <br />
1746
1747 <code class="code">&#160; &#160; $processed = htmLawed($text, array(&#39;safe&#39;=&gt;1, &#39;deny_attribute&#39;=&gt;&#39;style&#39;));</code>
1748 <br />
1749 <br />
1750 &#160; Permitting the <span class="term">style</span>&#160;attribute brings in risks of <em>click-jacking</em>, etc. CSS property values can render a page non-functional or be used to deface it. Except for URLs, dynamic expressions, and some other things, htmLawed does not completely check <span class="term">style</span>&#160;values. It does provide ways for the code-developer implementing htmLawed to do such checks through the <span class="term">$spec</span>&#160;argument, and through the <span class="term">hook_tag</span>&#160;parameter (see <a href="#s3.4.8">section 3.4.8</a>&#160;for more). Disallowing style completely and relying on CSS classes and stylesheet files is recommended.<br />
1751 <br />
1752 &#160; If a value for a parameter auto-set through <span class="term">safe</span>&#160;is still manually provided, then that value can over-ride the auto-set value. E.g., with <span class="term">$config["safe"] = 1</span>&#160;and <span class="term">$config["elements"] = "&#42; +script"</span>, <span class="term">script</span>, but not <span class="term">applet</span>, is allowed. Such over-ride does not occur for <span class="term">deny_attribute</span>&#160;(for legacy reason) when comma-separated attribute names are provided as the value for this parameter (<a href="#s3.4">section 3.4</a>); instead htmLawed will add <span class="term">on&#42;</span>&#160;to the value provided for <span class="term">deny_attribute</span>.<br />
1753 <br />
1754 &#160; A page illustrating the efficacy of htmLawed's anti-XSS abilities with <span class="term">safe</span>&#160;set to <span class="term">1</span>&#160;against XSS vectors listed by <a href="http://ha.ckers.org/xss.html">RSnake</a>&#160;may be available <a href="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/rsnake/RSnakeXSSTest.htm">here</a>.<br />
1755
1756 </div>
1757 <div class="sub-section"><h3>
1758 <a name="s3.7" id="s3.7"></a><span class="item-no">3.7</span>&#160; Using a hook function
1759 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1760 <br />
1761 &#160; If <span class="term">$config["hook"]</span>&#160;is not set to <span class="term">0</span>, then htmLawed will allow preliminarily processed input to be altered by a hook function named by <span class="term">$config["hook"]</span>&#160;before starting the main work (but after handling of characters, entities, HTML comments and <span class="term">CDATA</span>&#160;sections -- see code for function <span class="term">htmLawed()</span>).<br />
1762 <br />
1763 &#160; The hook function also allows one to alter the <em>finalized</em>&#160;values of <span class="term">$config</span>&#160;and <span class="term">$spec</span>.<br />
1764 <br />
1765 &#160; Note that the <span class="term">hook</span>&#160;parameter is different from the <span class="term">hook_tag</span>&#160;parameter (<a href="#s3.4.9">section 3.4.9</a>).<br />
1766 <br />
1767 &#160; Snippets of hook function code developed by others may be available on the <a href="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed">htmLawed</a>&#160;website.<br />
1768
1769 </div>
1770 <div class="sub-section"><h3>
1771 <a name="s3.8" id="s3.8"></a><span class="item-no">3.8</span>&#160; Obtaining <em>finalized</em>&#160;parameter values
1772 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1773 <br />
1774 &#160; htmLawed can assign the <em>finalized</em>&#160;<span class="term">$config</span>&#160;and <span class="term">$spec</span>&#160;values to a variable named by <span class="term">$config["show_setting"]</span>. The variable, made global by htmLawed, is set as an array with three keys: <span class="term">config</span>, with the <span class="term">$config</span>&#160;value, <span class="term">spec</span>, with the <span class="term">$spec</span>&#160;value, and <span class="term">time</span>, with a value that is the Unix time (the output of PHP's <span class="term">microtime()</span>&#160;function) when the value was assigned. Admins should use a PHP-compliant variable name (e.g., one that does not begin with a numerical digit) that does not conflict with variable names in their non-htmLawed code.<br />
1775 <br />
1776 &#160; The values, which are also post-hook function (if any), can be used to auto-generate information (on, e.g., the elements that are permitted) for input writers.<br />
1777
1778 </div>
1779 <div class="sub-section"><h3>
1780 <a name="s3.9" id="s3.9"></a><span class="item-no">3.9</span>&#160; Retaining non-HTML tags in input with mixed markup
1781 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1782 <br />
1783 &#160; htmLawed does not remove certain characters that, though invalid, are nevertheless <em>discouraged</em>&#160;in HTML documents as per the specifications (see <a href="#s5.1">section 5.1</a>). This can be utilized to deal with input that contains mixed markup. Input that may have HTML markup as well as some other markup that is based on the <span class="term">&lt;</span>, <span class="term">&gt;</span>&#160;and <span class="term">&amp;</span>&#160;characters is considered to have mixed markup. The non-HTML markup can be rather proprietary (like markup for emoticons/smileys), or standard (like MathML or SVG). Or it can be programming code meant for execution/evaluation (such as embedded PHP code).<br />
1784 <br />
1785 &#160; To deal with such mixed markup, the input text can be pre-processed to hide the non-HTML markup by specifically replacing the <span class="term">&lt;</span>, <span class="term">&gt;</span>&#160;and <span class="term">&amp;</span>&#160;characters with some of the HTML-discouraged characters (see <a href="#s3.1.2">section 3.1.2</a>). Post-htmLawed processing, the replacements are reverted.<br />
1786 <br />
1787 &#160; An example (mixed HTML and PHP code in input text):<br />
1788 <br />
1789
1790 <code class="code">&#160; &#160; $text = preg_replace(&#39;&#96;&lt;\?php(.+?)\?&gt;&#96;sm&#39;, "\x83?php\\1?\x84", $text);</code>
1791 <br />
1792
1793 <code class="code">&#160; &#160; $processed = htmLawed($text);</code>
1794 <br />
1795
1796 <code class="code">&#160; &#160; $processed = preg_replace(&#39;&#96;\x83\?php(.+?)\?\x84&#96;sm&#39;, &#39;&lt;?php$1?&gt;&#39;, $processed);</code>
1797 <br />
1798 <br />
1799 &#160; This code will not work if <span class="term">$config["clean_ms_char"]</span>&#160;is set to <span class="term">1</span>&#160;(<a href="#s3.1">section 3.1</a>), in which case one should instead deploy a hook function (<a href="#s3.7">section 3.7</a>). (htmLawed internally uses certain control characters, code-points <span class="term">1</span>&#160;to <span class="term">7</span>, and use of these characters as markers in the logic of hook functions may cause issues.)<br />
1800 <br />
1801 &#160; Admins may also be able to use <span class="term">$config["and_mark"]</span>&#160;to deal with such mixed markup; see <a href="#s3.2">section 3.2</a>.<br />
1802
1803 </div>
1804 </div>
1805 <div class="section"><h2>
1806 <a name="s4" id="s4"></a><span class="item-no">4</span>&#160; Other
1807 </h2><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1808 <div class="sub-section"><h3>
1809 <a name="s4.1" id="s4.1"></a><span class="item-no">4.1</span>&#160; Support
1810 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1811 <br />
1812 &#160; Software updates and forum-based community-support may be found at <a href="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed">http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed</a>. For general PHP issues (not htmLawed-specific), support may be found through internet searches and at <a href="http://php.net">http://php.net</a>.<br />
1813
1814 </div>
1815 <div class="sub-section"><h3>
1816 <a name="s4.2" id="s4.2"></a><span class="item-no">4.2</span>&#160; Known issues
1817 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1818 <br />
1819 &#160; See <a href="#s2.8">section 2.8</a>.<br />
1820
1821 </div>
1822 <div class="sub-section"><h3>
1823 <a name="s4.3" id="s4.3"></a><span class="item-no">4.3</span>&#160; Change-log
1824 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1825 <br />
1826 &#160; (The release date for the downloadable package of files containing documentation, demo script, test-cases, etc., besides the <span class="term">htmLawed.php</span>&#160;file, may be updated without a change-log entry if the secondary files, but not htmLawed per se, are revised.)<br />
1827 <br />
1828 &#160; <em>Version number - Release date. Notes</em><br />
1829 <br />
1830 &#160; 1.2.4.1 - 12 September 2017. Corrects a function re-declaration bug introduced in version 1.2.4<br />
1831 <br />
1832 &#160; 1.2.4 - 31 August 2017. Removes use of PHP <span class="term">create_function</span>&#160;function and <span class="term">$php_errormsg</span>&#160;reserved variable (deprecated in PHP 7.2)<br />
1833 <br />
1834 &#160; 1.2.3 - 5 July 2017. New option value of <span class="term">4</span>&#160;for <span class="term">$config["comments"]</span>&#160;to stop enforcing a space character before the <span class="term">--&gt;</span>&#160;comment-closing marker<br />
1835 <br />
1836 &#160; 1.2.2 - 25 May 2017. Fix for a bug in parsing <span class="term">$spec</span>&#160;that got introduced in version 1.2; also, <span class="term">$spec</span>&#160;is now parsed to accommodate specifications for an HTML element when they are specified in multiple rules<br />
1837 <br />
1838 &#160; 1.2.1.1 - 17 May 2017. Fix for a potential security vulnerability in transformation of deprecated attributes<br />
1839 <br />
1840 &#160; 1.2.1 - 15 May 2017. Fix for a potential security vulnerability in transformation of deprecated attributes<br />
1841 <br />
1842 &#160; 1.2 - 11 February 2017. (First beta release on 26 May 2013). Added support for HTML version 5; ARIA, data-* and microdata attributes; <span class="term">app</span>, <span class="term">data</span>, <span class="term">javascript</span>&#160;and <span class="term">tel</span>&#160;URL schemes (thus, <span class="term">javascript&#58;</span>&#160;is not filtered in default mode). Removed support for code using Kses functions (see <a href="#s2.6">section 2.6</a>). Changes in revisions to the beta releases are not noted here.<br />
1843 <br />
1844 &#160; 1.1.22 - 5 March 2016. Improved testing of attribute value rules specified in <span class="term">$spec</span><br />
1845 <br />
1846 &#160; 1.1.21 - 27 February 2016. Improvement and security fix in transforming <span class="term">font</span>&#160;element<br />
1847 <br />
1848 &#160; 1.1.20 - 9 June 2015. Fix for a potential security vulnerability arising from unescaped double-quote character in single-quoted attribute value of some deprecated elements when tag transformation is enabled; recognition for non-(HTML 4) standard <span class="term">allowfullscreen</span>&#160;attribute of <span class="term">iframe</span><br />
1849 <br />
1850 &#160; 1.1.19 - 19 January 2015. Fix for a bug in cleaning of soft-hyphens in URL values, etc<br />
1851 <br />
1852 &#160; 1.1.18 - 2 August 2014. Fix for a potential security vulnerability arising from specially encoded text with serial opening tags<br />
1853 <br />
1854 &#160; 1.1.17 - 11 March 2014. Removed use of PHP function preg_replace with <span class="term">e</span>&#160;modifier for compatibility with PHP 5.5.<br />
1855 <br />
1856 &#160; 1.1.16 - 29 August 2013. Fix for a potential security vulnerability arising from specialy encoded space characters in URL schemes/protocols<br />
1857 <br />
1858 &#160; 1.1.15 - 11 August 2013. Improved tidying/prettifying functionality<br />
1859 <br />
1860 &#160; 1.1.14 - 8 August 2012. Fix for possible segmental loss of incremental indentation during <span class="term">tidying</span>&#160;when <span class="term">balance</span>&#160;is disabled; fix for non-effectuation under some circumstances of a corrective behavior to preserve plain text within elements like <span class="term">blockquote</span><br />
1861 <br />
1862 &#160; 1.1.13 - 22 July 2012. Added feature allowing use of custom, non-standard attributes or custom rules for standard attributes<br />
1863 <br />
1864 &#160; 1.1.12 - 5 July 2012. Fix for a bug in identifying an unquoted value of the <span class="term">face</span>&#160;attribute<br />
1865 <br />
1866 &#160; 1.1.11 - 5 June 2012. Fix for possible problem with handling of multi-byte characters in attribute values in an mbstring.func_overload enviroment. <span class="term">$config["hook_tag"]</span>, if specified, now receives names of elements in closing tags.<br />
1867 <br />
1868 &#160; 1.1.10 - 22 October 2011. Fix for a bug in the <span class="term">tidy</span>&#160;functionality that caused the entire input to be replaced with a single space; new parameter, <span class="term">$config["direct_list_nest"]</span>&#160;to allow direct descendance of a list in a list. (5 April 2012. Dual licensing from LGPLv3 to LGPLv3 and GPLv2+.)<br />
1869 <br />
1870 &#160; 1.1.9.5 - 6 July 2011. Minor correction of a rule for nesting of <span class="term">li</span>&#160;within <span class="term">dir</span><br />
1871 <br />
1872 &#160; 1.1.9.4 - 3 July 2010. Parameter <span class="term">schemes</span>&#160;now accepts <span class="term">!</span>&#160;so any URL, even a local one, can be <em>denied</em>. An issue in which a second URL value in <span class="term">style</span>&#160;properties was not checked was fixed.<br />
1873 <br />
1874 &#160; 1.1.9.3 - 17 May 2010. Checks for correct nesting of <span class="term">param</span><br />
1875 <br />
1876 &#160; 1.1.9.2 - 26 April 2010. Minor fix regarding rendering of denied URL schemes<br />
1877 <br />
1878 &#160; 1.1.9.1 - 26 February 2010. htmLawed now uses the LGPL version 3 license; support for <span class="term">flashvars</span>&#160;attribute for <span class="term">embed</span><br />
1879 <br />
1880 &#160; 1.1.9 - 22 December 2009. Soft-hyphens are now removed only from URL-accepting attribute values<br />
1881 <br />
1882 &#160; 1.1.8.1 - 16 July 2009. Minor code-change to fix a PHP error notice<br />
1883 <br />
1884 &#160; 1.1.8 - 23 April 2009. Parameter <span class="term">deny_attribute</span>&#160;now accepts the wild-card <span class="term">&#42;</span>, making it simpler to specify its value when all but a few attributes are being denied; fixed a bug in interpreting <span class="term">$spec</span><br />
1885 <br />
1886 &#160; 1.1.7 - 11-12 March 2009. Attributes globally denied through <span class="term">deny_attribute</span>&#160;can be allowed element-specifically through <span class="term">$spec</span>; <span class="term">$config["style_pass"]</span>&#160;allowing letting through any <span class="term">style</span>&#160;value introduced; altered logic to catch certain types of dynamic crafted CSS expressions<br />
1887 <br />
1888 &#160; 1.1.3-6 - 28-31 January - 4 February 2009. Altered logic to catch certain types of dynamic crafted CSS expressions<br />
1889 <br />
1890 &#160; 1.1.2 - 22 January 2009. Fixed bug in parsing of <span class="term">font</span>&#160;attributes during tag transformation<br />
1891 <br />
1892 &#160; 1.1.1 - 27 September 2008. Better nesting correction when omitable closing tags are absent<br />
1893 <br />
1894 &#160; 1.1 - 29 June 2008. <span class="term">$config["hook_tag"]</span>&#160;and <span class="term">$config["tidy"]</span>&#160;introduced for custom tag/attribute check/modification/injection and output compaction/beautification; fixed a regex-in-$spec parsing bug<br />
1895 <br />
1896 &#160; 1.0.9 - 11 June 2008. Fix for a bug in checks for invalid HTML code-point entities<br />
1897 <br />
1898 &#160; 1.0.8 - 15 May 2008. Permit <span class="term">bordercolor</span>&#160;attribute for <span class="term">table</span>, <span class="term">td</span>&#160;and <span class="term">tr</span><br />
1899 <br />
1900 &#160; 1.0.7 - 1 May 2008. Support for <span class="term">wmode</span>&#160;attribute for <span class="term">embed</span>; <span class="term">$config["show_setting"]</span>&#160;introduced; improved <span class="term">$config["elements"]</span>&#160;evaluation<br />
1901 <br />
1902 &#160; 1.0.6 - 20 April 2008. <span class="term">$config["and_mark"]</span>&#160;introduced<br />
1903 <br />
1904 &#160; 1.0.5 - 12 March 2008. <span class="term">style</span>&#160;URL schemes essentially disallowed when $config <span class="term">safe</span>&#160;is on; improved regex for CSS expression search<br />
1905 <br />
1906 &#160; 1.0.4 - 10 March 2008. Improved corrections for <span class="term">blockquote</span>, <span class="term">form</span>, <span class="term">map</span>&#160;and <span class="term">noscript</span><br />
1907 <br />
1908 &#160; 1.0.3 - 3 March 2008. Character entities for soft-hyphens are now replaced with spaces (instead of being removed); fix for a bug allowing <span class="term">td</span>&#160;directly inside <span class="term">table</span>; <span class="term">$config["safe"]</span>&#160;introduced<br />
1909 <br />
1910 &#160; 1.0.2 - 13 February 2008. Improved implementation of <span class="term">$config["keep_bad"]</span><br />
1911 <br />
1912 &#160; 1.0.1 - 7 November 2007. Improved regex for identifying URLs, protocols and dynamic expressions (<span class="term">hl_tag()</span>&#160;and <span class="term">hl_prot()</span>); no error display with <span class="term">hl_regex()</span><br />
1913 <br />
1914 &#160; 1.0 - 2 November 2007. First release<br />
1915
1916 </div>
1917 <div class="sub-section"><h3>
1918 <a name="s4.4" id="s4.4"></a><span class="item-no">4.4</span>&#160; Testing
1919 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1920 <br />
1921 &#160; To test htmLawed using a form interface, a <a href="htmLawedTest.php">demo</a>&#160;web-page is provided with the htmLawed distribution (<span class="term">htmLawed.php</span>&#160;and <span class="term">htmLawedTest.php</span>&#160;should be in the same directory on the web-server). A file with <a href="htmLawed_TESTCASE.txt">test-cases</a>&#160;is also provided.<br />
1922
1923 </div>
1924 <div class="sub-section"><h3>
1925 <a name="s4.5" id="s4.5"></a><span class="item-no">4.5</span>&#160; Upgrade, &amp; old versions
1926 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1927 <br />
1928 &#160; Upgrading is as simple as replacing the previous version of <span class="term">htmLawed.php</span>, assuming the file was not modified for customized features. As htmLawed output is almost always used in static documents, upgrading should not affect old, finalized content.<br />
1929 <br />
1930 &#160; <strong>Note:</strong>&#160;The following upgrades may affect the functionality of a specific htmLawed installation:<br />
1931 <br />
1932 &#160; (1) From version 1.1-1.1.10 to 1.1.11 or later, if a <span class="term">hook_tag</span>&#160;function is in use: In version 1.1.11 and later, elements in closing tags (and not just the opening tags) are also passed to the function. There are no attribute names/values to pass, so a <span class="term">hook_tag</span>&#160;function receives only the element name. The <span class="term">hook_tag</span>&#160;function therefore may have to be edited. See <a href="#s3.4.9">section 3.4.9</a>.<br />
1933 <br />
1934 &#160; (2) From version older than 1.2.beta to later, if htmLawed was used as Kses replacement with Kses code in use: In version 1.2.beta or later, htmLawed no longer provides direct support for code that uses Kses functions (see <a href="#s2.6">section 2.6</a>).<br />
1935 <br />
1936 &#160; (3) From version older than 1.2 to later, if htmLawed is used without <span class="term">$config["safe"]</span>&#160;set to 1: Unlike previous versions, htmLawed version 1.2 and later permit <span class="term">data</span>&#160;and <span class="term">javascript</span>&#160;URL schemes by default (see <a href="#s3.4.3">section 3.4.3</a>).<br />
1937 <br />
1938 &#160; Old versions of htmLawed may be available online. E.g., for version 1.0, check <a href="http://www.bioinformatics.org/phplabware/downloads/htmLawed1.zip">http://www.bioinformatics.org/phplabware/downloads/htmLawed1.zip</a>; for 1.1.1, <a href="http://www.bioinformatics.org/phplabware/downloads/htmLawed111.zip">http://www.bioinformatics.org/phplabware/downloads/htmLawed111.zip</a>; and for 1.1.22, <a href="http://www.bioinformatics.org/phplabware/downloads/htmLawed1122.zip">http://www.bioinformatics.org/phplabware/downloads/htmLawed1122.zip</a>.<br />
1939
1940 </div>
1941 <div class="sub-section"><h3>
1942 <a name="s4.6" id="s4.6"></a><span class="item-no">4.6</span>&#160; Comparison with <span class="term">HTMLPurifier</span>
1943 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1944 <br />
1945 &#160; The HTMLPurifier PHP library by Edward Yang is a very good HTML filtering script that uses object oriented PHP code. Compared to htmLawed, it (as of year 2015):<br />
1946 <br />
1947 &#160; * &#160;does not support PHP versions older than 5.0 (HTMLPurifier dropped PHP 4 support after version 2)<br />
1948 <br />
1949 &#160; * &#160;is 15-20 times bigger (scores of files totalling more than 750 kb)<br />
1950 <br />
1951 &#160; * &#160;consumes 10-15 times more RAM memory (just including the HTMLPurifier files without calling the filter requires a few MBs of memory)<br />
1952 <br />
1953 &#160; * &#160;is expectedly slower<br />
1954 <br />
1955 &#160; * &#160;lacks many of the extra features of htmLawed (like entity conversions and code compaction/beautification)<br />
1956 <br />
1957 &#160; * &#160;has poor documentation<br />
1958 <br />
1959 &#160; However, HTMLPurifier has finer checks for character encodings and attribute values, and can log warnings and errors. Visit the HTMLPurifier <a href="http://htmlpurifier.org">website</a>&#160;for updated information.<br />
1960
1961 </div>
1962 <div class="sub-section"><h3>
1963 <a name="s4.7" id="s4.7"></a><span class="item-no">4.7</span>&#160; Use through application plug-ins/modules
1964 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1965 <br />
1966 &#160; Plug-ins/modules to implement htmLawed in applications such as Drupal may have been developed. Check the application websites and the htmLawed <a href="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed">forum</a>.<br />
1967
1968 </div>
1969 <div class="sub-section"><h3>
1970 <a name="s4.8" id="s4.8"></a><span class="item-no">4.8</span>&#160; Use in non-PHP applications
1971 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1972 <br />
1973 &#160; Non-PHP applications written in Python, Ruby, etc., may be able to use htmLawed through system calls to the PHP engine. Such code may have been documented on the internet. Also check the forum on the htmLawed <a href="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed">site</a>.<br />
1974
1975 </div>
1976 <div class="sub-section"><h3>
1977 <a name="s4.9" id="s4.9"></a><span class="item-no">4.9</span>&#160; Donate
1978 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1979 <br />
1980 &#160; A donation in any currency and amount to appreciate or support this software can be sent by <a href="http://paypal.com">PayPal</a>&#160;to this email address: drpatnaik at yahoo dot com.<br />
1981
1982 </div>
1983 <div class="sub-section"><h3>
1984 <a name="s4.10" id="s4.10"></a><span class="item-no">4.10</span>&#160; Acknowledgements
1985 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1986 <br />
1987 &#160; Nicholas Alipaz, Bryan Blakey, Pádraic Brady, Dac Chartrand, Alexandre Chouinard, Ulf Harnhammer, Gareth Heyes, Hakre, Klaus Leithoff, Lukasz Pilorz, Shelley Powers, Psych0tr1a, Lincoln Russell, Tomas Sykorka, Harro Verton, Edward Yang, and many anonymous users.<br />
1988 <br />
1989 &#160; Thank you!<br />
1990
1991 </div>
1992 </div>
1993 <div class="section"><h2>
1994 <a name="s5" id="s5"></a><span class="item-no">5</span>&#160; Appendices
1995 </h2><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1996 <div class="sub-section"><h3>
1997 <a name="s5.1" id="s5.1"></a><span class="item-no">5.1</span>&#160; Characters discouraged in XHTML
1998 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
1999 <br />
2000 &#160; Characters represented by the following hexadecimal code-points are <em>not</em>&#160;invalid, even though some validators may issue messages stating otherwise.<br />
2001 <br />
2002 &#160; <span class="term">7f</span>&#160;to <span class="term">84</span>, <span class="term">86</span>&#160;to <span class="term">9f</span>, <span class="term">fdd0</span>&#160;to <span class="term">fddf</span>, <span class="term">1fffe</span>, <span class="term">1ffff</span>, <span class="term">2fffe</span>, <span class="term">2ffff</span>, <span class="term">3fffe</span>, <span class="term">3ffff</span>, <span class="term">4fffe</span>, <span class="term">4ffff</span>, <span class="term">5fffe</span>, <span class="term">5ffff</span>, <span class="term">6fffe</span>, <span class="term">6ffff</span>, <span class="term">7fffe</span>, <span class="term">7ffff</span>, <span class="term">8fffe</span>, <span class="term">8ffff</span>, <span class="term">9fffe</span>, <span class="term">9ffff</span>, <span class="term">afffe</span>, <span class="term">affff</span>, <span class="term">bfffe</span>, <span class="term">bffff</span>, <span class="term">cfffe</span>, <span class="term">cffff</span>, <span class="term">dfffe</span>, <span class="term">dffff</span>, <span class="term">efffe</span>, <span class="term">effff</span>, <span class="term">ffffe</span>, <span class="term">fffff</span>, <span class="term">10fffe</span>&#160;and <span class="term">10ffff</span><br />
2003
2004 </div>
2005 <div class="sub-section"><h3>
2006 <a name="s5.2" id="s5.2"></a><span class="item-no">5.2</span>&#160; Valid attribute-element combinations
2007 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
2008 <br />
2009 &#160; * &#160;includes deprecated attributes (marked <span class="term">^</span>), attributes for microdata (marked <span class="term">&#42;</span>), the non-standard <span class="term">bordercolor</span>, and new-in-HTML5 attributes (marked <span class="term">~</span>); can have multiple comma-separated values (marked <span class="term">%</span>); can have multiple space-separated values (marked <span class="term">$</span>)<br />
2010 &#160; * &#160;only non-frameset, HTML body elements<br />
2011 &#160; * &#160;<span class="term">name</span>&#160;for <span class="term">a</span>&#160;and <span class="term">map</span>, and <span class="term">lang</span>&#160;are invalid in XHTML 1.1<br />
2012 &#160; * &#160;<span class="term">target</span>&#160;is valid for <span class="term">a</span>&#160;in XHTML 1.1 and higher<br />
2013 &#160; * &#160;<span class="term">xml&#58;space</span>&#160;is only for XHTML 1.1<br />
2014 <br />
2015 &#160; abbr - td, th<br />
2016 &#160; accept - form, input<br />
2017 &#160; accept-charset - form<br />
2018 &#160; action - form<br />
2019 &#160; align - applet, caption^, col, colgroup, div^, embed, h1^, h2^, h3^, h4^, h5^, h6^, hr^, iframe, img^, input^, legend^, object^, p^, table^, tbody, td, tfoot, th, thead, tr<br />
2020 &#160; allowfullscreen - iframe<br />
2021 &#160; alt - applet, area, img, input<br />
2022 &#160; archive - applet, object<br />
2023 &#160; async~ - script<br />
2024 &#160; autocomplete~ - input<br />
2025 &#160; autofocus~ - button, input, keygen, select, textarea<br />
2026 &#160; autoplay~ - audio, video<br />
2027 &#160; axis - td, th<br />
2028 &#160; bgcolor - embed, table^, td^, th^, tr^<br />
2029 &#160; border - img, object^, table<br />
2030 &#160; bordercolor - table, td, tr<br />
2031 &#160; cellpadding - table<br />
2032 &#160; cellspacing - table<br />
2033 &#160; challenge~ - keygen<br />
2034 &#160; char - col, colgroup, tbody, td, tfoot, th, thead, tr<br />
2035 &#160; charoff - col, colgroup, tbody, td, tfoot, th, thead, tr<br />
2036 &#160; charset - a, script<br />
2037 &#160; checked - command, input<br />
2038 &#160; cite - blockquote, del, ins, q<br />
2039 &#160; classid - object<br />
2040 &#160; clear - br^<br />
2041 &#160; code - applet<br />
2042 &#160; codebase - object, applet<br />
2043 &#160; codetype - object<br />
2044 &#160; color - font<br />
2045 &#160; cols - textarea<br />
2046 &#160; colspan - td, th<br />
2047 &#160; compact - dir, dl^, menu, ol^, ul^<br />
2048 &#160; content - meta<br />
2049 &#160; controls~ - audio, video<br />
2050 &#160; coords - area, a<br />
2051 &#160; crossorigin~ - img<br />
2052 &#160; data - object<br />
2053 &#160; datetime - del, ins, time<br />
2054 &#160; declare - object<br />
2055 &#160; default~ - track<br />
2056 &#160; defer - script<br />
2057 &#160; dir - bdo<br />
2058 &#160; dirname~ - input, textarea<br />
2059 &#160; disabled - button, command, fieldset, input, keygen, optgroup, option, select, textarea<br />
2060 &#160; download~ - a<br />
2061 &#160; enctype - form<br />
2062 &#160; face - font<br />
2063 &#160; flashvars** - embed<br />
2064 &#160; for - label, output<br />
2065 &#160; form~ - button, fieldset, input, keygen, label, object, output, select, textarea<br />
2066 &#160; formaction~ - button, input<br />
2067 &#160; formenctype~ - button, input<br />
2068 &#160; formmethod~ - button, input<br />
2069 &#160; formnovalidate~ - button, input<br />
2070 &#160; formtarget~ - button, input<br />
2071 &#160; frame - table<br />
2072 &#160; frameborder - iframe<br />
2073 &#160; headers - td, th<br />
2074 &#160; height - applet, canvas, embed, iframe, img, input, object, td^, th^, video<br />
2075 &#160; high~ - meter<br />
2076 &#160; href - a, area, link<br />
2077 &#160; hreflang - a, area, link<br />
2078 &#160; hspace - applet, embed, img^, object^<br />
2079 &#160; icon~ - command<br />
2080 &#160; ismap - img, input<br />
2081 &#160; keytype~ - keygen<br />
2082 &#160; keyparams~ - keygen<br />
2083 &#160; kind~ - track<br />
2084 &#160; label - command, menu, option, optgroup, track<br />
2085 &#160; language - script^<br />
2086 &#160; list~ - input<br />
2087 &#160; longdesc - img, iframe<br />
2088 &#160; loop~ - audio, video<br />
2089 &#160; low~ - meter<br />
2090 &#160; marginheight - iframe<br />
2091 &#160; marginwidth - iframe<br />
2092 &#160; max~ - input, meter, progress<br />
2093 &#160; maxlength - input, textarea<br />
2094 &#160; media~ - a, area, link, source, style<br />
2095 &#160; mediagroup~ - audio, video<br />
2096 &#160; method - form<br />
2097 &#160; min~ - input, meter<br />
2098 &#160; model** - embed<br />
2099 &#160; multiple - input, select<br />
2100 &#160; muted~ - audio, video<br />
2101 &#160; name - a^, applet^, button, embed, fieldset, form^, iframe^, img^, input, keygen, map^, object, output, param, select, textarea<br />
2102 &#160; nohref - area<br />
2103 &#160; noshade - hr^<br />
2104 &#160; novalidate~ - form<br />
2105 &#160; nowrap - td^, th^<br />
2106 &#160; object - applet<br />
2107 &#160; open~ - details<br />
2108 &#160; optimum~ - meter<br />
2109 &#160; pattern~ - input<br />
2110 &#160; ping~ - a, area<br />
2111 &#160; placeholder~ - input, textarea<br />
2112 &#160; pluginspage** - embed<br />
2113 &#160; pluginurl** - embed<br />
2114 &#160; poster~ - video<br />
2115 &#160; pqg~ - keygen<br />
2116 &#160; preload~ - audio, video<br />
2117 &#160; prompt - isindex<br />
2118 &#160; pubdate~ - time<br />
2119 &#160; radiogroup* - command<br />
2120 &#160; readonly - input, textarea<br />
2121 &#160; required~ - input, select, textarea<br />
2122 &#160; rel$ - a, area, link<br />
2123 &#160; rev - a<br />
2124 &#160; reversed~ - old<br />
2125 &#160; rows - textarea<br />
2126 &#160; rowspan - td, th<br />
2127 &#160; rules - table<br />
2128 &#160; sandbox~ - iframe<br />
2129 &#160; scope - td, th<br />
2130 &#160; scoped~ - style<br />
2131 &#160; scrolling - iframe<br />
2132 &#160; seamless~ - iframe<br />
2133 &#160; selected - option<br />
2134 &#160; shape - area, a<br />
2135 &#160; size - font, hr^, input, select<br />
2136 &#160; sizes~ - link<br />
2137 &#160; span - col, colgroup<br />
2138 &#160; src - audio, embed, iframe, img, input, script, source, track, video<br />
2139 &#160; srcdoc~ - iframe<br />
2140 &#160; srclang~ - track<br />
2141 &#160; srcset~% - img<br />
2142 &#160; standby - object<br />
2143 &#160; start - ol<br />
2144 &#160; step~ - input<br />
2145 &#160; summary - table<br />
2146 &#160; target - a, area, form<br />
2147 &#160; type - a, area, button, command, embed, input, li, link, menu, object, ol, param, script, source, style, ul<br />
2148 &#160; typemustmatch~ - object<br />
2149 &#160; usemap - img, input, object<br />
2150 &#160; valign - col, colgroup, tbody, td, tfoot, th, thead, tr<br />
2151 &#160; value - button, data, input, li, meter, option, param, progress<br />
2152 &#160; valuetype - param<br />
2153 &#160; vspace - applet, embed, img^, object^<br />
2154 &#160; width - applet, canvas, col, colgroup, embed, hr^, iframe, img, input, object, pre^, table, td^, th^, video<br />
2155 &#160; wmode - embed<br />
2156 &#160; wrap~ - textarea<br />
2157 <br />
2158 &#160; The following attributes, including event-specific ones and attributes of ARIA and microdata specifications, are considered global and allowed in all elements:<br />
2159 <br />
2160 &#160; accesskey, aria-activedescendant, aria-atomic, aria-autocomplete, aria-busy, aria-checked, aria-controls, aria-describedby, aria-disabled, aria-dropeffect, aria-expanded, aria-flowto, aria-grabbed, aria-haspopup, aria-hidden, aria-invalid, aria-label, aria-labelledby, aria-level, aria-live, aria-multiline, aria-multiselectable, aria-orientation, aria-owns, aria-posinset, aria-pressed, aria-readonly, aria-relevant, aria-required, aria-selected, aria-setsize, aria-sort, aria-valuemax, aria-valuemin, aria-valuenow, aria-valuetext, class$, contenteditable, contextmenu, dir, draggable, dropzone, hidden, id, inert, itemid, itemprop, itemref, itemscope, itemtype, lang, onabort, onblur, oncanplay, oncanplaythrough, onchange, onclick, oncontextmenu, oncopy, oncuechange, oncut, ondblclick, ondrag, ondragend, ondragenter, ondragleave, ondragover, ondragstart, ondrop, ondurationchange, onemptied, onended, onerror, onfocus, onformchange, onforminput, oninput, oninvalid, onkeydown, onkeypress, onkeyup, onload, onloadeddata, onloadedmetadata, onloadstart, onlostpointercapture, onmousedown, onmousemove, onmouseout, onmouseover, onmouseup, onmousewheel, onpaste, onpause, onplay, onplaying, onpointercancel, ongotpointercapture, onpointerdown, onpointerenter, onpointerleave, onpointermove, onpointerout, onpointerover, onpointerup, onprogress, onratechange, onreadystatechange, onreset, onsearch, onscroll, onseeked, onseeking, onselect, onshow, onstalled, onsubmit, onsuspend, ontimeupdate, ontoggle, ontouchcancel, ontouchend, ontouchmove, ontouchstart, onvolumechange, onwaiting, onwheel, role, spellcheck, style, tabindex, title, translate, xmlns, xml:base, xml:lang, xml:space<br />
2161 <br />
2162 &#160; Custom <em>data-*</em>&#160;attributes, where the first three characters of the value of <em>star</em>&#160;(*) after lower-casing do not equal <span class="term">xml</span>&#160;and the value of <em>star</em>&#160;does not have a colon (:), equal-to (=), newline, solidus (/), space, tab, or any A-Z character, are also considered global and allowed in all elements.<br />
2163
2164 </div>
2165 <div class="sub-section"><h3>
2166 <a name="s5.3" id="s5.3"></a><span class="item-no">5.3</span>&#160; CSS 2.1 properties accepting URLs
2167 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
2168 <br />
2169 &#160; background<br />
2170 &#160; background-image<br />
2171 &#160; content<br />
2172 &#160; cue-after<br />
2173 &#160; cue-before<br />
2174 &#160; cursor<br />
2175 &#160; list-style<br />
2176 &#160; list-style-image<br />
2177 &#160; play-during<br />
2178
2179 </div>
2180 <div class="sub-section"><h3>
2181 <a name="s5.4" id="s5.4"></a><span class="item-no">5.4</span>&#160; Microsoft Windows 1252 character replacements
2182 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
2183 <br />
2184 &#160; Key: <span class="term">d</span>&#160;double, <span class="term">l</span>&#160;left, <span class="term">q</span>&#160;quote, <span class="term">r</span>&#160;right, <span class="term">s.</span>&#160;single<br />
2185 <br />
2186 &#160; Code-point (decimal) - hexadecimal value - replacement entity - represented character<br />
2187 <br />
2188 &#160; 127 - 7f - (removed) - (not used)<br />
2189 &#160; 128 - 80 - &amp;#8364; - euro<br />
2190 &#160; 129 - 81 - (removed) - (not used)<br />
2191 &#160; 130 - 82 - &amp;#8218; - baseline s. q<br />
2192 &#160; 131 - 83 - &amp;#402; - florin<br />
2193 &#160; 132 - 84 - &amp;#8222; - baseline d q<br />
2194 &#160; 133 - 85 - &amp;#8230; - ellipsis<br />
2195 &#160; 134 - 86 - &amp;#8224; - dagger<br />
2196 &#160; 135 - 87 - &amp;#8225; - d dagger<br />
2197 &#160; 136 - 88 - &amp;#710; - circumflex accent<br />
2198 &#160; 137 - 89 - &amp;#8240; - permile<br />
2199 &#160; 138 - 8a - &amp;#352; - S Hacek<br />
2200 &#160; 139 - 8b - &amp;#8249; - l s. guillemet<br />
2201 &#160; 140 - 8c - &amp;#338; - OE ligature<br />
2202 &#160; 141 - 8d - (removed) - (not used)<br />
2203 &#160; 142 - 8e - &amp;#381; - Z dieresis<br />
2204 &#160; 143 - 8f - (removed) - (not used)<br />
2205 &#160; 144 - 90 - (removed) - (not used)<br />
2206 &#160; 145 - 91 - &amp;#8216; - l s. q<br />
2207 &#160; 146 - 92 - &amp;#8217; - r s. q<br />
2208 &#160; 147 - 93 - &amp;#8220; - l d q<br />
2209 &#160; 148 - 94 - &amp;#8221; - r d q<br />
2210 &#160; 149 - 95 - &amp;#8226; - bullet<br />
2211 &#160; 150 - 96 - &amp;#8211; - en dash<br />
2212 &#160; 151 - 97 - &amp;#8212; - em dash<br />
2213 &#160; 152 - 98 - &amp;#732; - tilde accent<br />
2214 &#160; 153 - 99 - &amp;#8482; - trademark<br />
2215 &#160; 154 - 9a - &amp;#353; - s Hacek<br />
2216 &#160; 155 - 9b - &amp;#8250; - r s. guillemet<br />
2217 &#160; 156 - 9c - &amp;#339; - oe ligature<br />
2218 &#160; 157 - 9d - (removed) - (not used)<br />
2219 &#160; 158 - 9e - &amp;#382; - z dieresis<br />
2220 &#160; 159 - 9f - &amp;#376; - Y dieresis<br />
2221
2222 </div>
2223 <div class="sub-section"><h3>
2224 <a name="s5.5" id="s5.5"></a><span class="item-no">5.5</span>&#160; URL format
2225 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
2226 <br />
2227 &#160; An <em>absolute</em>&#160;URL has a <span class="term">protocol</span>&#160;or <span class="term">scheme</span>, a <span class="term">network location</span>&#160;or <span class="term">hostname</span>, and, optional <span class="term">path</span>, <span class="term">parameters</span>, <span class="term">query</span>&#160;and <span class="term">fragment</span>&#160;segments. Thus, an absolute URL has this generic structure:<br />
2228 <br />
2229
2230 <code class="code">&#160; &#160; (scheme) &#58; (//network location) /(path) ;(parameters) ?(query) #(fragment)</code>
2231 <br />
2232 <br />
2233 &#160; The schemes can only contain letters, digits, <span class="term">+</span>, <span class="term">.</span>&#160;and <span class="term">-</span>. Hostname is the portion after the <span class="term">//</span>&#160;and up to the first <span class="term">/</span>&#160;(if any; else, up to the end) when <span class="term">&#58;</span>&#160;is followed by a <span class="term">//</span>&#160;(e.g., <span class="term">abc.com</span>&#160;in <span class="term">ftp&#58;//abc.com/def</span>); otherwise, it consists of everything after the <span class="term">&#58;</span>&#160;(e.g., <span class="term">def@abc.com</span>&#160;in mailto:def@abc.com').<br />
2234 <br />
2235 &#160; <em>Relative</em>&#160;URLs do not have explicit schemes and network locations; such values are inherited from a <em>base</em>&#160;URL.<br />
2236
2237 </div>
2238 <div class="sub-section"><h3>
2239 <a name="s5.6" id="s5.6"></a><span class="item-no">5.6</span>&#160; Brief on htmLawed code
2240 </h3><span class="totop"><a href="#peak">(to top)</a></span><br style="clear: both;" />
2241 <br />
2242 &#160; Much of the code's logic and reasoning can be understood from the documentation above.<br />
2243 <br />
2244 &#160; The <strong>output</strong>&#160;of htmLawed is a text string containing the processed input. There is no custom error tracking.<br />
2245 <br />
2246 &#160; <strong>Function arguments</strong>&#160;for htmLawed are:<br />
2247 <br />
2248 &#160; * &#160;<span class="term">$in</span>&#160;- first argument; a text string; the <strong>input text</strong>&#160;to be processed. Any extraneous slashes added by PHP when <em>magic quotes</em>&#160;are enabled should be removed beforehand using PHP's <span class="term">stripslashes()</span>&#160;function.<br />
2249 <br />
2250 &#160; * &#160;<span class="term">$config</span>&#160;- second argument; an associative array; optional; named <span class="term">$C</span>&#160;within htmLawed code. The array has keys with names like <span class="term">balance</span>&#160;and <span class="term">keep_bad</span>, and the values, which can be boolean, string, or array, depending on the key, are read to accordingly set the <strong>configurable parameters</strong>&#160;(indicated by the keys). All configurable parameters receive some default value if the value to be used is not specified by the user through <span class="term">$config</span>. <em>Finalized</em>&#160;<span class="term">$config</span>&#160;is thus a filtered and possibly larger array.<br />
2251 <br />
2252 &#160; * &#160;<span class="term">$spec</span>&#160;- third argument; a text string; optional. The string has rules, written in an htmLawed-designated format, <strong>specifying</strong>&#160;element-specific attribute and attribute value restrictions. Function <span class="term">hl_spec()</span>&#160;is used to convert the string to an associative-array, named <span class="term">$S</span>&#160;within htmLawed code, for internal use. <em>Finalized</em>&#160;<span class="term">$spec</span>&#160;is thus an array.<br />
2253 <br />
2254 &#160; <em>Finalized</em>&#160;<span class="term">$config</span>&#160;and <span class="term">$spec</span>&#160;are made <strong>global variables</strong>&#160;while htmLawed is at work. Values of any pre-existing global variables with same names are noted, and their values are restored after htmLawed finishes processing the input (to capture the <em>finalized</em>&#160;values, the <span class="term">show_settings</span>&#160;parameter of <span class="term">$config</span>&#160;should be used). Depending on <span class="term">$config</span>, another global variable <span class="term">hl_Ids</span>, to track <span class="term">id</span>&#160;attribute values for uniqueness, may be set. Unlike the other two variables, this one is not reset (or unset) post-processing.<br />
2255 <br />
2256 &#160; Except for the main <span class="term">htmLawed()</span>&#160;function, htmLawed's functions are <strong>name-spaced</strong>&#160;using the <span class="term">hl_</span>&#160;prefix. The <strong>functions</strong>&#160;and their roles are:<br />
2257 <br />
2258 &#160; * &#160;<span class="term">hl_attrval</span>&#160;- check attribute values against <span class="term">$spec</span><br />
2259 &#160; * &#160;<span class="term">hl_bal</span>&#160;- balance tags and ensure proper nesting<br />
2260 &#160; * &#160;<span class="term">hl_cmtcd</span>&#160;- handle CDATA sections and HTML comments<br />
2261 &#160; * &#160;<span class="term">hl_ent</span>&#160;- handle character entities<br />
2262 &#160; * &#160;<span class="term">hl_prot</span>&#160;- check a URL scheme/protocol<br />
2263 &#160; * &#160;<span class="term">hl_regex</span>&#160;- check syntax of a regular expression<br />
2264 &#160; * &#160;<span class="term">hl_spec</span>&#160;- convert user-supplied <span class="term">$spec</span>&#160;value to one used internally<br />
2265 &#160; * &#160;<span class="term">hl_tag</span>&#160;- handle element tags and attributes<br />
2266 &#160; * &#160;<span class="term">hl_tag2</span>&#160;- transform element tags<br />
2267 &#160; * &#160;<span class="term">hl_tidy</span>&#160;- compact/beautify HTML<br />
2268 &#160; * &#160;<span class="term">hl_version</span>&#160;- report htmLawed version<br />
2269 &#160; * &#160;<span class="term">htmLawed</span>&#160;- main function<br />
2270 <br />
2271 &#160; <span class="term">htmLawed()</span>&#160;finalizes <span class="term">$spec</span>&#160;(with the help of <span class="term">hl_spec()</span>) and <span class="term">$config</span>, and globalizes them. Finalization of <span class="term">$config</span>&#160;involves setting default values if an inappropriate or invalid one is supplied. This includes calling <span class="term">hl_regex()</span>&#160;to check well-formedness of regular expression patterns if such expressions are user-supplied through <span class="term">$config</span>. <span class="term">htmLawed()</span>&#160;then removes invalid characters like nulls and <span class="term">x01</span>&#160;and appropriately handles entities using <span class="term">hl_ent()</span>. HTML comments and CDATA sections are identified and treated as per <span class="term">$config</span>&#160;with the help of <span class="term">hl_cmtcd()</span>. When retained, the <span class="term">&lt;</span>&#160;and <span class="term">&gt;</span>&#160;characters identifying them, and the <span class="term">&lt;</span>, <span class="term">&gt;</span>&#160;and <span class="term">&amp;</span>&#160;characters inside them, are replaced with control characters (code-points <span class="term">1</span>&#160;to <span class="term">5</span>) till any tag balancing is completed.<br />
2272 <br />
2273 &#160; After this <em>initial processing</em>&#160;<span class="term">htmLawed()</span>&#160;identifies tags using regex and processes them with the help of <span class="term">hl_tag()</span>&#160;-- &#160;a large function that analyzes tag content, filtering it as per HTML standards, <span class="term">$config</span>&#160;and <span class="term">$spec</span>. Among other things, <span class="term">hl_tag()</span>&#160;transforms deprecated elements using <span class="term">hl_tag2()</span>, removes attributes from closing tags, checks attribute values as per <span class="term">$spec</span>&#160;rules using <span class="term">hl_attrval()</span>, and checks URL protocols using <span class="term">hl_prot()</span>. <span class="term">htmLawed()</span>&#160;performs tag balancing and nesting checks with a call to <span class="term">hl_bal()</span>, and optionally compacts/beautifies the output with proper white-spacing with a call to <span class="term">hl_tidy()</span>. The latter temporarily replaces white-space, and <span class="term">&lt;</span>, <span class="term">&gt;</span>&#160;and <span class="term">&amp;</span>&#160;characters inside <span class="term">pre</span>, <span class="term">script</span>&#160;and <span class="term">textarea</span>&#160;elements, and HTML comments and CDATA sections with control characters (code-points <span class="term">1</span>&#160;to <span class="term">5</span>, and <span class="term">7</span>).<br />
2274 <br />
2275 &#160; htmLawed permits the use of custom code or <strong>hook functions</strong>&#160;at two stages. The first, called inside <span class="term">htmLawed()</span>, allows the input text as well as the finalized <span class="term">$config</span>&#160;and <span class="term">$spec</span>&#160;values to be altered right after the initial processing (see <a href="#s3.7">section 3.7</a>). The second is called by <span class="term">hl_tag()</span>&#160;once the tag content is finalized (see <a href="#s3.4.9">section 3.4.9</a>).<br />
2276 <br />
2277 &#160; The functionality of htmLawed is dictated by the external HTML standards. The code of htmLawed is thus written for a clear-cut aim, with not much concern for tweaking by other developers. The code is only minimally annotated with comments -- it is not meant to instruct. PHP developers familiar with the HTML specifications will see the logic, and others can always refer to the htmLawed documentation.
2278 </div>
2279 </div>
2280 <br />
2281 <hr /><br /><br /><span class="subtle"><small>HTM version of <em><a href="htmLawed_README.txt">htmLawed_README.txt</a></em> generated on 12 Sep, 2017 using <a href="http://www.bioinformatics.org/phplabware/internal_utilities">rTxt2htm</a> from PHP Labware</small></span>
2282 </div><!-- ended div body -->
2283 </div><!-- ended div top -->
2284 </body>
2285 </html>