File indexing completed on 2024-04-28 05:58:53

0001 <?php
0002 
0003 class HTMLPurifier_DoctypeRegistry
0004 {
0005 
0006     /**
0007      * Hash of doctype names to doctype objects.
0008      * @type array
0009      */
0010     protected $doctypes;
0011 
0012     /**
0013      * Lookup table of aliases to real doctype names.
0014      * @type array
0015      */
0016     protected $aliases;
0017 
0018     /**
0019      * Registers a doctype to the registry
0020      * @note Accepts a fully-formed doctype object, or the
0021      *       parameters for constructing a doctype object
0022      * @param string $doctype Name of doctype or literal doctype object
0023      * @param bool $xml
0024      * @param array $modules Modules doctype will load
0025      * @param array $tidy_modules Modules doctype will load for certain modes
0026      * @param array $aliases Alias names for doctype
0027      * @param string $dtd_public
0028      * @param string $dtd_system
0029      * @return HTMLPurifier_Doctype Editable registered doctype
0030      */
0031     public function register(
0032         $doctype,
0033         $xml = true,
0034         $modules = array(),
0035         $tidy_modules = array(),
0036         $aliases = array(),
0037         $dtd_public = null,
0038         $dtd_system = null
0039     ) {
0040         if (!is_array($modules)) {
0041             $modules = array($modules);
0042         }
0043         if (!is_array($tidy_modules)) {
0044             $tidy_modules = array($tidy_modules);
0045         }
0046         if (!is_array($aliases)) {
0047             $aliases = array($aliases);
0048         }
0049         if (!is_object($doctype)) {
0050             $doctype = new HTMLPurifier_Doctype(
0051                 $doctype,
0052                 $xml,
0053                 $modules,
0054                 $tidy_modules,
0055                 $aliases,
0056                 $dtd_public,
0057                 $dtd_system
0058             );
0059         }
0060         $this->doctypes[$doctype->name] = $doctype;
0061         $name = $doctype->name;
0062         // hookup aliases
0063         foreach ($doctype->aliases as $alias) {
0064             if (isset($this->doctypes[$alias])) {
0065                 continue;
0066             }
0067             $this->aliases[$alias] = $name;
0068         }
0069         // remove old aliases
0070         if (isset($this->aliases[$name])) {
0071             unset($this->aliases[$name]);
0072         }
0073         return $doctype;
0074     }
0075 
0076     /**
0077      * Retrieves reference to a doctype of a certain name
0078      * @note This function resolves aliases
0079      * @note When possible, use the more fully-featured make()
0080      * @param string $doctype Name of doctype
0081      * @return HTMLPurifier_Doctype Editable doctype object
0082      */
0083     public function get($doctype)
0084     {
0085         if (isset($this->aliases[$doctype])) {
0086             $doctype = $this->aliases[$doctype];
0087         }
0088         if (!isset($this->doctypes[$doctype])) {
0089             trigger_error('Doctype ' . htmlspecialchars($doctype) . ' does not exist', E_USER_ERROR);
0090             $anon = new HTMLPurifier_Doctype($doctype);
0091             return $anon;
0092         }
0093         return $this->doctypes[$doctype];
0094     }
0095 
0096     /**
0097      * Creates a doctype based on a configuration object,
0098      * will perform initialization on the doctype
0099      * @note Use this function to get a copy of doctype that config
0100      *       can hold on to (this is necessary in order to tell
0101      *       Generator whether or not the current document is XML
0102      *       based or not).
0103      * @param HTMLPurifier_Config $config
0104      * @return HTMLPurifier_Doctype
0105      */
0106     public function make($config)
0107     {
0108         return clone $this->get($this->getDoctypeFromConfig($config));
0109     }
0110 
0111     /**
0112      * Retrieves the doctype from the configuration object
0113      * @param HTMLPurifier_Config $config
0114      * @return string
0115      */
0116     public function getDoctypeFromConfig($config)
0117     {
0118         // recommended test
0119         $doctype = $config->get('HTML.Doctype');
0120         if (!empty($doctype)) {
0121             return $doctype;
0122         }
0123         $doctype = $config->get('HTML.CustomDoctype');
0124         if (!empty($doctype)) {
0125             return $doctype;
0126         }
0127         // backwards-compatibility
0128         if ($config->get('HTML.XHTML')) {
0129             $doctype = 'XHTML 1.0';
0130         } else {
0131             $doctype = 'HTML 4.01';
0132         }
0133         if ($config->get('HTML.Strict')) {
0134             $doctype .= ' Strict';
0135         } else {
0136             $doctype .= ' Transitional';
0137         }
0138         return $doctype;
0139     }
0140 }
0141 
0142 // vim: et sw=4 sts=4