/usr/share/php/kohana3.1/modules/database/classes/kohana/session/database.php is in libkohana3.1-mod-database-php 3.1.5-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | <?php defined('SYSPATH') OR die('No direct script access.');
/**
* Database-based session class.
*
* Sample schema:
*
* CREATE TABLE `sessions` (
* `session_id` VARCHAR( 24 ) NOT NULL,
* `last_active` INT UNSIGNED NOT NULL,
* `contents` TEXT NOT NULL,
* PRIMARY KEY ( `session_id` ),
* INDEX ( `last_active` )
* ) ENGINE = MYISAM ;
*
* @package Kohana/Database
* @category Session
* @author Kohana Team
* @copyright (c) 2008-2009 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_Session_Database extends Session {
// Database instance
protected $_db;
// Database table name
protected $_table = 'sessions';
// Database column names
protected $_columns = array(
'session_id' => 'session_id',
'last_active' => 'last_active',
'contents' => 'contents'
);
// Garbage collection requests
protected $_gc = 500;
// The current session id
protected $_session_id;
// The old session id
protected $_update_id;
public function __construct(array $config = NULL, $id = NULL)
{
if ( ! isset($config['group']))
{
// Use the default group
$config['group'] = Database::$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();
}
}
public function id()
{
return $this->_session_id;
}
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 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 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;
}
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 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);
}
} // End Session_Database
|