You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

375 lines
14KB

  1. Install
  2. How to install HTML Purifier
  3. HTML Purifier is designed to run out of the box, so actually using the
  4. library is extremely easy. (Although... if you were looking for a
  5. step-by-step installation GUI, you've downloaded the wrong software!)
  6. While the impatient can get going immediately with some of the sample
  7. code at the bottom of this library, it's well worth reading this entire
  8. document--most of the other documentation assumes that you are familiar
  9. with these contents.
  10. ---------------------------------------------------------------------------
  11. 1. Compatibility
  12. HTML Purifier is PHP 5 only, and is actively tested from PHP 5.0.5 and
  13. up. It has no core dependencies with other libraries. PHP
  14. 4 support was deprecated on December 31, 2007 with HTML Purifier 3.0.0.
  15. HTML Purifier is not compatible with zend.ze1_compatibility_mode.
  16. These optional extensions can enhance the capabilities of HTML Purifier:
  17. * iconv : Converts text to and from non-UTF-8 encodings
  18. * bcmath : Used for unit conversion and imagecrash protection
  19. * tidy : Used for pretty-printing HTML
  20. These optional libraries can enhance the capabilities of HTML Purifier:
  21. * CSSTidy : Clean CSS stylesheets using %Core.ExtractStyleBlocks
  22. * Net_IDNA2 (PEAR) : IRI support using %Core.EnableIDNA
  23. ---------------------------------------------------------------------------
  24. 2. Reconnaissance
  25. A big plus of HTML Purifier is its inerrant support of standards, so
  26. your web-pages should be standards-compliant. (They should also use
  27. semantic markup, but that's another issue altogether, one HTML Purifier
  28. cannot fix without reading your mind.)
  29. HTML Purifier can process these doctypes:
  30. * XHTML 1.0 Transitional (default)
  31. * XHTML 1.0 Strict
  32. * HTML 4.01 Transitional
  33. * HTML 4.01 Strict
  34. * XHTML 1.1
  35. ...and these character encodings:
  36. * UTF-8 (default)
  37. * Any encoding iconv supports (with crippled internationalization support)
  38. These defaults reflect what my choices would be if I were authoring an
  39. HTML document, however, what you choose depends on the nature of your
  40. codebase. If you don't know what doctype you are using, you can determine
  41. the doctype from this identifier at the top of your source code:
  42. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  43. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  44. ...and the character encoding from this code:
  45. <meta http-equiv="Content-type" content="text/html;charset=ENCODING">
  46. If the character encoding declaration is missing, STOP NOW, and
  47. read 'docs/enduser-utf8.html' (web accessible at
  48. http://htmlpurifier.org/docs/enduser-utf8.html). In fact, even if it is
  49. present, read this document anyway, as many websites specify their
  50. document's character encoding incorrectly.
  51. ---------------------------------------------------------------------------
  52. 3. Including the library
  53. The procedure is quite simple:
  54. require_once '/path/to/library/HTMLPurifier.auto.php';
  55. This will setup an autoloader, so the library's files are only included
  56. when you use them.
  57. Only the contents in the library/ folder are necessary, so you can remove
  58. everything else when using HTML Purifier in a production environment.
  59. If you installed HTML Purifier via PEAR, all you need to do is:
  60. require_once 'HTMLPurifier.auto.php';
  61. Please note that the usual PEAR practice of including just the classes you
  62. want will not work with HTML Purifier's autoloading scheme.
  63. Advanced users, read on; other users can skip to section 4.
  64. Autoload compatibility
  65. ----------------------
  66. HTML Purifier attempts to be as smart as possible when registering an
  67. autoloader, but there are some cases where you will need to change
  68. your own code to accomodate HTML Purifier. These are those cases:
  69. PHP VERSION IS LESS THAN 5.1.2, AND YOU'VE DEFINED __autoload
  70. Because spl_autoload_register() doesn't exist in early versions
  71. of PHP 5, HTML Purifier has no way of adding itself to the autoload
  72. stack. Modify your __autoload function to test
  73. HTMLPurifier_Bootstrap::autoload($class)
  74. For example, suppose your autoload function looks like this:
  75. function __autoload($class) {
  76. require str_replace('_', '/', $class) . '.php';
  77. return true;
  78. }
  79. A modified version with HTML Purifier would look like this:
  80. function __autoload($class) {
  81. if (HTMLPurifier_Bootstrap::autoload($class)) return true;
  82. require str_replace('_', '/', $class) . '.php';
  83. return true;
  84. }
  85. Note that there *is* some custom behavior in our autoloader; the
  86. original autoloader in our example would work for 99% of the time,
  87. but would fail when including language files.
  88. AN __autoload FUNCTION IS DECLARED AFTER OUR AUTOLOADER IS REGISTERED
  89. spl_autoload_register() has the curious behavior of disabling
  90. the existing __autoload() handler. Users need to explicitly
  91. spl_autoload_register('__autoload'). Because we use SPL when it
  92. is available, __autoload() will ALWAYS be disabled. If __autoload()
  93. is declared before HTML Purifier is loaded, this is not a problem:
  94. HTML Purifier will register the function for you. But if it is
  95. declared afterwards, it will mysteriously not work. This
  96. snippet of code (after your autoloader is defined) will fix it:
  97. spl_autoload_register('__autoload')
  98. Users should also be on guard if they use a version of PHP previous
  99. to 5.1.2 without an autoloader--HTML Purifier will define __autoload()
  100. for you, which can collide with an autoloader that was added by *you*
  101. later.
  102. For better performance
  103. ----------------------
  104. Opcode caches, which greatly speed up PHP initialization for scripts
  105. with large amounts of code (HTML Purifier included), don't like
  106. autoloaders. We offer an include file that includes all of HTML Purifier's
  107. files in one go in an opcode cache friendly manner:
  108. // If /path/to/library isn't already in your include path, uncomment
  109. // the below line:
  110. // require '/path/to/library/HTMLPurifier.path.php';
  111. require 'HTMLPurifier.includes.php';
  112. Optional components still need to be included--you'll know if you try to
  113. use a feature and you get a class doesn't exists error! The autoloader
  114. can be used in conjunction with this approach to catch classes that are
  115. missing. Simply add this afterwards:
  116. require 'HTMLPurifier.autoload.php';
  117. Standalone version
  118. ------------------
  119. HTML Purifier has a standalone distribution; you can also generate
  120. a standalone file from the full version by running the script
  121. maintenance/generate-standalone.php . The standalone version has the
  122. benefit of having most of its code in one file, so parsing is much
  123. faster and the library is easier to manage.
  124. If HTMLPurifier.standalone.php exists in the library directory, you
  125. can use it like this:
  126. require '/path/to/HTMLPurifier.standalone.php';
  127. This is equivalent to including HTMLPurifier.includes.php, except that
  128. the contents of standalone/ will be added to your path. To override this
  129. behavior, specify a new HTMLPURIFIER_PREFIX where standalone files can
  130. be found (usually, this will be one directory up, the "true" library
  131. directory in full distributions). Don't forget to set your path too!
  132. The autoloader can be added to the end to ensure the classes are
  133. loaded when necessary; otherwise you can manually include them.
  134. To use the autoloader, use this:
  135. require 'HTMLPurifier.autoload.php';
  136. For advanced users
  137. ------------------
  138. HTMLPurifier.auto.php performs a number of operations that can be done
  139. individually. These are:
  140. HTMLPurifier.path.php
  141. Puts /path/to/library in the include path. For high performance,
  142. this should be done in php.ini.
  143. HTMLPurifier.autoload.php
  144. Registers our autoload handler HTMLPurifier_Bootstrap::autoload($class).
  145. You can do these operations by yourself--in fact, you must modify your own
  146. autoload handler if you are using a version of PHP earlier than PHP 5.1.2
  147. (See "Autoload compatibility" above).
  148. ---------------------------------------------------------------------------
  149. 4. Configuration
  150. HTML Purifier is designed to run out-of-the-box, but occasionally HTML
  151. Purifier needs to be told what to do. If you answer no to any of these
  152. questions, read on; otherwise, you can skip to the next section (or, if you're
  153. into configuring things just for the heck of it, skip to 4.3).
  154. * Am I using UTF-8?
  155. * Am I using XHTML 1.0 Transitional?
  156. If you answered no to any of these questions, instantiate a configuration
  157. object and read on:
  158. $config = HTMLPurifier_Config::createDefault();
  159. 4.1. Setting a different character encoding
  160. You really shouldn't use any other encoding except UTF-8, especially if you
  161. plan to support multilingual websites (read section three for more details).
  162. However, switching to UTF-8 is not always immediately feasible, so we can
  163. adapt.
  164. HTML Purifier uses iconv to support other character encodings, as such,
  165. any encoding that iconv supports <http://www.gnu.org/software/libiconv/>
  166. HTML Purifier supports with this code:
  167. $config->set('Core.Encoding', /* put your encoding here */);
  168. An example usage for Latin-1 websites (the most common encoding for English
  169. websites):
  170. $config->set('Core.Encoding', 'ISO-8859-1');
  171. Note that HTML Purifier's support for non-Unicode encodings is crippled by the
  172. fact that any character not supported by that encoding will be silently
  173. dropped, EVEN if it is ampersand escaped. If you want to work around
  174. this, you are welcome to read docs/enduser-utf8.html for a fix,
  175. but please be cognizant of the issues the "solution" creates (for this
  176. reason, I do not include the solution in this document).
  177. 4.2. Setting a different doctype
  178. For those of you using HTML 4.01 Transitional, you can disable
  179. XHTML output like this:
  180. $config->set('HTML.Doctype', 'HTML 4.01 Transitional');
  181. Other supported doctypes include:
  182. * HTML 4.01 Strict
  183. * HTML 4.01 Transitional
  184. * XHTML 1.0 Strict
  185. * XHTML 1.0 Transitional
  186. * XHTML 1.1
  187. 4.3. Other settings
  188. There are more configuration directives which can be read about
  189. here: <http://htmlpurifier.org/live/configdoc/plain.html> They're a bit boring,
  190. but they can help out for those of you who like to exert maximum control over
  191. your code. Some of the more interesting ones are configurable at the
  192. demo <http://htmlpurifier.org/demo.php> and are well worth looking into
  193. for your own system.
  194. For example, you can fine tune allowed elements and attributes, convert
  195. relative URLs to absolute ones, and even autoparagraph input text! These
  196. are, respectively, %HTML.Allowed, %URI.MakeAbsolute and %URI.Base, and
  197. %AutoFormat.AutoParagraph. The %Namespace.Directive naming convention
  198. translates to:
  199. $config->set('Namespace.Directive', $value);
  200. E.g.
  201. $config->set('HTML.Allowed', 'p,b,a[href],i');
  202. $config->set('URI.Base', 'http://www.example.com');
  203. $config->set('URI.MakeAbsolute', true);
  204. $config->set('AutoFormat.AutoParagraph', true);
  205. ---------------------------------------------------------------------------
  206. 5. Caching
  207. HTML Purifier generates some cache files (generally one or two) to speed up
  208. its execution. For maximum performance, make sure that
  209. library/HTMLPurifier/DefinitionCache/Serializer is writeable by the webserver.
  210. If you are in the library/ folder of HTML Purifier, you can set the
  211. appropriate permissions using:
  212. chmod -R 0755 HTMLPurifier/DefinitionCache/Serializer
  213. If the above command doesn't work, you may need to assign write permissions
  214. to all. This may be necessary if your webserver runs as nobody, but is
  215. not recommended since it means any other user can write files in the
  216. directory. Use:
  217. chmod -R 0777 HTMLPurifier/DefinitionCache/Serializer
  218. You can also chmod files via your FTP client; this option
  219. is usually accessible by right clicking the corresponding directory and
  220. then selecting "chmod" or "file permissions".
  221. Starting with 2.0.1, HTML Purifier will generate friendly error messages
  222. that will tell you exactly what you have to chmod the directory to, if in doubt,
  223. follow its advice.
  224. If you are unable or unwilling to give write permissions to the cache
  225. directory, you can either disable the cache (and suffer a performance
  226. hit):
  227. $config->set('Core.DefinitionCache', null);
  228. Or move the cache directory somewhere else (no trailing slash):
  229. $config->set('Cache.SerializerPath', '/home/user/absolute/path');
  230. ---------------------------------------------------------------------------
  231. 6. Using the code
  232. The interface is mind-numbingly simple:
  233. $purifier = new HTMLPurifier($config);
  234. $clean_html = $purifier->purify( $dirty_html );
  235. That's it! For more examples, check out docs/examples/ (they aren't very
  236. different though). Also, docs/enduser-slow.html gives advice on what to
  237. do if HTML Purifier is slowing down your application.
  238. ---------------------------------------------------------------------------
  239. 7. Quick install
  240. First, make sure library/HTMLPurifier/DefinitionCache/Serializer is
  241. writable by the webserver (see Section 5: Caching above for details).
  242. If your website is in UTF-8 and XHTML Transitional, use this code:
  243. <?php
  244. require_once '/path/to/htmlpurifier/library/HTMLPurifier.auto.php';
  245. $config = HTMLPurifier_Config::createDefault();
  246. $purifier = new HTMLPurifier($config);
  247. $clean_html = $purifier->purify($dirty_html);
  248. ?>
  249. If your website is in a different encoding or doctype, use this code:
  250. <?php
  251. require_once '/path/to/htmlpurifier/library/HTMLPurifier.auto.php';
  252. $config = HTMLPurifier_Config::createDefault();
  253. $config->set('Core.Encoding', 'ISO-8859-1'); // replace with your encoding
  254. $config->set('HTML.Doctype', 'HTML 4.01 Transitional'); // replace with your doctype
  255. $purifier = new HTMLPurifier($config);
  256. $clean_html = $purifier->purify($dirty_html);
  257. ?>
  258. vim: et sw=4 sts=4