Flexible.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Performs safe variable parsing based on types which can be used by
  4. * users. This may not be able to represent all possible data inputs,
  5. * however.
  6. */
  7. class HTMLPurifier_VarParser_Flexible extends HTMLPurifier_VarParser
  8. {
  9. protected function parseImplementation($var, $type, $allow_null) {
  10. if ($allow_null && $var === null) return null;
  11. switch ($type) {
  12. // Note: if code "breaks" from the switch, it triggers a generic
  13. // exception to be thrown. Specific errors can be specifically
  14. // done here.
  15. case self::MIXED :
  16. case self::ISTRING :
  17. case self::STRING :
  18. case self::TEXT :
  19. case self::ITEXT :
  20. return $var;
  21. case self::INT :
  22. if (is_string($var) && ctype_digit($var)) $var = (int) $var;
  23. return $var;
  24. case self::FLOAT :
  25. if ((is_string($var) && is_numeric($var)) || is_int($var)) $var = (float) $var;
  26. return $var;
  27. case self::BOOL :
  28. if (is_int($var) && ($var === 0 || $var === 1)) {
  29. $var = (bool) $var;
  30. } elseif (is_string($var)) {
  31. if ($var == 'on' || $var == 'true' || $var == '1') {
  32. $var = true;
  33. } elseif ($var == 'off' || $var == 'false' || $var == '0') {
  34. $var = false;
  35. } else {
  36. throw new HTMLPurifier_VarParserException("Unrecognized value '$var' for $type");
  37. }
  38. }
  39. return $var;
  40. case self::ALIST :
  41. case self::HASH :
  42. case self::LOOKUP :
  43. if (is_string($var)) {
  44. // special case: technically, this is an array with
  45. // a single empty string item, but having an empty
  46. // array is more intuitive
  47. if ($var == '') return array();
  48. if (strpos($var, "\n") === false && strpos($var, "\r") === false) {
  49. // simplistic string to array method that only works
  50. // for simple lists of tag names or alphanumeric characters
  51. $var = explode(',',$var);
  52. } else {
  53. $var = preg_split('/(,|[\n\r]+)/', $var);
  54. }
  55. // remove spaces
  56. foreach ($var as $i => $j) $var[$i] = trim($j);
  57. if ($type === self::HASH) {
  58. // key:value,key2:value2
  59. $nvar = array();
  60. foreach ($var as $keypair) {
  61. $c = explode(':', $keypair, 2);
  62. if (!isset($c[1])) continue;
  63. $nvar[trim($c[0])] = trim($c[1]);
  64. }
  65. $var = $nvar;
  66. }
  67. }
  68. if (!is_array($var)) break;
  69. $keys = array_keys($var);
  70. if ($keys === array_keys($keys)) {
  71. if ($type == self::ALIST) return $var;
  72. elseif ($type == self::LOOKUP) {
  73. $new = array();
  74. foreach ($var as $key) {
  75. $new[$key] = true;
  76. }
  77. return $new;
  78. } else break;
  79. }
  80. if ($type === self::ALIST) {
  81. trigger_error("Array list did not have consecutive integer indexes", E_USER_WARNING);
  82. return array_values($var);
  83. }
  84. if ($type === self::LOOKUP) {
  85. foreach ($var as $key => $value) {
  86. if ($value !== true) {
  87. trigger_error("Lookup array has non-true value at key '$key'; maybe your input array was not indexed numerically", E_USER_WARNING);
  88. }
  89. $var[$key] = true;
  90. }
  91. }
  92. return $var;
  93. default:
  94. $this->errorInconsistent(__CLASS__, $type);
  95. }
  96. $this->errorGeneric($var, $type);
  97. }
  98. }
  99. // vim: et sw=4 sts=4