2007-06-18 03:05:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error collection class that enables HTML Purifier to report HTML
|
|
|
|
* problems back to the user
|
|
|
|
*/
|
|
|
|
class HTMLPurifier_ErrorCollector
|
|
|
|
{
|
|
|
|
|
2008-08-16 04:41:34 +00:00
|
|
|
/**
|
|
|
|
* Identifiers for the returned error array. These are purposely numeric
|
|
|
|
* so list() can be used.
|
|
|
|
*/
|
|
|
|
const LINENO = 0;
|
|
|
|
const SEVERITY = 1;
|
|
|
|
const MESSAGE = 2;
|
|
|
|
const CHILDREN = 3;
|
|
|
|
|
|
|
|
protected $errors;
|
|
|
|
protected $_current;
|
|
|
|
protected $_stacks = array(array());
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $locale;
|
|
|
|
protected $generator;
|
|
|
|
protected $context;
|
2007-06-25 00:48:26 +00:00
|
|
|
|
2008-01-05 00:10:43 +00:00
|
|
|
public function __construct($context) {
|
2007-11-25 02:24:39 +00:00
|
|
|
$this->locale =& $context->get('Locale');
|
2008-04-23 02:40:17 +00:00
|
|
|
$this->context = $context;
|
2008-08-16 04:41:34 +00:00
|
|
|
$this->_current =& $this->_stacks[0];
|
2008-08-19 02:13:58 +00:00
|
|
|
$this->errors =& $this->_stacks[0];
|
2007-06-25 01:08:57 +00:00
|
|
|
}
|
2007-06-18 03:05:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends an error message to the collector for later use
|
2007-06-25 00:48:26 +00:00
|
|
|
* @param $severity int Error severity, PHP error style (don't use E_USER_)
|
|
|
|
* @param $msg string Error message text
|
2008-08-16 04:41:34 +00:00
|
|
|
* @param $subst1 string First substitution for $msg
|
|
|
|
* @param $subst2 string ...
|
2007-06-18 03:05:18 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function send($severity, $msg) {
|
2007-06-26 19:33:37 +00:00
|
|
|
|
2007-06-27 02:03:15 +00:00
|
|
|
$args = array();
|
|
|
|
if (func_num_args() > 2) {
|
2007-06-26 19:33:37 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
array_shift($args);
|
|
|
|
unset($args[0]);
|
2007-06-25 00:48:26 +00:00
|
|
|
}
|
2007-06-25 01:08:57 +00:00
|
|
|
|
|
|
|
$token = $this->context->get('CurrentToken', true);
|
2007-06-25 01:56:00 +00:00
|
|
|
$line = $token ? $token->line : $this->context->get('CurrentLine', true);
|
2007-06-25 01:08:57 +00:00
|
|
|
$attr = $this->context->get('CurrentAttr', true);
|
2007-06-26 19:33:37 +00:00
|
|
|
|
|
|
|
// perform special substitutions, also add custom parameters
|
|
|
|
$subst = array();
|
|
|
|
if (!is_null($token)) {
|
|
|
|
$args['CurrentToken'] = $token;
|
2007-06-26 02:49:21 +00:00
|
|
|
}
|
2007-06-26 19:33:37 +00:00
|
|
|
if (!is_null($attr)) {
|
|
|
|
$subst['$CurrentAttr.Name'] = $attr;
|
|
|
|
if (isset($token->attr[$attr])) $subst['$CurrentAttr.Value'] = $token->attr[$attr];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($args)) {
|
|
|
|
$msg = $this->locale->getMessage($msg);
|
|
|
|
} else {
|
|
|
|
$msg = $this->locale->formatMessage($msg, $args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($subst)) $msg = strtr($msg, $subst);
|
|
|
|
|
2008-08-16 04:41:34 +00:00
|
|
|
// (numerically indexed)
|
|
|
|
$this->_current[] = array(
|
|
|
|
self::LINENO => $line,
|
|
|
|
self::SEVERITY => $severity,
|
|
|
|
self::MESSAGE => $msg,
|
|
|
|
self::CHILDREN => array()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begins the collection of a number of sub-errors. This is useful if you
|
|
|
|
* are entering a function that may generate errors, but you are able
|
|
|
|
* to detect the overall state afterwards.
|
|
|
|
*/
|
|
|
|
public function start() {
|
|
|
|
$this->_stacks[] = array();
|
|
|
|
$this->_resetCurrent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Terminates the collection of sub-errors, interface is otherwise identical
|
|
|
|
* to send(). Any sub-errors will be registered as children (3) to this
|
|
|
|
* error.
|
|
|
|
*
|
|
|
|
* @param $severity int Error severity
|
|
|
|
* @param $msg string Error message text
|
|
|
|
* @param $subst1 string First substitution for $msg
|
|
|
|
* @param $subst2 string ...
|
|
|
|
*
|
|
|
|
* @note If end() is called with no parameters, it is quiet unless there
|
|
|
|
* were sub-errors.
|
|
|
|
*/
|
|
|
|
public function end() {
|
|
|
|
$stack = array_pop($this->_stacks);
|
|
|
|
$this->_resetCurrent();
|
|
|
|
$args = func_get_args();
|
|
|
|
if ($args) {
|
|
|
|
call_user_func_array(array($this, 'send'), $args);
|
|
|
|
} elseif ($stack) {
|
|
|
|
$this->send(E_NOTICE, 'ErrorCollector: Incidental errors');
|
|
|
|
}
|
|
|
|
if ($stack) {
|
|
|
|
$this->_current[count($this->_current) - 1][3] = $stack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the _current member variable to the top of the stacks; i.e.
|
|
|
|
* the active set of errors being collected.
|
|
|
|
*/
|
|
|
|
protected function _resetCurrent() {
|
|
|
|
$this->_current =& $this->_stacks[count($this->_stacks) - 1];
|
2007-06-18 03:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves raw error data for custom formatter to use
|
2008-08-16 04:41:34 +00:00
|
|
|
* @param List of arrays in format of array(line of error,
|
|
|
|
* error severity, error message,
|
|
|
|
* recursive sub-errors array)
|
2007-06-18 03:05:18 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function getRaw() {
|
2007-06-18 03:05:18 +00:00
|
|
|
return $this->errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default HTML formatting implementation for error messages
|
|
|
|
* @param $config Configuration array, vital for HTML output nature
|
2008-08-16 04:41:34 +00:00
|
|
|
* @param $errors Errors array to display; used for recursion.
|
2007-06-18 03:05:18 +00:00
|
|
|
*/
|
2008-08-16 04:41:34 +00:00
|
|
|
public function getHTMLFormatted($config, $errors = null) {
|
2007-06-18 03:05:18 +00:00
|
|
|
$ret = array();
|
|
|
|
|
2008-08-19 02:13:58 +00:00
|
|
|
$generator = new HTMLPurifier_Generator($config, $this->context);
|
2008-08-16 04:41:34 +00:00
|
|
|
if ($errors === null) $errors = $this->errors;
|
2007-06-18 03:05:18 +00:00
|
|
|
|
|
|
|
// sort error array by line
|
2007-06-25 23:22:35 +00:00
|
|
|
// line numbers are enabled if they aren't explicitly disabled
|
|
|
|
if ($config->get('Core', 'MaintainLineNumbers') !== false) {
|
2007-06-26 19:33:37 +00:00
|
|
|
$has_line = array();
|
|
|
|
$lines = array();
|
2007-06-26 02:49:21 +00:00
|
|
|
$original_order = array();
|
|
|
|
foreach ($errors as $i => $error) {
|
2008-08-16 04:41:34 +00:00
|
|
|
$has_line[] = (int) (bool) $error[self::LINENO];
|
|
|
|
$lines[] = $error[self::LINENO];
|
2007-06-26 02:49:21 +00:00
|
|
|
$original_order[] = $i;
|
2007-06-25 00:48:26 +00:00
|
|
|
}
|
2007-06-26 19:33:37 +00:00
|
|
|
array_multisort($has_line, SORT_DESC, $lines, SORT_ASC, $original_order, SORT_ASC, $errors);
|
2007-06-18 03:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($errors as $error) {
|
2008-08-16 04:41:34 +00:00
|
|
|
list($line, $severity, $msg, $children) = $error;
|
2007-06-24 23:20:35 +00:00
|
|
|
$string = '';
|
2007-06-27 02:03:15 +00:00
|
|
|
$string .= '<strong>' . $this->locale->getErrorName($severity) . '</strong>: ';
|
2008-08-19 02:13:58 +00:00
|
|
|
$string .= $generator->escape($msg);
|
2007-06-25 00:48:26 +00:00
|
|
|
if ($line) {
|
2007-06-26 02:49:21 +00:00
|
|
|
// have javascript link generation that causes
|
|
|
|
// textarea to skip to the specified line
|
2007-06-25 00:48:26 +00:00
|
|
|
$string .= $this->locale->formatMessage(
|
|
|
|
'ErrorCollector: At line', array('line' => $line));
|
2007-06-18 03:05:18 +00:00
|
|
|
}
|
2008-08-16 04:41:34 +00:00
|
|
|
if ($children) {
|
|
|
|
$string .= $this->getHTMLFormatted($config, $children);
|
|
|
|
}
|
2007-06-18 03:05:18 +00:00
|
|
|
$ret[] = $string;
|
|
|
|
}
|
2007-06-25 00:48:26 +00:00
|
|
|
|
|
|
|
if (empty($errors)) {
|
|
|
|
return '<p>' . $this->locale->getMessage('ErrorCollector: No errors') . '</p>';
|
|
|
|
} else {
|
|
|
|
return '<ul><li>' . implode('</li><li>', $ret) . '</li></ul>';
|
|
|
|
}
|
|
|
|
|
2007-06-18 03:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|