Development

/branches/1.1/lib/plugins/sfPropelPlugin/lib/creole/storage/sfCreoleSessionStorage.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/plugins/sfPropelPlugin/lib/creole/storage/sfCreoleSessionStorage.class.php

Revision 9663, 5.0 kB (checked in by FabianLange, 7 years ago)

fixed what was initially fixed with r8506: sfCreoleSessionStorage.

  • 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, 2005 Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) 2004, 2005 Sean Kerr <sean@code-box.org>
7  *
8  * The original version the file is based on is licensed under the LGPL, but a special license was granted.
9  * Please see the licenses/LICENSE.Agavi file
10  *
11  * For the full copyright and license information, please view the LICENSE
12  * file that was distributed with this source code.
13  */
14
15 /**
16  * Provides support for session storage using a CreoleDb database abstraction layer.
17  *
18  * <b>parameters:</b> see sfDatabaseSessionStorage
19  *
20  * @package    symfony
21  * @subpackage storage
22  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
23  * @author     Sean Kerr <sean@code-box.org>
24  * @author     Veikko Mäkinen <mail@veikkomakinen.com>
25  * @version    SVN: $Id$
26  */
27 class sfCreoleSessionStorage extends sfDatabaseSessionStorage
28 {
29   /**
30    * Destroy a session.
31    *
32    * @param string $id A session ID.
33    *
34    * @return bool true, if the session was destroyed, otherwise an exception is thrown.
35    *
36    * @throws <b>DatabaseException</b> If the session cannot be destroyed.
37    */
38   public function sessionDestroy($id)
39   {
40     // get table/column
41     $db_table  = $this->options['db_table'];
42     $db_id_col = $this->options['db_id_col'];
43
44     // delete the record associated with this id
45     $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.'= ?';
46
47     try
48     {
49       $stmt = $this->con->prepareStatement($sql);
50       $stmt->setString(1, $id);
51       $stmt->executeUpdate();
52     }
53     catch (SQLException $e)
54     {
55       throw new sfDatabaseException(sprintf('Creole SQLException was thrown when trying to manipulate session data. Message: %s.', $e->getMessage()));
56     }
57
58     return true;
59   }
60
61   /**
62    * Cleanup old sessions.
63    *
64    * @param int $lifetime The lifetime of a session.
65    *
66    * @return bool true, if old sessions have been cleaned, otherwise an exception is thrown.
67    *
68    * @throws <b>DatabaseException</b> If any old sessions cannot be cleaned.
69    */
70   public function sessionGC($lifetime)
71   {
72     // get table/column
73     $db_table    = $this->options['db_table'];
74     $db_time_col = $this->options['db_time_col'];
75
76     // delete the record associated with this id
77     $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime);
78
79     try
80     {
81       $this->con->executeQuery($sql);
82     }
83     catch (SQLException $e)
84     {
85       throw new sfDatabaseException(sprintf('Creole SQLException was thrown when trying to manipulate session data. Message: %s.', $e->getMessage()));
86     }
87
88     return true;
89   }
90
91   /**
92    * Read a session.
93    *
94    * @param string $id A session ID.
95    *
96    * @return bool true, if the session was read, otherwise an exception is thrown.
97    *
98    * @throws <b>DatabaseException</b> If the session cannot be read.
99    */
100   public function sessionRead($id)
101   {
102     // get table/columns
103     $db_table    = $this->options['db_table'];
104     $db_data_col = $this->options['db_data_col'];
105     $db_id_col   = $this->options['db_id_col'];
106     $db_time_col = $this->options['db_time_col'];
107
108     try
109     {
110       $sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.'=?';
111
112       $stmt = $this->con->prepareStatement($sql);
113       $stmt->setString(1, $id);
114
115       $dbRes = $stmt->executeQuery(ResultSet::FETCHMODE_NUM);
116
117       if ($dbRes->next())
118       {
119         $data = $dbRes->getString(1);
120
121         return $data;
122       }
123       else
124       {
125         // session does not exist, create it
126         $sql = 'INSERT INTO '.$db_table.'('.$db_id_col.','.$db_data_col.','.$db_time_col.') VALUES (?,?,?)';
127
128         $stmt = $this->con->prepareStatement($sql);
129         $stmt->setString(1, $id);
130         $stmt->setString(2, '');
131         $stmt->setInt(3, time());
132         $stmt->executeUpdate();
133
134         return '';
135       }
136     }
137     catch (SQLException $e)
138     {
139       throw new sfDatabaseException(sprintf('Creole SQLException was thrown when trying to manipulate session data. Message: %s.', $e->getMessage()));
140     }
141   }
142
143   /**
144    * Write session data.
145    *
146    * @param string $id A session ID.
147    * @param string $data A serialized chunk of session data.
148    *
149    * @return bool true, if the session was written, otherwise an exception is
150    *              thrown.
151    *
152    * @throws <b>DatabaseException</b> If the session data cannot be written.
153    */
154   public function sessionWrite($id, $data)
155   {
156     // get table/column
157     $db_table    = $this->options['db_table'];
158     $db_data_col = $this->options['db_data_col'];
159     $db_id_col   = $this->options['db_id_col'];
160     $db_time_col = $this->options['db_time_col'];
161
162     $sql = 'UPDATE '.$db_table.' SET '.$db_data_col.'=?, '.$db_time_col.' = '.time().' WHERE '.$db_id_col.'=?';
163
164     try
165     {
166       $stmt = $this->con->prepareStatement($sql);
167       $stmt->setString(1, $data);
168       $stmt->setString(2, $id);
169       $stmt->executeUpdate();
170     }
171     catch (SQLException $e)
172     {
173       throw new sfDatabaseException(sprintf('Creole SQLException was thrown when trying to manipulate session data. Message: %s.', $e->getMessage()));
174     }
175
176     return true;
177   }
178 }
179
Note: See TracBrowser for help on using the browser.