From 3f5f0b07f10da8d00dbf39154180dd08d05ef880 Mon Sep 17 00:00:00 2001 From: Chris DeGroat Date: Wed, 23 Jul 2014 20:12:01 -0500 Subject: [PATCH] fixed bugs in two redis methods --- autoload.php | 4 ++- crossbar.php | 4 ++- misc.php | 12 +++++++ model/base.php | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ model/mysql.php | 38 ++++++++++++++++++++++ mysql.php | 20 ++++++------ redis.php | 21 ++++++++++--- 7 files changed, 167 insertions(+), 16 deletions(-) diff --git a/autoload.php b/autoload.php index 1dffd8d..1acaa43 100755 --- a/autoload.php +++ b/autoload.php @@ -1,7 +1,9 @@ controllers_path = $application_root . 'controllers/'; $this->layouts_path = $application_root . 'layouts/'; $this->modules_path = $application_root . 'modules/'; + $this->url_segments = NULL; $this->starting_include_path = explode(PATH_SEPARATOR, get_include_path()); $this->custom_include_paths = array(); $this->missing_controller = ''; @@ -110,7 +111,7 @@ public function go() if($pre_response !== FALSE) // if the _pre function returns a false, we don't execute the action { - $this->controller_object->$action(); + call_user_func_array(array($this->controller_object, $action), $this->url_segments); } @@ -373,6 +374,7 @@ private function build_params() $url_parts = parse_url('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); $split_at_slash = explode("/", trim($url_parts['path'])); + $this->url_segments = array_slice($split_at_slash, 3); for($i = 3; $i <= count($split_at_slash)-1; $i += 2) { diff --git a/misc.php b/misc.php index a758bea..20a50a3 100644 --- a/misc.php +++ b/misc.php @@ -26,6 +26,18 @@ public static function reindex($array, $field) } return $new_array; } + + public static function get_between($string, $before, $after, $trim_whitespace = TRUE) + { + $starts_at = strpos($string, $before) + strlen($before); + $ends_at = strpos($string, $after, $starts_at); + $result = substr($string, $starts_at, $ends_at - $starts_at); + if($trim_whitespace) + { + return trim($result); + } + return $result; + } } ?> diff --git a/model/base.php b/model/base.php index 9992e22..1ff2534 100644 --- a/model/base.php +++ b/model/base.php @@ -3,6 +3,7 @@ class model_base { private static $errors = array(); + private static $param_errors = array(); // FIELDS public static function fields($method = 'create') @@ -126,6 +127,18 @@ public static function set_error($error) } } + // -- + public static function set_param_error($field, $error) + { + self::$param_errors[$field] = $error; + } + + // -- + public static function get_param_errors() + { + return self::$param_errors; + } + // -- public static function get_errors() { @@ -148,6 +161,77 @@ public static function get_error() } return array_shift($errors); } + + + public static function validate($values) + { + $errors = array(); + + $fields = self::fields(); + foreach($values as $param => $value) + { + if(!isset($fields[$param])) + { + continue; + } + $config = $fields[$param]; + + // Set label for error reporting + $label = ucwords($param); + if(isset($config['label'])) + { + $label = $config['label']; + } + + // Check if required + if($config['required'] === TRUE && ($value == NULL || $value == "")) + { + $errors[$param] = "Please enter a {$label}"; + } + + if(!empty($value)) + { + $type = $config['type']; + if($type != 'text' && $type != 'password') + { + if(!validate::$type(trim($value))) + { + $errors[$param] = "Invalid value entered for {$label}"; + } + } + + if(isset($config['max_length'])) + { + if(strlen($value) > $config['max_length']) + { + $errors[$param] = "{$label} exceeds maximum length of {$config['max_length']}"; + } + } + + if(isset($config['min_length'])) + { + if(strlen($value) < $config['min_length']) + { + $errors[$param] = "{$label} has a minimum length of {$config['min_length']}"; + } + } + } + } + + if(count($errors) > 0) + { + foreach($errors as $field => $error) + { + self::set_param_error($field, $error); + } + return FALSE; + } + + return TRUE; + + } + + } ?> diff --git a/model/mysql.php b/model/mysql.php index dfd163e..4239251 100644 --- a/model/mysql.php +++ b/model/mysql.php @@ -6,6 +6,14 @@ class model_mysql extends model_base public static function create($values) { self::verify(); + $validation_check = self::validate($values); + + if($validation_check === FALSE) + { + self::set_error("Please correct the fields highlighted below"); + return FALSE; + } + $values = self::sanitize($values); // Check for on duplicate key update fields @@ -60,6 +68,34 @@ public static function update_by_id($values) return TRUE; } + // UPDATE WHERE + public static function update_where($values, $update_condition) + { + self::verify(); + + $values = self::sanitize($values); + $string_vals = array(); + foreach($values as $var => $val) + { + $string_vals[] = $var . " = " . mysql::quote($val); + } + + if(count($string_vals) == 0) + { + self::set_error('No valid fields to update'); + return FALSE; + } + + $sql = "UPDATE ".static::$table." SET " . implode(', ', $string_vals) . " WHERE {$update_condition}"; + $response = mysql::query(static::$database, $sql); + if($response === FALSE) + { + self::set_error(mysql::get_errors()); + return FALSE; + } + return TRUE; + } + // GET BY FIELD public static function get_by_field($values) { @@ -133,5 +169,7 @@ public static function delete_by_id($values) $sql = "DELETE FROM ".static::$table." WHERE ".static::$id." = ".mysql::quote($values[static::$id]); return mysql::query(static::$database, $sql); } + + } ?> diff --git a/mysql.php b/mysql.php index ed8869d..9244fb3 100755 --- a/mysql.php +++ b/mysql.php @@ -38,6 +38,8 @@ public static function insert($alias, $table, $values, $on_dupkey_update = array { return self::last_insert_id($alias); } + + print $sql; exit; return FALSE; } @@ -78,11 +80,11 @@ public static function query($alias, $sql, $cache = FALSE, $update_cache = FALSE } // Execute our query and get the result - $result = mysql_query($sql, $connection); + $result = mysqli_query($connection, $sql); if(!$result) { - self::set_error(mysql_error()); + self::set_error(mysqli_error($connection)); return FALSE; } @@ -96,7 +98,7 @@ public static function query($alias, $sql, $cache = FALSE, $update_cache = FALSE // Build result set array and return default: $array_result = array(); - while($row = mysql_fetch_assoc($result)) + while($row = mysqli_fetch_assoc($result)) { $array_result[] = $row; } @@ -146,7 +148,7 @@ public static function last_insert_id($alias) return FALSE; } - return mysql_insert_id($connection); + return mysqli_insert_id($connection); } @@ -158,7 +160,7 @@ public static function rows_affected($alias) return FALSE; } - return mysql_affected_rows($connection); + return mysqli_affected_rows($connection); } @@ -194,7 +196,7 @@ public static function escape($string) return FALSE; } } - return mysql_real_escape_string($string); + return mysqli_real_escape_string(self::get_connection(key(self::$connections)), $string); } public static function quote($string) @@ -254,13 +256,13 @@ private static function get_connection($alias) private static function create_connection($alias) { - if(self::$connections[$alias] = mysql_connect(self::$config[$alias]['host'], self::$config[$alias]['username'], self::$config[$alias]['password'])) + if(self::$connections[$alias] = mysqli_connect(self::$config[$alias]['host'], self::$config[$alias]['username'], self::$config[$alias]['password'])) { return TRUE; } else { - self::set_error(mysql_error()); + self::set_error(mysqli_error()); return FALSE; } } @@ -269,7 +271,7 @@ private static function select_db($alias, $database) { if(!isset(self::$selected_database[$alias]) || self::$selected_database[$alias] != $database) { - mysql_select_db($database, self::$connections[$alias]); + mysqli_select_db(self::$connections[$alias], $database); $selected_database[$alias] = $database; } diff --git a/redis.php b/redis.php index 989734e..18f41df 100644 --- a/redis.php +++ b/redis.php @@ -76,7 +76,8 @@ protected function reportError($msg) */ public static function send_command($alias) { - return self::_send($alias, func_get_args()); + //return self::_send($alias, func_get_args()); + return self::_send($alias, array_slice(func_get_args(), 1)); } protected static function _send($alias, $args) @@ -224,9 +225,17 @@ public static function multi($alias) public static function sadd($alias, $set, $value) { - if (!is_array($value)) $value = func_get_args(); - else array_unshift($value, $set); - return self::_send($alias, array('sadd', $value)); + if(is_array($value)) + { + array_unshift($value, $set); + } + else + { + $value = array($set, $value); + } + + array_unshift($value, 'sadd'); + return self::_send($alias, $value); } public static function smembers($alias, $set) @@ -837,7 +846,9 @@ public static function zrevrangebyscore($alias,$key, $max, $min, $withscores = f $args[] = $limit[0]; $args[] = $limit[1]; } - return self::_send($alias,array('zRevRangeByScore', $args)); + array_unshift($args, 'zRevRangeByScore'); + + return self::_send($alias,$args); } public static function zrevrank($alias,$key, $member)