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