get() публичный Метод

Retrieves a variable reference from the context.
public get ( string $name, boolean $ignore_error = false ) : mixed
$name string String name
$ignore_error boolean Boolean whether or not to ignore error
Результат mixed
Пример #1
0
 /**
  * @param HTMLPurifier_URI $uri
  * @param HTMLPurifier_Config $config
  * @param HTMLPurifier_Context $context
  * @return bool
  */
 public function filter(&$uri, $config, $context)
 {
     // check if filter not applicable
     if (!$config->get('HTML.SafeIframe')) {
         return true;
     }
     // check if the filter should actually trigger
     if (!$context->get('EmbeddedURI', true)) {
         return true;
     }
     $token = $context->get('CurrentToken', true);
     if (!($token && $token->name == 'iframe')) {
         return true;
     }
     // check if we actually have some whitelists enabled
     if ($this->regexp === null) {
         return false;
     }
     // actually check the whitelists
     if (!preg_match($this->regexp, $uri->toString())) {
         return false;
     }
     // Make sure that if we're an HTTPS site, the iframe is also HTTPS
     if (is_https() && $uri->scheme == 'http') {
         // Convert it to a protocol-relative URL
         $uri->scheme = null;
     }
     return $uri;
 }
Пример #2
0
 /**
  * @param HTMLPurifier_Context $context
  */
 public function __construct($context)
 {
     $this->locale =& $context->get('Locale');
     $this->context = $context;
     $this->_current =& $this->_stacks[0];
     $this->errors =& $this->_stacks[0];
 }
 /**
  * @param HTMLPurifier_URI $uri
  * @param HTMLPurifier_Config $config
  * @param HTMLPurifier_Context $context
  * @return bool
  */
 public function filter(&$uri, $config, $context)
 {
     if (!$context->get('EmbeddedURI', true)) {
         return true;
     }
     return parent::filter($uri, $config, $context);
 }
Пример #4
0
 /**
  * @param HTMLPurifier_Node[] $children
  * @param HTMLPurifier_Config $config
  * @param HTMLPurifier_Context $context
  * @return bool
  */
 public function validateChildren($children, $config, $context)
 {
     if ($context->get('IsInline') === false) {
         return $this->block->validateChildren($children, $config, $context);
     } else {
         return $this->inline->validateChildren($children, $config, $context);
     }
 }
Пример #5
0
 public function testNull()
 {
     $context = new HTMLPurifier_Context();
     $var = NULL;
     $context->register('var', $var);
     $this->assertNull($context->get('var'));
     $context->destroy('var');
 }
Пример #6
0
 /**
  * Checks if CurrentToken is set and equal to $this->element
  * @param string $string
  * @param HTMLPurifier_Config $config
  * @param HTMLPurifier_Context $context
  * @return bool|string
  */
 public function validate($string, $config, $context)
 {
     $token = $context->get('CurrentToken', true);
     if ($token && $token->name == $this->element) {
         return false;
     }
     return $this->def->validate($string, $config, $context);
 }
Пример #7
0
 /**
  * @param string $string
  * @param HTMLPurifier_Config $config
  * @param HTMLPurifier_Context $context
  * @return bool|string
  */
 public function validate($string, $config, $context)
 {
     $token = $context->get('CurrentToken', true);
     if (!$token || $token->name !== $this->tag) {
         return $this->withoutTag->validate($string, $config, $context);
     } else {
         return $this->withTag->validate($string, $config, $context);
     }
 }
Пример #8
0
 /**
  * @param string $id
  * @param HTMLPurifier_Config $config
  * @param HTMLPurifier_Context $context
  * @return bool|string
  */
 public function validate($id, $config, $context)
 {
     if (!$this->selector && !$config->get('Attr.EnableID')) {
         return false;
     }
     $id = trim($id);
     // trim it first
     if ($id === '') {
         return false;
     }
     $prefix = $config->get('Attr.IDPrefix');
     if ($prefix !== '') {
         $prefix .= $config->get('Attr.IDPrefixLocal');
         // prevent re-appending the prefix
         if (strpos($id, $prefix) !== 0) {
             $id = $prefix . $id;
         }
     } elseif ($config->get('Attr.IDPrefixLocal') !== '') {
         trigger_error('%Attr.IDPrefixLocal cannot be used unless ' . '%Attr.IDPrefix is set', E_USER_WARNING);
     }
     if (!$this->selector) {
         $id_accumulator =& $context->get('IDAccumulator');
         if (isset($id_accumulator->ids[$id])) {
             return false;
         }
     }
     // we purposely avoid using regex, hopefully this is faster
     if ($config->get('Attr.ID.HTML5') === true) {
         if (preg_match('/[\\t\\n\\x0b\\x0c ]/', $id)) {
             return false;
         }
     } else {
         if (ctype_alpha($id)) {
             // OK
         } else {
             if (!ctype_alpha(@$id[0])) {
                 return false;
             }
             // primitive style of regexps, I suppose
             $trim = trim($id, 'A..Za..z0..9:-._');
             if ($trim !== '') {
                 return false;
             }
         }
     }
     $regexp = $config->get('Attr.IDBlacklistRegexp');
     if ($regexp && preg_match($regexp, $id)) {
         return false;
     }
     if (!$this->selector) {
         $id_accumulator->add($id);
     }
     // if no change was made to the ID, return the result
     // else, return the new id if stripping whitespace made it
     //     valid, or return false.
     return $id;
 }
