Development

/branches/1.2/lib/storage/sfDatabaseSessionStorage.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/storage/sfDatabaseSessionStorage.class.php

Revision 20300, 5.5 kB (checked in by fabien, 5 years ago)

[1.2, 1.3] fixed sfDatabaseSessionStorage ignores session storage configuration in factories.yml (closes #6861)

  • 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  * Base class for all sfStorage that uses a sfDatabase object as a storage.
14  *
15  * @package    symfony
16  * @subpackage storage
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @author     Sean Kerr <sean@code-box.org>
19  * @version    SVN: $Id$
20  */
21 abstract class sfDatabaseSessionStorage extends sfSessionStorage
22 {
23   protected
24     $db = null,
25     $con = null;
26
27   /**
28    * Available options:
29    *
30    *   * db_table:    The database table in which session data will be stored
31    *   * database:    The sfDatabase object to use
32    *   * db_id_col:   The database column in which the session id will be stored (sess_id by default)
33    *   * db_data_col: The database column in which the session data will be stored (sess_data by default)
34    *   * db_time_col: The database column in which the session timestamp will be stored (sess_time by default)
35    *
36    * @param  array $options  An associative array of options
37    *
38    * @see sfSessionStorage
39    */
40   public function initialize($options = array())
41   {
42     $options = array_merge(array(
43       'db_id_col'   => 'sess_id',
44       'db_data_col' => 'sess_data',
45       'db_time_col' => 'sess_time',
46     ), $options);
47
48     // disable auto_start
49     $options['auto_start'] = false;
50
51     // initialize the parent
52     parent::initialize($options);
53
54     if (!isset($this->options['db_table']))
55     {
56       throw new sfInitializationException('You must provide a "db_table" option to sfDatabaseSessionStorage.');
57     }
58
59     if (!isset($this->options['database']))
60     {
61       throw new sfInitializationException('You must provide a "database" option to sfDatabaseSessionStorage.');
62     }
63
64     // use this object as the session handler
65     session_set_save_handler(array($this, 'sessionOpen'),
66                              array($this, 'sessionClose'),
67                              array($this, 'sessionRead'),
68                              array($this, 'sessionWrite'),
69                              array($this, 'sessionDestroy'),
70                              array($this, 'sessionGC'));
71
72     // start our session
73     session_start();
74   }
75
76   /**
77    * Closes a session.
78    *
79    * @return boolean true, if the session was closed, otherwise false
80    */
81   public function sessionClose()
82   {
83     // do nothing
84     return true;
85   }
86
87   /**
88    * Opens a session.
89    *
90    * @param  string $path  (ignored)
91    * @param  string $name  (ignored)
92    *
93    * @return boolean true, if the session was opened, otherwise an exception is thrown
94    *
95    * @throws <b>DatabaseException</b> If a connection with the database does not exist or cannot be created
96    */
97   public function sessionOpen($path = null, $name = null)
98   {
99     // what database are we using?
100     $database = $this->options['database'];
101
102     // get the database and connection
103     $databaseClass = get_class($database);
104     if($databaseClass == 'sfPropelDatabase')
105     {
106       $this->db = Propel::getConnection($database->getParameter('name'));
107     }
108     elseif($databaseClass == 'sfDoctrineDatabase')
109     {
110       $this->db = $database->getConnection();
111     }
112     else
113     {
114       $this->db = $database->getResource();
115     }
116     $this->con = $database->getConnection();
117
118     if (is_null($this->db) && is_null($this->con))
119     {
120       throw new sfDatabaseException('Database connection does not exist. Unable to open session.');
121     }
122
123     return true;
124   }
125
126   /**
127    * Destroys a session.
128    *
129    * @param  string $id  A session ID
130    *
131    * @return bool true, if the session was destroyed, otherwise an exception is thrown
132    *
133    * @throws <b>DatabaseException</b> If the session cannot be destroyed
134    */
135   abstract public function sessionDestroy($id);
136
137   /**
138    * Cleans up old sessions.
139    *
140    * @param  int $lifetime  The lifetime of a session
141    *
142    * @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
143    *
144    * @throws <b>DatabaseException</b> If any old sessions cannot be cleaned
145    */
146   abstract public function sessionGC($lifetime);
147
148   /**
149    * Reads a session.
150    *
151    * @param  string $id  A session ID
152    *
153    * @return bool true, if the session was read, otherwise an exception is thrown
154    *
155    * @throws <b>DatabaseException</b> If the session cannot be read
156    */
157   abstract public function sessionRead($id);
158
159   /**
160    * Writes session data.
161    *
162    * @param  string $id    A session ID
163    * @param  string $data  A serialized chunk of session data
164    *
165    * @return bool true, if the session was written, otherwise an exception is thrown
166    *
167    * @throws <b>DatabaseException</b> If the session data cannot be written
168    */
169   abstract public function sessionWrite($id, $data);
170
171   /**
172    * Regenerates id that represents this storage.
173    *
174    * @param  boolean $destroy Destroy session when regenerating?
175    *
176    * @return boolean True if session regenerated, false if error
177    *
178    */
179   public function regenerate($destroy = false)
180   {
181     if (self::$sessionIdRegenerated)
182     {
183       return;
184     }
185
186     $currentId = session_id();
187
188     parent::regenerate($destroy);
189
190     $newId = session_id();
191     $this->sessionRead($newId);
192
193     return $this->sessionWrite($newId, $this->sessionRead($currentId));
194   }
195
196   /**
197    * Executes the shutdown procedure.
198    *
199    */
200   public function shutdown()
201   {
202     parent::shutdown();
203   }
204 }
205
Note: See TracBrowser for help on using the browser.