Skip to content

Commit

Permalink
added MY_Form_validation plus other fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Arnodo committed Feb 27, 2012
1 parent c465f6e commit 6b4f480
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 48 deletions.
11 changes: 9 additions & 2 deletions config/cimongo.php
@@ -1,7 +1,7 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// Generally localhost
$config['host'] = "";
$config['host'] = "localhost";
// Generally 27017
$config['port'] = 27017;
// The database you want to work on
Expand All @@ -17,4 +17,11 @@
$config['query_safety'] = TRUE;

//If running in auth mode and the user does not have global read/write then set this to true
$config['db_flag'] = TRUE;
$config['db_flag'] = TRUE;

/*
*Consider these config only if you want to store the session into mongoDB, they will be used in MY_Session.php
*/
$config['sess_use_mongo'] = TRUE;
$config['sess_collection_name'] = 'ci_sessions';

44 changes: 44 additions & 0 deletions libraries/MY_Form_validation.php
@@ -0,0 +1,44 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter MongoDB Library
*
* A library to interact with the NoSQL database MongoDB.
* For more information see http://www.mongodb.org
*
* @package CodeIgniter
* @author Alessandro Arnodo | a.arnodo@gmail.com | @vesparny
* @copyright Copyright (c) 2012, Alessandro Arnodo.
* @license http://www.opensource.org/licenses/mit-license.php
* @link
* @version Version 1.1.0
*
*/

/**
* MY_Form_validation
*
* Override core methods forcing them to interact with MongoDB.
* @since v1.1
*/
class MY_Form_validation extends CI_Form_validation{

public function __construct($rules = array())
{
log_message('debug', '*** Hello from MY_Session ***');
$this->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;
}

}
48 changes: 27 additions & 21 deletions libraries/MY_Session.php
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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']));

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

Expand All @@ -329,7 +334,7 @@ function sess_destroy()
*/
function _sess_gc()
{
if ($this->sess_use_database != TRUE)
if ($this->sess_use_mongo != TRUE)
{
return;
}
Expand All @@ -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.');
}
Expand Down
30 changes: 15 additions & 15 deletions libraries/cimongo/Cimongo.php
Expand Up @@ -23,7 +23,7 @@
* @since v1.0
*/
class Cimongo extends Cimongo_extras{

private $_inserted_id = FALSE;

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

}
20 changes: 11 additions & 9 deletions libraries/cimongo/Cimongo_cursor.php
Expand Up @@ -28,7 +28,7 @@ class Cimongo_cursor extends Cimongo_base
* @since v1.0.0
*/
protected $_cursor;

/**
* Construct a new Cimongo_extras
*
Expand Down Expand Up @@ -56,22 +56,22 @@ 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();
}catch (Exception $exception){
return $this->_handle_exception($exception->getMessage(),$as_object);
}
return FALSE;

}

/**
* Returns query results as an array
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/cimongo/Cimongo_extras.php
Expand Up @@ -275,7 +275,7 @@ public function inc($fields = array(), $value = 0){
$this->updates['$inc'][$field] = $value;
}
}
return $this;
return $this;
}

/**
Expand Down

0 comments on commit 6b4f480

Please sign in to comment.