<metaname="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"/>
 <spanclass="toc-item"><ahref="#s2.2"><spanclass="item-no">2.2</span>  Configuring htmLawed using the <spanclass="term">$config</span> parameter</a></span><br/>
 <spanclass="toc-item"><ahref="#s2.3"><spanclass="item-no">2.3</span>  Extra HTML specifications using the <spanclass="term">$spec</span> parameter</a></span><br/>
 <spanclass="toc-item"><ahref="#s2.4"><spanclass="item-no">2.4</span>  Performance time & memory usage</a></span><br/>
 <spanclass="toc-item"><ahref="#s2.5"><spanclass="item-no">2.5</span>  Some security risks to keep in mind</a></span><br/>
 <spanclass="toc-item"><ahref="#s2.6"><spanclass="item-no">2.6</span>  Use without modifying old <spanclass="term">kses()</span> code</a></span><br/>
 <spanclass="toc-item"><ahref="#s2.7"><spanclass="item-no">2.7</span>  Tolerance for ill-written HTML</a></span><br/>
 <spanclass="toc-item"><ahref="#s2.8"><spanclass="item-no">2.8</span>  Limitations & work-arounds</a></span><br/>
 <spanclass="toc-item"><ahref="#s2.9"><spanclass="item-no">2.9</span>  Examples of usage</a></span><br/>
 <spanclass="toc-item"><ahref="#s3.2"><spanclass="item-no">3.2</span>  Character references/entities</a></span><br/>
 <spanclass="toc-item"><ahref="#s3.3"><spanclass="item-no">3.3</span>  HTML elements</a></span><br/>
  <spanclass="toc-item"><ahref="#s3.3.1"><spanclass="item-no">3.3.1</span>  HTML comments and <spanclass="term">CDATA</span> sections</a></span><br/>
  <spanclass="toc-item"><ahref="#s3.3.2"><spanclass="item-no">3.3.2</span>  Tag-transformation for better XHTML-Strict</a></span><br/>
  <spanclass="toc-item"><ahref="#s3.3.3"><spanclass="item-no">3.3.3</span>  Tag balancing and proper nesting</a></span><br/>
  <spanclass="toc-item"><ahref="#s3.3.4"><spanclass="item-no">3.3.4</span>  Elements requiring child elements</a></span><br/>
  <spanclass="toc-item"><ahref="#s3.3.5"><spanclass="item-no">3.3.5</span>  Beautify or compact HTML</a></span><br/>
  <spanclass="toc-item"><ahref="#s3.4.3"><spanclass="item-no">3.4.3</span>  URL schemes (protocols) and scripts in attribute values</a></span><br/>
  <spanclass="toc-item"><ahref="#s3.4.4"><spanclass="item-no">3.4.4</span>  Absolute & relative URLs</a></span><br/>
  <spanclass="toc-item"><ahref="#s3.4.5"><spanclass="item-no">3.4.5</span>  Lower-cased, standard attribute values</a></span><br/>
  <spanclass="toc-item"><ahref="#s3.4.6"><spanclass="item-no">3.4.6</span>  Transformation of deprecated attributes</a></span><br/>
  <spanclass="toc-item"><ahref="#s3.4.9"><spanclass="item-no">3.4.9</span>  Hook function for tag content</a></span><br/>
 <spanclass="toc-item"><ahref="#s3.5"><spanclass="item-no">3.5</span>  Simple configuration directive for most valid XHTML</a></span><br/>
 <spanclass="toc-item"><ahref="#s3.6"><spanclass="item-no">3.6</span>  Simple configuration directive for most <em>safe</em> HTML</a></span><br/>
 <spanclass="toc-item"><ahref="#s3.7"><spanclass="item-no">3.7</span>  Using a hook function</a></span><br/>
  htmLawed is a highly customizable single-file PHP script to make text secure, and standard- and admin policy-compliant for use in the body of HTML 4, XHTML 1 or 1.1, or generic XML documents. It is thus a configurable input (X)HTML filter, processor, purifier, sanitizer, beautifier, etc., and an alternative to the <ahref="http://tidy.sourceforge.net">HTMLTidy</a> application.<br/>
<br/>
  The <em>lawing in</em> of input text is needed to ensure that HTML code in the text is standard-compliant, does not introduce security vulnerabilities, and does not break the aesthetics, design or layout of web-pages. htmLawed tries to do this by, for example, making HTML well-formed with balanced and properly nested tags, neutralizing code that may be used for cross-site scripting (<spanclass="term">XSS</span>) attacks, and allowing only specified HTML elements/tags and attributes.<br/>
<divclass="sub-section"><h3>
<aname="s1.1"id="s1.1"></a><spanclass="item-no">1.1</span>  Example uses
  *  Filtering of text submitted as comments on blogs to allow only certain HTML elements<br/>
<br/>
  *  Making RSS/Atom newsfeed item-content 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/>
<br/>
  *  Text processing for stricter XML standard-compliance: e.g., to have lowercased <spanclass="term">x</span> in hexadecimal numeric entities becomes necessary if an XHTML document with MathML content needs to be served as <spanclass="term">application/xml</span><br/>
<br/>
  *  Scraping text or data from web-pages<br/>
<br/>
  *  Pretty-printing HTML code<br/>
</div>
<divclass="sub-section"><h3>
<aname="s1.2"id="s1.2"></a><spanclass="item-no">1.2</span>  Features
  *  HTML <strong>comments</strong> and <spanclass="term">CDATA</span> sections can be permitted  ^~`<br/>
  *  elements like <spanclass="term">script</span>, <spanclass="term">object</span> and <spanclass="term">form</span> can be permitted  ~<br/>
<br/>
  *  <strong>restrict attributes</strong>, including <strong>element-specifically</strong>  ^~`<br/>
  *  replace potentially dangerous <strong>soft-hyphen</strong> character in URL-accepting attribute values with spaces  *<br/>
<br/>
  *  remove common <strong>invalid characters</strong> not allowed in HTML or XML  ^`<br/>
  *  replace <strong>characters from Microsoft applications</strong> like <spanclass="term">Word</span> that are discouraged in HTML or XML  ^~`<br/>
  *  neutralize entities for characters invalid or discouraged in HTML or XML  ^`<br/>
  *  appropriately neutralize <spanclass="term"><</span>, <spanclass="term">&</span>, <spanclass="term">"</span>, and <spanclass="term">></span> characters  ^*`<br/>
<br/>
  *  understands improperly spaced tag content (like, spread over more than a line) and properly spaces them  `<br/>
  *  attempts to <strong>balance tags</strong> for well-formedness  ^~`<br/>
  *  understands when <strong>omitable closing tags</strong> like <spanclass="term"></p></span> (allowed in HTML 4, transitional, e.g.) are missing  ^~`<br/>
  *  attempts to permit only <strong>validly nested tags</strong>  ^~`<br/>
  *  option to <strong>remove or neutralize bad content</strong> ^~`<br/>
  *  attempts to <strong>rectify common errors of plain-text misplacement</strong> (e.g., directly inside <spanclass="term">blockquote</span>) ^~`<br/>
<br/>
  *  fast, <strong>non-OOP</strong> code of ~45 kb incurring peak basal memory usage of ~0.5 MB<br/>
  *  <strong>compatible</strong> with pre-existing code using <spanclass="term">Kses</span> (the filter used by <spanclass="term">WordPress</span>)<br/>
<br/>
  *  optional <strong>anti-spam</strong> measures such as addition of <spanclass="term">rel="nofollow"</span> and link-disabling  ~`<br/>
  *  optionally makes <strong>relative URLs absolute</strong>, and vice versa  ~`<br/>
<br/>
  *  optionally mark <spanclass="term">&</span> to identify the entities for <spanclass="term">&</span>, <spanclass="term"><</span> and <spanclass="term">></span> introduced by htmLawed  ~`<br/>
<br/>
  *  allows deployment of powerful <strong>hook functions</strong> to <strong>inject</strong> HTML, <strong>consolidate</strong> <spanclass="term">style</span> attributes to <spanclass="term">class</span>, finely check attribute values, etc.  ~`<br/>
<br/>
  *  <strong>independent of character encoding</strong> of input and does not affect it<br/>
<br/>
  *  <strong>tolerance for ill-written HTML</strong> to a certain degree<br/>
</div>
<divclass="sub-section"><h3>
<aname="s1.3"id="s1.3"></a><spanclass="item-no">1.3</span>  History
  htmLawed was developed for use with <spanclass="term">LabWiki</span>, a wiki software developed at PHP Labware, as a suitable software could not be found. Existing PHP software like <spanclass="term">Kses</span> and <spanclass="term">HTMLPurifier</span> were deemed inadequate, slow, resource-intensive, or dependent on external applications like <spanclass="term">HTML Tidy</span>.<br/>
<br/>
  htmLawed started as a modification of Ulf Harnhammar's <spanclass="term">Kses</span> (version 0.2.2) software, and is compatible with code that uses <spanclass="term">Kses</span>; see <ahref="#s2.6">section 2.6</a>.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s1.4"id="s1.4"></a><spanclass="item-no">1.4</span>  License & copyright
  htmLawed is free and open-source software dual licensed under LGPL license version <ahref="http://www.gnu.org/licenses/lgpl-3.0.txt">3</a> and GPL license version <ahref="http://www.gnu.org/licenses/gpl-2.0.txt">2</a> or later, and copyrighted by Santosh Patnaik, MD, PhD.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s1.5"id="s1.5"></a><spanclass="item-no">1.5</span>  Terms used here
  *  <em>character</em> - atomic unit of text; internally represented by a numeric <em>code-point</em> as specified by the <em>encoding</em> or <em>charset</em> in use<br/>
  *  <em>entity</em> - markup like <spanclass="term">&gt;</span> and <spanclass="term">&#160;</span> used to refer to a character<br/>
  *  <em>element</em> - HTML element like <spanclass="term">a</span> and <spanclass="term">img</span><br/>
  *  <em>element content</em> -  content between the opening and closing tags of an element, like <spanclass="term">click</span> of <spanclass="term"><a href="x">click</a></span><br/>
  *  <em>input</em> - text string given to htmLawed to process<br/>
  *  <em>processing</em> - involves filtering, correction, etc., of input<br/>
  *  <em>safe</em> - absence or reduction of certain characters and HTML elements and attributes in the input that can otherwise potentially and circumstantially expose web-site users to security vulnerabilities like cross-site scripting attacks (XSS)<br/>
  *  <em>scheme</em> - URL protocol like <spanclass="term">http</span> and <spanclass="term">ftp</span><br/>
  *  <em>specs</em> - standard specifications<br/>
  *  <em>style property</em> - terms like <spanclass="term">border</span> and <spanclass="term">height</span> for which declarations are made in values for the <spanclass="term">style</span> attribute of elements<br/>
  *  <em>tag</em> - markers like <spanclass="term"><a href="x"></span> and <spanclass="term"></a></span> delineating element content; the opening tag can contain attributes<br/>
  *  <em>tag content</em> - consists of tag markers <spanclass="term"><</span> and <spanclass="term">></span>, element names like <spanclass="term">div</span>, and possibly attributes<br/>
  htmLawed should work with PHP 4.4 and higher. Either <spanclass="term">include()</span> the <spanclass="term">htmLawed.php</span> file or copy-paste the entire code.<br/>
<br/>
  To easily <strong>test</strong> htmLawed using a form-based interface, use the provided <ahref="htmLawedTest.php">demo</a> (<spanclass="term">htmLawed.php</span> and <spanclass="term">htmLawedTest.php</span> should be in the same directory on the web-server).<br/>
<br/>
 <strong>Note</strong>: For code for usage of the htmLawed class (for htmLawed in OOP), please refer to the <ahref="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed">htmLawed</a> website; the filtering itself can be configured, etc., as described here.<br/>
  The input text to be processed, <spanclass="term">$text</span>, is passed as an argument of type string; <spanclass="term">htmLawed()</span> returns the processed string:<br/>
 <strong>Note</strong>: If input is from a <spanclass="term">$_GET</span> or <spanclass="term">$_POST</span> value, and <spanclass="term">magic quotes</span> are enabled on the PHP setup, run <spanclass="term">stripslashes()</span> on the input before passing to htmLawed.<br/>
<br/>
  By default, htmLawed will process the text allowing all valid HTML elements/tags, secure URL scheme/CSS style properties, etc. It will allow <spanclass="term">CDATA</span> sections and HTML comments, balance tags, and ensure proper nesting of elements. Such actions can be configured using two other optional arguments -- <spanclass="term">$config</span> and <spanclass="term">$spec</span>:<br/>
  These extra parameters are detailed below. Some examples are shown in <ahref="#s2.9">section 2.9</a>.<br/>
<br/>
 <strong>Note</strong>: For maximum protection against <spanclass="term">XSS</span> and other scripting attacks (e.g., by disallowing Javascript code), consider using the <spanclass="term">safe</span> parameter; see <ahref="#s3.6">section 3.6</a>.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s2.2"id="s2.2"></a><spanclass="item-no">2.2</span>  Configuring htmLawed using the <spanclass="term">$config</span> parameter
 <spanclass="term">$config</span> instructs htmLawed on how to tackle certain tasks. When <spanclass="term">$config</span> is not specified, or not set as an array (e.g., <spanclass="term">$config = 1</span>), htmLawed will take default actions. One or many of the task-action or value-specification pairs can be specified in <spanclass="term">$config</span> as array key-value pairs. If a parameter is not specified, htmLawed will use the default value/action indicated further below.<br/>
  Below are the possible value-specification combinations. In PHP code, values that are integers should not be quoted and should be used as numeric types (unless meant as string/text).<br/>
<br/>
  Key: <spanclass="term">*</span> default, <spanclass="term">^</span> different default when htmLawed is used in the Kses-compatible mode (see <ahref="#s2.6">section 2.6</a>), <spanclass="term">~</span> different default when <spanclass="term">valid_xhtml</span> is set to <spanclass="term">1</span> (see <ahref="#s3.5">section 3.5</a>), <spanclass="term">"</span> different default when <spanclass="term">safe</span> is set to <spanclass="term">1</span> (see <ahref="#s3.6">section 3.6</a>)<br/>
