Development

/branches/1.0/lib/storage/sfPDOSessionStorage.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/storage/sfPDOSessionStorage.class.php

Revision 7791, 8.3 kB (checked in by fabien, 6 years ago)

updated Sean Kerr email address

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004, 2005 Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) 2004, 2005 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  * Provides support for session storage using a PDO database abstraction layer.
14  *
15  * <b>Required parameters:</b>
16  *
17  * # <b>db_table</b> - [none] - The database table in which session data will be stored.
18  *
19  * <b>Optional parameters:</b>
20  *
21  * # <b>database</b>     - [default]   - The database connection to use (see databases.yml).
22  * # <b>db_id_col</b>    - [sess_id]   - The database column in which the session id will be stored.
23  * # <b>db_data_col</b>  - [sess_data] - The database column in which the session data will be stored.
24  * # <b>db_time_col</b>  - [sess_time] - The database column in which the session timestamp will be stored.
25  * # <b>session_name</b> - [symfony]   - The name of the session.
26  *
27  * @package    symfony
28  * @subpackage storage
29  * @author     Mathew Toth <developer@poetryleague.com>
30  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
31  * @author     Sean Kerr <sean@code-box.org>
32  * @version    SVN: $Id$
33  */
34 class sfPDOSessionStorage extends sfSessionStorage
35 {
36   /**
37    * PDO connection
38    * @var Connection
39    */
40   protected $db;
41
42   /**
43    * Initializes this Storage instance.
44    *
45    * @param sfContext A sfContext instance
46    * @param array     An associative array of initialization parameters
47    *
48    * @return boolean true, if initialization completes successfully, otherwise false
49    *
50    * @throws <b>InitializationException</b> If an error occurs while initializing this Storage
51    */
52   public function initialize($context, $parameters = null)
53   {
54     // disable auto_start
55     $parameters['auto_start'] = false;
56
57     // initialize the parent
58     parent::initialize($context, $parameters);
59
60     if (!$this->getParameterHolder()->has('db_table'))
61     {
62       // missing required 'db_table' parameter
63       $error = 'Factory configuration file is missing required "db_table" parameter for the Storage category';
64
65       throw new sfInitializationException($error);
66     }
67
68     // use this object as the session handler
69     session_set_save_handler(array($this, 'sessionOpen'),
70                              array($this, 'sessionClose'),
71                              array($this, 'sessionRead'),
72                              array($this, 'sessionWrite'),
73                              array($this, 'sessionDestroy'),
74                              array($this, 'sessionGC'));
75
76     // start our session
77     session_start();
78   }
79
80   /**
81   * Closes a session.
82   *
83   * @return boolean true, if the session was closed, otherwise false
84   */
85   public function sessionClose()
86   {
87     // do nothing
88     return true;
89   }
90
91   /**
92    * Destroys a session.
93    *
94    * @param string A session ID
95    *
96    * @return boolean true, if the session was destroyed, otherwise an exception is thrown
97    *
98    * @throws <b>DatabaseException</b> If the session cannot be destroyed
99    */
100   public function sessionDestroy($id)
101   {
102     // get table/column
103     $db_table  = $this->getParameterHolder()->get('db_table');
104     $db_id_col = $this->getParameterHolder()->get('db_id_col', 'sess_id');
105
106     // delete the record associated with this id
107     $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.'= ?';
108
109     try
110     {
111       $stmt = $this->db->prepare($sql);
112       $stmt->bindParam(1, $id, PDO::PARAM_STR); // setString(1, $id);
113       $stmt->execute();
114     }
115     catch (PDOException $e)
116     {
117       $error = sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage());
118
119       throw new sfDatabaseException($error);
120     }
121   }
122
123   /**
124    * Cleans up old sessions.
125    *
126    * @param int The lifetime of a session
127    *
128    * @return boolean true, if old sessions have been cleaned, otherwise an exception is thrown
129    *
130    * @throws <b>DatabaseException</b> If any old sessions cannot be cleaned
131    */
132   public function sessionGC($lifetime)
133   {
134     // determine deletable session time
135     $time = time() - $lifetime;
136
137     // get table/column
138     $db_table    = $this->getParameterHolder()->get('db_table');
139     $db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time');
140
141     // delete the record associated with this id
142     $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.$time;
143
144     try
145     {
146       $this->db->query($sql);
147       return true;
148     }
149     catch (PDOException $e)
150     {
151       $error = sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage());
152
153       throw new sfDatabaseException($error);
154     }
155   }
156
157   /**
158    * Opens a session.
159    *
160    * @param string
161    * @param string
162    *
163    * @return boolean true, if the session was opened, otherwise an exception is thrown
164    *
165    * @throws <b>DatabaseException</b> If a connection with the database does not exist or cannot be created
166    */
167   public function sessionOpen($path, $name)
168   {
169     // what database are we using?
170     $database = $this->getParameterHolder()->get('database', 'default');
171
172     $this->db = $this->getContext()->getDatabaseConnection($database);
173     if ($this->db == null || !$this->db instanceof PDO)
174     {
175       $error = 'PDO dabatase connection doesn\'t exist. Unable to open session.';
176
177       throw new sfDatabaseException($error);
178     }
179
180     return true;
181   }
182
183   /**
184    * Reads a session.
185    *
186    * @param string A session ID
187    *
188    * @return boolean true, if the session was read, otherwise an exception is thrown
189    *
190    * @throws <b>DatabaseException</b> If the session cannot be read
191    */
192   public function sessionRead($id)
193   {
194     // get table/columns
195     $db_table    = $this->getParameterHolder()->get('db_table');
196     $db_data_col = $this->getParameterHolder()->get('db_data_col', 'sess_data');
197     $db_id_col   = $this->getParameterHolder()->get('db_id_col', 'sess_id');
198     $db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time');
199
200     try
201     {
202       $sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.'=?';
203
204       $stmt = $this->db->prepare($sql);
205       $stmt->bindParam(1, $id, PDO::PARAM_STR, 255);
206
207       $stmt->execute();
208       if ($data = $stmt->fetchColumn())
209       {
210         return $data;
211       }
212       else
213       {
214         // session does not exist, create it
215         $sql = 'INSERT INTO '.$db_table.'('.$db_id_col.', '.$db_data_col.', '.$db_time_col.') VALUES (?, ?, ?)';
216
217         $stmt = $this->db->prepare($sql);
218         $stmt->bindParam(1, $id, PDO::PARAM_STR); // setString(1, $id);
219         $stmt->bindValue(2, '', PDO::PARAM_STR); // setString(2, '');
220         $stmt->bindValue(3, time(), PDO::PARAM_INT); // setInt(3, time());
221         $stmt->execute();
222
223         return '';
224       }
225     }
226     catch (PDOException $e)
227     {
228       $error = sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage());
229
230       throw new sfDatabaseException($error);
231     }
232   }
233
234   /**
235    * Writes session data.
236    *
237    * @param string A session ID
238    * @param string A serialized chunk of session data
239    *
240    * @return boolean true, if the session was written, otherwise an exception is thrown
241    *
242    * @throws <b>DatabaseException</b> If the session data cannot be written
243    */
244   public function sessionWrite($id, $data)
245   {
246     // get table/column
247     $db_table    = $this->getParameterHolder()->get('db_table');
248     $db_data_col = $this->getParameterHolder()->get('db_data_col', 'sess_data');
249     $db_id_col   = $this->getParameterHolder()->get('db_id_col', 'sess_id');
250     $db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time');
251
252     $sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = ?, '.$db_time_col.' = '.time().' WHERE '.$db_id_col.'= ?';
253
254     try
255     {
256       $stmt = $this->db->prepare($sql);
257       $stmt->bindParam(1, $data, PDO::PARAM_STR); // setString(1, $data);
258       $stmt->bindParam(2, $id, PDO::PARAM_STR); // setString(2, $id);
259       $stmt->execute();
260       return true;
261     }
262
263     catch (PDOException $e)
264     {
265       $error = sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage());
266
267       throw new sfDatabaseException($error);
268     }
269
270     return false;
271   }
272
273   /**
274    * Executes the shutdown procedure.
275    *
276    */
277   public function shutdown()
278   {
279   }
280 }
281
Note: See TracBrowser for help on using the browser.