HTML.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. }