3 require_once 'HTMLPurifier/Generator.php';
6 * Error collection class that enables HTML Purifier to report HTML
7 * problems back to the user
9 class HTMLPurifier_ErrorCollector
12 var $errors = array();
17 function HTMLPurifier_ErrorCollector(&$context) {
18 $this->locale
=& $context->get('Locale');
19 $this->generator
=& $context->get('Generator');
20 $this->context
=& $context;
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) {
32 if (func_num_args() > 2) {
33 $args = func_get_args();
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
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];
53 $msg = $this->locale
->getMessage($msg);
55 $msg = $this->locale
->formatMessage($msg, $args);
58 if (!empty($subst)) $msg = strtr($msg, $subst);
60 $this->errors
[] = array($line, $severity, $msg);
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)
73 * Default HTML formatting implementation for error messages
74 * @param $config Configuration array, vital for HTML output nature
76 function getHTMLFormatted($config) {
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) {
86 $original_order = array();
87 foreach ($errors as $i => $error) {
88 $has_line[] = (int) (bool) $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;
98 $string .= '<strong>' . $this->locale
->getErrorName($severity) . '</strong>: ';
99 $string .= $this->generator
->escape($msg);
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));
109 if (empty($errors)) {
110 return '<p>' . $this->locale
->getMessage('ErrorCollector: No errors') . '</p>';
112 return '<ul><li>' . implode('</li><li>', $ret) . '</li></ul>';