"MDL-12304, fix double text"
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / ErrorCollector.php
blob70ac5d9a00e3b0996ee612bbfd763e2d79d43d98
1 <?php
3 require_once 'HTMLPurifier/Generator.php';
5 /**
6 * Error collection class that enables HTML Purifier to report HTML
7 * problems back to the user
8 */
9 class HTMLPurifier_ErrorCollector
12 var $errors = array();
13 var $locale;
14 var $generator;
15 var $context;
17 function HTMLPurifier_ErrorCollector(&$context) {
18 $this->locale =& $context->get('Locale');
19 $this->generator =& $context->get('Generator');
20 $this->context =& $context;
23 /**
24 * Sends an error message to the collector for later use
25 * @param $line Integer line number, or HTMLPurifier_Token that caused error
26 * @param $severity int Error severity, PHP error style (don't use E_USER_)
27 * @param $msg string Error message text
29 function send($severity, $msg) {
31 $args = array();
32 if (func_num_args() > 2) {
33 $args = func_get_args();
34 array_shift($args);
35 unset($args[0]);
38 $token = $this->context->get('CurrentToken', true);
39 $line = $token ? $token->line : $this->context->get('CurrentLine', true);
40 $attr = $this->context->get('CurrentAttr', true);
42 // perform special substitutions, also add custom parameters
43 $subst = array();
44 if (!is_null($token)) {
45 $args['CurrentToken'] = $token;
47 if (!is_null($attr)) {
48 $subst['$CurrentAttr.Name'] = $attr;
49 if (isset($token->attr[$attr])) $subst['$CurrentAttr.Value'] = $token->attr[$attr];
52 if (empty($args)) {
53 $msg = $this->locale->getMessage($msg);
54 } else {
55 $msg = $this->locale->formatMessage($msg, $args);
58 if (!empty($subst)) $msg = strtr($msg, $subst);
60 $this->errors[] = array($line, $severity, $msg);
63 /**
64 * Retrieves raw error data for custom formatter to use
65 * @param List of arrays in format of array(Error message text,
66 * token that caused error, tokens surrounding token)
68 function getRaw() {
69 return $this->errors;
72 /**
73 * Default HTML formatting implementation for error messages
74 * @param $config Configuration array, vital for HTML output nature
76 function getHTMLFormatted($config) {
77 $ret = array();
79 $errors = $this->errors;
81 // sort error array by line
82 // line numbers are enabled if they aren't explicitly disabled
83 if ($config->get('Core', 'MaintainLineNumbers') !== false) {
84 $has_line = array();
85 $lines = array();
86 $original_order = array();
87 foreach ($errors as $i => $error) {
88 $has_line[] = (int) (bool) $error[0];
89 $lines[] = $error[0];
90 $original_order[] = $i;
92 array_multisort($has_line, SORT_DESC, $lines, SORT_ASC, $original_order, SORT_ASC, $errors);
95 foreach ($errors as $error) {
96 list($line, $severity, $msg) = $error;
97 $string = '';
98 $string .= '<strong>' . $this->locale->getErrorName($severity) . '</strong>: ';
99 $string .= $this->generator->escape($msg);
100 if ($line) {
101 // have javascript link generation that causes
102 // textarea to skip to the specified line
103 $string .= $this->locale->formatMessage(
104 'ErrorCollector: At line', array('line' => $line));
106 $ret[] = $string;
109 if (empty($errors)) {
110 return '<p>' . $this->locale->getMessage('ErrorCollector: No errors') . '</p>';
111 } else {
112 return '<ul><li>' . implode('</li><li>', $ret) . '</li></ul>';