[1.2.0]
[htmlpurifier.git] / INSTALL
blob52991e338ea754d9fe7f21ff64d24c188bafe472
2 Install
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.
13 1.  Compatibility
15 HTML Purifier works in both PHP 4 and PHP 5, from PHP 4.3.9 and up. It has no
16 core dependencies with other libraries. (Whoopee!)
18 Optional extensions are iconv (usually installed) and tidy (also common).
19 If you use UTF-8 and don't plan on pretty-printing HTML, you can get away with
20 not having either of these extensions.
24 2.  Including the library
26 Simply use:
28     require_once '/path/to/library/HTMLPurifier.auto.php';
30 ...and you're good to go.  Since HTML Purifier's codebase is fairly
31 large, I recommend only including HTML Purifier when you need it.
33 If you don't like your include_path to be fiddled around with, simply set
34 HTML Purifier's library/ directory to the include path yourself and then:
36     require_once 'HTMLPurifier.php';
38 Only the contents in the library/ folder are necessary, so you can remove
39 everything else when using HTML Purifier in a production environment.  
43 3.  Preparing the proper output environment
45 HTML Purifier is all about web-standards, so accordingly your webpages should
46 be standards compliant.  HTML Purifier can deal with these doctypes:
48 * XHTML 1.0 Transitional (default)
49 * HTML 4.01 Transitional
51 ...and these character encodings:
53 * UTF-8 (default)
54 * Any encoding iconv supports (support is crippled for i18n though)
56 The defaults are there for a reason: they are best-practice choices that
57 should not be changed lightly.  For those of you in the dark, you can determine
58 the doctype from this code in your HTML documents:
60     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
61         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
63 ...and the character encoding from this code:
65     <meta http-equiv="Content-type" content="text/html;charset=ENCODING">
67 For legacy codebases these declarations may be missing.  If that is the case,
68 STOP, and read up on character encodings and doctypes (in that order).  Here
69 are some links:
71 * http://www.joelonsoftware.com/articles/Unicode.html
72 * http://alistapart.com/stories/doctype/
74 You may currently be vulnerable to XSS and other security threats, and HTML
75 Purifier won't be able to fix that.
79 4. Configuration
81 HTML Purifier is designed to run out-of-the-box, but occasionally HTML
82 Purifier needs to be told what to do.  If you answered no to any of these
83 questions, read on, otherwise, you can skip to the next section (or, if you're
84 into configuring things just for the heck of it, skip to 4.3).
86 * Am I using UTF-8?
87 * Am I using XHTML 1.0 Transitional?
89 If you answered yes to any of these questions, instantiate a configuration
90 object and read on:
92     $config = HTMLPurifier_Config::createDefault();
96 4.1. Setting a different character encoding
98 You really shouldn't use any other encoding except UTF-8, especially if you
99 plan to support multilingual websites (read section three for more details).
100 However, switching to UTF-8 is not always immediately feasible, so we can
101 adapt.
103 HTML Purifier uses iconv to support other character encodings, as such,
104 any encoding that iconv supports <http://www.gnu.org/software/libiconv/>
105 HTML Purifier supports with this code:
107     $config->set('Core', 'Encoding', /* put your encoding here */);
109 An example usage for Latin-1 websites (the most common encoding for English
110 websites):
112     $config->set('Core', 'Encoding', 'ISO-8859-1');
114 Note that HTML Purifier's support for non-Unicode encodings is crippled by the
115 fact that any character not supported by that encoding will be silently
116 dropped, EVEN if it is ampersand escaped.  This is a current limitation of
117 HTML Purifier that we are NOT actively working to fix.  Patches are welcome,
118 but there are so many other gotchas and problems in I18N for non-Unicode
119 encodings that this functionality is low priority.  See
120 <http://ppewww.ph.gla.ac.uk/~flavell/charset/form-i18n.html> for a more
121 detailed lowdown on the topic.
125 4.2. Setting a different doctype
127 For those of you stuck using HTML 4.01 Transitional, you can disable
128 XHTML output like this:
130     $config->set('Core', 'XHTML', false);
132 I recommend that you use XHTML, although not as much as I recommend UTF-8.  If
133 your HTML 4.01 page validates, good for you!
135 Currently, we can only guarantee transitional-complaint output, future
136 versions will also allow strict-compliant output.
140 4.3. Other settings
142 There are more configuration directives which can be read about
143 here: <http://hp.jpsband.org/live/configdoc/plain.html>  They're a bit boring,
144 but they can help out for those of you who like to exert maximum control over
145 your code.
149 5.   Using the code
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, SLOW gives advice on what to do if HTML Purifier
163 is slowing down your application.
167 6.   Quick install
169 If your website is in UTF-8 and XHTML Transitional, use this code:
171 <?php
172     require_once '/path/to/htmlpurifier/library/HTMLPurifier.auto.php';
173     
174     $purifier = new HTMLPurifier();
175     $clean_html = $purifier->purify($dirty_html);
178 If your website is in a different encoding or doctype, use this code:
180 <?php
181     require_once '/path/to/htmlpurifier/library/HTMLPurifier.auto.php';
182     
183     $config = HTMLPurifier_Config::createDefault();
184     $config->set('Core', 'Encoding', 'ISO-8859-1'); //replace with your encoding
185     $config->set('Core', 'XHTML', true); //replace with false if HTML 4.01
186     $purifier = new HTMLPurifier($config);
187     
188     $clean_html = $purifier->purify($dirty_html);