Source for file ErrorCollector.php

Documentation is available at ErrorCollector.php

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

Documentation generated on Thu, 19 Jun 2008 18:49:10 -0400 by phpDocumentor 1.4.2