HTMLPurifier 4.4.0
|
00001 <?php 00002 00008 class HTMLPurifier_VarParser_Flexible extends HTMLPurifier_VarParser 00009 { 00010 00011 protected function parseImplementation($var, $type, $allow_null) { 00012 if ($allow_null && $var === null) return null; 00013 switch ($type) { 00014 // Note: if code "breaks" from the switch, it triggers a generic 00015 // exception to be thrown. Specific errors can be specifically 00016 // done here. 00017 case self::MIXED : 00018 case self::ISTRING : 00019 case self::STRING : 00020 case self::TEXT : 00021 case self::ITEXT : 00022 return $var; 00023 case self::INT : 00024 if (is_string($var) && ctype_digit($var)) $var = (int) $var; 00025 return $var; 00026 case self::FLOAT : 00027 if ((is_string($var) && is_numeric($var)) || is_int($var)) $var = (float) $var; 00028 return $var; 00029 case self::BOOL : 00030 if (is_int($var) && ($var === 0 || $var === 1)) { 00031 $var = (bool) $var; 00032 } elseif (is_string($var)) { 00033 if ($var == 'on' || $var == 'true' || $var == '1') { 00034 $var = true; 00035 } elseif ($var == 'off' || $var == 'false' || $var == '0') { 00036 $var = false; 00037 } else { 00038 throw new HTMLPurifier_VarParserException("Unrecognized value '$var' for $type"); 00039 } 00040 } 00041 return $var; 00042 case self::ALIST : 00043 case self::HASH : 00044 case self::LOOKUP : 00045 if (is_string($var)) { 00046 // special case: technically, this is an array with 00047 // a single empty string item, but having an empty 00048 // array is more intuitive 00049 if ($var == '') return array(); 00050 if (strpos($var, "\n") === false && strpos($var, "\r") === false) { 00051 // simplistic string to array method that only works 00052 // for simple lists of tag names or alphanumeric characters 00053 $var = explode(',',$var); 00054 } else { 00055 $var = preg_split('/(,|[\n\r]+)/', $var); 00056 } 00057 // remove spaces 00058 foreach ($var as $i => $j) $var[$i] = trim($j); 00059 if ($type === self::HASH) { 00060 // key:value,key2:value2 00061 $nvar = array(); 00062 foreach ($var as $keypair) { 00063 $c = explode(':', $keypair, 2); 00064 if (!isset($c[1])) continue; 00065 $nvar[trim($c[0])] = trim($c[1]); 00066 } 00067 $var = $nvar; 00068 } 00069 } 00070 if (!is_array($var)) break; 00071 $keys = array_keys($var); 00072 if ($keys === array_keys($keys)) { 00073 if ($type == self::ALIST) return $var; 00074 elseif ($type == self::LOOKUP) { 00075 $new = array(); 00076 foreach ($var as $key) { 00077 $new[$key] = true; 00078 } 00079 return $new; 00080 } else break; 00081 } 00082 if ($type === self::ALIST) { 00083 trigger_error("Array list did not have consecutive integer indexes", E_USER_WARNING); 00084 return array_values($var); 00085 } 00086 if ($type === self::LOOKUP) { 00087 foreach ($var as $key => $value) { 00088 if ($value !== true) { 00089 trigger_error("Lookup array has non-true value at key '$key'; maybe your input array was not indexed numerically", E_USER_WARNING); 00090 } 00091 $var[$key] = true; 00092 } 00093 } 00094 return $var; 00095 default: 00096 $this->errorInconsistent(__CLASS__, $type); 00097 } 00098 $this->errorGeneric($var, $type); 00099 } 00100 00101 } 00102 00103 // vim: et sw=4 sts=4