<br/>
 <strong>abs_url</strong><br/>
  Make URLs absolute or relative; <spanclass="term">$config["base_url"]</span> needs to be set; see <ahref="#s3.4.4">section 3.4.4</a><br/>
<br/>
 <spanclass="term">-1</span> - make relative<br/>
 <spanclass="term">0</span> - no action  *<br/>
 <spanclass="term">1</span> - make absolute<br/>
<br/>
 <strong>and_mark</strong><br/>
  Mark <spanclass="term">&</span> characters in the original input; see <ahref="#s3.2">section 3.2</a><br/>
<br/>
 <strong>anti_link_spam</strong><br/>
  Anti-link-spam measure; see <ahref="#s3.4.7">section 3.4.7</a><br/>
<br/>
 <spanclass="term">0</span> - no measure taken  *<br/>
 <spanclass="term">array("regex1", "regex2")</span> - will ensure a <spanclass="term">rel</span> attribute with <spanclass="term">nofollow</span> in its value in case the <spanclass="term">href</span> attribute value matches the regular expression pattern <spanclass="term">regex1</span>, and/or will remove <spanclass="term">href</span> if its value matches the regular expression pattern <spanclass="term">regex2</span>. E.g., <spanclass="term">array("/./", "/://\W*(?!(abc\.com|xyz\.org))/")</span>; see <ahref="#s3.4.7">section 3.4.7</a> for more.<br/>
<br/>
 <strong>anti_mail_spam</strong><br/>
  Anti-mail-spam measure; see <ahref="#s3.4.7">section 3.4.7</a><br/>
<br/>
 <spanclass="term">0</span> - no measure taken  *<br/>
 <spanclass="term">word</span> - <spanclass="term">@</span> in mail address in <spanclass="term">href</span> attribute value is replaced with specified <spanclass="term">word</span><br/>
<br/>
 <strong>balance</strong><br/>
  Balance tags for well-formedness and proper nesting; see <ahref="#s3.3.3">section 3.3.3</a><br/>
  Base URL value that needs to be set if <spanclass="term">$config["abs_url"]</span> is not <spanclass="term">0</span>; see <ahref="#s3.4.4">section 3.4.4</a><br/>
<br/>
 <strong>cdata</strong><br/>
  Handling of <spanclass="term">CDATA</span> sections; see <ahref="#s3.3.1">section 3.3.1</a><br/>
<br/>
 <spanclass="term">0</span> - don't consider <spanclass="term">CDATA</span> sections as markup and proceed as if plain text  ^"<br/>
 <spanclass="term">2</span> - allow, but neutralize any <spanclass="term"><</span>, <spanclass="term">></span>, and <spanclass="term">&</span> inside by converting them to named entities<br/>
 <spanclass="term">2</span> - allow, but neutralize any <spanclass="term"><</span>, <spanclass="term">></span>, and <spanclass="term">&</span> inside by converting to named entities<br/>
  Allow dynamic CSS expression by not removing the expression from CSS property values in <spanclass="term">style</span> attributes; see <ahref="#s3.4.8">section 3.4.8</a><br/>
  Allow hexadecimal numeric entities and do not convert to the more widely accepted decimal ones, or convert decimal to hexadecimal ones; see <ahref="#s3.2">section 3.2</a><br/>
 <spanclass="term">2</span> - convert decimal to hexadecimal ones<br/>
<br/>
 <strong>hook</strong><br/>
  Name of an optional hook function to alter the input string, <spanclass="term">$config</span> or <spanclass="term">$spec</span> before htmLawed starts its main work; see <ahref="#s3.7">section 3.7</a><br/>
<br/>
 <spanclass="term">0</span> - no hook function  *<br/>
 <spanclass="term">name</span> - <spanclass="term">name</span> is name of the hook function (<spanclass="term">kses_hook</span>  ^)<br/>
<br/>
 <strong>hook_tag</strong><br/>
  Name of an optional hook function to alter tag content finalized by htmLawed; see <ahref="#s3.4.9">section 3.4.9</a><br/>
<br/>
 <spanclass="term">0</span> - no hook function  *<br/>
 <spanclass="term">name</span> - <spanclass="term">name</span> is name of the hook function<br/>
<br/>
 <strong>keep_bad</strong><br/>
  Neutralize bad tags by converting <spanclass="term"><</span> and <spanclass="term">></span> to entities, or remove them; see <ahref="#s3.3.3">section 3.3.3</a><br/>
 <spanclass="term">1</span> - neutralize both tags and element content<br/>
 <spanclass="term">2</span> - remove tags but neutralize element content<br/>
 <spanclass="term">3</span> and <spanclass="term">4</span> - like <spanclass="term">1</span> and <spanclass="term">2</span> but remove if text (<spanclass="term">pcdata</span>) is invalid in parent element<br/>
 <spanclass="term">5</span> and <spanclass="term">6</span> * -  like <spanclass="term">3</span> and <spanclass="term">4</span> but line-breaks, tabs and spaces are left<br/>
<br/>
 <strong>lc_std_val</strong><br/>
  For XHTML compliance, predefined, standard attribute values, like <spanclass="term">get</span> for the <spanclass="term">method</span> attribute of <spanclass="term">form</span>, must be lowercased; see <ahref="#s3.4.5">section 3.4.5</a><br/>
  Transform/remove these non-strict XHTML elements, even if they are allowed by the admin: <spanclass="term">applet</span> <spanclass="term">center</span> <spanclass="term">dir</span> <spanclass="term">embed</span> <spanclass="term">font</span> <spanclass="term">isindex</span> <spanclass="term">menu</span> <spanclass="term">s</span> <spanclass="term">strike</span> <spanclass="term">u</span>; see <ahref="#s3.3.2">section 3.3.2</a><br/>
<br/>
 <spanclass="term">0</span> - no  ^<br/>
 <spanclass="term">1</span> - yes, but leave <spanclass="term">applet</span>, <spanclass="term">embed</span> and <spanclass="term">isindex</span> elements that currently can't be transformed  *<br/>
 <spanclass="term">2</span> - yes, removing <spanclass="term">applet</span>, <spanclass="term">embed</span> and <spanclass="term">isindex</span> elements and their contents (nested elements remain)  ~<br/>
<br/>
 <strong>named_entity</strong><br/>
  Allow non-universal named HTML entities, or convert to numeric ones; see <ahref="#s3.2">section 3.2</a><br/>
 <spanclass="term">1</span> - transform, but <spanclass="term">name</span> attributes for <spanclass="term">a</span> and <spanclass="term">map</span> are retained  *<br/>
  Name of the parent element, possibly imagined, that will hold the input; see <ahref="#s3.3">section 3.3</a><br/>
<br/>
 <strong>safe</strong><br/>
  Magic parameter to make input the most secure against XSS without needing to specify other relevant <spanclass="term">$config</span> parameters; see <ahref="#s3.6">section 3.6</a><br/>
