2008-03-05 03:51:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs safe variable parsing based on types which can be used by
|
|
|
|
* users. This may not be able to represent all possible data inputs,
|
|
|
|
* however.
|
|
|
|
*/
|
|
|
|
class HTMLPurifier_VarParser_Flexible extends HTMLPurifier_VarParser
|
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-03-05 04:41:45 +00:00
|
|
|
protected function parseImplementation($var, $type, $allow_null) {
|
2008-03-05 03:51:09 +00:00
|
|
|
if ($allow_null && $var === null) return null;
|
|
|
|
switch ($type) {
|
|
|
|
// Note: if code "breaks" from the switch, it triggers a generic
|
|
|
|
// exception to be thrown. Specific errors can be specifically
|
|
|
|
// done here.
|
2008-05-23 16:43:24 +00:00
|
|
|
case self::MIXED :
|
|
|
|
case self::ISTRING :
|
|
|
|
case self::STRING :
|
|
|
|
case self::TEXT :
|
|
|
|
case self::ITEXT :
|
2008-03-05 03:51:09 +00:00
|
|
|
return $var;
|
2008-05-23 16:43:24 +00:00
|
|
|
case self::INT :
|
2008-03-05 03:51:09 +00:00
|
|
|
if (is_string($var) && ctype_digit($var)) $var = (int) $var;
|
|
|
|
return $var;
|
2008-05-23 16:43:24 +00:00
|
|
|
case self::FLOAT :
|
2008-03-05 05:07:12 +00:00
|
|
|
if ((is_string($var) && is_numeric($var)) || is_int($var)) $var = (float) $var;
|
2008-03-05 03:51:09 +00:00
|
|
|
return $var;
|
2008-05-23 16:43:24 +00:00
|
|
|
case self::BOOL :
|
2008-03-05 03:51:09 +00:00
|
|
|
if (is_int($var) && ($var === 0 || $var === 1)) {
|
|
|
|
$var = (bool) $var;
|
|
|
|
} elseif (is_string($var)) {
|
|
|
|
if ($var == 'on' || $var == 'true' || $var == '1') {
|
|
|
|
$var = true;
|
|
|
|
} elseif ($var == 'off' || $var == 'false' || $var == '0') {
|
|
|
|
$var = false;
|
|
|
|
} else {
|
|
|
|
throw new HTMLPurifier_VarParserException("Unrecognized value '$var' for $type");
|
|
|
|
}
|
2008-03-05 04:41:45 +00:00
|
|
|
}
|
2008-03-05 03:51:09 +00:00
|
|
|
return $var;
|
2008-05-23 16:43:24 +00:00
|
|
|
case self::ALIST :
|
|
|
|
case self::HASH :
|
|
|
|
case self::LOOKUP :
|
2008-03-05 03:51:09 +00:00
|
|
|
if (is_string($var)) {
|
|
|
|
// special case: technically, this is an array with
|
|
|
|
// a single empty string item, but having an empty
|
|
|
|
// array is more intuitive
|
|
|
|
if ($var == '') return array();
|
|
|
|
if (strpos($var, "\n") === false && strpos($var, "\r") === false) {
|
|
|
|
// simplistic string to array method that only works
|
|
|
|
// for simple lists of tag names or alphanumeric characters
|
|
|
|
$var = explode(',',$var);
|
|
|
|
} else {
|
|
|
|
$var = preg_split('/(,|[\n\r]+)/', $var);
|
|
|
|
}
|
|
|
|
// remove spaces
|
|
|
|
foreach ($var as $i => $j) $var[$i] = trim($j);
|
2008-05-23 16:43:24 +00:00
|
|
|
if ($type === self::HASH) {
|
2008-03-05 03:51:09 +00:00
|
|
|
// key:value,key2:value2
|
|
|
|
$nvar = array();
|
|
|
|
foreach ($var as $keypair) {
|
|
|
|
$c = explode(':', $keypair, 2);
|
|
|
|
if (!isset($c[1])) continue;
|
|
|
|
$nvar[$c[0]] = $c[1];
|
|
|
|
}
|
|
|
|
$var = $nvar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!is_array($var)) break;
|
|
|
|
$keys = array_keys($var);
|
|
|
|
if ($keys === array_keys($keys)) {
|
2008-05-23 16:43:24 +00:00
|
|
|
if ($type == self::ALIST) return $var;
|
|
|
|
elseif ($type == self::LOOKUP) {
|
2008-03-05 03:51:09 +00:00
|
|
|
$new = array();
|
|
|
|
foreach ($var as $key) {
|
|
|
|
$new[$key] = true;
|
|
|
|
}
|
|
|
|
return $new;
|
|
|
|
} else break;
|
|
|
|
}
|
2008-05-23 16:43:24 +00:00
|
|
|
if ($type === self::LOOKUP) {
|
2008-03-05 03:51:09 +00:00
|
|
|
foreach ($var as $key => $value) {
|
|
|
|
$var[$key] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $var;
|
|
|
|
default:
|
2008-03-05 04:41:45 +00:00
|
|
|
$this->errorInconsistent(__CLASS__, $type);
|
2008-03-05 03:51:09 +00:00
|
|
|
}
|
2008-03-05 04:41:45 +00:00
|
|
|
$this->errorGeneric($var, $type);
|
2008-03-05 03:51:09 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-03-05 03:51:09 +00:00
|
|
|
}
|
2008-12-06 09:24:59 +00:00
|
|
|
|
|
|
|
// vim: et sw=4 sts=4
|