Development

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

You must first sign up to be able to contribute.

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

Revision 24590, 6.0 kB (checked in by FabianLange, 4 years ago)

[1.2, 1.3, 1.4] fixed obtaining error from mysqli session storage (fixes #7737)

  • 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 MySQL 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  * @author     Julien Garand <julien.garand@gmail.com>
22  * @version    SVN: $Id$
23  */
24 class sfMySQLSessionStorage extends sfDatabaseSessionStorage
25 {
26   /**
27    * Destroys a session.
28    *
29    * @param  string $id  A session ID
30    *
31    * @return bool true, if the session was destroyed, otherwise an exception is thrown
32    *
33    * @throws <b>sfDatabaseException</b> If the session cannot be destroyed.
34    */
35   public function sessionDestroy($id)
36   {
37     // get table/column
38     $db_table  = $this->options['db_table'];
39     $db_id_col = $this->options['db_id_col'];
40
41     // cleanup the session id, just in case
42     $id = $this->db_escape($id);
43
44     // delete the record associated with this id
45     $sql = "DELETE FROM $db_table WHERE $db_id_col = '$id'";
46
47     if ($this->db_query($sql))
48     {
49       return true;
50     }
51
52     // failed to destroy session
53     throw new sfDatabaseException(sprintf('%s cannot destroy session id "%s" (%s).', get_class($this), $id, $this->db_error()));
54   }
55
56   /**
57    * Cleans up old sessions.
58    *
59    * @param  int $lifetime  The lifetime of a session
60    *
61    * @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
62    *
63    * @throws <b>sfDatabaseException</b> If any old sessions cannot be cleaned
64    */
65   public function sessionGC($lifetime)
66   {
67     // get table/column
68     $db_table    = $this->options['db_table'];
69     $db_time_col = $this->options['db_time_col'];
70
71     // delete the record older than the authorised session life time
72     $lifetime = $this->db_escape($lifetime); // We never know...
73     $sql = "DELETE FROM $db_table WHERE $db_time_col + $lifetime < UNIX_TIMESTAMP()";
74
75     if (!$this->db_query($sql))
76     {
77       throw new sfDatabaseException(sprintf('%s cannot delete old sessions (%s).', get_class($this), $this->db_error()));
78     }
79
80     return true;
81   }
82
83   /**
84    * Reads a session.
85    *
86    * @param  string $id  A session ID
87    *
88    * @return string      The session data if the session was read or created, otherwise an exception is thrown
89    *
90    * @throws <b>sfDatabaseException</b> If the session cannot be read
91    */
92   public function sessionRead($id)
93   {
94     // get table/column
95     $db_table    = $this->options['db_table'];
96     $db_data_col = $this->options['db_data_col'];
97     $db_id_col   = $this->options['db_id_col'];
98     $db_time_col = $this->options['db_time_col'];
99
100     // cleanup the session id, just in case
101     $id = $this->db_escape($id);
102
103     // get the record associated with this id
104     $sql = "SELECT $db_data_col FROM $db_table WHERE $db_id_col = '$id'";
105
106     $result = $this->db_query($sql);
107
108     if ($result != false && $this->db_num_rows($result) == 1)
109     {
110       // found the session
111       $data = $this->db_fetch_row($result);
112
113       return $data[0];
114     }
115     else
116     {
117       // session does not exist, create it
118       $sql = "INSERT INTO $db_table ($db_id_col, $db_data_col, $db_time_col) VALUES ('$id', '', UNIX_TIMESTAMP())";
119       if ($this->db_query($sql))
120       {
121         return '';
122       }
123
124       // can't create record
125       throw new sfDatabaseException(sprintf('%s cannot create new record for id "%s" (%s).', get_class($this), $id, $this->db_error()));
126     }
127   }
128
129   /**
130    * Writes session data.
131    *
132    * @param  string $id    A session ID
133    * @param  string $data  A serialized chunk of session data
134    *
135    * @return bool true, if the session was written, otherwise an exception is thrown
136    *
137    * @throws <b>sfDatabaseException</b> If the session data cannot be written
138    */
139   public function sessionWrite($id, $data)
140   {
141     // get table/column
142     $db_table    = $this->options['db_table'];
143     $db_data_col = $this->options['db_data_col'];
144     $db_id_col   = $this->options['db_id_col'];
145     $db_time_col = $this->options['db_time_col'];
146
147     // cleanup the session id and data, just in case
148     $id   = $this->db_escape($id);
149     $data = $this->db_escape($data);
150
151     // update the record associated with this id
152     $sql = "UPDATE $db_table SET $db_data_col='$data', $db_time_col=UNIX_TIMESTAMP() WHERE $db_id_col='$id'";
153
154     if ($this->db_query($sql))
155     {
156       return true;
157     }
158
159     // failed to write session data
160     throw new sfDatabaseException(sprintf('%s cannot write session data for id "%s" (%s).', get_class($this), $id, $this->db_error()));
161   }
162
163   /**
164    * Executes an SQL Query
165    *
166    * @param  string $query  The query to execute
167    * @return mixed The result of the query
168    */
169   protected function db_query($query)
170   {
171     return @mysql_query($query, $this->db);
172   }
173
174   /**
175    * Escapes a string before using it in a query statement
176    *
177    * @param  string $string  The string to escape
178    * @return string The escaped string
179    */
180   protected function db_escape($string)
181   {
182     return mysql_real_escape_string($string, $this->db);
183   }
184
185   /**
186    * Counts the rows in a query result
187    *
188    * @param  resource $result  Result of a query
189    * @return int Number of rows
190    */
191   protected function db_num_rows($result)
192   {
193     return mysql_num_rows($result);
194   }
195
196   /**
197    * Extracts a row from a query result set
198    *
199    * @param  resource $result  Result of a query
200    * @return array Extracted row as an indexed array
201    */
202   protected function db_fetch_row($result)
203   {
204     return mysql_fetch_row($result);
205   }
206
207   /**
208    * Returns the text of the error message from previous database operation
209    *
210    * @return string The error text from the last database function
211    */
212   protected function db_error()
213   {
214     return mysql_error($this->db);
215   }
216 }
217
Note: See TracBrowser for help on using the browser.