<br/>
 <spanclass="term">0</span> - no  *<br/>
 <spanclass="term">1</span> - will auto-adjust other relevant <spanclass="term">$config</span> parameters (indicated by <spanclass="term">"</span> in this list)<br/>
<br/>
 <strong>schemes</strong><br/>
  Array of attribute-specific, comma-separated, lower-cased list of schemes (protocols) allowed in attributes accepting URLs (or <spanclass="term">!</span> to <em>deny</em> any URL); <spanclass="term">*</span> covers all unspecified attributes; see <ahref="#s3.4.3">section 3.4.3</a><br/>
  Name of a PHP variable to assign the <em>finalized</em> <spanclass="term">$config</span> and <spanclass="term">$spec</span> values; see <ahref="#s3.8">section 3.8</a><br/>
<br/>
 <strong>style_pass</strong><br/>
  Do not look at <spanclass="term">style</span> attribute values, letting them through without any alteration<br/>
<br/>
 <spanclass="term">0</span> - no *<br/>
 <spanclass="term">1</span> - htmLawed will let through any <spanclass="term">style</span> value; see <ahref="#s3.4.8">section 3.4.8</a><br/>
<br/>
 <strong>tidy</strong><br/>
  Beautify or compact HTML code; see <ahref="#s3.3.5">section 3.3.5</a><br/>
 <spanclass="term">0</span> - no  *<br/>
 <spanclass="term">1</span> or <spanclass="term">string</span> - beautify (custom format specified by <spanclass="term">string</span>)<br/>
<br/>
 <strong>unique_ids</strong><br/>
 <spanclass="term">id</span> attribute value checks; see <ahref="#s3.4.2">section 3.4.2</a><br/>
<br/>
 <spanclass="term">0</span> - no  ^<br/>
 <spanclass="term">word</span> - remove invalid ones and replace duplicate ones with new and unique ones based on the <spanclass="term">word</span>; the admin-specified <spanclass="term">word</span>, like <spanclass="term">my_</span>, should begin with a letter (a-z) and can contain letters, digits, <spanclass="term">.</span>, <spanclass="term">_</span>, <spanclass="term">-</span>, and <spanclass="term">:</span>.<br/>
<br/>
 <strong>valid_xhtml</strong><br/>
  Magic parameter to make input the most valid XHTML without needing to specify other relevant <spanclass="term">$config</span> parameters; see <ahref="#s3.5">section 3.5</a><br/>
<br/>
 <spanclass="term">0</span> - no  *<br/>
 <spanclass="term">1</span> - will auto-adjust other relevant <spanclass="term">$config</span> parameters (indicated by <spanclass="term">~</span> in this list)<br/>
<br/>
 <strong>xml:lang</strong><br/>
  Auto-adding <spanclass="term">xml:lang</span> attribute; see <ahref="#s3.4.1">section 3.4.1</a><br/>
<br/>
 <spanclass="term">0</span> - no  *<br/>
 <spanclass="term">1</span> - add if <spanclass="term">lang</span> attribute is present<br/>
 <spanclass="term">2</span> - add if <spanclass="term">lang</span> attribute is present, and remove <spanclass="term">lang</span>  ~<br/>
</div>
<divclass="sub-section"><h3>
<aname="s2.3"id="s2.3"></a><spanclass="item-no">2.3</span>  Extra HTML specifications using the $spec parameter
  The <spanclass="term">$spec</span> argument 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 policy compliance. <spanclass="term">$spec</span> 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 (<spanclass="term">;</span>). E.g.,<br/>
  A rule begins with an HTML <strong>element</strong> name(s) (<em>rule-element</em>), for which the rule applies, followed by an equal (<spanclass="term">=</span>) sign. A rule-element may represent multiple elements if comma (,)-separated element names are used. E.g., <spanclass="term">th,td,tr=</span>.<br/>
<br/>
  Rest of the rule consists of comma-separated HTML <strong>attribute names</strong>. A minus (<spanclass="term">-</span>) character before an attribute means that the attribute is not permitted inside the rule-element. E.g., <spanclass="term">-width</span>. To deny all attributes, <spanclass="term">-*</span> can be used.<br/>
<br/>
  Following shows examples of rule excerpts with rule-element <spanclass="term">a</span> and the attributes that are being permitted:<br/>
  Rules regarding <strong>attribute values</strong> are optionally specified inside round brackets after attribute names in slash ('/')-separated <em>parameter = value</em> pairs. E.g., <spanclass="term">title(maxlen=30/minlen=5)</span>. None, or one or more of the following parameters may be specified:<br/>
<br/>
  *  <spanclass="term">oneof</span> - one or more choices separated by <spanclass="term">|</span> that the value should match; if only one choice is provided, then the value must match that choice<br/>
<br/>
  *  <spanclass="term">noneof</span> - one or more choices separated by <spanclass="term">|</span> that the value should not match<br/>
<br/>
  *  <spanclass="term">maxlen</span> and <spanclass="term">minlen</span> - upper and lower limits for the number of characters in the attribute value; specified in numbers<br/>
<br/>
  *  <spanclass="term">maxval</span> and <spanclass="term">minval</span> - upper and lower limits for the numerical value specified in the attribute value; specified in numbers<br/>
<br/>
  *  <spanclass="term">match</span> and <spanclass="term">nomatch</span> - pattern that the attribute value should or should not match; specified as PHP/PCRE-compatible regular expressions with delimiters and possibly modifiers<br/>
<br/>
  *  <spanclass="term">default</span> - a value to force on the attribute if the value provided by the writer does not fit any of the specified parameters<br/>
<br/>
  If <spanclass="term">default</span> is not set and the attribute value does not satisfy any of the specified parameters, then the attribute is removed. The <spanclass="term">default</span> 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., <spanclass="term">maxlen</span> to <spanclass="term">-1</span>).<br/>
 <strong>Special characters</strong>: The characters <spanclass="term">;</span>, <spanclass="term">,</span>, <spanclass="term">/</span>, <spanclass="term">(</span>, <spanclass="term">)</span>, <spanclass="term">|</span>, <spanclass="term">~</span> 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> by enclosing in pairs of double-quotes (<spanclass="term">"</span>). A back-tick (<spanclass="term">`</span>) can be used to escape a literal <spanclass="term">"</span>. An example rule illustrating this is <spanclass="term">input=value(maxlen=30/match="/^\w/"/default="your `"ID`"")</span>.<br/>
<br/>
 <strong>Note</strong>: To deny an attribute for all elements for which it is legal, <spanclass="term">$config["deny_attribute"]</span> (see <ahref="#s3.4">section 3.4</a>) can be used instead of <spanclass="term">$spec</span>. Also, attributes can be allowed element-specifically through <spanclass="term">$spec</span> while being denied globally through <spanclass="term">$config["deny_attribute"]</span>. The <spanclass="term">hook_tag</span> parameter (<ahref="#s3.4.9">section 3.4.9</a>) can also be used to implement the <spanclass="term">$spec</span> functionality.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s2.4"id="s2.4"></a><spanclass="item-no">2.4</span>  Performance time & memory usage
  The time and memory used by htmLawed depends on its configuration and the size of the input, and the amount, nestedness and well-formedness of the HTML markup within it. In particular, tag balancing and beautification each can increase the processing time by about a quarter.<br/>
<br/>
  The htmLawed <ahref="htmLawedTest.php">demo</a> can be used to evaluate the performance and effects of different types of input and <spanclass="term">$config</span>.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s2.5"id="s2.5"></a><spanclass="item-no">2.5</span>  Some security risks to keep in mind
  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> HTML code which is meant to steal user-data, deface a website, render a page non-functional, etc.<br/>
<br/>
  Unless end-users, either people or software, supplying the content are completely trusted, security issues arising from the degree of HTML usage permission has to be kept in mind. For example, following increase security risks:<br/>
<br/>
  *  Allowing <spanclass="term">script</span>, <spanclass="term">applet</span>, <spanclass="term">embed</span>, <spanclass="term">iframe</span> or <spanclass="term">object</span> elements, or certain of their attributes like <spanclass="term">allowscriptaccess</span><br/>
<br/>
  *  Allowing HTML comments (some Internet Explorer versions are vulnerable with, e.g., <spanclass="term"><!--[if gte IE 4]><script>alert("xss");</script><![endif]--></span><br/>
<br/>
  *  Allowing dynamic CSS expressions (a feature of the IE browser)<br/>
<br/>
  *  Allowing the <spanclass="term">style</span> attribute<br/>
<br/>
  To remove <em>unsecure</em> HTML, code-developers using htmLawed must set <spanclass="term">$config</span> appropriately. E.g., <spanclass="term">$config["elements"] = "* -script"</span> to deny the <spanclass="term">script</span> element (<ahref="#s3.3">section 3.3</a>), <spanclass="term">$config["safe"] = 1</span> to auto-configure ceratin htmLawed parameters for maximizing security (<ahref="#s3.6">section 3.6</a>), etc.<br/>
<br/>
  Permitting the <spanclass="term">*style*</span> attribute brings in risks of <em>click-jacking</em>, <em>phishing</em>, web-page overlays, etc., <em>even</em> when the <spanclass="term">safe</span> parameter is enabled (see <ahref="#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 <spanclass="term">$spec</span> argument, and through the <spanclass="term">hook_tag</span> parameter (see <ahref="#s3.4.8">section 3.4.8</a> for more). Disallowing <spanclass="term">style</span> completely and relying on CSS classes and stylesheet files is recommended.<br/>
<br/>
  htmLawed does not check or correct the character <strong>encoding</strong> of the input it receives. In conjunction with permitting circumstances such as when the character encoding is left undefined through HTTP headers or HTML <spanclass="term">meta</span> tags, this can permit an exploit (like Google's UTF-7/XSS vulnerability of the past).<br/>
</div>
<divclass="sub-section"><h3>
<aname="s2.6"id="s2.6"></a><spanclass="item-no">2.6</span>  Use without modifying old <spanclass="term">kses()</span> code
  The <spanclass="term">Kses</span> PHP script is used by many applications (like <spanclass="term">WordPress</span>). It is possible to have such applications use htmLawed instead, since it is compatible with code that calls the <spanclass="term">kses()</span> function declared in the <spanclass="term">Kses</span> file (usually named <spanclass="term">kses.php</span>). E.g., application code like this will continue to work after replacing <spanclass="term">Kses</span> with htmLawed:<br/>
  For some of the <spanclass="term">$config</span> parameters, htmLawed will use values other than the default ones. These are indicated by <spanclass="term">^</span> in <ahref="#s2.2">section 2.2</a>. To force htmLawed to use other values, function <spanclass="term">kses()</span> in the htmLawed code should be edited -- a few configurable parameters/variables need to be changed.<br/>
<br/>
  If the application uses a <spanclass="term">Kses</span> file that has the <spanclass="term">kses()</span> function declared, then, to have the application use htmLawed instead of <spanclass="term">Kses</span>, simply rename <spanclass="term">htmLawed.php</span> (to <spanclass="term">kses.php</span>, e.g.) and replace the <spanclass="term">Kses</span> file (or just replace the code in the <spanclass="term">Kses</span> file with the htmLawed code). If the <spanclass="term">kses()</span> function in the <spanclass="term">Kses</span> file had been renamed by the application developer (e.g., in <spanclass="term">WordPress</span>, it is named <spanclass="term">wp_kses()</span>), then appropriately rename the <spanclass="term">kses()</span> function in the htmLawed code.<br/>
<br/>
  If the <spanclass="term">Kses</span> file used by the application has been highly altered by the application developers, then one may need a different approach. E.g., with <spanclass="term">WordPress</span>, it is best to copy the htmLawed code to <spanclass="term">wp_includes/kses.php</span>, rename the newly added function <spanclass="term">kses()</span> to <spanclass="term">wp_kses()</span>, and delete the code for the original <spanclass="term">wp_kses()</span> function.<br/>
<br/>
  If the <spanclass="term">Kses</span> code has a non-empty hook function (e.g., <spanclass="term">wp_kses_hook()</span> in case of <spanclass="term">WordPress</span>), then the code for htmLawed's <spanclass="term">kses_hook()</span> function should be appropriately edited. However, the requirement of the hook function should be re-evaluated considering that htmLawed has extra capabilities. With <spanclass="term">WordPress</span>, the hook function is an essential one. The following code is suggested for the htmLawed <spanclass="term">kses_hook()</span> in case of <spanclass="term">WordPress</span>:<br/>
<br/>
<codeclass="code">   function kses_hook($string, &$cf, &$spec){</code>
  htmLawed can work with ill-written HTML code in the input. However, HTML that is too ill-written may not be <em>read</em> as HTML, and be considered mere plain text instead. Following statements indicate the degree of <em>looseness</em> that htmLawed can work with, and can be provided in instructions to writers:<br/>
<br/>
  *  Tags must be flanked by <spanclass="term"><</span> and <spanclass="term">></span> with no <spanclass="term">></span> inside -- any needed <spanclass="term">></span> should be put in as <spanclass="term">&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 <spanclass="term">></span>, like <spanclass="term"><div ></span> and <spanclass="term"><img / ></span>, but not after the <spanclass="term"><</span>.<br/>
<br/>
  *  Element and attribute names need not be lower-cased.<br/>
<br/>
  *  Attribute string of elements may be liberally spaced with tabs, line-breaks, etc.<br/>
<br/>
  *  Attribute values may not be double-quoted, or may be single-quoted.<br/>
<br/>
  *  Left-padding of numeric entities (like, <spanclass="term">&#0160;</span>, <spanclass="term">&x07ff;</span>) with <spanclass="term">0</span> is okay as long as the number of characters between between the <spanclass="term">&</span> and the <spanclass="term">;</span> does not exceed 8. All entities must end with <spanclass="term">;</span> though.<br/>
<br/>
  *  Named character entities must be properly cased. E.g., <spanclass="term">&Lt;</span> or <spanclass="term">&TILDE;</span> will not be let through without modification.<br/>
<br/>
  *  HTML comments should not be inside element tags (okay between tags), and should begin with <spanclass="term"><!--</span> and end with <spanclass="term">--></span>. Characters like <spanclass="term"><</span>, <spanclass="term">></span>, and <spanclass="term">&</span> may be allowed inside depending on <spanclass="term">$config</span>, but any <spanclass="term">--></span> inside should be put in as <spanclass="term">--&gt;</span>. Any <spanclass="term">--</span> inside will be automatically converted to <spanclass="term">-</span>, and a space will be added before the comment delimiter <spanclass="term">--></span>.<br/>
<br/>
  *  <spanclass="term">CDATA</span> 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 <spanclass="term"><[CDATA[</span> and end with <spanclass="term">]]></span>. Characters like <spanclass="term"><</span>, <spanclass="term">></span>, and <spanclass="term">&</span> may be allowed inside depending on <spanclass="term">$config</span>, but any <spanclass="term">]]></span> inside should be put in as <spanclass="term">]]&gt;</span>.<br/>
<br/>
  *  For attribute values, character entities <spanclass="term">&lt;</span>, <spanclass="term">&gt;</span> and <spanclass="term">&amp;</span> should be used instead of characters <spanclass="term"><</span> and <spanclass="term">></span>, and <spanclass="term">&</span> (when <spanclass="term">&</span> is not part of a character entity). This applies even for Javascript code in values of attributes like <spanclass="term">onclick</span>.<br/>
<br/>
  *  Characters <spanclass="term"><</span>, <spanclass="term">></span>, <spanclass="term">&</span> and <spanclass="term">"</span> that are part of actual Javascript, etc., code in <spanclass="term">script</span> elements should be used as such and not be put in as entities like <spanclass="term">&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 <spanclass="term">CDATA</span> sections.<br/>
<br/>
  *  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 <spanclass="term">$config["keep_bad"]</span>, some code/text may be lost.<br/>
<br/>
  *  Input authors should be notified of admin-specified allowed elements, attributes, configuration values (like conversion of named entities to numeric ones), etc.<br/>
<br/>
  *  With <spanclass="term">$config["unique_ids"]</span> not <spanclass="term">0</span> and the <spanclass="term">id</span> attribute being permitted, writers should carefully avoid using duplicate or invalid <spanclass="term">id</span> values as even though htmLawed will correct/remove the values, the final output may not be the one desired. E.g., when <spanclass="term"><a id="home"></a><input id="home" /><label for="home"></label></span> is processed into<br/>
  *  Note that even if intended HTML is lost in a highly ill-written input, the processed output will be more secure and standard-compliant.<br/>
<br/>
  *  For URLs, unless <spanclass="term">$config["scheme"]</span> is appropriately set, writers should avoid using escape characters or entities in schemes. E.g., <spanclass="term">htt&#112;</span> (which many browsers will read as the harmless <spanclass="term">http</span>) may be considered bad by htmLawed.<br/>
<br/>
  *  htmLawed will attempt to put plain text present directly inside <spanclass="term">blockquote</span>, <spanclass="term">form</span>, <spanclass="term">map</span> and <spanclass="term">noscript</span> elements (illegal as per the specs) inside auto-generated <spanclass="term">div</span> elements.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s2.8"id="s2.8"></a><spanclass="item-no">2.8</span>  Limitations & work-arounds
  htmLawed's main objective is to make the input text <em>more</em> standard-compliant, secure for web-page 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 work-arounds.<br/>
<br/>
  It should be borne in mind that no browser application is 100% standard-compliant, and that some of the standard specs (like asking for normalization of white-spacing within <spanclass="term">textarea</span> elements) are clearly wrong. Regarding security, note that <em>unsafe</em> HTML code is not necessarily legally invalid.<br/>
<br/>
  *  htmLawed is meant for input that goes into the <spanclass="term">body</span> of HTML documents. HTML's head-level elements are not supported, nor are the frameset elements <spanclass="term">frameset</span>, <spanclass="term">frame</span> and <spanclass="term">noframes</span>.<br/>
<br/>
  *  It cannot transform the non-standard <spanclass="term">embed</span> elements to the standard-compliant <spanclass="term">object</span> elements. Yet, it can allow <spanclass="term">embed</span> elements if permitted (<spanclass="term">embed</span> is widely used and supported). Admins can certainly use the <spanclass="term">hook_tag</span> parameter (<ahref="#s3.4.9">section 3.4.9</a>) to deploy a custom embed-to-object converter function.<br/>
<br/>
  *  The only non-standard element that may be permitted is <spanclass="term">embed</span>; others like <spanclass="term">noembed</span> and <spanclass="term">nobr</span> cannot be permitted without modifying the htmLawed code.<br/>
<br/>
  *  It cannot handle input that has non-HTML code like <spanclass="term">SVG</span> and <spanclass="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 <ahref="#s3.9">section 3.9</a>. A third way may be to some how take advantage of the <spanclass="term">$config["and_mark"]</span> parameter (see <ahref="#s3.2">section 3.2</a>).<br/>
<br/>
  *  By default, htmLawed won't check many attribute values for standard compliance. E.g., <spanclass="term">width="20m"</span> with the dimension in non-standard <spanclass="term">m</span> is let through. Implementing universal and strict attribute value checks can make htmLawed slow and resource-intensive. Admins should look at the <spanclass="term">hook_tag</span> parameter (<ahref="#s3.4.9">section 3.4.9</a>) or <spanclass="term">$spec</span> to enforce finer checks.<br/>
<br/>
  *  The attributes, deprecated (which can be transformed too) or not, that it supports are largely those that are in the specs. Only a few of the proprietary attributes are supported.<br/>
<br/>
  *  Except for contained URLs and dynamic expressions (also optional), htmLawed does not check CSS style property values. Admins should look at using the <spanclass="term">hook_tag</span> parameter (<ahref="#s3.4.9">section 3.4.9</a>) or <spanclass="term">$spec</span> for finer checks. Perhaps the best option is to disallow <spanclass="term">style</span> but allow <spanclass="term">class</span> attributes with the right <spanclass="term">oneof</span> or <spanclass="term">match</span> values for <spanclass="term">class</span>, and have the various class style properties in <spanclass="term">.css</span> CSS stylesheet files.<br/>
<br/>
  *  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 <spanclass="term">br</span> elements. Such functions are beyond its purview. Admins should use other code to pre- or post-process the input for such purposes.<br/>
<br/>
  *  htmLawed cannot be used to have links force-opened in new windows (by auto-adding appropriate <spanclass="term">target</span> and <spanclass="term">onclick</span> attributes to <spanclass="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 (<spanclass="term">hook_tag</span> parameter; see <ahref="#s3.4.9">section 3.4.9</a>).<br/>
<br/>
  *  Nesting-based checks are not possible. E.g., one cannot disallow <spanclass="term">p</span> elements specifically inside <spanclass="term">td</span> while permitting it elsewhere. Admins may be able to use a custom hook function to enforce such checks (<spanclass="term">hook_tag</span> parameter; see <ahref="#s3.4.9">section 3.4.9</a>).<br/>
<br/>
  *  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 <spanclass="term">http</span> to <spanclass="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 (<spanclass="term">hook_tag</span> parameter; see <ahref="#s3.4.9">section 3.4.9</a>).<br/>
<br/>
  *  Pairs of opening and closing tags that do not enclose any content (like <spanclass="term"><em></em></span>) are not removed. This may be against the standard specs for certain elements (e.g., <spanclass="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/>
<br/>
  *  htmLawed does not check for certain element orderings described in the standard specs (e.g., in a <spanclass="term">table</span>, <spanclass="term">tbody</span> is allowed before <spanclass="term">tfoot</span>). Admins may be able to use a custom hook function to enforce such checks (<spanclass="term">hook_tag</span> parameter; see <ahref="#s3.4.9">section 3.4.9</a>).<br/>
<br/>
  *  htmLawed does not check the number of nested elements. E.g., it will allow two <spanclass="term">caption</span> elements in a <spanclass="term">table</span> element, illegal as per the specs. Admins may be able to use a custom hook function to enforce such checks (<spanclass="term">hook_tag</span> parameter; see <ahref="#s3.4.9">section 3.4.9</a>).<br/>
<br/>
  *  htmLawed might convert certain entities to actual characters and remove backslashes and CSS comment-markers (<spanclass="term">/*</span>) in <spanclass="term">style</span> attribute values in order to detect malicious HTML like crafted IE-specific dynamic expressions like <spanclass="term">&#101;xpression...</span>. If this is too harsh, admins can allow CSS expressions through htmLawed core but then use a custom function through the <spanclass="term">hook_tag</span> parameter (<ahref="#s3.4.9">section 3.4.9</a>) to more specifically identify CSS expressions in the <spanclass="term">style</span> attribute values. Also, using <spanclass="term">$config["style_pass"]</span>, it is possible to have htmLawed pass <spanclass="term">style</span> attribute values without even looking at them (<ahref="#s3.4.8">section 3.4.8</a>).<br/>
<br/>
  *  htmLawed does not correct certain possible attribute-based security vulnerabilities (e.g., <spanclass="term"><a href="http://x%22+style=%22background-image:xss">x</a></span>). These arise when browsers mis-identify markup in <em>escaped</em> text, defeating the very purpose of escaping text (a bad browser will read the given example as <spanclass="term"><a href="http://x" style="background-image:xss">x</a></span>).<br/>
<br/>
  *  Because of poor Unicode support in PHP, htmLawed does not remove the <em>high value</em> HTML-invalid characters with multi-byte code-points. Such characters however are extremely unlikely to be in the input. (see <ahref="#s3.1">section 3.1</a>).<br/>
<br/>
  *  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 <spanclass="term">meta</span> tags, this can permit an exploit (like Google's UTF-7/XSS vulnerability of the past).<br/>
<br/>
  *  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/>
</div>
<divclass="sub-section"><h3>
<aname="s2.9"id="s2.9"></a><spanclass="item-no">2.9</span>  Examples of usage
  Allowing only <spanclass="term">safe</span> HTML and the elements <spanclass="term">a</span>, <spanclass="term">em</span>, and <spanclass="term">strong</span> --<br/>
  Allowing attribute <spanclass="term">title</span> only in <spanclass="term">a</span> and not allowing attributes <spanclass="term">id</span>, <spanclass="term">style</span>, or scriptable <em>on*</em> attributes like <spanclass="term">onclick</span> --<br/>
  Some case-studies are presented below.<br/>
<br/>
 <strong>1.</strong> A blog administrator wants to allow only <spanclass="term">a</span>, <spanclass="term">em</span>, <spanclass="term">strike</span>, <spanclass="term">strong</span> and <spanclass="term">u</span> in comments, but needs <spanclass="term">strike</span> and <spanclass="term">u</span> transformed to <spanclass="term">span</span> for better XHTML 1-strict compliance, and, he wants the <spanclass="term">a</span> links to be to <spanclass="term">http</span> or <spanclass="term">https</span> resources:<br/>
 <strong>2.</strong> An author uses a custom-made web application to load content on his web-site. 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> 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/>
  For the final submission process, <spanclass="term">keep_bad</span> is set to <spanclass="term">6</span>. A value of <spanclass="term">1</span> for the preview process allows the author to note and correct any HTML mistake without losing any of the typed text.<br/>
<br/>
 <strong>3.</strong> 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/>
  Valid characters (more correctly, their code-points) in HTML or XML are, hexadecimally, <spanclass="term">9</span>, <spanclass="term">a</span>, <spanclass="term">d</span>, <spanclass="term">20</span> to <spanclass="term">d7ff</span>, and <spanclass="term">e000</span> to <spanclass="term">10ffff</span>, except <spanclass="term">fffe</span> and <spanclass="term">ffff</span> (decimally, <spanclass="term">9</span>, <spanclass="term">10</span>, <spanclass="term">13</span>, <spanclass="term">32</span> to <spanclass="term">55295</span>, and <spanclass="term">57344</span> to <spanclass="term">1114111</span>, except <spanclass="term">65534</span> and <spanclass="term">65535</span>). htmLawed removes the invalid characters <spanclass="term">0</span> to <spanclass="term">8</span>, <spanclass="term">b</span>, <spanclass="term">c</span>, and <spanclass="term">e</span> to <spanclass="term">1f</span>.<br/>
<br/>
  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/>
<br/>
  Characters that are discouraged (see <ahref="#s5.1">section 5.1</a>) but not invalid are not removed by htmLawed.<br/>
<br/>
  It (function <spanclass="term">hl_tag()</span>) also replaces the potentially dangerous (in some Mozilla [Firefox] and Opera browsers) soft-hyphen character (code-point, hexadecimally, <spanclass="term">ad</span>, or decimally, <spanclass="term">173</span>) in attribute values with spaces. Where required, the characters <spanclass="term"><</span>, <spanclass="term">></span>, <spanclass="term">&</span>, and <spanclass="term">"</span> are converted to entities.<br/>
<br/>
  With <spanclass="term">$config["clean_ms_char"]</span> set as <spanclass="term">1</span> or <spanclass="term">2</span>, many of the discouraged characters (decimal code-points <spanclass="term">127</span> to <spanclass="term">159</span> except <spanclass="term">133</span>) that many Microsoft applications incorrectly use (as per the <spanclass="term">Windows 1252</span> [<spanclass="term">Cp-1252</span>] or a similar encoding system), and the character for decimal code-point <spanclass="term">133</span>, are converted to appropriate decimal numerical entities (or removed for a few cases)-- see appendix in <ahref="#s5.4">section 5.4</a>. This can help avoid some display issues arising from copying-pasting of content.<br/>
<br/>
  With <spanclass="term">$config["clean_ms_char"]</span> set as <spanclass="term">2</span>, characters for the hexadecimal code-points <spanclass="term">82</span>, <spanclass="term">91</span>, and <spanclass="term">92</span> (for special single-quotes), and <spanclass="term">84</span>, <spanclass="term">93</span>, and <spanclass="term">94</span> (for special double-quotes) are converted to ordinary single and double quotes respectively and not to entities.<br/>
<br/>
  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/>
<br/>
  The <spanclass="term">$config["clean_ms_char"]</span> 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 <spanclass="term">Windows 1252</span> (<spanclass="term">Cp-1252</span>) or a similar encoding like <spanclass="term">Cp-1251</span>. Further, the input form and the web-pages displaying it or its content should have the character encoding appropriately marked-up.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s3.2"id="s3.2"></a><spanclass="item-no">3.2</span>  Character references/entities
  Valid character entities take the form <spanclass="term">&*;</span> where <spanclass="term">*</span> is <spanclass="term">#x</span> followed by a hexadecimal number (hexadecimal numeric entity; like <spanclass="term">&#xA0;</span> for non-breaking space), or alphanumeric like <spanclass="term">gt</span> (external or named entity; like <spanclass="term">&nbsp;</span> for non-breaking space), or <spanclass="term">#</span> followed by a number (decimal numeric entity; like <spanclass="term">&#160;</span> for non-breaking space). Character entities referring to the soft-hyphen character (the <spanclass="term">&shy;</span> or <spanclass="term">\xad</span> character; hexadecimal code-point <spanclass="term">ad</span> [decimal <spanclass="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/>
  *  Neutralizes entities with multiple leading zeroes or missing semi-colons (potentially dangerous)<br/>
<br/>
  *  Lowercases the <spanclass="term">X</span> (for XML-compliance) and <spanclass="term">A-F</span> of hexadecimal numeric entities<br/>
<br/>
  *  Neutralizes entities referring to characters that are HTML-invalid (see <ahref="#s3.1">section 3.1</a>)<br/>
<br/>
  *  Neutralizes entities referring to characters that are HTML-discouraged (code-points, hexadecimally, <spanclass="term">7f</span> to <spanclass="term">84</span>, <spanclass="term">86</span> to <spanclass="term">9f</span>, and <spanclass="term">fdd0</span> to <spanclass="term">fddf</span>, or decimally, <spanclass="term">127</span> to <spanclass="term">132</span>, <spanclass="term">134</span> to <spanclass="term">159</span>, and <spanclass="term">64991</span> to <spanclass="term">64976</span>). Entities referring to the remaining discouraged characters (see <ahref="#s5.1">section 5.1</a> for a full list) are let through.<br/>
<br/>
  *  Neutralizes named entities that are not in the specs.<br/>
<br/>
  *  Optionally converts valid HTML-specific named entities except <spanclass="term">&gt;</span>, <spanclass="term">&lt;</span>, <spanclass="term">&quot;</span>, and <spanclass="term">&amp;</span> to decimal numeric ones (hexadecimal if $config["hexdec_entity"] is <spanclass="term">2</span>) for generic XML-compliance. For this, <spanclass="term">$config["named_entity"]</span> should be <spanclass="term">1</span>.<br/>
<br/>
  *  Optionally converts hexadecimal numeric entities to the more widely supported decimal ones. For this, <spanclass="term">$config["hexdec_entity"]</span> should be <spanclass="term">0</span>.<br/>
<br/>
  *  Optionally converts decimal numeric entities to the hexadecimal ones. For this, <spanclass="term">$config["hexdec_entity"]</span> should be <spanclass="term">2</span>.<br/>
<br/>
 <em>Neutralization</em> refers to the <em>entitification</em> of <spanclass="term">&</span> to <spanclass="term">&amp;</span>.<br/>
<br/>
 <strong>Note</strong>: htmLawed does not convert entities to the actual characters represented by them; one can pass the htmLawed output through PHP's <spanclass="term">html_entity_decode</span> <ahref="http://www.php.net/html_entity_decode">function</a> for that.<br/>
<br/>
 <strong>Note</strong>: If <spanclass="term">$config["and_mark"]</span> is set, and set to a value other than <spanclass="term">0</span>, then the <spanclass="term">&</span> characters in the original input are replaced with the control character for the hexadecimal code-point <spanclass="term">6</span> (<spanclass="term">\x06</span>; <spanclass="term">&</span> characters introduced by htmLawed, e.g., after converting <spanclass="term"><</span> to <spanclass="term">&lt;</span>, are not affected). This allows one to distinguish, say, an <spanclass="term">&gt;</span> introduced by htmLawed and an <spanclass="term">&gt;</span> 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 <spanclass="term">o(><)o</span> 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 <spanclass="term">\x06</span> can break documents. Before use in such documents, and preferably before any storage, any remaining <spanclass="term">\x06</span> should be changed back to <spanclass="term">&</span>, e.g., with:<br/>
  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 <spanclass="term">$config["keep_bad"]</span>, are either <em>neutralized</em> (converted to plain text by entitification of <spanclass="term"><</span> and <spanclass="term">></span>) or removed.<br/>
<br/>
  E.g., with only <spanclass="term">em</span> permitted:<br/>
<br/>
  Input:<br/>
<br/>
<codeclass="code">   <em>My</em> website is <a href="http://a.com>a.com</a>.</code>
<br/>
<br/>
  Output, with <spanclass="term">$config["keep_bad"] = 0</span>:<br/>
<br/>
<codeclass="code">   <em>My</em> website is a.com.</code>
<br/>
<br/>
  Output, with <spanclass="term">$config["keep_bad"]</span> not <spanclass="term">0</span>:<br/>
<br/>
<codeclass="code">   <em>My</em> website is &lt;a href=""&gt;a.com&lt;/a&gt;.</code>
<br/>
<br/>
  See <ahref="#s3.3.3">section 3.3.3</a> for differences between the various non-zero <spanclass="term">$config["keep_bad"]</span> values.<br/>
<br/>
  htmLawed by default permits these 86 elements:<br/>
  Except for <spanclass="term">embed</span> (included because of its wide-spread use) and the Ruby elements (<spanclass="term">rb</span>, <spanclass="term">rbc</span>, <spanclass="term">rp</span>, <spanclass="term">rt</span>, <spanclass="term">rtc</span>, <spanclass="term">ruby</span>; part of XHTML 1.1), these are all the elements in the HTML 4/XHTML 1 specs. Strict-specific specs. exclude <spanclass="term">center</span>, <spanclass="term">dir</span>, <spanclass="term">font</span>, <spanclass="term">isindex</span>, <spanclass="term">menu</span>, <spanclass="term">s</span>, <spanclass="term">strike</span>, and <spanclass="term">u</span>.<br/>
<br/>
  With <spanclass="term">$config["safe"] = 1</span>, the default set will exclude <spanclass="term">applet</span>, <spanclass="term">embed</span>, <spanclass="term">iframe</span>, <spanclass="term">object</span> and <spanclass="term">script</span>; see <ahref="#s3.6">section 3.6</a>.<br/>
<br/>
  When <spanclass="term">$config["elements"]</span>, which specifies allowed elements, is <em>properly</em> defined, and neither empty nor set to <spanclass="term">0</span> or <spanclass="term">*</span>, the default set is not used. To have elements added to or removed from the default set, a <spanclass="term">+/-</span> notation is used. E.g., <spanclass="term">*-script-object</span> implies that only <spanclass="term">script</span> and <spanclass="term">object</span> are disallowed, whereas <spanclass="term">*+embed</span> means that <spanclass="term">noembed</span> is also allowed. Elements can also be specified as comma separated names. E.g., <spanclass="term">a, b, i</span> means only <spanclass="term">a</span>, <spanclass="term">b</span> and <spanclass="term">i</span> are permitted. In this notation, <spanclass="term">*</span>, <spanclass="term">+</span> and <spanclass="term">-</span> have no significance and can actually cause a mis-reading.<br/>
<br/>
  Some more examples of <spanclass="term">$config["elements"]</span> values indicating permitted elements (note that empty spaces are liberally allowed for clarity):<br/>
<br/>
  *  <spanclass="term">a, blockquote, code, em, strong</span> -- only <spanclass="term">a</span>, <spanclass="term">blockquote</span>, <spanclass="term">code</span>, <spanclass="term">em</span>, and <spanclass="term">strong</span><br/>
  *  <spanclass="term">*-script</span> -- all excluding <spanclass="term">script</span><br/>
  *  <spanclass="term">*+noembed-script</span> -- all including <spanclass="term">noembed</span> excluding <spanclass="term">script</span><br/>
<br/>
  Some mis-usages (and the resulting permitted elements) that can be avoided:<br/>
<br/>
  *  <spanclass="term">-*</span> -- none; instead of htmLawed, one might just use, e.g., the <spanclass="term">htmlspecialchars()</span> PHP function<br/>
  *  <spanclass="term">*</span> -- all; admin need not have set <spanclass="term">elements</span><br/>
  *  <spanclass="term">*-form+form</span> -- all; a <spanclass="term">+</span> will always over-ride any <spanclass="term">-</span><br/>
  *  <spanclass="term">*, noembed</span> -- only <spanclass="term">noembed</span>; admin probably meant <spanclass="term">*+noembed</span><br/>
  Basically, when using the <spanclass="term">+/-</span> notation, commas (<spanclass="term">,</span>) should not be used, and vice versa, and <spanclass="term">*</span> should be used with the former but not the latter.<br/>
<br/>
 <strong>Note</strong>: Even if an element that is not in the default set is allowed through <spanclass="term">$config["elements"]</span>, like <spanclass="term">noembed</span> in the last example, it will eventually be removed during tag balancing unless such balancing is turned off (<spanclass="term">$config["balance"]</span> set to <spanclass="term">0</span>). Currently, the only way around this, which actually is simple, is to edit the various arrays in the function <spanclass="term">hl_bal()</span> to accommodate the element and its nesting properties.<br/>
<br/>
 <strong>A possibly second way to specify allowed elements</strong> is to set <spanclass="term">$config["parent"]</span> to an element name that supposedly will hold the input, and to set <spanclass="term">$config["balance"]</span> to <spanclass="term">1</span>. During tag balancing (see <ahref="#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 <spanclass="term">div</span> if <spanclass="term">$config["parent"]</span> is empty, <spanclass="term">body</span>, or an element not in htmLawed's default set of 86 elements.<br/>
<br/>
 <em>Tag transformation</em> is possible for improving XHTML-Strict compliance -- most of the deprecated elements are removed or converted to valid XHTML-Strict ones; see <ahref="#s3.3.2">section 3.3.2</a>.<br/>
<divclass="sub-sub-section"><h4>
<aname="s3.3.1"id="s3.3.1"></a><spanclass="item-no">3.3.1</span>  Handling of comments and CDATA sections
 <spanclass="term">CDATA</span> sections have the format <spanclass="term"><![CDATA[...anything but not "]]>"...]]></span>, and HTML comments, <spanclass="term"><!--...anything but not "-->"... --></span>. Neither HTML comments nor <spanclass="term">CDATA</span> sections can reside inside tags. HTML comments can exist anywhere else, but <spanclass="term">CDATA</span> sections can exist only where plain text is allowed (e.g., immediately inside <spanclass="term">td</span> element content but not immediately inside <spanclass="term">tr</span> element content).<br/>
<br/>
  htmLawed (function <spanclass="term">hl_cmtcd()</span>) handles HTML comments or <spanclass="term">CDATA</span> sections depending on the values of <spanclass="term">$config["comment"]</span> or <spanclass="term">$config["cdata"]</span>. If <spanclass="term">0</span>, such markup is not looked for and the text is processed like plain text. If <spanclass="term">1</span>, it is removed completely. If <spanclass="term">2</span>, it is preserved but any <spanclass="term"><</span>, <spanclass="term">></span> and <spanclass="term">&</span> inside are changed to entities. If <spanclass="term">3</span>, they are left as such.<br/>
<br/>
  Note that for the last two cases, HTML comments and <spanclass="term">CDATA</span> sections will always be removed from tag content (function <spanclass="term">hl_tag()</span>).<br/>
<br/>
  Examples:<br/>
<br/>
  Input:<br/>
<codeclass="code">  <!-- home link --><a href="home.htm"><![CDATA[x=&y]]>Home</a></code>
<codeclass="code">  <!-- home link --><a href="home.htm"><![CDATA[x=&y]]>Home</a></code>
<br/>
<br/>
  For standard-compliance, comments are given the form <spanclass="term"><!--comment --></span>, and any <spanclass="term">--</span> in the content is made <spanclass="term">-</span>.<br/>
<br/>
  When <spanclass="term">$config["safe"] = 1</span>, CDATA sections and comments are considered plain text unless <spanclass="term">$config["comment"]</span> or <spanclass="term">$config["cdata"]</span> is explicitly specified; see <ahref="#s3.6">section 3.6</a>.<br/>
</div>
<divclass="sub-sub-section"><h4>
<aname="s3.3.2"id="s3.3.2"></a><spanclass="item-no">3.3.2</span>  Tag-transformation for better XHTML-Strict
  If <spanclass="term">$config["make_tag_strict"]</span> is set and not <spanclass="term">0</span>, following non-XHTML-Strict elements (and attributes), even if admin-permitted, are mutated as indicated (element content remains intact; function <spanclass="term">hl_tag2()</span>):<br/>
<br/>
  *  applet - (based on <spanclass="term">$config["make_tag_strict"]</span>, unchanged (<spanclass="term">1</span>) or removed (<spanclass="term">2</span>))<br/>
<codeclass="code">   The PHP <s>software</s> script used for this <strike>web-page</strike> web-page is <font style="font-weight: bold " face=arial size='+3' color   =  "red  ">htmLawedTest.php</font>, from <u style= 'color:green'>PHP Labware</u>.</code>
  If <spanclass="term">$config["balance"]</span> is set to <spanclass="term">1</span>, htmLawed (function <spanclass="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/>
<br/>
  Depending on the value of <spanclass="term">$config["keep_bad"]</span> (see <ahref="#s2.2">section 2.2</a> and <ahref="#s3.3">section 3.3</a>), illegal content may be removed or neutralized to plain text by converting < and > to entities:<br/>
<br/>
 <spanclass="term">0</span> - remove; this option is available only to maintain Kses-compatibility and should not be used otherwise (see <ahref="#s2.6">section 2.6</a>)<br/>
 <spanclass="term">1</span> - neutralize tags and keep element content<br/>
 <spanclass="term">2</span> - remove tags but keep element content<br/>
 <spanclass="term">3</span> and <spanclass="term">4</span> - like <spanclass="term">1</span> and <spanclass="term">2</span>, but keep element content only if text (<spanclass="term">pcdata</span>) is valid in parent element as per specs<br/>
 <spanclass="term">5</span> and <spanclass="term">6</span> -  like <spanclass="term">3</span> and <spanclass="term">4</span>, but line-breaks, tabs and spaces are left<br/>
<br/>
  Example input (disallowing the <spanclass="term">p</span> element):<br/>
  An option like <spanclass="term">1</span> is useful, e.g., when a writer previews his submission, whereas one like <spanclass="term">3</span> is useful before content is finalized and made available to all.<br/>
<br/>
 <strong>Note:</strong> In the example above, unlike <spanclass="term"><*></span>, <spanclass="term"><xml></span> gets considered as a tag (even though there is no HTML element named <spanclass="term">xml</span>). In general, text matching the regular expression pattern <spanclass="term"><(/?)([a-zA-Z][a-zA-Z1-6]*)([^>]*?)\s?></span> is considered a tag (phrase enclosed by the angled brackets <spanclass="term"><</span> and <spanclass="term">></span>, and starting [with an optional slash preceding] with an alphanumeric word that starts with an alphabet...).<br/>
<br/>
  Nesting/content rules for each of the 86 elements in htmLawed's default set (see <ahref="#s3.3">section 3.3</a>) are defined in function <spanclass="term">hl_bal()</span>. This means that if a non-standard element besides <spanclass="term">embed</span> is being permitted through <spanclass="term">$config["elements"]</span>, the element's tag content will end up getting removed if <spanclass="term">$config["balance"]</span> is set to <spanclass="term">1</span>.<br/>
<br/>
  Plain text and/or certain elements nested inside <spanclass="term">blockquote</span>, <spanclass="term">form</span>, <spanclass="term">map</span> and <spanclass="term">noscript</span> 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 <spanclass="term">form</span>, the input <spanclass="term">B:<input type="text" value="b" />C:<input type="text" value="c" /></span> is converted to <spanclass="term"><div>B:<input type="text" value="b" />C:<input type="text" value="c" /></div></span>.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s3.3.4"id="s3.3.4"></a><spanclass="item-no">3.3.4</span>  Elements requiring child elements
  In some cases, the specs stipulate the number and/or the ordering of the child elements. A <spanclass="term">table</span> can have 0 or 1 <spanclass="term">caption</span>, <spanclass="term">tbody</span>, <spanclass="term">tfoot</span>, and <spanclass="term">thead</span>, but they must be in this order: <spanclass="term">caption</span>, <spanclass="term">thead</span>, <spanclass="term">tfoot</span>, <spanclass="term">tbody</span>.<br/>
<br/>
  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/>
<br/>
  With <spanclass="term">$config["direct_list_nest"]</span> set to <spanclass="term">1</span>, htmLawed will allow direct nesting of an <spanclass="term">ol</span> or <spanclass="term">ul</span> list within another <spanclass="term">ol</span> or <spanclass="term">ul</span> without requiring the child list to be within an <spanclass="term">li</span> of the parent list. While this is not standard-compliant, directly nested lists are rendered properly by almost all browsers. The parameter <spanclass="term">$config["direct_list_nest"]</span> has no effect if tag-balancing (<ahref="#s3.3.3">section 3.3.3</a>) is turned off.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s3.3.5"id="s3.3.5"></a><spanclass="item-no">3.3.5</span>  Beautify or compact HTML
  By default, htmLawed will neither <em>beautify</em> 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/>
<br/>
  As per the HTML standards, spaces, tabs and line-breaks in web-pages (except those inside <spanclass="term">pre</span> 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> allows the use of text/code beautifully formatted with indentations and line-spacings for readability. Such <em>pretty</em> HTML can, however, increase the size of web-pages, or make the extraction or scraping of plain text cumbersome.<br/>
<br/>
  With the <spanclass="term">$config</span> parameter <spanclass="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 <spanclass="term">pre</span>, the <spanclass="term">script</span> and <spanclass="term">textarea</span> elements, CDATA sections, and HTML comments are not subjected to the tidying process.<br/>
<br/>
  To <em>compact</em>, use <spanclass="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/>
<br/>
  To <em>beautify</em>, <spanclass="term">$config["tidy"]</span> is set as <spanclass="term">1</span>, or for customized tidying, as a string like <spanclass="term">2s2n</span>. The <spanclass="term">s</span> or <spanclass="term">t</span> 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 <spanclass="term">r</span> and <spanclass="term">n</span> characters are used to specify line-break characters: <spanclass="term">n</span> for <spanclass="term">\n</span> (Unix/Mac OS X line-breaks), <spanclass="term">rn</span> or <spanclass="term">nr</span> for <spanclass="term">\r\n</span> (Windows/DOS line-breaks), or <spanclass="term">r</span> for <spanclass="term">\r</span>.<br/>
<br/>
  The <spanclass="term">$config["tidy"]</span> value of <spanclass="term">1</span> is equivalent to <spanclass="term">2s0n</span>. Other <spanclass="term">$config["tidy"]</span> values are read loosely: a value of <spanclass="term">4</span> is equivalent to <spanclass="term">4s0n</span>; <spanclass="term">t2</span>, to <spanclass="term">1t2n</span>; <spanclass="term">s</span>, to <spanclass="term">2s0n</span>; <spanclass="term">2TR</span>, to <spanclass="term">2t0r</span>; <spanclass="term">T1</span>, to <spanclass="term">1t1n</span>; <spanclass="term">nr3</span>, to <spanclass="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/>
<br/>
  Input formatting using <spanclass="term">$config["tidy"]</span> is not recommended when input text has mixed markup (like HTML + PHP).<br/>
  htmLawed will only permit attributes described in the HTML specs (including deprecated ones). It also permits some attributes for use with the <spanclass="term">embed</span> element (the non-standard <spanclass="term">embed</span> element is supported in htmLawed because of its widespread use), and the the <spanclass="term">xml:space</span> attribute (valid only in XHTML 1.1). A list of such 111 attributes and the elements they are allowed in is in <ahref="#s5.2">section 5.2</a>.<br/>
<br/>
  When <spanclass="term">$config["deny_attribute"]</span> is not set, or set to <spanclass="term">0</span>, or empty (<spanclass="term">""</span>), all the 111 attributes are permitted. Otherwise, <spanclass="term">$config["deny_attribute"]</span> can be set as a list of comma-separated names of the denied attributes. <spanclass="term">on*</span> can be used to refer to the group of potentially dangerous, script-accepting attributes: <spanclass="term">onblur</span>, <spanclass="term">onchange</span>, <spanclass="term">onclick</span>, <spanclass="term">ondblclick</span>, <spanclass="term">onfocus</span>, <spanclass="term">onkeydown</span>, <spanclass="term">onkeypress</span>, <spanclass="term">onkeyup</span>, <spanclass="term">onmousedown</span>, <spanclass="term">onmousemove</span>, <spanclass="term">onmouseout</span>, <spanclass="term">onmouseover</span>, <spanclass="term">onmouseup</span>, <spanclass="term">onreset</span>, <spanclass="term">onselect</span> and <spanclass="term">onsubmit</span>.<br/>
<br/>
  Note that attributes specified in <spanclass="term">$config["deny_attribute"]</span> are denied globally, for all elements. To deny attributes for only specific elements, <spanclass="term">$spec</span> (see <ahref="#s2.3">section 2.3</a>) can be used. <spanclass="term">$spec</span> can also be used to element-specifically permit an attribute otherwise denied through <spanclass="term">$config["deny_attribute"]</span>.<br/>
<br/>
  With <spanclass="term">$config["safe"] = 1</span> (<ahref="#s3.6">section 3.6</a>), the <spanclass="term">on*</span> attributes are automatically disallowed.<br/>
<br/>
 <strong>Note</strong>: To deny all but a few attributes globally, a simpler way to specify <spanclass="term">$config["deny_attribute"]</span> would be to use the notation <spanclass="term">* -attribute1 -attribute2 ...</span>. Thus, a value of <spanclass="term">* -title -href</span> implies that except <spanclass="term">href</span> and <spanclass="term">title</span> (where allowed as per standards) all other attributes are to be removed. With this notation, the value for the parameter <spanclass="term">safe</span> (<ahref="#s3.6">section 3.6</a>) will have no effect on <spanclass="term">deny_attribute</span>.<br/>
  *  Removes duplicate attributes (last one stays)<br/>
  *  Gives attributes the form <spanclass="term">name="value"</span> and single-spaces them, removing unnecessary white-spacing<br/>
  *  Provides <em>required</em> attributes (see <ahref="#s3.4.1">section 3.4.1</a>)<br/>
  *  Double-quotes values and escapes any <spanclass="term">"</span> inside them<br/>
  *  Replaces the possibly dangerous soft-hyphen characters (hexadecimal code-point <spanclass="term">ad</span>) in the values with spaces<br/>
  *  Allows custom function to additionally filter/modify attribute values (see <ahref="#s3.4.9">section 3.4.9</a>)<br/>
<divclass="sub-sub-section"><h4>
<aname="s3.4.1"id="s3.4.1"></a><spanclass="item-no">3.4.1</span>  Auto-addition of XHTML-required attributes
  If indicated attributes for the following elements are found missing, htmLawed (function <spanclass="term">hl_tag()</span>) will add them (with values same as attribute names unless indicated otherwise below):<br/>
<br/>
  *  area - alt (<spanclass="term">area</span>)<br/>
  *  area, img - src, alt (<spanclass="term">image</span>)<br/>
  *  bdo - dir (<spanclass="term">ltr</span>)<br/>
  *  form - action<br/>
  *  map - name<br/>
  *  optgroup - label<br/>
  *  param - name<br/>
  *  script - type (<spanclass="term">text/javascript</span>)<br/>
  Additionally, with <spanclass="term">$config["xml:lang"]</span> set to <spanclass="term">1</span> or <spanclass="term">2</span>, if the <spanclass="term">lang</span> but not the <spanclass="term">xml:lang</span> attribute is declared, then the latter is added too, with a value copied from that of <spanclass="term">lang</span>. This is for better standard-compliance. With <spanclass="term">$config["xml:lang"]</span> set to <spanclass="term">2</span>, the <spanclass="term">lang</span> attribute is removed (XHTML 1.1 specs).<br/>
<br/>
  Note that the <spanclass="term">name</span> attribute for <spanclass="term">map</span>, invalid in XHTML 1.1, is also transformed if required -- see <ahref="#s3.4.6">section 3.4.6</a>.<br/>
  If <spanclass="term">$config["unique_ids"]</span> is <spanclass="term">1</span>, htmLawed (function <spanclass="term">hl_tag()</span>) removes <spanclass="term">id</span> attributes with values that are not XHTML-compliant (must begin with a letter and can contain letters, digits, <spanclass="term">:</span>, <spanclass="term">.</span>, <spanclass="term">-</span> and <spanclass="term">_</span>) or duplicate. If <spanclass="term">$config["unique_ids"]</span> is a word, any duplicate but otherwise valid value will be appropriately prefixed with the word to ensure its uniqueness. The word should begin with a letter and should contain only letters, numbers, <spanclass="term">:</span>, <spanclass="term">.</span>, <spanclass="term">_</span> and <spanclass="term">-</span>.<br/>
<br/>
  Even if multiple inputs need to be filtered (through multiple calls to htmLawed), htmLawed ensures uniqueness of <spanclass="term">id</span> values as it uses a global variable (<spanclass="term">$GLOBALS["hl_Ids"]</span> array). Further, an admin can restrict the use of certain <spanclass="term">id</span> values by presetting this variable before htmLawed is called into use. E.g.:<br/>
<br/>
<codeclass="code">   $GLOBALS['hl_Ids'] = array('top'=>1, 'bottom'=>1, 'myform'=>1); // id values not allowed in input</code>
  htmLawed edits attributes that take URLs as values if they are found to contain un-permitted schemes. E.g., if the <spanclass="term">afp</span> scheme is not permitted, then <spanclass="term"><a href="afp://domain.org"></span> becomes <spanclass="term"><a href="denied:afp://domain.org"></span>, and if Javascript is not permitted <spanclass="term"><a onclick="javascript:xss();"></span> becomes <spanclass="term"><a onclick="denied:javascript:xss();"></span>.<br/>
<br/>
  By default htmLawed permits these schemes in URLs for the <spanclass="term">href</span> attribute:<br/>
  Also, only <spanclass="term">file</span>, <spanclass="term">http</span> and <spanclass="term">https</span> are permitted in attributes whose names start with <spanclass="term">o</span> (like <spanclass="term">onmouseover</span>), and in these attributes that accept URLs:<br/>
  These default sets are used when <spanclass="term">$config["schemes"]</span> is not set (see <ahref="#s2.2">section 2.2</a>). To over-ride the defaults, <spanclass="term">$config["schemes"]</span> is defined as a string of semi-colon-separated sub-strings of type <spanclass="term">attribute: comma-separated schemes</span>. E.g., <spanclass="term">href: mailto, http, https; onclick: javascript; src: http, https</span>. For unspecified attributes, <spanclass="term">file</span>, <spanclass="term">http</span> and <spanclass="term">https</span> are permitted. This can be changed by passing schemes for <spanclass="term">*</span> in <spanclass="term">$config["schemes"]</span>. E.g., <spanclass="term">href: mailto, http, https; *: https, https</span>.<br/>
<br/>
 <spanclass="term">*</span> can be put in the list of schemes to permit all protocols. E.g., <spanclass="term">style:*; img: http, https</span> results in protocols not being checked in <spanclass="term">style</span> attribute values. However, in such cases, any relative-to-absolute URL conversion, or vice versa, (<ahref="#s3.4.4">section 3.4.4</a>) is not done.<br/>
<br/>
  Thus, <em>to allow Javascript</em>, one can set <spanclass="term">$config["schemes"]</span> as <spanclass="term">href: mailto, http, https; *: http, https, javascript</span>, or <spanclass="term">href: mailto, http, https, javascript; *: http, https, javascript</span>, or <spanclass="term">*:*</span>, and so on.<br/>
<br/>
  As a side-note, one may find <spanclass="term">style:*</span> useful as URLs in <spanclass="term">style</span> 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/>
<br/>
 <spanclass="term">!</span> can be put in the list of schemes to disallow all protocols as well as <em>local</em> URLs. Thus, with <spanclass="term">href: http, style: !</span>, '<a href="http://cnn.com" style="background-image: url('local.jpg');">CNN</a>' will become '<a href="http://cnn.com" style="background-image: url('denied:local.jpg');">CNN</a>'.<br/>
<br/>
 <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 <spanclass="term">src</span> (e.g., <spanclass="term">dynsrc</span>) or starts with <spanclass="term">o</span> (e.g., <spanclass="term">onbeforecopy</span>).<br/>
<br/>
  With <spanclass="term">$config["safe"] = 1</span>, all URLs are disallowed in the <spanclass="term">style</span> attribute values.<br/>
</div>
<divclass="sub-sub-section"><h4>
<aname="s3.4.4"id="s3.4.4"></a><spanclass="item-no">3.4.4</span>  Absolute & relative URLs in attribute values
  htmLawed can make absolute URLs in attributes like <spanclass="term">href</span> relative (<spanclass="term">$config["abs_url"]</span> is <spanclass="term">-1</span>), and vice versa (<spanclass="term">$config["abs_url"]</span> is <spanclass="term">1</span>). URLs in scripts are not considered for this, and so are URLs like <spanclass="term">#section_6</span> (fragment), <spanclass="term">?name=Tim#show</span> (starting with query string), and <spanclass="term">;var=1?name=Tim#show</span> (starting with parameters). Further, this requires that <spanclass="term">$config["base_url"]</span> be set properly, with the <spanclass="term">://</span> and a trailing slash (<spanclass="term">/</span>), with no query string, etc. E.g., <spanclass="term">file:///D:/page/</span>, <spanclass="term">https://abc.com/x/y/</span>, or <spanclass="term">http://localhost/demo/</span> are okay, but <spanclass="term">file:///D:/page/?help=1</span>, <spanclass="term">abc.com/x/y/</span> and <spanclass="term">http://localhost/demo/index.htm</span> are not.<br/>
<br/>
  For making absolute URLs relative, only those URLs that have the <spanclass="term">$config["base_url"]</span> string at the beginning are converted. E.g., with <spanclass="term">$config["base_url"] = "https://abc.com/x/y/"</span>, <spanclass="term">https://abc.com/x/y/a.gif</span> and <spanclass="term">https://abc.com/x/y/z/b.gif</span> become <spanclass="term">a.gif</span> and <spanclass="term">z/b.gif</span> respectively, while <spanclass="term">https://abc.com/x/c.gif</span> is not changed.<br/>
<br/>
  When making relative URLs absolute, only values for scheme, network location (host-name) and path values in the base URL are inherited. See <ahref="#s5.5">section 5.5</a> for more about the URL specification as per RFC <ahref="http://www.ietf.org/rfc/rfc1808.txt">1808</a>.<br/>
</div>
<divclass="sub-sub-section"><h4>
<aname="s3.4.5"id="s3.4.5"></a><spanclass="item-no">3.4.5</span>  Lower-cased, standard attribute values
  Optionally, for standard-compliance, htmLawed (function <spanclass="term">hl_tag()</span>) lower-cases standard attribute values to give, e.g., <spanclass="term">input type="password"</span> instead of <spanclass="term">input type="Password"</span>, if <spanclass="term">$config["lc_std_val"]</span> is <spanclass="term">1</span>. Attribute values matching those listed below for any of the elements (plus those for the <spanclass="term">type</span> attribute of <spanclass="term">button</span> or <spanclass="term">input</span>) are lower-cased:<br/>
  If <spanclass="term">$config["no_deprecated_attr"]</span> is <spanclass="term">0</span>, then deprecated attributes (see appendix in <ahref="#s5.2">section 5.2</a>) are removed and, in most cases, their values are transformed to CSS style properties and added to the <spanclass="term">style</span> attributes (function <spanclass="term">hl_tag()</span>). Except for <spanclass="term">bordercolor</span> for <spanclass="term">table</span>, <spanclass="term">tr</span> and <spanclass="term">td</span>, the scores of proprietary attributes that were never part of any cross-browser standard are not supported.<br/>
<br/>
 <strong>Note</strong>: The attribute <spanclass="term">target</span> for <spanclass="term">a</span> is allowed even though it is not in XHTML 1.0 specs. This is because of the attribute's wide-spread use and browser-support, and because the attribute is valid in XHTML 1.1 onwards.<br/>
<br/>
  *  align - for <spanclass="term">img</span> with value of <spanclass="term">left</span> or <spanclass="term">right</span>, becomes, e.g., <spanclass="term">float: left</span>; for <spanclass="term">div</span> and <spanclass="term">table</span> with value <spanclass="term">center</span>, becomes <spanclass="term">margin: auto</span>; all others become, e.g., <spanclass="term">text-align: right</span><br/>
  For <spanclass="term">lang</span>, deprecated in XHTML 1.1, transformation is taken care of through <spanclass="term">$config["xml:lang"]</span>; see <ahref="#s3.4.1">section 3.4.1</a>.<br/>
<br/>
  The attribute <spanclass="term">name</span> is deprecated in <spanclass="term">form</span>, <spanclass="term">iframe</span>, and <spanclass="term">img</span>, and is replaced with <spanclass="term">id</span> if an <spanclass="term">id</span> attribute doesn't exist and if the <spanclass="term">name</span> value is appropriate for <spanclass="term">id</span>. For such replacements for <spanclass="term">a</span> and <spanclass="term">map</span>, for which the <spanclass="term">name</span> attribute is deprecated in XHTML 1.1, <spanclass="term">$config["no_deprecated_attr"]</span> should be set to <spanclass="term">2</span> (when set to <spanclass="term">1</span>, for these two elements, the <spanclass="term">name</span> attribute is retained).<br/>
  htmLawed (function <spanclass="term">hl_tag()</span>) can check the <spanclass="term">href</span> attribute values (link addresses) as an anti-spam (email or link spam) measure.<br/>
<br/>
  If <spanclass="term">$config["anti_mail_spam"]</span> is not <spanclass="term">0</span>, the <spanclass="term">@</span> of email addresses in <spanclass="term">href</span> values like <spanclass="term">mailto:a@b.com</span> is replaced with text specified by <spanclass="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., <spanclass="term"><remove_this_antispam>@</span> (makes the example address <spanclass="term">a<remove_this_antispam>@b.com</span>).<br/>
<br/>
  For regular links, one can choose to have a <spanclass="term">rel</span> attribute with <spanclass="term">nofollow</span> 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 <spanclass="term">href</span> value altogether (disable the link).<br/>
<br/>
  For use of these options, <spanclass="term">$config["anti_link_spam"]</span> should be set as an array with values <spanclass="term">regex1</span> and <spanclass="term">regex2</span>, both or one of which can be empty (like <spanclass="term">array("", "regex2")</span>) to indicate that that option is not to be used. Otherwise, <spanclass="term">regex1</span> or <spanclass="term">regex2</span> should be PHP- and PCRE-compatible regular expression patterns: <spanclass="term">href</span> values will be matched against them and those matching the pattern will accordingly be treated.<br/>
<br/>
  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/>
<br/>
  An example, to have a <spanclass="term">rel</span> attribute with <spanclass="term">nofollow</span> for all links, and to disable links that do not point to domains <spanclass="term">abc.com</span> and <spanclass="term">xyz.org</span>:<br/>
  htmLawed can check URL schemes and dynamic expressions (to guard against Javascript, etc., script-based insecurities) in inline CSS style property values in the <spanclass="term">style</span> attributes. (CSS properties like <spanclass="term">background-image</span> that accept URLs in their values are noted in <ahref="#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 <spanclass="term">$config["css_expression"]</span> to <spanclass="term">1</span> (default setting). Note that when <spanclass="term">$config["css_expression"]</span> is set to <spanclass="term">1</span>, htmLawed will remove <spanclass="term">/*</span> from the <spanclass="term">style</span> values.<br/>
<br/>
 <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 <spanclass="term">style</span> 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 (<spanclass="term">$config["schemes"] = "...style:*..."</span>, see <ahref="#s3.4.3">section 3.4.3</a>, and <spanclass="term">$config["css_expression"] = 0</span>). Alternately, admins can use their own custom function for finer handling of <spanclass="term">style</span> values through the <spanclass="term">hook_tag</span> parameter (see <ahref="#s3.4.9">section 3.4.9</a>).<br/>
<br/>
  It is also possible to have htmLawed let through any <spanclass="term">style</span> value by setting <spanclass="term">$config["style_pass"]</span> to <spanclass="term">1</span>.<br/>
<br/>
  As such, it is better to set up a CSS file with class declarations, disallow the <spanclass="term">style</span> attribute, set a <spanclass="term">$spec</span> rule (see <ahref="#s2.3">section 2.3</a>) for <spanclass="term">class</span> for the <spanclass="term">oneof</span> or <spanclass="term">match</span> parameter, and ask writers to make use of the <spanclass="term">class</span> attribute.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s3.4.9"id="s3.4.9"></a><spanclass="item-no">3.4.9</span>  Hook function for tag content
  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/>
  When <spanclass="term">$config</span> parameter <spanclass="term">hook_tag</span> is set to the name of a function, htmLawed (function <spanclass="term">hl_tag()</span>) will pass on the element name, and, in the case of an opening tag, the <em>finalized</em> 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 <spanclass="term"><element_name attribute_1_name="attribute_1_value"...></span> (for empty elements like <spanclass="term">img</span> and <spanclass="term">input</span>, the element-closing slash <spanclass="term">/</span> should also be included), etc.<br/>
<br/>
  Any <spanclass="term">hook_tag</span> function, since htmLawed version 1.1.11, also receives names of elements in closing tags, such as <spanclass="term">a</span> in the closing <spanclass="term"></a></span> tag of the element <spanclass="term"><a href="http://cnn.com">CNN</a></span>. Unlike for opening tags, no other value (i.e., the attribute name-value array) 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 <spanclass="term"></a></span>).<br/>
  This is a <strong>powerful functionality</strong> that can be exploited for various objectives: consolidate-and-convert inline <spanclass="term">style</span> attributes to <spanclass="term">class</span>, convert <spanclass="term">embed</span> elements to <spanclass="term">object</span>, permit only one <spanclass="term">caption</span> element in a <spanclass="term">table</span> element, disallow embedding of certain types of media, <strong>inject HTML</strong>, use <ahref="http://csstidy.sourceforge.net">CSSTidy</a> to sanitize <spanclass="term">style</span> attribute values, etc.<br/>
<br/>
  As an example, the custom hook code below can be used to force a series of specifically ordered <spanclass="term">id</span> attributes on all elements, and a specific <spanclass="term">param</span> element inside all <spanclass="term">object</span> elements:<br/>
  The <spanclass="term">hook_tag</span> parameter is different from the <spanclass="term">hook</span> parameter (<ahref="#s3.7">section 3.7</a>).<br/>
<br/>
  Snippets of hook function code developed by others may be available on the <ahref="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed">htmLawed</a> website.<br/>
</div>
</div>
<divclass="sub-section"><h3>
<aname="s3.5"id="s3.5"></a><spanclass="item-no">3.5</span>  Simple configuration directive for most valid XHTML
  If <spanclass="term">$config["valid_xhtml"]</span> is set to <spanclass="term">1</span>, some relevant <spanclass="term">$config</span> parameters (indicated by <spanclass="term">~</span> in <ahref="#s2.2">section 2.2</a>) are auto-adjusted. This allows one to pass the <spanclass="term">$config</span> argument with a simpler value. If a value for a parameter auto-set through <spanclass="term">valid_xhtml</span> is still manually provided, then that value will over-ride the auto-set value.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s3.6"id="s3.6"></a><spanclass="item-no">3.6</span>  Simple configuration directive for most <em>safe</em> HTML
 <em>Safe</em> 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 specs. When elements such as <spanclass="term">script</span> and <spanclass="term">object</span>, and attributes such as <spanclass="term">onmouseover</span> and <spanclass="term">style</span> are allowed in the input text, an input writer can introduce malevolent HTML code. Note that what is considered <spanclass="term">safe</span> depends on the nature of the web application and the trust-level accorded to its users.<br/>
<br/>
  htmLawed allows an admin to use <spanclass="term">$config["safe"]</span> to auto-adjust multiple <spanclass="term">$config</span> parameters (such as <spanclass="term">elements</span> which declares the allowed element-set), which otherwise would have to be manually set. The relevant parameters are indicated by <spanclass="term">"</span> in <ahref="#s2.2">section 2.2</a>). Thus, one can pass the <spanclass="term">$config</span> argument with a simpler value.<br/>
<br/>
  With the value of <spanclass="term">1</span>, htmLawed considers <spanclass="term">CDATA</span> sections and HTML comments as plain text, and prohibits the <spanclass="term">applet</span>, <spanclass="term">embed</span>, <spanclass="term">iframe</span>, <spanclass="term">object</span> and <spanclass="term">script</span> elements, and the <spanclass="term">on*</span> attributes like <spanclass="term">onclick</span>. ( There are <spanclass="term">$config</span> parameters like <spanclass="term">css_expression</span> that are not affected by the value set for <spanclass="term">safe</span> but whose default values still contribute towards a more <em>safe</em> output.) Further, URLs with schemes (see <ahref="#s3.4.3">section 3.4.3</a>) are neutralized so that, e.g., <spanclass="term">style="moz-binding:url(http://danger)"</span> becomes <spanclass="term">style="moz-binding:url(denied:http://danger)"</span>.<br/>
<br/>
  Admins, however, may still want to completely deny the <spanclass="term">style</span> attribute, e.g., with code like<br/>
  Permitting the <spanclass="term">style</span> 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 <spanclass="term">style</span> values. It does provide ways for the code-developer implementing htmLawed to do such checks through the <spanclass="term">$spec</span> argument, and through the <spanclass="term">hook_tag</span> parameter (see <ahref="#s3.4.8">section 3.4.8</a> for more). Disallowing style completely and relying on CSS classes and stylesheet files is recommended.<br/>
<br/>
  If a value for a parameter auto-set through <spanclass="term">safe</span> is still manually provided, then that value can over-ride the auto-set value. E.g., with <spanclass="term">$config["safe"] = 1</span> and <spanclass="term">$config["elements"] = "*+script"</span>, <spanclass="term">script</span>, but not <spanclass="term">applet</span>, is allowed.<br/>
<br/>
  A page illustrating the efficacy of htmLawed's anti-XSS abilities with <spanclass="term">safe</span> set to <spanclass="term">1</span> against XSS vectors listed by <ahref="http://ha.ckers.org/xss.html">RSnake</a> may be available <ahref="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/rsnake/RSnakeXSSTest.htm">here</a>.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s3.7"id="s3.7"></a><spanclass="item-no">3.7</span>  Using a hook function
  If <spanclass="term">$config["hook"]</span> is not set to <spanclass="term">0</span>, then htmLawed will allow preliminarily processed input to be altered by a hook function named by <spanclass="term">$config["hook"]</span> before starting the main work (but after handling of characters, entities, HTML comments and <spanclass="term">CDATA</span> sections -- see code for function <spanclass="term">htmLawed()</span>).<br/>
<br/>
  The hook function also allows one to alter the <em>finalized</em> values of <spanclass="term">$config</span> and <spanclass="term">$spec</span>.<br/>
<br/>
  Note that the <spanclass="term">hook</span> parameter is different from the <spanclass="term">hook_tag</span> parameter (<ahref="#s3.4.9">section 3.4.9</a>).<br/>
<br/>
  Snippets of hook function code developed by others may be available on the <ahref="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed">htmLawed</a> website.<br/>
  htmLawed can assign the <em>finalized</em> <spanclass="term">$config</span> and <spanclass="term">$spec</span> values to a variable named by <spanclass="term">$config["show_setting"]</span>. The variable, made global by htmLawed, is set as an array with three keys: <spanclass="term">config</span>, with the <spanclass="term">$config</span> value, <spanclass="term">spec</span>, with the <spanclass="term">$spec</span> value, and <spanclass="term">time</span>, with a value that is the Unix time (the output of PHP's <spanclass="term">microtime()</span> 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/>
<br/>
  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/>
</div>
<divclass="sub-section"><h3>
<aname="s3.9"id="s3.9"></a><spanclass="item-no">3.9</span>  Retaining non-HTML tags in input with mixed markup
  htmLawed does not remove certain characters that though invalid are nevertheless discouraged in HTML documents as per the specs (see <ahref="#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 <spanclass="term"><</span>, <spanclass="term">></span> and <spanclass="term">&</span> 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/>
<br/>
  To deal with such mixed markup, the input text can be pre-processed to hide the non-HTML markup by specifically replacing the <spanclass="term"><</span>, <spanclass="term">></span> and <spanclass="term">&</span> characters with some of the HTML-discouraged characters (see <ahref="#s3.1.2">section 3.1.2</a>). Post-htmLawed processing, the replacements are reverted.<br/>
<br/>
  An example (mixed HTML and PHP code in input text):<br/>
  This code will not work if <spanclass="term">$config["clean_ms_char"]</span> is set to <spanclass="term">1</span> (<ahref="#s3.1">section 3.1</a>), in which case one should instead deploy a hook function (<ahref="#s3.7">section 3.7</a>). (htmLawed internally uses certain control characters, code-points <spanclass="term">1</span> to <spanclass="term">7</span>, and use of these characters as markers in the logic of hook functions may cause issues.)<br/>
<br/>
  Admins may also be able to use <spanclass="term">$config["and_mark"]</span> to deal with such mixed markup; see <ahref="#s3.2">section 3.2</a>.<br/>
</div>
</div>
<divclass="section"><h2>
<aname="s4"id="s4"></a><spanclass="item-no">4</span>  Other
  A careful re-reading of this documentation will very likely answer your questions.<br/>
<br/>
  Software updates and forum-based community-support may be found at <ahref="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 <ahref="http://php.net">http://php.net</a>.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s4.2"id="s4.2"></a><spanclass="item-no">4.2</span>  Known issues
  (The release date for the downloadable package of files containing documentation, demo script, test-cases, etc., besides the <spanclass="term">htmLawed.php</span> file may be updated independently if the secondary files are revised.)<br/>
<br/>
 <em>Version number - Release date. Notes</em><br/>
  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. <spanclass="term">$config["hook_tag"]</span>, if specified, now receives names of elements in closing tags.<br/>
  1.1.10 - 22 October 2011. Fix for a bug in the <spanclass="term">tidy</span> functionality that caused the entire input to be replaced with a single space; new parameter, <spanclass="term">$config["direct_list_nest"]</span> to allow direct descendance of a list in a list. (5 April 2012. Dual licensing from LGPLv3 to LGPLv3 and GPLv2+.)<br/>
<br/>
  1.1.9.5 - 6 July 2011. Minor correction of a rule for nesting of <spanclass="term">li</span> within <spanclass="term">dir</span><br/>
<br/>
  1.1.9.4 - 3 July 2010. Parameter <spanclass="term">schemes</span> now accepts <spanclass="term">!</span> so any URL, even a local one, can be <em>denied</em>. An issue in which a second URL value in <spanclass="term">style</span> properties was not checked was fixed.<br/>
<br/>
  1.1.9.3 - 17 May 2010. Checks for correct nesting of <spanclass="term">param</span><br/>
<br/>
  1.1.9.2 - 26 April 2010. Minor fix regarding rendering of denied URL schemes<br/>
<br/>
  1.1.9.1 - 26 February 2010. htmLawed now uses the LGPL version 3 license; support for <spanclass="term">flashvars</span> attribute for <spanclass="term">embed</span><br/>
<br/>
  1.1.9 - 22 December 2009. Soft-hyphens are now removed only from URL-accepting attribute values<br/>
<br/>
  1.1.8.1 - 16 July 2009. Minor code-change to fix a PHP error notice<br/>
<br/>
  1.1.8 - 23 April 2009. Parameter <spanclass="term">deny_attribute</span> now accepts the wild-card <spanclass="term">*</span>, making it simpler to specify its value when all but a few attributes are being denied; fixed a bug in interpreting <spanclass="term">$spec</span><br/>
<br/>
  1.1.7 - 11-12 March 2009. Attributes globally denied through <spanclass="term">deny_attribute</span> can be allowed element-specifically through <spanclass="term">$spec</span>; <spanclass="term">$config["style_pass"]</span> allowing letting through any <spanclass="term">style</span> value introduced; altered logic to catch certain types of dynamic crafted CSS expressions<br/>
<br/>
  1.1.3-6 - 28-31 January - 4 February 2009. Altered logic to catch certain types of dynamic crafted CSS expressions<br/>
<br/>
  1.1.2 - 22 January 2009. Fixed bug in parsing of <spanclass="term">font</span> attributes during tag transformation<br/>
<br/>
  1.1.1 - 27 September 2008. Better nesting correction when omitable closing tags are absent<br/>
<br/>
  1.1 - 29 June 2008. <spanclass="term">$config["hook_tag"]</span> and <spanclass="term">$config["format"]</span> introduced for custom tag/attribute check/modification/injection and output compaction/beautification; fixed a regex-in-$spec parsing bug<br/>
<br/>
  1.0.9 - 11 June 2008. Fixed bug in invalid HTML code-point entity check<br/>
<br/>
  1.0.8 - 15 May 2008. <spanclass="term">bordercolor</span> attribute for <spanclass="term">table</span>, <spanclass="term">td</span> and <spanclass="term">tr</span><br/>
<br/>
  1.0.7 - 1 May 2008. Support for <spanclass="term">wmode</span> attribute for <spanclass="term">embed</span>; <spanclass="term">$config["show_setting"]</span> introduced; improved <spanclass="term">$config["elements"]</span> evaluation<br/>
<br/>
  1.0.6 - 20 April 2008. <spanclass="term">$config["and_mark"]</span> introduced<br/>
<br/>
  1.0.5 - 12 March 2008. <spanclass="term">style</span> URL schemes essentially disallowed when $config <spanclass="term">safe</span> is on; improved regex for CSS expression search<br/>
<br/>
  1.0.4 - 10 March 2008. Improved corrections for <spanclass="term">blockquote</span>, <spanclass="term">form</span>, <spanclass="term">map</span> and <spanclass="term">noscript</span><br/>
<br/>
  1.0.3 - 3 March 2008. Character entities for soft-hyphens are now replaced with spaces (instead of being removed); a bug allowing <spanclass="term">td</span> directly inside <spanclass="term">table</span> fixed; <spanclass="term">safe</span> <spanclass="term">$config</span> parameter added<br/>
<br/>
  1.0.2 - 13 February 2008. Improved implementation of <spanclass="term">$config["keep_bad"]</span><br/>
<br/>
  1.0.1 - 7 November 2007. Improved regex for identifying URLs, protocols and dynamic expressions (<spanclass="term">hl_tag()</span> and <spanclass="term">hl_prot()</span>); no error display with <spanclass="term">hl_regex()</span><br/>
  To test htmLawed using a form interface, a <ahref="htmLawedTest.php">demo</a> web-page is provided with the htmLawed distribution (<spanclass="term">htmLawed.php</span> and <spanclass="term">htmLawedTest.php</span> should be in the same directory on the web-server). A file with <ahref="htmLawed_TESTCASE.txt">test-cases</a> is also provided.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s4.5"id="s4.5"></a><spanclass="item-no">4.5</span>  Upgrade, & old versions
  Upgrading is as simple as replacing the previous version of <spanclass="term">htmLawed.php</span> (assuming it was not modified for customized features). As htmLawed output is almost always used in static documents, upgrading should not affect old, finalized content.<br/>
 <strong>Important</strong>  The following upgrades may affect the functionality of a specific htmLawed as indicated by their corresponding notes:<br/>
<br/>
  (1) From version 1.1-1.1.10 to 1.1.11, if a <spanclass="term">hook_tag</span> function is in use: In version 1.1.11, 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 <spanclass="term">hook_tag</span> function receives only the element name. The <spanclass="term">hook_tag</span> function therefore may have to be edited. See <ahref="#s3.4.9">section 3.4.9</a>.<br/>
  Old versions of htmLawed may be available online. E.g., for version 1.0, check <ahref="http://www.bioinformatics.org/phplabware/downloads/htmLawed1.zip">http://www.bioinformatics.org/phplabware/downloads/htmLawed1.zip</a>, for 1.1.1, htmLawed111.zip, and for 1.1.10, htmLawed1110.zip.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s4.6"id="s4.6"></a><spanclass="item-no">4.6</span>  Comparison with <spanclass="term">HTMLPurifier</span>
  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 mid-2009):<br/>
<br/>
  *  does not support PHP versions older than 5.0 (HTMLPurifier dropped PHP 4 support after version 2)<br/>
<br/>
  *  is 15-20 times bigger (scores of files totalling more than 750 kb)<br/>
<br/>
  *  consumes 10-15 times more RAM memory (just including the HTMLPurifier files without calling the filter requires a few MBs of memory)<br/>
<br/>
  *  is expectedly slower<br/>
<br/>
  *  does not allow admins to fully allow all valid HTML (because of incomplete HTML support, it always considers elements like <spanclass="term">script</span> illegal)<br/>
<br/>
  *  lacks many of the extra features of htmLawed (like entity conversions and code compaction/beautification)<br/>
<br/>
  *  has poor documentation<br/>
<br/>
  However, HTMLPurifier has finer checks for character encodings and attribute values, and can log warnings and errors. Visit the HTMLPurifier <ahref="http://htmlpurifier.org">website</a> for updated information.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s4.7"id="s4.7"></a><spanclass="item-no">4.7</span>  Use through application plug-ins/modules
  Plug-ins/modules to implement htmLawed in applications such as Drupal and DokuWiki may have been developed. Please check the application websites and the forum on the htmLawed <ahref="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed">site</a>.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s4.8"id="s4.8"></a><spanclass="item-no">4.8</span>  Use in non-PHP applications
  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 <ahref="http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed">site</a>.<br/>
  A donation in any currency and amount to appreciate or support this software can be sent by <ahref="http://paypal.com">PayPal</a> to this email address: drpatnaik at yahoo dot com.<br/>
  Nicholas Alipaz, Bryan Blakey, Pádraic Brady, Ulf Harnhammer, Gareth Heyes, Klaus Leithoff, Lukasz Pilorz, Shelley Powers, Edward Yang, and many anonymous users.<br/>
  Characters represented by the following hexadecimal code-points are <em>not</em> invalid, even though some validators may issue messages stating otherwise.<br/>
  Valid attribute-element combinations as per W3C specs.<br/>
<br/>
  *  includes deprecated attributes (marked <spanclass="term">^</span>), attributes for the non-standard <spanclass="term">embed</span> element (marked <spanclass="term">*</span>), and the proprietary <spanclass="term">bordercolor</span> (marked <spanclass="term">~</span>)<br/>
  *  only non-frameset, HTML body elements<br/>
  *  <spanclass="term">name</span> for <spanclass="term">a</span> and <spanclass="term">map</span>, and <spanclass="term">lang</span> are invalid in XHTML 1.1<br/>
  *  <spanclass="term">target</span> is valid for <spanclass="term">a</span> in XHTML 1.1 and higher<br/>
  *  <spanclass="term">xml:space</span> is only for XHTML 1.1<br/>
<br/>
  abbr - td, th<br/>
  accept - form, input<br/>
  accept-charset - form<br/>
  accesskey - a, area, button, input, label, legend, textarea<br/>
  An <em>absolute</em> URL has a <spanclass="term">protocol</span> or <spanclass="term">scheme</span>, a <spanclass="term">network location</span> or <spanclass="term">hostname</span>, and, optional <spanclass="term">path</span>, <spanclass="term">parameters</span>, <spanclass="term">query</span> and <spanclass="term">fragment</span> segments. Thus, an absolute URL has this generic structure:<br/>
  The schemes can only contain letters, digits, <spanclass="term">+</span>, <spanclass="term">.</span> and <spanclass="term">-</span>. Hostname is the portion after the <spanclass="term">//</span> and up to the first <spanclass="term">/</span> (if any; else, up to the end) when <spanclass="term">:</span> is followed by a <spanclass="term">//</span> (e.g., <spanclass="term">abc.com</span> in <spanclass="term">ftp://abc.com/def</span>); otherwise, it consists of everything after the <spanclass="term">:</span> (e.g., <spanclass="term">def@abc.com</span> in mailto:def@abc.com').<br/>
<br/>
 <em>Relative</em> URLs do not have explicit schemes and network locations; such values are inherited from a <em>base</em> URL.<br/>
</div>
<divclass="sub-section"><h3>
<aname="s5.6"id="s5.6"></a><spanclass="item-no">5.6</span>  Brief on htmLawed code
  *  <spanclass="term">$in</span> - 1st argument; a text string; the <strong>input text</strong> to be processed. Any extraneous slashes added by PHP when <em>magic quotes</em> are enabled should be removed beforehand using PHP's <spanclass="term">stripslashes()</span> function.<br/>
<br/>
  *  <spanclass="term">$config</span> - 2nd argument; an associative array; optional (named <spanclass="term">$C</span> in htmLawed code). The array has keys with names like <spanclass="term">balance</span> and <spanclass="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> (indicated by the keys). All configurable parameters receive some default value if the value to be used is not specified by the user through <spanclass="term">$config</span>. <em>Finalized</em> <spanclass="term">$config</span> is thus a filtered and possibly larger array.<br/>
<br/>
  *  <spanclass="term">$spec</span> - 3rd argument; a text string; optional. The string has rules, written in an htmLawed-designated format, <strong>specifying</strong> element-specific attribute and attribute value restrictions. Function <spanclass="term">hl_spec()</span> is used to convert the string to an associative-array for internal use. <em>Finalized</em> <spanclass="term">$spec</span> is thus an array.<br/>
<br/>
 <em>Finalized</em> <spanclass="term">$config</span> and <spanclass="term">$spec</span> are made <strong>global variables</strong> 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> values, the <spanclass="term">show_settings</span> parameter of <spanclass="term">$config</span> should be used). Depending on <spanclass="term">$config</span>, another global variable <spanclass="term">hl_Ids</span>, to track <spanclass="term">id</span> attribute values for uniqueness, may be set. Unlike the other two variables, this one is not reset (or unset) post-processing.<br/>
<br/>
  Except for the main function <spanclass="term">htmLawed()</span> and the functions <spanclass="term">kses()</span> and <spanclass="term">kses_hook()</span>, htmLawed's functions are <strong>name-spaced</strong> using the <spanclass="term">hl_</span> prefix. The <strong>functions</strong> and their roles are:<br/>
<br/>
  *  <spanclass="term">hl_attrval</span> - checking attribute values against $spec<br/>
  *  <spanclass="term">hl_bal</span> - tag balancing<br/>
  *  <spanclass="term">hl_cmtcd</span> - handling CDATA sections and HTML comments<br/>
  *  <spanclass="term">htmLawed</span> - main function<br/>
  *  <spanclass="term">kses</span> - main function of <spanclass="term">kses</span><br/>
  *  <spanclass="term">kses_hook</span> - hook function of <spanclass="term">kses</span><br/>
<br/>
  The last two are for compatibility with pre-existing code using the <spanclass="term">kses</span> script. htmLawed's <spanclass="term">kses()</span> basically passes on the filtering task to <spanclass="term">htmLawed()</span> function after deciphering <spanclass="term">$config</span> and <spanclass="term">$spec</span> from the argument values supplied to it. <spanclass="term">kses_hook()</span> is an empty function and is meant for being filled with custom code if the <spanclass="term">kses</span> script users were using one.<br/>
<br/>
 <spanclass="term">htmLawed()</span> finalizes <spanclass="term">$spec</span> (with the help of <spanclass="term">hl_spec()</span>) and <spanclass="term">$config</span>, and globalizes them. Finalization of <spanclass="term">$config</span> involves setting default values if an inappropriate or invalid one is supplied. This includes calling <spanclass="term">hl_regex()</span> to check well-formedness of regular expression patterns if such expressions are user-supplied through <spanclass="term">$config</span>. <spanclass="term">htmLawed()</span> then removes invalid characters like nulls and <spanclass="term">x01</span> and appropriately handles entities using <spanclass="term">hl_ent()</span>. HTML comments and CDATA sections are identified and treated as per <spanclass="term">$config</span> with the help of <spanclass="term">hl_cmtcd()</span>. When retained, the <spanclass="term"><</span> and <spanclass="term">></span> characters identifying them, and the <spanclass="term"><</span>, <spanclass="term">></span> and <spanclass="term">&</span> characters inside them, are replaced with control characters (code-points <spanclass="term">1</span> to <spanclass="term">5</span>) till any tag balancing is completed.<br/>
<br/>
  After this <em>initial processing</em> <spanclass="term">htmLawed()</span> identifies tags using regex and processes them with the help of <spanclass="term">hl_tag()</span> --  a large function that analyzes tag content, filtering it as per HTML standards, <spanclass="term">$config</span> and <spanclass="term">$spec</span>. Among other things, <spanclass="term">hl_tag()</span> transforms deprecated elements using <spanclass="term">hl_tag2()</span>, removes attributes from closing tags, checks attribute values as per <spanclass="term">$spec</span> rules using <spanclass="term">hl_attrval()</span>, and checks URL protocols using <spanclass="term">hl_prot()</span>. <spanclass="term">htmLawed()</span> performs tag balancing and nesting checks with a call to <spanclass="term">hl_bal()</span>, and optionally compacts/beautifies the output with proper white-spacing with a call to <spanclass="term">hl_tidy()</span>. The latter temporarily replaces white-space, and <spanclass="term"><</span>, <spanclass="term">></span> and <spanclass="term">&</span> characters inside <spanclass="term">pre</span>, <spanclass="term">script</span> and <spanclass="term">textarea</span> elements, and HTML comments and CDATA sections with control characters (code-points <spanclass="term">1</span> to <spanclass="term">5</span>, and <spanclass="term">7</span>).<br/>
<br/>
  htmLawed permits the use of custom code or <strong>hook functions</strong> at two stages. The first, called inside <spanclass="term">htmLawed()</span>, allows the input text as well as the finalized $config and $spec values to be altered right after the initial processing (see <ahref="#s3.7">section 3.7</a>). The second is called by <spanclass="term">hl_tag()</span> once the tag content is finalized (see <ahref="#s3.4.9">section 3.4.9</a>).<br/>
<br/>
  Being dictated by the external and stable HTML standard, htmLawed's objective is very clear-cut and less concerned with tweakability. The code is only minimally annotated with comments -- it is not meant to instruct; PHP developers familiar with the HTML specs will see the logic, and others can always refer to the htmLawed documentation. The compact structuring of the statements is meant to aid in quickly grasping the logic, at least when viewed with code syntax highlighted.
<hr/><br/><br/><spanclass="subtle"><small>HTM version of <em><ahref="htmLawed_README.txt">htmLawed_README.txt</a></em> generated on 06 Jun, 2012 using <ahref="http://www.bioinformatics.org/phplabware/internal_utilities">rTxt2htm</a> from PHP Labware</small></span>