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
51 * XHTML 1.1 (sans Ruby)
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 fix,
116 but please be cognizant of the issues the "solution" creates (for this
117 reason, I do not include the solution in this document).
121 4.2. Setting a different doctype
123 For those of you using HTML 4.01 Transitional, you can disable
124 XHTML output like this:
126 $config->set('HTML', 'Doctype', 'HTML 4.01 Transitional');
128 Other supported doctypes include:
131 * HTML 4.01 Transitional
133 * XHTML 1.0 Transitional
140 There are more configuration directives which can be read about
141 here: <http://htmlpurifier.org/live/configdoc/plain.html> They're a bit boring,
142 but they can help out for those of you who like to exert maximum control over
143 your code. Some of the more interesting ones are configurable at the
144 demo <http://htmlpurifier.org/demo.php> and are well worth looking into
151 The interface is mind-numbingly simple:
153 $purifier = new HTMLPurifier();
154 $clean_html = $purifier->purify( $dirty_html );
156 ...or, if you're using the configuration object:
158 $purifier = new HTMLPurifier($config);
159 $clean_html = $purifier->purify( $dirty_html );
161 That's it! For more examples, check out docs/examples/ (they aren't very
162 different though). Also, docs/enduser-slow.html gives advice on what to
163 do if HTML Purifier is slowing down your application.
169 First, make sure library/HTMLPurifier/DefinitionCache/Serializer is
170 writable by the webserver (see Section 7: Caching below for details).
171 If your website is in UTF-8 and XHTML Transitional, use this code:
174 require_once '/path/to/htmlpurifier/library/HTMLPurifier.auto.php';
176 $purifier = new HTMLPurifier();
177 $clean_html = $purifier->purify($dirty_html);
180 If your website is in a different encoding or doctype, use this code:
183 require_once '/path/to/htmlpurifier/library/HTMLPurifier.auto.php';
185 $config = HTMLPurifier_Config::createDefault();
186 $config->set('Core', 'Encoding', 'ISO-8859-1'); // replace with your encoding
187 $config->set('HTML', 'Doctype', 'HTML 4.01 Transitional'); // replace with your doctype
188 $purifier = new HTMLPurifier($config);
190 $clean_html = $purifier->purify($dirty_html);
197 HTML Purifier generates some cache files (generally one or two) to speed up
198 its execution. For maximum performance, make sure that
199 library/HTMLPurifier/DefinitionCache/Serializer is writeable by the webserver.
201 If you are in the library/ folder of HTML Purifier, you can set the
202 appropriate permissions using:
204 chmod -R 0755 HTMLPurifier/DefinitionCache/Serializer
206 If the above command doesn't work, you may need to assign write permissions
207 to all. This may be necessary if your webserver runs as nobody, but is
208 not recommended since it means any other user can write files in the
211 chmod -R 0777 HTMLPurifier/DefinitionCache/Serializer
213 You can also chmod files via your FTP client; this option
214 is usually accessible by right clicking the corresponding directory and
215 then selecting "chmod" or "file permissions".
217 Starting with 2.0.1, HTML Purifier will generate friendly error messages
218 that will tell you exactly what you have to chmod the directory to, if in doubt,
221 If you are unable or unwilling to give write permissions to the cache
222 directory, you can either disable the cache (and suffer a performance
225 $config->set('Core', 'DefinitionCache', null);
227 Or move the cache directory somewhere else (no trailing slash):
229 $config->set('Cache', 'SerializerPath', '/home/user/absolute/path');