FormValidator.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Objects of this class can be used to create/manipulate/validate user input.
  5. */
  6. class FormValidator extends HTML_QuickForm
  7. {
  8. /**
  9. * Create a form validator based on an array of form data:
  10. *
  11. * array(
  12. * 'name' => 'zombie_report_parameters', //optional
  13. * 'method' => 'GET', //optional
  14. * 'items' => array(
  15. * array(
  16. * 'name' => 'ceiling',
  17. * 'label' => 'Ceiling', //optional
  18. * 'type' => 'date',
  19. * 'default' => date() //optional
  20. * ),
  21. * array(
  22. * 'name' => 'active_only',
  23. * 'label' => 'ActiveOnly',
  24. * 'type' => 'checkbox',
  25. * 'default' => true
  26. * ),
  27. * array(
  28. * 'name' => 'submit_button',
  29. * 'type' => 'style_submit_button',
  30. * 'value' => get_lang('Search'),
  31. * 'attributes' => array('class' => 'search')
  32. * )
  33. * )
  34. * );
  35. *
  36. * @param array form_data
  37. * @return FormValidator
  38. */
  39. public static function create($form_data)
  40. {
  41. if (empty($form_data)) {
  42. return null;
  43. }
  44. $form_name = isset($form_data['name']) ? $form_data['name'] : 'form';
  45. $form_method = isset($form_data['method']) ? $form_data['method'] : 'POST';
  46. $form_action = isset($form_data['action']) ? $form_data['action'] : '';
  47. $form_target = isset($form_data['target']) ? $form_data['target'] : '';
  48. $form_attributes = isset($form_data['attributes']) ? $form_data['attributes'] : null;
  49. $form_track_submit = isset($form_data['track_submit']) ? $form_data['track_submit'] : true;
  50. $result = new FormValidator($form_name, $form_method, $form_action, $form_target, $form_attributes, $form_track_submit);
  51. $defaults = array();
  52. foreach ($form_data['items'] as $item) {
  53. $name = $item['name'];
  54. $type = isset($item['type']) ? $item['type'] : 'text';
  55. $label = isset($item['label']) ? $item['label'] : '';
  56. if ($type == 'wysiwyg') {
  57. $element = $result->add_html_editor($name, $label);
  58. } else {
  59. $element = $result->addElement($type, $name, $label);
  60. }
  61. if (isset($item['attributes'])) {
  62. $attributes = $item['attributes'];
  63. $element->setAttributes($attributes);
  64. }
  65. if (isset($item['value'])) {
  66. $value = $item['value'];
  67. $element->setValue($value);
  68. }
  69. if (isset($item['default'])) {
  70. $defaults[$name] = $item['default'];
  71. }
  72. if (isset($item['rules'])) {
  73. $rules = $item['rules'];
  74. foreach ($rules as $rule) {
  75. $message = $rule['message'];
  76. $type = $rule['type'];
  77. $format = isset($rule['format']) ? $rule['format'] : null;
  78. $validation = isset($rule['validation']) ? $rule['validation'] : 'server';
  79. $force = isset($rule['force']) ? $rule['force'] : false;
  80. $result->addRule($name, $message, $type, $format, $validation, $reset, $force);
  81. }
  82. }
  83. }
  84. $result->setDefaults($defaults);
  85. return $result;
  86. }
  87. public $with_progress_bar = false;
  88. /**
  89. * Constructor
  90. * @param string $form_name Name of the form
  91. * @param string $method (optional Method ('post' (default) or 'get')
  92. * @param string $action (optional Action (default is $PHP_SELF)
  93. * @param string $target (optional Form's target defaults to '_self'
  94. * @param mixed $attributes (optional) Extra attributes for <form> tag
  95. * @param bool $track_submit (optional) Whether to track if the form was
  96. * submitted by adding a special hidden field (default = true)
  97. */
  98. public function __construct($form_name, $method = 'post', $action = '', $target = '', $attributes = null, $track_submit = true)
  99. {
  100. //Default form class
  101. if (is_array($attributes) && !isset($attributes['class']) || empty($attributes)) {
  102. $attributes['class'] = 'form-horizontal';
  103. }
  104. parent::__construct($form_name, $method, $action, $target, $attributes, $track_submit);
  105. // Load some custom elements and rules
  106. $dir = api_get_path(LIBRARY_PATH) . 'formvalidator/';
  107. $this->registerElementType('html_editor', $dir . 'Element/html_editor.php', 'HTML_QuickForm_html_editor');
  108. $this->registerElementType('datepicker', $dir . 'Element/datepicker.php', 'HTML_QuickForm_datepicker');
  109. $this->registerElementType('datepickerdate', $dir . 'Element/datepickerdate.php', 'HTML_QuickForm_datepickerdate');
  110. $this->registerElementType('receivers', $dir . 'Element/receivers.php', 'HTML_QuickForm_receivers');
  111. $this->registerElementType('select_language', $dir . 'Element/select_language.php', 'HTML_QuickForm_Select_Language');
  112. $this->registerElementType('select_theme', $dir . 'Element/select_theme.php', 'HTML_QuickForm_Select_Theme');
  113. $this->registerElementType('style_submit_button', $dir . 'Element/style_submit_button.php', 'HTML_QuickForm_stylesubmitbutton');
  114. $this->registerElementType('style_reset_button', $dir . 'Element/style_reset_button.php', 'HTML_QuickForm_styleresetbutton');
  115. $this->registerElementType('button', $dir . 'Element/style_submit_button.php', 'HTML_QuickForm_stylesubmitbutton');
  116. $this->registerElementType('captcha', 'HTML/QuickForm/CAPTCHA.php', 'HTML_QuickForm_CAPTCHA');
  117. $this->registerElementType('CAPTCHA_Image', 'HTML/QuickForm/CAPTCHA/Image.php', 'HTML_QuickForm_CAPTCHA_Image');
  118. $this->registerRule('date', null, 'HTML_QuickForm_Rule_Date', $dir . 'Rule/Date.php');
  119. $this->registerRule('date_compare', null, 'HTML_QuickForm_Rule_DateCompare', $dir . 'Rule/DateCompare.php');
  120. $this->registerRule('html', null, 'HTML_QuickForm_Rule_HTML', $dir . 'Rule/HTML.php');
  121. $this->registerRule('username_available', null, 'HTML_QuickForm_Rule_UsernameAvailable', $dir . 'Rule/UsernameAvailable.php');
  122. $this->registerRule('username', null, 'HTML_QuickForm_Rule_Username', $dir . 'Rule/Username.php');
  123. $this->registerRule('filetype', null, 'HTML_QuickForm_Rule_Filetype', $dir . 'Rule/Filetype.php');
  124. $this->registerRule('multiple_required', 'required', 'HTML_QuickForm_Rule_MultipleRequired', $dir . 'Rule/MultipleRequired.php');
  125. $this->registerRule('url', null, 'HTML_QuickForm_Rule_Url', $dir . 'Rule/Url.php');
  126. $this->registerRule('compare_fields', null, 'HTML_QuickForm_Compare_Fields', $dir . 'Rule/CompareFields.php');
  127. $this->registerRule('compare_datetime_text', null, 'HTML_QuickForm_Rule_CompareDateTimeText', $dir . 'Rule/CompareDateTimeText.php');
  128. $this->registerRule('CAPTCHA', 'rule', 'HTML_QuickForm_Rule_CAPTCHA', 'HTML/QuickForm/Rule/CAPTCHA.php');
  129. // Modify the default templates
  130. $renderer = & $this->defaultRenderer();
  131. //Form template
  132. $form_template = '<form{attributes}>
  133. <fieldset>
  134. {content}
  135. <div class="clear"></div>
  136. </fieldset>
  137. {hidden}
  138. </form>';
  139. $renderer->setFormTemplate($form_template);
  140. //Element template
  141. if (isset($attributes['class']) && $attributes['class'] == 'well form-inline') {
  142. $element_template = ' {label} {element} ';
  143. $renderer->setElementTemplate($element_template);
  144. } elseif (isset($attributes['class']) && $attributes['class'] == 'form-search') {
  145. $element_template = ' {label} {element} ';
  146. $renderer->setElementTemplate($element_template);
  147. } else {
  148. $element_template = '
  149. <div class="control-group {error_class}">
  150. <label class="control-label">
  151. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  152. {label}
  153. </label>
  154. <div class="controls">
  155. {element}
  156. <!-- BEGIN label_3 -->
  157. {label_3}
  158. <!-- END label_3 -->
  159. <!-- BEGIN label_2 -->
  160. <p class="help-block">{label_2}</p>
  161. <!-- END label_2 -->
  162. <!-- BEGIN error -->
  163. <span class="help-inline">{error}</span>
  164. <!-- END error -->
  165. </div>
  166. </div>';
  167. $renderer->setElementTemplate($element_template);
  168. //Display a gray div in the buttons
  169. $button_element_template_simple = '<div class="form-actions">{label} {element}</div>';
  170. $renderer->setElementTemplate($button_element_template_simple, 'submit_in_actions');
  171. //Display a gray div in the buttons + makes the button available when scrolling
  172. $button_element_template_in_bottom = '<div class="form-actions bottom_actions">{label} {element}</div>';
  173. $renderer->setElementTemplate($button_element_template_in_bottom, 'submit_fixed_in_bottom');
  174. //When you want to group buttons use something like this
  175. /* $group = array();
  176. $group[] = $form->createElement('button', 'mark_all', get_lang('MarkAll'));
  177. $group[] = $form->createElement('button', 'unmark_all', get_lang('UnmarkAll'));
  178. $form->addGroup($group, 'buttons_in_action');
  179. */
  180. $renderer->setElementTemplate($button_element_template_simple, 'buttons_in_action');
  181. $button_element_template_simple_right = '<div class="form-actions"> <div class="pull-right">{label} {element}</div></div>';
  182. $renderer->setElementTemplate($button_element_template_simple_right, 'buttons_in_action_right');
  183. /*
  184. $renderer->setElementTemplate($button_element_template, 'submit_button');
  185. $renderer->setElementTemplate($button_element_template, 'submit');
  186. $renderer->setElementTemplate($button_element_template, 'button');
  187. *
  188. */
  189. }
  190. //Set Header template
  191. $renderer->setHeaderTemplate('<legend>{header}</legend>');
  192. //Set required field template
  193. HTML_QuickForm::setRequiredNote('<span class="form_required">*</span> <small>' . get_lang('ThisFieldIsRequired') . '</small>');
  194. $required_note_template = <<<EOT
  195. <div class="control-group">
  196. <div class="controls">{requiredNote}</div>
  197. </div>
  198. EOT;
  199. $renderer->setRequiredNoteTemplate($required_note_template);
  200. }
  201. /**
  202. * Adds a textfield to the form.
  203. * A trim-filter is attached to the field.
  204. * @param string $label The label for the form-element
  205. * @param string $name The element name
  206. * @param boolean $required (optional) Is the form-element required (default=true)
  207. * @param array $attributes (optional) List of attributes for the form-element
  208. */
  209. public function add_textfield($name, $label, $required = true, $attributes = array())
  210. {
  211. $this->addElement('text', $name, $label, $attributes);
  212. $this->applyFilter($name, 'trim');
  213. if ($required) {
  214. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  215. }
  216. }
  217. public function add_hidden($name, $value)
  218. {
  219. $this->addElement('hidden', $name, $value);
  220. }
  221. public function add_textarea($name, $label, $attributes = array())
  222. {
  223. $this->addElement('textarea', $name, $label, $attributes);
  224. }
  225. public function add_button($name, $label, $attributes = array())
  226. {
  227. $this->addElement('button', $name, $label, $attributes);
  228. }
  229. public function add_checkbox($name, $label, $trailer = '', $attributes = array())
  230. {
  231. $this->addElement('checkbox', $name, $label, $trailer, $attributes);
  232. }
  233. public function add_radio($name, $label, $options = '')
  234. {
  235. $group = array();
  236. foreach ($options as $key => $value) {
  237. $group[] = $this->createElement('radio', null, null, $value, $key);
  238. }
  239. $this->addGroup($group, $name, $label);
  240. }
  241. public function add_select($name, $label, $options = '', $attributes = array())
  242. {
  243. $this->addElement('select', $name, $label, $options, $attributes);
  244. }
  245. public function add_label($label, $text)
  246. {
  247. $this->addElement('label', $label, $text);
  248. }
  249. public function add_header($text)
  250. {
  251. $this->addElement('header', $text);
  252. }
  253. public function add_file($name, $label, $attributes = array())
  254. {
  255. $this->addElement('file', $name, $label, $attributes);
  256. }
  257. public function add_html($snippet)
  258. {
  259. $this->addElement('html', $snippet);
  260. }
  261. /**
  262. * Adds a HTML-editor to the form to fill in a title.
  263. * A trim-filter is attached to the field.
  264. * A HTML-filter is attached to the field (cleans HTML)
  265. * A rule is attached to check for unwanted HTML
  266. * @param string $label The label for the form-element
  267. * @param string $name The element name
  268. * @param boolean $required (optional) Is the form-element required (default=true)
  269. * @param boolean $full_page (optional) When it is true, the editor loads completed html code for a full page.
  270. * @param array $editor_config (optional) Configuration settings for the online editor.
  271. */
  272. public function add_html_editor($name, $label, $required = true, $full_page = false, $config = null)
  273. {
  274. $this->addElement('html_editor', $name, $label, 'rows="15" cols="80"', $config);
  275. $this->applyFilter($name, 'trim');
  276. $html_type = STUDENT_HTML;
  277. if (!empty($_SESSION['status'])) {
  278. $html_type = $_SESSION['status'] == COURSEMANAGER ? TEACHER_HTML : STUDENT_HTML;
  279. }
  280. if (is_array($config)) {
  281. if (isset($config['FullPage'])) {
  282. $full_page = is_bool($config['FullPage']) ? $config['FullPage'] : ($config['FullPage'] === 'true');
  283. } else {
  284. $config['FullPage'] = $full_page;
  285. }
  286. } else {
  287. $config = array('FullPage' => (bool) $full_page);
  288. }
  289. if ($full_page) {
  290. $html_type = isset($_SESSION['status']) && $_SESSION['status'] == COURSEMANAGER ? TEACHER_HTML_FULLPAGE : STUDENT_HTML_FULLPAGE;
  291. //First *filter* the HTML (markup, indenting, ...)
  292. //$this->applyFilter($name,'html_filter_teacher_fullpage');
  293. } else {
  294. //First *filter* the HTML (markup, indenting, ...)
  295. //$this->applyFilter($name,'html_filter_teacher');
  296. }
  297. if ($required) {
  298. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  299. }
  300. if ($full_page) {
  301. $el = $this->getElement($name);
  302. $el->fullPage = true;
  303. }
  304. // Add rule to check not-allowed HTML
  305. //$this->addRule($name, get_lang('SomeHTMLNotAllowed'), 'html', $html_type);
  306. }
  307. /**
  308. * Adds a datepicker element to the form
  309. * A rule is added to check if the date is a valid one
  310. * @param string $label The label for the form-element
  311. * @param string $name The element name
  312. */
  313. public function add_datepicker($name, $label)
  314. {
  315. $this->addElement('datepicker', $name, $label, array('form_name' => $this->getAttribute('name')));
  316. $this->_elements[$this->_elementIndex[$name]]->setLocalOption('minYear', 1900); // TODO: Now - 9 years
  317. $this->addRule($name, get_lang('InvalidDate'), 'date');
  318. }
  319. /**
  320. * Adds a datepickerdate element to the form
  321. * A rule is added to check if the date is a valid one
  322. * @param string $label The label for the form-element
  323. * @param string $name The element name
  324. */
  325. public function add_datepickerdate($name, $label)
  326. {
  327. $this->addElement('datepickerdate', $name, $label, array('form_name' => $this->getAttribute('name')));
  328. $this->_elements[$this->_elementIndex[$name]]->setLocalOption('minYear', 1900); // TODO: Now - 9 years
  329. $this->addRule($name, get_lang('InvalidDate'), 'date');
  330. }
  331. /**
  332. * Adds a timewindow element to the form.
  333. * 2 datepicker elements are added and a rule to check if the first date is
  334. * before the second one.
  335. * @param string $label The label for the form-element
  336. * @param string $name The element name
  337. */
  338. public function add_timewindow($name_1, $name_2, $label_1, $label_2)
  339. {
  340. $this->add_datepicker($name_1, $label_1);
  341. $this->add_datepicker($name_2, $label_2);
  342. $this->addRule(array($name_1, $name_2), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
  343. }
  344. /**
  345. * Adds a button to the form to add resources.
  346. */
  347. public function add_resource_button()
  348. {
  349. $group = array();
  350. $group[] = $this->createElement('static', 'add_resource_img', null, '<img src="' . api_get_path(WEB_IMG_PATH) . 'attachment.gif" alt="' . get_lang('Attachment') . '"/>');
  351. $group[] = $this->createElement('submit', 'add_resource', get_lang('Attachment'), 'class="link_alike"');
  352. $this->addGroup($group);
  353. }
  354. /**
  355. * Adds a progress bar to the form.
  356. *
  357. * Once the user submits the form, a progress bar (animated gif) is
  358. * displayed. The progress bar will disappear once the page has been
  359. * reloaded.
  360. *
  361. * @param int $delay (optional) The number of seconds between the moment the user
  362. * @param string $label (optional) Custom label to be shown
  363. * submits the form and the start of the progress bar.
  364. */
  365. public function add_progress_bar($delay = 2, $label = '')
  366. {
  367. if (empty($label)) {
  368. $label = get_lang('PleaseStandBy');
  369. }
  370. $this->with_progress_bar = true;
  371. $this->updateAttributes("onsubmit=\"javascript: myUpload.start('dynamic_div','" . api_get_path(WEB_IMG_PATH) . "progress_bar.gif','" . $label . "','" . $this->getAttribute('id') . "')\"");
  372. $this->addElement('html', '<script language="javascript" src="' . api_get_path(WEB_LIBRARY_PATH) . 'javascript/upload.js" type="text/javascript"></script>');
  373. $this->addElement('html', '<script type="text/javascript">var myUpload = new upload(' . (abs(intval($delay)) * 1000) . ');</script>');
  374. }
  375. /**
  376. * Uses new functions (php 5.2) for displaying real upload progress.
  377. * @param string $upload_id The value of the field UPLOAD_IDENTIFIER, the second parameter (XXX) of the $form->addElement('file', XXX) sentence
  378. * @param string $element_after The first element of the form (to place at first UPLOAD_IDENTIFIER)
  379. * @param int $delay (optional) The frequency of the xajax call
  380. * @param bool $wait_after_upload (optional)
  381. */
  382. public function add_real_progress_bar($upload_id, $element_after, $delay = 2, $wait_after_upload = false)
  383. {
  384. if (!function_exists('uploadprogress_get_info')) {
  385. $this->add_progress_bar($delay);
  386. return;
  387. }
  388. if (!class_exists('xajax')) {
  389. require_once api_get_path(LIBRARY_PATH) . 'xajax/xajax.inc.php';
  390. }
  391. $xajax_upload = new xajax(api_get_path(WEB_LIBRARY_PATH) . 'upload.xajax.php');
  392. $xajax_upload->registerFunction('updateProgress');
  393. // IMPORTANT : must be the first element of the form
  394. $el = $this->insertElementBefore(FormValidator::createElement('html', '<input type="hidden" name="UPLOAD_IDENTIFIER" value="' . $upload_id . '" />'), $element_after);
  395. $this->addElement('html', '<br />');
  396. // Add div-element where the progress bar is to be displayed
  397. $this->addElement('html', '
  398. <div id="dynamic_div_container" style="display:none">
  399. <div id="dynamic_div_label">' . get_lang('UploadFile') . '</div>
  400. <div id="dynamic_div_frame" style="width:214px; height:12px; border:1px solid grey; background-image:url(' . api_get_path(WEB_IMG_PATH) . 'real_upload_frame.gif);">
  401. <div id="dynamic_div_filled" style="width:0%;height:100%;background-image:url(' . api_get_path(WEB_IMG_PATH) . 'real_upload_step.gif);background-repeat:repeat-x;background-position:center;"></div>
  402. </div>
  403. </div>');
  404. if ($wait_after_upload) {
  405. $this->addElement('html', '
  406. <div id="dynamic_div_waiter_container" style="display:none">
  407. <div id="dynamic_div_waiter_label">
  408. ' . get_lang('SlideshowConversion') . '
  409. </div>
  410. <div id="dynamic_div_waiter_frame">
  411. <img src="' . api_get_path(WEB_IMG_PATH) . 'real_upload_frame.gif" />
  412. </div>
  413. </div>
  414. ');
  415. }
  416. // Get the xajax code
  417. $this->addElement('html', $xajax_upload->getJavascript(api_get_path(WEB_LIBRARY_PATH) . 'xajax'));
  418. // Get the upload code
  419. $this->addElement('html', '<script language="javascript" src="' . api_get_path(WEB_LIBRARY_PATH) . 'javascript/upload.js" type="text/javascript"></script>');
  420. $this->addElement('html', '<script type="text/javascript">var myUpload = new upload(' . (abs(intval($delay)) * 1000) . ');</script>');
  421. if (!$wait_after_upload) {
  422. $wait_after_upload = 0;
  423. }
  424. // Add the upload event
  425. $this->updateAttributes("onsubmit=\"javascript: myUpload.startRealUpload('dynamic_div','" . $upload_id . "','" . $this->getAttribute('id') . "'," . $wait_after_upload . ")\"");
  426. }
  427. /**
  428. * This function has been created for avoiding changes directly within QuickForm class.
  429. * When we use it, the element is threated as 'required' to be dealt during validation.
  430. * @param array $element The array of elements
  431. * @param string $message The message displayed
  432. */
  433. public function add_multiple_required_rule($elements, $message)
  434. {
  435. $this->_required[] = $elements[0];
  436. $this->addRule($elements, $message, 'multiple_required');
  437. }
  438. /**
  439. * Displays the form.
  440. * If an element in the form didn't validate, an error message is showed
  441. * asking the user to complete the form.
  442. */
  443. public function display()
  444. {
  445. echo $this->return_form();
  446. }
  447. /**
  448. * Returns the HTML code of the form.
  449. * If an element in the form didn't validate, an error message is showed
  450. * asking the user to complete the form.
  451. *
  452. * @return string $return_value HTML code of the form
  453. *
  454. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, august 2006
  455. */
  456. public function return_form()
  457. {
  458. $error = false;
  459. foreach ($this->_elements as $element) {
  460. if (!is_null(parent::getElementError($element->getName()))) {
  461. $error = true;
  462. break;
  463. }
  464. }
  465. $return_value = '';
  466. if ($error) {
  467. $return_value = Display::return_message(get_lang('FormHasErrorsPleaseComplete'), 'warning');
  468. }
  469. $return_value .= parent::toHtml();
  470. // Add div-element which is to hold the progress bar
  471. if (isset($this->with_progress_bar) && $this->with_progress_bar) {
  472. $return_value .= '<div id="dynamic_div" style="display:block; margin-left:40%; margin-top:10px; height:50px;"></div>';
  473. }
  474. return $return_value;
  475. }
  476. }
  477. // @todo remove this!
  478. /**
  479. * Cleans HTML text
  480. * @param string $html HTML to clean
  481. * @param int $mode (optional)
  482. * @return string The cleaned HTML
  483. */
  484. function html_filter($html, $mode = NO_HTML)
  485. {
  486. require_once api_get_path(LIBRARY_PATH) . 'formvalidator/Rule/HTML.php';
  487. $allowed_tags = HTML_QuickForm_Rule_HTML::get_allowed_tags($mode);
  488. $cleaned_html = kses($html, $allowed_tags);
  489. return $cleaned_html;
  490. }
  491. function html_filter_teacher($html)
  492. {
  493. return html_filter($html, TEACHER_HTML);
  494. }
  495. function html_filter_student($html)
  496. {
  497. return html_filter($html, STUDENT_HTML);
  498. }
  499. function html_filter_teacher_fullpage($html)
  500. {
  501. return html_filter($html, TEACHER_HTML_FULLPAGE);
  502. }
  503. function html_filter_student_fullpage($html)
  504. {
  505. return html_filter($html, STUDENT_HTML_FULLPAGE);
  506. }