2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
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 // +----------------------------------------------------------------------+
22 // $Id: States.php 3188 2012-07-12 12:13:23Z ctrlaltca $
26 * @package System.Security.SafeHtml
27 * @version $Id: States.php 3188 2012-07-12 12:13:23Z ctrlaltca $
30 * Define parser states
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);
42 * StartingState searches for the start of any XML tag
43 * @package System.Security.SafeHtml
46 class TSax3_StartingState {
48 * @param TSax3_StateParser subclass
49 * @return constant TSAX3_STATE_TAG
52 function parse(&$context) {
53 $data = $context->scanUntilString('<');
55 $context->handler_object_data->
56 {$context->handler_method_data}($context->htmlsax, $data);
58 $context->IgnoreCharacter();
59 return TSax3_StateParser::TSAX3_STATE_TAG;
63 * Decides which state to move one from after StartingState
64 * @package System.Security.SafeHtml
67 class TSax3_TagState {
69 * @param TSax3_StateParser subclass
70 * @return constant the next state to move into
73 function parse(&$context) {
74 switch($context->ScanCharacter()) {
76 return TSax3_StateParser::TSAX3_STATE_CLOSING_TAG;
79 return TSax3_StateParser::TSAX3_STATE_PI;
82 return TSax3_StateParser::TSAX3_STATE_JASP;
85 return TSax3_StateParser::TSAX3_STATE_ESCAPE;
88 $context->unscanCharacter();
89 return TSax3_StateParser::TSAX3_STATE_OPENING_TAG;
94 * Dealing with closing XML tags
95 * @package System.Security.SafeHtml
98 class TSax3_ClosingTagState {
100 * @param TSax3_StateParser subclass
101 * @return constant TSAX3_STATE_START
104 function parse(&$context) {
105 $tag = $context->scanUntilCharacters('/>');
107 $char = $context->scanCharacter();
109 $char = $context->scanCharacter();
111 $context->unscanCharacter();
114 $context->handler_object_element->
115 {$context->handler_method_closing}($context->htmlsax, $tag, FALSE);
117 return TSax3_StateParser::TSAX3_STATE_START;
121 * Dealing with opening XML tags
122 * @package System.Security.SafeHtml
125 class TSax3_OpeningTagState {
128 * @param string attribute name
129 * @param string attribute value
132 * @see TSax3_AttributeStartState
134 function parseAttributes(&$context) {
135 $Attributes = array();
137 $context->ignoreWhitespace();
138 $attributename = $context->scanUntilCharacters("=/> \n\r\t");
139 while ($attributename != '') {
140 $attributevalue = NULL;
141 $context->ignoreWhitespace();
142 $char = $context->scanCharacter();
144 $context->ignoreWhitespace();
145 $char = $context->ScanCharacter();
147 $attributevalue= $context->scanUntilString('"');
148 $context->IgnoreCharacter();
149 } else if ($char == "'") {
150 $attributevalue = $context->scanUntilString("'");
151 $context->IgnoreCharacter();
153 $context->unscanCharacter();
155 $context->scanUntilCharacters("> \n\r\t");
157 } else if ($char !== NULL) {
158 $attributevalue = NULL;
159 $context->unscanCharacter();
161 $Attributes[$attributename] = $attributevalue;
163 $context->ignoreWhitespace();
164 $attributename = $context->scanUntilCharacters("=/> \n\r\t");
170 * @param TSax3_StateParser subclass
171 * @return constant TSAX3_STATE_START
174 function parse(&$context) {
175 $tag = $context->scanUntilCharacters("/> \n\r\t");
177 $this->attrs = array();
178 $Attributes = $this->parseAttributes($context);
179 $char = $context->scanCharacter();
181 $char = $context->scanCharacter();
183 $context->unscanCharacter();
185 $context->handler_object_element->
186 {$context->handler_method_opening}($context->htmlsax, $tag,
188 $context->handler_object_element->
189 {$context->handler_method_closing}($context->htmlsax, $tag,
192 $context->handler_object_element->
193 {$context->handler_method_opening}($context->htmlsax, $tag,
197 return TSax3_StateParser::TSAX3_STATE_START;
202 * Deals with XML escapes handling comments and CDATA correctly
203 * @package System.Security.SafeHtml
206 class TSax3_EscapeState {
208 * @param TSax3_StateParser subclass
209 * @return constant TSAX3_STATE_START
212 function parse(&$context) {
213 $char = $context->ScanCharacter();
215 $char = $context->ScanCharacter();
217 $context->unscanCharacter();
218 $context->unscanCharacter();
219 $text = $context->scanUntilString('-->');
220 $text .= $context->scanCharacter();
221 $text .= $context->scanCharacter();
223 $context->unscanCharacter();
224 $text = $context->scanUntilString('>');
226 } else if ( $char == '[') {
227 $context->unscanCharacter();
228 $text = $context->scanUntilString(']>');
229 $text.= $context->scanCharacter();
231 $context->unscanCharacter();
232 $text = $context->scanUntilString('>');
235 $context->IgnoreCharacter();
237 $context->handler_object_escape->
238 {$context->handler_method_escape}($context->htmlsax, $text);
240 return TSax3_StateParser::TSAX3_STATE_START;
244 * Deals with JASP/ASP markup
245 * @package System.Security.SafeHtml
248 class TSax3_JaspState {
250 * @param TSax3_StateParser subclass
251 * @return constant TSAX3_STATE_START
254 function parse(&$context) {
255 $text = $context->scanUntilString('%>');
257 $context->handler_object_jasp->
258 {$context->handler_method_jasp}($context->htmlsax, $text);
260 $context->IgnoreCharacter();
261 $context->IgnoreCharacter();
262 return TSax3_StateParser::TSAX3_STATE_START;
266 * Deals with XML processing instructions
267 * @package System.Security.SafeHtml
270 class TSax3_PiState {
272 * @param TSax3_StateParser subclass
273 * @return constant TSAX3_STATE_START
276 function parse(&$context) {
277 $target = $context->scanUntilCharacters(" \n\r\t");
278 $data = $context->scanUntilString('?>');
280 $context->handler_object_pi->
281 {$context->handler_method_pi}($context->htmlsax, $target, $data);
283 $context->IgnoreCharacter();
284 $context->IgnoreCharacter();
285 return TSax3_StateParser::TSAX3_STATE_START;