Пример #9
0
 /**
  * @param HTMLPurifier_URI $uri
  * @param HTMLPurifier_Config $config
  * @param HTMLPurifier_Context $context
  * @return bool
  */
 public function filter(&$uri, $config, $context)
 {
     // check if filter not applicable
     if (!$config->get('HTML.SafeIframe')) {
         return true;
     }
     // check if the filter should actually trigger
     if (!$context->get('EmbeddedURI', true)) {
         return true;
     }
     $token = $context->get('CurrentToken', true);
     if (!($token && $token->name == 'iframe')) {
         return true;
     }
     // check if we actually have some whitelists enabled
     if ($this->regexp === null) {
         return false;
     }
     // actually check the whitelists
     return preg_match($this->regexp, $uri->toString());
 }
 /**
  * filter
  * 
  * @param HTMLPurifier_URI $uri
  * @param HTMLPurifier_Config $config
  * @param HTMLPurifier_Context $context
  * @return boolean
  */
 public function filter(&$uri, $config, $context)
 {
     $result = TRUE;
     $token = $context->get('CurrentToken', true);
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' URI: ' . var_export($uri, TRUE) . ' ' . ' TOKEN: ' . var_export($token, TRUE));
     }
     if ($uri->host) {
         $result = $this->_checkExternalUrl($uri, $token);
     }
     return $result;
 }
Пример #11
0
 /**
  * @param HTMLPurifier_URI     $uri
  * @param HTMLPurifier_Config  $config
  * @param HTMLPurifier_Context $context
  *
  * @return bool
  */
 public function filter(&$uri, $config, $context)
 {
     // skip non-resource URIs
     if (!$context->get('EmbeddedURI', true)) {
         return true;
     }
     //if(empty($this->allowed)) return false;
     if (!empty($uri->scheme) && strtolower($uri->scheme) != 'http' && strtolower($uri->scheme) != 'https') {
         // do not touch non-HTTP URLs
         return true;
     }
     // relative URLs permitted since email templates use it
     // if(empty($uri->host)) return false;
     // allow URLs with no query
     if (empty($uri->query)) {
         return true;
     }
     // allow URLs for known good hosts
     foreach ($this->allowed as $allow) {
         // must be equal to our domain or subdomain of our domain
         if ($uri->host == $allow || substr($uri->host, -(strlen($allow) + 1)) == ".{$allow}") {
             return true;
         }
     }
     // Here we try to block URLs that may be used for nasty XSRF stuff by
     // referring back to Sugar URLs
     // allow URLs that don't start with /? or /index.php?
     if (!empty($uri->path) && $uri->path != '/') {
         $lpath = strtolower($uri->path);
         if (substr($lpath, -10) != '/index.php' && $lpath != 'index.php') {
             return true;
         }
     }
     $query_items = [];
     parse_str(from_html($uri->query), $query_items);
     // weird query, probably harmless
     if (empty($query_items)) {
         return true;
     }
     // suspiciously like SugarCRM query, reject
     if (!empty($query_items['module']) && !empty($query_items['action'])) {
         return false;
     }
     // looks like non-download entry point - allow only specific entry points
     if (!empty($query_items['entryPoint']) && !in_array($query_items['entryPoint'], ['download', 'image', 'getImage'])) {
         return false;
     }
     return true;
 }
