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

0001 <?php
0002 
0003 /**
0004  * Registry for retrieving specific URI scheme validator objects.
0005  */
0006 class HTMLPurifier_URISchemeRegistry
0007 {
0008 
0009     /**
0010      * Retrieve sole instance of the registry.
0011      * @param HTMLPurifier_URISchemeRegistry $prototype Optional prototype to overload sole instance with,
0012      *                   or bool true to reset to default registry.
0013      * @return HTMLPurifier_URISchemeRegistry
0014      * @note Pass a registry object $prototype with a compatible interface and
0015      *       the function will copy it and return it all further times.
0016      */
0017     public static function instance($prototype = null)
0018     {
0019         static $instance = null;
0020         if ($prototype !== null) {
0021             $instance = $prototype;
0022         } elseif ($instance === null || $prototype == true) {
0023             $instance = new HTMLPurifier_URISchemeRegistry();
0024         }
0025         return $instance;
0026     }
0027 
0028     /**
0029      * Cache of retrieved schemes.
0030      * @type HTMLPurifier_URIScheme[]
0031      */
0032     protected $schemes = array();
0033 
0034     /**
0035      * Retrieves a scheme validator object
0036      * @param string $scheme String scheme name like http or mailto
0037      * @param HTMLPurifier_Config $config
0038      * @param HTMLPurifier_Context $context
0039      * @return HTMLPurifier_URIScheme
0040      */
0041     public function getScheme($scheme, $config, $context)
0042     {
0043         if (!$config) {
0044             $config = HTMLPurifier_Config::createDefault();
0045         }
0046 
0047         // important, otherwise attacker could include arbitrary file
0048         $allowed_schemes = $config->get('URI.AllowedSchemes');
0049         if (!$config->get('URI.OverrideAllowedSchemes') &&
0050             !isset($allowed_schemes[$scheme])
0051         ) {
0052             return;
0053         }
0054 
0055         if (isset($this->schemes[$scheme])) {
0056             return $this->schemes[$scheme];
0057         }
0058         if (!isset($allowed_schemes[$scheme])) {
0059             return;
0060         }
0061 
0062         $class = 'HTMLPurifier_URIScheme_' . $scheme;
0063         if (!class_exists($class)) {
0064             return;
0065         }
0066         $this->schemes[$scheme] = new $class();
0067         return $this->schemes[$scheme];
0068     }
0069 
0070     /**
0071      * Registers a custom scheme to the cache, bypassing reflection.
0072      * @param string $scheme Scheme name
0073      * @param HTMLPurifier_URIScheme $scheme_obj
0074      */
0075     public function register($scheme, $scheme_obj)
0076     {
0077         $this->schemes[$scheme] = $scheme_obj;
0078     }
0079 }
0080 
0081 // vim: et sw=4 sts=4