| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* vim: set expandtab tabstop=4 shiftwidth=4: */
|
|
|
3 |
//
|
|
|
4 |
// +----------------------------------------------------------------------+
|
|
|
5 |
// | PHP Version 4 |
|
|
|
6 |
// +----------------------------------------------------------------------+
|
|
|
7 |
// | Copyright (c) 1997-2002 The PHP Group |
|
|
|
8 |
// +----------------------------------------------------------------------+
|
|
|
9 |
// | This source file is subject to version 2.02 of the PHP license, |
|
|
|
10 |
// | that is bundled with this package in the file LICENSE, and is |
|
|
|
11 |
// | available at through the world-wide-web at |
|
|
|
12 |
// | http://www.php.net/license/3_0.txt. |
|
|
|
13 |
// | If you did not receive a copy of the PHP license and are unable to |
|
|
|
14 |
// | obtain it through the world-wide-web, please send a note to |
|
|
|
15 |
// | license@php.net so we can mail you a copy immediately. |
|
|
|
16 |
// +----------------------------------------------------------------------+
|
|
|
17 |
// | Authors: Alexander Zhukov <alex@veresk.ru> Original port from Python |
|
|
|
18 |
// | Authors: Harry Fuecks <hfuecks@phppatterns.com> Port to PEAR + more |
|
|
|
19 |
// | Authors: Many @ Sitepointforums Advanced PHP Forums |
|
|
|
20 |
// +----------------------------------------------------------------------+
|
|
|
21 |
//
|
|
|
22 |
// $Id: States.php 1398 2006-09-08 19:31:03Z xue $
|
|
|
23 |
//
|
|
|
24 |
/**
|
|
|
25 |
* Parsing states.
|
|
|
26 |
* @package System.Security.SafeHtml
|
|
|
27 |
* @version $Id: States.php 1398 2006-09-08 19:31:03Z xue $
|
|
|
28 |
*/
|
|
|
29 |
/**
|
|
|
30 |
* Define parser states
|
|
|
31 |
*/
|
|
|
32 |
/*define('TSAX3_STATE_STOP', 0);
|
|
|
33 |
define('TSAX3_STATE_START', 1);
|
|
|
34 |
define('TSAX3_STATE_TAG', 2);
|
|
|
35 |
define('TSAX3_STATE_OPENING_TAG', 3);
|
|
|
36 |
define('TSAX3_STATE_CLOSING_TAG', 4);
|
|
|
37 |
define('TSAX3_STATE_ESCAPE', 6);
|
|
|
38 |
define('TSAX3_STATE_JASP', 7);
|
|
|
39 |
define('TSAX3_STATE_PI', 8);
|
|
|
40 |
*/
|
|
|
41 |
/**
|
|
|
42 |
* StartingState searches for the start of any XML tag
|
|
|
43 |
* @package System.Security.SafeHtml
|
|
|
44 |
* @access protected
|
|
|
45 |
*/
|
|
|
46 |
class TSax3_StartingState {
|
|
|
47 |
/**
|
|
|
48 |
* @param TSax3_StateParser subclass
|
|
|
49 |
* @return constant TSAX3_STATE_TAG
|
|
|
50 |
* @access protected
|
|
|
51 |
*/
|
|
|
52 |
function parse(&$context) {
|
|
|
53 |
$data = $context->scanUntilString('<');
|
|
|
54 |
if ($data != '') {
|
|
|
55 |
$context->handler_object_data->
|
|
|
56 |
{$context->handler_method_data}($context->htmlsax, $data);
|
|
|
57 |
}
|
|
|
58 |
$context->IgnoreCharacter();
|
|
|
59 |
return TSax3_StateParser::TSAX3_STATE_TAG;
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
/**
|
|
|
63 |
* Decides which state to move one from after StartingState
|
|
|
64 |
* @package System.Security.SafeHtml
|
|
|
65 |
* @access protected
|
|
|
66 |
*/
|
|
|
67 |
class TSax3_TagState {
|
|
|
68 |
/**
|
|
|
69 |
* @param TSax3_StateParser subclass
|
|
|
70 |
* @return constant the next state to move into
|
|
|
71 |
* @access protected
|
|
|
72 |
*/
|
|
|
73 |
function parse(&$context) {
|
|
|
74 |
switch($context->ScanCharacter()) {
|
|
|
75 |
case '/':
|
|
|
76 |
return TSax3_StateParser::TSAX3_STATE_CLOSING_TAG;
|
|
|
77 |
break;
|
|
|
78 |
case '?':
|
|
|
79 |
return TSax3_StateParser::TSAX3_STATE_PI;
|
|
|
80 |
break;
|
|
|
81 |
case '%':
|
|
|
82 |
return TSax3_StateParser::TSAX3_STATE_JASP;
|
|
|
83 |
break;
|
|
|
84 |
case '!':
|
|
|
85 |
return TSax3_StateParser::TSAX3_STATE_ESCAPE;
|
|
|
86 |
break;
|
|
|
87 |
default:
|
|
|
88 |
$context->unscanCharacter();
|
|
|
89 |
return TSax3_StateParser::TSAX3_STATE_OPENING_TAG;
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
/**
|
|
|
94 |
* Dealing with closing XML tags
|
|
|
95 |
* @package System.Security.SafeHtml
|
|
|
96 |
* @access protected
|
|
|
97 |
*/
|
|
|
98 |
class TSax3_ClosingTagState {
|
|
|
99 |
/**
|
|
|
100 |
* @param TSax3_StateParser subclass
|
|
|
101 |
* @return constant TSAX3_STATE_START
|
|
|
102 |
* @access protected
|
|
|
103 |
*/
|
|
|
104 |
function parse(&$context) {
|
|
|
105 |
$tag = $context->scanUntilCharacters('/>');
|
|
|
106 |
if ($tag != '') {
|
|
|
107 |
$char = $context->scanCharacter();
|
|
|
108 |
if ($char == '/') {
|
|
|
109 |
$char = $context->scanCharacter();
|
|
|
110 |
if ($char != '>') {
|
|
|
111 |
$context->unscanCharacter();
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
$context->handler_object_element->
|
|
|
115 |
{$context->handler_method_closing}($context->htmlsax, $tag, FALSE);
|
|
|
116 |
}
|
|
|
117 |
return TSax3_StateParser::TSAX3_STATE_START;
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
/**
|
|
|
121 |
* Dealing with opening XML tags
|
|
|
122 |
* @package System.Security.SafeHtml
|
|
|
123 |
* @access protected
|
|
|
124 |
*/
|
|
|
125 |
class TSax3_OpeningTagState {
|
|
|
126 |
/**
|
|
|
127 |
* Handles attributes
|
|
|
128 |
* @param string attribute name
|
|
|
129 |
* @param string attribute value
|
|
|
130 |
* @return void
|
|
|
131 |
* @access protected
|
|
|
132 |
* @see TSax3_AttributeStartState
|
|
|
133 |
*/
|
|
|
134 |
function parseAttributes(&$context) {
|
|
|
135 |
$Attributes = array();
|
|
|
136 |
|
|
|
137 |
$context->ignoreWhitespace();
|
|
|
138 |
$attributename = $context->scanUntilCharacters("=/> \n\r\t");
|
|
|
139 |
while ($attributename != '') {
|
|
|
140 |
$attributevalue = NULL;
|
|
|
141 |
$context->ignoreWhitespace();
|
|
|
142 |
$char = $context->scanCharacter();
|
|
|
143 |
if ($char == '=') {
|
|
|
144 |
$context->ignoreWhitespace();
|
|
|
145 |
$char = $context->ScanCharacter();
|
|
|
146 |
if ($char == '"') {
|
|
|
147 |
$attributevalue= $context->scanUntilString('"');
|
|
|
148 |
$context->IgnoreCharacter();
|
|
|
149 |
} else if ($char == "'") {
|
|
|
150 |
$attributevalue = $context->scanUntilString("'");
|
|
|
151 |
$context->IgnoreCharacter();
|
|
|
152 |
} else {
|
|
|
153 |
$context->unscanCharacter();
|
|
|
154 |
$attributevalue =
|
|
|
155 |
$context->scanUntilCharacters("> \n\r\t");
|
|
|
156 |
}
|
|
|
157 |
} else if ($char !== NULL) {
|
|
|
158 |
$attributevalue = NULL;
|
|
|
159 |
$context->unscanCharacter();
|
|
|
160 |
}
|
|
|
161 |
$Attributes[$attributename] = $attributevalue;
|
|
|
162 |
|
|
|
163 |
$context->ignoreWhitespace();
|
|
|
164 |
$attributename = $context->scanUntilCharacters("=/> \n\r\t");
|
|
|
165 |
}
|
|
|
166 |
return $Attributes;
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* @param TSax3_StateParser subclass
|
|
|
171 |
* @return constant TSAX3_STATE_START
|
|
|
172 |
* @access protected
|
|
|
173 |
*/
|
|
|
174 |
function parse(&$context) {
|
|
|
175 |
$tag = $context->scanUntilCharacters("/> \n\r\t");
|
|
|
176 |
if ($tag != '') {
|
|
|
177 |
$this->attrs = array();
|
|
|
178 |
$Attributes = $this->parseAttributes($context);
|
|
|
179 |
$char = $context->scanCharacter();
|
|
|
180 |
if ($char == '/') {
|
|
|
181 |
$char = $context->scanCharacter();
|
|
|
182 |
if ($char != '>') {
|
|
|
183 |
$context->unscanCharacter();
|
|
|
184 |
}
|
|
|
185 |
$context->handler_object_element->
|
|
|
186 |
{$context->handler_method_opening}($context->htmlsax, $tag,
|
|
|
187 |
$Attributes, TRUE);
|
|
|
188 |
$context->handler_object_element->
|
|
|
189 |
{$context->handler_method_closing}($context->htmlsax, $tag,
|
|
|
190 |
TRUE);
|
|
|
191 |
} else {
|
|
|
192 |
$context->handler_object_element->
|
|
|
193 |
{$context->handler_method_opening}($context->htmlsax, $tag,
|
|
|
194 |
$Attributes, FALSE);
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
return TSax3_StateParser::TSAX3_STATE_START;
|
|
|
198 |
}
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
* Deals with XML escapes handling comments and CDATA correctly
|
|
|
203 |
* @package System.Security.SafeHtml
|
|
|
204 |
* @access protected
|
|
|
205 |
*/
|
|
|
206 |
class TSax3_EscapeState {
|
|
|
207 |
/**
|
|
|
208 |
* @param TSax3_StateParser subclass
|
|
|
209 |
* @return constant TSAX3_STATE_START
|
|
|
210 |
* @access protected
|
|
|
211 |
*/
|
|
|
212 |
function parse(&$context) {
|
|
|
213 |
$char = $context->ScanCharacter();
|
|
|
214 |
if ($char == '-') {
|
|
|
215 |
$char = $context->ScanCharacter();
|
|
|
216 |
if ($char == '-') {
|
|
|
217 |
$context->unscanCharacter();
|
|
|
218 |
$context->unscanCharacter();
|
|
|
219 |
$text = $context->scanUntilString('-->');
|
|
|
220 |
$text .= $context->scanCharacter();
|
|
|
221 |
$text .= $context->scanCharacter();
|
|
|
222 |
} else {
|
|
|
223 |
$context->unscanCharacter();
|
|
|
224 |
$text = $context->scanUntilString('>');
|
|
|
225 |
}
|
|
|
226 |
} else if ( $char == '[') {
|
|
|
227 |
$context->unscanCharacter();
|
|
|
228 |
$text = $context->scanUntilString(']>');
|
|
|
229 |
$text.= $context->scanCharacter();
|
|
|
230 |
} else {
|
|
|
231 |
$context->unscanCharacter();
|
|
|
232 |
$text = $context->scanUntilString('>');
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
$context->IgnoreCharacter();
|
|
|
236 |
if ($text != '') {
|
|
|
237 |
$context->handler_object_escape->
|
|
|
238 |
{$context->handler_method_escape}($context->htmlsax, $text);
|
|
|
239 |
}
|
|
|
240 |
return TSax3_StateParser::TSAX3_STATE_START;
|
|
|
241 |
}
|
|
|
242 |
}
|
|
|
243 |
/**
|
|
|
244 |
* Deals with JASP/ASP markup
|
|
|
245 |
* @package System.Security.SafeHtml
|
|
|
246 |
* @access protected
|
|
|
247 |
*/
|
|
|
248 |
class TSax3_JaspState {
|
|
|
249 |
/**
|
|
|
250 |
* @param TSax3_StateParser subclass
|
|
|
251 |
* @return constant TSAX3_STATE_START
|
|
|
252 |
* @access protected
|
|
|
253 |
*/
|
|
|
254 |
function parse(&$context) {
|
|
|
255 |
$text = $context->scanUntilString('%>');
|
|
|
256 |
if ($text != '') {
|
|
|
257 |
$context->handler_object_jasp->
|
|
|
258 |
{$context->handler_method_jasp}($context->htmlsax, $text);
|
|
|
259 |
}
|
|
|
260 |
$context->IgnoreCharacter();
|
|
|
261 |
$context->IgnoreCharacter();
|
|
|
262 |
return TSax3_StateParser::TSAX3_STATE_START;
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
/**
|
|
|
266 |
* Deals with XML processing instructions
|
|
|
267 |
* @package System.Security.SafeHtml
|
|
|
268 |
* @access protected
|
|
|
269 |
*/
|
|
|
270 |
class TSax3_PiState {
|
|
|
271 |
/**
|
|
|
272 |
* @param TSax3_StateParser subclass
|
|
|
273 |
* @return constant TSAX3_STATE_START
|
|
|
274 |
* @access protected
|
|
|
275 |
*/
|
|
|
276 |
function parse(&$context) {
|
|
|
277 |
$target = $context->scanUntilCharacters(" \n\r\t");
|
|
|
278 |
$data = $context->scanUntilString('?>');
|
|
|
279 |
if ($data != '') {
|
|
|
280 |
$context->handler_object_pi->
|
|
|
281 |
{$context->handler_method_pi}($context->htmlsax, $target, $data);
|
|
|
282 |
}
|
|
|
283 |
$context->IgnoreCharacter();
|
|
|
284 |
$context->IgnoreCharacter();
|
|
|
285 |
return TSax3_StateParser::TSAX3_STATE_START;
|
|
|
286 |
}
|
|
|
287 |
}
|
|
|
288 |
?>
|