Пример #12
0
 /**
  * Validates the attributes of a token, mutating it as necessary.
  * that has valid tokens
  * @param HTMLPurifier_Token $token Token to validate.
  * @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config
  * @param HTMLPurifier_Context $context Instance of HTMLPurifier_Context
  */
 public function validateToken($token, $config, $context)
 {
     $definition = $config->getHTMLDefinition();
     $e =& $context->get('ErrorCollector', true);
     // initialize IDAccumulator if necessary
     $ok =& $context->get('IDAccumulator', true);
     if (!$ok) {
         $id_accumulator = HTMLPurifier_IDAccumulator::build($config, $context);
         $context->register('IDAccumulator', $id_accumulator);
     }
     // initialize CurrentToken if necessary
     $current_token =& $context->get('CurrentToken', true);
     if (!$current_token) {
         $context->register('CurrentToken', $token);
     }
     if (!$token instanceof HTMLPurifier_Token_Start && !$token instanceof HTMLPurifier_Token_Empty) {
         return;
     }
     // create alias to global definition array, see also $defs
     // DEFINITION CALL
     $d_defs = $definition->info_global_attr;
     // don't update token until the very end, to ensure an atomic update
     $attr = $token->attr;
     // do global transformations (pre)
     // nothing currently utilizes this
     foreach ($definition->info_attr_transform_pre as $transform) {
         $attr = $transform->transform($o = $attr, $config, $context);
         if ($e) {
             if ($attr != $o) {
                 $e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
             }
         }
     }
     // do local transformations only applicable to this element (pre)
     // ex. <p align="right"> to <p style="text-align:right;">
     foreach ($definition->info[$token->name]->attr_transform_pre as $transform) {
         $attr = $transform->transform($o = $attr, $config, $context);
         if ($e) {
             if ($attr != $o) {
                 $e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
             }
         }
     }
     // create alias to this element's attribute definition array, see
     // also $d_defs (global attribute definition array)
     // DEFINITION CALL
     $defs = $definition->info[$token->name]->attr;
     $attr_key = false;
     $context->register('CurrentAttr', $attr_key);
     // iterate through all the attribute keypairs
     // Watch out for name collisions: $key has previously been used
     foreach ($attr as $attr_key => $value) {
         // call the definition
         if (isset($defs[$attr_key])) {
             // there is a local definition defined
             if ($defs[$attr_key] === false) {
                 // We've explicitly been told not to allow this element.
                 // This is usually when there's a global definition
                 // that must be overridden.
                 // Theoretically speaking, we could have a
                 // AttrDef_DenyAll, but this is faster!
                 $result = false;
             } else {
                 // validate according to the element's definition
                 $result = $defs[$attr_key]->validate($value, $config, $context);
             }
         } elseif (isset($d_defs[$attr_key])) {
             // there is a global definition defined, validate according
             // to the global definition
             $result = $d_defs[$attr_key]->validate($value, $config, $context);
         } else {
             // system never heard of the attribute? DELETE!
             $result = false;
         }
         // put the results into effect
         if ($result === false || $result === null) {
             // this is a generic error message that should replaced
             // with more specific ones when possible
             if ($e) {
                 $e->send(E_ERROR, 'AttrValidator: Attribute removed');
             }
             // remove the attribute
             unset($attr[$attr_key]);
         } elseif (is_string($result)) {
             // generally, if a substitution is happening, there
             // was some sort of implicit correction going on. We'll
             // delegate it to the attribute classes to say exactly what.
             // simple substitution
             $attr[$attr_key] = $result;
         } else {
             // nothing happens
         }
         // we'd also want slightly more complicated substitution
         // involving an array as the return value,
         // although we're not sure how colliding attributes would
         // resolve (certain ones would be completely overriden,
         // others would prepend themselves).
     }
     $context->destroy('CurrentAttr');
     // post transforms
     // global (error reporting untested)
     foreach ($definition->info_attr_transform_post as $transform) {
         $attr = $transform->transform($o = $attr, $config, $context);
         if ($e) {
             if ($attr != $o) {
                 $e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
             }
         }
     }
     // local (error reporting untested)
     foreach ($definition->info[$token->name]->attr_transform_post as $transform) {
         $attr = $transform->transform($o = $attr, $config, $context);
         if ($e) {
             if ($attr != $o) {
                 $e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
             }
         }
     }
     $token->attr = $attr;
     // destroy CurrentToken if we made it ourselves
     if (!$current_token) {
         $context->destroy('CurrentToken');
     }
 }
Пример #13
0
 /**
  * Prepares the injector by giving it the config and context objects:
  * this allows references to important variables to be made within
  * the injector. This function also checks if the HTML environment
  * will work with the Injector (see checkNeeded()).
  * @param HTMLPurifier_Config $config
  * @param HTMLPurifier_Context $context
  * @return bool|string Boolean false if success, string of missing needed element/attribute if failure
  */
 public function prepare($config, $context)
 {
     $this->htmlDefinition = $config->getHTMLDefinition();
     // Even though this might fail, some unit tests ignore this and
     // still test checkNeeded, so be careful. Maybe get rid of that
     // dependency.
     $result = $this->checkNeeded($config);
     if ($result !== false) {
         return $result;
     }
     $this->currentNesting =& $context->get('CurrentNesting');
     $this->currentToken =& $context->get('CurrentToken');
     $this->inputZipper =& $context->get('InputZipper');
     return false;
 }
