1: | <?php
|
2: |
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class HTMLPurifier_AttrValidator
|
9: | {
|
10: |
|
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: |
|
18: | public function validateToken($token, $config, $context)
|
19: | {
|
20: | $definition = $config->getHTMLDefinition();
|
21: | $e =& $context->get('ErrorCollector', true);
|
22: |
|
23: |
|
24: | $ok =& $context->get('IDAccumulator', true);
|
25: | if (!$ok) {
|
26: | $id_accumulator = HTMLPurifier_IDAccumulator::build($config, $context);
|
27: | $context->register('IDAccumulator', $id_accumulator);
|
28: | }
|
29: |
|
30: |
|
31: | $current_token =& $context->get('CurrentToken', true);
|
32: | if (!$current_token) {
|
33: | $context->register('CurrentToken', $token);
|
34: | }
|
35: |
|
36: | if (!$token instanceof HTMLPurifier_Token_Start &&
|
37: | !$token instanceof HTMLPurifier_Token_Empty
|
38: | ) {
|
39: | return;
|
40: | }
|
41: |
|
42: |
|
43: |
|
44: | $d_defs = $definition->info_global_attr;
|
45: |
|
46: |
|
47: | $attr = $token->attr;
|
48: |
|
49: |
|
50: |
|
51: | foreach ($definition->info_attr_transform_pre as $transform) {
|
52: | $attr = $transform->transform($o = $attr, $config, $context);
|
53: | if ($e) {
|
54: | if ($attr != $o) {
|
55: | $e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
|
56: | }
|
57: | }
|
58: | }
|
59: |
|
60: |
|
61: |
|
62: | foreach ($definition->info[$token->name]->attr_transform_pre as $transform) {
|
63: | $attr = $transform->transform($o = $attr, $config, $context);
|
64: | if ($e) {
|
65: | if ($attr != $o) {
|
66: | $e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
|
67: | }
|
68: | }
|
69: | }
|
70: |
|
71: |
|
72: |
|
73: |
|
74: | $defs = $definition->info[$token->name]->attr;
|
75: |
|
76: | $attr_key = false;
|
77: | $context->register('CurrentAttr', $attr_key);
|
78: |
|
79: |
|
80: |
|
81: | foreach ($attr as $attr_key => $value) {
|
82: |
|
83: |
|
84: | if (isset($defs[$attr_key])) {
|
85: |
|
86: | if ($defs[$attr_key] === false) {
|
87: |
|
88: |
|
89: |
|
90: |
|
91: |
|
92: | $result = false;
|
93: | } else {
|
94: |
|
95: | $result = $defs[$attr_key]->validate(
|
96: | $value,
|
97: | $config,
|
98: | $context
|
99: | );
|
100: | }
|
101: | } elseif (isset($d_defs[$attr_key])) {
|
102: |
|
103: |
|
104: | $result = $d_defs[$attr_key]->validate(
|
105: | $value,
|
106: | $config,
|
107: | $context
|
108: | );
|
109: | } else {
|
110: |
|
111: | $result = false;
|
112: | }
|
113: |
|
114: |
|
115: | if ($result === false || $result === null) {
|
116: |
|
117: |
|
118: | if ($e) {
|
119: | $e->send(E_ERROR, 'AttrValidator: Attribute removed');
|
120: | }
|
121: |
|
122: |
|
123: | unset($attr[$attr_key]);
|
124: | } elseif (is_string($result)) {
|
125: |
|
126: |
|
127: |
|
128: |
|
129: |
|
130: | $attr[$attr_key] = $result;
|
131: | } else {
|
132: |
|
133: | }
|
134: |
|
135: |
|
136: |
|
137: |
|
138: |
|
139: |
|
140: | }
|
141: |
|
142: | $context->destroy('CurrentAttr');
|
143: |
|
144: |
|
145: |
|
146: |
|
147: | foreach ($definition->info_attr_transform_post as $transform) {
|
148: | $attr = $transform->transform($o = $attr, $config, $context);
|
149: | if ($e) {
|
150: | if ($attr != $o) {
|
151: | $e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
|
152: | }
|
153: | }
|
154: | }
|
155: |
|
156: |
|
157: | foreach ($definition->info[$token->name]->attr_transform_post as $transform) {
|
158: | $attr = $transform->transform($o = $attr, $config, $context);
|
159: | if ($e) {
|
160: | if ($attr != $o) {
|
161: | $e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
|
162: | }
|
163: | }
|
164: | }
|
165: |
|
166: | $token->attr = $attr;
|
167: |
|
168: |
|
169: | if (!$current_token) {
|
170: | $context->destroy('CurrentToken');
|
171: | }
|
172: |
|
173: | }
|
174: |
|
175: |
|
176: | }
|
177: |
|
178: |
|
179: | |