Completed
Push — namespace2 ( 791eac...5c23fb )
by Fabio
08:41
created

TSax3_EscapeState::parse()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 30
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 25
nc 8
nop 1
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
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
// | [email protected] so we can mail you a copy immediately.               |
16
// +----------------------------------------------------------------------+
17
// | Authors: Alexander Zhukov <[email protected]> Original port from Python |
18
// | Authors: Harry Fuecks <[email protected]> Port to PEAR + more  |
19
// | Authors: Many @ Sitepointforums Advanced PHP Forums                  |
20
// +----------------------------------------------------------------------+
21
//
22
// $Id: States.php 3188 2012-07-12 12:13:23Z ctrlaltca $
23
//
24
/**
25
* Parsing states.
26
* @package System.Security.SafeHtml
27
*/
28
/**
29
* Define parser states
30
*/
31
/*define('TSAX3_STATE_STOP', 0);
32
define('TSAX3_STATE_START', 1);
33
define('TSAX3_STATE_TAG', 2);
34
define('TSAX3_STATE_OPENING_TAG', 3);
35
define('TSAX3_STATE_CLOSING_TAG', 4);
36
define('TSAX3_STATE_ESCAPE', 6);
37
define('TSAX3_STATE_JASP', 7);
38
define('TSAX3_STATE_PI', 8);
39
*/
40
41
namespace Prado\Vendor\SafeHtml\HTMLSax3;
42
43
/**
44
* StartingState searches for the start of any XML tag
45
* @package System.Security.SafeHtml
46
* @access protected
47
*/
48
class TSax3_StartingState  {
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
Coding Style introduced by
Expected 1 space after class name; 2 found
Loading history...
49
    /**
50
    * @param TSax3_StateParser subclass
51
    * @return constant TSAX3_STATE_TAG
52
    * @access protected
53
    */
54
    function parse(&$context) {
55
        $data = $context->scanUntilString('<');
56
        if ($data != '') {
57
            $context->handler_object_data->
58
                {$context->handler_method_data}($context->htmlsax, $data);
59
        }
60
        $context->IgnoreCharacter();
61
        return TSax3_StateParser::TSAX3_STATE_TAG;
62
    }
63
}
64
/**
65
* Decides which state to move one from after StartingState
66
* @package System.Security.SafeHtml
67
* @access protected
68
*/
69
class TSax3_TagState {
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
70
    /**
71
    * @param TSax3_StateParser subclass
72
    * @return constant the next state to move into
73
    * @access protected
74
    */
75
    function parse(&$context) {
76
        switch($context->ScanCharacter()) {
77
        case '/':
78
            return TSax3_StateParser::TSAX3_STATE_CLOSING_TAG;
79
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
80
        case '?':
81
            return TSax3_StateParser::TSAX3_STATE_PI;
82
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
83
        case '%':
84
            return TSax3_StateParser::TSAX3_STATE_JASP;
85
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
86
        case '!':
87
            return TSax3_StateParser::TSAX3_STATE_ESCAPE;
88
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
89
        default:
90
            $context->unscanCharacter();
91
            return TSax3_StateParser::TSAX3_STATE_OPENING_TAG;
92
        }
93
    }
94
}
95
/**
96
* Dealing with closing XML tags
97
* @package System.Security.SafeHtml
98
* @access protected
99
*/
100
class TSax3_ClosingTagState {
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
101
    /**
102
    * @param TSax3_StateParser subclass
103
    * @return constant TSAX3_STATE_START
104
    * @access protected
105
    */
106
    function parse(&$context) {
107
        $tag = $context->scanUntilCharacters('/>');
108
        if ($tag != '') {
109
            $char = $context->scanCharacter();
110
            if ($char == '/') {
111
                $char = $context->scanCharacter();
112
                if ($char != '>') {
113
                    $context->unscanCharacter();
114
                }
115
            }
116
            $context->handler_object_element->
117
                {$context->handler_method_closing}($context->htmlsax, $tag, FALSE);
118
        }
119
        return TSax3_StateParser::TSAX3_STATE_START;
120
    }
121
}
122
/**
123
* Dealing with opening XML tags
124
* @package System.Security.SafeHtml
125
* @access protected
126
*/
127
class TSax3_OpeningTagState {
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
128
    /**
129
    * Handles attributes
130
    * @param string attribute name
131
    * @param string attribute value
132
    * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
133
    * @access protected
134
    * @see TSax3_AttributeStartState
135
    */
136
    function parseAttributes(&$context) {
137
        $Attributes = array();
138
139
        $context->ignoreWhitespace();
140
        $attributename = $context->scanUntilCharacters("=/> \n\r\t");
141
        while ($attributename != '') {
142
            $attributevalue = NULL;
143
            $context->ignoreWhitespace();
144
            $char = $context->scanCharacter();
145
            if ($char == '=') {
146
                $context->ignoreWhitespace();
147
                $char = $context->ScanCharacter();
148
                if ($char == '"') {
149
                    $attributevalue= $context->scanUntilString('"');
150
                    $context->IgnoreCharacter();
151
                } else if ($char == "'") {
152
                    $attributevalue = $context->scanUntilString("'");
153
                    $context->IgnoreCharacter();
154
                } else {
155
                    $context->unscanCharacter();
156
                    $attributevalue =
157
                        $context->scanUntilCharacters("> \n\r\t");
158
                }
159
            } else if ($char !== NULL) {
160
                $attributevalue = NULL;
161
                $context->unscanCharacter();
162
            }
163
            $Attributes[$attributename] = $attributevalue;
164
165
            $context->ignoreWhitespace();
166
            $attributename = $context->scanUntilCharacters("=/> \n\r\t");
167
        }
168
        return $Attributes;
169
    }
170
171
    /**
172
    * @param TSax3_StateParser subclass
173
    * @return constant TSAX3_STATE_START
174
    * @access protected
175
    */
176
    function parse(&$context) {
177
        $tag = $context->scanUntilCharacters("/> \n\r\t");
178
        if ($tag != '') {
179
            $this->attrs = array();
0 ignored issues
show
Bug introduced by
The property attrs does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
180
            $Attributes = $this->parseAttributes($context);
181
            $char = $context->scanCharacter();
182
            if ($char == '/') {
183
                $char = $context->scanCharacter();
184
                if ($char != '>') {
185
                    $context->unscanCharacter();
186
                }
187
                $context->handler_object_element->
188
                    {$context->handler_method_opening}($context->htmlsax, $tag,
189
                    $Attributes, TRUE);
190
                $context->handler_object_element->
191
                    {$context->handler_method_closing}($context->htmlsax, $tag,
192
                    TRUE);
193
            } else {
194
                $context->handler_object_element->
195
                    {$context->handler_method_opening}($context->htmlsax, $tag,
196
                    $Attributes, FALSE);
197
            }
198
        }
199
        return TSax3_StateParser::TSAX3_STATE_START;
200
    }
201
}
202
203
/**
204
* Deals with XML escapes handling comments and CDATA correctly
205
* @package System.Security.SafeHtml
206
* @access protected
207
*/
208
class TSax3_EscapeState {
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
209
    /**
210
    * @param TSax3_StateParser subclass
211
    * @return constant TSAX3_STATE_START
212
    * @access protected
213
    */
214
    function parse(&$context) {
215
        $char = $context->ScanCharacter();
216
        if ($char == '-') {
217
            $char = $context->ScanCharacter();
218
            if ($char == '-') {
219
                $context->unscanCharacter();
220
                $context->unscanCharacter();
221
                $text = $context->scanUntilString('-->');
222
                $text .= $context->scanCharacter();
223
                $text .= $context->scanCharacter();
224
            } else {
225
                $context->unscanCharacter();
226
                $text = $context->scanUntilString('>');
227
            }
228
        } else if ( $char == '[') {
229
            $context->unscanCharacter();
230
            $text = $context->scanUntilString(']>');
231
            $text.= $context->scanCharacter();
232
        } else {
233
            $context->unscanCharacter();
234
            $text = $context->scanUntilString('>');
235
        }
236
237
        $context->IgnoreCharacter();
238
        if ($text != '') {
239
            $context->handler_object_escape->
240
            {$context->handler_method_escape}($context->htmlsax, $text);
241
        }
242
        return TSax3_StateParser::TSAX3_STATE_START;
243
    }
244
}
245
/**
246
* Deals with JASP/ASP markup
247
* @package System.Security.SafeHtml
248
* @access protected
249
*/
250
class TSax3_JaspState {
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
251
    /**
252
    * @param TSax3_StateParser subclass
253
    * @return constant TSAX3_STATE_START
254
    * @access protected
255
    */
256
    function parse(&$context) {
257
        $text = $context->scanUntilString('%>');
258
        if ($text != '') {
259
            $context->handler_object_jasp->
260
                {$context->handler_method_jasp}($context->htmlsax, $text);
261
        }
262
        $context->IgnoreCharacter();
263
        $context->IgnoreCharacter();
264
        return TSax3_StateParser::TSAX3_STATE_START;
265
    }
266
}
267
/**
268
* Deals with XML processing instructions
269
* @package System.Security.SafeHtml
270
* @access protected
271
*/
272
class TSax3_PiState {
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
273
    /**
274
    * @param TSax3_StateParser subclass
275
    * @return constant TSAX3_STATE_START
276
    * @access protected
277
    */
278
    function parse(&$context) {
279
        $target = $context->scanUntilCharacters(" \n\r\t");
280
        $data = $context->scanUntilString('?>');
281
        if ($data != '') {
282
            $context->handler_object_pi->
283
            {$context->handler_method_pi}($context->htmlsax, $target, $data);
284
        }
285
        $context->IgnoreCharacter();
286
        $context->IgnoreCharacter();
287
        return TSax3_StateParser::TSAX3_STATE_START;
288
    }
289
}
290