File indexing completed on 2024-05-12 06:03:12

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  * @see Zend_Uri
0024  */
0025 // require_once 'Zend/Uri.php';
0026 
0027 /**
0028  * @see Zend_Validate_Hostname
0029  */
0030 // require_once 'Zend/Validate/Hostname.php';
0031 
0032 /**
0033  * HTTP(S) URI handler
0034  *
0035  * @category  Zend
0036  * @package   Zend_Uri
0037  * @uses      Zend_Uri
0038  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0039  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0040  */
0041 class Zend_Uri_Http extends Zend_Uri
0042 {
0043     /**
0044      * Character classes for validation regular expressions
0045      */
0046     const CHAR_ALNUM    = 'A-Za-z0-9';
0047     const CHAR_MARK     = '-_.!~*\'()\[\]';
0048     const CHAR_RESERVED = ';\/?:@&=+$,';
0049     const CHAR_SEGMENT  = ':@&=+$,;';
0050     const CHAR_UNWISE   = '{}|\\\\^`';
0051 
0052     /**
0053      * HTTP username
0054      *
0055      * @var string
0056      */
0057     protected $_username = '';
0058 
0059     /**
0060      * HTTP password
0061      *
0062      * @var string
0063      */
0064     protected $_password = '';
0065 
0066     /**
0067      * HTTP host
0068      *
0069      * @var string
0070      */
0071     protected $_host = '';
0072 
0073     /**
0074      * HTTP post
0075      *
0076      * @var string
0077      */
0078     protected $_port = '';
0079 
0080     /**
0081      * HTTP part
0082      *
0083      * @var string
0084      */
0085     protected $_path = '';
0086 
0087     /**
0088      * HTTP query
0089      *
0090      * @var string
0091      */
0092     protected $_query = '';
0093 
0094     /**
0095      * HTTP fragment
0096      *
0097      * @var string
0098      */
0099     protected $_fragment = '';
0100 
0101     /**
0102      * Regular expression grammar rules for validation; values added by constructor
0103      *
0104      * @var array
0105      */
0106     protected $_regex = array();
0107 
0108     /**
0109      * Constructor accepts a string $scheme (e.g., http, https) and a scheme-specific part of the URI
0110      * (e.g., example.com/path/to/resource?query=param#fragment)
0111      *
0112      * @param  string $scheme         The scheme of the URI
0113      * @param  string $schemeSpecific The scheme-specific part of the URI
0114      * @throws Zend_Uri_Exception When the URI is not valid
0115      */
0116     protected function __construct($scheme, $schemeSpecific = '')
0117     {
0118         // Set the scheme
0119         $this->_scheme = $scheme;
0120 
0121         // Set up grammar rules for validation via regular expressions. These
0122         // are to be used with slash-delimited regular expression strings.
0123 
0124         // Escaped special characters (eg. '%25' for '%')
0125         $this->_regex['escaped']    = '%[[:xdigit:]]{2}';
0126 
0127         // Unreserved characters
0128         $this->_regex['unreserved'] = '[' . self::CHAR_ALNUM . self::CHAR_MARK . ']';
0129 
0130         // Segment can use escaped, unreserved or a set of additional chars
0131         $this->_regex['segment']    = '(?:' . $this->_regex['escaped'] . '|[' .
0132             self::CHAR_ALNUM . self::CHAR_MARK . self::CHAR_SEGMENT . '])*';
0133 
0134         // Path can be a series of segmets char strings seperated by '/'
0135         $this->_regex['path']       = '(?:\/(?:' . $this->_regex['segment'] . ')?)+';
0136 
0137         // URI characters can be escaped, alphanumeric, mark or reserved chars
0138         $this->_regex['uric']       = '(?:' . $this->_regex['escaped'] . '|[' .
0139             self::CHAR_ALNUM . self::CHAR_MARK . self::CHAR_RESERVED .
0140 
0141         // If unwise chars are allowed, add them to the URI chars class
0142             (self::$_config['allow_unwise'] ? self::CHAR_UNWISE : '') . '])';
0143 
0144         // If no scheme-specific part was supplied, the user intends to create
0145         // a new URI with this object.  No further parsing is required.
0146         if (strlen($schemeSpecific) === 0) {
0147             return;
0148         }
0149 
0150         // Parse the scheme-specific URI parts into the instance variables.
0151         $this->_parseUri($schemeSpecific);
0152 
0153         // Validate the URI
0154         if ($this->valid() === false) {
0155             // require_once 'Zend/Uri/Exception.php';
0156             throw new Zend_Uri_Exception('Invalid URI supplied');
0157         }
0158     }
0159 
0160     /**
0161      * Creates a Zend_Uri_Http from the given string
0162      *
0163      * @param  string $uri String to create URI from, must start with
0164      *                     'http://' or 'https://'
0165      * @throws InvalidArgumentException  When the given $uri is not a string or
0166      *                                   does not start with http:// or https://
0167      * @throws Zend_Uri_Exception        When the given $uri is invalid
0168      * @return Zend_Uri_Http
0169      */
0170     public static function fromString($uri)
0171     {
0172         if (is_string($uri) === false) {
0173             // require_once 'Zend/Uri/Exception.php';
0174             throw new Zend_Uri_Exception('$uri is not a string');
0175         }
0176 
0177         $uri            = explode(':', $uri, 2);
0178         $scheme         = strtolower($uri[0]);
0179         $schemeSpecific = isset($uri[1]) === true ? $uri[1] : '';
0180 
0181         if (in_array($scheme, array('http', 'https')) === false) {
0182             // require_once 'Zend/Uri/Exception.php';
0183             throw new Zend_Uri_Exception("Invalid scheme: '$scheme'");
0184         }
0185 
0186         $schemeHandler = new Zend_Uri_Http($scheme, $schemeSpecific);
0187         return $schemeHandler;
0188     }
0189 
0190     /**
0191      * Parse the scheme-specific portion of the URI and place its parts into instance variables.
0192      *
0193      * @param  string $schemeSpecific The scheme-specific portion to parse
0194      * @throws Zend_Uri_Exception When scheme-specific decoposition fails
0195      * @throws Zend_Uri_Exception When authority decomposition fails
0196      * @return void
0197      */
0198     protected function _parseUri($schemeSpecific)
0199     {
0200         // High-level decomposition parser
0201         $pattern = '~^((//)([^/?#]*))([^?#]*)(\?([^#]*))?(#(.*))?$~';
0202         $status  = @preg_match($pattern, $schemeSpecific, $matches);
0203         if ($status === false) {
0204             // require_once 'Zend/Uri/Exception.php';
0205             throw new Zend_Uri_Exception('Internal error: scheme-specific decomposition failed');
0206         }
0207 
0208         // Failed decomposition; no further processing needed
0209         if ($status === false) {
0210             return;
0211         }
0212 
0213         // Save URI components that need no further decomposition
0214         $this->_path     = isset($matches[4]) === true ? $matches[4] : '';
0215         $this->_query    = isset($matches[6]) === true ? $matches[6] : '';
0216         $this->_fragment = isset($matches[8]) === true ? $matches[8] : '';
0217 
0218         // Additional decomposition to get username, password, host, and port
0219         $combo   = isset($matches[3]) === true ? $matches[3] : '';
0220         $pattern = '~^(([^:@]*)(:([^@]*))?@)?((?(?=[[])[[][^]]+[]]|[^:]+))(:(.*))?$~';        
0221         $status  = @preg_match($pattern, $combo, $matches);
0222         if ($status === false) {
0223             // require_once 'Zend/Uri/Exception.php';
0224             throw new Zend_Uri_Exception('Internal error: authority decomposition failed');
0225         }
0226         
0227         // Save remaining URI components
0228         $this->_username = isset($matches[2]) === true ? $matches[2] : '';
0229         $this->_password = isset($matches[4]) === true ? $matches[4] : '';
0230         $this->_host     = isset($matches[5]) === true 
0231                          ? preg_replace('~^\[([^]]+)\]$~', '\1', $matches[5])  // Strip wrapper [] from IPv6 literal
0232                          : '';
0233         $this->_port     = isset($matches[7]) === true ? $matches[7] : '';
0234     }
0235 
0236     /**
0237      * Returns a URI based on current values of the instance variables. If any
0238      * part of the URI does not pass validation, then an exception is thrown.
0239      *
0240      * @throws Zend_Uri_Exception When one or more parts of the URI are invalid
0241      * @return string
0242      */
0243     public function getUri()
0244     {
0245         if ($this->valid() === false) {
0246             // require_once 'Zend/Uri/Exception.php';
0247             throw new Zend_Uri_Exception('One or more parts of the URI are invalid');
0248         }
0249 
0250         $password = strlen($this->_password) > 0 ? ":$this->_password" : '';
0251         $auth     = strlen($this->_username) > 0 ? "$this->_username$password@" : '';
0252         $port     = strlen($this->_port) > 0 ? ":$this->_port" : '';
0253         $query    = strlen($this->_query) > 0 ? "?$this->_query" : '';
0254         $fragment = strlen($this->_fragment) > 0 ? "#$this->_fragment" : '';
0255 
0256         return $this->_scheme
0257              . '://'
0258              . $auth
0259              . $this->_host
0260              . $port
0261              . $this->_path
0262              . $query
0263              . $fragment;
0264     }
0265 
0266     /**
0267      * Validate the current URI from the instance variables. Returns true if and only if all
0268      * parts pass validation.
0269      *
0270      * @return boolean
0271      */
0272     public function valid()
0273     {
0274         // Return true if and only if all parts of the URI have passed validation
0275         return $this->validateUsername()
0276            and $this->validatePassword()
0277            and $this->validateHost()
0278            and $this->validatePort()
0279            and $this->validatePath()
0280            and $this->validateQuery()
0281            and $this->validateFragment();
0282     }
0283 
0284     /**
0285      * Returns the username portion of the URL, or FALSE if none.
0286      *
0287      * @return string
0288      */
0289     public function getUsername()
0290     {
0291         return strlen($this->_username) > 0 ? $this->_username : false;
0292     }
0293 
0294     /**
0295      * Returns true if and only if the username passes validation. If no username is passed,
0296      * then the username contained in the instance variable is used.
0297      *
0298      * @param  string $username The HTTP username
0299      * @throws Zend_Uri_Exception When username validation fails
0300      * @return boolean
0301      * @link   http://www.faqs.org/rfcs/rfc2396.html
0302      */
0303     public function validateUsername($username = null)
0304     {
0305         if ($username === null) {
0306             $username = $this->_username;
0307         }
0308 
0309         // If the username is empty, then it is considered valid
0310         if (strlen($username) === 0) {
0311             return true;
0312         }
0313 
0314         // Check the username against the allowed values
0315         $status = @preg_match('/^(?:' . $this->_regex['escaped'] . '|[' .
0316             self::CHAR_ALNUM . self::CHAR_MARK . ';:&=+$,' . '])+$/', $username);
0317 
0318         if ($status === false) {
0319             // require_once 'Zend/Uri/Exception.php';
0320             throw new Zend_Uri_Exception('Internal error: username validation failed');
0321         }
0322 
0323         return $status === 1;
0324     }
0325 
0326     /**
0327      * Sets the username for the current URI, and returns the old username
0328      *
0329      * @param  string $username The HTTP username
0330      * @throws Zend_Uri_Exception When $username is not a valid HTTP username
0331      * @return string
0332      */
0333     public function setUsername($username)
0334     {
0335         if ($this->validateUsername($username) === false) {
0336             // require_once 'Zend/Uri/Exception.php';
0337             throw new Zend_Uri_Exception("Username \"$username\" is not a valid HTTP username");
0338         }
0339 
0340         $oldUsername     = $this->_username;
0341         $this->_username = $username;
0342 
0343         return $oldUsername;
0344     }
0345 
0346     /**
0347      * Returns the password portion of the URL, or FALSE if none.
0348      *
0349      * @return string
0350      */
0351     public function getPassword()
0352     {
0353         return strlen($this->_password) > 0 ? $this->_password : false;
0354     }
0355 
0356     /**
0357      * Returns true if and only if the password passes validation. If no password is passed,
0358      * then the password contained in the instance variable is used.
0359      *
0360      * @param  string $password The HTTP password
0361      * @throws Zend_Uri_Exception When password validation fails
0362      * @return boolean
0363      * @link   http://www.faqs.org/rfcs/rfc2396.html
0364      */
0365     public function validatePassword($password = null)
0366     {
0367         if ($password === null) {
0368             $password = $this->_password;
0369         }
0370 
0371         // If the password is empty, then it is considered valid
0372         if (strlen($password) === 0) {
0373             return true;
0374         }
0375 
0376         // If the password is nonempty, but there is no username, then it is considered invalid
0377         if (strlen($password) > 0 and strlen($this->_username) === 0) {
0378             return false;
0379         }
0380 
0381         // Check the password against the allowed values
0382         $status = @preg_match('/^(?:' . $this->_regex['escaped'] . '|[' .
0383             self::CHAR_ALNUM . self::CHAR_MARK . ';:&=+$,' . '])+$/', $password);
0384 
0385         if ($status === false) {
0386             // require_once 'Zend/Uri/Exception.php';
0387             throw new Zend_Uri_Exception('Internal error: password validation failed.');
0388         }
0389 
0390         return $status == 1;
0391     }
0392 
0393     /**
0394      * Sets the password for the current URI, and returns the old password
0395      *
0396      * @param  string $password The HTTP password
0397      * @throws Zend_Uri_Exception When $password is not a valid HTTP password
0398      * @return string
0399      */
0400     public function setPassword($password)
0401     {
0402         if ($this->validatePassword($password) === false) {
0403             // require_once 'Zend/Uri/Exception.php';
0404             throw new Zend_Uri_Exception("Password \"$password\" is not a valid HTTP password.");
0405         }
0406 
0407         $oldPassword     = $this->_password;
0408         $this->_password = $password;
0409 
0410         return $oldPassword;
0411     }
0412 
0413     /**
0414      * Returns the domain or host IP portion of the URL, or FALSE if none.
0415      *
0416      * @return string
0417      */
0418     public function getHost()
0419     {
0420         return strlen($this->_host) > 0 ? $this->_host : false;
0421     }
0422 
0423     /**
0424      * Returns true if and only if the host string passes validation. If no host is passed,
0425      * then the host contained in the instance variable is used.
0426      *
0427      * @param  string $host The HTTP host
0428      * @return boolean
0429      * @uses   Zend_Filter
0430      */
0431     public function validateHost($host = null)
0432     {
0433         if ($host === null) {
0434             $host = $this->_host;
0435         }
0436 
0437         // If the host is empty, then it is considered invalid
0438         if (strlen($host) === 0) {
0439             return false;
0440         }
0441 
0442         // Check the host against the allowed values; delegated to Zend_Filter.
0443         $validate = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_ALL);
0444 
0445         return $validate->isValid($host);
0446     }
0447 
0448     /**
0449      * Sets the host for the current URI, and returns the old host
0450      *
0451      * @param  string $host The HTTP host
0452      * @throws Zend_Uri_Exception When $host is nota valid HTTP host
0453      * @return string
0454      */
0455     public function setHost($host)
0456     {
0457         if ($this->validateHost($host) === false) {
0458             // require_once 'Zend/Uri/Exception.php';
0459             throw new Zend_Uri_Exception("Host \"$host\" is not a valid HTTP host");
0460         }
0461 
0462         $oldHost     = $this->_host;
0463         $this->_host = $host;
0464 
0465         return $oldHost;
0466     }
0467 
0468     /**
0469      * Returns the TCP port, or FALSE if none.
0470      *
0471      * @return string
0472      */
0473     public function getPort()
0474     {
0475         return strlen($this->_port) > 0 ? $this->_port : false;
0476     }
0477 
0478     /**
0479      * Returns true if and only if the TCP port string passes validation. If no port is passed,
0480      * then the port contained in the instance variable is used.
0481      *
0482      * @param  string $port The HTTP port
0483      * @return boolean
0484      */
0485     public function validatePort($port = null)
0486     {
0487         if ($port === null) {
0488             $port = $this->_port;
0489         }
0490 
0491         // If the port is empty, then it is considered valid
0492         if (strlen($port) === 0) {
0493             return true;
0494         }
0495 
0496         // Check the port against the allowed values
0497         return ctype_digit((string) $port) and 1 <= $port and $port <= 65535;
0498     }
0499 
0500     /**
0501      * Sets the port for the current URI, and returns the old port
0502      *
0503      * @param  string $port The HTTP port
0504      * @throws Zend_Uri_Exception When $port is not a valid HTTP port
0505      * @return string
0506      */
0507     public function setPort($port)
0508     {
0509         if ($this->validatePort($port) === false) {
0510             // require_once 'Zend/Uri/Exception.php';
0511             throw new Zend_Uri_Exception("Port \"$port\" is not a valid HTTP port.");
0512         }
0513 
0514         $oldPort     = $this->_port;
0515         $this->_port = $port;
0516 
0517         return $oldPort;
0518     }
0519 
0520     /**
0521      * Returns the path and filename portion of the URL.
0522      *
0523      * @return string
0524      */
0525     public function getPath()
0526     {
0527         return strlen($this->_path) > 0 ? $this->_path : '/';
0528     }
0529 
0530     /**
0531      * Returns true if and only if the path string passes validation. If no path is passed,
0532      * then the path contained in the instance variable is used.
0533      *
0534      * @param  string $path The HTTP path
0535      * @throws Zend_Uri_Exception When path validation fails
0536      * @return boolean
0537      */
0538     public function validatePath($path = null)
0539     {
0540         if ($path === null) {
0541             $path = $this->_path;
0542         }
0543 
0544         // If the path is empty, then it is considered valid
0545         if (strlen($path) === 0) {
0546             return true;
0547         }
0548 
0549         // Determine whether the path is well-formed
0550         $pattern = '/^' . $this->_regex['path'] . '$/';
0551         $status  = @preg_match($pattern, $path);
0552         if ($status === false) {
0553             // require_once 'Zend/Uri/Exception.php';
0554             throw new Zend_Uri_Exception('Internal error: path validation failed');
0555         }
0556 
0557         return (boolean) $status;
0558     }
0559 
0560     /**
0561      * Sets the path for the current URI, and returns the old path
0562      *
0563      * @param  string $path The HTTP path
0564      * @throws Zend_Uri_Exception When $path is not a valid HTTP path
0565      * @return string
0566      */
0567     public function setPath($path)
0568     {
0569         if ($this->validatePath($path) === false) {
0570             // require_once 'Zend/Uri/Exception.php';
0571             throw new Zend_Uri_Exception("Path \"$path\" is not a valid HTTP path");
0572         }
0573 
0574         $oldPath     = $this->_path;
0575         $this->_path = $path;
0576 
0577         return $oldPath;
0578     }
0579 
0580     /**
0581      * Returns the query portion of the URL (after ?), or FALSE if none.
0582      *
0583      * @return string
0584      */
0585     public function getQuery()
0586     {
0587         return strlen($this->_query) > 0 ? $this->_query : false;
0588     }
0589 
0590     /**
0591      * Returns the query portion of the URL (after ?) as a
0592      * key-value-array. If the query is empty an empty array
0593      * is returned
0594      *
0595      * @return array
0596      */
0597     public function getQueryAsArray()
0598     {
0599         $query = $this->getQuery();
0600         $querryArray = array();
0601         if ($query !== false) {
0602             parse_str($query, $querryArray);
0603         }
0604         return $querryArray;
0605     }
0606 
0607     /**
0608      * Returns true if and only if the query string passes validation. If no query is passed,
0609      * then the query string contained in the instance variable is used.
0610      *
0611      * @param  string $query The query to validate
0612      * @throws Zend_Uri_Exception When query validation fails
0613      * @return boolean
0614      * @link   http://www.faqs.org/rfcs/rfc2396.html
0615      */
0616     public function validateQuery($query = null)
0617     {
0618         if ($query === null) {
0619             $query = $this->_query;
0620         }
0621 
0622         // If query is empty, it is considered to be valid
0623         if (strlen($query) === 0) {
0624             return true;
0625         }
0626 
0627         // Determine whether the query is well-formed
0628         $pattern = '/^' . $this->_regex['uric'] . '*$/';
0629         $status  = @preg_match($pattern, $query);
0630         if ($status === false) {
0631             // require_once 'Zend/Uri/Exception.php';
0632             throw new Zend_Uri_Exception('Internal error: query validation failed');
0633         }
0634 
0635         return $status == 1;
0636     }
0637 
0638     /**
0639      * Add or replace params in the query string for the current URI, and
0640      * return the old query.
0641      *
0642      * @param  array $queryParams
0643      * @return string Old query string
0644      */
0645     public function addReplaceQueryParameters(array $queryParams)
0646     {
0647         $queryParams = array_merge($this->getQueryAsArray(), $queryParams);
0648         return $this->setQuery($queryParams);
0649     }
0650 
0651     /**
0652      * Remove params in the query string for the current URI, and
0653      * return the old query.
0654      *
0655      * @param  array $queryParamKeys
0656      * @return string Old query string
0657      */
0658     public function removeQueryParameters(array $queryParamKeys)
0659     {
0660         $queryParams = array_diff_key($this->getQueryAsArray(), array_fill_keys($queryParamKeys, 0));
0661         return $this->setQuery($queryParams);
0662     }
0663 
0664     /**
0665      * Set the query string for the current URI, and return the old query
0666      * string This method accepts both strings and arrays.
0667      *
0668      * @param  string|array $query The query string or array
0669      * @throws Zend_Uri_Exception When $query is not a valid query string
0670      * @return string              Old query string
0671      */
0672     public function setQuery($query)
0673     {
0674         $oldQuery = $this->_query;
0675 
0676         // If query is empty, set an empty string
0677         if (empty($query) === true) {
0678             $this->_query = '';
0679             return $oldQuery;
0680         }
0681 
0682         // If query is an array, make a string out of it
0683         if (is_array($query) === true) {
0684             $query = http_build_query($query, '', '&');
0685         } else {
0686             // If it is a string, make sure it is valid. If not parse and encode it
0687             $query = (string) $query;
0688             if ($this->validateQuery($query) === false) {
0689                 parse_str($query, $queryArray);
0690                 $query = http_build_query($queryArray, '', '&');
0691             }
0692         }
0693 
0694         // Make sure the query is valid, and set it
0695         if ($this->validateQuery($query) === false) {
0696             // require_once 'Zend/Uri/Exception.php';
0697             throw new Zend_Uri_Exception("'$query' is not a valid query string");
0698         }
0699 
0700         $this->_query = $query;
0701 
0702         return $oldQuery;
0703     }
0704 
0705     /**
0706      * Returns the fragment portion of the URL (after #), or FALSE if none.
0707      *
0708      * @return string|false
0709      */
0710     public function getFragment()
0711     {
0712         return strlen($this->_fragment) > 0 ? $this->_fragment : false;
0713     }
0714 
0715     /**
0716      * Returns true if and only if the fragment passes validation. If no fragment is passed,
0717      * then the fragment contained in the instance variable is used.
0718      *
0719      * @param  string $fragment Fragment of an URI
0720      * @throws Zend_Uri_Exception When fragment validation fails
0721      * @return boolean
0722      * @link   http://www.faqs.org/rfcs/rfc2396.html
0723      */
0724     public function validateFragment($fragment = null)
0725     {
0726         if ($fragment === null) {
0727             $fragment = $this->_fragment;
0728         }
0729 
0730         // If fragment is empty, it is considered to be valid
0731         if (strlen($fragment) === 0) {
0732             return true;
0733         }
0734 
0735         // Determine whether the fragment is well-formed
0736         $pattern = '/^' . $this->_regex['uric'] . '*$/';
0737         $status  = @preg_match($pattern, $fragment);
0738         if ($status === false) {
0739             // require_once 'Zend/Uri/Exception.php';
0740             throw new Zend_Uri_Exception('Internal error: fragment validation failed');
0741         }
0742 
0743         return (boolean) $status;
0744     }
0745 
0746     /**
0747      * Sets the fragment for the current URI, and returns the old fragment
0748      *
0749      * @param  string $fragment Fragment of the current URI
0750      * @throws Zend_Uri_Exception When $fragment is not a valid HTTP fragment
0751      * @return string
0752      */
0753     public function setFragment($fragment)
0754     {
0755         if ($this->validateFragment($fragment) === false) {
0756             // require_once 'Zend/Uri/Exception.php';
0757             throw new Zend_Uri_Exception("Fragment \"$fragment\" is not a valid HTTP fragment");
0758         }
0759 
0760         $oldFragment     = $this->_fragment;
0761         $this->_fragment = $fragment;
0762 
0763         return $oldFragment;
0764     }
0765 }