Source for file URIParser.php

Documentation is available at URIParser.php

  1. <?php
  2.  
  3. /**
  4.  * Parses a URI into the components and fragment identifier as specified
  5.  * by RFC 3986.
  6.  */
  7. {
  8.     
  9.     /**
  10.      * Instance of HTMLPurifier_PercentEncoder to do normalization with.
  11.      */
  12.     protected $percentEncoder;
  13.     
  14.     public function __construct({
  15.         $this->percentEncoder = new HTMLPurifier_PercentEncoder();
  16.     }
  17.     
  18.     /**
  19.      * Parses a URI.
  20.      * @param $uri string URI to parse
  21.      * @return HTMLPurifier_URI representation of URI. This representation has
  22.      *          not been validated yet and may not conform to RFC.
  23.      */
  24.     public function parse($uri{
  25.         
  26.         $uri $this->percentEncoder->normalize($uri);
  27.         
  28.         // Regexp is as per Appendix B.
  29.         // Note that ["<>] are an addition to the RFC's recommended 
  30.         // characters, because they represent external delimeters.
  31.         $r_URI '!'.
  32.             '(([^:/?#"<>]+):)?'// 2. Scheme
  33.             '(//([^/?#"<>]*))?'// 4. Authority
  34.             '([^?#"<>]*)'.       // 5. Path
  35.             '(\?([^#"<>]*))?'.   // 7. Query
  36.             '(#([^"<>]*))?'.     // 8. Fragment
  37.             '!';
  38.         
  39.         $matches array();
  40.         $result preg_match($r_URI$uri$matches);
  41.         
  42.         if (!$resultreturn false// *really* invalid URI
  43.         
  44.         // seperate out parts
  45.         $scheme     !empty($matches[1]$matches[2null;
  46.         $authority  !empty($matches[3]$matches[4null;
  47.         $path       $matches[5]// always present, can be empty
  48.         $query      !empty($matches[6]$matches[7null;
  49.         $fragment   !empty($matches[8]$matches[9null;
  50.         
  51.         // further parse authority
  52.         if ($authority !== null{
  53.             $r_authority "/^((.+?)@)?(\[[^\]]+\]|[^:]*)(:(\d*))?/";
  54.             $matches array();
  55.             preg_match($r_authority$authority$matches);
  56.             $userinfo   !empty($matches[1]$matches[2null;
  57.             $host       !empty($matches[3]$matches[3'';
  58.             $port       !empty($matches[4]? (int) $matches[5null;
  59.         else {
  60.             $port $host $userinfo null;
  61.         }
  62.         
  63.         return new HTMLPurifier_URI(
  64.             $scheme$userinfo$host$port$path$query$fragment);
  65.     }
  66.     
  67. }

Documentation generated on Thu, 19 Jun 2008 18:50:30 -0400 by phpDocumentor 1.4.2