Source for file Custom.php

Documentation is available at Custom.php

  1. <?php
  2.  
  3. /**
  4.  * Custom validation class, accepts DTD child definitions
  5.  * 
  6.  * @warning Currently this class is an all or nothing proposition, that is,
  7.  *           it will only give a bool return value.
  8.  * @note This class is currently not used by any code, although it is unit
  9.  *        tested.
  10.  */
  11. {
  12.     public $type = 'custom';
  13.     public $allow_empty = false;
  14.     /**
  15.      * Allowed child pattern as defined by the DTD
  16.      */
  17.     public $dtd_regex;
  18.     /**
  19.      * PCRE regex derived from $dtd_regex
  20.      * @private
  21.      */
  22.     private $_pcre_regex;
  23.     /**
  24.      * @param $dtd_regex Allowed child pattern from the DTD
  25.      */
  26.     public function __construct($dtd_regex{
  27.         $this->dtd_regex = $dtd_regex;
  28.         $this->_compileRegex();
  29.     }
  30.     /**
  31.      * Compiles the PCRE regex from a DTD regex ($dtd_regex to $_pcre_regex)
  32.      */
  33.     protected function _compileRegex({
  34.         $raw str_replace(' '''$this->dtd_regex);
  35.         if ($raw{0!= '('{
  36.             $raw "($raw)";
  37.         }
  38.         $el '[#a-zA-Z0-9_.-]+';
  39.         $reg $raw;
  40.         
  41.         // COMPLICATED! AND MIGHT BE BUGGY! I HAVE NO CLUE WHAT I'M
  42.         // DOING! Seriously: if there's problems, please report them.
  43.         
  44.         // collect all elements into the $elements array
  45.         preg_match_all("/$el/"$reg$matches);
  46.         foreach ($matches[0as $match{
  47.             $this->elements[$matchtrue;
  48.         }
  49.         
  50.         // setup all elements as parentheticals with leading commas
  51.         $reg preg_replace("/$el/"'(,\\0)'$reg);
  52.         
  53.         // remove commas when they were not solicited
  54.         $reg preg_replace("/([^,(|]\(+),/"'\\1'$reg);
  55.         
  56.         // remove all non-paranthetical commas: they are handled by first regex
  57.         $reg preg_replace("/,\(/"'('$reg);
  58.         
  59.         $this->_pcre_regex $reg;
  60.     }
  61.     public function validateChildren($tokens_of_children$config$context{
  62.         $list_of_children '';
  63.         $nesting 0// depth into the nest
  64.         foreach ($tokens_of_children as $token{
  65.             if (!empty($token->is_whitespace)) continue;
  66.             
  67.             $is_child ($nesting == 0)// direct
  68.             
  69.             if ($token instanceof HTMLPurifier_Token_Start{
  70.                 $nesting++;
  71.             elseif ($token instanceof HTMLPurifier_Token_End{
  72.                 $nesting--;
  73.             }
  74.             
  75.             if ($is_child{
  76.                 $list_of_children .= $token->name ',';
  77.             }
  78.         }
  79.         // add leading comma to deal with stray comma declarations
  80.         $list_of_children ',' rtrim($list_of_children',');
  81.         $okay =
  82.             preg_match(
  83.                 '/^,?'.$this->_pcre_regex.'$/',
  84.                 $list_of_children
  85.             );
  86.         
  87.         return (bool) $okay;
  88.     }
  89. }

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