[ Index ] |
|
Code source de Symfony 1.0.0 |
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. 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>Required parameters:</b> 19 * 20 * # <b>db_table</b> - [none] - The database table in which session data will be 21 * stored. 22 * 23 * <b>Optional parameters:</b> 24 * 25 * # <b>database</b> - [default] - The database connection to use 26 * (see databases.ini). 27 * # <b>db_id_col</b> - [sess_id] - The database column in which the 28 * session id will be stored. 29 * # <b>db_data_col</b> - [sess_data] - The database column in which the 30 * session data will be stored. 31 * # <b>db_time_col</b> - [sess_time] - The database column in which the 32 * session timestamp will be stored. 33 * # <b>session_name</b> - [Agavi] - The name of the session. 34 * 35 * @package symfony 36 * @subpackage storage 37 * @author Fabien Potencier <fabien.potencier@symfony-project.com> 38 * @author Sean Kerr <skerr@mojavi.org> 39 * @author Veikko Mäkinen <mail@veikkomakinen.com> 40 * @version SVN: $Id: sfCreoleSessionStorage.class.php 2995 2006-12-09 18:01:32Z fabien $ 41 */ 42 class sfCreoleSessionStorage extends sfSessionStorage 43 { 44 /** 45 * Creole Database Connection 46 * @var Connection 47 */ 48 protected $db; 49 50 /** 51 * Initialize this Storage. 52 * 53 * @param Context A Context instance. 54 * @param array An associative array of initialization parameters. 55 * 56 * @return bool true, if initialization completes successfully, otherwise 57 * false. 58 * 59 * @throws <b>InitializationException</b> If an error occurs while 60 * initializing this Storage. 61 */ 62 public function initialize($context, $parameters = null) 63 { 64 // disable auto_start 65 $parameters['auto_start'] = false; 66 67 // initialize the parent 68 parent::initialize($context, $parameters); 69 70 if (!$this->getParameterHolder()->has('db_table')) 71 { 72 // missing required 'db_table' parameter 73 $error = 'Factory configuration file is missing required "db_table" parameter for the Storage category'; 74 75 throw new sfInitializationException($error); 76 } 77 78 // use this object as the session handler 79 session_set_save_handler(array($this, 'sessionOpen'), 80 array($this, 'sessionClose'), 81 array($this, 'sessionRead'), 82 array($this, 'sessionWrite'), 83 array($this, 'sessionDestroy'), 84 array($this, 'sessionGC')); 85 86 // start our session 87 session_start(); 88 } 89 90 /** 91 * Close a session. 92 * 93 * @return bool true, if the session was closed, otherwise false. 94 */ 95 public function sessionClose() 96 { 97 // do nothing 98 return true; 99 } 100 101 /** 102 * Destroy a session. 103 * 104 * @param string A session ID. 105 * 106 * @return bool true, if the session was destroyed, otherwise an exception 107 * is thrown. 108 * 109 * @throws <b>DatabaseException</b> If the session cannot be destroyed. 110 */ 111 public function sessionDestroy($id) 112 { 113 // get table/column 114 $db_table = $this->getParameterHolder()->get('db_table'); 115 $db_id_col = $this->getParameterHolder()->get('db_id_col', 'sess_id'); 116 117 // delete the record associated with this id 118 $sql = 'DELETE FROM ' . $db_table . ' WHERE ' . $db_id_col . '=?'; 119 120 try 121 { 122 $stmt = $this->db->prepareStatement($sql); 123 $stmt->setString(1, $id); 124 $stmt->executeUpdate(); 125 } 126 catch (SQLException $e) { 127 $error = 'Creole SQLException was thrown when trying to manipulate session data. '; 128 $error .= 'Message: ' . $e->getMessage(); 129 throw new sfDatabaseException($error); 130 } 131 } 132 133 /** 134 * Cleanup old sessions. 135 * 136 * @param int The lifetime of a session. 137 * 138 * @return bool true, if old sessions have been cleaned, otherwise an 139 * exception is thrown. 140 * 141 * @throws <b>DatabaseException</b> If any old sessions cannot be cleaned. 142 */ 143 public function sessionGC($lifetime) 144 { 145 // determine deletable session time 146 $time = time() - $lifetime; 147 148 // get table/column 149 $db_table = $this->getParameterHolder()->get('db_table'); 150 $db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time'); 151 152 // delete the record associated with this id 153 $sql = 'DELETE FROM ' . $db_table . ' ' . 154 'WHERE ' . $db_time_col . ' < ' . $time; 155 156 try 157 { 158 $this->db->executeQuery($sql); 159 return true; 160 } 161 catch (SQLException $e) 162 { 163 $error = 'Creole SQLException was thrown when trying to manipulate session data. '; 164 $error .= 'Message: ' . $e->getMessage(); 165 throw new sfDatabaseException($error); 166 } 167 } 168 169 /** 170 * Open a session. 171 * 172 * @param string 173 * @param string 174 * 175 * @return bool true, if the session was opened, otherwise an exception is 176 * thrown. 177 * 178 * @throws <b>DatabaseException</b> If a connection with the database does 179 * not exist or cannot be created. 180 */ 181 public function sessionOpen($path, $name) 182 { 183 // what database are we using? 184 $database = $this->getParameterHolder()->get('database', 'default'); 185 186 // autoload propel propely if we're reusing the propel connection for session storage 187 if ($this->getContext()->getDatabaseManager()->getDatabase($database) instanceof sfPropelDatabase && !Propel::isInit()) 188 { 189 $error = 'Creole dabatase connection is the same as the propel database connection, but could not be initialized.'; 190 throw new sfDatabaseException($error); 191 } 192 193 $this->db = $this->getContext()->getDatabaseConnection($database); 194 if ($this->db == null || !$this->db instanceof Connection) 195 { 196 $error = 'Creole dabatase connection doesn\'t exist. Unable to open session.'; 197 throw new sfDatabaseException($error); 198 } 199 200 return true; 201 } 202 203 /** 204 * Read a session. 205 * 206 * @param string A session ID. 207 * 208 * @return bool true, if the session was read, otherwise an exception is 209 * thrown. 210 * 211 * @throws <b>DatabaseException</b> If the session cannot be read. 212 */ 213 public function sessionRead($id) 214 { 215 // get table/columns 216 $db_table = $this->getParameterHolder()->get('db_table'); 217 $db_data_col = $this->getParameterHolder()->get('db_data_col', 'sess_data'); 218 $db_id_col = $this->getParameterHolder()->get('db_id_col', 'sess_id'); 219 $db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time'); 220 221 try 222 { 223 $sql = 'SELECT ' . $db_data_col . ' FROM ' . $db_table . ' WHERE ' . $db_id_col . '=?'; 224 225 $stmt = $this->db->prepareStatement($sql); 226 $stmt->setString(1, $id); 227 228 $dbRes = $stmt->executeQuery(ResultSet::FETCHMODE_NUM); 229 230 if ($dbRes->next()) 231 { 232 $data = $dbRes->getString(1); 233 return $data; 234 } 235 else 236 { 237 // session does not exist, create it 238 $sql = 'INSERT INTO ' . $db_table . '('.$db_id_col.','.$db_data_col.','.$db_time_col; 239 $sql .= ') VALUES (?,?,?)'; 240 241 $stmt = $this->db->prepareStatement($sql); 242 $stmt->setString(1, $id); 243 $stmt->setString(2, ''); 244 $stmt->setInt(3, time()); 245 $stmt->executeUpdate(); 246 return ''; 247 } 248 } 249 catch (SQLException $e) 250 { 251 $error = 'Creole SQLException was thrown when trying to manipulate session data. '; 252 $error .= 'Message: ' . $e->getMessage(); 253 throw new sfDatabaseException($error); 254 } 255 } 256 257 /** 258 * Write session data. 259 * 260 * @param string A session ID. 261 * @param string A serialized chunk of session data. 262 * 263 * @return bool true, if the session was written, otherwise an exception is 264 * thrown. 265 * 266 * @throws <b>DatabaseException</b> If the session data cannot be written. 267 */ 268 public function sessionWrite($id, $data) 269 { 270 // get table/column 271 $db_table = $this->getParameterHolder()->get('db_table'); 272 $db_data_col = $this->getParameterHolder()->get('db_data_col', 'sess_data'); 273 $db_id_col = $this->getParameterHolder()->get('db_id_col', 'sess_id'); 274 $db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time'); 275 276 $sql = 'UPDATE ' . $db_table . ' SET ' . $db_data_col . '=?, ' . $db_time_col . ' = ' . time() . 277 ' WHERE ' . $db_id_col . '=?'; 278 279 try 280 { 281 $stmt = $this->db->prepareStatement($sql); 282 $stmt->setString(1, $data); 283 $stmt->setString(2, $id); 284 $stmt->executeUpdate(); 285 return true; 286 } 287 288 catch (SQLException $e) 289 { 290 $error = 'Creole SQLException was thrown when trying to manipulate session data. '; 291 $error .= 'Message: ' . $e->getMessage(); 292 throw new sfDatabaseException($error); 293 } 294 295 return false; 296 } 297 298 /** 299 * Execute the shutdown procedure. 300 * 301 * @return void 302 */ 303 public function shutdown() 304 { 305 } 306 307 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |