Session_Database
Kohana_Session_Database
Session
Kohana_Session

Class Contents

Class declared in MODPATH/database/classes/session/database.php on line 3.

Properties

public static $default
string(6) "native"

Methods

public __construct( )
Kohana_Session_Database

Source Code
public function __construct(array $config = NULL, $id = NULL)
{
	if ( ! isset($config['group']))
	{
		// Use the default group
		$config['group'] = 'default';
	}

	// Load the database
	$this->_db = Database::instance($config['group']);

	if (isset($config['table']))
	{
		// Set the table name
		$this->_table = (string) $config['table'];
	}

	if (isset($config['gc']))
	{
		// Set the gc chance
		$this->_gc = (int) $config['gc'];
	}

	if (isset($config['columns']))
	{
		// Overload column names
		$this->_columns = $config['columns'];
	}

	parent::__construct($config, $id);

	if (mt_rand(0, $this->_gc) === $this->_gc)
	{
		// Run garbage collection
		// This will average out to run once every X requests
		$this->_gc();
	}
}

protected _destroy( )
Kohana_Session_Database

Source Code
protected function _destroy()
{
	if ($this->_update_id === NULL)
	{
		// Session has not been created yet
		return TRUE;
	}

	// Delete the current session
	$query = DB::delete($this->_table)
		->where($this->_columns['session_id'], '=', ':id')
		->param(':id', $this->_update_id);

	try
	{
		// Execute the query
		$query->execute($this->_db);

		// Delete the cookie
		Cookie::delete($this->_name);
	}
	catch (Exception $e)
	{
		// An error occurred, the session has not been deleted
		return FALSE;
	}

	return TRUE;
}

protected _gc( )
Kohana_Session_Database

Source Code
protected function _gc()
{
	if ($this->_lifetime)
	{
		// Expire sessions when their lifetime is up
		$expires = $this->_lifetime;
	}
	else
	{
		// Expire sessions after one month
		$expires = Date::MONTH;
	}

	// Delete all sessions that have expired
	DB::delete($this->_table)
		->where($this->_columns['last_active'], '<', ':time')
		->param(':time', time() - $expires)
		->execute($this->_db);
}

protected _read( )
Kohana_Session_Database

Source Code
protected function _read($id = NULL)
{
	if ($id OR $id = Cookie::get($this->_name))
	{
		$result = DB::select(array($this->_columns['contents'], 'contents'))
			->from($this->_table)
			->where($this->_columns['session_id'], '=', ':id')
			->limit(1)
			->param(':id', $id)
			->execute($this->_db);

		if ($result->count())
		{
			// Set the current session id
			$this->_session_id = $this->_update_id = $id;

			// Return the contents
			return $result->get('contents');
		}
	}

	// Create a new session id
	$this->_regenerate();

	return NULL;
}

protected _regenerate( )
Kohana_Session_Database

Source Code
protected function _regenerate()
{
	// Create the query to find an ID
	$query = DB::select($this->_columns['session_id'])
		->from($this->_table)
		->where($this->_columns['session_id'], '=', ':id')
		->limit(1)
		->bind(':id', $id);

	do
	{
		// Create a new session id
		$id = str_replace('.', '-', uniqid(NULL, TRUE));

		// Get the the id from the database
		$result = $query->execute($this->_db);
	}
	while ($result->count());

	return $this->_session_id = $id;
}

protected _write( )
Kohana_Session_Database

Source Code
protected function _write()
{
	if ($this->_update_id === NULL)
	{
		// Insert a new row
		$query = DB::insert($this->_table, $this->_columns)
			->values(array(':new_id', ':active', ':contents'));
	}
	else
	{
		// Update the row
		$query = DB::update($this->_table)
			->value($this->_columns['last_active'], ':active')
			->value($this->_columns['contents'], ':contents')
			->where($this->_columns['session_id'], '=', ':old_id');

		if ($this->_update_id !== $this->_session_id)
		{
			// Also update the session id
			$query->value($this->_columns['session_id'], ':new_id');
		}
	}

	$query
		->param(':new_id',   $this->_session_id)
		->param(':old_id',   $this->_update_id)
		->param(':active',   $this->_data['last_active'])
		->param(':contents', $this->__toString());

	// Execute the query
	$query->execute($this->_db);

	// The update and the session id are now the same
	$this->_update_id = $this->_session_id;

	// Update the cookie with the new session id
	Cookie::set($this->_name, $this->_session_id, $this->_lifetime);

	return TRUE;
}

