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

0001 <?php
0002 
0003 /**
0004  * Validator for the components of a URI for a specific scheme
0005  */
0006 abstract class HTMLPurifier_URIScheme
0007 {
0008 
0009     /**
0010      * Scheme's default port (integer). If an explicit port number is
0011      * specified that coincides with the default port, it will be
0012      * elided.
0013      * @type int
0014      */
0015     public $default_port = null;
0016 
0017     /**
0018      * Whether or not URIs of this scheme are locatable by a browser
0019      * http and ftp are accessible, while mailto and news are not.
0020      * @type bool
0021      */
0022     public $browsable = false;
0023 
0024     /**
0025      * Whether or not data transmitted over this scheme is encrypted.
0026      * https is secure, http is not.
0027      * @type bool
0028      */
0029     public $secure = false;
0030 
0031     /**
0032      * Whether or not the URI always uses <hier_part>, resolves edge cases
0033      * with making relative URIs absolute
0034      * @type bool
0035      */
0036     public $hierarchical = false;
0037 
0038     /**
0039      * Whether or not the URI may omit a hostname when the scheme is
0040      * explicitly specified, ala file:///path/to/file. As of writing,
0041      * 'file' is the only scheme that browsers support his properly.
0042      * @type bool
0043      */
0044     public $may_omit_host = false;
0045 
0046     /**
0047      * Validates the components of a URI for a specific scheme.
0048      * @param HTMLPurifier_URI $uri Reference to a HTMLPurifier_URI object
0049      * @param HTMLPurifier_Config $config
0050      * @param HTMLPurifier_Context $context
0051      * @return bool success or failure
0052      */
0053     abstract public function doValidate(&$uri, $config, $context);
0054 
0055     /**
0056      * Public interface for validating components of a URI.  Performs a
0057      * bunch of default actions. Don't overload this method.
0058      * @param HTMLPurifier_URI $uri Reference to a HTMLPurifier_URI object
0059      * @param HTMLPurifier_Config $config
0060      * @param HTMLPurifier_Context $context
0061      * @return bool success or failure
0062      */
0063     public function validate(&$uri, $config, $context)
0064     {
0065         if ($this->default_port == $uri->port) {
0066             $uri->port = null;
0067         }
0068         // kludge: browsers do funny things when the scheme but not the
0069         // authority is set
0070         if (!$this->may_omit_host &&
0071             // if the scheme is present, a missing host is always in error
0072             (!is_null($uri->scheme) && ($uri->host === '' || is_null($uri->host))) ||
0073             // if the scheme is not present, a *blank* host is in error,
0074             // since this translates into '///path' which most browsers
0075             // interpret as being 'http://path'.
0076             (is_null($uri->scheme) && $uri->host === '')
0077         ) {
0078             do {
0079                 if (is_null($uri->scheme)) {
0080                     if (substr($uri->path, 0, 2) != '//') {
0081                         $uri->host = null;
0082                         break;
0083                     }
0084                     // URI is '////path', so we cannot nullify the
0085                     // host to preserve semantics.  Try expanding the
0086                     // hostname instead (fall through)
0087                 }
0088                 // first see if we can manually insert a hostname
0089                 $host = $config->get('URI.Host');
0090                 if (!is_null($host)) {
0091                     $uri->host = $host;
0092                 } else {
0093                     // we can't do anything sensible, reject the URL.
0094                     return false;
0095                 }
0096             } while (false);
0097         }
0098         return $this->doValidate($uri, $config, $context);
0099     }
0100 }
0101 
0102 // vim: et sw=4 sts=4