ID.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Validates the HTML attribute ID.
  4. * @warning Even though this is the id processor, it
  5. * will ignore the directive Attr:IDBlacklist, since it will only
  6. * go according to the ID accumulator. Since the accumulator is
  7. * automatically generated, it will have already absorbed the
  8. * blacklist. If you're hacking around, make sure you use load()!
  9. */
  10. class HTMLPurifier_AttrDef_HTML_ID extends HTMLPurifier_AttrDef
  11. {
  12. // ref functionality disabled, since we also have to verify
  13. // whether or not the ID it refers to exists
  14. public function validate($id, $config, $context) {
  15. if (!$config->get('Attr.EnableID')) return false;
  16. $id = trim($id); // trim it first
  17. if ($id === '') return false;
  18. $prefix = $config->get('Attr.IDPrefix');
  19. if ($prefix !== '') {
  20. $prefix .= $config->get('Attr.IDPrefixLocal');
  21. // prevent re-appending the prefix
  22. if (strpos($id, $prefix) !== 0) $id = $prefix . $id;
  23. } elseif ($config->get('Attr.IDPrefixLocal') !== '') {
  24. trigger_error('%Attr.IDPrefixLocal cannot be used unless '.
  25. '%Attr.IDPrefix is set', E_USER_WARNING);
  26. }
  27. //if (!$this->ref) {
  28. $id_accumulator =& $context->get('IDAccumulator');
  29. if (isset($id_accumulator->ids[$id])) return false;
  30. //}
  31. // we purposely avoid using regex, hopefully this is faster
  32. if (ctype_alpha($id)) {
  33. $result = true;
  34. } else {
  35. if (!ctype_alpha(@$id[0])) return false;
  36. $trim = trim( // primitive style of regexps, I suppose
  37. $id,
  38. 'A..Za..z0..9:-._'
  39. );
  40. $result = ($trim === '');
  41. }
  42. $regexp = $config->get('Attr.IDBlacklistRegexp');
  43. if ($regexp && preg_match($regexp, $id)) {
  44. return false;
  45. }
  46. if (/*!$this->ref && */$result) $id_accumulator->add($id);
  47. // if no change was made to the ID, return the result
  48. // else, return the new id if stripping whitespace made it
  49. // valid, or return false.
  50. return $result ? $id : false;
  51. }
  52. }
  53. // vim: et sw=4 sts=4