Пример #14
0
 /**
  * @param HTMLPurifier_Token[] $tokens
  * @param HTMLPurifier_Config $config
  * @param HTMLPurifier_Context $context
  * @return HTMLPurifier_Token[]
  * @throws HTMLPurifier_Exception
  */
 public function execute($tokens, $config, $context)
 {
     $definition = $config->getHTMLDefinition();
     // local variables
     $generator = new HTMLPurifier_Generator($config, $context);
     $escape_invalid_tags = $config->get('Core.EscapeInvalidTags');
     // used for autoclose early abortion
     $global_parent_allowed_elements = $definition->info_parent_def->child->getAllowedElements($config);
     $e = $context->get('ErrorCollector', true);
     $i = false;
     // injector index
     list($zipper, $token) = HTMLPurifier_Zipper::fromArray($tokens);
     if ($token === NULL) {
         return array();
     }
     $reprocess = false;
     // whether or not to reprocess the same token
     $stack = array();
     // member variables
     $this->stack =& $stack;
     $this->tokens =& $tokens;
     $this->token =& $token;
     $this->zipper =& $zipper;
     $this->config = $config;
     $this->context = $context;
     // context variables
     $context->register('CurrentNesting', $stack);
     $context->register('InputZipper', $zipper);
     $context->register('CurrentToken', $token);
     // -- begin INJECTOR --
     $this->injectors = array();
     $injectors = $config->getBatch('AutoFormat');
     $def_injectors = $definition->info_injector;
     $custom_injectors = $injectors['Custom'];
     unset($injectors['Custom']);
     // special case
     foreach ($injectors as $injector => $b) {
         // XXX: Fix with a legitimate lookup table of enabled filters
         if (strpos($injector, '.') !== false) {
             continue;
         }
         $injector = "HTMLPurifier_Injector_{$injector}";
         if (!$b) {
             continue;
         }
         $this->injectors[] = new $injector();
     }
     foreach ($def_injectors as $injector) {
         // assumed to be objects
         $this->injectors[] = $injector;
     }
     foreach ($custom_injectors as $injector) {
         if (!$injector) {
             continue;
         }
         if (is_string($injector)) {
             $injector = "HTMLPurifier_Injector_{$injector}";
             $injector = new $injector();
         }
         $this->injectors[] = $injector;
     }
     // give the injectors references to the definition and context
     // variables for performance reasons
     foreach ($this->injectors as $ix => $injector) {
         $error = $injector->prepare($config, $context);
         if (!$error) {
             continue;
         }
         array_splice($this->injectors, $ix, 1);
         // rm the injector
         trigger_error("Cannot enable {$injector->name} injector because {$error} is not allowed", E_USER_WARNING);
     }
     // -- end INJECTOR --
     // a note on reprocessing:
     //      In order to reduce code duplication, whenever some code needs
     //      to make HTML changes in order to make things "correct", the
     //      new HTML gets sent through the purifier, regardless of its
     //      status. This means that if we add a start token, because it
     //      was totally necessary, we don't have to update nesting; we just
     //      punt ($reprocess = true; continue;) and it does that for us.
     // isset is in loop because $tokens size changes during loop exec
     for (;; $reprocess ? $reprocess = false : ($token = $zipper->next($token))) {
         // check for a rewind
         if (is_int($i)) {
             // possibility: disable rewinding if the current token has a
             // rewind set on it already. This would offer protection from
             // infinite loop, but might hinder some advanced rewinding.
             $rewind_offset = $this->injectors[$i]->getRewindOffset();
             if (is_int($rewind_offset)) {
                 for ($j = 0; $j < $rewind_offset; $j++) {
                     if (empty($zipper->front)) {
                         break;
                     }
                     $token = $zipper->prev($token);
                     // indicate that other injectors should not process this token,
                     // but we need to reprocess it
                     unset($token->skip[$i]);
                     $token->rewind = $i;
                     if ($token instanceof HTMLPurifier_Token_Start) {
                         array_pop($this->stack);
                     } elseif ($token instanceof HTMLPurifier_Token_End) {
                         $this->stack[] = $token->start;
                     }
                 }
             }
             $i = false;
         }
         // handle case of document end
         if ($token === NULL) {
             // kill processing if stack is empty
             if (empty($this->stack)) {
                 break;
             }
             // peek
             $top_nesting = array_pop($this->stack);
             $this->stack[] = $top_nesting;
             // send error [TagClosedSuppress]
             if ($e && !isset($top_nesting->armor['MakeWellFormed_TagClosedError'])) {
                 $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag closed by document end', $top_nesting);
             }
             // append, don't splice, since this is the end
             $token = new HTMLPurifier_Token_End($top_nesting->name);
             // punt!
             $reprocess = true;
             continue;
         }
         //echo '<br>'; printZipper($zipper, $token);//printTokens($this->stack);
         //flush();
         // quick-check: if it's not a tag, no need to process
         if (empty($token->is_tag)) {
             if ($token instanceof HTMLPurifier_Token_Text) {
                 foreach ($this->injectors as $i => $injector) {
                     if (isset($token->skip[$i])) {
                         continue;
                     }
                     if ($token->rewind !== null && $token->rewind !== $i) {
                         continue;
                     }
                     // XXX fuckup
                     $r = $token;
                     $injector->handleText($r);
                     $token = $this->processToken($r, $i);
                     $reprocess = true;
                     break;
                 }
             }
             // another possibility is a comment
             continue;
         }
         if (isset($definition->info[$token->name])) {
             $type = $definition->info[$token->name]->child->type;
         } else {
             $type = false;
             // Type is unknown, treat accordingly
         }
         // quick tag checks: anything that's *not* an end tag
         $ok = false;
         if ($type === 'empty' && $token instanceof HTMLPurifier_Token_Start) {
             // claims to be a start tag but is empty
             $token = new HTMLPurifier_Token_Empty($token->name, $token->attr, $token->line, $token->col, $token->armor);
             $ok = true;
         } elseif ($type && $type !== 'empty' && $token instanceof HTMLPurifier_Token_Empty) {
             // claims to be empty but really is a start tag
             // NB: this assignment is required
             $old_token = $token;
             $token = new HTMLPurifier_Token_End($token->name);
             $token = $this->insertBefore(new HTMLPurifier_Token_Start($old_token->name, $old_token->attr, $old_token->line, $old_token->col, $old_token->armor));
             // punt (since we had to modify the input stream in a non-trivial way)
             $reprocess = true;
             continue;
         } elseif ($token instanceof HTMLPurifier_Token_Empty) {
             // real empty token
             $ok = true;
         } elseif ($token instanceof HTMLPurifier_Token_Start) {
             // start tag
             // ...unless they also have to close their parent
             if (!empty($this->stack)) {
                 // Performance note: you might think that it's rather
                 // inefficient, recalculating the autoclose information
                 // for every tag that a token closes (since when we
                 // do an autoclose, we push a new token into the
                 // stream and then /process/ that, before
                 // re-processing this token.)  But this is
                 // necessary, because an injector can make an
                 // arbitrary transformations to the autoclosing
                 // tokens we introduce, so things may have changed
                 // in the meantime.  Also, doing the inefficient thing is
                 // "easy" to reason about (for certain perverse definitions
                 // of "easy")
                 $parent = array_pop($this->stack);
                 $this->stack[] = $parent;
                 $parent_def = null;
                 $parent_elements = null;
                 $autoclose = false;
                 if (isset($definition->info[$parent->name])) {
                     $parent_def = $definition->info[$parent->name];
                     $parent_elements = $parent_def->child->getAllowedElements($config);
                     $autoclose = !isset($parent_elements[$token->name]);
                 }
                 if ($autoclose && $definition->info[$token->name]->wrap) {
                     // Check if an element can be wrapped by another
                     // element to make it valid in a context (for
                     // example, <ul><ul> needs a <li> in between)
                     $wrapname = $definition->info[$token->name]->wrap;
                     $wrapdef = $definition->info[$wrapname];
                     $elements = $wrapdef->child->getAllowedElements($config);
                     if (isset($elements[$token->name]) && isset($parent_elements[$wrapname])) {
                         $newtoken = new HTMLPurifier_Token_Start($wrapname);
                         $token = $this->insertBefore($newtoken);
                         $reprocess = true;
                         continue;
                     }
                 }
                 $carryover = false;
                 if ($autoclose && $parent_def->formatting) {
                     $carryover = true;
                 }
                 if ($autoclose) {
                     // check if this autoclose is doomed to fail
                     // (this rechecks $parent, which his harmless)
                     $autoclose_ok = isset($global_parent_allowed_elements[$token->name]);
                     if (!$autoclose_ok) {
                         foreach ($this->stack as $ancestor) {
                             $elements = $definition->info[$ancestor->name]->child->getAllowedElements($config);
                             if (isset($elements[$token->name])) {
                                 $autoclose_ok = true;
                                 break;
                             }
                             if ($definition->info[$token->name]->wrap) {
                                 $wrapname = $definition->info[$token->name]->wrap;
                                 $wrapdef = $definition->info[$wrapname];
                                 $wrap_elements = $wrapdef->child->getAllowedElements($config);
                                 if (isset($wrap_elements[$token->name]) && isset($elements[$wrapname])) {
                                     $autoclose_ok = true;
                                     break;
                                 }
                             }
                         }
                     }
                     if ($autoclose_ok) {
                         // errors need to be updated
                         $new_token = new HTMLPurifier_Token_End($parent->name);
                         $new_token->start = $parent;
                         // [TagClosedSuppress]
                         if ($e && !isset($parent->armor['MakeWellFormed_TagClosedError'])) {
                             if (!$carryover) {
                                 $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag auto closed', $parent);
                             } else {
                                 $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag carryover', $parent);
                             }
                         }
                         if ($carryover) {
                             $element = clone $parent;
                             // [TagClosedAuto]
                             $element->armor['MakeWellFormed_TagClosedError'] = true;
                             $element->carryover = true;
                             $token = $this->processToken(array($new_token, $token, $element));
                         } else {
                             $token = $this->insertBefore($new_token);
                         }
                     } else {
                         $token = $this->remove();
                     }
                     $reprocess = true;
                     continue;
                 }
             }
             $ok = true;
         }
         if ($ok) {
             foreach ($this->injectors as $i => $injector) {
                 if (isset($token->skip[$i])) {
                     continue;
                 }
                 if ($token->rewind !== null && $token->rewind !== $i) {
                     continue;
                 }
                 $r = $token;
                 $injector->handleElement($r);
                 $token = $this->processToken($r, $i);
                 $reprocess = true;
                 break;
             }
             if (!$reprocess) {
                 // ah, nothing interesting happened; do normal processing
                 if ($token instanceof HTMLPurifier_Token_Start) {
                     $this->stack[] = $token;
                 } elseif ($token instanceof HTMLPurifier_Token_End) {
                     throw new HTMLPurifier_Exception('Improper handling of end tag in start code; possible error in MakeWellFormed');
                 }
             }
             continue;
         }
         // sanity check: we should be dealing with a closing tag
         if (!$token instanceof HTMLPurifier_Token_End) {
             throw new HTMLPurifier_Exception('Unaccounted for tag token in input stream, bug in HTML Purifier');
         }
         // make sure that we have something open
         if (empty($this->stack)) {
             if ($escape_invalid_tags) {
                 if ($e) {
                     $e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag to text');
                 }
                 $token = new HTMLPurifier_Token_Text($generator->generateFromToken($token));
             } else {
                 if ($e) {
                     $e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag removed');
                 }
                 $token = $this->remove();
             }
             $reprocess = true;
             continue;
         }
         // first, check for the simplest case: everything closes neatly.
         // Eventually, everything passes through here; if there are problems
         // we modify the input stream accordingly and then punt, so that
         // the tokens get processed again.
         $current_parent = array_pop($this->stack);
         if ($current_parent->name == $token->name) {
             $token->start = $current_parent;
             foreach ($this->injectors as $i => $injector) {
                 if (isset($token->skip[$i])) {
                     continue;
                 }
                 if ($token->rewind !== null && $token->rewind !== $i) {
                     continue;
                 }
                 $r = $token;
                 $injector->handleEnd($r);
                 $token = $this->processToken($r, $i);
                 $this->stack[] = $current_parent;
                 $reprocess = true;
                 break;
             }
             continue;
         }
         // okay, so we're trying to close the wrong tag
         // undo the pop previous pop
         $this->stack[] = $current_parent;
         // scroll back the entire nest, trying to find our tag.
         // (feature could be to specify how far you'd like to go)
         $size = count($this->stack);
         // -2 because -1 is the last element, but we already checked that
         $skipped_tags = false;
         for ($j = $size - 2; $j >= 0; $j--) {
             if ($this->stack[$j]->name == $token->name) {
                 $skipped_tags = array_slice($this->stack, $j);
                 break;
             }
         }
         // we didn't find the tag, so remove
         if ($skipped_tags === false) {
             if ($escape_invalid_tags) {
                 if ($e) {
                     $e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag to text');
                 }
                 $token = new HTMLPurifier_Token_Text($generator->generateFromToken($token));
             } else {
                 if ($e) {
                     $e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag removed');
                 }
                 $token = $this->remove();
             }
             $reprocess = true;
             continue;
         }
         // do errors, in REVERSE $j order: a,b,c with </a></b></c>
         $c = count($skipped_tags);
         if ($e) {
             for ($j = $c - 1; $j > 0; $j--) {
                 // notice we exclude $j == 0, i.e. the current ending tag, from
                 // the errors... [TagClosedSuppress]
                 if (!isset($skipped_tags[$j]->armor['MakeWellFormed_TagClosedError'])) {
                     $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag closed by element end', $skipped_tags[$j]);
                 }
             }
         }
         // insert tags, in FORWARD $j order: c,b,a with </a></b></c>
         $replace = array($token);
         for ($j = 1; $j < $c; $j++) {
             // ...as well as from the insertions
             $new_token = new HTMLPurifier_Token_End($skipped_tags[$j]->name);
             $new_token->start = $skipped_tags[$j];
             array_unshift($replace, $new_token);
             if (isset($definition->info[$new_token->name]) && $definition->info[$new_token->name]->formatting) {
                 // [TagClosedAuto]
                 $element = clone $skipped_tags[$j];
                 $element->carryover = true;
                 $element->armor['MakeWellFormed_TagClosedError'] = true;
                 $replace[] = $element;
             }
         }
         $token = $this->processToken($replace);
         $reprocess = true;
         continue;
     }
     $context->destroy('CurrentToken');
     $context->destroy('CurrentNesting');
     $context->destroy('InputZipper');
     unset($this->injectors, $this->stack, $this->tokens);
     return $zipper->toArray($token);
 }
