vendor/ezyang/htmlpurifier/library/HTMLPurifier/HTMLModuleManager.php line 288

Open in your IDE?
  1. <?php
  2. class HTMLPurifier_HTMLModuleManager
  3. {
  4.     /**
  5.      * @type HTMLPurifier_DoctypeRegistry
  6.      */
  7.     public $doctypes;
  8.     /**
  9.      * Instance of current doctype.
  10.      * @type string
  11.      */
  12.     public $doctype;
  13.     /**
  14.      * @type HTMLPurifier_AttrTypes
  15.      */
  16.     public $attrTypes;
  17.     /**
  18.      * Active instances of modules for the specified doctype are
  19.      * indexed, by name, in this array.
  20.      * @type HTMLPurifier_HTMLModule[]
  21.      */
  22.     public $modules = array();
  23.     /**
  24.      * Array of recognized HTMLPurifier_HTMLModule instances,
  25.      * indexed by module's class name. This array is usually lazy loaded, but a
  26.      * user can overload a module by pre-emptively registering it.
  27.      * @type HTMLPurifier_HTMLModule[]
  28.      */
  29.     public $registeredModules = array();
  30.     /**
  31.      * List of extra modules that were added by the user
  32.      * using addModule(). These get unconditionally merged into the current doctype, whatever
  33.      * it may be.
  34.      * @type HTMLPurifier_HTMLModule[]
  35.      */
  36.     public $userModules = array();
  37.     /**
  38.      * Associative array of element name to list of modules that have
  39.      * definitions for the element; this array is dynamically filled.
  40.      * @type array
  41.      */
  42.     public $elementLookup = array();
  43.     /**
  44.      * List of prefixes we should use for registering small names.
  45.      * @type array
  46.      */
  47.     public $prefixes = array('HTMLPurifier_HTMLModule_');
  48.     /**
  49.      * @type HTMLPurifier_ContentSets
  50.      */
  51.     public $contentSets;
  52.     /**
  53.      * @type HTMLPurifier_AttrCollections
  54.      */
  55.     public $attrCollections;
  56.     /**
  57.      * If set to true, unsafe elements and attributes will be allowed.
  58.      * @type bool
  59.      */
  60.     public $trusted false;
  61.     public function __construct()
  62.     {
  63.         // editable internal objects
  64.         $this->attrTypes = new HTMLPurifier_AttrTypes();
  65.         $this->doctypes  = new HTMLPurifier_DoctypeRegistry();
  66.         // setup basic modules
  67.         $common = array(
  68.             'CommonAttributes''Text''Hypertext''List',
  69.             'Presentation''Edit''Bdo''Tables''Image',
  70.             'StyleAttribute',
  71.             // Unsafe:
  72.             'Scripting''Object''Forms',
  73.             // Sorta legacy, but present in strict:
  74.             'Name',
  75.         );
  76.         $transitional = array('Legacy''Target''Iframe');
  77.         $xml = array('XMLCommonAttributes');
  78.         $non_xml = array('NonXMLCommonAttributes');
  79.         // setup basic doctypes
  80.         $this->doctypes->register(
  81.             'HTML 4.01 Transitional',
  82.             false,
  83.             array_merge($common$transitional$non_xml),
  84.             array('Tidy_Transitional''Tidy_Proprietary'),
  85.             array(),
  86.             '-//W3C//DTD HTML 4.01 Transitional//EN',
  87.             'http://www.w3.org/TR/html4/loose.dtd'
  88.         );
  89.         $this->doctypes->register(
  90.             'HTML 4.01 Strict',
  91.             false,
  92.             array_merge($common$non_xml),
  93.             array('Tidy_Strict''Tidy_Proprietary''Tidy_Name'),
  94.             array(),
  95.             '-//W3C//DTD HTML 4.01//EN',
  96.             'http://www.w3.org/TR/html4/strict.dtd'
  97.         );
  98.         $this->doctypes->register(
  99.             'XHTML 1.0 Transitional',
  100.             true,
  101.             array_merge($common$transitional$xml$non_xml),
  102.             array('Tidy_Transitional''Tidy_XHTML''Tidy_Proprietary''Tidy_Name'),
  103.             array(),
  104.             '-//W3C//DTD XHTML 1.0 Transitional//EN',
  105.             'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
  106.         );
  107.         $this->doctypes->register(
  108.             'XHTML 1.0 Strict',
  109.             true,
  110.             array_merge($common$xml$non_xml),
  111.             array('Tidy_Strict''Tidy_XHTML''Tidy_Strict''Tidy_Proprietary''Tidy_Name'),
  112.             array(),
  113.             '-//W3C//DTD XHTML 1.0 Strict//EN',
  114.             'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
  115.         );
  116.         $this->doctypes->register(
  117.             'XHTML 1.1',
  118.             true,
  119.             // Iframe is a real XHTML 1.1 module, despite being
  120.             // "transitional"!
  121.             array_merge($common$xml, array('Ruby''Iframe')),
  122.             array('Tidy_Strict''Tidy_XHTML''Tidy_Proprietary''Tidy_Strict''Tidy_Name'), // Tidy_XHTML1_1
  123.             array(),
  124.             '-//W3C//DTD XHTML 1.1//EN',
  125.             'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'
  126.         );
  127.     }
  128.     /**
  129.      * Registers a module to the recognized module list, useful for
  130.      * overloading pre-existing modules.
  131.      * @param $module Mixed: string module name, with or without
  132.      *                HTMLPurifier_HTMLModule prefix, or instance of
  133.      *                subclass of HTMLPurifier_HTMLModule.
  134.      * @param $overload Boolean whether or not to overload previous modules.
  135.      *                  If this is not set, and you do overload a module,
  136.      *                  HTML Purifier will complain with a warning.
  137.      * @note This function will not call autoload, you must instantiate
  138.      *       (and thus invoke) autoload outside the method.
  139.      * @note If a string is passed as a module name, different variants
  140.      *       will be tested in this order:
  141.      *          - Check for HTMLPurifier_HTMLModule_$name
  142.      *          - Check all prefixes with $name in order they were added
  143.      *          - Check for literal object name
  144.      *          - Throw fatal error
  145.      *       If your object name collides with an internal class, specify
  146.      *       your module manually. All modules must have been included
  147.      *       externally: registerModule will not perform inclusions for you!
  148.      */
  149.     public function registerModule($module$overload false)
  150.     {
  151.         if (is_string($module)) {
  152.             // attempt to load the module
  153.             $original_module $module;
  154.             $ok false;
  155.             foreach ($this->prefixes as $prefix) {
  156.                 $module $prefix $original_module;
  157.                 if (class_exists($module)) {
  158.                     $ok true;
  159.                     break;
  160.                 }
  161.             }
  162.             if (!$ok) {
  163.                 $module $original_module;
  164.                 if (!class_exists($module)) {
  165.                     trigger_error(
  166.                         $original_module ' module does not exist',
  167.                         E_USER_ERROR
  168.                     );
  169.                     return;
  170.                 }
  171.             }
  172.             $module = new $module();
  173.         }
  174.         if (empty($module->name)) {
  175.             trigger_error('Module instance of ' get_class($module) . ' must have name');
  176.             return;
  177.         }
  178.         if (!$overload && isset($this->registeredModules[$module->name])) {
  179.             trigger_error('Overloading ' $module->name ' without explicit overload parameter'E_USER_WARNING);
  180.         }
  181.         $this->registeredModules[$module->name] = $module;
  182.     }
  183.     /**
  184.      * Adds a module to the current doctype by first registering it,
  185.      * and then tacking it on to the active doctype
  186.      */
  187.     public function addModule($module)
  188.     {
  189.         $this->registerModule($module);
  190.         if (is_object($module)) {
  191.             $module $module->name;
  192.         }
  193.         $this->userModules[] = $module;
  194.     }
  195.     /**
  196.      * Adds a class prefix that registerModule() will use to resolve a
  197.      * string name to a concrete class
  198.      */
  199.     public function addPrefix($prefix)
  200.     {
  201.         $this->prefixes[] = $prefix;
  202.     }
  203.     /**
  204.      * Performs processing on modules, after being called you may
  205.      * use getElement() and getElements()
  206.      * @param HTMLPurifier_Config $config
  207.      */
  208.     public function setup($config)
  209.     {
  210.         $this->trusted $config->get('HTML.Trusted');
  211.         // generate
  212.         $this->doctype $this->doctypes->make($config);
  213.         $modules $this->doctype->modules;
  214.         // take out the default modules that aren't allowed
  215.         $lookup $config->get('HTML.AllowedModules');
  216.         $special_cases $config->get('HTML.CoreModules');
  217.         if (is_array($lookup)) {
  218.             foreach ($modules as $k => $m) {
  219.                 if (isset($special_cases[$m])) {
  220.                     continue;
  221.                 }
  222.                 if (!isset($lookup[$m])) {
  223.                     unset($modules[$k]);
  224.                 }
  225.             }
  226.         }
  227.         // custom modules
  228.         if ($config->get('HTML.Proprietary')) {
  229.             $modules[] = 'Proprietary';
  230.         }
  231.         if ($config->get('HTML.SafeObject')) {
  232.             $modules[] = 'SafeObject';
  233.         }
  234.         if ($config->get('HTML.SafeEmbed')) {
  235.             $modules[] = 'SafeEmbed';
  236.         }
  237.         if ($config->get('HTML.SafeScripting') !== array()) {
  238.             $modules[] = 'SafeScripting';
  239.         }
  240.         if ($config->get('HTML.Nofollow')) {
  241.             $modules[] = 'Nofollow';
  242.         }
  243.         if ($config->get('HTML.TargetBlank')) {
  244.             $modules[] = 'TargetBlank';
  245.         }
  246.         // NB: HTML.TargetNoreferrer and HTML.TargetNoopener must be AFTER HTML.TargetBlank
  247.         // so that its post-attr-transform gets run afterwards.
  248.         if ($config->get('HTML.TargetNoreferrer')) {
  249.             $modules[] = 'TargetNoreferrer';
  250.         }
  251.         if ($config->get('HTML.TargetNoopener')) {
  252.             $modules[] = 'TargetNoopener';
  253.         }
  254.         // merge in custom modules
  255.         $modules array_merge($modules$this->userModules);
  256.         foreach ($modules as $module) {
  257.             $this->processModule($module);
  258.             $this->modules[$module]->setup($config);
  259.         }
  260.         foreach ($this->doctype->tidyModules as $module) {
  261.             $this->processModule($module);
  262.             $this->modules[$module]->setup($config);
  263.         }
  264.         // prepare any injectors
  265.         foreach ($this->modules as $module) {
  266.             $n = array();
  267.             foreach ($module->info_injector as $injector) {
  268.                 if (!is_object($injector)) {
  269.                     $class "HTMLPurifier_Injector_$injector";
  270.                     $injector = new $class;
  271.                 }
  272.                 $n[$injector->name] = $injector;
  273.             }
  274.             $module->info_injector $n;
  275.         }
  276.         // setup lookup table based on all valid modules
  277.         foreach ($this->modules as $module) {
  278.             foreach ($module->info as $name => $def) {
  279.                 if (!isset($this->elementLookup[$name])) {
  280.                     $this->elementLookup[$name] = array();
  281.                 }
  282.                 $this->elementLookup[$name][] = $module->name;
  283.             }
  284.         }
  285.         // note the different choice
  286.         $this->contentSets = new HTMLPurifier_ContentSets(
  287.             // content set assembly deals with all possible modules,
  288.             // not just ones deemed to be "safe"
  289.             $this->modules
  290.         );
  291.         $this->attrCollections = new HTMLPurifier_AttrCollections(
  292.             $this->attrTypes,
  293.             // there is no way to directly disable a global attribute,
  294.             // but using AllowedAttributes or simply not including
  295.             // the module in your custom doctype should be sufficient
  296.             $this->modules
  297.         );
  298.     }
  299.     /**
  300.      * Takes a module and adds it to the active module collection,
  301.      * registering it if necessary.
  302.      */
  303.     public function processModule($module)
  304.     {
  305.         if (!isset($this->registeredModules[$module]) || is_object($module)) {
  306.             $this->registerModule($module);
  307.         }
  308.         $this->modules[$module] = $this->registeredModules[$module];
  309.     }
  310.     /**
  311.      * Retrieves merged element definitions.
  312.      * @return Array of HTMLPurifier_ElementDef
  313.      */
  314.     public function getElements()
  315.     {
  316.         $elements = array();
  317.         foreach ($this->modules as $module) {
  318.             if (!$this->trusted && !$module->safe) {
  319.                 continue;
  320.             }
  321.             foreach ($module->info as $name => $v) {
  322.                 if (isset($elements[$name])) {
  323.                     continue;
  324.                 }
  325.                 $elements[$name] = $this->getElement($name);
  326.             }
  327.         }
  328.         // remove dud elements, this happens when an element that
  329.         // appeared to be safe actually wasn't
  330.         foreach ($elements as $n => $v) {
  331.             if ($v === false) {
  332.                 unset($elements[$n]);
  333.             }
  334.         }
  335.         return $elements;
  336.     }
  337.     /**
  338.      * Retrieves a single merged element definition
  339.      * @param string $name Name of element
  340.      * @param bool $trusted Boolean trusted overriding parameter: set to true
  341.      *                 if you want the full version of an element
  342.      * @return HTMLPurifier_ElementDef Merged HTMLPurifier_ElementDef
  343.      * @note You may notice that modules are getting iterated over twice (once
  344.      *       in getElements() and once here). This
  345.      *       is because
  346.      */
  347.     public function getElement($name$trusted null)
  348.     {
  349.         if (!isset($this->elementLookup[$name])) {
  350.             return false;
  351.         }
  352.         // setup global state variables
  353.         $def false;
  354.         if ($trusted === null) {
  355.             $trusted $this->trusted;
  356.         }
  357.         // iterate through each module that has registered itself to this
  358.         // element
  359.         foreach ($this->elementLookup[$name] as $module_name) {
  360.             $module $this->modules[$module_name];
  361.             // refuse to create/merge from a module that is deemed unsafe--
  362.             // pretend the module doesn't exist--when trusted mode is not on.
  363.             if (!$trusted && !$module->safe) {
  364.                 continue;
  365.             }
  366.             // clone is used because, ideally speaking, the original
  367.             // definition should not be modified. Usually, this will
  368.             // make no difference, but for consistency's sake
  369.             $new_def = clone $module->info[$name];
  370.             if (!$def && $new_def->standalone) {
  371.                 $def $new_def;
  372.             } elseif ($def) {
  373.                 // This will occur even if $new_def is standalone. In practice,
  374.                 // this will usually result in a full replacement.
  375.                 $def->mergeIn($new_def);
  376.             } else {
  377.                 // :TODO:
  378.                 // non-standalone definitions that don't have a standalone
  379.                 // to merge into could be deferred to the end
  380.                 // HOWEVER, it is perfectly valid for a non-standalone
  381.                 // definition to lack a standalone definition, even
  382.                 // after all processing: this allows us to safely
  383.                 // specify extra attributes for elements that may not be
  384.                 // enabled all in one place.  In particular, this might
  385.                 // be the case for trusted elements.  WARNING: care must
  386.                 // be taken that the /extra/ definitions are all safe.
  387.                 continue;
  388.             }
  389.             // attribute value expansions
  390.             $this->attrCollections->performInclusions($def->attr);
  391.             $this->attrCollections->expandIdentifiers($def->attr$this->attrTypes);
  392.             // descendants_are_inline, for ChildDef_Chameleon
  393.             if (is_string($def->content_model) &&
  394.                 strpos($def->content_model'Inline') !== false) {
  395.                 if ($name != 'del' && $name != 'ins') {
  396.                     // this is for you, ins/del
  397.                     $def->descendants_are_inline true;
  398.                 }
  399.             }
  400.             $this->contentSets->generateChildDef($def$module);
  401.         }
  402.         // This can occur if there is a blank definition, but no base to
  403.         // mix it in with
  404.         if (!$def) {
  405.             return false;
  406.         }
  407.         // add information on required attributes
  408.         foreach ($def->attr as $attr_name => $attr_def) {
  409.             if ($attr_def->required) {
  410.                 $def->required_attr[] = $attr_name;
  411.             }
  412.         }
  413.         return $def;
  414.     }
  415. }
  416. // vim: et sw=4 sts=4