2007-06-18 03:05:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/Generator.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error collection class that enables HTML Purifier to report HTML
|
|
|
|
* problems back to the user
|
|
|
|
*/
|
|
|
|
class HTMLPurifier_ErrorCollector
|
|
|
|
{
|
|
|
|
|
|
|
|
var $errors = array();
|
2007-06-25 00:48:26 +00:00
|
|
|
var $locale;
|
2007-06-25 01:08:57 +00:00
|
|
|
var $context;
|
2007-06-25 00:48:26 +00:00
|
|
|
|
2007-06-25 01:08:57 +00:00
|
|
|
function HTMLPurifier_ErrorCollector(&$context) {
|
|
|
|
$this->locale =& $context->get('Locale');
|
|
|
|
$this->context =& $context;
|
|
|
|
}
|
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 $line Integer line number, or HTMLPurifier_Token that caused error
|
|
|
|
* @param $severity int Error severity, PHP error style (don't use E_USER_)
|
|
|
|
* @param $msg string Error message text
|
2007-06-18 03:05:18 +00:00
|
|
|
*/
|
2007-06-25 01:08:57 +00:00
|
|
|
function send($severity, $msg, $args = array()) {
|
|
|
|
if (func_num_args() == 2) {
|
|
|
|
$msg = $this->locale->getMessage($msg);
|
|
|
|
} else {
|
|
|
|
// setup one-based array if necessary
|
|
|
|
if (!is_array($args)) {
|
|
|
|
$args = func_get_args();
|
|
|
|
array_shift($args);
|
|
|
|
unset($args[0]);
|
|
|
|
}
|
2007-06-25 00:48:26 +00:00
|
|
|
$msg = $this->locale->formatMessage($msg, $args);
|
|
|
|
}
|
2007-06-25 01:08:57 +00:00
|
|
|
|
|
|
|
$line = $this->context->get('CurrentLine', true);
|
|
|
|
$token = $this->context->get('CurrentToken', true);
|
|
|
|
$attr = $this->context->get('CurrentAttr', true);
|
|
|
|
|
2007-06-25 00:48:26 +00:00
|
|
|
$this->errors[] = array($line, $severity, $msg);
|
2007-06-18 03:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves raw error data for custom formatter to use
|
|
|
|
* @param List of arrays in format of array(Error message text,
|
|
|
|
* token that caused error, tokens surrounding token)
|
|
|
|
*/
|
|
|
|
function getRaw() {
|
|
|
|
return $this->errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default HTML formatting implementation for error messages
|
|
|
|
* @param $config Configuration array, vital for HTML output nature
|
|
|
|
*/
|
2007-06-25 00:48:26 +00:00
|
|
|
function getHTMLFormatted($config) {
|
2007-06-18 03:05:18 +00:00
|
|
|
$generator = new HTMLPurifier_Generator();
|
2007-06-25 00:48:26 +00:00
|
|
|
$generator->generateFromTokens(array(), $config, $this->context); // initialize
|
2007-06-18 03:05:18 +00:00
|
|
|
$ret = array();
|
|
|
|
|
|
|
|
$errors = $this->errors;
|
|
|
|
|
|
|
|
// sort error array by line
|
|
|
|
if ($config->get('Core', 'MaintainLineNumbers')) {
|
|
|
|
$lines = array();
|
2007-06-25 00:48:26 +00:00
|
|
|
foreach ($errors as $error) {
|
|
|
|
$lines[] = $error[0];
|
|
|
|
}
|
2007-06-18 03:05:18 +00:00
|
|
|
array_multisort($lines, SORT_ASC, $errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($errors as $error) {
|
2007-06-25 00:48:26 +00:00
|
|
|
list($line, $severity, $msg) = $error;
|
2007-06-24 23:20:35 +00:00
|
|
|
$string = '';
|
2007-06-25 00:48:26 +00:00
|
|
|
$string .= $this->locale->getErrorName($severity) . ': ';
|
2007-06-24 23:20:35 +00:00
|
|
|
$string .= $generator->escape($msg);
|
2007-06-25 00:48:26 +00:00
|
|
|
if ($line) {
|
|
|
|
$string .= $this->locale->formatMessage(
|
|
|
|
'ErrorCollector: At line', array('line' => $line));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|