3 How to install HTML Purifier
5 HTML Purifier is designed to run out of the box, so actually using the library
6 is extremely easy. (Although, if you were looking for a step-by-step
7 installation GUI, you've come to the wrong place!) The impatient can scroll
8 down to the bottom of this INSTALL document to see the code, but you really
9 should make sure a few things are properly done.
14 HTML Purifier works in both PHP 4 and PHP 5, from PHP 4.3.2 and up. It has no
15 core dependencies with other libraries.
17 Optional extensions are iconv (usually installed) and tidy (also common).
18 If you use UTF-8 and don't plan on pretty-printing HTML, you can get away with
19 not having either of these extensions.
23 2. Including the library
27 require_once '/path/to/library/HTMLPurifier.auto.php';
29 ...and you're good to go. Since HTML Purifier's codebase is fairly
30 large, I recommend only including HTML Purifier when you need it.
32 If you don't like your include_path to be fiddled around with, simply set
33 HTML Purifier's library/ directory to the include path yourself and then:
35 require_once 'HTMLPurifier.php';
37 Only the contents in the library/ folder are necessary, so you can remove
38 everything else when using HTML Purifier in a production environment.
42 3. Preparing the proper output environment
44 HTML Purifier is all about web-standards, so accordingly your webpages should
45 be standards compliant. HTML Purifier can deal with these doctypes:
47 * XHTML 1.0 Transitional (default)
49 * HTML 4.01 Transitional
53 ...and these character encodings:
56 * Any encoding iconv supports (support is crippled for i18n though)
58 The defaults are there for a reason: they are best-practice choices that
59 should not be changed lightly. For those of you in the dark, you can determine
60 the doctype from this code in your HTML documents:
62 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
63 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
65 ...and the character encoding from this code:
67 <meta http-equiv="Content-type" content="text/html;charset=ENCODING">
69 For legacy codebases these declarations may be missing. If that is the case,
70 STOP, and read docs/enduser-utf8.html
72 You may currently be vulnerable to XSS and other security threats, and HTML
73 Purifier won't be able to fix that.
79 HTML Purifier is designed to run out-of-the-box, but occasionally HTML
80 Purifier needs to be told what to do. If you answered no to any of these
81 questions, read on, otherwise, you can skip to the next section (or, if you're
82 into configuring things just for the heck of it, skip to 4.3).
85 * Am I using XHTML 1.0 Transitional?
87 If you answered no to any of these questions, instantiate a configuration
90 $config = HTMLPurifier_Config::createDefault();
94 4.1. Setting a different character encoding
96 You really shouldn't use any other encoding except UTF-8, especially if you
97 plan to support multilingual websites (read section three for more details).
98 However, switching to UTF-8 is not always immediately feasible, so we can
101 HTML Purifier uses iconv to support other character encodings, as such,
102 any encoding that iconv supports <http://www.gnu.org/software/libiconv/>
103 HTML Purifier supports with this code:
105 $config->set('Core', 'Encoding', /* put your encoding here */);
107 An example usage for Latin-1 websites (the most common encoding for English
110 $config->set('Core', 'Encoding', 'ISO-8859-1');
112 Note that HTML Purifier's support for non-Unicode encodings is crippled by the
113 fact that any character not supported by that encoding will be silently
114 dropped, EVEN if it is ampersand escaped. If you want to work around
115 this, you are welcome to read docs/enduser-utf8.html for a workaround,
116 but please be cognizant of the issues the "solution" creates.
120 4.2. Setting a different doctype
122 For those of you using HTML 4.01 Transitional, you can disable
123 XHTML output like this:
125 $config->set('HTML', 'Doctype', 'HTML 4.01 Transitional');
127 Other supported doctypes include:
130 * HTML 4.01 Transitional
132 * XHTML 1.0 Transitional
139 There are more configuration directives which can be read about
140 here: <http://htmlpurifier.org/live/configdoc/plain.html> They're a bit boring,
141 but they can help out for those of you who like to exert maximum control over
148 The interface is mind-numbingly simple:
150 $purifier = new HTMLPurifier();
151 $clean_html = $purifier->purify( $dirty_html );
153 ...or, if you're using the configuration object:
155 $purifier = new HTMLPurifier($config);
156 $clean_html = $purifier->purify( $dirty_html );
158 That's it! For more examples, check out docs/examples/ (they aren't very
159 different though). Also, SLOW gives advice on what to do if HTML Purifier
160 is slowing down your application.
166 If your website is in UTF-8 and XHTML Transitional, use this code:
169 require_once '/path/to/htmlpurifier/library/HTMLPurifier.auto.php';
171 $purifier = new HTMLPurifier();
172 $clean_html = $purifier->purify($dirty_html);
175 If your website is in a different encoding or doctype, use this code:
178 require_once '/path/to/htmlpurifier/library/HTMLPurifier.auto.php';
180 $config = HTMLPurifier_Config::createDefault();
181 $config->set('Core', 'Encoding', 'ISO-8859-1'); // replace with your encoding
182 $config->set('HTML', 'Doctype', 'HTML 4.01 Transitional'); // replace with your doctype
183 $purifier = new HTMLPurifier($config);
185 $clean_html = $purifier->purify($dirty_html);
192 HTML Purifier generates some cache files to speed up its execution. For
193 maximum performance, make sure that library/HTMLPurifier/DefinitionCache/Serializer
194 is writeable by the webserver.