File indexing completed on 2024-04-28 06:00:03

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category  Zend
0016  * @package   Zend_Uri
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version   $Id$
0020  */
0021 
0022 /**
0023  * Abstract class for all Zend_Uri handlers
0024  *
0025  * @category  Zend
0026  * @package   Zend_Uri
0027  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0028  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0029  */
0030 abstract class Zend_Uri
0031 {
0032     /**
0033      * Scheme of this URI (http, ftp, etc.)
0034      *
0035      * @var string
0036      */
0037     protected $_scheme = '';
0038 
0039     /**
0040      * Global configuration array
0041      *
0042      * @var array
0043      */
0044     static protected $_config = array(
0045         'allow_unwise' => false
0046     );
0047 
0048     /**
0049      * Return a string representation of this URI.
0050      *
0051      * @see    getUri()
0052      * @return string
0053      */
0054     public function __toString()
0055     {
0056         try {
0057             return $this->getUri();
0058         } catch (Exception $e) {
0059             trigger_error($e->getMessage(), E_USER_WARNING);
0060             return '';
0061         }
0062     }
0063 
0064     /**
0065      * Convenience function, checks that a $uri string is well-formed
0066      * by validating it but not returning an object.  Returns TRUE if
0067      * $uri is a well-formed URI, or FALSE otherwise.
0068      *
0069      * @param  string $uri The URI to check
0070      * @return boolean
0071      */
0072     public static function check($uri)
0073     {
0074         try {
0075             $uri = self::factory($uri);
0076         } catch (Exception $e) {
0077             return false;
0078         }
0079 
0080         return $uri->valid();
0081     }
0082 
0083     /**
0084      * Create a new Zend_Uri object for a URI.  If building a new URI, then $uri should contain
0085      * only the scheme (http, ftp, etc).  Otherwise, supply $uri with the complete URI.
0086      *
0087      * @param  string $uri       The URI form which a Zend_Uri instance is created
0088      * @param  string $className The name of the class to use in order to manipulate URI
0089      * @throws Zend_Uri_Exception When an empty string was supplied for the scheme
0090      * @throws Zend_Uri_Exception When an illegal scheme is supplied
0091      * @throws Zend_Uri_Exception When the scheme is not supported
0092      * @throws Zend_Uri_Exception When $className doesn't exist or doesn't implements Zend_Uri
0093      * @return Zend_Uri
0094      * @link   http://www.faqs.org/rfcs/rfc2396.html
0095      */
0096     public static function factory($uri = 'http', $className = null)
0097     {
0098         // Separate the scheme from the scheme-specific parts
0099         $uri            = explode(':', $uri, 2);
0100         $scheme         = strtolower($uri[0]);
0101         $schemeSpecific = isset($uri[1]) === true ? $uri[1] : '';
0102 
0103         if (strlen($scheme) === 0) {
0104             // require_once 'Zend/Uri/Exception.php';
0105             throw new Zend_Uri_Exception('An empty string was supplied for the scheme');
0106         }
0107 
0108         // Security check: $scheme is used to load a class file, so only alphanumerics are allowed.
0109         if (ctype_alnum($scheme) === false) {
0110             // require_once 'Zend/Uri/Exception.php';
0111             throw new Zend_Uri_Exception('Illegal scheme supplied, only alphanumeric characters are permitted');
0112         }
0113 
0114         if ($className === null) {
0115             /**
0116              * Create a new Zend_Uri object for the $uri. If a subclass of Zend_Uri exists for the
0117              * scheme, return an instance of that class. Otherwise, a Zend_Uri_Exception is thrown.
0118              */
0119             switch ($scheme) {
0120                 case 'http':
0121                     // Break intentionally omitted
0122                 case 'https':
0123                     $className = 'Zend_Uri_Http';
0124                     break;
0125 
0126                 case 'mailto':
0127                     // TODO
0128                 default:
0129                     // require_once 'Zend/Uri/Exception.php';
0130                     throw new Zend_Uri_Exception("Scheme \"$scheme\" is not supported");
0131                     break;
0132             }
0133         }
0134 
0135         // require_once 'Zend/Loader.php';
0136         try {
0137             Zend_Loader::loadClass($className);
0138         } catch (Exception $e) {
0139             // require_once 'Zend/Uri/Exception.php';
0140             throw new Zend_Uri_Exception("\"$className\" not found");
0141         }
0142 
0143         $schemeHandler = new $className($scheme, $schemeSpecific);
0144 
0145         if (! $schemeHandler instanceof Zend_Uri) {
0146             // require_once 'Zend/Uri/Exception.php';
0147             throw new Zend_Uri_Exception("\"$className\" is not an instance of Zend_Uri");
0148         }
0149 
0150         return $schemeHandler;
0151     }
0152 
0153     /**
0154      * Get the URI's scheme
0155      *
0156      * @return string|false Scheme or false if no scheme is set.
0157      */
0158     public function getScheme()
0159     {
0160         if (empty($this->_scheme) === false) {
0161             return $this->_scheme;
0162         } else {
0163             return false;
0164         }
0165     }
0166 
0167     /**
0168      * Set global configuration options
0169      *
0170      * @param Zend_Config|array $config
0171      */
0172     static public function setConfig($config)
0173     {
0174         if ($config instanceof Zend_Config) {
0175             $config = $config->toArray();
0176         } elseif (!is_array($config)) {
0177             throw new Zend_Uri_Exception("Config must be an array or an instance of Zend_Config.");
0178         }
0179 
0180         foreach ($config as $k => $v) {
0181             self::$_config[$k] = $v;
0182         }
0183     }
0184 
0185     /**
0186      * Zend_Uri and its subclasses cannot be instantiated directly.
0187      * Use Zend_Uri::factory() to return a new Zend_Uri object.
0188      *
0189      * @param string $scheme         The scheme of the URI
0190      * @param string $schemeSpecific The scheme-specific part of the URI
0191      */
0192     abstract protected function __construct($scheme, $schemeSpecific = '');
0193 
0194     /**
0195      * Return a string representation of this URI.
0196      *
0197      * @return string
0198      */
0199     abstract public function getUri();
0200 
0201     /**
0202      * Returns TRUE if this URI is valid, or FALSE otherwise.
0203      *
0204      * @return boolean
0205      */
0206     abstract public function valid();
0207 }