Пример #15
0
 /**
  * @param HTMLPurifier_Token[] $tokens
  * @param HTMLPurifier_Config $config
  * @param HTMLPurifier_Context $context
  * @return array|HTMLPurifier_Token[]
  */
 public function execute($tokens, $config, $context)
 {
     $definition = $config->getHTMLDefinition();
     $generator = new HTMLPurifier_Generator($config, $context);
     $result = array();
     $escape_invalid_tags = $config->get('Core.EscapeInvalidTags');
     $remove_invalid_img = $config->get('Core.RemoveInvalidImg');
     // currently only used to determine if comments should be kept
     $trusted = $config->get('HTML.Trusted');
     $comment_lookup = $config->get('HTML.AllowedComments');
     $comment_regexp = $config->get('HTML.AllowedCommentsRegexp');
     $check_comments = $comment_lookup !== array() || $comment_regexp !== null;
     $remove_script_contents = $config->get('Core.RemoveScriptContents');
     $hidden_elements = $config->get('Core.HiddenElements');
     // remove script contents compatibility
     if ($remove_script_contents === true) {
         $hidden_elements['script'] = true;
     } elseif ($remove_script_contents === false && isset($hidden_elements['script'])) {
         unset($hidden_elements['script']);
     }
     $attr_validator = new HTMLPurifier_AttrValidator();
     // removes tokens until it reaches a closing tag with its value
     $remove_until = false;
     // converts comments into text tokens when this is equal to a tag name
     $textify_comments = false;
     $token = false;
     $context->register('CurrentToken', $token);
     $e = false;
     if ($config->get('Core.CollectErrors')) {
         $e =& $context->get('ErrorCollector');
     }
     foreach ($tokens as $token) {
         if ($remove_until) {
             if (empty($token->is_tag) || $token->name !== $remove_until) {
                 continue;
             }
         }
         if (!empty($token->is_tag)) {
             // DEFINITION CALL
             // before any processing, try to transform the element
             if (isset($definition->info_tag_transform[$token->name])) {
                 $original_name = $token->name;
                 // there is a transformation for this tag
                 // DEFINITION CALL
                 $token = $definition->info_tag_transform[$token->name]->transform($token, $config, $context);
                 if ($e) {
                     $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Tag transform', $original_name);
                 }
             }
             if (isset($definition->info[$token->name])) {
                 // mostly everything's good, but
                 // we need to make sure required attributes are in order
                 if (($token instanceof HTMLPurifier_Token_Start || $token instanceof HTMLPurifier_Token_Empty) && $definition->info[$token->name]->required_attr && ($token->name != 'img' || $remove_invalid_img)) {
                     $attr_validator->validateToken($token, $config, $context);
                     $ok = true;
                     foreach ($definition->info[$token->name]->required_attr as $name) {
                         if (!isset($token->attr[$name])) {
                             $ok = false;
                             break;
                         }
                     }
                     if (!$ok) {
                         if ($e) {
                             $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Missing required attribute', $name);
                         }
                         continue;
                     }
                     $token->armor['ValidateAttributes'] = true;
                 }
                 if (isset($hidden_elements[$token->name]) && $token instanceof HTMLPurifier_Token_Start) {
                     $textify_comments = $token->name;
                 } elseif ($token->name === $textify_comments && $token instanceof HTMLPurifier_Token_End) {
                     $textify_comments = false;
                 }
             } elseif ($escape_invalid_tags) {
                 // invalid tag, generate HTML representation and insert in
                 if ($e) {
                     $e->send(E_WARNING, 'Strategy_RemoveForeignElements: Foreign element to text');
                 }
                 $token = new HTMLPurifier_Token_Text($generator->generateFromToken($token));
             } else {
                 // check if we need to destroy all of the tag's children
                 // CAN BE GENERICIZED
                 if (isset($hidden_elements[$token->name])) {
                     if ($token instanceof HTMLPurifier_Token_Start) {
                         $remove_until = $token->name;
                     } elseif ($token instanceof HTMLPurifier_Token_Empty) {
                         // do nothing: we're still looking
                     } else {
                         $remove_until = false;
                     }
                     if ($e) {
                         $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Foreign meta element removed');
                     }
                 } else {
                     if ($e) {
                         $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Foreign element removed');
                     }
                 }
                 continue;
             }
         } elseif ($token instanceof HTMLPurifier_Token_Comment) {
             // textify comments in script tags when they are allowed
             if ($textify_comments !== false) {
                 $data = $token->data;
                 $token = new HTMLPurifier_Token_Text($data);
             } elseif ($trusted || $check_comments) {
                 // always cleanup comments
                 $trailing_hyphen = false;
                 if ($e) {
                     // perform check whether or not there's a trailing hyphen
                     if (substr($token->data, -1) == '-') {
                         $trailing_hyphen = true;
                     }
                 }
                 $token->data = rtrim($token->data, '-');
                 $found_double_hyphen = false;
                 while (strpos($token->data, '--') !== false) {
                     $found_double_hyphen = true;
                     $token->data = str_replace('--', '-', $token->data);
                 }
                 if ($trusted || !empty($comment_lookup[trim($token->data)]) || $comment_regexp !== null && preg_match($comment_regexp, trim($token->data))) {
                     // OK good
                     if ($e) {
                         if ($trailing_hyphen) {
                             $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Trailing hyphen in comment removed');
                         }
                         if ($found_double_hyphen) {
                             $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Hyphens in comment collapsed');
                         }
                     }
                 } else {
                     if ($e) {
                         $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Comment removed');
                     }
                     continue;
                 }
             } else {
                 // strip comments
                 if ($e) {
                     $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Comment removed');
                 }
                 continue;
             }
         } elseif ($token instanceof HTMLPurifier_Token_Text) {
         } else {
             continue;
         }
         $result[] = $token;
     }
     if ($remove_until && $e) {
         // we removed tokens until the end, throw error
         $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Token removed to end', $remove_until);
     }
     $context->destroy('CurrentToken');
     return $result;
 }
