PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/vjCommentPlugin/lib/tools/htmlpurifier/library/HTMLPurifier/URIFilter/Munge.php

https://bitbucket.org/Kudlaty/360kdw
PHP | 58 lines | 43 code | 9 blank | 6 comment | 8 complexity | af92dd990abc627653f029dca94ed2d2 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class HTMLPurifier_URIFilter_Munge extends HTMLPurifier_URIFilter
  3. {
  4. public $name = 'Munge';
  5. public $post = true;
  6. private $target, $parser, $doEmbed, $secretKey;
  7. protected $replace = array();
  8. public function prepare($config) {
  9. $this->target = $config->get('URI.' . $this->name);
  10. $this->parser = new HTMLPurifier_URIParser();
  11. $this->doEmbed = $config->get('URI.MungeResources');
  12. $this->secretKey = $config->get('URI.MungeSecretKey');
  13. return true;
  14. }
  15. public function filter(&$uri, $config, $context) {
  16. if ($context->get('EmbeddedURI', true) && !$this->doEmbed) return true;
  17. $scheme_obj = $uri->getSchemeObj($config, $context);
  18. if (!$scheme_obj) return true; // ignore unknown schemes, maybe another postfilter did it
  19. if (is_null($uri->host) || empty($scheme_obj->browsable)) {
  20. return true;
  21. }
  22. // don't redirect if target host is our host
  23. if ($uri->host === $config->getDefinition('URI')->host) {
  24. return true;
  25. }
  26. $this->makeReplace($uri, $config, $context);
  27. $this->replace = array_map('rawurlencode', $this->replace);
  28. $new_uri = strtr($this->target, $this->replace);
  29. $new_uri = $this->parser->parse($new_uri);
  30. // don't redirect if the target host is the same as the
  31. // starting host
  32. if ($uri->host === $new_uri->host) return true;
  33. $uri = $new_uri; // overwrite
  34. return true;
  35. }
  36. protected function makeReplace($uri, $config, $context) {
  37. $string = $uri->toString();
  38. // always available
  39. $this->replace['%s'] = $string;
  40. $this->replace['%r'] = $context->get('EmbeddedURI', true);
  41. $token = $context->get('CurrentToken', true);
  42. $this->replace['%n'] = $token ? $token->name : null;
  43. $this->replace['%m'] = $context->get('CurrentAttr', true);
  44. $this->replace['%p'] = $context->get('CurrentCSSProperty', true);
  45. // not always available
  46. if ($this->secretKey) $this->replace['%t'] = sha1($this->secretKey . ':' . $string);
  47. }
  48. }
  49. // vim: et sw=4 sts=4