Source for file HTMLPurifier.php

Documentation is available at HTMLPurifier.php

  1. <?php
  2.  
  3. /*! @mainpage
  4.  * 
  5.  * HTML Purifier is an HTML filter that will take an arbitrary snippet of
  6.  * HTML and rigorously test, validate and filter it into a version that
  7.  * is safe for output onto webpages. It achieves this by:
  8.  * 
  9.  *  -# Lexing (parsing into tokens) the document,
  10.  *  -# Executing various strategies on the tokens:
  11.  *      -# Removing all elements not in the whitelist,
  12.  *      -# Making the tokens well-formed,
  13.  *      -# Fixing the nesting of the nodes, and
  14.  *      -# Validating attributes of the nodes; and
  15.  *  -# Generating HTML from the purified tokens.
  16.  * 
  17.  * However, most users will only need to interface with the HTMLPurifier
  18.  * and HTMLPurifier_Config.
  19.  */
  20.  
  21. /*
  22.     HTML Purifier 3.1.1 - Standards Compliant HTML Filtering
  23.     Copyright (C) 2006-2008 Edward Z. Yang
  24.  
  25.     This library is free software; you can redistribute it and/or
  26.     modify it under the terms of the GNU Lesser General Public
  27.     License as published by the Free Software Foundation; either
  28.     version 2.1 of the License, or (at your option) any later version.
  29.  
  30.     This library is distributed in the hope that it will be useful,
  31.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  32.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  33.     Lesser General Public License for more details.
  34.  
  35.     You should have received a copy of the GNU Lesser General Public
  36.     License along with this library; if not, write to the Free Software
  37.     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  38.  */
  39.  
  40. /**
  41.  * Facade that coordinates HTML Purifier's subsystems in order to purify HTML.
  42.  * 
  43.  * @note There are several points in which configuration can be specified
  44.  *        for HTML Purifier.  The precedence of these (from lowest to
  45.  *        highest) is as follows:
  46.  *           -# Instance: new HTMLPurifier($config)
  47.  *           -# Invocation: purify($html, $config)
  48.  *        These configurations are entirely independent of each other and
  49.  *        are *not* merged (this behavior may change in the future).
  50.  * 
  51.  * @todo We need an easier way to inject strategies using the configuration
  52.  *        object.
  53.  */
  54. {
  55.     
  56.     /** Version of HTML Purifier */
  57.     public $version = '3.1.1';
  58.     
  59.     /** Constant with version of HTML Purifier */
  60.     const VERSION '3.1.1';
  61.     
  62.     /** Global configuration object */
  63.     public $config;
  64.     
  65.     /** Array of extra HTMLPurifier_Filter objects to run on HTML, for backwards compatibility */
  66.     private $filters array();
  67.     
  68.     /** Single instance of HTML Purifier */
  69.     private static $instance;
  70.     
  71.     protected $strategy$generator;
  72.     
  73.     /**
  74.      * Resultant HTMLPurifier_Context of last run purification. Is an array
  75.      * of contexts if the last called method was purifyArray().
  76.      */
  77.     public $context;
  78.     
  79.     /**
  80.      * Initializes the purifier.
  81.      * @param $config Optional HTMLPurifier_Config object for all instances of
  82.      *                 the purifier, if omitted, a default configuration is
  83.      *                 supplied (which can be overridden on a per-use basis).
  84.      *                 The parameter can also be any type that
  85.      *                 HTMLPurifier_Config::create() supports.
  86.      */
  87.     public function __construct($config null{
  88.         
  89.         $this->config = HTMLPurifier_Config::create($config);
  90.         
  91.         $this->strategy     = new HTMLPurifier_Strategy_Core();
  92.         
  93.     }
  94.     
  95.     /**
  96.      * Adds a filter to process the output. First come first serve
  97.      * @param $filter HTMLPurifier_Filter object
  98.      */
  99.     public function addFilter($filter{
  100.         trigger_error('HTMLPurifier->addFilter() is deprecated, use configuration directives in the Filter namespace or Filter.Custom'E_USER_WARNING);
  101.         $this->filters[$filter;
  102.     }
  103.     
  104.     /**
  105.      * Filters an HTML snippet/document to be XSS-free and standards-compliant.
  106.      * 
  107.      * @param $html String of HTML to purify
  108.      * @param $config HTMLPurifier_Config object for this operation, if omitted,
  109.      *                 defaults to the config object specified during this
  110.      *                 object's construction. The parameter can also be any type
  111.      *                 that HTMLPurifier_Config::create() supports.
  112.      * @return Purified HTML
  113.      */
  114.     public function purify($html$config null{
  115.         
  116.         // :TODO: make the config merge in, instead of replace
  117.         $config $config HTMLPurifier_Config::create($config$this->config;
  118.         
  119.         // implementation is partially environment dependant, partially
  120.         // configuration dependant
  121.         $lexer HTMLPurifier_Lexer::create($config);
  122.         
  123.         $context new HTMLPurifier_Context();
  124.         
  125.         // setup HTML generator
  126.         $this->generator = new HTMLPurifier_Generator($config$context);
  127.         $context->register('Generator'$this->generator);
  128.         
  129.         // set up global context variables
  130.         if ($config->get('Core''CollectErrors')) {
  131.             // may get moved out if other facilities use it
  132.             $language_factory HTMLPurifier_LanguageFactory::instance();
  133.             $language $language_factory->create($config$context);
  134.             $context->register('Locale'$language);
  135.             
  136.             $error_collector new HTMLPurifier_ErrorCollector($context);
  137.             $context->register('ErrorCollector'$error_collector);
  138.         }
  139.         
  140.         // setup id_accumulator context, necessary due to the fact that
  141.         // AttrValidator can be called from many places
  142.         $id_accumulator HTMLPurifier_IDAccumulator::build($config$context);
  143.         $context->register('IDAccumulator'$id_accumulator);
  144.         
  145.         $html HTMLPurifier_Encoder::convertToUTF8($html$config$context);
  146.         
  147.         // setup filters
  148.         $filter_flags $config->getBatch('Filter');
  149.         $custom_filters $filter_flags['Custom'];
  150.         unset($filter_flags['Custom']);
  151.         $filters array();
  152.         foreach ($filter_flags as $filter => $flag{
  153.             if (!$flagcontinue;
  154.             $class "HTMLPurifier_Filter_$filter";
  155.             $filters[new $class;
  156.         }
  157.         foreach ($custom_filters as $filter{
  158.             // maybe "HTMLPurifier_Filter_$filter", but be consistent with AutoFormat
  159.             $filters[$filter;
  160.         }
  161.         $filters array_merge($filters$this->filters);
  162.         // maybe prepare(), but later
  163.         
  164.         for ($i 0$filter_size count($filters)$i $filter_size$i++{
  165.             $html $filters[$i]->preFilter($html$config$context);
  166.         }
  167.         
  168.         // purified HTML
  169.         $html 
  170.             $this->generator->generateFromTokens(
  171.                 // list of tokens
  172.                 $this->strategy->execute(
  173.                     // list of un-purified tokens
  174.                     $lexer->tokenizeHTML(
  175.                         // un-purified HTML
  176.                         $html$config$context
  177.                     ),
  178.                     $config$context
  179.                 )
  180.             );
  181.         
  182.         for ($i $filter_size 1$i >= 0$i--{
  183.             $html $filters[$i]->postFilter($html$config$context);
  184.         }
  185.         
  186.         $html HTMLPurifier_Encoder::convertFromUTF8($html$config$context);
  187.         $this->context =$context;
  188.         return $html;
  189.     }
  190.     
  191.     /**
  192.      * Filters an array of HTML snippets
  193.      * @param $config Optional HTMLPurifier_Config object for this operation.
  194.      *                 See HTMLPurifier::purify() for more details.
  195.      * @return Array of purified HTML
  196.      */
  197.     public function purifyArray($array_of_html$config null{
  198.         $context_array array();
  199.         foreach ($array_of_html as $key => $html{
  200.             $array_of_html[$key$this->purify($html$config);
  201.             $context_array[$key$this->context;
  202.         }
  203.         $this->context = $context_array;
  204.         return $array_of_html;
  205.     }
  206.     
  207.     /**
  208.      * Singleton for enforcing just one HTML Purifier in your system
  209.      * @param $prototype Optional prototype HTMLPurifier instance to
  210.      *                    overload singleton with, or HTMLPurifier_Config
  211.      *                    instance to configure the generated version with.
  212.      */
  213.     public static function instance($prototype null{
  214.         if (!self::$instance || $prototype{
  215.             if ($prototype instanceof HTMLPurifier{
  216.                 self::$instance $prototype;
  217.             elseif ($prototype{
  218.                 self::$instance new HTMLPurifier($prototype);
  219.             else {
  220.                 self::$instance new HTMLPurifier();
  221.             }
  222.         }
  223.         return self::$instance;
  224.     }
  225.     
  226.     /**
  227.      * @note Backwards compatibility, see instance()
  228.      */
  229.     public static function getInstance($prototype null{
  230.         return HTMLPurifier::instance($prototype);
  231.     }
  232.     
  233. }

Documentation generated on Thu, 19 Jun 2008 18:49:33 -0400 by phpDocumentor 1.4.2