2006-08-16 04:14:59 +00:00
|
|
|
|
|
|
|
Install
|
2006-09-01 14:57:47 +00:00
|
|
|
How to install HTML Purifier
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
HTML Purifier is designed to run out of the box, so actually using the library
|
|
|
|
is extremely easy. (Although, if you were looking for a step-by-step
|
|
|
|
installation GUI, you've come to the wrong place!) The impatient can scroll
|
|
|
|
down to the bottom of this INSTALL document to see the code, but you really
|
|
|
|
should make sure a few things are properly done.
|
2006-08-16 04:14:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
1. Compatibility
|
2006-09-01 14:57:47 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
HTML Purifier works in both PHP 4 and PHP 5, from PHP 4.3.9 and up. It has no
|
|
|
|
core dependencies with other libraries. (Whoopee!)
|
2006-09-01 14:57:47 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
Optional extensions are iconv (usually installed) and tidy (also common).
|
|
|
|
If you use UTF-8 and don't plan on pretty-printing HTML, you can get away with
|
|
|
|
not having either of these extensions.
|
2006-09-01 14:57:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
2. Including the library
|
2006-09-01 14:57:47 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
Simply use:
|
2006-09-01 14:57:47 +00:00
|
|
|
|
2006-09-30 20:18:08 +00:00
|
|
|
require_once '/path/to/library/HTMLPurifier.auto.php';
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
...and you're good to go. Since HTML Purifier's codebase is fairly
|
|
|
|
large, I recommend only including HTML Purifier when you need it.
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
If you don't like your include_path to be fiddled around with, simply set
|
|
|
|
HTML Purifier's library/ directory to the include path yourself and then:
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-30 20:18:08 +00:00
|
|
|
require_once 'HTMLPurifier.php';
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
Only the contents in the library/ folder are necessary, so you can remove
|
|
|
|
everything else when using HTML Purifier in a production environment.
|
2006-08-16 04:14:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
3. Preparing the proper output environment
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
HTML Purifier is all about web-standards, so accordingly your webpages should
|
|
|
|
be standards compliant. HTML Purifier can deal with these doctypes:
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
* XHTML 1.0 Transitional (default)
|
|
|
|
* HTML 4.01 Transitional
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
...and these character encodings:
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
* UTF-8 (default)
|
|
|
|
* Any encoding iconv supports (support is crippled for i18n though)
|
|
|
|
|
|
|
|
The defaults are there for a reason: they are best-practice choices that
|
|
|
|
should not be changed lightly. For those of you in the dark, you can determine
|
|
|
|
the doctype from this code in your HTML documents:
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-09 21:10:04 +00:00
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
2006-09-28 00:31:12 +00:00
|
|
|
|
|
|
|
...and the character encoding from this code:
|
|
|
|
|
2006-09-09 21:10:04 +00:00
|
|
|
<meta http-equiv="Content-type" content="text/html;charset=ENCODING">
|
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
For legacy codebases these declarations may be missing. If that is the case,
|
|
|
|
STOP, and read up on character encodings and doctypes (in that order). Here
|
|
|
|
are some links:
|
|
|
|
|
|
|
|
* http://www.joelonsoftware.com/articles/Unicode.html
|
|
|
|
* http://alistapart.com/stories/doctype/
|
2006-09-16 00:37:33 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
You may currently be vulnerable to XSS and other security threats, and HTML
|
|
|
|
Purifier won't be able to fix that.
|
2006-09-16 00:37:33 +00:00
|
|
|
|
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
|
|
|
|
4. Configuration
|
2006-09-16 00:37:33 +00:00
|
|
|
|
|
|
|
HTML Purifier is designed to run out-of-the-box, but occasionally HTML
|
2006-09-28 00:31:12 +00:00
|
|
|
Purifier needs to be told what to do. If you answered no to any of these
|
|
|
|
questions, read on, otherwise, you can skip to the next section (or, if you're
|
|
|
|
into configuring things just for the heck of it, skip to 4.3).
|
|
|
|
|
|
|
|
* Am I using UTF-8?
|
|
|
|
* Am I using XHTML 1.0 Transitional?
|
|
|
|
|
|
|
|
If you answered yes to any of these questions, instantiate a configuration
|
|
|
|
object and read on:
|
|
|
|
|
|
|
|
$config = HTMLPurifier_Config::createDefault();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4.1. Setting a different character encoding
|
|
|
|
|
|
|
|
You really shouldn't use any other encoding except UTF-8, especially if you
|
|
|
|
plan to support multilingual websites (read section three for more details).
|
|
|
|
However, switching to UTF-8 is not always immediately feasible, so we can
|
|
|
|
adapt.
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
HTML Purifier uses iconv to support other character encodings, as such,
|
|
|
|
any encoding that iconv supports <http://www.gnu.org/software/libiconv/>
|
|
|
|
HTML Purifier supports with this code:
|
2006-09-01 00:54:38 +00:00
|
|
|
|
2006-09-09 21:10:04 +00:00
|
|
|
$config->set('Core', 'Encoding', /* put your encoding here */);
|
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
An example usage for Latin-1 websites (the most common encoding for English
|
|
|
|
websites):
|
2006-09-09 21:10:04 +00:00
|
|
|
|
|
|
|
$config->set('Core', 'Encoding', 'ISO-8859-1');
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
Note that HTML Purifier's support for non-Unicode encodings is crippled by the
|
|
|
|
fact that any character not supported by that encoding will be silently
|
|
|
|
dropped, EVEN if it is ampersand escaped. This is a current limitation of
|
|
|
|
HTML Purifier that we are NOT actively working to fix. Patches are welcome,
|
|
|
|
but there are so many other gotchas and problems in I18N for non-Unicode
|
|
|
|
encodings that this functionality is low priority. See
|
|
|
|
<http://ppewww.ph.gla.ac.uk/~flavell/charset/form-i18n.html> for a more
|
|
|
|
detailed lowdown on the topic.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4.2. Setting a different doctype
|
|
|
|
|
2006-09-16 00:37:33 +00:00
|
|
|
For those of you stuck using HTML 4.01 Transitional, you can disable
|
|
|
|
XHTML output like this:
|
|
|
|
|
|
|
|
$config->set('Core', 'XHTML', false);
|
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
I recommend that you use XHTML, although not as much as I recommend UTF-8. If
|
|
|
|
your HTML 4.01 page validates, good for you!
|
|
|
|
|
|
|
|
Currently, we can only guarantee transitional-complaint output, future
|
|
|
|
versions will also allow strict-compliant output.
|
|
|
|
|
|
|
|
|
2006-09-16 00:37:33 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
4.3. Other settings
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
There are more configuration directives which can be read about
|
|
|
|
here: <http://hp.jpsband.org/live/configdoc/plain.html> They're a bit boring,
|
|
|
|
but they can help out for those of you who like to exert maximum control over
|
|
|
|
your code.
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
5. Using the code
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-01 00:54:38 +00:00
|
|
|
The interface is mind-numbingly simple:
|
|
|
|
|
|
|
|
$purifier = new HTMLPurifier();
|
2006-09-28 00:31:12 +00:00
|
|
|
$clean_html = $purifier->purify( $dirty_html );
|
2006-09-01 00:54:38 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
...or, if you're using the configuration object:
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-01 00:54:38 +00:00
|
|
|
$purifier = new HTMLPurifier($config);
|
2006-09-28 00:31:12 +00:00
|
|
|
$clean_html = $purifier->purify( $dirty_html );
|
2006-08-16 04:14:59 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
That's it! For more examples, check out docs/examples/ (they aren't very
|
|
|
|
different though). Also, SLOW gives advice on what to do if HTML Purifier
|
|
|
|
is slowing down your application.
|
2006-09-09 21:10:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
6. Quick install
|
2006-09-09 21:10:04 +00:00
|
|
|
|
2006-09-16 00:37:33 +00:00
|
|
|
If your website is in UTF-8 and XHTML Transitional, use this code:
|
2006-09-09 21:10:04 +00:00
|
|
|
|
|
|
|
<?php
|
2006-09-28 00:31:12 +00:00
|
|
|
require_once '/path/to/htmlpurifier/library/HTMLPurifier.auto.php';
|
2006-09-09 21:10:04 +00:00
|
|
|
|
2006-09-28 00:31:12 +00:00
|
|
|
$purifier = new HTMLPurifier();
|
2006-09-09 21:10:04 +00:00
|
|
|
$clean_html = $purifier->purify($dirty_html);
|
2006-09-23 00:43:21 +00:00
|
|
|
?>
|
2006-09-09 21:10:04 +00:00
|
|
|
|
2006-09-16 00:37:33 +00:00
|
|
|
If your website is in a different encoding or doctype, use this code:
|
2006-09-09 21:10:04 +00:00
|
|
|
|
|
|
|
<?php
|
2006-09-28 00:31:12 +00:00
|
|
|
require_once '/path/to/htmlpurifier/library/HTMLPurifier.auto.php';
|
2006-09-09 21:10:04 +00:00
|
|
|
|
|
|
|
$config = HTMLPurifier_Config::createDefault();
|
|
|
|
$config->set('Core', 'Encoding', 'ISO-8859-1'); //replace with your encoding
|
2006-09-16 00:37:33 +00:00
|
|
|
$config->set('Core', 'XHTML', true); //replace with false if HTML 4.01
|
2006-09-09 21:10:04 +00:00
|
|
|
$purifier = new HTMLPurifier($config);
|
|
|
|
|
|
|
|
$clean_html = $purifier->purify($dirty_html);
|
|
|
|
?>
|