Development

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

You must first sign up to be able to contribute.

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

Revision 7791, 8.1 kB (checked in by fabien, 7 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 Rev Date
Line 
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  * Provides support for session storage using a PostgreSQL brand database.
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>db_id_col</b>    - [sess_id]   - The database column in which the
22  *                                       session id will be stored.
23  * # <b>db_data_col</b>  - [sess_data] - The database column in which the
24  *                                       session data will be stored.
25  * # <b>db_time_col</b>  - [sess_time] - The database column in which the
26  *                                       session timestamp will be stored.
27  * # <b>session_name</b> - [symfony]   - The name of the session.
28  *
29  * @package    symfony
30  * @subpackage storage
31  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
32  * @author     Sean Kerr <sean@code-box.org>
33  * @version    SVN: $Id$
34  */
35 class sfPostgreSQLSessionStorage extends sfSessionStorage
36 {
37   protected
38     $resource = null;
39
40   /**
41    * Initializes this Storage instance.
42    *
43    * @param sfContext A sfContext instance
44    * @param array   An associative array of initialization parameters
45    *
46    * @return boolean true, if initialization completes successfully, otherwise false
47    *
48    * @throws <b>sfInitializationException</b> If an error occurs while initializing this Storage
49    */
50   public function initialize($context, $parameters = null)
51   {
52     // disable auto_start
53     $parameters['auto_start'] = false;
54
55     // initialize the parent
56     parent::initialize($context, $parameters);
57
58     if (!$this->getParameterHolder()->has('db_table'))
59     {
60       // missing required 'db_table' parameter
61       $error = 'Factory configuration file is missing required "db_table" parameter for the Storage category';
62
63       throw new sfInitializationException($error);
64     }
65
66     // use this object as the session handler
67     session_set_save_handler(array($this, 'sessionOpen'),
68                              array($this, 'sessionClose'),
69                              array($this, 'sessionRead'),
70                              array($this, 'sessionWrite'),
71                              array($this, 'sessionDestroy'),
72                              array($this, 'sessionGC'));
73
74     // start our session
75     session_start();
76   }
77
78   /**
79    * Closes a session.
80    *
81    * @return boolean true, if the session was closed, otherwise false
82    */
83   public function sessionClose()
84   {
85     // do nothing
86     return true;
87   }
88
89   /**
90    * Destroys a session.
91    *
92    * @param string A session ID
93    *
94    * @return boolean true, if the session was destroyed, otherwise an exception is thrown
95    *
96    * @throws <b>sfDatabaseException</b> If the session cannot be destroyed
97    */
98   public function sessionDestroy($id)
99   {
100     // get table/column
101     $db_table  = $this->getParameterHolder()->get('db_table');
102     $db_id_col = $this->getParameterHolder()->get('db_id_col', 'sess_id');
103
104     // cleanup the session id, just in case
105     $id = addslashes($id);
106
107     // delete the record associated with this id
108     $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.' = \''.$id.'\'';
109
110     if (@pg_query($this->resource, $sql))
111     {
112       return true;
113     }
114
115     // failed to destroy session
116     $error = 'PostgreSQLSessionStorage cannot destroy session id "%s"';
117     $error = sprintf($error, $id);
118
119     throw new sfDatabaseException($error);
120   }
121
122   /**
123    * Cleans up old sessions.
124    *
125    * @param int The lifetime of a session
126    *
127    * @return boolean true, if old sessions have been cleaned, otherwise an exception is thrown
128    *
129    * @throws <b>sfDatabaseException</b> If any old sessions cannot be cleaned
130    */
131   public function sessionGC($lifetime)
132   {
133     // determine deletable session time
134     $time = time() - $lifetime;
135
136     // get table/column
137     $db_table    = $this->getParameterHolder()->get('db_table');
138     $db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time');
139
140     // delete the record associated with this id
141     $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.$time;
142
143     if (@pg_query($this->resource, $sql))
144     {
145       return true;
146     }
147
148     // failed to cleanup old sessions
149     $error = 'PostgreSQLSessionStorage cannot delete old sessions';
150
151     throw new sfDatabaseException($error);
152   }
153
154   /**
155    * Opens a session.
156    *
157    * @param string
158    * @param string
159    *
160    * @return boolean true, if the session was opened, otherwise an exception is thrown
161    *
162    * @throws <b>sfDatabaseException</b> If a connection with the database does
163    *                                  not exist or cannot be created
164    */
165   public function sessionOpen($path, $name)
166   {
167     // what database are we using?
168     $database = $this->getParameterHolder()->get('database', 'default');
169
170     // get the database resource
171     $this->resource = $this->getContext()
172                            ->getDatabaseManager()
173                            ->getDatabase($database)
174                            ->getResource();
175
176     return true;
177   }
178
179   /**
180    * Reads a session.
181    *
182    * @param string A session ID
183    *
184    * @return boolean true, if the session was read, otherwise an exception is thrown
185    *
186    * @throws <b>sfDatabaseException</b> If the session cannot be read
187    */
188   public function sessionRead($id)
189   {
190     // get table/column
191     $db_table    = $this->getParameterHolder()->get('db_table');
192     $db_data_col = $this->getParameterHolder()->get('db_data_col', 'sess_data');
193     $db_id_col   = $this->getParameterHolder()->get('db_id_col', 'sess_id');
194     $db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time');
195
196     // cleanup the session id, just in case
197     $id = addslashes($id);
198
199     // delete the record associated with this id
200     $sql = 'SELECT '.$db_data_col.' ' .
201            'FROM '.$db_table.' ' .
202            'WHERE '.$db_id_col.' = \''.$id.'\'';
203
204     $result = @pg_query($this->resource, $sql);
205
206     if ($result != false && @pg_num_rows($result) == 1)
207     {
208       // found the session
209       $data = pg_fetch_row($result);
210
211       return $data[0];
212     }
213     else
214     {
215       // session does not exist, create it
216       $sql = 'INSERT INTO '.$db_table.' ('.$db_id_col.', ' .
217              $db_data_col.', '.$db_time_col.') VALUES (' .
218              '\''.$id.'\', \'\', '.time().')';
219
220       if (@pg_query($this->resource, $sql))
221       {
222         return '';
223       }
224
225       // can't create record
226       $error = 'PostgreSQLSessionStorage cannot create new record for id "%s"';
227       $error = sprintf($error, $id);
228
229       throw new sfDatabaseException($error);
230     }
231   }
232
233   /**
234    * Writes session data.
235    *
236    * @param string A session ID
237    * @param string A serialized chunk of session data
238    *
239    * @return boolean true, if the session was written, otherwise an exception is thrown
240    *
241    * @throws <b>sfDatabaseException</b> If the session data cannot be written
242    */
243   public function sessionWrite($id, &$data)
244   {
245     // get table/column
246     $db_table    = $this->getParameterHolder()->get('db_table');
247     $db_data_col = $this->getParameterHolder()->get('db_data_col', 'sess_data');
248     $db_id_col   = $this->getParameterHolder()->get('db_id_col', 'sess_id');
249     $db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time');
250
251     // cleanup the session id and data, just in case
252     $id   = addslashes($id);
253     $data = addslashes($data);
254
255     // delete the record associated with this id
256     $sql = 'UPDATE '.$db_table.' '.
257            'SET '.$db_data_col.' = \''.$data.'\', '.
258            $db_time_col.' = '.time().' '.
259            'WHERE '.$db_id_col.' = \''.$id.'\'';
260
261     if (@pg_query($this->resource, $sql))
262     {
263       return true;
264     }
265
266     // failed to write session data
267     $error = 'PostgreSQLSessionStorage cannot write session data for id "%s"';
268     $error = sprintf($error, $id);
269
270     throw new sfDatabaseException($error);
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.