public id( )
Kohana_Session_Database

Source Code
public function id()
{
	return $this->_session_id;
}

public __toString( )
Kohana_Session

Source Code
public function __toString()
{
	// Serialize the data array
	$data = serialize($this->_data);

	if ($this->_encrypted)
	{
		// Encrypt the data using the default key
		$data = Encrypt::instance($this->_encrypted)->encode($data);
	}
	else
	{
		// Obfuscate the data with base64 encoding
		$data = base64_encode($data);
	}

	return $data;
}

public as_array( )
Kohana_Session

Source Code
public function & as_array()
{
	return $this->_data;
}

public bind( )
Kohana_Session

Source Code
public function bind($key, & $value)
{
	$this->_data[$key] =& $value;

	return $this;
}

public delete( )
Kohana_Session

Source Code
public function delete($key)
{
	$args = func_get_args();

	foreach ($args as $key)
	{
		unset($this->_data[$key]);
	}

	return $this;
}

public destroy( )
Kohana_Session

Source Code
public function destroy()
{
	if ($this->_destroyed === FALSE)
	{
		if ($this->_destroyed = $this->_destroy())
		{
			// The session has been destroyed, clear all data
			$this->_data = array();
		}
	}

	return $this->_destroyed;
}

public get( )
Kohana_Session

Source Code
public function get($key, $default = NULL)
{
	return array_key_exists($key, $this->_data) ? $this->_data[$key] : $default;
}

public get_once( )
Kohana_Session

Source Code
public function get_once($key, $default = NULL)
{
	$value = $this->get($key, $default);

	unset($this->_data[$key]);

	return $value;
}

public static instance( )
Kohana_Session

Source Code
public static function instance($type = NULL, $id = NULL)
{
	if ($type === NULL)
	{
		// Use the default type
		$type = Session::$default;
	}

	if ( ! isset(Session::$instances[$type]))
	{
		// Load the configuration for this type
		$config = Kohana::config('session')->get($type);

		// Set the session class name
		$class = 'Session_'.ucfirst($type);

		// Create a new session instance
		Session::$instances[$type] = $session = new $class($config, $id);

		// Write the session at shutdown
		register_shutdown_function(array($session, 'write'));
	}

	return Session::$instances[$type];
}

public name( )
Kohana_Session

Source Code
public function name()
{
	return $this->_name;
}

public read( )
Kohana_Session

Source Code
public function read($id = NULL)
{
	if (is_string($data = $this->_read($id)))
	{
		try
		{
			if ($this->_encrypted)
			{
				// Decrypt the data using the default key
				$data = Encrypt::instance($this->_encrypted)->decode($data);
			}
			else
			{
				// Decode the base64 encoded data
				$data = base64_decode($data);
			}

			// Unserialize the data
			$data = unserialize($data);
		}
		catch (Exception $e)
		{
			// Ignore all reading errors
		}
	}

	if (is_array($data))
	{
		// Load the data locally
		$this->_data = $data;
	}
}

public regenerate( )
Kohana_Session

Source Code
public function regenerate()
{
	return $this->_regenerate();
}

public set( )
Kohana_Session

Source Code
public function set($key, $value)
{
	$this->_data[$key] = $value;

	return $this;
}

public write( )
Kohana_Session

Source Code
public function write()
{
	if (headers_sent() OR $this->_destroyed)
	{
		// Session cannot be written when the headers are sent or when
		// the session has been destroyed
		return FALSE;
	}

	// Set the last active timestamp
	$this->_data['last_active'] = time();

	try
	{
		return $this->_write();
	}
	catch (Exception $e)
	{
		// Log & ignore all errors when a write fails
		Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e))->write();

		return FALSE;
	}
}