Source for file AttrValidator.php

Documentation is available at AttrValidator.php

  1. <?php
  2.  
  3. /**
  4.  * Validates the attributes of a token. Doesn't manage required attributes
  5.  * very well. The only reason we factored this out was because RemoveForeignElements
  6.  * also needed it besides ValidateAttributes.
  7.  */
  8. {
  9.     
  10.     /**
  11.      * Validates the attributes of a token, returning a modified token
  12.      * that has valid tokens
  13.      * @param $token Reference to token to validate. We require a reference
  14.      *      because the operation this class performs on the token are
  15.      *      not atomic, so the context CurrentToken to be updated
  16.      *      throughout
  17.      * @param $config Instance of HTMLPurifier_Config
  18.      * @param $context Instance of HTMLPurifier_Context
  19.      */
  20.     public function validateToken(&$token&$config$context{
  21.             
  22.         $definition $config->getHTMLDefinition();
  23.         $e =$context->get('ErrorCollector'true);
  24.         
  25.         // initialize IDAccumulator if necessary
  26.         $ok =$context->get('IDAccumulator'true);
  27.         if (!$ok{
  28.             $id_accumulator HTMLPurifier_IDAccumulator::build($config$context);
  29.             $context->register('IDAccumulator'$id_accumulator);
  30.         }
  31.         
  32.         // initialize CurrentToken if necessary
  33.         $current_token =$context->get('CurrentToken'true);
  34.         if (!$current_token$context->register('CurrentToken'$token);
  35.         
  36.         if (
  37.           !$token instanceof HTMLPurifier_Token_Start &&
  38.           !$token instanceof HTMLPurifier_Token_Empty
  39.         return $token;
  40.         
  41.         // create alias to global definition array, see also $defs
  42.         // DEFINITION CALL
  43.         $d_defs $definition->info_global_attr;
  44.         
  45.         // don't update token until the very end, to ensure an atomic update
  46.         $attr $token->attr;
  47.         
  48.         // do global transformations (pre)
  49.         // nothing currently utilizes this
  50.         foreach ($definition->info_attr_transform_pre as $transform{
  51.             $attr $transform->transform($o $attr$config$context);
  52.             if ($e && ($attr != $o)) $e->send(E_NOTICE'AttrValidator: Attributes transformed'$o$attr);
  53.         }
  54.         
  55.         // do local transformations only applicable to this element (pre)
  56.         // ex. <p align="right"> to <p style="text-align:right;">
  57.         foreach ($definition->info[$token->name]->attr_transform_pre as $transform{
  58.             $attr $transform->transform($o $attr$config$context);
  59.             if ($e && ($attr != $o)) $e->send(E_NOTICE'AttrValidator: Attributes transformed'$o$attr);
  60.         }
  61.         
  62.         // create alias to this element's attribute definition array, see
  63.         // also $d_defs (global attribute definition array)
  64.         // DEFINITION CALL
  65.         $defs $definition->info[$token->name]->attr;
  66.         
  67.         $attr_key false;
  68.         $context->register('CurrentAttr'$attr_key);
  69.         
  70.         // iterate through all the attribute keypairs
  71.         // Watch out for name collisions: $key has previously been used
  72.         foreach ($attr as $attr_key => $value{
  73.             
  74.             // call the definition
  75.             if isset($defs[$attr_key]) ) {
  76.                 // there is a local definition defined
  77.                 if ($defs[$attr_key=== false{
  78.                     // We've explicitly been told not to allow this element.
  79.                     // This is usually when there's a global definition
  80.                     // that must be overridden.
  81.                     // Theoretically speaking, we could have a
  82.                     // AttrDef_DenyAll, but this is faster!
  83.                     $result false;
  84.                 else {
  85.                     // validate according to the element's definition
  86.                     $result $defs[$attr_key]->validate(
  87.                                     $value$config$context
  88.                                );
  89.                 }
  90.             elseif isset($d_defs[$attr_key]) ) {
  91.                 // there is a global definition defined, validate according
  92.                 // to the global definition
  93.                 $result $d_defs[$attr_key]->validate(
  94.                                 $value$config$context
  95.                            );
  96.             else {
  97.                 // system never heard of the attribute? DELETE!
  98.                 $result false;
  99.             }
  100.             
  101.             // put the results into effect
  102.             if ($result === false || $result === null{
  103.                 // this is a generic error message that should replaced
  104.                 // with more specific ones when possible
  105.                 if ($e$e->send(E_ERROR'AttrValidator: Attribute removed');
  106.                 
  107.                 // remove the attribute
  108.                 unset($attr[$attr_key]);
  109.             elseif (is_string($result)) {
  110.                 // generally, if a substitution is happening, there
  111.                 // was some sort of implicit correction going on. We'll
  112.                 // delegate it to the attribute classes to say exactly what.
  113.                 
  114.                 // simple substitution
  115.                 $attr[$attr_key$result;
  116.             }
  117.             
  118.             // we'd also want slightly more complicated substitution
  119.             // involving an array as the return value,
  120.             // although we're not sure how colliding attributes would
  121.             // resolve (certain ones would be completely overriden,
  122.             // others would prepend themselves).
  123.         }
  124.         
  125.         $context->destroy('CurrentAttr');
  126.         
  127.         // post transforms
  128.         
  129.         // global (error reporting untested)
  130.         foreach ($definition->info_attr_transform_post as $transform{
  131.             $attr $transform->transform($o $attr$config$context);
  132.             if ($e && ($attr != $o)) $e->send(E_NOTICE'AttrValidator: Attributes transformed'$o$attr);
  133.         }
  134.         
  135.         // local (error reporting untested)
  136.         foreach ($definition->info[$token->name]->attr_transform_post as $transform{
  137.             $attr $transform->transform($o $attr$config$context);
  138.             if ($e && ($attr != $o)) $e->send(E_NOTICE'AttrValidator: Attributes transformed'$o$attr);
  139.         }
  140.         
  141.         $token->attr $attr;
  142.         
  143.         // destroy CurrentToken if we made it ourselves
  144.         if (!$current_token$context->destroy('CurrentToken');
  145.         
  146.     }
  147.     
  148.     
  149. }

Documentation generated on Thu, 19 Jun 2008 18:48:50 -0400 by phpDocumentor 1.4.2