Skip to content

Commit

Permalink
fixed bugs in two redis methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris DeGroat committed Jul 24, 2014
1 parent 464d928 commit 3f5f0b0
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 16 deletions.
4 changes: 3 additions & 1 deletion autoload.php
@@ -1,7 +1,9 @@
<?


function crossbar_autoload($class_name)
{
include_once str_replace('_', '/', $class_name) . '.php';
include_once str_replace('\\', '/', str_replace('_', '/', $class_name)) . '.php';
}

spl_autoload_register('crossbar_autoload', FALSE);
Expand Down
4 changes: 3 additions & 1 deletion crossbar.php
Expand Up @@ -26,6 +26,7 @@ public function __construct($script_mode = FALSE)
$this->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 = '';
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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)
{
Expand Down
12 changes: 12 additions & 0 deletions misc.php
Expand Up @@ -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;
}
}

?>
84 changes: 84 additions & 0 deletions model/base.php
Expand Up @@ -3,6 +3,7 @@
class model_base
{
private static $errors = array();
private static $param_errors = array();

// FIELDS
public static function fields($method = 'create')
Expand Down Expand Up @@ -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()
{
Expand All @@ -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;

}


}

?>
38 changes: 38 additions & 0 deletions model/mysql.php
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}


}
?>
20 changes: 11 additions & 9 deletions mysql.php
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -146,7 +148,7 @@ public static function last_insert_id($alias)
return FALSE;
}

return mysql_insert_id($connection);
return mysqli_insert_id($connection);

}

Expand All @@ -158,7 +160,7 @@ public static function rows_affected($alias)
return FALSE;
}

return mysql_affected_rows($connection);
return mysqli_affected_rows($connection);

}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}

Expand Down
21 changes: 16 additions & 5 deletions redis.php
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3f5f0b0

Please sign in to comment.