A better version of bool_escape() that uses filter_var if possible.
Also removed undb_bool() as it was basically a copy of the old bool_escape function.
This commit is contained in:
parent
372f4fad7c
commit
a0a0ba5bbc
4 changed files with 29 additions and 24 deletions
|
@ -103,7 +103,7 @@ abstract class BaseConfig implements Config {
|
|||
return $this->get($name, $default);
|
||||
}
|
||||
public function get_bool(/*string*/ $name, $default=null) {
|
||||
return undb_bool($this->get($name, $default));
|
||||
return bool_escape($this->get($name, $default));
|
||||
}
|
||||
public function get_array(/*string*/ $name, $default=array()) {
|
||||
return explode(",", $this->get($name, ""));
|
||||
|
|
|
@ -56,7 +56,7 @@ class Image {
|
|||
$this->$name = $value; // hax
|
||||
}
|
||||
$this->posted_timestamp = strtotime($this->posted); // pray
|
||||
$this->locked = undb_bool($this->locked);
|
||||
$this->locked = bool_escape($this->locked);
|
||||
|
||||
assert(is_numeric($this->id));
|
||||
assert(is_numeric($this->height));
|
||||
|
@ -439,7 +439,7 @@ class Image {
|
|||
$sln = $database->engine->scoreql_to_sql('SCORE_BOOL_'.$ln);
|
||||
$sln = str_replace("'", "", $sln);
|
||||
$sln = str_replace('"', "", $sln);
|
||||
if(undb_bool($sln) !== $this->locked) {
|
||||
if(bool_escape($sln) !== $this->locked) {
|
||||
$database->execute("UPDATE images SET locked=:yn WHERE id=:id", array("yn"=>$sln, "id"=>$this->id));
|
||||
log_info("core-image", "Setting Image #{$this->id} lock to: $ln");
|
||||
}
|
||||
|
|
|
@ -65,16 +65,31 @@ function sql_escape($input) {
|
|||
* @retval boolean
|
||||
*/
|
||||
function bool_escape($input) {
|
||||
$input = strtolower($input);
|
||||
return (
|
||||
$input === "y" ||
|
||||
$input === "yes" ||
|
||||
$input === "t" ||
|
||||
$input === "true" ||
|
||||
$input === "on" ||
|
||||
$input === 1 ||
|
||||
$input === true
|
||||
);
|
||||
/*
|
||||
Sometimes, I don't like PHP -- this, is one of those times...
|
||||
"a boolean FALSE is not considered a valid boolean value by this function."
|
||||
Yay for Got'chas!
|
||||
http://php.net/manual/en/filter.filters.validate.php
|
||||
*/
|
||||
if (is_bool($value)) {
|
||||
return $value;
|
||||
} else {
|
||||
$value = filter_var($input, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
||||
if (!is_null($value)) {
|
||||
return $value;
|
||||
} else {
|
||||
$input = strtolower($input);
|
||||
return (
|
||||
$input === "y" ||
|
||||
$input === "yes" ||
|
||||
$input === "t" ||
|
||||
$input === "true" ||
|
||||
$input === "on" ||
|
||||
$input === 1 ||
|
||||
$input === true
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -209,16 +224,6 @@ function show_ip($ip, $ban_reason) {
|
|||
return $ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Different databases have different ways to represent booleans; this
|
||||
* will try and standardise them
|
||||
*/
|
||||
function undb_bool($val) {
|
||||
// Could this be combined with bool_escape() ?
|
||||
if($val === true || $val == 'Y' || $val == 'y' || $val == 'T' || $val == 't' || $val === 1) return true;
|
||||
if($val === false || $val == 'N' || $val == 'n' || $val == 'F' || $val == 'f' || $val === 0) return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given string contains another at the beginning.
|
||||
*
|
||||
|
|
|
@ -28,7 +28,7 @@ class PM {
|
|||
$this->sent_date = $a["sent_date"];
|
||||
$this->subject = $a["subject"];
|
||||
$this->message = $a["message"];
|
||||
$this->is_read = undb_bool($a["is_read"]);
|
||||
$this->is_read = bool_escape($a["is_read"]);
|
||||
}
|
||||
else {
|
||||
$this->id = -1;
|
||||
|
|
Reference in a new issue