[3.1.0] Split out VarParser from ConfigSchema
[htmlpurifier.git] / library / HTMLPurifier / VarParser.php
blobf02355609febb013688968f75a878eb48f7654bf
1 <?php
3 /**
4 * Parses string representations into their corresponding native PHP
5 * variable type.
6 */
7 class HTMLPurifier_VarParser
10 /**
11 * Lookup table of allowed types.
13 public $types = array(
14 'string' => true,
15 'istring' => true,
16 'text' => true,
17 'itext' => true,
18 'int' => true,
19 'float' => true,
20 'bool' => true,
21 'lookup' => true,
22 'list' => true,
23 'hash' => true,
24 'mixed' => true
27 /**
28 * Validate a variable according to type. Throws exception if invalid.
29 * It may return NULL as a valid type.
31 public function parse($var, $type, $allow_null = false) {
32 if (!isset($this->types[$type])) {
33 throw new HTMLPurifier_VarParserException("Invalid type $type");
35 if ($allow_null && $var === null) return null;
36 switch ($type) {
37 // Note: if code "breaks" from the switch, it triggers a generic
38 // exception to be thrown. Specific errors can be specifically
39 // done here.
40 case 'mixed':
41 //if (is_string($var)) $var = unserialize($var);
42 return $var;
43 case 'istring':
44 case 'string':
45 case 'text': // no difference, just is longer/multiple line string
46 case 'itext':
47 if (!is_string($var)) break;
48 if ($type === 'istring' || $type === 'itext') $var = strtolower($var);
49 return $var;
50 case 'int':
51 if (is_string($var) && ctype_digit($var)) $var = (int) $var;
52 elseif (!is_int($var)) break;
53 return $var;
54 case 'float':
55 if (is_string($var) && is_numeric($var)) $var = (float) $var;
56 elseif (!is_float($var)) break;
57 return $var;
58 case 'bool':
59 if (is_int($var) && ($var === 0 || $var === 1)) {
60 $var = (bool) $var;
61 } elseif (is_string($var)) {
62 if ($var == 'on' || $var == 'true' || $var == '1') {
63 $var = true;
64 } elseif ($var == 'off' || $var == 'false' || $var == '0') {
65 $var = false;
66 } else {
67 throw new HTMLPurifier_VarParserException("Unrecognized value '$var' for $type");
69 } elseif (!is_bool($var)) break;
70 return $var;
71 case 'list':
72 case 'hash':
73 case 'lookup':
74 if (is_string($var)) {
75 // special case: technically, this is an array with
76 // a single empty string item, but having an empty
77 // array is more intuitive
78 if ($var == '') return array();
79 if (strpos($var, "\n") === false && strpos($var, "\r") === false) {
80 // simplistic string to array method that only works
81 // for simple lists of tag names or alphanumeric characters
82 $var = explode(',',$var);
83 } else {
84 $var = preg_split('/(,|[\n\r]+)/', $var);
86 // remove spaces
87 foreach ($var as $i => $j) $var[$i] = trim($j);
88 if ($type === 'hash') {
89 // key:value,key2:value2
90 $nvar = array();
91 foreach ($var as $keypair) {
92 $c = explode(':', $keypair, 2);
93 if (!isset($c[1])) continue;
94 $nvar[$c[0]] = $c[1];
96 $var = $nvar;
99 if (!is_array($var)) break;
100 $keys = array_keys($var);
101 if ($keys === array_keys($keys)) {
102 if ($type == 'list') return $var;
103 elseif ($type == 'lookup') {
104 $new = array();
105 foreach ($var as $key) {
106 $new[$key] = true;
108 return $new;
109 } else break;
111 if ($type === 'lookup') {
112 foreach ($var as $key => $value) {
113 $var[$key] = true;
116 return $var;
117 default:
118 // This should not happen!
119 throw new HTMLPurifier_Exception("Inconsistency in HTMLPurifier_VarParser: $type is not implemented");
121 throw new HTMLPurifier_VarParserException("Invalid input for type $type");