From 6b4f48035c88e40dcee589eefa0578571d055092 Mon Sep 17 00:00:00 2001 From: Alessandro Arnodo Date: Mon, 27 Feb 2012 15:37:06 +0100 Subject: [PATCH] added MY_Form_validation plus other fix --- config/cimongo.php | 11 +++++-- libraries/MY_Form_validation.php | 44 +++++++++++++++++++++++++ libraries/MY_Session.php | 48 ++++++++++++++++------------ libraries/cimongo/Cimongo.php | 30 ++++++++--------- libraries/cimongo/Cimongo_cursor.php | 20 ++++++------ libraries/cimongo/Cimongo_extras.php | 2 +- 6 files changed, 107 insertions(+), 48 deletions(-) create mode 100644 libraries/MY_Form_validation.php diff --git a/config/cimongo.php b/config/cimongo.php index f43ff6a..568eb5c 100644 --- a/config/cimongo.php +++ b/config/cimongo.php @@ -1,7 +1,7 @@ CI->load->library("cimongo/cimongo"); + parent::__construct($rules); + } + + /** + * is_unique + * + */ + public function is_unique($str, $field) + { + list($table, $field)=explode('.', $field); + $query = $this->CI->cimongo->limit(1)->get_where($table, array($field => $str)); + + return $query->num_rows() === 0; + } + +} \ No newline at end of file diff --git a/libraries/MY_Session.php b/libraries/MY_Session.php index 4a52be0..e450824 100644 --- a/libraries/MY_Session.php +++ b/libraries/MY_Session.php @@ -23,14 +23,19 @@ */ class MY_Session extends CI_Session { + protected $sess_use_mongo = FALSE; + protected $sess_collection_name = FALSE; + public function __construct($rules = array()) { log_message('debug', '*** Hello from MY_Session ***'); - $this->CI =& get_instance(); + $this->CI->load->config('cimongo'); + $this->sess_use_mongo = $this->CI->config->item('sess_use_mongo'); + $this->sess_collection_name = $this->CI->config->item('sess_collection_name'); $this->CI->load->library("cimongo/cimongo"); parent::__construct(); - + } /** @@ -103,7 +108,7 @@ function sess_read() } // Is there a corresponding session in the DB? - if ($this->sess_use_database === TRUE) + if ($this->sess_use_mongo === TRUE) { $this->CI->cimongo->where(array('session_id'=>$session['session_id'])); @@ -114,10 +119,10 @@ function sess_read() if ($this->sess_match_useragent == TRUE) { - $this->CI->db->where(array('user_agent'=>$session['user_agent'])); + $this->CI->cimongo->where(array('user_agent'=>$session['user_agent'])); } - $query = $this->CI->cimongo->get($this->sess_table_name); + $query = $this->CI->cimongo->get($this->sess_collection_name); // No result? Kill it! if ($query->num_rows() == 0) @@ -159,7 +164,7 @@ function sess_read() function sess_write() { // Are we saving custom data to the DB? If not, all we do is update the cookie - if ($this->sess_use_database === FALSE) + if ($this->sess_use_mongo === FALSE) { $this->_set_cookie(); return; @@ -192,7 +197,7 @@ function sess_write() // Run the update query $this->CI->cimongo->where(array('session_id'=>$this->userdata['session_id'])); - $this->CI->cimongo->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata)); + $this->CI->cimongo->update($this->sess_collection_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata)); // Write the cookie. Notice that we manually pass the cookie data array to the // _set_cookie() function. Normally that function will store $this->userdata, but @@ -227,9 +232,9 @@ function sess_create() // Save the data to the DB if needed - if ($this->sess_use_database === TRUE) + if ($this->sess_use_mongo === TRUE) { - $this->CI->cimongo->insert($this->sess_table_name, $this->userdata); + $this->CI->cimongo->insert($this->sess_collection_name, $this->userdata); } // Write the cookie @@ -275,7 +280,7 @@ function sess_update() $cookie_data = NULL; // Update the session ID and last_activity field in the DB if needed - if ($this->sess_use_database === TRUE) + if ($this->sess_use_mongo === TRUE) { // set cookie explicitly to only have our session data $cookie_data = array(); @@ -284,7 +289,7 @@ function sess_update() $cookie_data[$val] = $this->userdata[$val]; } - $this->CI->cimongo->where(array("session_id"=>$old_sessid))->update($this->sess_table_name, array('last_activity' => $this->now, 'session_id' => $new_sessid)); + $this->CI->cimongo->where(array("session_id"=>$old_sessid))->update($this->sess_collection_name, array('last_activity' => $this->now, 'session_id' => $new_sessid)); } // Write the cookie @@ -300,20 +305,20 @@ function sess_update() function sess_destroy() { // Kill the session DB row - if ($this->sess_use_database === TRUE AND isset($this->userdata['session_id'])) + if ($this->sess_use_mongo === TRUE AND isset($this->userdata['session_id'])) { $this->CI->cimongo->where(array('session_id'=>$this->userdata['session_id'])); - $this->CI->cimongo->delete($this->sess_table_name); + $this->CI->cimongo->delete($this->sess_collection_name); } // Kill the cookie setcookie( - $this->sess_cookie_name, - addslashes(serialize(array())), - ($this->now - 31500000), - $this->cookie_path, - $this->cookie_domain, - 0 + $this->sess_cookie_name, + addslashes(serialize(array())), + ($this->now - 31500000), + $this->cookie_path, + $this->cookie_domain, + 0 ); } @@ -329,7 +334,7 @@ function sess_destroy() */ function _sess_gc() { - if ($this->sess_use_database != TRUE) + if ($this->sess_use_mongo != TRUE) { return; } @@ -339,8 +344,9 @@ function _sess_gc() { $expire = $this->now - $this->sess_expiration; + $this->CI->cimongo->where(array("last_activity" =>array('$lt'=>$expire))); - $this->CI->cimongo->delete($this->sess_table_name); + $this->CI->cimongo->delete($this->sess_collection_name); log_message('debug', 'Session garbage collection performed.'); } diff --git a/libraries/cimongo/Cimongo.php b/libraries/cimongo/Cimongo.php index 5404f47..67cbc09 100644 --- a/libraries/cimongo/Cimongo.php +++ b/libraries/cimongo/Cimongo.php @@ -23,7 +23,7 @@ * @since v1.0 */ class Cimongo extends Cimongo_extras{ - + private $_inserted_id = FALSE; /** @@ -47,7 +47,7 @@ public function get($collection = "", $limit = FALSE, $offset = FALSE){ } $cursor = $this->db->{$collection}->find($this->wheres, $this->selects); $cimongo_cursor = new Cimongo_cursor($cursor); - + $this->limit=($limit!==FALSE && is_numeric($limit))?$limit:$this->limit; if($this->limit!==FALSE){ $cimongo_cursor->limit($this->limit); @@ -452,28 +452,28 @@ public function delete($collection = ""){ } } - + /** - * - * Limit results - * - * @since v1.1.0 - */ + * + * Limit results + * + * @since v1.1.0 + */ public function limit($limit = FALSE){ if ($limit && is_numeric($limit)){ $this->limit = $limit; } return $this; } - + /** - * - * Returns the last inserted document's id - * - * @since v1.1.0 - */ + * + * Returns the last inserted document's id + * + * @since v1.1.0 + */ public function insert_id(){ return $this->_inserted_id; } - + } \ No newline at end of file diff --git a/libraries/cimongo/Cimongo_cursor.php b/libraries/cimongo/Cimongo_cursor.php index 3403ce3..34986b9 100644 --- a/libraries/cimongo/Cimongo_cursor.php +++ b/libraries/cimongo/Cimongo_cursor.php @@ -28,7 +28,7 @@ class Cimongo_cursor extends Cimongo_base * @since v1.0.0 */ protected $_cursor; - + /** * Construct a new Cimongo_extras * @@ -56,12 +56,12 @@ public function result($as_object=TRUE){ return $result; } - + /** - * Check if cursor is iterable, but maybe this could be done better FIXME - * - * @since v1.1.0 - */ + * Check if cursor is iterable, but maybe this could be done better FIXME + * + * @since v1.1.0 + */ public function has_error(){ try { $this->_cursor->next(); @@ -69,9 +69,9 @@ public function has_error(){ return $this->_handle_exception($exception->getMessage(),$as_object); } return FALSE; - + } - + /** * Returns query results as an array * @@ -118,7 +118,7 @@ public function row($index=0, $class=NULL, $as_object=TRUE){ } } return $res; - } + } /** * Returns the document at the specified index as an array @@ -173,6 +173,8 @@ public function count($foundOnly = FALSE) { $count = array(); try { $count = $this->_cursor->count($foundOnly); + }catch (MongoCursorException $exception){ + show_error($exception->getMessage(), 500); }catch (MongoConnectionException $exception){ show_error($exception->getMessage(), 500); } diff --git a/libraries/cimongo/Cimongo_extras.php b/libraries/cimongo/Cimongo_extras.php index e6cdf9b..2293203 100644 --- a/libraries/cimongo/Cimongo_extras.php +++ b/libraries/cimongo/Cimongo_extras.php @@ -275,7 +275,7 @@ public function inc($fields = array(), $value = 0){ $this->updates['$inc'][$field] = $value; } } - return $this; + return $this; } /**