From 9fe9eb7acf9e27965a0ffa5d53adfdafd043fc71 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Sun, 15 Dec 2013 22:59:39 -0500 Subject: [PATCH] Working on this RedisModel class Got it working fairly well, just trying to get all of my assumption / opinions baked in. Still need to come up with a decent way to generate session hashes to help avoid session collision . Incidentally Retwis seems to be susceptible to this. --- classes/CustomModule.php | 16 -------- classes/CustomRedis.php | 4 +- classes/RedisModel.php | 83 ++++++++++++++++++++++++++++++++++++++++ models/User.php | 2 +- modules/user/create.php | 73 +++++++++++++++++++++++++---------- 5 files changed, 140 insertions(+), 38 deletions(-) diff --git a/classes/CustomModule.php b/classes/CustomModule.php index 4c588dd..221b4b9 100644 --- a/classes/CustomModule.php +++ b/classes/CustomModule.php @@ -9,22 +9,6 @@ class CustomModule extends Module parent::__construct(); $this->redis = new CustomRedis(); - - /* - $datasource = $this->config->datasources['redis']; - - try - { - $this->redis = new Redis(); - $this->redis->connect($datasource['hostname'], $datasource['port']); - $this->redis->setOption(Redis::OPT_PREFIX, $datasource['namespace'] . ':'); - $this->redis->select($datasource['database']); - } - catch (RedisException $e) - { - - } - */ } } diff --git a/classes/CustomRedis.php b/classes/CustomRedis.php index d27c372..6b6b4a6 100644 --- a/classes/CustomRedis.php +++ b/classes/CustomRedis.php @@ -12,7 +12,7 @@ class CustomRedis extends Object try { - $this->redis = new Redis(); + $this->redis = parent::getInstance('Redis'); // new Redis(); $this->redis->connect($datasource['hostname'], $datasource['port']); $this->redis->setOption(Redis::OPT_PREFIX, $datasource['namespace'] . ':'); $this->redis->select($datasource['database']); @@ -23,6 +23,8 @@ class CustomRedis extends Object } } + //var_dump($name, $arguments); + return call_user_func_array(array($this->redis, $name), $arguments); } } diff --git a/classes/RedisModel.php b/classes/RedisModel.php index c77126b..4182061 100644 --- a/classes/RedisModel.php +++ b/classes/RedisModel.php @@ -2,7 +2,90 @@ class RedisModel extends Object { + protected $redis = false; + protected $prefix = false; + public function __construct() + { + parent::__construct(); + + $this->redis = new CustomRedis(); + } + + public function key() + { + $parts = func_get_args(); + + if ($this->prefix) + { + array_unshift($parts, $this->prefix); + } + + return strtolower(implode(':', $parts)); + } + + public function nextUID() + { + return $this->redis->incr($this->key('uid')); + } + + public function getUID($variable, $value) + { + return $this->redis->get($this->key($variable, $value, 'uid')); + } + + public function __call($name, $arguments) + { + $name = strtolower($name); + + if ($name == 'set') + { + // Grabs our variables + $uid = $arguments[0]; + $variables = $arguments[1]; + $arguments = array(); + + // Assembles our new arguments + foreach ($variables as $key => $value) + { + $arguments[$this->key($uid, $key)] = $value; + } + + // Sets us up for MSET or just SET + if (count($arguments) > 1) + { + $name = 'mset'; + $arguments = array($arguments); + } + else + { + $arguments = array(key($arguments), current($arguments)); + } + } + else + { + $base = substr($name, 0, 3); + + if (in_array($base, array('set', 'get'))) + { + $key = $this->key($arguments[0], substr($name, 3)); + $name = $base; + } + + switch ($base) + { + case 'set': + $arguments = array($key, $arguments[1]); + break; + + case 'get': + $arguments = array($key); + break; + } + } + + return call_user_func_array(array($this->redis, $name), $arguments); + } } ?> diff --git a/models/User.php b/models/User.php index 59f38e1..a4b4831 100644 --- a/models/User.php +++ b/models/User.php @@ -2,7 +2,7 @@ class User extends RedisModel { - + protected $prefix = 'user'; } ?> diff --git a/modules/user/create.php b/modules/user/create.php index 7c68e93..59c0823 100644 --- a/modules/user/create.php +++ b/modules/user/create.php @@ -2,29 +2,62 @@ class user_create extends CustomModule { - //protected $ajax = true; - //protected $method = 'POST'; - //protected $validate = array( - // 'username' => array( - // 'length:>:30' => 'Usernames may not be more than 30 characters.', - // 'regex:is:/[^a-z0-9-]+/i' => 'Usernames may only contain alphanumeric characters or dashes.', - // 'regex:is:/^(-.+|.+-)$/' => 'Usernames may not start or end with a dash.', - // 'regex:is:/-{2,}/' => 'Usernames may not have two or more dashes in a row.', - // ), - // 'email' => array( - // 'length:>:100' => 'Email addresses may not be more than 100 characters.', - // 'filter:email' => 'Your email address is invalid.', - // ), - // 'password' => array( - // 'length:<:8' => 'Passwords may not be less than 8 characters.', - // ), - //); +// protected $ajax = true; +// protected $method = 'POST'; +// protected $validate = array( +// 'email' => array( +// 'length:>:100' => 'Email addresses may not be more than 100 characters.', +// 'filter:email' => 'Your email address is invalid.', +// ), +// 'username' => array( +// 'length:<:4' => 'Usernames may not be less than 4 characters.', +// 'length:>:30' => 'Usernames may not be more than 50 characters.', +// 'regex:is:/[^a-z0-9]+/i' => 'Usernames may only contain letters and numbers.', +// ), +// 'password' => array( +// 'length:<:8' => 'Passwords may not be less than 8 characters.', +// ), +// ); public function __default() { - var_dump($this->redis->set('test', sha1(microtime(true)))); - var_dump($this->redis->get('test')); - var_dump($this->redis->info()); + try + { + $_POST = array( + 'email' => 'foo@bar.com', + 'username' => 'fubar', + 'password' => 'insecure123', + ); + + + $user = new User(); + + // Checks if the email or username is already in use + foreach (array('email', 'username') as $field) + { + if ($user->getUID($field, $_POST[$field])) + { + return array('error' => 'The ' . $field . ' is already in use.'); + } + } + + // Grabs the next UID + $uid = $user->nextUID(); + + // Writes the user data + $user->set('1001', array( + 'username' => $_POST['username'], + 'email' => $_POST['email'], + 'password' => crypt($_POST['password'], '$2y$11$' . String::random(22) . '$'), + )); + + // Sets up the authentication cookie + //$user->setAuth() + } + catch (RedisException $e) + { + return array('error' => $e->getMessage()); + } } }