HTML.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once api_get_path(SYS_PATH).'main/inc/lib/kses-0.2.2/kses.php';
  4. /**
  5. * QuickForm rule to check a html.
  6. */
  7. class HTML_QuickForm_Rule_HTML extends HTML_QuickForm_Rule
  8. {
  9. /**
  10. * Function to validate HTML.
  11. *
  12. * @see HTML_QuickForm_Rule
  13. *
  14. * @param string $html
  15. *
  16. * @return bool True if html is valid
  17. */
  18. public function validate($html, $mode = NO_HTML)
  19. {
  20. $allowed_tags = self::get_allowed_tags($mode, $fullpage);
  21. $cleaned_html = kses($html, $allowed_tags);
  22. return $html == $cleaned_html;
  23. }
  24. /**
  25. * Get allowed tags.
  26. *
  27. * @param int $mode NO_HTML, STUDENT_HTML, TEACHER_HTML,
  28. * STUDENT_HTML_FULLPAGE or TEACHER_HTML_FULLPAGE
  29. * @param bool $fullpage if true, the allowed tags for full-page editing
  30. * are returned
  31. */
  32. public static function get_allowed_tags($mode)
  33. {
  34. // Include the allowed tags.
  35. //include __DIR__.'/allowed_tags.inc.php';
  36. global $allowed_tags_student, $allowed_tags_student_full_page, $allowed_tags_teacher, $allowed_tags_teacher_full_page;
  37. switch ($mode) {
  38. case NO_HTML:
  39. return [];
  40. break;
  41. case STUDENT_HTML:
  42. return $allowed_tags_student;
  43. break;
  44. case STUDENT_HTML_FULLPAGE:
  45. return array_merge($allowed_tags_student, $allowed_tags_student_full_page);
  46. break;
  47. case TEACHER_HTML:
  48. return $allowed_tags_teacher;
  49. break;
  50. case TEACHER_HTML_FULLPAGE:
  51. return array_merge($allowed_tags_teacher, $allowed_tags_teacher_full_page);
  52. break;
  53. default:
  54. return [];
  55. break;
  56. }
  57. }
  58. }