Development

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

You must first sign up to be able to contribute.

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

Revision 23810, 4.6 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3] set svn:eol-style property to native and svn:keywords property to Id on all .php files

  • 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-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>parameters:</b> see sfDatabaseSessionStorage
16  *
17  * @package    symfony
18  * @subpackage storage
19  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
20  * @author     Sean Kerr <sean@code-box.org>
21  * @version    SVN: $Id$
22  */
23 class sfPostgreSQLSessionStorage extends sfDatabaseSessionStorage
24 {
25   /**
26    * Destroys a session.
27    *
28    * @param  string $id  A session ID
29    *
30    * @return bool true, if the session was destroyed, otherwise an exception is thrown
31    *
32    * @throws <b>sfDatabaseException</b> If the session cannot be destroyed
33    */
34   public function sessionDestroy($id)
35   {
36     // get table/column
37     $db_table  = $this->options['db_table'];
38     $db_id_col = $this->options['db_id_col'];
39
40     // cleanup the session id, just in case
41     $id = addslashes($id);
42
43     // delete the record associated with this id
44     $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.' = \''.$id.'\'';
45
46     if (@pg_query($this->db, $sql))
47     {
48       return true;
49     }
50
51     // failed to destroy session
52     throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot destroy session id "%s".', $id));
53   }
54
55   /**
56    * Cleans up old sessions.
57    *
58    * @param  int $lifetime  The lifetime of a session
59    *
60    * @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
61    *
62    * @throws <b>sfDatabaseException</b> If any old sessions cannot be cleaned
63    */
64   public function sessionGC($lifetime)
65   {
66     // get table/column
67     $db_table    = $this->options['db_table'];
68     $db_time_col = $this->options['db_time_col'];
69
70     // delete the record associated with this id
71     $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime);
72
73     if (!@pg_query($this->db, $sql))
74     {
75       throw new sfDatabaseException('sfPostgreSQLSessionStorage cannot delete old sessions.');
76     }
77
78     return true;
79   }
80
81   /**
82    * Reads a session.
83    *
84    * @param  string $id  A session ID
85    *
86    * @return string      The session data if the session was read or created, otherwise an exception is thrown
87    *
88    * @throws <b>sfDatabaseException</b> If the session cannot be read
89    */
90   public function sessionRead($id)
91   {
92     // get table/column
93     $db_table    = $this->options['db_table'];
94     $db_data_col = $this->options['db_data_col'];
95     $db_id_col   = $this->options['db_id_col'];
96     $db_time_col = $this->options['db_time_col'];
97
98     // cleanup the session id, just in case
99     $id = addslashes($id);
100
101     // delete the record associated with this id
102     $sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.' = \''.$id.'\'';
103
104     $result = @pg_query($this->db, $sql);
105
106     if ($result != false && @pg_num_rows($result) == 1)
107     {
108       // found the session
109       $data = pg_fetch_row($result);
110
111       return $data[0];
112     }
113     else
114     {
115       // session does not exist, create it
116       $sql = 'INSERT INTO '.$db_table.' ('.$db_id_col.', '.$db_data_col.', '.$db_time_col.') VALUES (\''.$id.'\', \'\', '.time().')';
117
118       if (@pg_query($this->db, $sql))
119       {
120         return '';
121       }
122
123       // can't create record
124       throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot create new record for id "%s".', $id));
125     }
126   }
127
128   /**
129    * Writes session data.
130    *
131    * @param  string $id    A session ID
132    * @param  string $data  A serialized chunk of session data
133    *
134    * @return bool true, if the session was written, otherwise an exception is thrown
135    *
136    * @throws <b>sfDatabaseException</b> If the session data cannot be written
137    */
138   public function sessionWrite($id, $data)
139   {
140     // get table/column
141     $db_table    = $this->options['db_table'];
142     $db_data_col = $this->options['db_data_col'];
143     $db_id_col   = $this->options['db_id_col'];
144     $db_time_col = $this->options['db_time_col'];
145
146     // cleanup the session id and data, just in case
147     $id   = addslashes($id);
148     $data = addslashes($data);
149
150     // delete the record associated with this id
151     $sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = \''.$data.'\', '.$db_time_col.' = '.time().' WHERE '.$db_id_col.' = \''.$id.'\'';
152
153     if (@pg_query($this->db, $sql))
154     {
155       return true;
156     }
157
158     // failed to write session data
159     throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot write session data for id "%s".', $id));
160   }
161 }
162
Note: See TracBrowser for help on using the browser.