Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

75 rindas
2.4KB

  1. <?php
  2. /**
  3. * Validates a URI in CSS syntax, which uses url('http://example.com')
  4. * @note While theoretically speaking a URI in a CSS document could
  5. * be non-embedded, as of CSS2 there is no such usage so we're
  6. * generalizing it. This may need to be changed in the future.
  7. * @warning Since HTMLPurifier_AttrDef_CSS blindly uses semicolons as
  8. * the separator, you cannot put a literal semicolon in
  9. * in the URI. Try percent encoding it, in that case.
  10. */
  11. class HTMLPurifier_AttrDef_CSS_URI extends HTMLPurifier_AttrDef_URI
  12. {
  13. public function __construct()
  14. {
  15. parent::__construct(true); // always embedded
  16. }
  17. /**
  18. * @param string $uri_string
  19. * @param HTMLPurifier_Config $config
  20. * @param HTMLPurifier_Context $context
  21. * @return bool|string
  22. */
  23. public function validate($uri_string, $config, $context)
  24. {
  25. // parse the URI out of the string and then pass it onto
  26. // the parent object
  27. $uri_string = $this->parseCDATA($uri_string);
  28. if (strpos($uri_string, 'url(') !== 0) {
  29. return false;
  30. }
  31. $uri_string = substr($uri_string, 4);
  32. $new_length = strlen($uri_string) - 1;
  33. if ($uri_string[$new_length] != ')') {
  34. return false;
  35. }
  36. $uri = trim(substr($uri_string, 0, $new_length));
  37. if (!empty($uri) && ($uri[0] == "'" || $uri[0] == '"')) {
  38. $quote = $uri[0];
  39. $new_length = strlen($uri) - 1;
  40. if ($uri[$new_length] !== $quote) {
  41. return false;
  42. }
  43. $uri = substr($uri, 1, $new_length - 1);
  44. }
  45. $uri = $this->expandCSSEscape($uri);
  46. $result = parent::validate($uri, $config, $context);
  47. if ($result === false) {
  48. return false;
  49. }
  50. // extra sanity check; should have been done by URI
  51. $result = str_replace(array('"', "\\", "\n", "\x0c", "\r"), "", $result);
  52. // suspicious characters are ()'; we're going to percent encode
  53. // them for safety.
  54. $result = str_replace(array('(', ')', "'"), array('%28', '%29', '%27'), $result);
  55. // there's an extra bug where ampersands lose their escaping on
  56. // an innerHTML cycle, so a very unlucky query parameter could
  57. // then change the meaning of the URL. Unfortunately, there's
  58. // not much we can do about that...
  59. return "url(\"$result\")";
  60. }
  61. }
  62. // vim: et sw=4 sts=4