| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
6 |
* (c) 2004-2006 Sean Kerr <sean@code-box.org>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
/**
|
|
|
13 |
* Base class for all sfStorage that uses a sfDatabase object as a storage.
|
|
|
14 |
*
|
|
|
15 |
* @package symfony
|
|
|
16 |
* @subpackage storage
|
|
|
17 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
18 |
* @author Sean Kerr <sean@code-box.org>
|
|
|
19 |
* @version SVN: $Id: sfDatabaseSessionStorage.class.php 22037 2009-09-15 11:00:20Z fabien $
|
|
|
20 |
*/
|
|
|
21 |
abstract class sfDatabaseSessionStorage extends sfSessionStorage
|
|
|
22 |
{
|
|
|
23 |
protected
|
|
|
24 |
$db = null,
|
|
|
25 |
$con = null;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Available options:
|
|
|
29 |
*
|
|
|
30 |
* * db_table: The database table in which session data will be stored
|
|
|
31 |
* * database: The sfDatabase object to use
|
|
|
32 |
* * db_id_col: The database column in which the session id will be stored (sess_id by default)
|
|
|
33 |
* * db_data_col: The database column in which the session data will be stored (sess_data by default)
|
|
|
34 |
* * db_time_col: The database column in which the session timestamp will be stored (sess_time by default)
|
|
|
35 |
*
|
|
|
36 |
* @param array $options An associative array of options
|
|
|
37 |
*
|
|
|
38 |
* @see sfSessionStorage
|
|
|
39 |
*/
|
|
|
40 |
public function initialize($options = array())
|
|
|
41 |
{
|
|
|
42 |
$options = array_merge(array(
|
|
|
43 |
'db_id_col' => 'sess_id',
|
|
|
44 |
'db_data_col' => 'sess_data',
|
|
|
45 |
'db_time_col' => 'sess_time',
|
|
|
46 |
), $options);
|
|
|
47 |
|
|
|
48 |
// disable auto_start
|
|
|
49 |
$options['auto_start'] = false;
|
|
|
50 |
|
|
|
51 |
// initialize the parent
|
|
|
52 |
parent::initialize($options);
|
|
|
53 |
|
|
|
54 |
if (!isset($this->options['db_table']))
|
|
|
55 |
{
|
|
|
56 |
throw new sfInitializationException('You must provide a "db_table" option to sfDatabaseSessionStorage.');
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
if (!isset($this->options['database']))
|
|
|
60 |
{
|
|
|
61 |
throw new sfInitializationException('You must provide a "database" option to sfDatabaseSessionStorage.');
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
// use this object as the session handler
|
|
|
65 |
session_set_save_handler(array($this, 'sessionOpen'),
|
|
|
66 |
array($this, 'sessionClose'),
|
|
|
67 |
array($this, 'sessionRead'),
|
|
|
68 |
array($this, 'sessionWrite'),
|
|
|
69 |
array($this, 'sessionDestroy'),
|
|
|
70 |
array($this, 'sessionGC'));
|
|
|
71 |
|
|
|
72 |
// start our session
|
|
|
73 |
session_start();
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Closes a session.
|
|
|
78 |
*
|
|
|
79 |
* @return boolean true, if the session was closed, otherwise false
|
|
|
80 |
*/
|
|
|
81 |
public function sessionClose()
|
|
|
82 |
{
|
|
|
83 |
// do nothing
|
|
|
84 |
return true;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Opens a session.
|
|
|
89 |
*
|
|
|
90 |
* @param string $path (ignored)
|
|
|
91 |
* @param string $name (ignored)
|
|
|
92 |
*
|
|
|
93 |
* @return boolean true, if the session was opened, otherwise an exception is thrown
|
|
|
94 |
*
|
|
|
95 |
* @throws <b>DatabaseException</b> If a connection with the database does not exist or cannot be created
|
|
|
96 |
*/
|
|
|
97 |
public function sessionOpen($path = null, $name = null)
|
|
|
98 |
{
|
|
|
99 |
// what database are we using?
|
|
|
100 |
$database = $this->options['database'];
|
|
|
101 |
|
|
|
102 |
// get the database and connection
|
|
|
103 |
$databaseClass = get_class($database);
|
|
|
104 |
if($databaseClass == 'sfPropelDatabase')
|
|
|
105 |
{
|
|
|
106 |
$this->db = Propel::getConnection($database->getParameter('name'));
|
|
|
107 |
}
|
|
|
108 |
elseif($databaseClass == 'sfDoctrineDatabase')
|
|
|
109 |
{
|
|
|
110 |
$this->db = $database->getConnection();
|
|
|
111 |
}
|
|
|
112 |
else
|
|
|
113 |
{
|
|
|
114 |
$this->db = $database->getResource();
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
$this->con = $database->getConnection();
|
|
|
118 |
|
|
|
119 |
if (null === $this->db && null === $this->con)
|
|
|
120 |
{
|
|
|
121 |
throw new sfDatabaseException('Database connection does not exist. Unable to open session.');
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
return true;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Destroys a session.
|
|
|
129 |
*
|
|
|
130 |
* @param string $id A session ID
|
|
|
131 |
*
|
|
|
132 |
* @return bool true, if the session was destroyed, otherwise an exception is thrown
|
|
|
133 |
*
|
|
|
134 |
* @throws <b>DatabaseException</b> If the session cannot be destroyed
|
|
|
135 |
*/
|
|
|
136 |
abstract public function sessionDestroy($id);
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Cleans up old sessions.
|
|
|
140 |
*
|
|
|
141 |
* @param int $lifetime The lifetime of a session
|
|
|
142 |
*
|
|
|
143 |
* @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
|
|
|
144 |
*
|
|
|
145 |
* @throws <b>DatabaseException</b> If any old sessions cannot be cleaned
|
|
|
146 |
*/
|
|
|
147 |
abstract public function sessionGC($lifetime);
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Reads a session.
|
|
|
151 |
*
|
|
|
152 |
* @param string $id A session ID
|
|
|
153 |
*
|
|
|
154 |
* @return bool true, if the session was read, otherwise an exception is thrown
|
|
|
155 |
*
|
|
|
156 |
* @throws <b>DatabaseException</b> If the session cannot be read
|
|
|
157 |
*/
|
|
|
158 |
abstract public function sessionRead($id);
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Writes session data.
|
|
|
162 |
*
|
|
|
163 |
* @param string $id A session ID
|
|
|
164 |
* @param string $data A serialized chunk of session data
|
|
|
165 |
*
|
|
|
166 |
* @return bool true, if the session was written, otherwise an exception is thrown
|
|
|
167 |
*
|
|
|
168 |
* @throws <b>DatabaseException</b> If the session data cannot be written
|
|
|
169 |
*/
|
|
|
170 |
abstract public function sessionWrite($id, $data);
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Regenerates id that represents this storage.
|
|
|
174 |
*
|
|
|
175 |
* @param boolean $destroy Destroy session when regenerating?
|
|
|
176 |
*
|
|
|
177 |
* @return boolean True if session regenerated, false if error
|
|
|
178 |
*
|
|
|
179 |
*/
|
|
|
180 |
public function regenerate($destroy = false)
|
|
|
181 |
{
|
|
|
182 |
if (self::$sessionIdRegenerated)
|
|
|
183 |
{
|
|
|
184 |
return;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
$currentId = session_id();
|
|
|
188 |
|
|
|
189 |
parent::regenerate($destroy);
|
|
|
190 |
|
|
|
191 |
$newId = session_id();
|
|
|
192 |
$this->sessionRead($newId);
|
|
|
193 |
|
|
|
194 |
return $this->sessionWrite($newId, $this->sessionRead($currentId));
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
/**
|
|
|
198 |
* Executes the shutdown procedure.
|
|
|
199 |
*
|
|
|
200 |
*/
|
|
|
201 |
public function shutdown()
|
|
|
202 |
{
|
|
|
203 |
parent::shutdown();
|
|
|
204 |
}
|
|
|
205 |
}
|