Source for file EntityParser.php

Documentation is available at EntityParser.php

  1. <?php
  2.  
  3. // if want to implement error collecting here, we'll need to use some sort
  4. // of global data (probably trigger_error) because it's impossible to pass
  5. // $config or $context to the callback functions.
  6.  
  7. /**
  8.  * Handles referencing and derefencing character entities
  9.  */
  10. {
  11.     
  12.     /**
  13.      * Reference to entity lookup table.
  14.      */
  15.     protected $_entity_lookup;
  16.     
  17.     /**
  18.      * Callback regex string for parsing entities.
  19.      */                             
  20.     protected $_substituteEntitiesRegex =
  21. '/&(?:[#]x([a-fA-F0-9]+)|[#]0*(\d+)|([A-Za-z_:][A-Za-z0-9.\-_:]*));?/';
  22. //     1. hex             2. dec      3. string (XML style)
  23.     
  24.     
  25.     /**
  26.      * Decimal to parsed string conversion table for special entities.
  27.      */
  28.     protected $_special_dec2str =
  29.             array(
  30.                     34 => '"',
  31.                     38 => '&',
  32.                     39 => "'",
  33.                     60 => '<',
  34.                     62 => '>'
  35.             );
  36.     
  37.     /**
  38.      * Stripped entity names to decimal conversion table for special entities.
  39.      */
  40.     protected $_special_ent2dec =
  41.             array(
  42.                     'quot' => 34,
  43.                     'amp'  => 38,
  44.                     'lt'   => 60,
  45.                     'gt'   => 62
  46.             );
  47.     
  48.     /**
  49.      * Substitutes non-special entities with their parsed equivalents. Since
  50.      * running this whenever you have parsed character is t3h 5uck, we run
  51.      * it before everything else.
  52.      * 
  53.      * @param $string String to have non-special entities parsed.
  54.      * @returns Parsed string.
  55.      */
  56.     public function substituteNonSpecialEntities($string{
  57.         // it will try to detect missing semicolons, but don't rely on it
  58.         return preg_replace_callback(
  59.             $this->_substituteEntitiesRegex,
  60.             array($this'nonSpecialEntityCallback'),
  61.             $string
  62.             );
  63.     }
  64.     
  65.     /**
  66.      * Callback function for substituteNonSpecialEntities() that does the work.
  67.      * 
  68.      * @param $matches  PCRE matches array, with 0 the entire match, and
  69.      *                   either index 1, 2 or 3 set with a hex value, dec value,
  70.      *                   or string (respectively).
  71.      * @returns Replacement string.
  72.      */
  73.     
  74.     protected function nonSpecialEntityCallback($matches{
  75.         // replaces all but big five
  76.         $entity $matches[0];
  77.         $is_num (@$matches[0][1=== '#');
  78.         if ($is_num{
  79.             $is_hex (@$entity[2=== 'x');
  80.             $code $is_hex hexdec($matches[1]: (int) $matches[2];
  81.             
  82.             // abort for special characters
  83.             if (isset($this->_special_dec2str[$code]))  return $entity;
  84.             
  85.             return HTMLPurifier_Encoder::unichr($code);
  86.         else {
  87.             if (isset($this->_special_ent2dec[$matches[3]])) return $entity;
  88.             if (!$this->_entity_lookup{
  89.                 $this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
  90.             }
  91.             if (isset($this->_entity_lookup->table[$matches[3]])) {
  92.                 return $this->_entity_lookup->table[$matches[3]];
  93.             else {
  94.                 return $entity;
  95.             }
  96.         }
  97.     }
  98.     
  99.     /**
  100.      * Substitutes only special entities with their parsed equivalents.
  101.      * 
  102.      * @notice We try to avoid calling this function because otherwise, it
  103.      *  would have to be called a lot (for every parsed section).
  104.      * 
  105.      * @param $string String to have non-special entities parsed.
  106.      * @returns Parsed string.
  107.      */
  108.     public function substituteSpecialEntities($string{
  109.         return preg_replace_callback(
  110.             $this->_substituteEntitiesRegex,
  111.             array($this'specialEntityCallback'),
  112.             $string);
  113.     }
  114.     
  115.     /**
  116.      * Callback function for substituteSpecialEntities() that does the work.
  117.      * 
  118.      * This callback has same syntax as nonSpecialEntityCallback().
  119.      * 
  120.      * @param $matches  PCRE-style matches array, with 0 the entire match, and
  121.      *                   either index 1, 2 or 3 set with a hex value, dec value,
  122.      *                   or string (respectively).
  123.      * @returns Replacement string.
  124.      */
  125.     protected function specialEntityCallback($matches{
  126.         $entity $matches[0];
  127.         $is_num (@$matches[0][1=== '#');
  128.         if ($is_num{
  129.             $is_hex (@$entity[2=== 'x');
  130.             $int $is_hex hexdec($matches[1]: (int) $matches[2];
  131.             return isset($this->_special_dec2str[$int]?
  132.                 $this->_special_dec2str[$int:
  133.                 $entity;
  134.         else {
  135.             return isset($this->_special_ent2dec[$matches[3]]?
  136.                 $this->_special_ent2dec[$matches[3]] :
  137.                 $entity;
  138.         }
  139.     }
  140.     
  141. }

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