PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/purifier/vendor/htmlpurifier/library/HTMLPurifier/AttrDef/Lang.php

https://bitbucket.org/levjke/kohanablogds
PHP | 73 lines | 51 code | 14 blank | 8 comment | 28 complexity | b5cf349d4bb318b51c2109423fb7556d MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Validates the HTML attribute lang, effectively a language code.
  4. * @note Built according to RFC 3066, which obsoleted RFC 1766
  5. */
  6. class HTMLPurifier_AttrDef_Lang extends HTMLPurifier_AttrDef
  7. {
  8. public function validate($string, $config, $context) {
  9. $string = trim($string);
  10. if (!$string) return false;
  11. $subtags = explode('-', $string);
  12. $num_subtags = count($subtags);
  13. if ($num_subtags == 0) return false; // sanity check
  14. // process primary subtag : $subtags[0]
  15. $length = strlen($subtags[0]);
  16. switch ($length) {
  17. case 0:
  18. return false;
  19. case 1:
  20. if (! ($subtags[0] == 'x' || $subtags[0] == 'i') ) {
  21. return false;
  22. }
  23. break;
  24. case 2:
  25. case 3:
  26. if (! ctype_alpha($subtags[0]) ) {
  27. return false;
  28. } elseif (! ctype_lower($subtags[0]) ) {
  29. $subtags[0] = strtolower($subtags[0]);
  30. }
  31. break;
  32. default:
  33. return false;
  34. }
  35. $new_string = $subtags[0];
  36. if ($num_subtags == 1) return $new_string;
  37. // process second subtag : $subtags[1]
  38. $length = strlen($subtags[1]);
  39. if ($length == 0 || ($length == 1 && $subtags[1] != 'x') || $length > 8 || !ctype_alnum($subtags[1])) {
  40. return $new_string;
  41. }
  42. if (!ctype_lower($subtags[1])) $subtags[1] = strtolower($subtags[1]);
  43. $new_string .= '-' . $subtags[1];
  44. if ($num_subtags == 2) return $new_string;
  45. // process all other subtags, index 2 and up
  46. for ($i = 2; $i < $num_subtags; $i++) {
  47. $length = strlen($subtags[$i]);
  48. if ($length == 0 || $length > 8 || !ctype_alnum($subtags[$i])) {
  49. return $new_string;
  50. }
  51. if (!ctype_lower($subtags[$i])) {
  52. $subtags[$i] = strtolower($subtags[$i]);
  53. }
  54. $new_string .= '-' . $subtags[$i];
  55. }
  56. return $new_string;
  57. }
  58. }
  59. // vim: et sw=4 sts=4