Пример #16
0
 /**
  * Takes a piece of HTML and normalizes it by converting entities, fixing
  * encoding, extracting bits, and other good stuff.
  * @param string $html HTML.
  * @param HTMLPurifier_Config $config
  * @param HTMLPurifier_Context $context
  * @return string
  * @todo Consider making protected
  */
 public function normalize($html, $config, $context)
 {
     // normalize newlines to \n
     if ($config->get('Core.NormalizeNewlines')) {
         $html = str_replace("\r\n", "\n", $html);
         $html = str_replace("\r", "\n", $html);
     }
     if ($config->get('HTML.Trusted')) {
         // escape convoluted CDATA
         $html = $this->escapeCommentedCDATA($html);
     }
     // escape CDATA
     $html = $this->escapeCDATA($html);
     $html = $this->removeIEConditional($html);
     // extract body from document if applicable
     if ($config->get('Core.ConvertDocumentToFragment')) {
         $e = false;
         if ($config->get('Core.CollectErrors')) {
             $e =& $context->get('ErrorCollector');
         }
         $new_html = $this->extractBody($html);
         if ($e && $new_html != $html) {
             $e->send(E_WARNING, 'Lexer: Extracted body');
         }
         $html = $new_html;
     }
     // expand entities that aren't the big five
     $html = $this->_entity_parser->substituteNonSpecialEntities($html);
     // clean into wellformed UTF-8 string for an SGML context: this has
     // to be done after entity expansion because the entities sometimes
     // represent non-SGML characters (horror, horror!)
     $html = HTMLPurifier_Encoder::cleanUTF8($html);
     // if processing instructions are to removed, remove them now
     if ($config->get('Core.RemoveProcessingInstructions')) {
         $html = preg_replace('#<\\?.+?\\?>#s', '', $html);
     }
     return $html;
 }
