HTMLPurifier 4.4.0
|
00001 <?php 00002 00009 class HTMLPurifier_ChildDef_Custom extends HTMLPurifier_ChildDef 00010 { 00011 public $type = 'custom'; 00012 public $allow_empty = false; 00016 public $dtd_regex; 00021 private $_pcre_regex; 00025 public function __construct($dtd_regex) { 00026 $this->dtd_regex = $dtd_regex; 00027 $this->_compileRegex(); 00028 } 00032 protected function _compileRegex() { 00033 $raw = str_replace(' ', '', $this->dtd_regex); 00034 if ($raw{0} != '(') { 00035 $raw = "($raw)"; 00036 } 00037 $el = '[#a-zA-Z0-9_.-]+'; 00038 $reg = $raw; 00039 00040 // COMPLICATED! AND MIGHT BE BUGGY! I HAVE NO CLUE WHAT I'M 00041 // DOING! Seriously: if there's problems, please report them. 00042 00043 // collect all elements into the $elements array 00044 preg_match_all("/$el/", $reg, $matches); 00045 foreach ($matches[0] as $match) { 00046 $this->elements[$match] = true; 00047 } 00048 00049 // setup all elements as parentheticals with leading commas 00050 $reg = preg_replace("/$el/", '(,\\0)', $reg); 00051 00052 // remove commas when they were not solicited 00053 $reg = preg_replace("/([^,(|]\(+),/", '\\1', $reg); 00054 00055 // remove all non-paranthetical commas: they are handled by first regex 00056 $reg = preg_replace("/,\(/", '(', $reg); 00057 00058 $this->_pcre_regex = $reg; 00059 } 00060 public function validateChildren($tokens_of_children, $config, $context) { 00061 $list_of_children = ''; 00062 $nesting = 0; // depth into the nest 00063 foreach ($tokens_of_children as $token) { 00064 if (!empty($token->is_whitespace)) continue; 00065 00066 $is_child = ($nesting == 0); // direct 00067 00068 if ($token instanceof HTMLPurifier_Token_Start) { 00069 $nesting++; 00070 } elseif ($token instanceof HTMLPurifier_Token_End) { 00071 $nesting--; 00072 } 00073 00074 if ($is_child) { 00075 $list_of_children .= $token->name . ','; 00076 } 00077 } 00078 // add leading comma to deal with stray comma declarations 00079 $list_of_children = ',' . rtrim($list_of_children, ','); 00080 $okay = 00081 preg_match( 00082 '/^,?'.$this->_pcre_regex.'$/', 00083 $list_of_children 00084 ); 00085 00086 return (bool) $okay; 00087 } 00088 } 00089 00090 // vim: et sw=4 sts=4