@@ -1,507 +1,507 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | - // This is a q&d program that shows some of the results of |
|
4 | - // running KSES. If you have further questions, check the |
|
5 | - // current valid email address at http://chaos.org/contact/ |
|
6 | - |
|
7 | - // Make sure we're in a usable PHP environment |
|
8 | - if(substr(phpversion(), 0, 1) < 4) |
|
9 | - { |
|
10 | - define('KSESTEST_VER', 0); |
|
11 | - } |
|
12 | - elseif(substr(phpversion(), 0, 1) >= 5) |
|
13 | - { |
|
14 | - define('KSESTEST_VER', 5); |
|
15 | - } |
|
16 | - else |
|
17 | - { |
|
18 | - define('KSESTEST_VER', 4); |
|
19 | - } |
|
20 | - |
|
21 | - // See if we're in command line or web |
|
22 | - if($_SERVER["DOCUMENT_ROOT"] == "") |
|
23 | - { |
|
24 | - define('KSESTEST_ENV', 'CLI'); |
|
25 | - } |
|
26 | - else |
|
27 | - { |
|
28 | - define('KSESTEST_ENV', 'WEB'); |
|
29 | - } |
|
30 | - |
|
31 | - if(KSESTEST_VER == 0) |
|
32 | - { |
|
33 | - $message = array( |
|
34 | - "Error: Not using a current version of PHP!", |
|
35 | - "You are using PHP version " . phpversion() . ".", |
|
36 | - "KSES Class version requires PHP4 or better.", |
|
37 | - "KSES test program ending." |
|
38 | - ); |
|
39 | - |
|
40 | - displayPage( |
|
41 | - array("title" => "Error running KSES test", "message" => $message) |
|
42 | - ); |
|
43 | - |
|
44 | - exit(); |
|
45 | - } |
|
46 | - |
|
47 | - $include_file = "php" . KSESTEST_VER . ".class.kses.php"; |
|
48 | - if(file_exists($include_file) && is_readable($include_file)) |
|
49 | - { |
|
50 | - include_once($include_file); |
|
51 | - } |
|
52 | - else |
|
53 | - { |
|
54 | - $message = array( |
|
55 | - "Error: Unable to find '" . $include_file . "'.", |
|
56 | - "Please check your include path and make sure the file is available.", |
|
57 | - "Path: " . ini_get('include_path') |
|
58 | - ); |
|
59 | - |
|
60 | - displayPage( |
|
61 | - array('title' => 'Unable to include ' . $include_file, 'message' => $message) |
|
62 | - ); |
|
63 | - |
|
64 | - exit(); |
|
65 | - } |
|
66 | - |
|
67 | - $kses_type = "kses" . KSESTEST_VER; |
|
68 | - $myKses = new $kses_type; |
|
69 | - |
|
70 | - $test_text = array(); |
|
71 | - $test_text = test1_protocols($myKses); |
|
72 | - $test_text = array_merge($test_text, test1_html($myKses)); |
|
73 | - $test_text = array_merge($test_text, test1_kses($myKses)); |
|
74 | - |
|
75 | - displayPage( |
|
76 | - array('title' => 'New Test', 'message' => $test_text) |
|
77 | - ); |
|
78 | - |
|
79 | - function test1_kses(&$myKses) |
|
80 | - { |
|
81 | - $out = array(output_hr(), "Testing current configuration"); |
|
82 | - |
|
83 | - $test_tags = array( |
|
84 | - '<a href="http://www.chaos.org/">www.chaos.org</a>', |
|
85 | - '<a name="X">Short \'a name\' tag</a>', |
|
86 | - '<td colspan="3" rowspan="5">Foo</td>', |
|
87 | - '<td rowspan="2" class="mugwump" style="background-color: rgb(255, 204 204);">Bar</td>', |
|
88 | - '<td nowrap>Very Long String running to 1000 characters...</td>', |
|
89 | - '<td bgcolor="#00ff00" nowrap>Very Long String with a blue background</td>', |
|
90 | - '<a href="proto1://www.foo.com">New protocol test</a>', |
|
91 | - '<img src="proto2://www.foo.com" />', |
|
92 | - '<a href="javascript:javascript:javascript:javascript:javascript:alert(\'Boo!\');">bleep</a>', |
|
93 | - '<a href="proto4://abc.xyz.foo.com">Another new protocol</a>', |
|
94 | - '<a href="proto9://foo.foo.foo.foo.foo.org/">Test of "proto9"</a>', |
|
95 | - '<td width="75">Bar!</td>', |
|
96 | - '<td width="200">Long Cell</td>' |
|
97 | - ); |
|
98 | - |
|
99 | - $out_li = array(); |
|
100 | - // Keep only allowed HTML from the presumed 'form'. |
|
101 | - foreach($test_tags as $tag) |
|
102 | - { |
|
103 | - $temp = $myKses->Parse($tag); |
|
104 | - $check = ($temp == $tag) ? true : false; |
|
105 | - $text = ($temp == $tag) ? 'pass' : 'fail'; |
|
106 | - |
|
107 | - $li_text = output_testresult($check, $text) . output_newline(); |
|
108 | - $li_text .= "Input: " . output_translate($tag) . output_newline(); |
|
109 | - $li_text .= "Output: " . output_translate($temp); |
|
110 | - if(KSESTEST_ENV == 'CLI') |
|
111 | - { |
|
112 | - $li_text .= output_newline(); |
|
113 | - } |
|
114 | - |
|
115 | - array_push($out_li, output_code_wrap($li_text)); |
|
116 | - } |
|
117 | - |
|
118 | - $out = array_merge($out, array(output_ul($out_li))); |
|
119 | - array_push($out, output_hr()); |
|
120 | - array_push($out, "Testing is now finished."); |
|
121 | - return $out; |
|
122 | - } |
|
123 | - |
|
124 | - function output_code_wrap($text) |
|
125 | - { |
|
126 | - if(KSESTEST_ENV == 'CLI') |
|
127 | - { |
|
128 | - return $text; |
|
129 | - } |
|
130 | - else |
|
131 | - { |
|
132 | - return "<code>\n$text<code>\n"; |
|
133 | - } |
|
134 | - } |
|
135 | - |
|
136 | - function output_translate($text) |
|
137 | - { |
|
138 | - if(KSESTEST_ENV == 'CLI') |
|
139 | - { |
|
140 | - return $text; |
|
141 | - } |
|
142 | - else |
|
143 | - { |
|
144 | - return htmlentities($text); |
|
145 | - } |
|
146 | - } |
|
147 | - |
|
148 | - function output_testresult($pass = false, $text = "") |
|
149 | - { |
|
150 | - if(KSESTEST_ENV == 'CLI') |
|
151 | - { |
|
152 | - return '[' . $text . ']'; |
|
153 | - } |
|
154 | - else |
|
155 | - { |
|
156 | - if($pass == true) |
|
157 | - { |
|
158 | - return '<span style="color: green;">[' . $text . ']</span>'; |
|
159 | - } |
|
160 | - else |
|
161 | - { |
|
162 | - return '<span style="color: red;">[' . $text . ']</span>'; |
|
163 | - } |
|
164 | - } |
|
165 | - } |
|
166 | - |
|
167 | - function output_spaces() |
|
168 | - { |
|
169 | - if(KSESTEST_ENV == 'WEB') |
|
170 | - { |
|
171 | - $out = " "; |
|
172 | - } |
|
173 | - else |
|
174 | - { |
|
175 | - $out = " "; |
|
176 | - } |
|
177 | - |
|
178 | - return $out; |
|
179 | - } |
|
180 | - |
|
181 | - function output_newline() |
|
182 | - { |
|
183 | - if(KSESTEST_ENV == 'WEB') |
|
184 | - { |
|
185 | - $out = "<br />\n"; |
|
186 | - } |
|
187 | - else |
|
188 | - { |
|
189 | - $out = "\n"; |
|
190 | - } |
|
191 | - |
|
192 | - return $out; |
|
193 | - } |
|
194 | - |
|
195 | - function displayPage($data = array()) |
|
196 | - { |
|
197 | - $title = ($data['title'] == '') ? 'No title' : $data['title']; |
|
198 | - $message = ($data['message'] == '') ? array('No message') : $data['message']; |
|
199 | - |
|
200 | - $out = ""; |
|
201 | - |
|
202 | - foreach($message as $text) |
|
203 | - { |
|
204 | - if(KSESTEST_ENV == 'WEB') |
|
205 | - { |
|
206 | - $header = "\t\t<h1>$title</h1>\n\t\t<hr />\n"; |
|
207 | - $out .= "\t\t<p>\n"; |
|
208 | - $out .= "\t\t\t$text\n"; |
|
209 | - $out .= "\t\t</p>\n"; |
|
210 | - } |
|
211 | - else |
|
212 | - { |
|
213 | - $header = "$title\n" . str_repeat('-', 60) . "\n\n"; |
|
214 | - $out .= "\t$text\n\n"; |
|
215 | - } |
|
216 | - } |
|
217 | - |
|
218 | - if(KSESTEST_ENV == 'WEB') |
|
219 | - { |
|
220 | - echo "<html>\n"; |
|
221 | - echo "\t<head>\n"; |
|
222 | - echo "\t\t<title>$title</title>\n"; |
|
223 | - echo "\t</head>\n"; |
|
224 | - echo "\t<body>\n"; |
|
225 | - echo $header; |
|
226 | - echo $out; |
|
227 | - echo "\t</body>\n"; |
|
228 | - echo "</html>\n"; |
|
229 | - } |
|
230 | - else |
|
231 | - { |
|
232 | - echo $header; |
|
233 | - echo $out; |
|
234 | - } |
|
235 | - } |
|
236 | - |
|
237 | - function output_hr() |
|
238 | - { |
|
239 | - if(KSESTEST_ENV == 'WEB') |
|
240 | - { |
|
241 | - return "\t\t\t<hr />\n"; |
|
242 | - } |
|
243 | - else |
|
244 | - { |
|
245 | - return str_repeat(60, '-') . "\n"; |
|
246 | - } |
|
247 | - } |
|
248 | - |
|
249 | - function output_ul($data = array(), $padding = "") |
|
250 | - { |
|
251 | - if(!is_array($data) || count($data) < 1) |
|
252 | - { |
|
253 | - return ""; |
|
254 | - } |
|
255 | - |
|
256 | - $text = ""; |
|
257 | - if(KSESTEST_ENV == 'WEB') |
|
258 | - { |
|
259 | - $text = "\t\t\t<ul>\n"; |
|
260 | - foreach($data as $li) |
|
261 | - { |
|
262 | - $text .= "\t\t\t\t<li>$li</li>\n"; |
|
263 | - } |
|
264 | - $text .= "\t\t\t</ul>\n"; |
|
265 | - } |
|
266 | - else |
|
267 | - { |
|
268 | - foreach($data as $li) |
|
269 | - { |
|
270 | - $text .= $padding . " * $li\n"; |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - return $text; |
|
275 | - } |
|
276 | - |
|
277 | - function test1_protocols(&$myKses) |
|
278 | - { |
|
279 | - $default_prots = $myKses->dumpProtocols(); |
|
280 | - $out_text = array(); |
|
281 | - if(count($default_prots) > 0) |
|
282 | - { |
|
283 | - array_push($out_text, "Initial protocols from KSES" . KSESTEST_VER . ":"); |
|
284 | - array_push($out_text, output_ul($default_prots)); |
|
285 | - array_push($out_text, output_hr()); |
|
286 | - } |
|
287 | - |
|
288 | - $myKses->AddProtocols(array("proto1", "proto2:", "proto3")); // Add a list of protocols |
|
289 | - $myKses->AddProtocols("proto4:"); // Add a single protocol (Note ':' is optional at end) |
|
290 | - $myKses->AddProtocol("proto9", "mystery:", "anarchy"); |
|
291 | - $myKses->AddProtocol("alpha", "beta", "gamma:"); |
|
292 | - |
|
293 | - $add_protocol = "\t\t\t<ol>\n"; |
|
294 | - $add_protocol .= "\t\t\t\t" . '<li>$myKses->AddProtocols(array("proto1", "proto2:", "proto3"));</li>' . "\n"; |
|
295 | - $add_protocol .= "\t\t\t\t" . '<li>$myKses->AddProtocols("proto4:");</li>' . "\n"; |
|
296 | - $add_protocol .= "\t\t\t\t" . '<li>$myKses->AddProtocols("proto4:");</li>' . "\n"; |
|
297 | - $add_protocol .= "\t\t\t\t" . '<li>$myKses->AddProtocol("proto9", "mystery:", "anarchy");</li>' . "\n"; |
|
298 | - $add_protocol .= "\t\t\t\t" . '<li>$myKses->AddProtocol("alpha", "beta", "gamma:");</li>' . "\n"; |
|
299 | - $add_protocol .= "\t\t\t</ol>\n"; |
|
300 | - |
|
301 | - array_push($out_text, $add_protocol); |
|
302 | - |
|
303 | - $new_prots = $myKses->dumpProtocols(); |
|
304 | - if(count($new_prots) > 0) |
|
305 | - { |
|
306 | - array_push($out_text, "New protocols from KSES" . KSESTEST_VER . " after using AddProtocol(s):"); |
|
307 | - array_push($out_text, output_ul($new_prots)); |
|
308 | - array_push($out_text, output_hr()); |
|
309 | - } |
|
310 | - |
|
311 | - $myKses->RemoveProtocols(array("mystery", "anarchy:")); |
|
312 | - $myKses->RemoveProtocols("alpha:"); |
|
313 | - $myKses->RemoveProtocol("beta:"); |
|
314 | - $myKses->RemoveProtocol("gamma"); |
|
315 | - |
|
316 | - $remove_protocol = "\t\t\t<ol>\n"; |
|
317 | - $remove_protocol .= "\t\t\t\t" . '<li>$myKses->RemoveProtocols(array("mystery", "anarchy:"));</li>' . "\n"; |
|
318 | - $remove_protocol .= "\t\t\t\t" . '<li>$myKses->RemoveProtocols("alpha:");</li>' . "\n"; |
|
319 | - $remove_protocol .= "\t\t\t\t" . '<li>$myKses->RemoveProtocol("beta:");</li>' . "\n"; |
|
320 | - $remove_protocol .= "\t\t\t\t" . '<li>$myKses->RemoveProtocol("gamma");</li>' . "\n"; |
|
321 | - $remove_protocol .= "\t\t\t</ol>\n"; |
|
322 | - array_push($out_text, $remove_protocol); |
|
323 | - |
|
324 | - $new_prots = $myKses->dumpProtocols(); |
|
325 | - if(count($new_prots) > 0) |
|
326 | - { |
|
327 | - array_push($out_text, "Resulting protocols from KSES" . KSESTEST_VER . " after using RemoveProtocol(s):"); |
|
328 | - array_push($out_text, output_ul($new_prots)); |
|
329 | - array_push($out_text, output_hr()); |
|
330 | - } |
|
331 | - |
|
332 | - $myKses->SetProtocols(array("https", "gopher", "news")); |
|
333 | - $set_protocol = "\t\t\t<ol>\n"; |
|
334 | - $set_protocol .= "\t\t\t\t" . '<li>$myKses->SetProtocols(array("https", "gopher", "news"));</li>' . "\n"; |
|
335 | - $set_protocol .= "\t\t\t</ol>\n"; |
|
336 | - array_push($out_text, $set_protocol); |
|
337 | - |
|
338 | - $new_prots = $myKses->dumpProtocols(); |
|
339 | - if(count($new_prots) > 0) |
|
340 | - { |
|
341 | - array_push($out_text, "Resulting protocols from KSES" . KSESTEST_VER . " after using SetProtocols:"); |
|
342 | - array_push($out_text, output_ul($new_prots)); |
|
343 | - array_push($out_text, output_hr()); |
|
344 | - } |
|
345 | - |
|
346 | - // Invisible reset |
|
347 | - $myKses->SetProtocols(array("http", "proto1", "proto2", "proto9")); |
|
348 | - |
|
349 | - return $out_text; |
|
350 | - } |
|
351 | - |
|
352 | - function test1_html(&$myKses) |
|
353 | - { |
|
354 | - $out = array(); |
|
355 | - |
|
356 | - // Allows <p>|</p> tag |
|
357 | - $myKses->AddHTML("p"); |
|
358 | - |
|
359 | - // Allows 'a' tag with href|name attributes, |
|
360 | - // href has minlen of 10 chars, and maxlen of 25 chars |
|
361 | - // name has minlen of 2 chars |
|
362 | - $myKses->AddHTML( |
|
363 | - "a", |
|
364 | - array( |
|
365 | - "href" => array('maxlen' => 25, 'minlen' => 10), |
|
366 | - "name" => array('minlen' => 2) |
|
367 | - ) |
|
368 | - ); |
|
369 | - |
|
370 | - // Allows 'td' tag with colspan|rowspan|class|style|width|nowrap attributes, |
|
371 | - // colspan has minval of 2 and maxval of 5 |
|
372 | - // rowspan has minval of 3 and maxval of 6 |
|
373 | - // class has minlen of 1 char and maxlen of 10 chars |
|
374 | - // style has minlen of 10 chars and maxlen of 100 chars |
|
375 | - // width has maxval of 100 |
|
376 | - // nowrap is valueless |
|
377 | - $myKses->AddHTML( |
|
378 | - "td", |
|
379 | - array( |
|
380 | - "colspan" => array('minval' => 2, 'maxval' => 5), |
|
381 | - "rowspan" => array('minval' => 3, 'maxval' => 6), |
|
382 | - "class" => array("minlen" => 1, 'maxlen' => 10), |
|
383 | - "width" => array("maxval" => 100), |
|
384 | - "style" => array('minlen' => 10, 'maxlen' => 100), |
|
385 | - "nowrap" => array('valueless' => 'y') |
|
386 | - ) |
|
387 | - ); |
|
388 | - |
|
389 | - array_push($out, "Modifying HTML Tests:"); |
|
390 | - $code_text = "<pre>\n"; |
|
391 | - $code_text .= " // Allows <p>|</p> tag\n"; |
|
392 | - $code_text .= " \$myKses->AddHTML(\"p\");\n"; |
|
393 | - $code_text .= "\n"; |
|
394 | - $code_text .= " // Allows 'a' tag with href|name attributes,\n"; |
|
395 | - $code_text .= " // href has minlen of 10 chars, and maxlen of 25 chars\n"; |
|
396 | - $code_text .= " // name has minlen of 2 chars\n"; |
|
397 | - $code_text .= " \$myKses->AddHTML(\n"; |
|
398 | - $code_text .= " \"a\",\n"; |
|
399 | - $code_text .= " array(\n"; |
|
400 | - $code_text .= " \"href\" => array('maxlen' => 25, 'minlen' => 10),\n"; |
|
401 | - $code_text .= " \"name\" => array('minlen' => 2)\n"; |
|
402 | - $code_text .= " )\n"; |
|
403 | - $code_text .= " );\n"; |
|
404 | - $code_text .= "\n"; |
|
405 | - $code_text .= " // Allows 'td' tag with colspan|rowspan|class|style|width|nowrap attributes,\n"; |
|
406 | - $code_text .= " // colspan has minval of 2 and maxval of 5\n"; |
|
407 | - $code_text .= " // rowspan has minval of 3 and maxval of 6\n"; |
|
408 | - $code_text .= " // class has minlen of 1 char and maxlen of 10 chars\n"; |
|
409 | - $code_text .= " // style has minlen of 10 chars and maxlen of 100 chars\n"; |
|
410 | - $code_text .= " // width has maxval of 100\n"; |
|
411 | - $code_text .= " // nowrap is valueless\n"; |
|
412 | - $code_text .= " \$myKses->AddHTML(\n"; |
|
413 | - $code_text .= " \"td\",\n"; |
|
414 | - $code_text .= " array(\n"; |
|
415 | - $code_text .= " \"colspan\" => array('minval' => 2, 'maxval' => 5),\n"; |
|
416 | - $code_text .= " \"rowspan\" => array('minval' => 3, 'maxval' => 6),\n"; |
|
417 | - $code_text .= " \"class\" => array(\"minlen\" => 1, 'maxlen' => 10),\n"; |
|
418 | - $code_text .= " \"width\" => array(\"maxval\" => 100),\n"; |
|
419 | - $code_text .= " \"style\" => array('minlen' => 10, 'maxlen' => 100),\n"; |
|
420 | - $code_text .= " \"nowrap\" => array('valueless' => 'y')\n"; |
|
421 | - $code_text .= " )\n"; |
|
422 | - $code_text .= " );\n"; |
|
423 | - $code_text .= "</pre>\n"; |
|
424 | - |
|
425 | - array_push($out, $code_text); |
|
426 | - array_push($out, output_hr()); |
|
427 | - array_push($out, "Net results:"); |
|
428 | - |
|
429 | - $out_elems = $myKses->DumpElements(); |
|
430 | - if(count($out_elems) > 0) |
|
431 | - { |
|
432 | - //array_push($out, "\t\t\t<ul>\n"); |
|
433 | - foreach($out_elems as $tag => $attr_data) |
|
434 | - { |
|
435 | - $out_li_elems = array(); |
|
436 | - $elem_text = "(X)HTML element $tag"; |
|
437 | - $allow = ""; |
|
438 | - if(isset($attr_data) && is_array($attr_data) && count($attr_data) > 0) |
|
439 | - { |
|
440 | - $allow = " allows attribute"; |
|
441 | - if(count($attr_data) > 1) |
|
442 | - { |
|
443 | - $allow .= "s"; |
|
444 | - } |
|
445 | - $allow .= ":\n"; |
|
446 | - } |
|
447 | - |
|
448 | - array_push($out_li_elems, "$elem_text$allow"); |
|
449 | - |
|
450 | - $attr_test_li = array(); |
|
451 | - if(isset($attr_data) && is_array($attr_data) && count($attr_data) > 0) |
|
452 | - { |
|
453 | - foreach($attr_data as $attr_name => $attr_tests) |
|
454 | - { |
|
455 | - $li_text = $attr_name; |
|
456 | - if(isset($attr_tests) && count($attr_tests) > 0) |
|
457 | - { |
|
458 | - foreach($attr_tests as $test_name => $test_val) |
|
459 | - { |
|
460 | - switch($test_name) |
|
461 | - { |
|
462 | - case "maxlen": |
|
463 | - $li_text .= " - maximum length of '" . $test_val . "' characters"; |
|
464 | - break; |
|
465 | - case "minlen": |
|
466 | - $li_text .= " - minimum length of '" . $test_val . "' characters"; |
|
467 | - break; |
|
468 | - case "minval": |
|
469 | - $li_text .= " - minimum value of '" . $test_val . "'"; |
|
470 | - break; |
|
471 | - case "maxval": |
|
472 | - $li_text .= " - maximum value of '" . $test_val . "'"; |
|
473 | - break; |
|
474 | - case "valueless": |
|
475 | - switch(strtolower($test_val)) |
|
476 | - { |
|
477 | - case 'n': |
|
478 | - $li_text .= " - must not be valueless"; |
|
479 | - break; |
|
480 | - case 'y': |
|
481 | - $li_text .= " - must be valueless"; |
|
482 | - break; |
|
483 | - default: |
|
484 | - break; |
|
485 | - } |
|
486 | - break; |
|
487 | - default: |
|
488 | - break; |
|
489 | - } |
|
490 | - } |
|
491 | - } |
|
492 | - array_push($attr_test_li, $li_text); |
|
493 | - } |
|
494 | - if(count($attr_test_li) > 0) |
|
495 | - { |
|
496 | - $attr_test_li = output_ul($attr_test_li, " "); |
|
497 | - $out_li_elems = array("$elem_text$allow$attr_test_li"); |
|
498 | - } |
|
499 | - } |
|
500 | - $out = array_merge($out, $out_li_elems); |
|
501 | - } |
|
502 | - } |
|
503 | - |
|
504 | - return $out; |
|
505 | - } |
|
3 | + // This is a q&d program that shows some of the results of |
|
4 | + // running KSES. If you have further questions, check the |
|
5 | + // current valid email address at http://chaos.org/contact/ |
|
6 | + |
|
7 | + // Make sure we're in a usable PHP environment |
|
8 | + if(substr(phpversion(), 0, 1) < 4) |
|
9 | + { |
|
10 | + define('KSESTEST_VER', 0); |
|
11 | + } |
|
12 | + elseif(substr(phpversion(), 0, 1) >= 5) |
|
13 | + { |
|
14 | + define('KSESTEST_VER', 5); |
|
15 | + } |
|
16 | + else |
|
17 | + { |
|
18 | + define('KSESTEST_VER', 4); |
|
19 | + } |
|
20 | + |
|
21 | + // See if we're in command line or web |
|
22 | + if($_SERVER["DOCUMENT_ROOT"] == "") |
|
23 | + { |
|
24 | + define('KSESTEST_ENV', 'CLI'); |
|
25 | + } |
|
26 | + else |
|
27 | + { |
|
28 | + define('KSESTEST_ENV', 'WEB'); |
|
29 | + } |
|
30 | + |
|
31 | + if(KSESTEST_VER == 0) |
|
32 | + { |
|
33 | + $message = array( |
|
34 | + "Error: Not using a current version of PHP!", |
|
35 | + "You are using PHP version " . phpversion() . ".", |
|
36 | + "KSES Class version requires PHP4 or better.", |
|
37 | + "KSES test program ending." |
|
38 | + ); |
|
39 | + |
|
40 | + displayPage( |
|
41 | + array("title" => "Error running KSES test", "message" => $message) |
|
42 | + ); |
|
43 | + |
|
44 | + exit(); |
|
45 | + } |
|
46 | + |
|
47 | + $include_file = "php" . KSESTEST_VER . ".class.kses.php"; |
|
48 | + if(file_exists($include_file) && is_readable($include_file)) |
|
49 | + { |
|
50 | + include_once($include_file); |
|
51 | + } |
|
52 | + else |
|
53 | + { |
|
54 | + $message = array( |
|
55 | + "Error: Unable to find '" . $include_file . "'.", |
|
56 | + "Please check your include path and make sure the file is available.", |
|
57 | + "Path: " . ini_get('include_path') |
|
58 | + ); |
|
59 | + |
|
60 | + displayPage( |
|
61 | + array('title' => 'Unable to include ' . $include_file, 'message' => $message) |
|
62 | + ); |
|
63 | + |
|
64 | + exit(); |
|
65 | + } |
|
66 | + |
|
67 | + $kses_type = "kses" . KSESTEST_VER; |
|
68 | + $myKses = new $kses_type; |
|
69 | + |
|
70 | + $test_text = array(); |
|
71 | + $test_text = test1_protocols($myKses); |
|
72 | + $test_text = array_merge($test_text, test1_html($myKses)); |
|
73 | + $test_text = array_merge($test_text, test1_kses($myKses)); |
|
74 | + |
|
75 | + displayPage( |
|
76 | + array('title' => 'New Test', 'message' => $test_text) |
|
77 | + ); |
|
78 | + |
|
79 | + function test1_kses(&$myKses) |
|
80 | + { |
|
81 | + $out = array(output_hr(), "Testing current configuration"); |
|
82 | + |
|
83 | + $test_tags = array( |
|
84 | + '<a href="http://www.chaos.org/">www.chaos.org</a>', |
|
85 | + '<a name="X">Short \'a name\' tag</a>', |
|
86 | + '<td colspan="3" rowspan="5">Foo</td>', |
|
87 | + '<td rowspan="2" class="mugwump" style="background-color: rgb(255, 204 204);">Bar</td>', |
|
88 | + '<td nowrap>Very Long String running to 1000 characters...</td>', |
|
89 | + '<td bgcolor="#00ff00" nowrap>Very Long String with a blue background</td>', |
|
90 | + '<a href="proto1://www.foo.com">New protocol test</a>', |
|
91 | + '<img src="proto2://www.foo.com" />', |
|
92 | + '<a href="javascript:javascript:javascript:javascript:javascript:alert(\'Boo!\');">bleep</a>', |
|
93 | + '<a href="proto4://abc.xyz.foo.com">Another new protocol</a>', |
|
94 | + '<a href="proto9://foo.foo.foo.foo.foo.org/">Test of "proto9"</a>', |
|
95 | + '<td width="75">Bar!</td>', |
|
96 | + '<td width="200">Long Cell</td>' |
|
97 | + ); |
|
98 | + |
|
99 | + $out_li = array(); |
|
100 | + // Keep only allowed HTML from the presumed 'form'. |
|
101 | + foreach($test_tags as $tag) |
|
102 | + { |
|
103 | + $temp = $myKses->Parse($tag); |
|
104 | + $check = ($temp == $tag) ? true : false; |
|
105 | + $text = ($temp == $tag) ? 'pass' : 'fail'; |
|
106 | + |
|
107 | + $li_text = output_testresult($check, $text) . output_newline(); |
|
108 | + $li_text .= "Input: " . output_translate($tag) . output_newline(); |
|
109 | + $li_text .= "Output: " . output_translate($temp); |
|
110 | + if(KSESTEST_ENV == 'CLI') |
|
111 | + { |
|
112 | + $li_text .= output_newline(); |
|
113 | + } |
|
114 | + |
|
115 | + array_push($out_li, output_code_wrap($li_text)); |
|
116 | + } |
|
117 | + |
|
118 | + $out = array_merge($out, array(output_ul($out_li))); |
|
119 | + array_push($out, output_hr()); |
|
120 | + array_push($out, "Testing is now finished."); |
|
121 | + return $out; |
|
122 | + } |
|
123 | + |
|
124 | + function output_code_wrap($text) |
|
125 | + { |
|
126 | + if(KSESTEST_ENV == 'CLI') |
|
127 | + { |
|
128 | + return $text; |
|
129 | + } |
|
130 | + else |
|
131 | + { |
|
132 | + return "<code>\n$text<code>\n"; |
|
133 | + } |
|
134 | + } |
|
135 | + |
|
136 | + function output_translate($text) |
|
137 | + { |
|
138 | + if(KSESTEST_ENV == 'CLI') |
|
139 | + { |
|
140 | + return $text; |
|
141 | + } |
|
142 | + else |
|
143 | + { |
|
144 | + return htmlentities($text); |
|
145 | + } |
|
146 | + } |
|
147 | + |
|
148 | + function output_testresult($pass = false, $text = "") |
|
149 | + { |
|
150 | + if(KSESTEST_ENV == 'CLI') |
|
151 | + { |
|
152 | + return '[' . $text . ']'; |
|
153 | + } |
|
154 | + else |
|
155 | + { |
|
156 | + if($pass == true) |
|
157 | + { |
|
158 | + return '<span style="color: green;">[' . $text . ']</span>'; |
|
159 | + } |
|
160 | + else |
|
161 | + { |
|
162 | + return '<span style="color: red;">[' . $text . ']</span>'; |
|
163 | + } |
|
164 | + } |
|
165 | + } |
|
166 | + |
|
167 | + function output_spaces() |
|
168 | + { |
|
169 | + if(KSESTEST_ENV == 'WEB') |
|
170 | + { |
|
171 | + $out = " "; |
|
172 | + } |
|
173 | + else |
|
174 | + { |
|
175 | + $out = " "; |
|
176 | + } |
|
177 | + |
|
178 | + return $out; |
|
179 | + } |
|
180 | + |
|
181 | + function output_newline() |
|
182 | + { |
|
183 | + if(KSESTEST_ENV == 'WEB') |
|
184 | + { |
|
185 | + $out = "<br />\n"; |
|
186 | + } |
|
187 | + else |
|
188 | + { |
|
189 | + $out = "\n"; |
|
190 | + } |
|
191 | + |
|
192 | + return $out; |
|
193 | + } |
|
194 | + |
|
195 | + function displayPage($data = array()) |
|
196 | + { |
|
197 | + $title = ($data['title'] == '') ? 'No title' : $data['title']; |
|
198 | + $message = ($data['message'] == '') ? array('No message') : $data['message']; |
|
199 | + |
|
200 | + $out = ""; |
|
201 | + |
|
202 | + foreach($message as $text) |
|
203 | + { |
|
204 | + if(KSESTEST_ENV == 'WEB') |
|
205 | + { |
|
206 | + $header = "\t\t<h1>$title</h1>\n\t\t<hr />\n"; |
|
207 | + $out .= "\t\t<p>\n"; |
|
208 | + $out .= "\t\t\t$text\n"; |
|
209 | + $out .= "\t\t</p>\n"; |
|
210 | + } |
|
211 | + else |
|
212 | + { |
|
213 | + $header = "$title\n" . str_repeat('-', 60) . "\n\n"; |
|
214 | + $out .= "\t$text\n\n"; |
|
215 | + } |
|
216 | + } |
|
217 | + |
|
218 | + if(KSESTEST_ENV == 'WEB') |
|
219 | + { |
|
220 | + echo "<html>\n"; |
|
221 | + echo "\t<head>\n"; |
|
222 | + echo "\t\t<title>$title</title>\n"; |
|
223 | + echo "\t</head>\n"; |
|
224 | + echo "\t<body>\n"; |
|
225 | + echo $header; |
|
226 | + echo $out; |
|
227 | + echo "\t</body>\n"; |
|
228 | + echo "</html>\n"; |
|
229 | + } |
|
230 | + else |
|
231 | + { |
|
232 | + echo $header; |
|
233 | + echo $out; |
|
234 | + } |
|
235 | + } |
|
236 | + |
|
237 | + function output_hr() |
|
238 | + { |
|
239 | + if(KSESTEST_ENV == 'WEB') |
|
240 | + { |
|
241 | + return "\t\t\t<hr />\n"; |
|
242 | + } |
|
243 | + else |
|
244 | + { |
|
245 | + return str_repeat(60, '-') . "\n"; |
|
246 | + } |
|
247 | + } |
|
248 | + |
|
249 | + function output_ul($data = array(), $padding = "") |
|
250 | + { |
|
251 | + if(!is_array($data) || count($data) < 1) |
|
252 | + { |
|
253 | + return ""; |
|
254 | + } |
|
255 | + |
|
256 | + $text = ""; |
|
257 | + if(KSESTEST_ENV == 'WEB') |
|
258 | + { |
|
259 | + $text = "\t\t\t<ul>\n"; |
|
260 | + foreach($data as $li) |
|
261 | + { |
|
262 | + $text .= "\t\t\t\t<li>$li</li>\n"; |
|
263 | + } |
|
264 | + $text .= "\t\t\t</ul>\n"; |
|
265 | + } |
|
266 | + else |
|
267 | + { |
|
268 | + foreach($data as $li) |
|
269 | + { |
|
270 | + $text .= $padding . " * $li\n"; |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + return $text; |
|
275 | + } |
|
276 | + |
|
277 | + function test1_protocols(&$myKses) |
|
278 | + { |
|
279 | + $default_prots = $myKses->dumpProtocols(); |
|
280 | + $out_text = array(); |
|
281 | + if(count($default_prots) > 0) |
|
282 | + { |
|
283 | + array_push($out_text, "Initial protocols from KSES" . KSESTEST_VER . ":"); |
|
284 | + array_push($out_text, output_ul($default_prots)); |
|
285 | + array_push($out_text, output_hr()); |
|
286 | + } |
|
287 | + |
|
288 | + $myKses->AddProtocols(array("proto1", "proto2:", "proto3")); // Add a list of protocols |
|
289 | + $myKses->AddProtocols("proto4:"); // Add a single protocol (Note ':' is optional at end) |
|
290 | + $myKses->AddProtocol("proto9", "mystery:", "anarchy"); |
|
291 | + $myKses->AddProtocol("alpha", "beta", "gamma:"); |
|
292 | + |
|
293 | + $add_protocol = "\t\t\t<ol>\n"; |
|
294 | + $add_protocol .= "\t\t\t\t" . '<li>$myKses->AddProtocols(array("proto1", "proto2:", "proto3"));</li>' . "\n"; |
|
295 | + $add_protocol .= "\t\t\t\t" . '<li>$myKses->AddProtocols("proto4:");</li>' . "\n"; |
|
296 | + $add_protocol .= "\t\t\t\t" . '<li>$myKses->AddProtocols("proto4:");</li>' . "\n"; |
|
297 | + $add_protocol .= "\t\t\t\t" . '<li>$myKses->AddProtocol("proto9", "mystery:", "anarchy");</li>' . "\n"; |
|
298 | + $add_protocol .= "\t\t\t\t" . '<li>$myKses->AddProtocol("alpha", "beta", "gamma:");</li>' . "\n"; |
|
299 | + $add_protocol .= "\t\t\t</ol>\n"; |
|
300 | + |
|
301 | + array_push($out_text, $add_protocol); |
|
302 | + |
|
303 | + $new_prots = $myKses->dumpProtocols(); |
|
304 | + if(count($new_prots) > 0) |
|
305 | + { |
|
306 | + array_push($out_text, "New protocols from KSES" . KSESTEST_VER . " after using AddProtocol(s):"); |
|
307 | + array_push($out_text, output_ul($new_prots)); |
|
308 | + array_push($out_text, output_hr()); |
|
309 | + } |
|
310 | + |
|
311 | + $myKses->RemoveProtocols(array("mystery", "anarchy:")); |
|
312 | + $myKses->RemoveProtocols("alpha:"); |
|
313 | + $myKses->RemoveProtocol("beta:"); |
|
314 | + $myKses->RemoveProtocol("gamma"); |
|
315 | + |
|
316 | + $remove_protocol = "\t\t\t<ol>\n"; |
|
317 | + $remove_protocol .= "\t\t\t\t" . '<li>$myKses->RemoveProtocols(array("mystery", "anarchy:"));</li>' . "\n"; |
|
318 | + $remove_protocol .= "\t\t\t\t" . '<li>$myKses->RemoveProtocols("alpha:");</li>' . "\n"; |
|
319 | + $remove_protocol .= "\t\t\t\t" . '<li>$myKses->RemoveProtocol("beta:");</li>' . "\n"; |
|
320 | + $remove_protocol .= "\t\t\t\t" . '<li>$myKses->RemoveProtocol("gamma");</li>' . "\n"; |
|
321 | + $remove_protocol .= "\t\t\t</ol>\n"; |
|
322 | + array_push($out_text, $remove_protocol); |
|
323 | + |
|
324 | + $new_prots = $myKses->dumpProtocols(); |
|
325 | + if(count($new_prots) > 0) |
|
326 | + { |
|
327 | + array_push($out_text, "Resulting protocols from KSES" . KSESTEST_VER . " after using RemoveProtocol(s):"); |
|
328 | + array_push($out_text, output_ul($new_prots)); |
|
329 | + array_push($out_text, output_hr()); |
|
330 | + } |
|
331 | + |
|
332 | + $myKses->SetProtocols(array("https", "gopher", "news")); |
|
333 | + $set_protocol = "\t\t\t<ol>\n"; |
|
334 | + $set_protocol .= "\t\t\t\t" . '<li>$myKses->SetProtocols(array("https", "gopher", "news"));</li>' . "\n"; |
|
335 | + $set_protocol .= "\t\t\t</ol>\n"; |
|
336 | + array_push($out_text, $set_protocol); |
|
337 | + |
|
338 | + $new_prots = $myKses->dumpProtocols(); |
|
339 | + if(count($new_prots) > 0) |
|
340 | + { |
|
341 | + array_push($out_text, "Resulting protocols from KSES" . KSESTEST_VER . " after using SetProtocols:"); |
|
342 | + array_push($out_text, output_ul($new_prots)); |
|
343 | + array_push($out_text, output_hr()); |
|
344 | + } |
|
345 | + |
|
346 | + // Invisible reset |
|
347 | + $myKses->SetProtocols(array("http", "proto1", "proto2", "proto9")); |
|
348 | + |
|
349 | + return $out_text; |
|
350 | + } |
|
351 | + |
|
352 | + function test1_html(&$myKses) |
|
353 | + { |
|
354 | + $out = array(); |
|
355 | + |
|
356 | + // Allows <p>|</p> tag |
|
357 | + $myKses->AddHTML("p"); |
|
358 | + |
|
359 | + // Allows 'a' tag with href|name attributes, |
|
360 | + // href has minlen of 10 chars, and maxlen of 25 chars |
|
361 | + // name has minlen of 2 chars |
|
362 | + $myKses->AddHTML( |
|
363 | + "a", |
|
364 | + array( |
|
365 | + "href" => array('maxlen' => 25, 'minlen' => 10), |
|
366 | + "name" => array('minlen' => 2) |
|
367 | + ) |
|
368 | + ); |
|
369 | + |
|
370 | + // Allows 'td' tag with colspan|rowspan|class|style|width|nowrap attributes, |
|
371 | + // colspan has minval of 2 and maxval of 5 |
|
372 | + // rowspan has minval of 3 and maxval of 6 |
|
373 | + // class has minlen of 1 char and maxlen of 10 chars |
|
374 | + // style has minlen of 10 chars and maxlen of 100 chars |
|
375 | + // width has maxval of 100 |
|
376 | + // nowrap is valueless |
|
377 | + $myKses->AddHTML( |
|
378 | + "td", |
|
379 | + array( |
|
380 | + "colspan" => array('minval' => 2, 'maxval' => 5), |
|
381 | + "rowspan" => array('minval' => 3, 'maxval' => 6), |
|
382 | + "class" => array("minlen" => 1, 'maxlen' => 10), |
|
383 | + "width" => array("maxval" => 100), |
|
384 | + "style" => array('minlen' => 10, 'maxlen' => 100), |
|
385 | + "nowrap" => array('valueless' => 'y') |
|
386 | + ) |
|
387 | + ); |
|
388 | + |
|
389 | + array_push($out, "Modifying HTML Tests:"); |
|
390 | + $code_text = "<pre>\n"; |
|
391 | + $code_text .= " // Allows <p>|</p> tag\n"; |
|
392 | + $code_text .= " \$myKses->AddHTML(\"p\");\n"; |
|
393 | + $code_text .= "\n"; |
|
394 | + $code_text .= " // Allows 'a' tag with href|name attributes,\n"; |
|
395 | + $code_text .= " // href has minlen of 10 chars, and maxlen of 25 chars\n"; |
|
396 | + $code_text .= " // name has minlen of 2 chars\n"; |
|
397 | + $code_text .= " \$myKses->AddHTML(\n"; |
|
398 | + $code_text .= " \"a\",\n"; |
|
399 | + $code_text .= " array(\n"; |
|
400 | + $code_text .= " \"href\" => array('maxlen' => 25, 'minlen' => 10),\n"; |
|
401 | + $code_text .= " \"name\" => array('minlen' => 2)\n"; |
|
402 | + $code_text .= " )\n"; |
|
403 | + $code_text .= " );\n"; |
|
404 | + $code_text .= "\n"; |
|
405 | + $code_text .= " // Allows 'td' tag with colspan|rowspan|class|style|width|nowrap attributes,\n"; |
|
406 | + $code_text .= " // colspan has minval of 2 and maxval of 5\n"; |
|
407 | + $code_text .= " // rowspan has minval of 3 and maxval of 6\n"; |
|
408 | + $code_text .= " // class has minlen of 1 char and maxlen of 10 chars\n"; |
|
409 | + $code_text .= " // style has minlen of 10 chars and maxlen of 100 chars\n"; |
|
410 | + $code_text .= " // width has maxval of 100\n"; |
|
411 | + $code_text .= " // nowrap is valueless\n"; |
|
412 | + $code_text .= " \$myKses->AddHTML(\n"; |
|
413 | + $code_text .= " \"td\",\n"; |
|
414 | + $code_text .= " array(\n"; |
|
415 | + $code_text .= " \"colspan\" => array('minval' => 2, 'maxval' => 5),\n"; |
|
416 | + $code_text .= " \"rowspan\" => array('minval' => 3, 'maxval' => 6),\n"; |
|
417 | + $code_text .= " \"class\" => array(\"minlen\" => 1, 'maxlen' => 10),\n"; |
|
418 | + $code_text .= " \"width\" => array(\"maxval\" => 100),\n"; |
|
419 | + $code_text .= " \"style\" => array('minlen' => 10, 'maxlen' => 100),\n"; |
|
420 | + $code_text .= " \"nowrap\" => array('valueless' => 'y')\n"; |
|
421 | + $code_text .= " )\n"; |
|
422 | + $code_text .= " );\n"; |
|
423 | + $code_text .= "</pre>\n"; |
|
424 | + |
|
425 | + array_push($out, $code_text); |
|
426 | + array_push($out, output_hr()); |
|
427 | + array_push($out, "Net results:"); |
|
428 | + |
|
429 | + $out_elems = $myKses->DumpElements(); |
|
430 | + if(count($out_elems) > 0) |
|
431 | + { |
|
432 | + //array_push($out, "\t\t\t<ul>\n"); |
|
433 | + foreach($out_elems as $tag => $attr_data) |
|
434 | + { |
|
435 | + $out_li_elems = array(); |
|
436 | + $elem_text = "(X)HTML element $tag"; |
|
437 | + $allow = ""; |
|
438 | + if(isset($attr_data) && is_array($attr_data) && count($attr_data) > 0) |
|
439 | + { |
|
440 | + $allow = " allows attribute"; |
|
441 | + if(count($attr_data) > 1) |
|
442 | + { |
|
443 | + $allow .= "s"; |
|
444 | + } |
|
445 | + $allow .= ":\n"; |
|
446 | + } |
|
447 | + |
|
448 | + array_push($out_li_elems, "$elem_text$allow"); |
|
449 | + |
|
450 | + $attr_test_li = array(); |
|
451 | + if(isset($attr_data) && is_array($attr_data) && count($attr_data) > 0) |
|
452 | + { |
|
453 | + foreach($attr_data as $attr_name => $attr_tests) |
|
454 | + { |
|
455 | + $li_text = $attr_name; |
|
456 | + if(isset($attr_tests) && count($attr_tests) > 0) |
|
457 | + { |
|
458 | + foreach($attr_tests as $test_name => $test_val) |
|
459 | + { |
|
460 | + switch($test_name) |
|
461 | + { |
|
462 | + case "maxlen": |
|
463 | + $li_text .= " - maximum length of '" . $test_val . "' characters"; |
|
464 | + break; |
|
465 | + case "minlen": |
|
466 | + $li_text .= " - minimum length of '" . $test_val . "' characters"; |
|
467 | + break; |
|
468 | + case "minval": |
|
469 | + $li_text .= " - minimum value of '" . $test_val . "'"; |
|
470 | + break; |
|
471 | + case "maxval": |
|
472 | + $li_text .= " - maximum value of '" . $test_val . "'"; |
|
473 | + break; |
|
474 | + case "valueless": |
|
475 | + switch(strtolower($test_val)) |
|
476 | + { |
|
477 | + case 'n': |
|
478 | + $li_text .= " - must not be valueless"; |
|
479 | + break; |
|
480 | + case 'y': |
|
481 | + $li_text .= " - must be valueless"; |
|
482 | + break; |
|
483 | + default: |
|
484 | + break; |
|
485 | + } |
|
486 | + break; |
|
487 | + default: |
|
488 | + break; |
|
489 | + } |
|
490 | + } |
|
491 | + } |
|
492 | + array_push($attr_test_li, $li_text); |
|
493 | + } |
|
494 | + if(count($attr_test_li) > 0) |
|
495 | + { |
|
496 | + $attr_test_li = output_ul($attr_test_li, " "); |
|
497 | + $out_li_elems = array("$elem_text$allow$attr_test_li"); |
|
498 | + } |
|
499 | + } |
|
500 | + $out = array_merge($out, $out_li_elems); |
|
501 | + } |
|
502 | + } |
|
503 | + |
|
504 | + return $out; |
|
505 | + } |
|
506 | 506 | |
507 | 507 | ?> |
508 | 508 | \ No newline at end of file |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | - /* |
|
2 | + /* |
|
3 | 3 | * ========================================================================================== |
4 | 4 | * |
5 | 5 | * This program is free software and open source software; you can redistribute |
@@ -20,1143 +20,1143 @@ discard block |
||
20 | 20 | * ========================================================================================== |
21 | 21 | */ |
22 | 22 | |
23 | - /** |
|
24 | - * Class file for PHP4 OOP version of kses |
|
25 | - * |
|
26 | - * This is an updated version of kses to work with PHP4 that works under E_STRICT. |
|
27 | - * |
|
28 | - * This upgrade provides the following: |
|
29 | - * + Version number synced to procedural version number |
|
30 | - * + PHPdoc style documentation has been added to the class. See http://www.phpdoc.org/ for more info. |
|
31 | - * + Some methods are now deprecated due to nomenclature style change. See method documentation for specifics. |
|
32 | - * + Kses4 now works in E_STRICT |
|
33 | - * + Addition of methods AddProtocols(), filterKsestextHook(), RemoveProtocol() and RemoveProtocols() |
|
34 | - * + Deprecated _hook(), Protocols() |
|
35 | - * + Integrated code from kses 0.2.2 into class. |
|
36 | - * + Added methods DumpProtocols(), DumpMethods() |
|
37 | - * |
|
38 | - * @package kses |
|
39 | - * @subpackage kses4 |
|
40 | - */ |
|
41 | - |
|
42 | - if(substr(phpversion(), 0, 1) < 4) |
|
43 | - { |
|
44 | - die("Class kses requires PHP 4 or higher."); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Only install KSES4 once |
|
49 | - */ |
|
50 | - if(!defined('KSES_CLASS_PHP4')) |
|
51 | - { |
|
52 | - define('KSES_CLASS_PHP4', true); |
|
53 | - |
|
54 | - /** |
|
55 | - * Kses strips evil scripts! |
|
56 | - * |
|
57 | - * This class provides the capability for removing unwanted HTML/XHTML, attributes from |
|
58 | - * tags, and protocols contained in links. The net result is a much more powerful tool |
|
59 | - * than the PHP internal strip_tags() |
|
60 | - * |
|
61 | - * This is a fork of a slick piece of procedural code called 'kses' written by Ulf Harnhammar |
|
62 | - * The entire set of functions was wrapped in a PHP object with some internal modifications |
|
63 | - * by Richard Vasquez (http://www.chaos.org/) 7/25/2003 |
|
64 | - * |
|
65 | - * This upgrade provides the following: |
|
66 | - * + Version number synced to procedural version number |
|
67 | - * + PHPdoc style documentation has been added to the class. See http://www.phpdoc.org/ for more info. |
|
68 | - * + Some methods are now deprecated due to nomenclature style change. See method documentation for specifics. |
|
69 | - * + Kses4 now works in E_STRICT |
|
70 | - * + Addition of methods AddProtocols(), filterKsestextHook(), RemoveProtocol(), RemoveProtocols() and SetProtocols() |
|
71 | - * + Deprecated _hook(), Protocols() |
|
72 | - * + Integrated code from kses 0.2.2 into class. |
|
73 | - * |
|
74 | - * @author Richard R. V�squez, Jr. (Original procedural code by Ulf H�rnhammar) |
|
75 | - * @link http://sourceforge.net/projects/kses/ Home Page for Kses |
|
76 | - * @link http://chaos.org/contact/ Contact page with current email address for Richard Vasquez |
|
77 | - * @copyright Richard R. V�squez, Jr. 2003-2005 |
|
78 | - * @version PHP4 OOP 0.2.2 |
|
79 | - * @license http://www.gnu.org/licenses/gpl.html GNU Public License |
|
80 | - * @package kses |
|
81 | - */ |
|
82 | - class kses4 |
|
83 | - { |
|
84 | - /**#@+ |
|
23 | + /** |
|
24 | + * Class file for PHP4 OOP version of kses |
|
25 | + * |
|
26 | + * This is an updated version of kses to work with PHP4 that works under E_STRICT. |
|
27 | + * |
|
28 | + * This upgrade provides the following: |
|
29 | + * + Version number synced to procedural version number |
|
30 | + * + PHPdoc style documentation has been added to the class. See http://www.phpdoc.org/ for more info. |
|
31 | + * + Some methods are now deprecated due to nomenclature style change. See method documentation for specifics. |
|
32 | + * + Kses4 now works in E_STRICT |
|
33 | + * + Addition of methods AddProtocols(), filterKsestextHook(), RemoveProtocol() and RemoveProtocols() |
|
34 | + * + Deprecated _hook(), Protocols() |
|
35 | + * + Integrated code from kses 0.2.2 into class. |
|
36 | + * + Added methods DumpProtocols(), DumpMethods() |
|
37 | + * |
|
38 | + * @package kses |
|
39 | + * @subpackage kses4 |
|
40 | + */ |
|
41 | + |
|
42 | + if(substr(phpversion(), 0, 1) < 4) |
|
43 | + { |
|
44 | + die("Class kses requires PHP 4 or higher."); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Only install KSES4 once |
|
49 | + */ |
|
50 | + if(!defined('KSES_CLASS_PHP4')) |
|
51 | + { |
|
52 | + define('KSES_CLASS_PHP4', true); |
|
53 | + |
|
54 | + /** |
|
55 | + * Kses strips evil scripts! |
|
56 | + * |
|
57 | + * This class provides the capability for removing unwanted HTML/XHTML, attributes from |
|
58 | + * tags, and protocols contained in links. The net result is a much more powerful tool |
|
59 | + * than the PHP internal strip_tags() |
|
60 | + * |
|
61 | + * This is a fork of a slick piece of procedural code called 'kses' written by Ulf Harnhammar |
|
62 | + * The entire set of functions was wrapped in a PHP object with some internal modifications |
|
63 | + * by Richard Vasquez (http://www.chaos.org/) 7/25/2003 |
|
64 | + * |
|
65 | + * This upgrade provides the following: |
|
66 | + * + Version number synced to procedural version number |
|
67 | + * + PHPdoc style documentation has been added to the class. See http://www.phpdoc.org/ for more info. |
|
68 | + * + Some methods are now deprecated due to nomenclature style change. See method documentation for specifics. |
|
69 | + * + Kses4 now works in E_STRICT |
|
70 | + * + Addition of methods AddProtocols(), filterKsestextHook(), RemoveProtocol(), RemoveProtocols() and SetProtocols() |
|
71 | + * + Deprecated _hook(), Protocols() |
|
72 | + * + Integrated code from kses 0.2.2 into class. |
|
73 | + * |
|
74 | + * @author Richard R. V�squez, Jr. (Original procedural code by Ulf H�rnhammar) |
|
75 | + * @link http://sourceforge.net/projects/kses/ Home Page for Kses |
|
76 | + * @link http://chaos.org/contact/ Contact page with current email address for Richard Vasquez |
|
77 | + * @copyright Richard R. V�squez, Jr. 2003-2005 |
|
78 | + * @version PHP4 OOP 0.2.2 |
|
79 | + * @license http://www.gnu.org/licenses/gpl.html GNU Public License |
|
80 | + * @package kses |
|
81 | + */ |
|
82 | + class kses4 |
|
83 | + { |
|
84 | + /**#@+ |
|
85 | 85 | * @access private |
86 | 86 | * @var array |
87 | 87 | */ |
88 | - var $allowed_protocols = array(); |
|
89 | - var $allowed_html = array(); |
|
90 | - /**#@-*/ |
|
91 | - |
|
92 | - /** |
|
93 | - * Constructor for kses. |
|
94 | - * |
|
95 | - * This sets a default collection of protocols allowed in links, and creates an |
|
96 | - * empty set of allowed HTML tags. |
|
97 | - * @since PHP4 OOP 0.0.1 |
|
98 | - */ |
|
99 | - function kses4() |
|
100 | - { |
|
101 | - /** |
|
102 | - * You could add protocols such as ftp, new, gopher, mailto, irc, etc. |
|
103 | - * |
|
104 | - * The base values the original kses provided were: |
|
105 | - * 'http', 'https', 'ftp', 'news', 'nntp', 'telnet', 'gopher', 'mailto' |
|
106 | - */ |
|
107 | - $this->allowed_protocols = array('http', 'ftp', 'mailto'); |
|
108 | - $this->allowed_html = array(); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Basic task of kses - parses $string and strips it as required. |
|
113 | - * |
|
114 | - * This method strips all the disallowed (X)HTML tags, attributes |
|
115 | - * and protocols from the input $string. |
|
116 | - * |
|
117 | - * @access public |
|
118 | - * @param string $string String to be stripped of 'evil scripts' |
|
119 | - * @return string The stripped string |
|
120 | - * @since PHP4 OOP 0.2.1 |
|
121 | - */ |
|
122 | - function Parse($string = "") |
|
123 | - { |
|
124 | - if (get_magic_quotes_gpc()) |
|
125 | - { |
|
126 | - $string = stripslashes($string); |
|
127 | - } |
|
128 | - $string = $this->_no_null($string); |
|
129 | - $string = $this->_js_entities($string); |
|
130 | - $string = $this->_normalize_entities($string); |
|
131 | - $string = $this->filterKsesTextHook($string); |
|
132 | - return $this->_split($string); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Allows for single/batch addition of protocols |
|
137 | - * |
|
138 | - * This method accepts one argument that can be either a string |
|
139 | - * or an array of strings. Invalid data will be ignored. |
|
140 | - * |
|
141 | - * The argument will be processed, and each string will be added |
|
142 | - * via AddProtocol(). |
|
143 | - * |
|
144 | - * @access public |
|
145 | - * @param mixed , A string or array of protocols that will be added to the internal list of allowed protocols. |
|
146 | - * @return bool Status of adding valid protocols. |
|
147 | - * @see AddProtocol() |
|
148 | - * @since PHP4 OOP 0.2.1 |
|
149 | - */ |
|
150 | - function AddProtocols() |
|
151 | - { |
|
152 | - $c_args = func_num_args(); |
|
153 | - if($c_args != 1) |
|
154 | - { |
|
155 | - trigger_error("kses4::AddProtocols() did not receive an argument.", E_USER_WARNING); |
|
156 | - return false; |
|
157 | - } |
|
158 | - |
|
159 | - $protocol_data = func_get_arg(0); |
|
160 | - |
|
161 | - if(is_array($protocol_data) && count($protocol_data) > 0) |
|
162 | - { |
|
163 | - foreach($protocol_data as $protocol) |
|
164 | - { |
|
165 | - $this->AddProtocol($protocol); |
|
166 | - } |
|
167 | - return true; |
|
168 | - } |
|
169 | - elseif(is_string($protocol_data)) |
|
170 | - { |
|
171 | - $this->AddProtocol($protocol_data); |
|
172 | - return true; |
|
173 | - } |
|
174 | - else |
|
175 | - { |
|
176 | - trigger_error("kses4::AddProtocols() did not receive a string or an array.", E_USER_WARNING); |
|
177 | - return false; |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Allows for single/batch addition of protocols |
|
183 | - * |
|
184 | - * @deprecated Use AddProtocols() |
|
185 | - * @see AddProtocols() |
|
186 | - * @return bool |
|
187 | - * @since PHP4 OOP 0.0.1 |
|
188 | - */ |
|
189 | - function Protocols() |
|
190 | - { |
|
191 | - $c_args = func_num_args(); |
|
192 | - if($c_args != 1) |
|
193 | - { |
|
194 | - trigger_error("kses4::Protocols() did not receive an argument.", E_USER_WARNING); |
|
195 | - return false; |
|
196 | - } |
|
197 | - |
|
198 | - return $this->AddProtocols(func_get_arg(0)); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Adds a single protocol to $this->allowed_protocols. |
|
203 | - * |
|
204 | - * This method accepts a string argument and adds it to |
|
205 | - * the list of allowed protocols to keep when performing |
|
206 | - * Parse(). |
|
207 | - * |
|
208 | - * @access public |
|
209 | - * @param string $protocol The name of the protocol to be added. |
|
210 | - * @return bool Status of adding valid protocol. |
|
211 | - * @since PHP4 OOP 0.0.1 |
|
212 | - */ |
|
213 | - function AddProtocol($protocol = "") |
|
214 | - { |
|
215 | - if(!is_string($protocol)) |
|
216 | - { |
|
217 | - trigger_error("kses4::AddProtocol() requires a string.", E_USER_WARNING); |
|
218 | - return false; |
|
219 | - } |
|
220 | - |
|
221 | - $protocol = strtolower(trim($protocol)); |
|
222 | - if($protocol == "") |
|
223 | - { |
|
224 | - trigger_error("kses4::AddProtocol() tried to add an empty/NULL protocol.", E_USER_WARNING); |
|
225 | - return false; |
|
226 | - } |
|
227 | - |
|
228 | - // Remove any inadvertent ':' at the end of the protocol. |
|
229 | - if(substr($protocol, strlen($protocol) - 1, 1) == ":") |
|
230 | - { |
|
231 | - $protocol = substr($protocol, 0, strlen($protocol) - 1); |
|
232 | - } |
|
233 | - |
|
234 | - if(!in_array($protocol, $this->allowed_protocols)) |
|
235 | - { |
|
236 | - array_push($this->allowed_protocols, $protocol); |
|
237 | - sort($this->allowed_protocols); |
|
238 | - } |
|
239 | - return true; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Allows for single/batch replacement of protocols |
|
244 | - * |
|
245 | - * This method accepts one argument that can be either a string |
|
246 | - * or an array of strings. Invalid data will be ignored. |
|
247 | - * |
|
248 | - * Existing protocols will be removed, then the argument will be |
|
249 | - * processed, and each string will be added via AddProtocol(). |
|
250 | - * |
|
251 | - * @access public |
|
252 | - * @param mixed , A string or array of protocols that will be the new internal list of allowed protocols. |
|
253 | - * @return bool Status of replacing valid protocols. |
|
254 | - * @since PHP4 OOP 0.2.2 |
|
255 | - * @see AddProtocol() |
|
256 | - */ |
|
257 | - function SetProtocols() |
|
258 | - { |
|
259 | - $c_args = func_num_args(); |
|
260 | - if($c_args != 1) |
|
261 | - { |
|
262 | - trigger_error("kses4::SetProtocols() did not receive an argument.", E_USER_WARNING); |
|
263 | - return false; |
|
264 | - } |
|
265 | - |
|
266 | - $protocol_data = func_get_arg(0); |
|
267 | - |
|
268 | - if(is_array($protocol_data) && count($protocol_data) > 0) |
|
269 | - { |
|
270 | - $this->allowed_protocols = array(); |
|
271 | - foreach($protocol_data as $protocol) |
|
272 | - { |
|
273 | - $this->AddProtocol($protocol); |
|
274 | - } |
|
275 | - return true; |
|
276 | - } |
|
277 | - elseif(is_string($protocol_data)) |
|
278 | - { |
|
279 | - $this->allowed_protocols = array(); |
|
280 | - $this->AddProtocol($protocol_data); |
|
281 | - return true; |
|
282 | - } |
|
283 | - else |
|
284 | - { |
|
285 | - trigger_error("kses4::SetProtocols() did not receive a string or an array.", E_USER_WARNING); |
|
286 | - return false; |
|
287 | - } |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Raw dump of allowed protocols |
|
292 | - * |
|
293 | - * This returns an indexed array of allowed protocols for a particular KSES |
|
294 | - * instantiation. |
|
295 | - * |
|
296 | - * @access public |
|
297 | - * @return array The list of allowed protocols. |
|
298 | - * @since PHP4 OOP 0.2.2 |
|
299 | - */ |
|
300 | - function DumpProtocols() |
|
301 | - { |
|
302 | - return $this->allowed_protocols; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Raw dump of allowed (X)HTML elements |
|
307 | - * |
|
308 | - * This returns an indexed array of allowed (X)HTML elements and attributes |
|
309 | - * for a particular KSES instantiation. |
|
310 | - * |
|
311 | - * @access public |
|
312 | - * @return array The list of allowed elements. |
|
313 | - * @since PHP4 OOP 0.2.2 |
|
314 | - */ |
|
315 | - function DumpElements() |
|
316 | - { |
|
317 | - return $this->allowed_html; |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Adds valid (X)HTML with corresponding attributes that will be kept when stripping 'evil scripts'. |
|
322 | - * |
|
323 | - * This method accepts one argument that can be either a string |
|
324 | - * or an array of strings. Invalid data will be ignored. |
|
325 | - * |
|
326 | - * @access public |
|
327 | - * @param string $tag (X)HTML tag that will be allowed after stripping text. |
|
328 | - * @param array $attribs Associative array of allowed attributes - key => attribute name - value => attribute parameter |
|
329 | - * @return bool Status of Adding (X)HTML and attributes. |
|
330 | - * @since PHP4 OOP 0.0.1 |
|
331 | - */ |
|
332 | - function AddHTML($tag = "", $attribs = array()) |
|
333 | - { |
|
334 | - if(!is_string($tag)) |
|
335 | - { |
|
336 | - trigger_error("kses4::AddHTML() requires the tag to be a string", E_USER_WARNING); |
|
337 | - return false; |
|
338 | - } |
|
339 | - |
|
340 | - $tag = strtolower(trim($tag)); |
|
341 | - if($tag == "") |
|
342 | - { |
|
343 | - trigger_error("kses4::AddHTML() tried to add an empty/NULL tag", E_USER_WARNING); |
|
344 | - return false; |
|
345 | - } |
|
346 | - |
|
347 | - if(!is_array($attribs)) |
|
348 | - { |
|
349 | - trigger_error("kses4::AddHTML() requires an array (even an empty one) of attributes for '$tag'", E_USER_WARNING); |
|
350 | - return false; |
|
351 | - } |
|
352 | - |
|
353 | - $new_attribs = array(); |
|
354 | - if(is_array($attribs) && count($attribs) > 0) |
|
355 | - { |
|
356 | - foreach($attribs as $idx1 => $val1) |
|
357 | - { |
|
358 | - $new_idx1 = strtolower($idx1); |
|
359 | - $new_val1 = $attribs[$idx1]; |
|
360 | - |
|
361 | - if(is_array($new_val1) && count($new_val1) > 0) |
|
362 | - { |
|
363 | - $tmp_val = array(); |
|
364 | - foreach($new_val1 as $idx2 => $val2) |
|
365 | - { |
|
366 | - $new_idx2 = strtolower($idx2); |
|
367 | - $tmp_val[$new_idx2] = $val2; |
|
368 | - } |
|
369 | - $new_val1 = $tmp_val; |
|
370 | - } |
|
371 | - |
|
372 | - $new_attribs[$new_idx1] = $new_val1; |
|
373 | - } |
|
374 | - } |
|
375 | - |
|
376 | - $this->allowed_html[$tag] = $new_attribs; |
|
377 | - return true; |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * Removes a single protocol from $this->allowed_protocols. |
|
382 | - * |
|
383 | - * This method accepts a string argument and removes it from |
|
384 | - * the list of allowed protocols to keep when performing |
|
385 | - * Parse(). |
|
386 | - * |
|
387 | - * @access public |
|
388 | - * @param string $protocol The name of the protocol to be removed. |
|
389 | - * @return bool Status of removing valid protocol. |
|
390 | - * @since PHP4 OOP 0.2.1 |
|
391 | - */ |
|
392 | - function RemoveProtocol($protocol = "") |
|
393 | - { |
|
394 | - if(!is_string($protocol)) |
|
395 | - { |
|
396 | - trigger_error("kses4::RemoveProtocol() requires a string.", E_USER_WARNING); |
|
397 | - return false; |
|
398 | - } |
|
399 | - |
|
400 | - // Remove any inadvertent ':' at the end of the protocol. |
|
401 | - if(substr($protocol, strlen($protocol) - 1, 1) == ":") |
|
402 | - { |
|
403 | - $protocol = substr($protocol, 0, strlen($protocol) - 1); |
|
404 | - } |
|
405 | - |
|
406 | - $protocol = strtolower(trim($protocol)); |
|
407 | - if($protocol == "") |
|
408 | - { |
|
409 | - trigger_error("kses4::RemoveProtocol() tried to remove an empty/NULL protocol.", E_USER_WARNING); |
|
410 | - return false; |
|
411 | - } |
|
412 | - |
|
413 | - // Ensures that the protocol exists before removing it. |
|
414 | - if(in_array($protocol, $this->allowed_protocols)) |
|
415 | - { |
|
416 | - $this->allowed_protocols = array_diff($this->allowed_protocols, array($protocol)); |
|
417 | - sort($this->allowed_protocols); |
|
418 | - } |
|
419 | - |
|
420 | - return true; |
|
421 | - } |
|
422 | - |
|
423 | - /** |
|
424 | - * Allows for single/batch removal of protocols |
|
425 | - * |
|
426 | - * This method accepts one argument that can be either a string |
|
427 | - * or an array of strings. Invalid data will be ignored. |
|
428 | - * |
|
429 | - * The argument will be processed, and each string will be removed |
|
430 | - * via RemoveProtocol(). |
|
431 | - * |
|
432 | - * @access public |
|
433 | - * @param mixed , A string or array of protocols that will be removed from the internal list of allowed protocols. |
|
434 | - * @return bool Status of removing valid protocols. |
|
435 | - * @see RemoveProtocol() |
|
436 | - * @since PHP5 OOP 0.2.1 |
|
437 | - */ |
|
438 | - function RemoveProtocols() |
|
439 | - { |
|
440 | - $c_args = func_num_args(); |
|
441 | - if($c_args != 1) |
|
442 | - { |
|
443 | - return false; |
|
444 | - } |
|
445 | - |
|
446 | - $protocol_data = func_get_arg(0); |
|
447 | - |
|
448 | - if(is_array($protocol_data) && count($protocol_data) > 0) |
|
449 | - { |
|
450 | - foreach($protocol_data as $protocol) |
|
451 | - { |
|
452 | - $this->RemoveProtocol($protocol); |
|
453 | - } |
|
454 | - } |
|
455 | - elseif(is_string($protocol_data)) |
|
456 | - { |
|
457 | - $this->RemoveProtocol($protocol_data); |
|
458 | - return true; |
|
459 | - } |
|
460 | - else |
|
461 | - { |
|
462 | - trigger_error("kses4::RemoveProtocols() did not receive a string or an array.", E_USER_WARNING); |
|
463 | - return false; |
|
464 | - } |
|
465 | - } |
|
466 | - |
|
467 | - /** |
|
468 | - * This method removes any NULL or characters in $string. |
|
469 | - * |
|
470 | - * @access private |
|
471 | - * @param string $string |
|
472 | - * @return string String without any NULL/chr(173) |
|
473 | - * @since PHP4 OOP 0.0.1 |
|
474 | - */ |
|
475 | - function _no_null($string) |
|
476 | - { |
|
477 | - $string = preg_replace('/\0+/', '', $string); |
|
478 | - $string = preg_replace('/(\\\\0)+/', '', $string); |
|
479 | - return $string; |
|
480 | - } |
|
481 | - |
|
482 | - /** |
|
483 | - * This function removes the HTML JavaScript entities found in early versions of |
|
484 | - * Netscape 4. |
|
485 | - * |
|
486 | - * @access private |
|
487 | - * @param string $string |
|
488 | - * @return string String without any NULL/chr(173) |
|
489 | - * @since PHP4 OOP 0.0.1 |
|
490 | - */ |
|
491 | - function _js_entities($string) |
|
492 | - { |
|
493 | - return preg_replace('%&\s*\{[^}]*(\}\s*;?|$)%', '', $string); |
|
494 | - } |
|
495 | - |
|
496 | - /** |
|
497 | - * Normalizes HTML entities |
|
498 | - * |
|
499 | - * This function normalizes HTML entities. It will convert "AT&T" to the correct |
|
500 | - * "AT&T", ":" to ":", "&#XYZZY;" to "&#XYZZY;" and so on. |
|
501 | - * |
|
502 | - * @access private |
|
503 | - * @param string $string |
|
504 | - * @return string String with normalized entities |
|
505 | - * @since PHP4 OOP 0.0.1 |
|
506 | - */ |
|
507 | - function _normalize_entities($string) |
|
508 | - { |
|
509 | - # Disarm all entities by converting & to & |
|
510 | - $string = str_replace('&', '&', $string); |
|
511 | - |
|
512 | - # Change back the allowed entities in our entity white list |
|
513 | - |
|
514 | - $string = preg_replace('/&([A-Za-z][A-Za-z0-9]{0,19});/', '&\\1;', $string); |
|
515 | - $string = preg_replace('/&#0*([0-9]{1,5});/e', '\$this->_normalize_entities2("\\1")', $string); |
|
516 | - $string = preg_replace('/&#([Xx])0*(([0-9A-Fa-f]{2}){1,2});/', '&#\\1\\2;', $string); |
|
517 | - |
|
518 | - return $string; |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * Helper method used by normalizeEntites() |
|
523 | - * |
|
524 | - * This method helps normalizeEntities() to only accept 16 bit values |
|
525 | - * and nothing more for &#number; entities. |
|
526 | - * |
|
527 | - * This method helps normalize_entities() during a preg_replace() |
|
528 | - * where a &#(0)*XXXXX; occurs. The '(0)*XXXXXX' value is converted to |
|
529 | - * a number and the result is returned as a numeric entity if the number |
|
530 | - * is less than 65536. Otherwise, the value is returned 'as is'. |
|
531 | - * |
|
532 | - * @access private |
|
533 | - * @param string $i |
|
534 | - * @return string Normalized numeric entity |
|
535 | - * @see _normalize_entities() |
|
536 | - * @since PHP4 OOP 0.0.1 |
|
537 | - */ |
|
538 | - function _normalize_entities2($i) |
|
539 | - { |
|
540 | - return (($i > 65535) ? "&#$i;" : "&#$i;"); |
|
541 | - } |
|
542 | - |
|
543 | - /** |
|
544 | - * Allows for additional user defined modifications to text. |
|
545 | - * |
|
546 | - * @deprecated use filterKsesTextHook() |
|
547 | - * @param string $string |
|
548 | - * @see filterKsesTextHook() |
|
549 | - * @return string |
|
550 | - * @since PHP4 OOP 0.0.1 |
|
551 | - */ |
|
552 | - function _hook($string) |
|
553 | - { |
|
554 | - return $this->filterKsesTextHook($string); |
|
555 | - } |
|
556 | - |
|
557 | - /** |
|
558 | - * Allows for additional user defined modifications to text. |
|
559 | - * |
|
560 | - * This method allows for additional modifications to be performed on |
|
561 | - * a string that's being run through Parse(). Currently, it returns the |
|
562 | - * input string 'as is'. |
|
563 | - * |
|
564 | - * This method is provided for users to extend the kses class for their own |
|
565 | - * requirements. |
|
566 | - * |
|
567 | - * @access public |
|
568 | - * @param string $string String to perfrom additional modifications on. |
|
569 | - * @return string User modified string. |
|
570 | - * @see Parse() |
|
571 | - * @since PHP5 OOP 1.0.0 |
|
572 | - */ |
|
573 | - function filterKsesTextHook($string) |
|
574 | - { |
|
575 | - return $string; |
|
576 | - } |
|
577 | - |
|
578 | - /** |
|
579 | - * This method goes through an array, and changes the keys to all lower case. |
|
580 | - * |
|
581 | - * @access private |
|
582 | - * @param array $in_array Associative array |
|
583 | - * @return array Modified array |
|
584 | - * @since PHP4 OOP 0.0.1 |
|
585 | - */ |
|
586 | - function _array_lc($inarray) |
|
587 | - { |
|
588 | - $outarray = array(); |
|
589 | - |
|
590 | - if(is_array($inarray) && count($inarray) > 0) |
|
591 | - { |
|
592 | - foreach ($inarray as $inkey => $inval) |
|
593 | - { |
|
594 | - $outkey = strtolower($inkey); |
|
595 | - $outarray[$outkey] = array(); |
|
596 | - |
|
597 | - if(is_array($inval) && count($inval) > 0) |
|
598 | - { |
|
599 | - foreach ($inval as $inkey2 => $inval2) |
|
600 | - { |
|
601 | - $outkey2 = strtolower($inkey2); |
|
602 | - $outarray[$outkey][$outkey2] = $inval2; |
|
603 | - } |
|
604 | - } |
|
605 | - } |
|
606 | - } |
|
607 | - |
|
608 | - return $outarray; |
|
609 | - } |
|
610 | - |
|
611 | - /** |
|
612 | - * This method searched for HTML tags, no matter how malformed. It also |
|
613 | - * matches stray ">" characters. |
|
614 | - * |
|
615 | - * @access private |
|
616 | - * @param string $string |
|
617 | - * @return string HTML tags |
|
618 | - * @since PHP4 OOP 0.0.1 |
|
619 | - */ |
|
620 | - function _split($string) |
|
621 | - { |
|
622 | - return preg_replace( |
|
623 | - '%(<'. # EITHER: < |
|
624 | - '[^>]*'. # things that aren't > |
|
625 | - '(>|$)'. # > or end of string |
|
626 | - '|>)%e', # OR: just a > |
|
627 | - "\$this->_split2('\\1')", |
|
628 | - $string); |
|
629 | - } |
|
630 | - |
|
631 | - /** |
|
632 | - * This method strips out disallowed and/or mangled (X)HTML tags along with assigned attributes. |
|
633 | - * |
|
634 | - * This method does a lot of work. It rejects some very malformed things |
|
635 | - * like <:::>. It returns an empty string if the element isn't allowed (look |
|
636 | - * ma, no strip_tags()!). Otherwise it splits the tag into an element and an |
|
637 | - * allowed attribute list. |
|
638 | - * |
|
639 | - * @access private |
|
640 | - * @param string $string |
|
641 | - * @return string Modified string minus disallowed/mangled (X)HTML and attributes |
|
642 | - * @since PHP4 OOP 0.0.1 |
|
643 | - */ |
|
644 | - function _split2($string) |
|
645 | - { |
|
646 | - $string = $this->_stripslashes($string); |
|
647 | - |
|
648 | - if (substr($string, 0, 1) != '<') |
|
649 | - { |
|
650 | - # It matched a ">" character |
|
651 | - return '>'; |
|
652 | - } |
|
653 | - |
|
654 | - if (!preg_match('%^<\s*(/\s*)?([a-zA-Z0-9]+)([^>]*)>?$%', $string, $matches)) |
|
655 | - { |
|
656 | - # It's seriously malformed |
|
657 | - return ''; |
|
658 | - } |
|
659 | - |
|
660 | - $slash = trim($matches[1]); |
|
661 | - $elem = $matches[2]; |
|
662 | - $attrlist = $matches[3]; |
|
663 | - |
|
664 | - if ( |
|
665 | - !isset($this->allowed_html[strtolower($elem)]) || |
|
666 | - !is_array($this->allowed_html[strtolower($elem)]) |
|
667 | - ) |
|
668 | - { |
|
669 | - # They are using a not allowed HTML element |
|
670 | - return ''; |
|
671 | - } |
|
672 | - |
|
673 | - if ($slash != '') |
|
674 | - { |
|
675 | - return "<$slash$elem>"; |
|
676 | - } |
|
677 | - # No attributes are allowed for closing elements |
|
678 | - |
|
679 | - return $this->_attr("$slash$elem", $attrlist); |
|
680 | - } |
|
681 | - |
|
682 | - /** |
|
683 | - * This method strips out disallowed attributes for (X)HTML tags. |
|
684 | - * |
|
685 | - * This method removes all attributes if none are allowed for this element. |
|
686 | - * If some are allowed it calls $this->_hair() to split them further, and then it |
|
687 | - * builds up new HTML code from the data that $this->_hair() returns. It also |
|
688 | - * removes "<" and ">" characters, if there are any left. One more thing it |
|
689 | - * does is to check if the tag has a closing XHTML slash, and if it does, |
|
690 | - * it puts one in the returned code as well. |
|
691 | - * |
|
692 | - * @access private |
|
693 | - * @param string $element (X)HTML tag to check |
|
694 | - * @param string $attr Text containing attributes to check for validity. |
|
695 | - * @return string Resulting valid (X)HTML or '' |
|
696 | - * @see _hair() |
|
697 | - * @since PHP4 OOP 0.0.1 |
|
698 | - */ |
|
699 | - function _attr($element, $attr) |
|
700 | - { |
|
701 | - # Is there a closing XHTML slash at the end of the attributes? |
|
702 | - $xhtml_slash = ''; |
|
703 | - if (preg_match('%\s/\s*$%', $attr)) |
|
704 | - { |
|
705 | - $xhtml_slash = ' /'; |
|
706 | - } |
|
707 | - |
|
708 | - # Are any attributes allowed at all for this element? |
|
709 | - if ( |
|
710 | - !isset($this->allowed_html[strtolower($element)]) || |
|
711 | - count($this->allowed_html[strtolower($element)]) == 0 |
|
712 | - ) |
|
713 | - { |
|
714 | - return "<$element$xhtml_slash>"; |
|
715 | - } |
|
716 | - |
|
717 | - # Split it |
|
718 | - $attrarr = $this->_hair($attr); |
|
719 | - |
|
720 | - # Go through $attrarr, and save the allowed attributes for this element |
|
721 | - # in $attr2 |
|
722 | - $attr2 = ''; |
|
723 | - if(is_array($attrarr) && count($attrarr) > 0) |
|
724 | - { |
|
725 | - foreach ($attrarr as $arreach) |
|
726 | - { |
|
727 | - if(!isset($this->allowed_html[strtolower($element)][strtolower($arreach['name'])])) |
|
728 | - { |
|
729 | - continue; |
|
730 | - } |
|
731 | - |
|
732 | - $current = $this->allowed_html[strtolower($element)][strtolower($arreach['name'])]; |
|
733 | - if ($current == '') |
|
734 | - { |
|
735 | - # the attribute is not allowed |
|
736 | - continue; |
|
737 | - } |
|
738 | - |
|
739 | - if (!is_array($current)) |
|
740 | - { |
|
741 | - # there are no checks |
|
742 | - $attr2 .= ' '.$arreach['whole']; |
|
743 | - } |
|
744 | - else |
|
745 | - { |
|
746 | - # there are some checks |
|
747 | - $ok = true; |
|
748 | - if(is_array($current) && count($current) > 0) |
|
749 | - { |
|
750 | - foreach ($current as $currkey => $currval) |
|
751 | - { |
|
752 | - if (!$this->_check_attr_val($arreach['value'], $arreach['vless'], $currkey, $currval)) |
|
753 | - { |
|
754 | - $ok = false; |
|
755 | - break; |
|
756 | - } |
|
757 | - } |
|
758 | - |
|
759 | - if ($ok) |
|
760 | - { |
|
761 | - # it passed them |
|
762 | - $attr2 .= ' '.$arreach['whole']; |
|
763 | - } |
|
764 | - } |
|
765 | - } |
|
766 | - } |
|
767 | - } |
|
768 | - |
|
769 | - # Remove any "<" or ">" characters |
|
770 | - $attr2 = preg_replace('/[<>]/', '', $attr2); |
|
771 | - return "<$element$attr2$xhtml_slash>"; |
|
772 | - } |
|
773 | - |
|
774 | - /** |
|
775 | - * This method combs through an attribute list string and returns an associative array of attributes and values. |
|
776 | - * |
|
777 | - * This method does a lot of work. It parses an attribute list into an array |
|
778 | - * with attribute data, and tries to do the right thing even if it gets weird |
|
779 | - * input. It will add quotes around attribute values that don't have any quotes |
|
780 | - * or apostrophes around them, to make it easier to produce HTML code that will |
|
781 | - * conform to W3C's HTML specification. It will also remove bad URL protocols |
|
782 | - * from attribute values. |
|
783 | - * |
|
784 | - * @access private |
|
785 | - * @param string $attr Text containing tag attributes for parsing |
|
786 | - * @return array Associative array containing data on attribute and value |
|
787 | - * @since PHP4 OOP 0.0.1 |
|
788 | - */ |
|
789 | - function _hair($attr) |
|
790 | - { |
|
791 | - $attrarr = array(); |
|
792 | - $mode = 0; |
|
793 | - $attrname = ''; |
|
794 | - |
|
795 | - # Loop through the whole attribute list |
|
796 | - |
|
797 | - while (strlen($attr) != 0) |
|
798 | - { |
|
799 | - # Was the last operation successful? |
|
800 | - $working = 0; |
|
801 | - |
|
802 | - switch ($mode) |
|
803 | - { |
|
804 | - case 0: # attribute name, href for instance |
|
805 | - if (preg_match('/^([-a-zA-Z]+)/', $attr, $match)) |
|
806 | - { |
|
807 | - $attrname = $match[1]; |
|
808 | - $working = $mode = 1; |
|
809 | - $attr = preg_replace('/^[-a-zA-Z]+/', '', $attr); |
|
810 | - } |
|
811 | - break; |
|
812 | - case 1: # equals sign or valueless ("selected") |
|
813 | - if (preg_match('/^\s*=\s*/', $attr)) # equals sign |
|
814 | - { |
|
815 | - $working = 1; |
|
816 | - $mode = 2; |
|
817 | - $attr = preg_replace('/^\s*=\s*/', '', $attr); |
|
818 | - break; |
|
819 | - } |
|
820 | - if (preg_match('/^\s+/', $attr)) # valueless |
|
821 | - { |
|
822 | - $working = 1; |
|
823 | - $mode = 0; |
|
824 | - $attrarr[] = array( |
|
825 | - 'name' => $attrname, |
|
826 | - 'value' => '', |
|
827 | - 'whole' => $attrname, |
|
828 | - 'vless' => 'y' |
|
829 | - ); |
|
830 | - $attr = preg_replace('/^\s+/', '', $attr); |
|
831 | - } |
|
832 | - break; |
|
833 | - case 2: # attribute value, a URL after href= for instance |
|
834 | - if (preg_match('/^"([^"]*)"(\s+|$)/', $attr, $match)) # "value" |
|
835 | - { |
|
836 | - $thisval = $this->_bad_protocol($match[1]); |
|
837 | - $attrarr[] = array( |
|
838 | - 'name' => $attrname, |
|
839 | - 'value' => $thisval, |
|
840 | - 'whole' => "$attrname=\"$thisval\"", |
|
841 | - 'vless' => 'n' |
|
842 | - ); |
|
843 | - $working = 1; |
|
844 | - $mode = 0; |
|
845 | - $attr = preg_replace('/^"[^"]*"(\s+|$)/', '', $attr); |
|
846 | - break; |
|
847 | - } |
|
848 | - if (preg_match("/^'([^']*)'(\s+|$)/", $attr, $match)) # 'value' |
|
849 | - { |
|
850 | - $thisval = $this->_bad_protocol($match[1]); |
|
851 | - $attrarr[] = array( |
|
852 | - 'name' => $attrname, |
|
853 | - 'value' => $thisval, |
|
854 | - 'whole' => "$attrname='$thisval'", |
|
855 | - 'vless' => 'n' |
|
856 | - ); |
|
857 | - $working = 1; |
|
858 | - $mode = 0; |
|
859 | - $attr = preg_replace("/^'[^']*'(\s+|$)/", '', $attr); |
|
860 | - break; |
|
861 | - } |
|
862 | - if (preg_match("%^([^\s\"']+)(\s+|$)%", $attr, $match)) # value |
|
863 | - { |
|
864 | - $thisval = $this->_bad_protocol($match[1]); |
|
865 | - $attrarr[] = array( |
|
866 | - 'name' => $attrname, |
|
867 | - 'value' => $thisval, |
|
868 | - 'whole' => "$attrname=\"$thisval\"", |
|
869 | - 'vless' => 'n' |
|
870 | - ); |
|
871 | - # We add quotes to conform to W3C's HTML spec. |
|
872 | - $working = 1; |
|
873 | - $mode = 0; |
|
874 | - $attr = preg_replace("%^[^\s\"']+(\s+|$)%", '', $attr); |
|
875 | - } |
|
876 | - break; |
|
877 | - } |
|
878 | - |
|
879 | - if ($working == 0) # not well formed, remove and try again |
|
880 | - { |
|
881 | - $attr = $this->_html_error($attr); |
|
882 | - $mode = 0; |
|
883 | - } |
|
884 | - } |
|
885 | - |
|
886 | - # special case, for when the attribute list ends with a valueless |
|
887 | - # attribute like "selected" |
|
888 | - if ($mode == 1) |
|
889 | - { |
|
890 | - $attrarr[] = array( |
|
891 | - 'name' => $attrname, |
|
892 | - 'value' => '', |
|
893 | - 'whole' => $attrname, |
|
894 | - 'vless' => 'y' |
|
895 | - ); |
|
896 | - } |
|
897 | - |
|
898 | - return $attrarr; |
|
899 | - } |
|
900 | - |
|
901 | - /** |
|
902 | - * This method removes disallowed protocols. |
|
903 | - * |
|
904 | - * This method removes all non-allowed protocols from the beginning of |
|
905 | - * $string. It ignores whitespace and the case of the letters, and it does |
|
906 | - * understand HTML entities. It does its work in a while loop, so it won't be |
|
907 | - * fooled by a string like "javascript:javascript:alert(57)". |
|
908 | - * |
|
909 | - * @access private |
|
910 | - * @param string $string String to check for protocols |
|
911 | - * @return string String with removed protocols |
|
912 | - * @since PHP4 OOP 0.0.1 |
|
913 | - */ |
|
914 | - function _bad_protocol($string) |
|
915 | - { |
|
916 | - $string = $this->_no_null($string); |
|
917 | - $string = preg_replace('/\xad+/', '', $string); # deals with Opera "feature" |
|
918 | - $string2 = $string.'a'; |
|
919 | - |
|
920 | - while ($string != $string2) |
|
921 | - { |
|
922 | - $string2 = $string; |
|
923 | - $string = $this->_bad_protocol_once($string); |
|
924 | - } # while |
|
925 | - |
|
926 | - return $string; |
|
927 | - } |
|
928 | - |
|
929 | - /** |
|
930 | - * Helper method used by _bad_protocol() |
|
931 | - * |
|
932 | - * This function searches for URL protocols at the beginning of $string, while |
|
933 | - * handling whitespace and HTML entities. |
|
934 | - * Function updated to fix security vulnerability (see http://projects.dokeos.com/index.php?do=details&task_id=2312) |
|
935 | - * |
|
936 | - * @access private |
|
937 | - * @param string $string String to check for protocols |
|
938 | - * @return string String with removed protocols |
|
939 | - * @see _bad_protocol() |
|
940 | - * @since PHP4 OOP 0.0.1 |
|
941 | - */ |
|
942 | - function _bad_protocol_once($string) |
|
943 | - { |
|
944 | - $string2 = preg_split('/:|:|:/i', $string, 2); |
|
945 | - if(isset($string2[1]) && !preg_match('%/\?%',$string2[0])) |
|
946 | - { |
|
947 | - $string = $this->_bad_protocol_once2($string2[0]).trim($string2[1]); |
|
948 | - } |
|
949 | - return $string; |
|
950 | - } |
|
951 | - /** |
|
952 | - * Helper method used by _bad_protocol_once() regex |
|
953 | - * |
|
954 | - * This function processes URL protocols, checks to see if they're in the white- |
|
955 | - * list or not, and returns different data depending on the answer. |
|
956 | - * |
|
957 | - * @access private |
|
958 | - * @param string $string String to check for protocols |
|
959 | - * @return string String with removed protocols |
|
960 | - * @see _bad_protocol() |
|
961 | - * @see _bad_protocol_once() |
|
962 | - * @since PHP4 OOP 0.0.1 |
|
963 | - */ |
|
964 | - function _bad_protocol_once2($string) |
|
965 | - { |
|
966 | - $string = $this->_decode_entities($string); |
|
967 | - $string = preg_replace('/\s/', '', $string); |
|
968 | - $string = $this->_no_null($string); |
|
969 | - $string = preg_replace('/\xad+/', '', $string); # deals with Opera "feature" |
|
970 | - $string = strtolower($string); |
|
971 | - |
|
972 | - $allowed = false; |
|
973 | - if(is_array($this->allowed_protocols) && count($this->allowed_protocols) > 0) |
|
974 | - { |
|
975 | - foreach ($this->allowed_protocols as $one_protocol) |
|
976 | - { |
|
977 | - if (strtolower($one_protocol) == $string) |
|
978 | - { |
|
979 | - $allowed = true; |
|
980 | - break; |
|
981 | - } |
|
982 | - } |
|
983 | - } |
|
984 | - |
|
985 | - if ($allowed) |
|
986 | - { |
|
987 | - return "$string:"; |
|
988 | - } |
|
989 | - else |
|
990 | - { |
|
991 | - return ''; |
|
992 | - } |
|
993 | - } |
|
994 | - |
|
995 | - /** |
|
996 | - * This function performs different checks for attribute values. |
|
997 | - * |
|
998 | - * The currently implemented checks are "maxlen", "minlen", "maxval", |
|
999 | - * "minval" and "valueless" with even more checks to come soon. |
|
1000 | - * |
|
1001 | - * @access private |
|
1002 | - * @param string $value The value of the attribute to be checked. |
|
1003 | - * @param string $vless Indicates whether the the value is supposed to be valueless |
|
1004 | - * @param string $checkname The check to be performed |
|
1005 | - * @param string $checkvalue The value that is to be checked against |
|
1006 | - * @return bool Indicates whether the check passed or not |
|
1007 | - * @since PHP4 OOP 0.0.1 |
|
1008 | - */ |
|
1009 | - function _check_attr_val($value, $vless, $checkname, $checkvalue) |
|
1010 | - { |
|
1011 | - $ok = true; |
|
1012 | - |
|
1013 | - switch (strtolower($checkname)) |
|
1014 | - { |
|
1015 | - /** |
|
1016 | - * The maxlen check makes sure that the attribute value has a length not |
|
1017 | - * greater than the given value. This can be used to avoid Buffer Overflows |
|
1018 | - * in WWW clients and various Internet servers. |
|
1019 | - */ |
|
1020 | - case 'maxlen': |
|
1021 | - if (strlen($value) > $checkvalue) |
|
1022 | - { |
|
1023 | - $ok = false; |
|
1024 | - } |
|
1025 | - break; |
|
1026 | - |
|
1027 | - /** |
|
1028 | - * The minlen check makes sure that the attribute value has a length not |
|
1029 | - * smaller than the given value. |
|
1030 | - */ |
|
1031 | - case 'minlen': |
|
1032 | - if (strlen($value) < $checkvalue) |
|
1033 | - { |
|
1034 | - $ok = false; |
|
1035 | - } |
|
1036 | - break; |
|
1037 | - |
|
1038 | - /** |
|
1039 | - * The maxval check does two things: it checks that the attribute value is |
|
1040 | - * an integer from 0 and up, without an excessive amount of zeroes or |
|
1041 | - * whitespace (to avoid Buffer Overflows). It also checks that the attribute |
|
1042 | - * value is not greater than the given value. |
|
1043 | - * This check can be used to avoid Denial of Service attacks. |
|
1044 | - */ |
|
1045 | - case 'maxval': |
|
1046 | - if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value)) |
|
1047 | - { |
|
1048 | - $ok = false; |
|
1049 | - } |
|
1050 | - if ($value > $checkvalue) |
|
1051 | - { |
|
1052 | - $ok = false; |
|
1053 | - } |
|
1054 | - break; |
|
1055 | - |
|
1056 | - /** |
|
1057 | - * The minval check checks that the attribute value is a positive integer, |
|
1058 | - * and that it is not smaller than the given value. |
|
1059 | - */ |
|
1060 | - case 'minval': |
|
1061 | - if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value)) |
|
1062 | - { |
|
1063 | - $ok = false; |
|
1064 | - } |
|
1065 | - if ($value < $checkvalue) |
|
1066 | - { |
|
1067 | - $ok = false; |
|
1068 | - } |
|
1069 | - break; |
|
1070 | - |
|
1071 | - /** |
|
1072 | - * The valueless check checks if the attribute has a value |
|
1073 | - * (like <a href="blah">) or not (<option selected>). If the given value |
|
1074 | - * is a "y" or a "Y", the attribute must not have a value. |
|
1075 | - * If the given value is an "n" or an "N", the attribute must have one. |
|
1076 | - */ |
|
1077 | - case 'valueless': |
|
1078 | - if (strtolower($checkvalue) != $vless) |
|
1079 | - { |
|
1080 | - $ok = false; |
|
1081 | - } |
|
1082 | - break; |
|
1083 | - |
|
1084 | - } |
|
1085 | - |
|
1086 | - return $ok; |
|
1087 | - } |
|
1088 | - |
|
1089 | - /** |
|
1090 | - * Changes \" to " |
|
1091 | - * |
|
1092 | - * This function changes the character sequence \" to just " |
|
1093 | - * It leaves all other slashes alone. It's really weird, but the quoting from |
|
1094 | - * preg_replace(//e) seems to require this. |
|
1095 | - * |
|
1096 | - * @access private |
|
1097 | - * @param string $string The string to be stripped. |
|
1098 | - * @return string string stripped of \" |
|
1099 | - * @since PHP4 OOP 0.0.1 |
|
1100 | - */ |
|
1101 | - function _stripslashes($string) |
|
1102 | - { |
|
1103 | - return preg_replace('%\\\\"%', '"', $string); |
|
1104 | - } |
|
1105 | - |
|
1106 | - /** |
|
1107 | - * helper method for _hair() |
|
1108 | - * |
|
1109 | - * This function deals with parsing errors in _hair(). The general plan is |
|
1110 | - * to remove everything to and including some whitespace, but it deals with |
|
1111 | - * quotes and apostrophes as well. |
|
1112 | - * |
|
1113 | - * @access private |
|
1114 | - * @param string $string The string to be stripped. |
|
1115 | - * @return string string stripped of whitespace |
|
1116 | - * @see _hair() |
|
1117 | - * @since PHP4 OOP 0.0.1 |
|
1118 | - */ |
|
1119 | - function _html_error($string) |
|
1120 | - { |
|
1121 | - return preg_replace('/^("[^"]*("|$)|\'[^\']*(\'|$)|\S)*\s*/', '', $string); |
|
1122 | - } |
|
1123 | - |
|
1124 | - /** |
|
1125 | - * Decodes numeric HTML entities |
|
1126 | - * |
|
1127 | - * This method decodes numeric HTML entities (A and A). It doesn't |
|
1128 | - * do anything with other entities like ä, but we don't need them in the |
|
1129 | - * URL protocol white listing system anyway. |
|
1130 | - * |
|
1131 | - * @access private |
|
1132 | - * @param string $value The entitiy to be decoded. |
|
1133 | - * @return string Decoded entity |
|
1134 | - * @since PHP4 OOP 0.0.1 |
|
1135 | - */ |
|
1136 | - function _decode_entities($string) |
|
1137 | - { |
|
1138 | - $string = preg_replace('/&#([0-9]+);/e', 'chr("\\1")', $string); |
|
1139 | - $string = preg_replace('/&#[Xx]([0-9A-Fa-f]+);/e', 'chr(hexdec("\\1"))', $string); |
|
1140 | - return $string; |
|
1141 | - } |
|
1142 | - |
|
1143 | - /** |
|
1144 | - * Returns PHP4 OOP version # of kses. |
|
1145 | - * |
|
1146 | - * Since this class has been refactored and documented and proven to work, |
|
1147 | - * I'm syncing the version number to procedural kses. |
|
1148 | - * |
|
1149 | - * @access public |
|
1150 | - * @return string Version number |
|
1151 | - * @since PHP4 OOP 0.0.1 |
|
1152 | - */ |
|
1153 | - function _version() |
|
1154 | - { |
|
1155 | - return 'PHP4 0.2.2 (OOP fork of procedural kses 0.2.2)'; |
|
1156 | - } |
|
1157 | - } |
|
1158 | - |
|
1159 | - |
|
1160 | - |
|
1161 | - } |
|
88 | + var $allowed_protocols = array(); |
|
89 | + var $allowed_html = array(); |
|
90 | + /**#@-*/ |
|
91 | + |
|
92 | + /** |
|
93 | + * Constructor for kses. |
|
94 | + * |
|
95 | + * This sets a default collection of protocols allowed in links, and creates an |
|
96 | + * empty set of allowed HTML tags. |
|
97 | + * @since PHP4 OOP 0.0.1 |
|
98 | + */ |
|
99 | + function kses4() |
|
100 | + { |
|
101 | + /** |
|
102 | + * You could add protocols such as ftp, new, gopher, mailto, irc, etc. |
|
103 | + * |
|
104 | + * The base values the original kses provided were: |
|
105 | + * 'http', 'https', 'ftp', 'news', 'nntp', 'telnet', 'gopher', 'mailto' |
|
106 | + */ |
|
107 | + $this->allowed_protocols = array('http', 'ftp', 'mailto'); |
|
108 | + $this->allowed_html = array(); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Basic task of kses - parses $string and strips it as required. |
|
113 | + * |
|
114 | + * This method strips all the disallowed (X)HTML tags, attributes |
|
115 | + * and protocols from the input $string. |
|
116 | + * |
|
117 | + * @access public |
|
118 | + * @param string $string String to be stripped of 'evil scripts' |
|
119 | + * @return string The stripped string |
|
120 | + * @since PHP4 OOP 0.2.1 |
|
121 | + */ |
|
122 | + function Parse($string = "") |
|
123 | + { |
|
124 | + if (get_magic_quotes_gpc()) |
|
125 | + { |
|
126 | + $string = stripslashes($string); |
|
127 | + } |
|
128 | + $string = $this->_no_null($string); |
|
129 | + $string = $this->_js_entities($string); |
|
130 | + $string = $this->_normalize_entities($string); |
|
131 | + $string = $this->filterKsesTextHook($string); |
|
132 | + return $this->_split($string); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Allows for single/batch addition of protocols |
|
137 | + * |
|
138 | + * This method accepts one argument that can be either a string |
|
139 | + * or an array of strings. Invalid data will be ignored. |
|
140 | + * |
|
141 | + * The argument will be processed, and each string will be added |
|
142 | + * via AddProtocol(). |
|
143 | + * |
|
144 | + * @access public |
|
145 | + * @param mixed , A string or array of protocols that will be added to the internal list of allowed protocols. |
|
146 | + * @return bool Status of adding valid protocols. |
|
147 | + * @see AddProtocol() |
|
148 | + * @since PHP4 OOP 0.2.1 |
|
149 | + */ |
|
150 | + function AddProtocols() |
|
151 | + { |
|
152 | + $c_args = func_num_args(); |
|
153 | + if($c_args != 1) |
|
154 | + { |
|
155 | + trigger_error("kses4::AddProtocols() did not receive an argument.", E_USER_WARNING); |
|
156 | + return false; |
|
157 | + } |
|
158 | + |
|
159 | + $protocol_data = func_get_arg(0); |
|
160 | + |
|
161 | + if(is_array($protocol_data) && count($protocol_data) > 0) |
|
162 | + { |
|
163 | + foreach($protocol_data as $protocol) |
|
164 | + { |
|
165 | + $this->AddProtocol($protocol); |
|
166 | + } |
|
167 | + return true; |
|
168 | + } |
|
169 | + elseif(is_string($protocol_data)) |
|
170 | + { |
|
171 | + $this->AddProtocol($protocol_data); |
|
172 | + return true; |
|
173 | + } |
|
174 | + else |
|
175 | + { |
|
176 | + trigger_error("kses4::AddProtocols() did not receive a string or an array.", E_USER_WARNING); |
|
177 | + return false; |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Allows for single/batch addition of protocols |
|
183 | + * |
|
184 | + * @deprecated Use AddProtocols() |
|
185 | + * @see AddProtocols() |
|
186 | + * @return bool |
|
187 | + * @since PHP4 OOP 0.0.1 |
|
188 | + */ |
|
189 | + function Protocols() |
|
190 | + { |
|
191 | + $c_args = func_num_args(); |
|
192 | + if($c_args != 1) |
|
193 | + { |
|
194 | + trigger_error("kses4::Protocols() did not receive an argument.", E_USER_WARNING); |
|
195 | + return false; |
|
196 | + } |
|
197 | + |
|
198 | + return $this->AddProtocols(func_get_arg(0)); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Adds a single protocol to $this->allowed_protocols. |
|
203 | + * |
|
204 | + * This method accepts a string argument and adds it to |
|
205 | + * the list of allowed protocols to keep when performing |
|
206 | + * Parse(). |
|
207 | + * |
|
208 | + * @access public |
|
209 | + * @param string $protocol The name of the protocol to be added. |
|
210 | + * @return bool Status of adding valid protocol. |
|
211 | + * @since PHP4 OOP 0.0.1 |
|
212 | + */ |
|
213 | + function AddProtocol($protocol = "") |
|
214 | + { |
|
215 | + if(!is_string($protocol)) |
|
216 | + { |
|
217 | + trigger_error("kses4::AddProtocol() requires a string.", E_USER_WARNING); |
|
218 | + return false; |
|
219 | + } |
|
220 | + |
|
221 | + $protocol = strtolower(trim($protocol)); |
|
222 | + if($protocol == "") |
|
223 | + { |
|
224 | + trigger_error("kses4::AddProtocol() tried to add an empty/NULL protocol.", E_USER_WARNING); |
|
225 | + return false; |
|
226 | + } |
|
227 | + |
|
228 | + // Remove any inadvertent ':' at the end of the protocol. |
|
229 | + if(substr($protocol, strlen($protocol) - 1, 1) == ":") |
|
230 | + { |
|
231 | + $protocol = substr($protocol, 0, strlen($protocol) - 1); |
|
232 | + } |
|
233 | + |
|
234 | + if(!in_array($protocol, $this->allowed_protocols)) |
|
235 | + { |
|
236 | + array_push($this->allowed_protocols, $protocol); |
|
237 | + sort($this->allowed_protocols); |
|
238 | + } |
|
239 | + return true; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Allows for single/batch replacement of protocols |
|
244 | + * |
|
245 | + * This method accepts one argument that can be either a string |
|
246 | + * or an array of strings. Invalid data will be ignored. |
|
247 | + * |
|
248 | + * Existing protocols will be removed, then the argument will be |
|
249 | + * processed, and each string will be added via AddProtocol(). |
|
250 | + * |
|
251 | + * @access public |
|
252 | + * @param mixed , A string or array of protocols that will be the new internal list of allowed protocols. |
|
253 | + * @return bool Status of replacing valid protocols. |
|
254 | + * @since PHP4 OOP 0.2.2 |
|
255 | + * @see AddProtocol() |
|
256 | + */ |
|
257 | + function SetProtocols() |
|
258 | + { |
|
259 | + $c_args = func_num_args(); |
|
260 | + if($c_args != 1) |
|
261 | + { |
|
262 | + trigger_error("kses4::SetProtocols() did not receive an argument.", E_USER_WARNING); |
|
263 | + return false; |
|
264 | + } |
|
265 | + |
|
266 | + $protocol_data = func_get_arg(0); |
|
267 | + |
|
268 | + if(is_array($protocol_data) && count($protocol_data) > 0) |
|
269 | + { |
|
270 | + $this->allowed_protocols = array(); |
|
271 | + foreach($protocol_data as $protocol) |
|
272 | + { |
|
273 | + $this->AddProtocol($protocol); |
|
274 | + } |
|
275 | + return true; |
|
276 | + } |
|
277 | + elseif(is_string($protocol_data)) |
|
278 | + { |
|
279 | + $this->allowed_protocols = array(); |
|
280 | + $this->AddProtocol($protocol_data); |
|
281 | + return true; |
|
282 | + } |
|
283 | + else |
|
284 | + { |
|
285 | + trigger_error("kses4::SetProtocols() did not receive a string or an array.", E_USER_WARNING); |
|
286 | + return false; |
|
287 | + } |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Raw dump of allowed protocols |
|
292 | + * |
|
293 | + * This returns an indexed array of allowed protocols for a particular KSES |
|
294 | + * instantiation. |
|
295 | + * |
|
296 | + * @access public |
|
297 | + * @return array The list of allowed protocols. |
|
298 | + * @since PHP4 OOP 0.2.2 |
|
299 | + */ |
|
300 | + function DumpProtocols() |
|
301 | + { |
|
302 | + return $this->allowed_protocols; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Raw dump of allowed (X)HTML elements |
|
307 | + * |
|
308 | + * This returns an indexed array of allowed (X)HTML elements and attributes |
|
309 | + * for a particular KSES instantiation. |
|
310 | + * |
|
311 | + * @access public |
|
312 | + * @return array The list of allowed elements. |
|
313 | + * @since PHP4 OOP 0.2.2 |
|
314 | + */ |
|
315 | + function DumpElements() |
|
316 | + { |
|
317 | + return $this->allowed_html; |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Adds valid (X)HTML with corresponding attributes that will be kept when stripping 'evil scripts'. |
|
322 | + * |
|
323 | + * This method accepts one argument that can be either a string |
|
324 | + * or an array of strings. Invalid data will be ignored. |
|
325 | + * |
|
326 | + * @access public |
|
327 | + * @param string $tag (X)HTML tag that will be allowed after stripping text. |
|
328 | + * @param array $attribs Associative array of allowed attributes - key => attribute name - value => attribute parameter |
|
329 | + * @return bool Status of Adding (X)HTML and attributes. |
|
330 | + * @since PHP4 OOP 0.0.1 |
|
331 | + */ |
|
332 | + function AddHTML($tag = "", $attribs = array()) |
|
333 | + { |
|
334 | + if(!is_string($tag)) |
|
335 | + { |
|
336 | + trigger_error("kses4::AddHTML() requires the tag to be a string", E_USER_WARNING); |
|
337 | + return false; |
|
338 | + } |
|
339 | + |
|
340 | + $tag = strtolower(trim($tag)); |
|
341 | + if($tag == "") |
|
342 | + { |
|
343 | + trigger_error("kses4::AddHTML() tried to add an empty/NULL tag", E_USER_WARNING); |
|
344 | + return false; |
|
345 | + } |
|
346 | + |
|
347 | + if(!is_array($attribs)) |
|
348 | + { |
|
349 | + trigger_error("kses4::AddHTML() requires an array (even an empty one) of attributes for '$tag'", E_USER_WARNING); |
|
350 | + return false; |
|
351 | + } |
|
352 | + |
|
353 | + $new_attribs = array(); |
|
354 | + if(is_array($attribs) && count($attribs) > 0) |
|
355 | + { |
|
356 | + foreach($attribs as $idx1 => $val1) |
|
357 | + { |
|
358 | + $new_idx1 = strtolower($idx1); |
|
359 | + $new_val1 = $attribs[$idx1]; |
|
360 | + |
|
361 | + if(is_array($new_val1) && count($new_val1) > 0) |
|
362 | + { |
|
363 | + $tmp_val = array(); |
|
364 | + foreach($new_val1 as $idx2 => $val2) |
|
365 | + { |
|
366 | + $new_idx2 = strtolower($idx2); |
|
367 | + $tmp_val[$new_idx2] = $val2; |
|
368 | + } |
|
369 | + $new_val1 = $tmp_val; |
|
370 | + } |
|
371 | + |
|
372 | + $new_attribs[$new_idx1] = $new_val1; |
|
373 | + } |
|
374 | + } |
|
375 | + |
|
376 | + $this->allowed_html[$tag] = $new_attribs; |
|
377 | + return true; |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * Removes a single protocol from $this->allowed_protocols. |
|
382 | + * |
|
383 | + * This method accepts a string argument and removes it from |
|
384 | + * the list of allowed protocols to keep when performing |
|
385 | + * Parse(). |
|
386 | + * |
|
387 | + * @access public |
|
388 | + * @param string $protocol The name of the protocol to be removed. |
|
389 | + * @return bool Status of removing valid protocol. |
|
390 | + * @since PHP4 OOP 0.2.1 |
|
391 | + */ |
|
392 | + function RemoveProtocol($protocol = "") |
|
393 | + { |
|
394 | + if(!is_string($protocol)) |
|
395 | + { |
|
396 | + trigger_error("kses4::RemoveProtocol() requires a string.", E_USER_WARNING); |
|
397 | + return false; |
|
398 | + } |
|
399 | + |
|
400 | + // Remove any inadvertent ':' at the end of the protocol. |
|
401 | + if(substr($protocol, strlen($protocol) - 1, 1) == ":") |
|
402 | + { |
|
403 | + $protocol = substr($protocol, 0, strlen($protocol) - 1); |
|
404 | + } |
|
405 | + |
|
406 | + $protocol = strtolower(trim($protocol)); |
|
407 | + if($protocol == "") |
|
408 | + { |
|
409 | + trigger_error("kses4::RemoveProtocol() tried to remove an empty/NULL protocol.", E_USER_WARNING); |
|
410 | + return false; |
|
411 | + } |
|
412 | + |
|
413 | + // Ensures that the protocol exists before removing it. |
|
414 | + if(in_array($protocol, $this->allowed_protocols)) |
|
415 | + { |
|
416 | + $this->allowed_protocols = array_diff($this->allowed_protocols, array($protocol)); |
|
417 | + sort($this->allowed_protocols); |
|
418 | + } |
|
419 | + |
|
420 | + return true; |
|
421 | + } |
|
422 | + |
|
423 | + /** |
|
424 | + * Allows for single/batch removal of protocols |
|
425 | + * |
|
426 | + * This method accepts one argument that can be either a string |
|
427 | + * or an array of strings. Invalid data will be ignored. |
|
428 | + * |
|
429 | + * The argument will be processed, and each string will be removed |
|
430 | + * via RemoveProtocol(). |
|
431 | + * |
|
432 | + * @access public |
|
433 | + * @param mixed , A string or array of protocols that will be removed from the internal list of allowed protocols. |
|
434 | + * @return bool Status of removing valid protocols. |
|
435 | + * @see RemoveProtocol() |
|
436 | + * @since PHP5 OOP 0.2.1 |
|
437 | + */ |
|
438 | + function RemoveProtocols() |
|
439 | + { |
|
440 | + $c_args = func_num_args(); |
|
441 | + if($c_args != 1) |
|
442 | + { |
|
443 | + return false; |
|
444 | + } |
|
445 | + |
|
446 | + $protocol_data = func_get_arg(0); |
|
447 | + |
|
448 | + if(is_array($protocol_data) && count($protocol_data) > 0) |
|
449 | + { |
|
450 | + foreach($protocol_data as $protocol) |
|
451 | + { |
|
452 | + $this->RemoveProtocol($protocol); |
|
453 | + } |
|
454 | + } |
|
455 | + elseif(is_string($protocol_data)) |
|
456 | + { |
|
457 | + $this->RemoveProtocol($protocol_data); |
|
458 | + return true; |
|
459 | + } |
|
460 | + else |
|
461 | + { |
|
462 | + trigger_error("kses4::RemoveProtocols() did not receive a string or an array.", E_USER_WARNING); |
|
463 | + return false; |
|
464 | + } |
|
465 | + } |
|
466 | + |
|
467 | + /** |
|
468 | + * This method removes any NULL or characters in $string. |
|
469 | + * |
|
470 | + * @access private |
|
471 | + * @param string $string |
|
472 | + * @return string String without any NULL/chr(173) |
|
473 | + * @since PHP4 OOP 0.0.1 |
|
474 | + */ |
|
475 | + function _no_null($string) |
|
476 | + { |
|
477 | + $string = preg_replace('/\0+/', '', $string); |
|
478 | + $string = preg_replace('/(\\\\0)+/', '', $string); |
|
479 | + return $string; |
|
480 | + } |
|
481 | + |
|
482 | + /** |
|
483 | + * This function removes the HTML JavaScript entities found in early versions of |
|
484 | + * Netscape 4. |
|
485 | + * |
|
486 | + * @access private |
|
487 | + * @param string $string |
|
488 | + * @return string String without any NULL/chr(173) |
|
489 | + * @since PHP4 OOP 0.0.1 |
|
490 | + */ |
|
491 | + function _js_entities($string) |
|
492 | + { |
|
493 | + return preg_replace('%&\s*\{[^}]*(\}\s*;?|$)%', '', $string); |
|
494 | + } |
|
495 | + |
|
496 | + /** |
|
497 | + * Normalizes HTML entities |
|
498 | + * |
|
499 | + * This function normalizes HTML entities. It will convert "AT&T" to the correct |
|
500 | + * "AT&T", ":" to ":", "&#XYZZY;" to "&#XYZZY;" and so on. |
|
501 | + * |
|
502 | + * @access private |
|
503 | + * @param string $string |
|
504 | + * @return string String with normalized entities |
|
505 | + * @since PHP4 OOP 0.0.1 |
|
506 | + */ |
|
507 | + function _normalize_entities($string) |
|
508 | + { |
|
509 | + # Disarm all entities by converting & to & |
|
510 | + $string = str_replace('&', '&', $string); |
|
511 | + |
|
512 | + # Change back the allowed entities in our entity white list |
|
513 | + |
|
514 | + $string = preg_replace('/&([A-Za-z][A-Za-z0-9]{0,19});/', '&\\1;', $string); |
|
515 | + $string = preg_replace('/&#0*([0-9]{1,5});/e', '\$this->_normalize_entities2("\\1")', $string); |
|
516 | + $string = preg_replace('/&#([Xx])0*(([0-9A-Fa-f]{2}){1,2});/', '&#\\1\\2;', $string); |
|
517 | + |
|
518 | + return $string; |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * Helper method used by normalizeEntites() |
|
523 | + * |
|
524 | + * This method helps normalizeEntities() to only accept 16 bit values |
|
525 | + * and nothing more for &#number; entities. |
|
526 | + * |
|
527 | + * This method helps normalize_entities() during a preg_replace() |
|
528 | + * where a &#(0)*XXXXX; occurs. The '(0)*XXXXXX' value is converted to |
|
529 | + * a number and the result is returned as a numeric entity if the number |
|
530 | + * is less than 65536. Otherwise, the value is returned 'as is'. |
|
531 | + * |
|
532 | + * @access private |
|
533 | + * @param string $i |
|
534 | + * @return string Normalized numeric entity |
|
535 | + * @see _normalize_entities() |
|
536 | + * @since PHP4 OOP 0.0.1 |
|
537 | + */ |
|
538 | + function _normalize_entities2($i) |
|
539 | + { |
|
540 | + return (($i > 65535) ? "&#$i;" : "&#$i;"); |
|
541 | + } |
|
542 | + |
|
543 | + /** |
|
544 | + * Allows for additional user defined modifications to text. |
|
545 | + * |
|
546 | + * @deprecated use filterKsesTextHook() |
|
547 | + * @param string $string |
|
548 | + * @see filterKsesTextHook() |
|
549 | + * @return string |
|
550 | + * @since PHP4 OOP 0.0.1 |
|
551 | + */ |
|
552 | + function _hook($string) |
|
553 | + { |
|
554 | + return $this->filterKsesTextHook($string); |
|
555 | + } |
|
556 | + |
|
557 | + /** |
|
558 | + * Allows for additional user defined modifications to text. |
|
559 | + * |
|
560 | + * This method allows for additional modifications to be performed on |
|
561 | + * a string that's being run through Parse(). Currently, it returns the |
|
562 | + * input string 'as is'. |
|
563 | + * |
|
564 | + * This method is provided for users to extend the kses class for their own |
|
565 | + * requirements. |
|
566 | + * |
|
567 | + * @access public |
|
568 | + * @param string $string String to perfrom additional modifications on. |
|
569 | + * @return string User modified string. |
|
570 | + * @see Parse() |
|
571 | + * @since PHP5 OOP 1.0.0 |
|
572 | + */ |
|
573 | + function filterKsesTextHook($string) |
|
574 | + { |
|
575 | + return $string; |
|
576 | + } |
|
577 | + |
|
578 | + /** |
|
579 | + * This method goes through an array, and changes the keys to all lower case. |
|
580 | + * |
|
581 | + * @access private |
|
582 | + * @param array $in_array Associative array |
|
583 | + * @return array Modified array |
|
584 | + * @since PHP4 OOP 0.0.1 |
|
585 | + */ |
|
586 | + function _array_lc($inarray) |
|
587 | + { |
|
588 | + $outarray = array(); |
|
589 | + |
|
590 | + if(is_array($inarray) && count($inarray) > 0) |
|
591 | + { |
|
592 | + foreach ($inarray as $inkey => $inval) |
|
593 | + { |
|
594 | + $outkey = strtolower($inkey); |
|
595 | + $outarray[$outkey] = array(); |
|
596 | + |
|
597 | + if(is_array($inval) && count($inval) > 0) |
|
598 | + { |
|
599 | + foreach ($inval as $inkey2 => $inval2) |
|
600 | + { |
|
601 | + $outkey2 = strtolower($inkey2); |
|
602 | + $outarray[$outkey][$outkey2] = $inval2; |
|
603 | + } |
|
604 | + } |
|
605 | + } |
|
606 | + } |
|
607 | + |
|
608 | + return $outarray; |
|
609 | + } |
|
610 | + |
|
611 | + /** |
|
612 | + * This method searched for HTML tags, no matter how malformed. It also |
|
613 | + * matches stray ">" characters. |
|
614 | + * |
|
615 | + * @access private |
|
616 | + * @param string $string |
|
617 | + * @return string HTML tags |
|
618 | + * @since PHP4 OOP 0.0.1 |
|
619 | + */ |
|
620 | + function _split($string) |
|
621 | + { |
|
622 | + return preg_replace( |
|
623 | + '%(<'. # EITHER: < |
|
624 | + '[^>]*'. # things that aren't > |
|
625 | + '(>|$)'. # > or end of string |
|
626 | + '|>)%e', # OR: just a > |
|
627 | + "\$this->_split2('\\1')", |
|
628 | + $string); |
|
629 | + } |
|
630 | + |
|
631 | + /** |
|
632 | + * This method strips out disallowed and/or mangled (X)HTML tags along with assigned attributes. |
|
633 | + * |
|
634 | + * This method does a lot of work. It rejects some very malformed things |
|
635 | + * like <:::>. It returns an empty string if the element isn't allowed (look |
|
636 | + * ma, no strip_tags()!). Otherwise it splits the tag into an element and an |
|
637 | + * allowed attribute list. |
|
638 | + * |
|
639 | + * @access private |
|
640 | + * @param string $string |
|
641 | + * @return string Modified string minus disallowed/mangled (X)HTML and attributes |
|
642 | + * @since PHP4 OOP 0.0.1 |
|
643 | + */ |
|
644 | + function _split2($string) |
|
645 | + { |
|
646 | + $string = $this->_stripslashes($string); |
|
647 | + |
|
648 | + if (substr($string, 0, 1) != '<') |
|
649 | + { |
|
650 | + # It matched a ">" character |
|
651 | + return '>'; |
|
652 | + } |
|
653 | + |
|
654 | + if (!preg_match('%^<\s*(/\s*)?([a-zA-Z0-9]+)([^>]*)>?$%', $string, $matches)) |
|
655 | + { |
|
656 | + # It's seriously malformed |
|
657 | + return ''; |
|
658 | + } |
|
659 | + |
|
660 | + $slash = trim($matches[1]); |
|
661 | + $elem = $matches[2]; |
|
662 | + $attrlist = $matches[3]; |
|
663 | + |
|
664 | + if ( |
|
665 | + !isset($this->allowed_html[strtolower($elem)]) || |
|
666 | + !is_array($this->allowed_html[strtolower($elem)]) |
|
667 | + ) |
|
668 | + { |
|
669 | + # They are using a not allowed HTML element |
|
670 | + return ''; |
|
671 | + } |
|
672 | + |
|
673 | + if ($slash != '') |
|
674 | + { |
|
675 | + return "<$slash$elem>"; |
|
676 | + } |
|
677 | + # No attributes are allowed for closing elements |
|
678 | + |
|
679 | + return $this->_attr("$slash$elem", $attrlist); |
|
680 | + } |
|
681 | + |
|
682 | + /** |
|
683 | + * This method strips out disallowed attributes for (X)HTML tags. |
|
684 | + * |
|
685 | + * This method removes all attributes if none are allowed for this element. |
|
686 | + * If some are allowed it calls $this->_hair() to split them further, and then it |
|
687 | + * builds up new HTML code from the data that $this->_hair() returns. It also |
|
688 | + * removes "<" and ">" characters, if there are any left. One more thing it |
|
689 | + * does is to check if the tag has a closing XHTML slash, and if it does, |
|
690 | + * it puts one in the returned code as well. |
|
691 | + * |
|
692 | + * @access private |
|
693 | + * @param string $element (X)HTML tag to check |
|
694 | + * @param string $attr Text containing attributes to check for validity. |
|
695 | + * @return string Resulting valid (X)HTML or '' |
|
696 | + * @see _hair() |
|
697 | + * @since PHP4 OOP 0.0.1 |
|
698 | + */ |
|
699 | + function _attr($element, $attr) |
|
700 | + { |
|
701 | + # Is there a closing XHTML slash at the end of the attributes? |
|
702 | + $xhtml_slash = ''; |
|
703 | + if (preg_match('%\s/\s*$%', $attr)) |
|
704 | + { |
|
705 | + $xhtml_slash = ' /'; |
|
706 | + } |
|
707 | + |
|
708 | + # Are any attributes allowed at all for this element? |
|
709 | + if ( |
|
710 | + !isset($this->allowed_html[strtolower($element)]) || |
|
711 | + count($this->allowed_html[strtolower($element)]) == 0 |
|
712 | + ) |
|
713 | + { |
|
714 | + return "<$element$xhtml_slash>"; |
|
715 | + } |
|
716 | + |
|
717 | + # Split it |
|
718 | + $attrarr = $this->_hair($attr); |
|
719 | + |
|
720 | + # Go through $attrarr, and save the allowed attributes for this element |
|
721 | + # in $attr2 |
|
722 | + $attr2 = ''; |
|
723 | + if(is_array($attrarr) && count($attrarr) > 0) |
|
724 | + { |
|
725 | + foreach ($attrarr as $arreach) |
|
726 | + { |
|
727 | + if(!isset($this->allowed_html[strtolower($element)][strtolower($arreach['name'])])) |
|
728 | + { |
|
729 | + continue; |
|
730 | + } |
|
731 | + |
|
732 | + $current = $this->allowed_html[strtolower($element)][strtolower($arreach['name'])]; |
|
733 | + if ($current == '') |
|
734 | + { |
|
735 | + # the attribute is not allowed |
|
736 | + continue; |
|
737 | + } |
|
738 | + |
|
739 | + if (!is_array($current)) |
|
740 | + { |
|
741 | + # there are no checks |
|
742 | + $attr2 .= ' '.$arreach['whole']; |
|
743 | + } |
|
744 | + else |
|
745 | + { |
|
746 | + # there are some checks |
|
747 | + $ok = true; |
|
748 | + if(is_array($current) && count($current) > 0) |
|
749 | + { |
|
750 | + foreach ($current as $currkey => $currval) |
|
751 | + { |
|
752 | + if (!$this->_check_attr_val($arreach['value'], $arreach['vless'], $currkey, $currval)) |
|
753 | + { |
|
754 | + $ok = false; |
|
755 | + break; |
|
756 | + } |
|
757 | + } |
|
758 | + |
|
759 | + if ($ok) |
|
760 | + { |
|
761 | + # it passed them |
|
762 | + $attr2 .= ' '.$arreach['whole']; |
|
763 | + } |
|
764 | + } |
|
765 | + } |
|
766 | + } |
|
767 | + } |
|
768 | + |
|
769 | + # Remove any "<" or ">" characters |
|
770 | + $attr2 = preg_replace('/[<>]/', '', $attr2); |
|
771 | + return "<$element$attr2$xhtml_slash>"; |
|
772 | + } |
|
773 | + |
|
774 | + /** |
|
775 | + * This method combs through an attribute list string and returns an associative array of attributes and values. |
|
776 | + * |
|
777 | + * This method does a lot of work. It parses an attribute list into an array |
|
778 | + * with attribute data, and tries to do the right thing even if it gets weird |
|
779 | + * input. It will add quotes around attribute values that don't have any quotes |
|
780 | + * or apostrophes around them, to make it easier to produce HTML code that will |
|
781 | + * conform to W3C's HTML specification. It will also remove bad URL protocols |
|
782 | + * from attribute values. |
|
783 | + * |
|
784 | + * @access private |
|
785 | + * @param string $attr Text containing tag attributes for parsing |
|
786 | + * @return array Associative array containing data on attribute and value |
|
787 | + * @since PHP4 OOP 0.0.1 |
|
788 | + */ |
|
789 | + function _hair($attr) |
|
790 | + { |
|
791 | + $attrarr = array(); |
|
792 | + $mode = 0; |
|
793 | + $attrname = ''; |
|
794 | + |
|
795 | + # Loop through the whole attribute list |
|
796 | + |
|
797 | + while (strlen($attr) != 0) |
|
798 | + { |
|
799 | + # Was the last operation successful? |
|
800 | + $working = 0; |
|
801 | + |
|
802 | + switch ($mode) |
|
803 | + { |
|
804 | + case 0: # attribute name, href for instance |
|
805 | + if (preg_match('/^([-a-zA-Z]+)/', $attr, $match)) |
|
806 | + { |
|
807 | + $attrname = $match[1]; |
|
808 | + $working = $mode = 1; |
|
809 | + $attr = preg_replace('/^[-a-zA-Z]+/', '', $attr); |
|
810 | + } |
|
811 | + break; |
|
812 | + case 1: # equals sign or valueless ("selected") |
|
813 | + if (preg_match('/^\s*=\s*/', $attr)) # equals sign |
|
814 | + { |
|
815 | + $working = 1; |
|
816 | + $mode = 2; |
|
817 | + $attr = preg_replace('/^\s*=\s*/', '', $attr); |
|
818 | + break; |
|
819 | + } |
|
820 | + if (preg_match('/^\s+/', $attr)) # valueless |
|
821 | + { |
|
822 | + $working = 1; |
|
823 | + $mode = 0; |
|
824 | + $attrarr[] = array( |
|
825 | + 'name' => $attrname, |
|
826 | + 'value' => '', |
|
827 | + 'whole' => $attrname, |
|
828 | + 'vless' => 'y' |
|
829 | + ); |
|
830 | + $attr = preg_replace('/^\s+/', '', $attr); |
|
831 | + } |
|
832 | + break; |
|
833 | + case 2: # attribute value, a URL after href= for instance |
|
834 | + if (preg_match('/^"([^"]*)"(\s+|$)/', $attr, $match)) # "value" |
|
835 | + { |
|
836 | + $thisval = $this->_bad_protocol($match[1]); |
|
837 | + $attrarr[] = array( |
|
838 | + 'name' => $attrname, |
|
839 | + 'value' => $thisval, |
|
840 | + 'whole' => "$attrname=\"$thisval\"", |
|
841 | + 'vless' => 'n' |
|
842 | + ); |
|
843 | + $working = 1; |
|
844 | + $mode = 0; |
|
845 | + $attr = preg_replace('/^"[^"]*"(\s+|$)/', '', $attr); |
|
846 | + break; |
|
847 | + } |
|
848 | + if (preg_match("/^'([^']*)'(\s+|$)/", $attr, $match)) # 'value' |
|
849 | + { |
|
850 | + $thisval = $this->_bad_protocol($match[1]); |
|
851 | + $attrarr[] = array( |
|
852 | + 'name' => $attrname, |
|
853 | + 'value' => $thisval, |
|
854 | + 'whole' => "$attrname='$thisval'", |
|
855 | + 'vless' => 'n' |
|
856 | + ); |
|
857 | + $working = 1; |
|
858 | + $mode = 0; |
|
859 | + $attr = preg_replace("/^'[^']*'(\s+|$)/", '', $attr); |
|
860 | + break; |
|
861 | + } |
|
862 | + if (preg_match("%^([^\s\"']+)(\s+|$)%", $attr, $match)) # value |
|
863 | + { |
|
864 | + $thisval = $this->_bad_protocol($match[1]); |
|
865 | + $attrarr[] = array( |
|
866 | + 'name' => $attrname, |
|
867 | + 'value' => $thisval, |
|
868 | + 'whole' => "$attrname=\"$thisval\"", |
|
869 | + 'vless' => 'n' |
|
870 | + ); |
|
871 | + # We add quotes to conform to W3C's HTML spec. |
|
872 | + $working = 1; |
|
873 | + $mode = 0; |
|
874 | + $attr = preg_replace("%^[^\s\"']+(\s+|$)%", '', $attr); |
|
875 | + } |
|
876 | + break; |
|
877 | + } |
|
878 | + |
|
879 | + if ($working == 0) # not well formed, remove and try again |
|
880 | + { |
|
881 | + $attr = $this->_html_error($attr); |
|
882 | + $mode = 0; |
|
883 | + } |
|
884 | + } |
|
885 | + |
|
886 | + # special case, for when the attribute list ends with a valueless |
|
887 | + # attribute like "selected" |
|
888 | + if ($mode == 1) |
|
889 | + { |
|
890 | + $attrarr[] = array( |
|
891 | + 'name' => $attrname, |
|
892 | + 'value' => '', |
|
893 | + 'whole' => $attrname, |
|
894 | + 'vless' => 'y' |
|
895 | + ); |
|
896 | + } |
|
897 | + |
|
898 | + return $attrarr; |
|
899 | + } |
|
900 | + |
|
901 | + /** |
|
902 | + * This method removes disallowed protocols. |
|
903 | + * |
|
904 | + * This method removes all non-allowed protocols from the beginning of |
|
905 | + * $string. It ignores whitespace and the case of the letters, and it does |
|
906 | + * understand HTML entities. It does its work in a while loop, so it won't be |
|
907 | + * fooled by a string like "javascript:javascript:alert(57)". |
|
908 | + * |
|
909 | + * @access private |
|
910 | + * @param string $string String to check for protocols |
|
911 | + * @return string String with removed protocols |
|
912 | + * @since PHP4 OOP 0.0.1 |
|
913 | + */ |
|
914 | + function _bad_protocol($string) |
|
915 | + { |
|
916 | + $string = $this->_no_null($string); |
|
917 | + $string = preg_replace('/\xad+/', '', $string); # deals with Opera "feature" |
|
918 | + $string2 = $string.'a'; |
|
919 | + |
|
920 | + while ($string != $string2) |
|
921 | + { |
|
922 | + $string2 = $string; |
|
923 | + $string = $this->_bad_protocol_once($string); |
|
924 | + } # while |
|
925 | + |
|
926 | + return $string; |
|
927 | + } |
|
928 | + |
|
929 | + /** |
|
930 | + * Helper method used by _bad_protocol() |
|
931 | + * |
|
932 | + * This function searches for URL protocols at the beginning of $string, while |
|
933 | + * handling whitespace and HTML entities. |
|
934 | + * Function updated to fix security vulnerability (see http://projects.dokeos.com/index.php?do=details&task_id=2312) |
|
935 | + * |
|
936 | + * @access private |
|
937 | + * @param string $string String to check for protocols |
|
938 | + * @return string String with removed protocols |
|
939 | + * @see _bad_protocol() |
|
940 | + * @since PHP4 OOP 0.0.1 |
|
941 | + */ |
|
942 | + function _bad_protocol_once($string) |
|
943 | + { |
|
944 | + $string2 = preg_split('/:|:|:/i', $string, 2); |
|
945 | + if(isset($string2[1]) && !preg_match('%/\?%',$string2[0])) |
|
946 | + { |
|
947 | + $string = $this->_bad_protocol_once2($string2[0]).trim($string2[1]); |
|
948 | + } |
|
949 | + return $string; |
|
950 | + } |
|
951 | + /** |
|
952 | + * Helper method used by _bad_protocol_once() regex |
|
953 | + * |
|
954 | + * This function processes URL protocols, checks to see if they're in the white- |
|
955 | + * list or not, and returns different data depending on the answer. |
|
956 | + * |
|
957 | + * @access private |
|
958 | + * @param string $string String to check for protocols |
|
959 | + * @return string String with removed protocols |
|
960 | + * @see _bad_protocol() |
|
961 | + * @see _bad_protocol_once() |
|
962 | + * @since PHP4 OOP 0.0.1 |
|
963 | + */ |
|
964 | + function _bad_protocol_once2($string) |
|
965 | + { |
|
966 | + $string = $this->_decode_entities($string); |
|
967 | + $string = preg_replace('/\s/', '', $string); |
|
968 | + $string = $this->_no_null($string); |
|
969 | + $string = preg_replace('/\xad+/', '', $string); # deals with Opera "feature" |
|
970 | + $string = strtolower($string); |
|
971 | + |
|
972 | + $allowed = false; |
|
973 | + if(is_array($this->allowed_protocols) && count($this->allowed_protocols) > 0) |
|
974 | + { |
|
975 | + foreach ($this->allowed_protocols as $one_protocol) |
|
976 | + { |
|
977 | + if (strtolower($one_protocol) == $string) |
|
978 | + { |
|
979 | + $allowed = true; |
|
980 | + break; |
|
981 | + } |
|
982 | + } |
|
983 | + } |
|
984 | + |
|
985 | + if ($allowed) |
|
986 | + { |
|
987 | + return "$string:"; |
|
988 | + } |
|
989 | + else |
|
990 | + { |
|
991 | + return ''; |
|
992 | + } |
|
993 | + } |
|
994 | + |
|
995 | + /** |
|
996 | + * This function performs different checks for attribute values. |
|
997 | + * |
|
998 | + * The currently implemented checks are "maxlen", "minlen", "maxval", |
|
999 | + * "minval" and "valueless" with even more checks to come soon. |
|
1000 | + * |
|
1001 | + * @access private |
|
1002 | + * @param string $value The value of the attribute to be checked. |
|
1003 | + * @param string $vless Indicates whether the the value is supposed to be valueless |
|
1004 | + * @param string $checkname The check to be performed |
|
1005 | + * @param string $checkvalue The value that is to be checked against |
|
1006 | + * @return bool Indicates whether the check passed or not |
|
1007 | + * @since PHP4 OOP 0.0.1 |
|
1008 | + */ |
|
1009 | + function _check_attr_val($value, $vless, $checkname, $checkvalue) |
|
1010 | + { |
|
1011 | + $ok = true; |
|
1012 | + |
|
1013 | + switch (strtolower($checkname)) |
|
1014 | + { |
|
1015 | + /** |
|
1016 | + * The maxlen check makes sure that the attribute value has a length not |
|
1017 | + * greater than the given value. This can be used to avoid Buffer Overflows |
|
1018 | + * in WWW clients and various Internet servers. |
|
1019 | + */ |
|
1020 | + case 'maxlen': |
|
1021 | + if (strlen($value) > $checkvalue) |
|
1022 | + { |
|
1023 | + $ok = false; |
|
1024 | + } |
|
1025 | + break; |
|
1026 | + |
|
1027 | + /** |
|
1028 | + * The minlen check makes sure that the attribute value has a length not |
|
1029 | + * smaller than the given value. |
|
1030 | + */ |
|
1031 | + case 'minlen': |
|
1032 | + if (strlen($value) < $checkvalue) |
|
1033 | + { |
|
1034 | + $ok = false; |
|
1035 | + } |
|
1036 | + break; |
|
1037 | + |
|
1038 | + /** |
|
1039 | + * The maxval check does two things: it checks that the attribute value is |
|
1040 | + * an integer from 0 and up, without an excessive amount of zeroes or |
|
1041 | + * whitespace (to avoid Buffer Overflows). It also checks that the attribute |
|
1042 | + * value is not greater than the given value. |
|
1043 | + * This check can be used to avoid Denial of Service attacks. |
|
1044 | + */ |
|
1045 | + case 'maxval': |
|
1046 | + if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value)) |
|
1047 | + { |
|
1048 | + $ok = false; |
|
1049 | + } |
|
1050 | + if ($value > $checkvalue) |
|
1051 | + { |
|
1052 | + $ok = false; |
|
1053 | + } |
|
1054 | + break; |
|
1055 | + |
|
1056 | + /** |
|
1057 | + * The minval check checks that the attribute value is a positive integer, |
|
1058 | + * and that it is not smaller than the given value. |
|
1059 | + */ |
|
1060 | + case 'minval': |
|
1061 | + if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value)) |
|
1062 | + { |
|
1063 | + $ok = false; |
|
1064 | + } |
|
1065 | + if ($value < $checkvalue) |
|
1066 | + { |
|
1067 | + $ok = false; |
|
1068 | + } |
|
1069 | + break; |
|
1070 | + |
|
1071 | + /** |
|
1072 | + * The valueless check checks if the attribute has a value |
|
1073 | + * (like <a href="blah">) or not (<option selected>). If the given value |
|
1074 | + * is a "y" or a "Y", the attribute must not have a value. |
|
1075 | + * If the given value is an "n" or an "N", the attribute must have one. |
|
1076 | + */ |
|
1077 | + case 'valueless': |
|
1078 | + if (strtolower($checkvalue) != $vless) |
|
1079 | + { |
|
1080 | + $ok = false; |
|
1081 | + } |
|
1082 | + break; |
|
1083 | + |
|
1084 | + } |
|
1085 | + |
|
1086 | + return $ok; |
|
1087 | + } |
|
1088 | + |
|
1089 | + /** |
|
1090 | + * Changes \" to " |
|
1091 | + * |
|
1092 | + * This function changes the character sequence \" to just " |
|
1093 | + * It leaves all other slashes alone. It's really weird, but the quoting from |
|
1094 | + * preg_replace(//e) seems to require this. |
|
1095 | + * |
|
1096 | + * @access private |
|
1097 | + * @param string $string The string to be stripped. |
|
1098 | + * @return string string stripped of \" |
|
1099 | + * @since PHP4 OOP 0.0.1 |
|
1100 | + */ |
|
1101 | + function _stripslashes($string) |
|
1102 | + { |
|
1103 | + return preg_replace('%\\\\"%', '"', $string); |
|
1104 | + } |
|
1105 | + |
|
1106 | + /** |
|
1107 | + * helper method for _hair() |
|
1108 | + * |
|
1109 | + * This function deals with parsing errors in _hair(). The general plan is |
|
1110 | + * to remove everything to and including some whitespace, but it deals with |
|
1111 | + * quotes and apostrophes as well. |
|
1112 | + * |
|
1113 | + * @access private |
|
1114 | + * @param string $string The string to be stripped. |
|
1115 | + * @return string string stripped of whitespace |
|
1116 | + * @see _hair() |
|
1117 | + * @since PHP4 OOP 0.0.1 |
|
1118 | + */ |
|
1119 | + function _html_error($string) |
|
1120 | + { |
|
1121 | + return preg_replace('/^("[^"]*("|$)|\'[^\']*(\'|$)|\S)*\s*/', '', $string); |
|
1122 | + } |
|
1123 | + |
|
1124 | + /** |
|
1125 | + * Decodes numeric HTML entities |
|
1126 | + * |
|
1127 | + * This method decodes numeric HTML entities (A and A). It doesn't |
|
1128 | + * do anything with other entities like ä, but we don't need them in the |
|
1129 | + * URL protocol white listing system anyway. |
|
1130 | + * |
|
1131 | + * @access private |
|
1132 | + * @param string $value The entitiy to be decoded. |
|
1133 | + * @return string Decoded entity |
|
1134 | + * @since PHP4 OOP 0.0.1 |
|
1135 | + */ |
|
1136 | + function _decode_entities($string) |
|
1137 | + { |
|
1138 | + $string = preg_replace('/&#([0-9]+);/e', 'chr("\\1")', $string); |
|
1139 | + $string = preg_replace('/&#[Xx]([0-9A-Fa-f]+);/e', 'chr(hexdec("\\1"))', $string); |
|
1140 | + return $string; |
|
1141 | + } |
|
1142 | + |
|
1143 | + /** |
|
1144 | + * Returns PHP4 OOP version # of kses. |
|
1145 | + * |
|
1146 | + * Since this class has been refactored and documented and proven to work, |
|
1147 | + * I'm syncing the version number to procedural kses. |
|
1148 | + * |
|
1149 | + * @access public |
|
1150 | + * @return string Version number |
|
1151 | + * @since PHP4 OOP 0.0.1 |
|
1152 | + */ |
|
1153 | + function _version() |
|
1154 | + { |
|
1155 | + return 'PHP4 0.2.2 (OOP fork of procedural kses 0.2.2)'; |
|
1156 | + } |
|
1157 | + } |
|
1158 | + |
|
1159 | + |
|
1160 | + |
|
1161 | + } |
|
1162 | 1162 | ?> |
1163 | 1163 | \ No newline at end of file |
@@ -42,141 +42,141 @@ |
||
42 | 42 | */ |
43 | 43 | function xajaxCompressJavascript($sJS) |
44 | 44 | { |
45 | - //remove windows cariage returns |
|
46 | - $sJS = str_replace("\r","",$sJS); |
|
47 | - |
|
48 | - //array to store replaced literal strings |
|
49 | - $literal_strings = array(); |
|
50 | - |
|
51 | - //explode the string into lines |
|
52 | - $lines = explode("\n",$sJS); |
|
53 | - //loop through all the lines, building a new string at the same time as removing literal strings |
|
54 | - $clean = ""; |
|
55 | - $inComment = false; |
|
56 | - $literal = ""; |
|
57 | - $inQuote = false; |
|
58 | - $escaped = false; |
|
59 | - $quoteChar = ""; |
|
60 | - |
|
61 | - for($i=0;$i<count($lines);$i++) |
|
62 | - { |
|
63 | - $line = $lines[$i]; |
|
64 | - $inNormalComment = false; |
|
65 | - |
|
66 | - //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string |
|
67 | - for($j=0;$j<strlen($line);$j++) |
|
68 | - { |
|
69 | - $c = substr($line,$j,1); |
|
70 | - $d = substr($line,$j,2); |
|
71 | - |
|
72 | - //look for start of quote |
|
73 | - if(!$inQuote && !$inComment) |
|
74 | - { |
|
75 | - //is this character a quote or a comment |
|
76 | - if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment) |
|
77 | - { |
|
78 | - $inQuote = true; |
|
79 | - $inComment = false; |
|
80 | - $escaped = false; |
|
81 | - $quoteChar = $c; |
|
82 | - $literal = $c; |
|
83 | - } |
|
84 | - else if($d=="/*" && !$inNormalComment) |
|
85 | - { |
|
86 | - $inQuote = false; |
|
87 | - $inComment = true; |
|
88 | - $escaped = false; |
|
89 | - $quoteChar = $d; |
|
90 | - $literal = $d; |
|
91 | - $j++; |
|
92 | - } |
|
93 | - else if($d=="//") //ignore string markers that are found inside comments |
|
94 | - { |
|
95 | - $inNormalComment = true; |
|
96 | - $clean .= $c; |
|
97 | - } |
|
98 | - else |
|
99 | - { |
|
100 | - $clean .= $c; |
|
101 | - } |
|
102 | - } |
|
103 | - else //allready in a string so find end quote |
|
104 | - { |
|
105 | - if($c == $quoteChar && !$escaped && !$inComment) |
|
106 | - { |
|
107 | - $inQuote = false; |
|
108 | - $literal .= $c; |
|
109 | - |
|
110 | - //subsitute in a marker for the string |
|
111 | - $clean .= "___" . count($literal_strings) . "___"; |
|
112 | - |
|
113 | - //push the string onto our array |
|
114 | - array_push($literal_strings,$literal); |
|
115 | - |
|
116 | - } |
|
117 | - else if($inComment && $d=="*/") |
|
118 | - { |
|
119 | - $inComment = false; |
|
120 | - $literal .= $d; |
|
121 | - |
|
122 | - //subsitute in a marker for the string |
|
123 | - $clean .= "___" . count($literal_strings) . "___"; |
|
124 | - |
|
125 | - //push the string onto our array |
|
126 | - array_push($literal_strings,$literal); |
|
127 | - |
|
128 | - $j++; |
|
129 | - } |
|
130 | - else if($c == "\\" && !$escaped) |
|
131 | - $escaped = true; |
|
132 | - else |
|
133 | - $escaped = false; |
|
134 | - |
|
135 | - $literal .= $c; |
|
136 | - } |
|
137 | - } |
|
138 | - if($inComment) $literal .= "\n"; |
|
139 | - $clean .= "\n"; |
|
140 | - } |
|
141 | - //explode the clean string into lines again |
|
142 | - $lines = explode("\n",$clean); |
|
143 | - |
|
144 | - //now process each line at a time |
|
145 | - for($i=0;$i<count($lines);$i++) |
|
146 | - { |
|
147 | - $line = $lines[$i]; |
|
148 | - |
|
149 | - //remove comments |
|
150 | - $line = preg_replace("/\/\/(.*)/","",$line); |
|
151 | - |
|
152 | - //strip leading and trailing whitespace |
|
153 | - $line = trim($line); |
|
154 | - |
|
155 | - //remove all whitespace with a single space |
|
156 | - $line = preg_replace("/\s+/"," ",$line); |
|
157 | - |
|
158 | - //remove any whitespace that occurs after/before an operator |
|
159 | - $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line); |
|
160 | - |
|
161 | - $lines[$i] = $line; |
|
162 | - } |
|
163 | - |
|
164 | - //implode the lines |
|
165 | - $sJS = implode("\n",$lines); |
|
166 | - |
|
167 | - //make sure there is a max of 1 \n after each line |
|
168 | - $sJS = preg_replace("/[\n]+/","\n",$sJS); |
|
169 | - |
|
170 | - //strip out line breaks that immediately follow a semi-colon |
|
171 | - $sJS = preg_replace("/;\n/",";",$sJS); |
|
172 | - |
|
173 | - //curly brackets aren't on their own |
|
174 | - $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS); |
|
175 | - |
|
176 | - //finally loop through and replace all the literal strings: |
|
177 | - for($i=0;$i<count($literal_strings);$i++) |
|
178 | - $sJS = str_replace("___".$i."___",$literal_strings[$i],$sJS); |
|
179 | - |
|
180 | - return $sJS; |
|
45 | + //remove windows cariage returns |
|
46 | + $sJS = str_replace("\r","",$sJS); |
|
47 | + |
|
48 | + //array to store replaced literal strings |
|
49 | + $literal_strings = array(); |
|
50 | + |
|
51 | + //explode the string into lines |
|
52 | + $lines = explode("\n",$sJS); |
|
53 | + //loop through all the lines, building a new string at the same time as removing literal strings |
|
54 | + $clean = ""; |
|
55 | + $inComment = false; |
|
56 | + $literal = ""; |
|
57 | + $inQuote = false; |
|
58 | + $escaped = false; |
|
59 | + $quoteChar = ""; |
|
60 | + |
|
61 | + for($i=0;$i<count($lines);$i++) |
|
62 | + { |
|
63 | + $line = $lines[$i]; |
|
64 | + $inNormalComment = false; |
|
65 | + |
|
66 | + //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string |
|
67 | + for($j=0;$j<strlen($line);$j++) |
|
68 | + { |
|
69 | + $c = substr($line,$j,1); |
|
70 | + $d = substr($line,$j,2); |
|
71 | + |
|
72 | + //look for start of quote |
|
73 | + if(!$inQuote && !$inComment) |
|
74 | + { |
|
75 | + //is this character a quote or a comment |
|
76 | + if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment) |
|
77 | + { |
|
78 | + $inQuote = true; |
|
79 | + $inComment = false; |
|
80 | + $escaped = false; |
|
81 | + $quoteChar = $c; |
|
82 | + $literal = $c; |
|
83 | + } |
|
84 | + else if($d=="/*" && !$inNormalComment) |
|
85 | + { |
|
86 | + $inQuote = false; |
|
87 | + $inComment = true; |
|
88 | + $escaped = false; |
|
89 | + $quoteChar = $d; |
|
90 | + $literal = $d; |
|
91 | + $j++; |
|
92 | + } |
|
93 | + else if($d=="//") //ignore string markers that are found inside comments |
|
94 | + { |
|
95 | + $inNormalComment = true; |
|
96 | + $clean .= $c; |
|
97 | + } |
|
98 | + else |
|
99 | + { |
|
100 | + $clean .= $c; |
|
101 | + } |
|
102 | + } |
|
103 | + else //allready in a string so find end quote |
|
104 | + { |
|
105 | + if($c == $quoteChar && !$escaped && !$inComment) |
|
106 | + { |
|
107 | + $inQuote = false; |
|
108 | + $literal .= $c; |
|
109 | + |
|
110 | + //subsitute in a marker for the string |
|
111 | + $clean .= "___" . count($literal_strings) . "___"; |
|
112 | + |
|
113 | + //push the string onto our array |
|
114 | + array_push($literal_strings,$literal); |
|
115 | + |
|
116 | + } |
|
117 | + else if($inComment && $d=="*/") |
|
118 | + { |
|
119 | + $inComment = false; |
|
120 | + $literal .= $d; |
|
121 | + |
|
122 | + //subsitute in a marker for the string |
|
123 | + $clean .= "___" . count($literal_strings) . "___"; |
|
124 | + |
|
125 | + //push the string onto our array |
|
126 | + array_push($literal_strings,$literal); |
|
127 | + |
|
128 | + $j++; |
|
129 | + } |
|
130 | + else if($c == "\\" && !$escaped) |
|
131 | + $escaped = true; |
|
132 | + else |
|
133 | + $escaped = false; |
|
134 | + |
|
135 | + $literal .= $c; |
|
136 | + } |
|
137 | + } |
|
138 | + if($inComment) $literal .= "\n"; |
|
139 | + $clean .= "\n"; |
|
140 | + } |
|
141 | + //explode the clean string into lines again |
|
142 | + $lines = explode("\n",$clean); |
|
143 | + |
|
144 | + //now process each line at a time |
|
145 | + for($i=0;$i<count($lines);$i++) |
|
146 | + { |
|
147 | + $line = $lines[$i]; |
|
148 | + |
|
149 | + //remove comments |
|
150 | + $line = preg_replace("/\/\/(.*)/","",$line); |
|
151 | + |
|
152 | + //strip leading and trailing whitespace |
|
153 | + $line = trim($line); |
|
154 | + |
|
155 | + //remove all whitespace with a single space |
|
156 | + $line = preg_replace("/\s+/"," ",$line); |
|
157 | + |
|
158 | + //remove any whitespace that occurs after/before an operator |
|
159 | + $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line); |
|
160 | + |
|
161 | + $lines[$i] = $line; |
|
162 | + } |
|
163 | + |
|
164 | + //implode the lines |
|
165 | + $sJS = implode("\n",$lines); |
|
166 | + |
|
167 | + //make sure there is a max of 1 \n after each line |
|
168 | + $sJS = preg_replace("/[\n]+/","\n",$sJS); |
|
169 | + |
|
170 | + //strip out line breaks that immediately follow a semi-colon |
|
171 | + $sJS = preg_replace("/;\n/",";",$sJS); |
|
172 | + |
|
173 | + //curly brackets aren't on their own |
|
174 | + $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS); |
|
175 | + |
|
176 | + //finally loop through and replace all the literal strings: |
|
177 | + for($i=0;$i<count($literal_strings);$i++) |
|
178 | + $sJS = str_replace("___".$i."___",$literal_strings[$i],$sJS); |
|
179 | + |
|
180 | + return $sJS; |
|
181 | 181 | } |
182 | 182 | ?> |
@@ -248,17 +248,17 @@ discard block |
||
248 | 248 | } |
249 | 249 | } |
250 | 250 | |
251 | - if (trim($home_top_temp) == '' && api_is_platform_admin()) { |
|
252 | - $home_top_temp = '<div class="welcome-mascot">' . get_lang('PortalHomepageDefaultIntroduction') . '</div>'; |
|
253 | - } else { |
|
254 | - $home_top_temp = '<div class="welcome-home-top-temp">' . $home_top_temp . '</div>'; |
|
255 | - } |
|
256 | - $open = str_replace('{rel_path}', api_get_path(REL_PATH), $home_top_temp); |
|
257 | - $html = api_to_system_encoding($open, api_detect_encoding(strip_tags($open))); |
|
258 | - } |
|
259 | - |
|
260 | - return $html; |
|
261 | - } |
|
251 | + if (trim($home_top_temp) == '' && api_is_platform_admin()) { |
|
252 | + $home_top_temp = '<div class="welcome-mascot">' . get_lang('PortalHomepageDefaultIntroduction') . '</div>'; |
|
253 | + } else { |
|
254 | + $home_top_temp = '<div class="welcome-home-top-temp">' . $home_top_temp . '</div>'; |
|
255 | + } |
|
256 | + $open = str_replace('{rel_path}', api_get_path(REL_PATH), $home_top_temp); |
|
257 | + $html = api_to_system_encoding($open, api_detect_encoding(strip_tags($open))); |
|
258 | + } |
|
259 | + |
|
260 | + return $html; |
|
261 | + } |
|
262 | 262 | |
263 | 263 | function return_notice() |
264 | 264 | { |
@@ -509,7 +509,7 @@ discard block |
||
509 | 509 | $thereIsSubCat = true; |
510 | 510 | } elseif (api_get_setting('show_empty_course_categories') == 'true') { |
511 | 511 | /* End changed code to eliminate the (0 courses) after empty categories. */ |
512 | - $htmlListCat .= '<li>'; |
|
512 | + $htmlListCat .= '<li>'; |
|
513 | 513 | $htmlListCat .= $catLine['name']; |
514 | 514 | $htmlListCat .= "</li>"; |
515 | 515 | $thereIsSubCat = true; |
@@ -639,11 +639,11 @@ discard block |
||
639 | 639 | } |
640 | 640 | |
641 | 641 | /** |
642 | - * retrieves all the courses that the user has already subscribed to |
|
643 | - * @author Patrick Cool <[email protected]>, Ghent University, Belgium |
|
644 | - * @param int $user_id: the id of the user |
|
645 | - * @return array an array containing all the information of the courses of the given user |
|
646 | - */ |
|
642 | + * retrieves all the courses that the user has already subscribed to |
|
643 | + * @author Patrick Cool <[email protected]>, Ghent University, Belgium |
|
644 | + * @param int $user_id: the id of the user |
|
645 | + * @return array an array containing all the information of the courses of the given user |
|
646 | + */ |
|
647 | 647 | public function get_courses_of_user($user_id) |
648 | 648 | { |
649 | 649 | $table_course = Database::get_main_table(TABLE_MAIN_COURSE); |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | |
79 | 79 | // getting all the students of the course |
80 | 80 | if (empty($session_id)) { |
81 | - // Registered students in a course outside session. |
|
81 | + // Registered students in a course outside session. |
|
82 | 82 | $a_students = CourseManager:: get_student_list_from_course_code( |
83 | 83 | api_get_course_id(), |
84 | 84 | false, |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | api_get_group_id() |
90 | 90 | ); |
91 | 91 | } else { |
92 | - // Registered students in session. |
|
92 | + // Registered students in session. |
|
93 | 93 | $a_students = CourseManager:: get_student_list_from_course_code( |
94 | 94 | api_get_course_id(), |
95 | 95 | true, |
@@ -111,7 +111,7 @@ |
||
111 | 111 | get_lang('HideColumn'), |
112 | 112 | array('align' => 'absmiddle', 'hspace' => '3px'), |
113 | 113 | ICON_SIZE_SMALL |
114 | - ) . "</div>' |
|
114 | + ) . "</div>' |
|
115 | 115 | ); |
116 | 116 | } |
117 | 117 | ); |
@@ -14,23 +14,23 @@ discard block |
||
14 | 14 | $is_allowedToTrack = $is_courseAdmin || $is_platformAdmin || $is_courseCoach || $is_sessionAdmin; |
15 | 15 | |
16 | 16 | if (!$is_allowedToTrack) { |
17 | - api_not_allowed(true); |
|
17 | + api_not_allowed(true); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | $export_to_xls = false; |
21 | 21 | if (isset($_GET['export'])) { |
22 | - $export_to_xls = true; |
|
22 | + $export_to_xls = true; |
|
23 | 23 | } |
24 | 24 | if (api_is_platform_admin() ) { |
25 | - $global = true; |
|
25 | + $global = true; |
|
26 | 26 | } else { |
27 | - $global = false; |
|
27 | + $global = false; |
|
28 | 28 | } |
29 | 29 | $global = true; |
30 | 30 | |
31 | 31 | $session_id = isset($_GET['session_id']) ? intval($_GET['session_id']) : null; |
32 | 32 | if (empty($session_id)) { |
33 | - $session_id = 1; |
|
33 | + $session_id = 1; |
|
34 | 34 | } |
35 | 35 | |
36 | 36 | $form = new FormValidator('search_simple','POST','','',null,false); |
@@ -39,10 +39,10 @@ discard block |
||
39 | 39 | $session_list = SessionManager::get_sessions_list(array(), array('name')); |
40 | 40 | $my_session_list = array(); |
41 | 41 | foreach($session_list as $sesion_item) { |
42 | - $my_session_list[$sesion_item['id']] = $sesion_item['name']; |
|
42 | + $my_session_list[$sesion_item['id']] = $sesion_item['name']; |
|
43 | 43 | } |
44 | 44 | if (count($session_list) == 0) { |
45 | - $my_session_list[0] = get_lang('None'); |
|
45 | + $my_session_list[0] = get_lang('None'); |
|
46 | 46 | } |
47 | 47 | $form->addElement('select', 'session_id', get_lang('Sessions'), $my_session_list); |
48 | 48 | $form->addButtonFilter(get_lang('Filter')); |
@@ -51,32 +51,32 @@ discard block |
||
51 | 51 | if (!empty($_REQUEST['session_id'])) $session_id = intval($_REQUEST['session_id']); else $session_id = 0; |
52 | 52 | |
53 | 53 | if (empty($session_id)) { |
54 | - $session_id = key($my_session_list); |
|
54 | + $session_id = key($my_session_list); |
|
55 | 55 | } |
56 | 56 | $form->setDefaults(array('session_id'=>$session_id)); |
57 | 57 | $course_list = SessionManager::get_course_list_by_session_id($session_id); |
58 | 58 | |
59 | 59 | if (!$export_to_xls) { |
60 | - Display :: display_header(get_lang("MySpace")); |
|
61 | - echo '<div class="actions">'; |
|
60 | + Display :: display_header(get_lang("MySpace")); |
|
61 | + echo '<div class="actions">'; |
|
62 | 62 | |
63 | - if ($global) { |
|
64 | - echo MySpace::getTopMenu(); |
|
65 | - } else { |
|
66 | - echo '<div style="float:left; clear:left"> |
|
63 | + if ($global) { |
|
64 | + echo MySpace::getTopMenu(); |
|
65 | + } else { |
|
66 | + echo '<div style="float:left; clear:left"> |
|
67 | 67 | <a href="courseLog.php?'.api_get_cidreq().'&studentlist=true">'.get_lang('StudentsTracking').'</a> | |
68 | 68 | <a href="courseLog.php?'.api_get_cidreq().'&studentlist=false">'.get_lang('CourseTracking').'</a> '; |
69 | - echo '</div>'; |
|
70 | - } |
|
71 | - echo '</div>'; |
|
69 | + echo '</div>'; |
|
70 | + } |
|
71 | + echo '</div>'; |
|
72 | 72 | |
73 | - if (api_is_platform_admin()) { |
|
74 | - echo MySpace::getAdminActions(); |
|
75 | - } |
|
73 | + if (api_is_platform_admin()) { |
|
74 | + echo MySpace::getAdminActions(); |
|
75 | + } |
|
76 | 76 | |
77 | - echo '<h2>'.get_lang('LPExerciseResultsBySession').'</h2>'; |
|
78 | - $form->display(); |
|
79 | - Display::display_normal_message(get_lang('StudentScoreAverageIsCalculatedBaseInAllLPsAndAllAttempts')); |
|
77 | + echo '<h2>'.get_lang('LPExerciseResultsBySession').'</h2>'; |
|
78 | + $form->display(); |
|
79 | + Display::display_normal_message(get_lang('StudentScoreAverageIsCalculatedBaseInAllLPsAndAllAttempts')); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | $users = SessionManager::get_users_by_session($session_id); |
@@ -86,15 +86,15 @@ discard block |
||
86 | 86 | $main_result = array(); |
87 | 87 | //Getting course list |
88 | 88 | foreach ($course_list as $current_course ) { |
89 | - $course_info = api_get_course_info($current_course['code']); |
|
90 | - $_course = $course_info; |
|
91 | - $attempt_result = array(); |
|
89 | + $course_info = api_get_course_info($current_course['code']); |
|
90 | + $_course = $course_info; |
|
91 | + $attempt_result = array(); |
|
92 | 92 | |
93 | - //Getting LP list |
|
94 | - $list = new LearnpathList('', $current_course['code'], $session_id); |
|
95 | - $lp_list = $list->get_flat_list(); |
|
93 | + //Getting LP list |
|
94 | + $list = new LearnpathList('', $current_course['code'], $session_id); |
|
95 | + $lp_list = $list->get_flat_list(); |
|
96 | 96 | |
97 | - // Looping LPs |
|
97 | + // Looping LPs |
|
98 | 98 | foreach ($lp_list as $lp_id =>$lp) { |
99 | 99 | $exercise_list = Event::get_all_exercises_from_lp($lp_id, $course_info['real_id']); |
100 | 100 | // Looping Chamilo Exercises in LP |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | } |
112 | 112 | } |
113 | 113 | } |
114 | - $main_result[$current_course['code']] = $attempt_result; |
|
114 | + $main_result[$current_course['code']] = $attempt_result; |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | $total_average_score = 0; |
@@ -119,102 +119,102 @@ discard block |
||
119 | 119 | $html_result = ''; |
120 | 120 | if (!empty($users) && is_array($users)) { |
121 | 121 | |
122 | - $html_result .= '<table class="data_table">'; |
|
123 | - $html_result .= '<tr><th>'.get_lang('User').'</th>'; |
|
124 | - foreach($course_list as $item ) { |
|
125 | - $html_result .= '<th>'.$item['title'].'<br /> '.get_lang('AverageScore').' %</th>'; |
|
126 | - } |
|
127 | - $html_result .= '<th>'.get_lang('AverageScore').' %</th>'; |
|
128 | - $html_result .= '<th>'.get_lang('LastConnexionDate').'</th></tr>'; |
|
129 | - |
|
130 | - foreach ($users as $user) { |
|
131 | - $total_student = 0; |
|
132 | - $counter ++; |
|
133 | - $s_css_class = 'row_even'; |
|
134 | - if ($counter % 2 ==0 ) { |
|
135 | - $s_css_class = 'row_odd'; |
|
136 | - } |
|
137 | - $html_result .= "<tr class='$s_css_class'> |
|
122 | + $html_result .= '<table class="data_table">'; |
|
123 | + $html_result .= '<tr><th>'.get_lang('User').'</th>'; |
|
124 | + foreach($course_list as $item ) { |
|
125 | + $html_result .= '<th>'.$item['title'].'<br /> '.get_lang('AverageScore').' %</th>'; |
|
126 | + } |
|
127 | + $html_result .= '<th>'.get_lang('AverageScore').' %</th>'; |
|
128 | + $html_result .= '<th>'.get_lang('LastConnexionDate').'</th></tr>'; |
|
129 | + |
|
130 | + foreach ($users as $user) { |
|
131 | + $total_student = 0; |
|
132 | + $counter ++; |
|
133 | + $s_css_class = 'row_even'; |
|
134 | + if ($counter % 2 ==0 ) { |
|
135 | + $s_css_class = 'row_odd'; |
|
136 | + } |
|
137 | + $html_result .= "<tr class='$s_css_class'> |
|
138 | 138 | <td >"; |
139 | - $html_result .= $user['firstname'].' '.$user['lastname']; |
|
140 | - $html_result .= "</td>"; |
|
141 | - |
|
142 | - // Getting course list |
|
143 | - |
|
144 | - $counter = 0; |
|
145 | - $total_result_by_user = 0; |
|
146 | - foreach ($course_list as $current_course ) { |
|
147 | - $total_course = 0; |
|
148 | - $html_result .= "<td>"; |
|
149 | - |
|
150 | - |
|
151 | - $result = '-'; |
|
152 | - if (isset($main_result[$current_course['code']][$user['user_id']])) { |
|
153 | - $user_info_stat = $main_result[$current_course['code']][$user['user_id']]; |
|
154 | - if (!empty($user_info_stat['result']) && !empty($user_info_stat['attempts'])) { |
|
155 | - $result = round( |
|
156 | - $user_info_stat['result'] / $user_info_stat['attempts'] * 100, |
|
157 | - 2 |
|
158 | - ); |
|
159 | - $total_course += $result; |
|
160 | - $total_result_by_user += $result; |
|
161 | - $course_average[$current_course['code']] += $total_course; |
|
162 | - $course_average_counter[$current_course['code']]++; |
|
163 | - $result = $result . ' (' . $user_info_stat['attempts'] . ' ' . get_lang( |
|
164 | - 'Attempts' |
|
165 | - ) . ')'; |
|
166 | - $counter++; |
|
167 | - } |
|
168 | - } |
|
169 | - |
|
170 | - $html_result .= $result; |
|
171 | - $html_result .= "</td>"; |
|
172 | - } |
|
173 | - if (empty($counter)) { |
|
174 | - $total_student = '-'; |
|
175 | - } else { |
|
176 | - $total_student = $total_result_by_user/$counter; |
|
177 | - $total_average_score+=$total_student; |
|
178 | - $total_average_score_count++; |
|
179 | - } |
|
180 | - $string_date=Tracking :: get_last_connection_date($user['user_id'],true); |
|
181 | - $html_result .="<td>$total_student</td><td>$string_date</td></tr>"; |
|
182 | - } |
|
183 | - |
|
184 | - $html_result .="<tr><th>".get_lang('AverageScore')."</th>"; |
|
185 | - $total_average = 0; |
|
186 | - $counter = 0; |
|
187 | - foreach($course_list as $course_item) { |
|
188 | - if (!empty($course_average_counter[$course_item['code']])) { |
|
189 | - $average_per_course = round( |
|
190 | - $course_average[$course_item['code']]/($course_average_counter[$course_item['code']]*100)*100, |
|
191 | - 2 |
|
192 | - ); |
|
193 | - } else { |
|
194 | - $average_per_course = '-'; |
|
195 | - } |
|
196 | - if (!empty($average_per_course)) { |
|
197 | - $counter++; |
|
198 | - } |
|
199 | - $total_average = $total_average + $average_per_course; |
|
200 | - $html_result .="<td>$average_per_course</td>"; |
|
201 | - } |
|
202 | - if (!empty($total_average_score_count)) { |
|
203 | - $total_average = round($total_average_score/($total_average_score_count*100)*100,2); |
|
204 | - } else { |
|
205 | - $total_average = '-'; |
|
206 | - } |
|
207 | - |
|
208 | - $html_result .='<td>'.$total_average.'</td>'; |
|
209 | - $html_result .="<td>-</td>"; |
|
210 | - $html_result .="</tr>"; |
|
211 | - $html_result .= '</table>'; |
|
139 | + $html_result .= $user['firstname'].' '.$user['lastname']; |
|
140 | + $html_result .= "</td>"; |
|
141 | + |
|
142 | + // Getting course list |
|
143 | + |
|
144 | + $counter = 0; |
|
145 | + $total_result_by_user = 0; |
|
146 | + foreach ($course_list as $current_course ) { |
|
147 | + $total_course = 0; |
|
148 | + $html_result .= "<td>"; |
|
149 | + |
|
150 | + |
|
151 | + $result = '-'; |
|
152 | + if (isset($main_result[$current_course['code']][$user['user_id']])) { |
|
153 | + $user_info_stat = $main_result[$current_course['code']][$user['user_id']]; |
|
154 | + if (!empty($user_info_stat['result']) && !empty($user_info_stat['attempts'])) { |
|
155 | + $result = round( |
|
156 | + $user_info_stat['result'] / $user_info_stat['attempts'] * 100, |
|
157 | + 2 |
|
158 | + ); |
|
159 | + $total_course += $result; |
|
160 | + $total_result_by_user += $result; |
|
161 | + $course_average[$current_course['code']] += $total_course; |
|
162 | + $course_average_counter[$current_course['code']]++; |
|
163 | + $result = $result . ' (' . $user_info_stat['attempts'] . ' ' . get_lang( |
|
164 | + 'Attempts' |
|
165 | + ) . ')'; |
|
166 | + $counter++; |
|
167 | + } |
|
168 | + } |
|
169 | + |
|
170 | + $html_result .= $result; |
|
171 | + $html_result .= "</td>"; |
|
172 | + } |
|
173 | + if (empty($counter)) { |
|
174 | + $total_student = '-'; |
|
175 | + } else { |
|
176 | + $total_student = $total_result_by_user/$counter; |
|
177 | + $total_average_score+=$total_student; |
|
178 | + $total_average_score_count++; |
|
179 | + } |
|
180 | + $string_date=Tracking :: get_last_connection_date($user['user_id'],true); |
|
181 | + $html_result .="<td>$total_student</td><td>$string_date</td></tr>"; |
|
182 | + } |
|
183 | + |
|
184 | + $html_result .="<tr><th>".get_lang('AverageScore')."</th>"; |
|
185 | + $total_average = 0; |
|
186 | + $counter = 0; |
|
187 | + foreach($course_list as $course_item) { |
|
188 | + if (!empty($course_average_counter[$course_item['code']])) { |
|
189 | + $average_per_course = round( |
|
190 | + $course_average[$course_item['code']]/($course_average_counter[$course_item['code']]*100)*100, |
|
191 | + 2 |
|
192 | + ); |
|
193 | + } else { |
|
194 | + $average_per_course = '-'; |
|
195 | + } |
|
196 | + if (!empty($average_per_course)) { |
|
197 | + $counter++; |
|
198 | + } |
|
199 | + $total_average = $total_average + $average_per_course; |
|
200 | + $html_result .="<td>$average_per_course</td>"; |
|
201 | + } |
|
202 | + if (!empty($total_average_score_count)) { |
|
203 | + $total_average = round($total_average_score/($total_average_score_count*100)*100,2); |
|
204 | + } else { |
|
205 | + $total_average = '-'; |
|
206 | + } |
|
207 | + |
|
208 | + $html_result .='<td>'.$total_average.'</td>'; |
|
209 | + $html_result .="<td>-</td>"; |
|
210 | + $html_result .="</tr>"; |
|
211 | + $html_result .= '</table>'; |
|
212 | 212 | } else { |
213 | - Display::display_warning_message(get_lang('NoResults')); |
|
213 | + Display::display_warning_message(get_lang('NoResults')); |
|
214 | 214 | } |
215 | 215 | |
216 | 216 | if (!$export_to_xls) { |
217 | - echo $html_result; |
|
217 | + echo $html_result; |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | Display :: display_footer(); |
@@ -14,20 +14,20 @@ discard block |
||
14 | 14 | $is_allowedToTrack = $is_courseAdmin || $is_platformAdmin || $is_courseCoach || $is_sessionAdmin; |
15 | 15 | |
16 | 16 | if (!$is_allowedToTrack) { |
17 | - Display :: display_header(null); |
|
18 | - api_not_allowed(); |
|
19 | - Display :: display_footer(); |
|
17 | + Display :: display_header(null); |
|
18 | + api_not_allowed(); |
|
19 | + Display :: display_footer(); |
|
20 | 20 | } |
21 | 21 | |
22 | 22 | $export_to_xls = false; |
23 | 23 | if (isset($_GET['export'])) { |
24 | - $export_to_xls = true; |
|
24 | + $export_to_xls = true; |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | if (api_is_platform_admin() ) { |
28 | - $global = true; |
|
28 | + $global = true; |
|
29 | 29 | } else { |
30 | - $global = false; |
|
30 | + $global = false; |
|
31 | 31 | } |
32 | 32 | $global = true; |
33 | 33 | |
@@ -45,14 +45,14 @@ discard block |
||
45 | 45 | $session_id = isset($_REQUEST['session_id']) ? intval($_REQUEST['session_id']) : null; |
46 | 46 | |
47 | 47 | if (empty($session_id)) { |
48 | - $temp_course_list = CourseManager :: get_courses_list(); |
|
48 | + $temp_course_list = CourseManager :: get_courses_list(); |
|
49 | 49 | } else { |
50 | - $temp_course_list = SessionManager::get_course_list_by_session_id($session_id); |
|
50 | + $temp_course_list = SessionManager::get_course_list_by_session_id($session_id); |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | foreach ($temp_course_list as $temp_course_item) { |
54 | - $course_item = CourseManager ::get_course_information($temp_course_item['code']); |
|
55 | - $course_select_list[$temp_course_item['code']] = $course_item['title']; |
|
54 | + $course_item = CourseManager ::get_course_information($temp_course_item['code']); |
|
55 | + $course_select_list[$temp_course_item['code']] = $course_item['title']; |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | //Get session list |
@@ -95,132 +95,132 @@ discard block |
||
95 | 95 | $course_info = api_get_course_info($course_code); |
96 | 96 | |
97 | 97 | if (!empty($course_info)) { |
98 | - $list = new LearnpathList('', $course_code); |
|
99 | - $lp_list = $list->get_flat_list(); |
|
98 | + $list = new LearnpathList('', $course_code); |
|
99 | + $lp_list = $list->get_flat_list(); |
|
100 | 100 | |
101 | - $main_question_list = array(); |
|
101 | + $main_question_list = array(); |
|
102 | 102 | |
103 | - foreach ($lp_list as $lp_id => $lp) { |
|
103 | + foreach ($lp_list as $lp_id => $lp) { |
|
104 | 104 | $exercise_list = Event::get_all_exercises_from_lp( |
105 | 105 | $lp_id, |
106 | 106 | $course_info['real_id'] |
107 | 107 | ); |
108 | 108 | |
109 | - foreach ($exercise_list as $exercise) { |
|
110 | - $my_exercise = new Exercise($course_info['real_id']); |
|
111 | - $my_exercise->read($exercise['path']); |
|
112 | - $question_list = $my_exercise->selectQuestionList(); |
|
113 | - |
|
114 | - $exercise_stats = Event::get_all_exercise_event_from_lp( |
|
115 | - $exercise['path'], |
|
116 | - $course_info['real_id'], |
|
117 | - $session_id |
|
118 | - ); |
|
119 | - |
|
120 | - foreach ($question_list as $question_id) { |
|
121 | - $question_data = Question::read($question_id); |
|
122 | - $main_question_list[$question_id] = $question_data; |
|
123 | - $quantity_exercises = 0; |
|
124 | - $question_result = 0; |
|
125 | - |
|
126 | - foreach ($exercise_stats as $stats) { |
|
127 | - if (!empty($stats['question_list'])) { |
|
128 | - foreach($stats['question_list'] as $my_question_stat) { |
|
129 | - if ($question_id == $my_question_stat['question_id']) { |
|
130 | - $question_result = $question_result + $my_question_stat['marks']; |
|
131 | - $quantity_exercises++; |
|
132 | - } |
|
133 | - } |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - if (!empty($quantity_exercises)) { |
|
138 | - // Score % average |
|
139 | - $main_question_list[$question_id]->results = ($question_result / ($quantity_exercises)); |
|
140 | - } else { |
|
141 | - $main_question_list[$question_id]->results = 0; |
|
142 | - } |
|
143 | - |
|
144 | - $main_question_list[$question_id]->quantity = $quantity_exercises; |
|
145 | - } |
|
146 | - } |
|
147 | - } |
|
109 | + foreach ($exercise_list as $exercise) { |
|
110 | + $my_exercise = new Exercise($course_info['real_id']); |
|
111 | + $my_exercise->read($exercise['path']); |
|
112 | + $question_list = $my_exercise->selectQuestionList(); |
|
113 | + |
|
114 | + $exercise_stats = Event::get_all_exercise_event_from_lp( |
|
115 | + $exercise['path'], |
|
116 | + $course_info['real_id'], |
|
117 | + $session_id |
|
118 | + ); |
|
119 | + |
|
120 | + foreach ($question_list as $question_id) { |
|
121 | + $question_data = Question::read($question_id); |
|
122 | + $main_question_list[$question_id] = $question_data; |
|
123 | + $quantity_exercises = 0; |
|
124 | + $question_result = 0; |
|
125 | + |
|
126 | + foreach ($exercise_stats as $stats) { |
|
127 | + if (!empty($stats['question_list'])) { |
|
128 | + foreach($stats['question_list'] as $my_question_stat) { |
|
129 | + if ($question_id == $my_question_stat['question_id']) { |
|
130 | + $question_result = $question_result + $my_question_stat['marks']; |
|
131 | + $quantity_exercises++; |
|
132 | + } |
|
133 | + } |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + if (!empty($quantity_exercises)) { |
|
138 | + // Score % average |
|
139 | + $main_question_list[$question_id]->results = ($question_result / ($quantity_exercises)); |
|
140 | + } else { |
|
141 | + $main_question_list[$question_id]->results = 0; |
|
142 | + } |
|
143 | + |
|
144 | + $main_question_list[$question_id]->quantity = $quantity_exercises; |
|
145 | + } |
|
146 | + } |
|
147 | + } |
|
148 | 148 | } |
149 | 149 | |
150 | 150 | if (!$export_to_xls) { |
151 | - Display :: display_header(get_lang("MySpace")); |
|
152 | - echo '<div class="actions">'; |
|
153 | - if ($global) { |
|
154 | - echo MySpace::getTopMenu(); |
|
155 | - } else { |
|
156 | - echo '<div style="float:left; clear:left"> |
|
151 | + Display :: display_header(get_lang("MySpace")); |
|
152 | + echo '<div class="actions">'; |
|
153 | + if ($global) { |
|
154 | + echo MySpace::getTopMenu(); |
|
155 | + } else { |
|
156 | + echo '<div style="float:left; clear:left"> |
|
157 | 157 | <a href="courseLog.php?'.api_get_cidreq().'&studentlist=true">'. |
158 | 158 | get_lang('StudentsTracking').'</a> | |
159 | 159 | <a href="courseLog.php?'.api_get_cidreq().'&studentlist=false">'. |
160 | 160 | get_lang('CourseTracking').'</a> '; |
161 | - echo '</div>'; |
|
162 | - } |
|
163 | - echo '</div>'; |
|
161 | + echo '</div>'; |
|
162 | + } |
|
163 | + echo '</div>'; |
|
164 | 164 | |
165 | - if (api_is_platform_admin()) { |
|
166 | - echo MySpace::getAdminActions(); |
|
167 | - } |
|
168 | - echo '<br />'; |
|
169 | - echo '<h2>'.get_lang('LPQuestionListResults').'</h2>'; |
|
165 | + if (api_is_platform_admin()) { |
|
166 | + echo MySpace::getAdminActions(); |
|
167 | + } |
|
168 | + echo '<br />'; |
|
169 | + echo '<h2>'.get_lang('LPQuestionListResults').'</h2>'; |
|
170 | 170 | |
171 | - $form->display(); |
|
171 | + $form->display(); |
|
172 | 172 | |
173 | - if (empty($course_code)) { |
|
174 | - Display::display_warning_message(get_lang('PleaseSelectACourse')); |
|
175 | - } |
|
173 | + if (empty($course_code)) { |
|
174 | + Display::display_warning_message(get_lang('PleaseSelectACourse')); |
|
175 | + } |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | $course_average = array(); |
179 | 179 | $counter = 0; |
180 | 180 | |
181 | 181 | if (!empty($main_question_list) && is_array($main_question_list)) { |
182 | - $html_result .= '<table class="data_table">'; |
|
183 | - $html_result .= '<tr><th>'.get_lang('Question'). |
|
182 | + $html_result .= '<table class="data_table">'; |
|
183 | + $html_result .= '<tr><th>'.get_lang('Question'). |
|
184 | 184 | Display :: return_icon('info3.gif', get_lang('QuestionsAreTakenFromLPExercises'), array('align' => 'absmiddle', 'hspace' => '3px')).'</th>'; |
185 | - $html_result .= '<th>'.$course_info['visual_code'].' '.get_lang('AverageScore').Display :: return_icon('info3.gif', get_lang('AllStudentsAttemptsAreConsidered'), array('align' => 'absmiddle', 'hspace' => '3px')).' </th>'; |
|
186 | - $html_result .= '<th>'.get_lang('Quantity').'</th>'; |
|
187 | - |
|
188 | - foreach ($main_question_list as $question) { |
|
189 | - $total_student = 0; |
|
190 | - $counter ++; |
|
191 | - $s_css_class = 'row_even'; |
|
192 | - if ($counter % 2 ==0 ) { |
|
193 | - $s_css_class = 'row_odd'; |
|
194 | - } |
|
195 | - $html_result .= "<tr class='$s_css_class'> |
|
185 | + $html_result .= '<th>'.$course_info['visual_code'].' '.get_lang('AverageScore').Display :: return_icon('info3.gif', get_lang('AllStudentsAttemptsAreConsidered'), array('align' => 'absmiddle', 'hspace' => '3px')).' </th>'; |
|
186 | + $html_result .= '<th>'.get_lang('Quantity').'</th>'; |
|
187 | + |
|
188 | + foreach ($main_question_list as $question) { |
|
189 | + $total_student = 0; |
|
190 | + $counter ++; |
|
191 | + $s_css_class = 'row_even'; |
|
192 | + if ($counter % 2 ==0 ) { |
|
193 | + $s_css_class = 'row_odd'; |
|
194 | + } |
|
195 | + $html_result .= "<tr class='$s_css_class'> |
|
196 | 196 | <td >"; |
197 | - $question_title = trim($question->question); |
|
198 | - if (empty($question_title)) { |
|
199 | - $html_result .= get_lang('Untitled').' '.get_lang('Question').' #'.$question->id; |
|
200 | - } else { |
|
201 | - $html_result .= $question->question; |
|
202 | - } |
|
203 | - |
|
204 | - $html_result .= "</td>"; |
|
205 | - $html_result .= "<td>"; |
|
206 | - $html_result .= round($question->results, 2).' / '.$question->weighting; |
|
207 | - $html_result .= "</td>"; |
|
208 | - |
|
209 | - $html_result .= "<td>"; |
|
210 | - $html_result .= $question->quantity; |
|
211 | - $html_result .= "</td>"; |
|
212 | - } |
|
213 | - |
|
214 | - $html_result .="</tr>"; |
|
215 | - $html_result .= '</table>'; |
|
197 | + $question_title = trim($question->question); |
|
198 | + if (empty($question_title)) { |
|
199 | + $html_result .= get_lang('Untitled').' '.get_lang('Question').' #'.$question->id; |
|
200 | + } else { |
|
201 | + $html_result .= $question->question; |
|
202 | + } |
|
203 | + |
|
204 | + $html_result .= "</td>"; |
|
205 | + $html_result .= "<td>"; |
|
206 | + $html_result .= round($question->results, 2).' / '.$question->weighting; |
|
207 | + $html_result .= "</td>"; |
|
208 | + |
|
209 | + $html_result .= "<td>"; |
|
210 | + $html_result .= $question->quantity; |
|
211 | + $html_result .= "</td>"; |
|
212 | + } |
|
213 | + |
|
214 | + $html_result .="</tr>"; |
|
215 | + $html_result .= '</table>'; |
|
216 | 216 | } else { |
217 | - if (!empty($course_code)) { |
|
218 | - Display::display_warning_message(get_lang('NoResults')); |
|
219 | - } |
|
217 | + if (!empty($course_code)) { |
|
218 | + Display::display_warning_message(get_lang('NoResults')); |
|
219 | + } |
|
220 | 220 | } |
221 | 221 | |
222 | 222 | if (!$export_to_xls) { |
223 | - echo $html_result; |
|
223 | + echo $html_result; |
|
224 | 224 | } |
225 | 225 | |
226 | 226 | Display :: display_footer(); |
@@ -38,7 +38,7 @@ |
||
38 | 38 | if (stripos($line, '$_configuration[\'system_version\']') !== false) { |
39 | 39 | $found_version = true; |
40 | 40 | $line = '$_configuration[\'system_version\'] = \'' . $new_version . '\';' . "\r\n"; |
41 | - } elseif (stripos($line, '$_configuration[\'system_stable\']') !== false) { |
|
41 | + } elseif (stripos($line, '$_configuration[\'system_stable\']') !== false) { |
|
42 | 42 | $found_stable = true; |
43 | 43 | $line = '$_configuration[\'system_stable\'] = ' . ($new_version_stable ? 'true' : 'false') . ';' . "\r\n"; |
44 | 44 | } elseif (stripos($line, '$_configuration[\'software_name\']') !== false) { |