Пример #17
0
 /**
  * @param HTMLPurifier_URI $uri
  * @param HTMLPurifier_Config $config
  * @param HTMLPurifier_Context $context
  */
 protected function makeReplace($uri, $config, $context)
 {
     $string = $uri->toString();
     // always available
     $this->replace['%s'] = $string;
     $this->replace['%r'] = $context->get('EmbeddedURI', true);
     $token = $context->get('CurrentToken', true);
     $this->replace['%n'] = $token ? $token->name : null;
     $this->replace['%m'] = $context->get('CurrentAttr', true);
     $this->replace['%p'] = $context->get('CurrentCSSProperty', true);
     // not always available
     if ($this->secretKey) {
         $this->replace['%t'] = hash_hmac("sha256", $string, $this->secretKey);
     }
 }
Пример #18
0
 /**
  * @param HTMLPurifier_URI $uri
  * @param HTMLPurifier_Config $config
  * @param HTMLPurifier_Context $context
  * @return bool
  */
 public function filter(&$uri, $config, $context)
 {
     return !$context->get('EmbeddedURI', true);
 }
Пример #19
0
 /**
  * Takes the inside of an HTML tag and makes an assoc array of attributes.
  *
  * @param string $string Inside of tag excluding name.
  * @param HTMLPurifier_Config $config
  * @param HTMLPurifier_Context $context
  * @return array Assoc array of attributes.
  */
 public function parseAttributeString($string, $config, $context)
 {
     $string = (string) $string;
     // quick typecast
     if ($string == '') {
         return array();
     }
     // no attributes
     $e = false;
     if ($config->get('Core.CollectErrors')) {
         $e =& $context->get('ErrorCollector');
     }
     // let's see if we can abort as quickly as possible
     // one equal sign, no spaces => one attribute
     $num_equal = substr_count($string, '=');
     $has_space = strpos($string, ' ');
     if ($num_equal === 0 && !$has_space) {
         // bool attribute
         return array($string => $string);
     } elseif ($num_equal === 1 && !$has_space) {
         // only one attribute
         list($key, $quoted_value) = explode('=', $string);
         $quoted_value = trim($quoted_value);
         if (!$key) {
             if ($e) {
                 $e->send(E_ERROR, 'Lexer: Missing attribute key');
             }
             return array();
         }
         if (!$quoted_value) {
             return array($key => '');
         }
         $first_char = @$quoted_value[0];
         $last_char = @$quoted_value[strlen($quoted_value) - 1];
         $same_quote = $first_char == $last_char;
         $open_quote = $first_char == '"' || $first_char == "'";
         if ($same_quote && $open_quote) {
             // well behaved
             $value = substr($quoted_value, 1, strlen($quoted_value) - 2);
         } else {
             // not well behaved
             if ($open_quote) {
                 if ($e) {
                     $e->send(E_ERROR, 'Lexer: Missing end quote');
                 }
                 $value = substr($quoted_value, 1);
             } else {
                 $value = $quoted_value;
             }
         }
         if ($value === false) {
             $value = '';
         }
         return array($key => $this->parseData($value));
     }
     // setup loop environment
     $array = array();
     // return assoc array of attributes
     $cursor = 0;
     // current position in string (moves forward)
     $size = strlen($string);
     // size of the string (stays the same)
     // if we have unquoted attributes, the parser expects a terminating
     // space, so let's guarantee that there's always a terminating space.
     $string .= ' ';
     $old_cursor = -1;
     while ($cursor < $size) {
         if ($old_cursor >= $cursor) {
             throw new Exception("Infinite loop detected");
         }
         $old_cursor = $cursor;
         $cursor += $value = strspn($string, $this->_whitespace, $cursor);
         // grab the key
         $key_begin = $cursor;
         //we're currently at the start of the key
         // scroll past all characters that are the key (not whitespace or =)
         $cursor += strcspn($string, $this->_whitespace . '=', $cursor);
         $key_end = $cursor;
         // now at the end of the key
         $key = substr($string, $key_begin, $key_end - $key_begin);
         if (!$key) {
             if ($e) {
                 $e->send(E_ERROR, 'Lexer: Missing attribute key');
             }
             $cursor += 1 + strcspn($string, $this->_whitespace, $cursor + 1);
             // prevent infinite loop
             continue;
             // empty key
         }
         // scroll past all whitespace
         $cursor += strspn($string, $this->_whitespace, $cursor);
         if ($cursor >= $size) {
             $array[$key] = $key;
             break;
         }
         // if the next character is an equal sign, we've got a regular
         // pair, otherwise, it's a bool attribute
         $first_char = @$string[$cursor];
         if ($first_char == '=') {
             // key="value"
             $cursor++;
             $cursor += strspn($string, $this->_whitespace, $cursor);
             if ($cursor === false) {
                 $array[$key] = '';
                 break;
             }
             // we might be in front of a quote right now
             $char = @$string[$cursor];
             if ($char == '"' || $char == "'") {
                 // it's quoted, end bound is $char
                 $cursor++;
                 $value_begin = $cursor;
                 $cursor = strpos($string, $char, $cursor);
                 $value_end = $cursor;
             } else {
                 // it's not quoted, end bound is whitespace
                 $value_begin = $cursor;
                 $cursor += strcspn($string, $this->_whitespace, $cursor);
                 $value_end = $cursor;
             }
             // we reached a premature end
             if ($cursor === false) {
                 $cursor = $size;
                 $value_end = $cursor;
             }
             $value = substr($string, $value_begin, $value_end - $value_begin);
             if ($value === false) {
                 $value = '';
             }
             $array[$key] = $this->parseData($value);
             $cursor++;
         } else {
             // boolattr
             if ($key !== '') {
                 $array[$key] = $key;
             } else {
                 // purely theoretical
                 if ($e) {
                     $e->send(E_ERROR, 'Lexer: Missing attribute key');
                 }
             }
         }
     }
     return $array;
 }