Source for file PercentEncoder.php

Documentation is available at PercentEncoder.php

  1. <?php
  2.  
  3. /**
  4.  * Class that handles operations involving percent-encoding in URIs.
  5.  *
  6.  * @warning
  7.  *       Be careful when reusing instances of PercentEncoder. The object
  8.  *       you use for normalize() SHOULD NOT be used for encode(), or
  9.  *       vice-versa.
  10.  */
  11. {
  12.     
  13.     /**
  14.      * Reserved characters to preserve when using encode().
  15.      */
  16.     protected $preserve = array();
  17.     
  18.     /**
  19.      * String of characters that should be preserved while using encode().
  20.      */
  21.     public function __construct($preserve false{
  22.         // unreserved letters, ought to const-ify
  23.         for ($i 48$i <= 57;  $i++$this->preserve[$itrue// digits
  24.         for ($i 65$i <= 90;  $i++$this->preserve[$itrue// upper-case
  25.         for ($i 97$i <= 122$i++$this->preserve[$itrue// lower-case
  26.         $this->preserve[45true// Dash         -
  27.         $this->preserve[46true// Period       .
  28.         $this->preserve[95true// Underscore   _
  29.         $this->preserve[126]true// Tilde        ~
  30.         
  31.         // extra letters not to escape
  32.         if ($preserve !== false{
  33.             for ($i 0$c strlen($preserve)$i $c$i++{
  34.                 $this->preserve[ord($preserve[$i])true;
  35.             }
  36.         }
  37.     }
  38.     
  39.     /**
  40.      * Our replacement for urlencode, it encodes all non-reserved characters,
  41.      * as well as any extra characters that were instructed to be preserved.
  42.      * @note
  43.      *       Assumes that the string has already been normalized, making any
  44.      *       and all percent escape sequences valid. Percents will not be
  45.      *       re-escaped, regardless of their status in $preserve
  46.      * @param $string String to be encoded
  47.      * @return Encoded string.
  48.      */
  49.     public function encode($string{
  50.         $ret '';
  51.         for ($i 0$c strlen($string)$i $c$i++{
  52.             if ($string[$i!== '%' && !isset($this->preserve[$int ord($string[$i])]) ) {
  53.                 $ret .= '%' sprintf('%02X'$int);
  54.             else {
  55.                 $ret .= $string[$i];
  56.             }
  57.         }
  58.         return $ret;
  59.     }
  60.     
  61.     /**
  62.      * Fix up percent-encoding by decoding unreserved characters and normalizing.
  63.      * @warning This function is affected by $preserve, even though the
  64.      *           usual desired behavior is for this not to preserve those
  65.      *           characters. Be careful when reusing instances of PercentEncoder!
  66.      * @param $string String to normalize
  67.      */
  68.     public function normalize($string{
  69.         if ($string == ''return '';
  70.         $parts explode('%'$string);
  71.         $ret array_shift($parts);
  72.         foreach ($parts as $part{
  73.             $length strlen($part);
  74.             if ($length 2{
  75.                 $ret .= '%25' $part;
  76.                 continue;
  77.             }
  78.             $encoding substr($part02);
  79.             $text     substr($part2);
  80.             if (!ctype_xdigit($encoding)) {
  81.                 $ret .= '%25' $part;
  82.                 continue;
  83.             }
  84.             $int hexdec($encoding);
  85.             if (isset($this->preserve[$int])) {
  86.                 $ret .= chr($int$text;
  87.                 continue;
  88.             }
  89.             $encoding strtoupper($encoding);
  90.             $ret .= '%' $encoding $text;
  91.         }
  92.         return $ret;
  93.     }
  94.     
  95. }

Documentation generated on Thu, 19 Jun 2008 18:49:52 -0400 by phpDocumentor 1.4.2