test.oop.kses.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <?php
  2. // This is a q&d program that shows some of the results of
  3. // running KSES. If you have further questions, check the
  4. // current valid email address at http://chaos.org/contact/
  5. // Make sure we're in a usable PHP environment
  6. if(substr(phpversion(), 0, 1) < 4)
  7. {
  8. define('KSESTEST_VER', 0);
  9. }
  10. elseif(substr(phpversion(), 0, 1) >= 5)
  11. {
  12. define('KSESTEST_VER', 5);
  13. }
  14. else
  15. {
  16. define('KSESTEST_VER', 4);
  17. }
  18. // See if we're in command line or web
  19. if($_SERVER["DOCUMENT_ROOT"] == "")
  20. {
  21. define('KSESTEST_ENV', 'CLI');
  22. }
  23. else
  24. {
  25. define('KSESTEST_ENV', 'WEB');
  26. }
  27. if(KSESTEST_VER == 0)
  28. {
  29. $message = array(
  30. "Error: Not using a current version of PHP!",
  31. "You are using PHP version " . phpversion() . ".",
  32. "KSES Class version requires PHP4 or better.",
  33. "KSES test program ending."
  34. );
  35. displayPage(
  36. array("title" => "Error running KSES test", "message" => $message)
  37. );
  38. exit();
  39. }
  40. $include_file = "php" . KSESTEST_VER . ".class.kses.php";
  41. if(file_exists($include_file) && is_readable($include_file))
  42. {
  43. include_once($include_file);
  44. }
  45. else
  46. {
  47. $message = array(
  48. "Error: Unable to find '" . $include_file . "'.",
  49. "Please check your include path and make sure the file is available.",
  50. "Path: " . ini_get('include_path')
  51. );
  52. displayPage(
  53. array('title' => 'Unable to include ' . $include_file, 'message' => $message)
  54. );
  55. exit();
  56. }
  57. $kses_type = "kses" . KSESTEST_VER;
  58. $myKses = new $kses_type;
  59. $test_text = array();
  60. $test_text = test1_protocols($myKses);
  61. $test_text = array_merge($test_text, test1_html($myKses));
  62. $test_text = array_merge($test_text, test1_kses($myKses));
  63. displayPage(
  64. array('title' => 'New Test', 'message' => $test_text)
  65. );
  66. function test1_kses(&$myKses)
  67. {
  68. $out = array(output_hr(), "Testing current configuration");
  69. $test_tags = array(
  70. '<a href="http://www.chaos.org/">www.chaos.org</a>',
  71. '<a name="X">Short \'a name\' tag</a>',
  72. '<td colspan="3" rowspan="5">Foo</td>',
  73. '<td rowspan="2" class="mugwump" style="background-color: rgb(255, 204 204);">Bar</td>',
  74. '<td nowrap>Very Long String running to 1000 characters...</td>',
  75. '<td bgcolor="#00ff00" nowrap>Very Long String with a blue background</td>',
  76. '<a href="proto1://www.foo.com">New protocol test</a>',
  77. '<img src="proto2://www.foo.com" />',
  78. '<a href="javascript:javascript:javascript:javascript:javascript:alert(\'Boo!\');">bleep</a>',
  79. '<a href="proto4://abc.xyz.foo.com">Another new protocol</a>',
  80. '<a href="proto9://foo.foo.foo.foo.foo.org/">Test of "proto9"</a>',
  81. '<td width="75">Bar!</td>',
  82. '<td width="200">Long Cell</td>'
  83. );
  84. $out_li = array();
  85. // Keep only allowed HTML from the presumed 'form'.
  86. foreach($test_tags as $tag)
  87. {
  88. $temp = $myKses->Parse($tag);
  89. $check = ($temp == $tag) ? true : false;
  90. $text = ($temp == $tag) ? 'pass' : 'fail';
  91. $li_text = output_testresult($check, $text) . output_newline();
  92. $li_text .= "Input: &nbsp;" . output_translate($tag) . output_newline();
  93. $li_text .= "Output: " . output_translate($temp);
  94. if(KSESTEST_ENV == 'CLI')
  95. {
  96. $li_text .= output_newline();
  97. }
  98. array_push($out_li, output_code_wrap($li_text));
  99. }
  100. $out = array_merge($out, array(output_ul($out_li)));
  101. array_push($out, output_hr());
  102. array_push($out, "Testing is now finished.");
  103. return $out;
  104. }
  105. function output_code_wrap($text)
  106. {
  107. if(KSESTEST_ENV == 'CLI')
  108. {
  109. return $text;
  110. }
  111. else
  112. {
  113. return "<code>\n$text<code>\n";
  114. }
  115. }
  116. function output_translate($text)
  117. {
  118. if(KSESTEST_ENV == 'CLI')
  119. {
  120. return $text;
  121. }
  122. else
  123. {
  124. return htmlentities($text);
  125. }
  126. }
  127. function output_testresult($pass = false, $text = "")
  128. {
  129. if(KSESTEST_ENV == 'CLI')
  130. {
  131. return '[' . $text . ']';
  132. }
  133. else
  134. {
  135. if($pass == true)
  136. {
  137. return '<span style="color: green;">[' . $text . ']</span>';
  138. }
  139. else
  140. {
  141. return '<span style="color: red;">[' . $text . ']</span>';
  142. }
  143. }
  144. }
  145. function output_spaces()
  146. {
  147. if(KSESTEST_ENV == 'WEB')
  148. {
  149. $out = "&nbsp;&nbsp;&nbsp;";
  150. }
  151. else
  152. {
  153. $out = " ";
  154. }
  155. return $out;
  156. }
  157. function output_newline()
  158. {
  159. if(KSESTEST_ENV == 'WEB')
  160. {
  161. $out = "<br />\n";
  162. }
  163. else
  164. {
  165. $out = "\n";
  166. }
  167. return $out;
  168. }
  169. function displayPage($data = array())
  170. {
  171. $title = ($data['title'] == '') ? 'No title' : $data['title'];
  172. $message = ($data['message'] == '') ? array('No message') : $data['message'];
  173. $out = "";
  174. foreach($message as $text)
  175. {
  176. if(KSESTEST_ENV == 'WEB')
  177. {
  178. $header = "\t\t<h1>$title</h1>\n\t\t<hr />\n";
  179. $out .= "\t\t<p>\n";
  180. $out .= "\t\t\t$text\n";
  181. $out .= "\t\t</p>\n";
  182. }
  183. else
  184. {
  185. $header = "$title\n" . str_repeat('-', 60) . "\n\n";
  186. $out .= "\t$text\n\n";
  187. }
  188. }
  189. if(KSESTEST_ENV == 'WEB')
  190. {
  191. echo "<html>\n";
  192. echo "\t<head>\n";
  193. echo "\t\t<title>$title</title>\n";
  194. echo "\t</head>\n";
  195. echo "\t<body>\n";
  196. echo $header;
  197. echo $out;
  198. echo "\t</body>\n";
  199. echo "</html>\n";
  200. }
  201. else
  202. {
  203. echo $header;
  204. echo $out;
  205. }
  206. }
  207. function output_hr()
  208. {
  209. if(KSESTEST_ENV == 'WEB')
  210. {
  211. return "\t\t\t<hr />\n";
  212. }
  213. else
  214. {
  215. return str_repeat(60, '-') . "\n";
  216. }
  217. }
  218. function output_ul($data = array(), $padding = "")
  219. {
  220. if(!is_array($data) || count($data) < 1)
  221. {
  222. return "";
  223. }
  224. $text = "";
  225. if(KSESTEST_ENV == 'WEB')
  226. {
  227. $text = "\t\t\t<ul>\n";
  228. foreach($data as $li)
  229. {
  230. $text .= "\t\t\t\t<li>$li</li>\n";
  231. }
  232. $text .= "\t\t\t</ul>\n";
  233. }
  234. else
  235. {
  236. foreach($data as $li)
  237. {
  238. $text .= $padding . " * $li\n";
  239. }
  240. }
  241. return $text;
  242. }
  243. function test1_protocols(&$myKses)
  244. {
  245. $default_prots = $myKses->dumpProtocols();
  246. $out_text = array();
  247. if(count($default_prots) > 0)
  248. {
  249. array_push($out_text, "Initial protocols from KSES" . KSESTEST_VER . ":");
  250. array_push($out_text, output_ul($default_prots));
  251. array_push($out_text, output_hr());
  252. }
  253. $myKses->AddProtocols(array("proto1", "proto2:", "proto3")); // Add a list of protocols
  254. $myKses->AddProtocols("proto4:"); // Add a single protocol (Note ':' is optional at end)
  255. $myKses->AddProtocol("proto9", "mystery:", "anarchy");
  256. $myKses->AddProtocol("alpha", "beta", "gamma:");
  257. $add_protocol = "\t\t\t<ol>\n";
  258. $add_protocol .= "\t\t\t\t" . '<li>$myKses->AddProtocols(array("proto1", "proto2:", "proto3"));</li>' . "\n";
  259. $add_protocol .= "\t\t\t\t" . '<li>$myKses->AddProtocols("proto4:");</li>' . "\n";
  260. $add_protocol .= "\t\t\t\t" . '<li>$myKses->AddProtocols("proto4:");</li>' . "\n";
  261. $add_protocol .= "\t\t\t\t" . '<li>$myKses->AddProtocol("proto9", "mystery:", "anarchy");</li>' . "\n";
  262. $add_protocol .= "\t\t\t\t" . '<li>$myKses->AddProtocol("alpha", "beta", "gamma:");</li>' . "\n";
  263. $add_protocol .= "\t\t\t</ol>\n";
  264. array_push($out_text, $add_protocol);
  265. $new_prots = $myKses->dumpProtocols();
  266. if(count($new_prots) > 0)
  267. {
  268. array_push($out_text, "New protocols from KSES" . KSESTEST_VER . " after using AddProtocol(s):");
  269. array_push($out_text, output_ul($new_prots));
  270. array_push($out_text, output_hr());
  271. }
  272. $myKses->RemoveProtocols(array("mystery", "anarchy:"));
  273. $myKses->RemoveProtocols("alpha:");
  274. $myKses->RemoveProtocol("beta:");
  275. $myKses->RemoveProtocol("gamma");
  276. $remove_protocol = "\t\t\t<ol>\n";
  277. $remove_protocol .= "\t\t\t\t" . '<li>$myKses->RemoveProtocols(array("mystery", "anarchy:"));</li>' . "\n";
  278. $remove_protocol .= "\t\t\t\t" . '<li>$myKses->RemoveProtocols("alpha:");</li>' . "\n";
  279. $remove_protocol .= "\t\t\t\t" . '<li>$myKses->RemoveProtocol("beta:");</li>' . "\n";
  280. $remove_protocol .= "\t\t\t\t" . '<li>$myKses->RemoveProtocol("gamma");</li>' . "\n";
  281. $remove_protocol .= "\t\t\t</ol>\n";
  282. array_push($out_text, $remove_protocol);
  283. $new_prots = $myKses->dumpProtocols();
  284. if(count($new_prots) > 0)
  285. {
  286. array_push($out_text, "Resulting protocols from KSES" . KSESTEST_VER . " after using RemoveProtocol(s):");
  287. array_push($out_text, output_ul($new_prots));
  288. array_push($out_text, output_hr());
  289. }
  290. $myKses->SetProtocols(array("https", "gopher", "news"));
  291. $set_protocol = "\t\t\t<ol>\n";
  292. $set_protocol .= "\t\t\t\t" . '<li>$myKses->SetProtocols(array("https", "gopher", "news"));</li>' . "\n";
  293. $set_protocol .= "\t\t\t</ol>\n";
  294. array_push($out_text, $set_protocol);
  295. $new_prots = $myKses->dumpProtocols();
  296. if(count($new_prots) > 0)
  297. {
  298. array_push($out_text, "Resulting protocols from KSES" . KSESTEST_VER . " after using SetProtocols:");
  299. array_push($out_text, output_ul($new_prots));
  300. array_push($out_text, output_hr());
  301. }
  302. // Invisible reset
  303. $myKses->SetProtocols(array("http", "proto1", "proto2", "proto9"));
  304. return $out_text;
  305. }
  306. function test1_html(&$myKses)
  307. {
  308. $out = array();
  309. // Allows <p>|</p> tag
  310. $myKses->AddHTML("p");
  311. // Allows 'a' tag with href|name attributes,
  312. // href has minlen of 10 chars, and maxlen of 25 chars
  313. // name has minlen of 2 chars
  314. $myKses->AddHTML(
  315. "a",
  316. array(
  317. "href" => array('maxlen' => 25, 'minlen' => 10),
  318. "name" => array('minlen' => 2)
  319. )
  320. );
  321. // Allows 'td' tag with colspan|rowspan|class|style|width|nowrap attributes,
  322. // colspan has minval of 2 and maxval of 5
  323. // rowspan has minval of 3 and maxval of 6
  324. // class has minlen of 1 char and maxlen of 10 chars
  325. // style has minlen of 10 chars and maxlen of 100 chars
  326. // width has maxval of 100
  327. // nowrap is valueless
  328. $myKses->AddHTML(
  329. "td",
  330. array(
  331. "colspan" => array('minval' => 2, 'maxval' => 5),
  332. "rowspan" => array('minval' => 3, 'maxval' => 6),
  333. "class" => array("minlen" => 1, 'maxlen' => 10),
  334. "width" => array("maxval" => 100),
  335. "style" => array('minlen' => 10, 'maxlen' => 100),
  336. "nowrap" => array('valueless' => 'y')
  337. )
  338. );
  339. array_push($out, "Modifying HTML Tests:");
  340. $code_text = "<pre>\n";
  341. $code_text .= " // Allows &lt;p&gt;|&lt;/p&gt; tag\n";
  342. $code_text .= " \$myKses-&gt;AddHTML(\"p\");\n";
  343. $code_text .= "\n";
  344. $code_text .= " // Allows 'a' tag with href|name attributes,\n";
  345. $code_text .= " // href has minlen of 10 chars, and maxlen of 25 chars\n";
  346. $code_text .= " // name has minlen of 2 chars\n";
  347. $code_text .= " \$myKses-&gt;AddHTML(\n";
  348. $code_text .= " \"a\",\n";
  349. $code_text .= " array(\n";
  350. $code_text .= " \"href\" =&gt; array('maxlen' =&gt; 25, 'minlen' =&gt; 10),\n";
  351. $code_text .= " \"name\" =&gt; array('minlen' =&gt; 2)\n";
  352. $code_text .= " )\n";
  353. $code_text .= " );\n";
  354. $code_text .= "\n";
  355. $code_text .= " // Allows 'td' tag with colspan|rowspan|class|style|width|nowrap attributes,\n";
  356. $code_text .= " // colspan has minval of 2 and maxval of 5\n";
  357. $code_text .= " // rowspan has minval of 3 and maxval of 6\n";
  358. $code_text .= " // class has minlen of 1 char and maxlen of 10 chars\n";
  359. $code_text .= " // style has minlen of 10 chars and maxlen of 100 chars\n";
  360. $code_text .= " // width has maxval of 100\n";
  361. $code_text .= " // nowrap is valueless\n";
  362. $code_text .= " \$myKses-&gt;AddHTML(\n";
  363. $code_text .= " \"td\",\n";
  364. $code_text .= " array(\n";
  365. $code_text .= " \"colspan\" =&gt; array('minval' =&gt; 2, 'maxval' =&gt; 5),\n";
  366. $code_text .= " \"rowspan\" =&gt; array('minval' =&gt; 3, 'maxval' =&gt; 6),\n";
  367. $code_text .= " \"class\" =&gt; array(\"minlen\" =&gt; 1, 'maxlen' =&gt; 10),\n";
  368. $code_text .= " \"width\" =&gt; array(\"maxval\" =&gt; 100),\n";
  369. $code_text .= " \"style\" =&gt; array('minlen' =&gt; 10, 'maxlen' =&gt; 100),\n";
  370. $code_text .= " \"nowrap\" =&gt; array('valueless' =&gt; 'y')\n";
  371. $code_text .= " )\n";
  372. $code_text .= " );\n";
  373. $code_text .= "</pre>\n";
  374. array_push($out, $code_text);
  375. array_push($out, output_hr());
  376. array_push($out, "Net results:");
  377. $out_elems = $myKses->DumpElements();
  378. if(count($out_elems) > 0)
  379. {
  380. //array_push($out, "\t\t\t<ul>\n");
  381. foreach($out_elems as $tag => $attr_data)
  382. {
  383. $out_li_elems = array();
  384. $elem_text = "(X)HTML element $tag";
  385. $allow = "";
  386. if(isset($attr_data) && is_array($attr_data) && count($attr_data) > 0)
  387. {
  388. $allow = " allows attribute";
  389. if(count($attr_data) > 1)
  390. {
  391. $allow .= "s";
  392. }
  393. $allow .= ":\n";
  394. }
  395. array_push($out_li_elems, "$elem_text$allow");
  396. $attr_test_li = array();
  397. if(isset($attr_data) && is_array($attr_data) && count($attr_data) > 0)
  398. {
  399. foreach($attr_data as $attr_name => $attr_tests)
  400. {
  401. $li_text = $attr_name;
  402. if(isset($attr_tests) && count($attr_tests) > 0)
  403. {
  404. foreach($attr_tests as $test_name => $test_val)
  405. {
  406. switch($test_name)
  407. {
  408. case "maxlen":
  409. $li_text .= " - maximum length of '" . $test_val . "' characters";
  410. break;
  411. case "minlen":
  412. $li_text .= " - minimum length of '" . $test_val . "' characters";
  413. break;
  414. case "minval":
  415. $li_text .= " - minimum value of '" . $test_val . "'";
  416. break;
  417. case "maxval":
  418. $li_text .= " - maximum value of '" . $test_val . "'";
  419. break;
  420. case "valueless":
  421. switch(strtolower($test_val))
  422. {
  423. case 'n':
  424. $li_text .= " - must not be valueless";
  425. break;
  426. case 'y':
  427. $li_text .= " - must be valueless";
  428. break;
  429. default:
  430. break;
  431. }
  432. break;
  433. default:
  434. break;
  435. }
  436. }
  437. }
  438. array_push($attr_test_li, $li_text);
  439. }
  440. if(count($attr_test_li) > 0)
  441. {
  442. $attr_test_li = output_ul($attr_test_li, " ");
  443. $out_li_elems = array("$elem_text$allow$attr_test_li");
  444. }
  445. }
  446. $out = array_merge($out, $out_li_elems);
  447. }
  448. }
  449. return $out;
  450. }
  451. ?>