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:
green-ponies (jgen) 2012-04-15 19:28:27 -04:00
parent 372f4fad7c
commit a0a0ba5bbc
4 changed files with 29 additions and 24 deletions

View file

@ -103,7 +103,7 @@ abstract class BaseConfig implements Config {
return $this->get($name, $default); return $this->get($name, $default);
} }
public function get_bool(/*string*/ $name, $default=null) { 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()) { public function get_array(/*string*/ $name, $default=array()) {
return explode(",", $this->get($name, "")); return explode(",", $this->get($name, ""));

View file

@ -56,7 +56,7 @@ class Image {
$this->$name = $value; // hax $this->$name = $value; // hax
} }
$this->posted_timestamp = strtotime($this->posted); // pray $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->id));
assert(is_numeric($this->height)); assert(is_numeric($this->height));
@ -439,7 +439,7 @@ class Image {
$sln = $database->engine->scoreql_to_sql('SCORE_BOOL_'.$ln); $sln = $database->engine->scoreql_to_sql('SCORE_BOOL_'.$ln);
$sln = str_replace("'", "", $sln); $sln = str_replace("'", "", $sln);
$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)); $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"); log_info("core-image", "Setting Image #{$this->id} lock to: $ln");
} }

View file

@ -65,6 +65,19 @@ function sql_escape($input) {
* @retval boolean * @retval boolean
*/ */
function bool_escape($input) { function bool_escape($input) {
/*
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); $input = strtolower($input);
return ( return (
$input === "y" || $input === "y" ||
@ -76,6 +89,8 @@ function bool_escape($input) {
$input === true $input === true
); );
} }
}
}
/** /**
* Some functions require a callback function for escaping, * Some functions require a callback function for escaping,
@ -209,16 +224,6 @@ function show_ip($ip, $ban_reason) {
return $ip; 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. * Checks if a given string contains another at the beginning.
* *

View file

@ -28,7 +28,7 @@ class PM {
$this->sent_date = $a["sent_date"]; $this->sent_date = $a["sent_date"];
$this->subject = $a["subject"]; $this->subject = $a["subject"];
$this->message = $a["message"]; $this->message = $a["message"];
$this->is_read = undb_bool($a["is_read"]); $this->is_read = bool_escape($a["is_read"]);
} }
else { else {
$this->id = -1; $this->id = -1;