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