您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

87 行
2.4KB

  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. /**
  9. * @param string $string
  10. * @param HTMLPurifier_Config $config
  11. * @param HTMLPurifier_Context $context
  12. * @return bool|string
  13. */
  14. public function validate($string, $config, $context)
  15. {
  16. $string = trim($string);
  17. if (!$string) {
  18. return false;
  19. }
  20. $subtags = explode('-', $string);
  21. $num_subtags = count($subtags);
  22. if ($num_subtags == 0) { // sanity check
  23. return false;
  24. }
  25. // process primary subtag : $subtags[0]
  26. $length = strlen($subtags[0]);
  27. switch ($length) {
  28. case 0:
  29. return false;
  30. case 1:
  31. if (!($subtags[0] == 'x' || $subtags[0] == 'i')) {
  32. return false;
  33. }
  34. break;
  35. case 2:
  36. case 3:
  37. if (!ctype_alpha($subtags[0])) {
  38. return false;
  39. } elseif (!ctype_lower($subtags[0])) {
  40. $subtags[0] = strtolower($subtags[0]);
  41. }
  42. break;
  43. default:
  44. return false;
  45. }
  46. $new_string = $subtags[0];
  47. if ($num_subtags == 1) {
  48. return $new_string;
  49. }
  50. // process second subtag : $subtags[1]
  51. $length = strlen($subtags[1]);
  52. if ($length == 0 || ($length == 1 && $subtags[1] != 'x') || $length > 8 || !ctype_alnum($subtags[1])) {
  53. return $new_string;
  54. }
  55. if (!ctype_lower($subtags[1])) {
  56. $subtags[1] = strtolower($subtags[1]);
  57. }
  58. $new_string .= '-' . $subtags[1];
  59. if ($num_subtags == 2) {
  60. return $new_string;
  61. }
  62. // process all other subtags, index 2 and up
  63. for ($i = 2; $i < $num_subtags; $i++) {
  64. $length = strlen($subtags[$i]);
  65. if ($length == 0 || $length > 8 || !ctype_alnum($subtags[$i])) {
  66. return $new_string;
  67. }
  68. if (!ctype_lower($subtags[$i])) {
  69. $subtags[$i] = strtolower($subtags[$i]);
  70. }
  71. $new_string .= '-' . $subtags[$i];
  72. }
  73. return $new_string;
  74. }
  75. }
  76. // vim: et sw=4 sts=4