Microsoft_Uri
[ class tree: Microsoft_Uri ] [ index: Microsoft_Uri ] [ all elements ]

Source for file Http.php

Documentation is available at Http.php

  1. <?php
  2. /**
  3.  * Zend Framework
  4.  *
  5.  * LICENSE
  6.  *
  7.  * This source file is subject to the new BSD license that is bundled
  8.  * with this package in the file LICENSE.txt.
  9.  * It is also available through the world-wide-web at this URL:
  10.  * http://framework.zend.com/license/new-bsd
  11.  * If you did not receive a copy of the license and are unable to
  12.  * obtain it through the world-wide-web, please send an email
  13.  * to license@zend.com so we can send you a copy immediately.
  14.  *
  15.  * @category  Microsoft
  16.  * @package   Microsoft_Uri
  17.  * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18.  * @license   http://framework.zend.com/license/new-bsd     New BSD License
  19.  * @version   $Id: Http.php 19041 2009-11-19 15:19:07Z sgehrig $
  20.  */
  21.  
  22. /**
  23.  * @see Microsoft_AutoLoader
  24.  */
  25. require_once dirname(__FILE__'/../AutoLoader.php';
  26.  
  27. /**
  28.  * HTTP(S) URI handler
  29.  *
  30.  * @category  Microsoft
  31.  * @package   Microsoft_Uri
  32.  * @uses      Microsoft_Uri
  33.  * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  34.  * @license   http://framework.zend.com/license/new-bsd     New BSD License
  35.  */
  36. {
  37.     /**
  38.      * Character classes for validation regular expressions
  39.      */
  40.     const CHAR_ALNUM    = 'A-Za-z0-9';
  41.     const CHAR_MARK     = '-_.!~*\'()\[\]';
  42.     const CHAR_RESERVED = ';\/?:@&=+$,';
  43.     const CHAR_SEGMENT  = ':@&=+$,;';
  44.     const CHAR_UNWISE   = '{}|\\\\^`';
  45.  
  46.     /**
  47.      * HTTP username
  48.      *
  49.      * @var string 
  50.      */
  51.     protected $_username = '';
  52.  
  53.     /**
  54.      * HTTP password
  55.      *
  56.      * @var string 
  57.      */
  58.     protected $_password = '';
  59.  
  60.     /**
  61.      * HTTP host
  62.      *
  63.      * @var string 
  64.      */
  65.     protected $_host = '';
  66.  
  67.     /**
  68.      * HTTP post
  69.      *
  70.      * @var string 
  71.      */
  72.     protected $_port = '';
  73.  
  74.     /**
  75.      * HTTP part
  76.      *
  77.      * @var string 
  78.      */
  79.     protected $_path = '';
  80.  
  81.     /**
  82.      * HTTP query
  83.      *
  84.      * @var string 
  85.      */
  86.     protected $_query = '';
  87.  
  88.     /**
  89.      * HTTP fragment
  90.      *
  91.      * @var string 
  92.      */
  93.     protected $_fragment = '';
  94.  
  95.     /**
  96.      * Regular expression grammar rules for validation; values added by constructor
  97.      *
  98.      * @var array 
  99.      */
  100.     protected $_regex = array();
  101.  
  102.     /**
  103.      * Constructor accepts a string $scheme (e.g., http, https) and a scheme-specific part of the URI
  104.      * (e.g., example.com/path/to/resource?query=param#fragment)
  105.      *
  106.      * @param  string $scheme         The scheme of the URI
  107.      * @param  string $schemeSpecific The scheme-specific part of the URI
  108.      * @throws Microsoft_Uri_Exception When the URI is not valid
  109.      */
  110.     protected function __construct($scheme$schemeSpecific '')
  111.     {
  112.         // Set the scheme
  113.         $this->_scheme = $scheme;
  114.  
  115.         // Set up grammar rules for validation via regular expressions. These
  116.         // are to be used with slash-delimited regular expression strings.
  117.  
  118.         // Escaped special characters (eg. '%25' for '%')
  119.         $this->_regex['escaped']    '%[[:xdigit:]]{2}';
  120.  
  121.         // Unreserved characters
  122.         $this->_regex['unreserved''[' self::CHAR_ALNUM self::CHAR_MARK ']';
  123.  
  124.         // Segment can use escaped, unreserved or a set of additional chars
  125.         $this->_regex['segment']    '(?:' $this->_regex['escaped''|[' .
  126.             self::CHAR_ALNUM self::CHAR_MARK self::CHAR_SEGMENT '])*';
  127.  
  128.         // Path can be a series of segmets char strings seperated by '/'
  129.         $this->_regex['path']       '(?:\/(?:' $this->_regex['segment'')?)+';
  130.  
  131.         // URI characters can be escaped, alphanumeric, mark or reserved chars
  132.         $this->_regex['uric']       '(?:' $this->_regex['escaped''|[' .
  133.             self::CHAR_ALNUM self::CHAR_MARK self::CHAR_RESERVED .
  134.  
  135.         // If unwise chars are allowed, add them to the URI chars class
  136.             (self::$_config['allow_unwise'self::CHAR_UNWISE '''])';
  137.  
  138.         // If no scheme-specific part was supplied, the user intends to create
  139.         // a new URI with this object.  No further parsing is required.
  140.         if (strlen($schemeSpecific=== 0{
  141.             return;
  142.         }
  143.  
  144.         // Parse the scheme-specific URI parts into the instance variables.
  145.         $this->_parseUri($schemeSpecific);
  146.  
  147.         // Validate the URI
  148.         if ($this->valid(=== false{
  149.             require_once 'Microsoft/Uri/Exception.php';
  150.             throw new Microsoft_Uri_Exception('Invalid URI supplied');
  151.         }
  152.     }
  153.  
  154.     /**
  155.      * Creates a Microsoft_Uri_Http from the given string
  156.      *
  157.      * @param  string $uri String to create URI from, must start with
  158.      *                      'http://' or 'https://'
  159.      * @throws InvalidArgumentException  When the given $uri is not a string or
  160.      *                                    does not start with http:// or https://
  161.      * @throws Microsoft_Uri_Exception        When the given $uri is invalid
  162.      * @return Microsoft_Uri_Http 
  163.      */
  164.     public static function fromString($uri)
  165.     {
  166.         if (is_string($uri=== false{
  167.             require_once 'Microsoft/Uri/Exception.php';
  168.             throw new Microsoft_Uri_Exception('$uri is not a string');
  169.         }
  170.  
  171.         $uri            explode(':'$uri2);
  172.         $scheme         strtolower($uri[0]);
  173.         $schemeSpecific = isset($uri[1]=== true $uri[1'';
  174.  
  175.         if (in_array($schemearray('http''https')) === false{
  176.             require_once 'Microsoft/Uri/Exception.php';
  177.             throw new Microsoft_Uri_Exception("Invalid scheme: '$scheme'");
  178.         }
  179.  
  180.         $schemeHandler new Microsoft_Uri_Http($scheme$schemeSpecific);
  181.         return $schemeHandler;
  182.     }
  183.  
  184.     /**
  185.      * Parse the scheme-specific portion of the URI and place its parts into instance variables.
  186.      *
  187.      * @param  string $schemeSpecific The scheme-specific portion to parse
  188.      * @throws Microsoft_Uri_Exception When scheme-specific decoposition fails
  189.      * @throws Microsoft_Uri_Exception When authority decomposition fails
  190.      * @return void 
  191.      */
  192.     protected function _parseUri($schemeSpecific)
  193.     {
  194.         // High-level decomposition parser
  195.         $pattern '~^((//)([^/?#]*))([^?#]*)(\?([^#]*))?(#(.*))?$~';
  196.         $status  @preg_match($pattern$schemeSpecific$matches);
  197.         if ($status === false{
  198.             require_once 'Microsoft/Uri/Exception.php';
  199.             throw new Microsoft_Uri_Exception('Internal error: scheme-specific decomposition failed');
  200.         }
  201.  
  202.         // Failed decomposition; no further processing needed
  203.         if ($status === false{
  204.             return;
  205.         }
  206.  
  207.         // Save URI components that need no further decomposition
  208.         $this->_path     = isset($matches[4]=== true $matches[4'';
  209.         $this->_query    = isset($matches[6]=== true $matches[6'';
  210.         $this->_fragment = isset($matches[8]=== true $matches[8'';
  211.  
  212.         // Additional decomposition to get username, password, host, and port
  213.         $combo   = isset($matches[3]=== true $matches[3'';
  214.         $pattern '~^(([^:@]*)(:([^@]*))?@)?([^:]+)(:(.*))?$~';
  215.         $status  @preg_match($pattern$combo$matches);
  216.         if ($status === false{
  217.             require_once 'Microsoft/Uri/Exception.php';
  218.             throw new Microsoft_Uri_Exception('Internal error: authority decomposition failed');
  219.         }
  220.  
  221.         // Failed decomposition; no further processing needed
  222.         if ($status === false{
  223.             return;
  224.         }
  225.  
  226.         // Save remaining URI components
  227.         $this->_username = isset($matches[2]=== true $matches[2'';
  228.         $this->_password = isset($matches[4]=== true $matches[4'';
  229.         $this->_host     = isset($matches[5]=== true $matches[5'';
  230.         $this->_port     = isset($matches[7]=== true $matches[7'';
  231.  
  232.     }
  233.  
  234.     /**
  235.      * Returns a URI based on current values of the instance variables. If any
  236.      * part of the URI does not pass validation, then an exception is thrown.
  237.      *
  238.      * @throws Microsoft_Uri_Exception When one or more parts of the URI are invalid
  239.      * @return string 
  240.      */
  241.     public function getUri()
  242.     {
  243.         if ($this->valid(=== false{
  244.             require_once 'Microsoft/Uri/Exception.php';
  245.             throw new Microsoft_Uri_Exception('One or more parts of the URI are invalid');
  246.         }
  247.  
  248.         $password strlen($this->_password":$this->_password" '';
  249.         $auth     strlen($this->_username"$this->_username$password@'';
  250.         $port     strlen($this->_port":$this->_port" '';
  251.         $query    strlen($this->_query"?$this->_query" '';
  252.         $fragment strlen($this->_fragment"#$this->_fragment" '';
  253.  
  254.         return $this->_scheme
  255.              . '://'
  256.              . $auth
  257.              . $this->_host
  258.              . $port
  259.              . $this->_path
  260.              . $query
  261.              . $fragment;
  262.     }
  263.  
  264.     /**
  265.      * Validate the current URI from the instance variables. Returns true if and only if all
  266.      * parts pass validation.
  267.      *
  268.      * @return boolean 
  269.      */
  270.     public function valid()
  271.     {
  272.         // Return true if and only if all parts of the URI have passed validation
  273.         return $this->validateUsername()
  274.            and $this->validatePassword()
  275.            and $this->validateHost()
  276.            and $this->validatePort()
  277.            and $this->validatePath()
  278.            and $this->validateQuery()
  279.            and $this->validateFragment();
  280.     }
  281.  
  282.     /**
  283.      * Returns the username portion of the URL, or FALSE if none.
  284.      *
  285.      * @return string 
  286.      */
  287.     public function getUsername()
  288.     {
  289.         return strlen($this->_username$this->_username : false;
  290.     }
  291.  
  292.     /**
  293.      * Returns true if and only if the username passes validation. If no username is passed,
  294.      * then the username contained in the instance variable is used.
  295.      *
  296.      * @param  string $username The HTTP username
  297.      * @throws Microsoft_Uri_Exception When username validation fails
  298.      * @return boolean 
  299.      * @link   http://www.faqs.org/rfcs/rfc2396.html
  300.      */
  301.     public function validateUsername($username null)
  302.     {
  303.         if ($username === null{
  304.             $username $this->_username;
  305.         }
  306.  
  307.         // If the username is empty, then it is considered valid
  308.         if (strlen($username=== 0{
  309.             return true;
  310.         }
  311.  
  312.         // Check the username against the allowed values
  313.         $status @preg_match('/^(?:' $this->_regex['escaped''|[' .
  314.             self::CHAR_ALNUM self::CHAR_MARK ';:&=+$,' '])+$/'$username);
  315.  
  316.         if ($status === false{
  317.             require_once 'Microsoft/Uri/Exception.php';
  318.             throw new Microsoft_Uri_Exception('Internal error: username validation failed');
  319.         }
  320.  
  321.         return $status === 1;
  322.     }
  323.  
  324.     /**
  325.      * Sets the username for the current URI, and returns the old username
  326.      *
  327.      * @param  string $username The HTTP username
  328.      * @throws Microsoft_Uri_Exception When $username is not a valid HTTP username
  329.      * @return string 
  330.      */
  331.     public function setUsername($username)
  332.     {
  333.         if ($this->validateUsername($username=== false{
  334.             require_once 'Microsoft/Uri/Exception.php';
  335.             throw new Microsoft_Uri_Exception("Username \"$username\" is not a valid HTTP username");
  336.         }
  337.  
  338.         $oldUsername     $this->_username;
  339.         $this->_username = $username;
  340.  
  341.         return $oldUsername;
  342.     }
  343.  
  344.     /**
  345.      * Returns the password portion of the URL, or FALSE if none.
  346.      *
  347.      * @return string 
  348.      */
  349.     public function getPassword()
  350.     {
  351.         return strlen($this->_password$this->_password : false;
  352.     }
  353.  
  354.     /**
  355.      * Returns true if and only if the password passes validation. If no password is passed,
  356.      * then the password contained in the instance variable is used.
  357.      *
  358.      * @param  string $password The HTTP password
  359.      * @throws Microsoft_Uri_Exception When password validation fails
  360.      * @return boolean 
  361.      * @link   http://www.faqs.org/rfcs/rfc2396.html
  362.      */
  363.     public function validatePassword($password null)
  364.     {
  365.         if ($password === null{
  366.             $password $this->_password;
  367.         }
  368.  
  369.         // If the password is empty, then it is considered valid
  370.         if (strlen($password=== 0{
  371.             return true;
  372.         }
  373.  
  374.         // If the password is nonempty, but there is no username, then it is considered invalid
  375.         if (strlen($passwordand strlen($this->_username=== 0{
  376.             return false;
  377.         }
  378.  
  379.         // Check the password against the allowed values
  380.         $status @preg_match('/^(?:' $this->_regex['escaped''|[' .
  381.             self::CHAR_ALNUM self::CHAR_MARK ';:&=+$,' '])+$/'$password);
  382.  
  383.         if ($status === false{
  384.             require_once 'Microsoft/Uri/Exception.php';
  385.             throw new Microsoft_Uri_Exception('Internal error: password validation failed.');
  386.         }
  387.  
  388.         return $status == 1;
  389.     }
  390.  
  391.     /**
  392.      * Sets the password for the current URI, and returns the old password
  393.      *
  394.      * @param  string $password The HTTP password
  395.      * @throws Microsoft_Uri_Exception When $password is not a valid HTTP password
  396.      * @return string 
  397.      */
  398.     public function setPassword($password)
  399.     {
  400.         if ($this->validatePassword($password=== false{
  401.             require_once 'Microsoft/Uri/Exception.php';
  402.             throw new Microsoft_Uri_Exception("Password \"$password\" is not a valid HTTP password.");
  403.         }
  404.  
  405.         $oldPassword     $this->_password;
  406.         $this->_password = $password;
  407.  
  408.         return $oldPassword;
  409.     }
  410.  
  411.     /**
  412.      * Returns the domain or host IP portion of the URL, or FALSE if none.
  413.      *
  414.      * @return string 
  415.      */
  416.     public function getHost()
  417.     {
  418.         return strlen($this->_host$this->_host : false;
  419.     }
  420.  
  421.     /**
  422.      * Returns true if and only if the host string passes validation. If no host is passed,
  423.      * then the host contained in the instance variable is used.
  424.      *
  425.      * @param  string $host The HTTP host
  426.      * @return boolean 
  427.      * @uses   Microsoft_Filter
  428.      */
  429.     public function validateHost($host null)
  430.     {
  431.         if ($host === null{
  432.             $host $this->_host;
  433.         }
  434.  
  435.         // If the host is empty, then it is considered invalid
  436.         if (strlen($host=== 0{
  437.             return false;
  438.         }
  439.         
  440.         return true;
  441.     }
  442.  
  443.     /**
  444.      * Sets the host for the current URI, and returns the old host
  445.      *
  446.      * @param  string $host The HTTP host
  447.      * @throws Microsoft_Uri_Exception When $host is nota valid HTTP host
  448.      * @return string 
  449.      */
  450.     public function setHost($host)
  451.     {
  452.         if ($this->validateHost($host=== false{
  453.             require_once 'Microsoft/Uri/Exception.php';
  454.             throw new Microsoft_Uri_Exception("Host \"$host\" is not a valid HTTP host");
  455.         }
  456.  
  457.         $oldHost     $this->_host;
  458.         $this->_host = $host;
  459.  
  460.         return $oldHost;
  461.     }
  462.  
  463.     /**
  464.      * Returns the TCP port, or FALSE if none.
  465.      *
  466.      * @return string 
  467.      */
  468.     public function getPort()
  469.     {
  470.         return strlen($this->_port$this->_port : false;
  471.     }
  472.  
  473.     /**
  474.      * Returns true if and only if the TCP port string passes validation. If no port is passed,
  475.      * then the port contained in the instance variable is used.
  476.      *
  477.      * @param  string $port The HTTP port
  478.      * @return boolean 
  479.      */
  480.     public function validatePort($port null)
  481.     {
  482.         if ($port === null{
  483.             $port $this->_port;
  484.         }
  485.  
  486.         // If the port is empty, then it is considered valid
  487.         if (strlen($port=== 0{
  488.             return true;
  489.         }
  490.  
  491.         // Check the port against the allowed values
  492.         return ctype_digit((string) $portand <= $port and $port <= 65535;
  493.     }
  494.  
  495.     /**
  496.      * Sets the port for the current URI, and returns the old port
  497.      *
  498.      * @param  string $port The HTTP port
  499.      * @throws Microsoft_Uri_Exception When $port is not a valid HTTP port
  500.      * @return string 
  501.      */
  502.     public function setPort($port)
  503.     {
  504.         if ($this->validatePort($port=== false{
  505.             require_once 'Microsoft/Uri/Exception.php';
  506.             throw new Microsoft_Uri_Exception("Port \"$port\" is not a valid HTTP port.");
  507.         }
  508.  
  509.         $oldPort     $this->_port;
  510.         $this->_port = $port;
  511.  
  512.         return $oldPort;
  513.     }
  514.  
  515.     /**
  516.      * Returns the path and filename portion of the URL, or FALSE if none.
  517.      *
  518.      * @return string 
  519.      */
  520.     public function getPath()
  521.     {
  522.         return strlen($this->_path$this->_path : '/';
  523.     }
  524.  
  525.     /**
  526.      * Returns true if and only if the path string passes validation. If no path is passed,
  527.      * then the path contained in the instance variable is used.
  528.      *
  529.      * @param  string $path The HTTP path
  530.      * @throws Microsoft_Uri_Exception When path validation fails
  531.      * @return boolean 
  532.      */
  533.     public function validatePath($path null)
  534.     {
  535.         if ($path === null{
  536.             $path $this->_path;
  537.         }
  538.  
  539.         // If the path is empty, then it is considered valid
  540.         if (strlen($path=== 0{
  541.             return true;
  542.         }
  543.  
  544.         // Determine whether the path is well-formed
  545.         $pattern '/^' $this->_regex['path''$/';
  546.         $status  @preg_match($pattern$path);
  547.         if ($status === false{
  548.             require_once 'Microsoft/Uri/Exception.php';
  549.             throw new Microsoft_Uri_Exception('Internal error: path validation failed');
  550.         }
  551.  
  552.         return (boolean) $status;
  553.     }
  554.  
  555.     /**
  556.      * Sets the path for the current URI, and returns the old path
  557.      *
  558.      * @param  string $path The HTTP path
  559.      * @throws Microsoft_Uri_Exception When $path is not a valid HTTP path
  560.      * @return string 
  561.      */
  562.     public function setPath($path)
  563.     {
  564.         if ($this->validatePath($path=== false{
  565.             require_once 'Microsoft/Uri/Exception.php';
  566.             throw new Microsoft_Uri_Exception("Path \"$path\" is not a valid HTTP path");
  567.         }
  568.  
  569.         $oldPath     $this->_path;
  570.         $this->_path = $path;
  571.  
  572.         return $oldPath;
  573.     }
  574.  
  575.     /**
  576.      * Returns the query portion of the URL (after ?), or FALSE if none.
  577.      *
  578.      * @return string 
  579.      */
  580.     public function getQuery()
  581.     {
  582.         return strlen($this->_query$this->_query : false;
  583.     }
  584.  
  585.     /**
  586.      * Returns the query portion of the URL (after ?) as a
  587.      * key-value-array. If the query is empty an empty array
  588.      * is returned
  589.      *
  590.      * @return array 
  591.      */
  592.     public function getQueryAsArray()
  593.     {
  594.         $query $this->getQuery();
  595.         $querryArray array();
  596.         if ($query !== false{
  597.             parse_str($query$querryArray);
  598.         }
  599.         return $querryArray;
  600.     }
  601.  
  602.     /**
  603.      * Returns true if and only if the query string passes validation. If no query is passed,
  604.      * then the query string contained in the instance variable is used.
  605.      *
  606.      * @param  string $query The query to validate
  607.      * @throws Microsoft_Uri_Exception When query validation fails
  608.      * @return boolean 
  609.      * @link   http://www.faqs.org/rfcs/rfc2396.html
  610.      */
  611.     public function validateQuery($query null)
  612.     {
  613.         if ($query === null{
  614.             $query $this->_query;
  615.         }
  616.  
  617.         // If query is empty, it is considered to be valid
  618.         if (strlen($query=== 0{
  619.             return true;
  620.         }
  621.  
  622.         // Determine whether the query is well-formed
  623.         $pattern '/^' $this->_regex['uric''*$/';
  624.         $status  @preg_match($pattern$query);
  625.         if ($status === false{
  626.             require_once 'Microsoft/Uri/Exception.php';
  627.             throw new Microsoft_Uri_Exception('Internal error: query validation failed');
  628.         }
  629.  
  630.         return $status == 1;
  631.     }
  632.  
  633.     /**
  634.      * Add or replace params in the query string for the current URI, and
  635.      * return the old query.
  636.      *
  637.      * @param  array $queryParams 
  638.      * @return string Old query string
  639.      */
  640.     public function addReplaceQueryParameters(array $queryParams)
  641.     {
  642.         $queryParams array_merge($this->getQueryAsArray()$queryParams);
  643.         return $this->setQuery($queryParams);
  644.     }
  645.  
  646.     /**
  647.      * Remove params in the query string for the current URI, and
  648.      * return the old query.
  649.      *
  650.      * @param  array $queryParamKeys 
  651.      * @return string Old query string
  652.      */
  653.     public function removeQueryParameters(array $queryParamKeys)
  654.     {
  655.         $queryParams array_diff_key($this->getQueryAsArray()array_fill_keys($queryParamKeys0));
  656.         return $this->setQuery($queryParams);
  657.     
  658.  
  659.     /**
  660.      * Set the query string for the current URI, and return the old query
  661.      * string This method accepts both strings and arrays.
  662.      *
  663.      * @param  string|array$query The query string or array
  664.      * @throws Microsoft_Uri_Exception When $query is not a valid query string
  665.      * @return string              Old query string
  666.      */
  667.     public function setQuery($query)
  668.     {
  669.         $oldQuery $this->_query;
  670.  
  671.         // If query is empty, set an empty string
  672.         if (empty($query=== true{
  673.             $this->_query '';
  674.             return $oldQuery;
  675.         }
  676.  
  677.         // If query is an array, make a string out of it
  678.         if (is_array($query=== true{
  679.             $query http_build_query($query'''&');
  680.         else {
  681.             // If it is a string, make sure it is valid. If not parse and encode it
  682.             $query = (string) $query;
  683.             if ($this->validateQuery($query=== false{
  684.                 parse_str($query$queryArray);
  685.                 $query http_build_query($queryArray'''&');
  686.             }
  687.         }
  688.  
  689.         // Make sure the query is valid, and set it
  690.         if ($this->validateQuery($query=== false{
  691.             require_once 'Microsoft/Uri/Exception.php';
  692.             throw new Microsoft_Uri_Exception("'$query' is not a valid query string");
  693.         }
  694.  
  695.         $this->_query $query;
  696.  
  697.         return $oldQuery;
  698.     }
  699.  
  700.     /**
  701.      * Returns the fragment portion of the URL (after #), or FALSE if none.
  702.      *
  703.      * @return string|false
  704.      */
  705.     public function getFragment()
  706.     {
  707.         return strlen($this->_fragment$this->_fragment false;
  708.     }
  709.  
  710.     /**
  711.      * Returns true if and only if the fragment passes validation. If no fragment is passed,
  712.      * then the fragment contained in the instance variable is used.
  713.      *
  714.      * @param  string $fragment Fragment of an URI
  715.      * @throws Microsoft_Uri_Exception When fragment validation fails
  716.      * @return boolean 
  717.      * @link   http://www.faqs.org/rfcs/rfc2396.html
  718.      */
  719.     public function validateFragment($fragment null)
  720.     {
  721.         if ($fragment === null{
  722.             $fragment $this->_fragment;
  723.         }
  724.  
  725.         // If fragment is empty, it is considered to be valid
  726.         if (strlen($fragment=== 0{
  727.             return true;
  728.         }
  729.  
  730.         // Determine whether the fragment is well-formed
  731.         $pattern '/^' $this->_regex['uric''*$/';
  732.         $status  @preg_match($pattern$fragment);
  733.         if ($status === false{
  734.             require_once 'Microsoft/Uri/Exception.php';
  735.             throw new Microsoft_Uri_Exception('Internal error: fragment validation failed');
  736.         }
  737.  
  738.         return (boolean) $status;
  739.     }
  740.  
  741.     /**
  742.      * Sets the fragment for the current URI, and returns the old fragment
  743.      *
  744.      * @param  string $fragment Fragment of the current URI
  745.      * @throws Microsoft_Uri_Exception When $fragment is not a valid HTTP fragment
  746.      * @return string 
  747.      */
  748.     public function setFragment($fragment)
  749.     {
  750.         if ($this->validateFragment($fragment=== false{
  751.             require_once 'Microsoft/Uri/Exception.php';
  752.             throw new Microsoft_Uri_Exception("Fragment \"$fragment\" is not a valid HTTP fragment");
  753.         }
  754.  
  755.         $oldFragment     $this->_fragment;
  756.         $this->_fragment $fragment;
  757.  
  758.         return $oldFragment;
  759.     }
  760. }

Documentation generated on Wed, 18 May 2011 12:06:38 +0200 by phpDocumentor 1.4.3