| [ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * SessionHandler implementation for PHP's PEAR database abstraction layer. 4 * 5 * Required parameters:<pre> 6 * 'phptype' The database type (e.g. 'pgsql', 'mysql', etc.).</pre> 7 * 8 * Required by some database implementations:<pre> 9 * 'hostspec' The hostname of the database server. 10 * 'protocol' The communication protocol ('tcp', 'unix', etc.). 11 * 'database' The name of the database. 12 * 'username' The username with which to connect to the database. 13 * 'password' The password associated with 'username'. 14 * 'options' Additional options to pass to the database. 15 * 'tty' The TTY on which to connect to the database. 16 * 'port' The port on which to connect to the database.</pre> 17 * 18 * Optional parameters:<pre> 19 * 'persistent' Use persistent DB connections? (boolean) 20 * 'table' The name of the sessiondata table in 'database'. Default 21 * is 'horde_sessionhandler'.</pre> 22 * 23 * The table structure for the SessionHandler can be found in 24 * horde/scripts/sql/horde_sessionhandler.sql. 25 * 26 * $Horde: framework/SessionHandler/SessionHandler/sql.php,v 1.22.10.13 2006/01/01 21:28:34 jan Exp $ 27 * 28 * Copyright 2002-2006 Mike Cochrane <mike@graftonhall.co.nz> 29 * 30 * See the enclosed file COPYING for license information (LGPL). If you 31 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 32 * 33 * @author Mike Cochrane <mike@graftonhall.co.nz> 34 * @since Horde 3.0 35 * @package Horde_SessionHandler 36 */ 37 class SessionHandler_sql extends SessionHandler { 38 39 /** 40 * Handle for the current database connection. 41 * 42 * @var DB 43 */ 44 var $_db; 45 46 /** 47 * Boolean indicating whether or not we're connected to the SQL server. 48 * 49 * @var boolean 50 */ 51 var $_connected = false; 52 53 /** 54 * Close the SessionHandler backend. 55 * 56 * @return boolean True on success, false otherwise. 57 */ 58 function close() 59 { 60 /* Disconnect from database. */ 61 if ($this->_connected) { 62 /* Close any open transactions. */ 63 $this->_db->commit(); 64 $this->_db->autoCommit(true); 65 66 $this->_connected = false; 67 return $this->_db->disconnect(); 68 } 69 70 return true; 71 } 72 73 /** 74 * Read the data for a particular session identifier from the 75 * SessionHandler backend. 76 * 77 * @param string $id The session identifier. 78 * 79 * @return string The session data. 80 */ 81 function read($id) 82 { 83 /* Make sure we have a valid database connection. */ 84 $this->_connect(); 85 86 /* Begin a transaction. */ 87 $result = $this->_db->autocommit(false); 88 if (is_a($result, 'PEAR_Error')) { 89 Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); 90 return ''; 91 } 92 93 /* Execute the query. */ 94 require_once 'Horde/SQL.php'; 95 $result = Horde_SQL::readBlob($this->_db, $this->_params['table'], 'session_data', 96 array('session_id' => $id)); 97 98 if (is_a($result, 'PEAR_Error')) { 99 Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); 100 return ''; 101 } 102 103 return $result; 104 } 105 106 /** 107 * Write session data to the SessionHandler backend. 108 * 109 * @param string $id The session identifier. 110 * @param string $session_data The session data. 111 * 112 * @return boolean True on success, false otherwise. 113 */ 114 function write($id, $session_data) 115 { 116 /* Make sure we have a valid database connection. */ 117 $this->_connect(); 118 119 /* Build the SQL query. */ 120 $query = sprintf('SELECT session_id FROM %s WHERE session_id = ?', 121 $this->_params['table']); 122 $values = array($id); 123 124 /* Log the query at a DEBUG log level. */ 125 Horde::logMessage(sprintf('SQL Query by SessionHandler_sql::write(): query = "%s"', $query), 126 __FILE__, __LINE__, PEAR_LOG_DEBUG); 127 128 /* Execute the query. */ 129 $result = $this->_db->getOne($query, $values); 130 if (is_a($result, 'PEAR_Error')) { 131 Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); 132 return false; 133 } 134 135 require_once 'Horde/SQL.php'; 136 if ($result) { 137 $result = Horde_SQL::updateBlob($this->_db, $this->_params['table'], 'session_data', 138 $session_data, array('session_id' => $id), 139 array('session_lastmodified' => time())); 140 } else { 141 $result = Horde_SQL::insertBlob($this->_db, $this->_params['table'], 'session_data', 142 $session_data, array('session_id' => $id, 143 'session_lastmodified' => time())); 144 } 145 146 if (is_a($result, 'PEAR_Error')) { 147 $this->_db->rollback(); 148 $this->_db->autoCommit(true); 149 Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); 150 return false; 151 } 152 153 $result = $this->_db->commit(); 154 if (is_a($result, 'PEAR_Error')) { 155 $this->_db->autoCommit(true); 156 Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); 157 return false; 158 } 159 160 $this->_db->autoCommit(true); 161 return true; 162 } 163 164 /** 165 * Destroy the data for a particular session identifier in the 166 * SessionHandler backend. 167 * 168 * @param string $id The session identifier. 169 * 170 * @return boolean True on success, false otherwise. 171 */ 172 function destroy($id) 173 { 174 /* Make sure we have a valid database connection. */ 175 $this->_connect(); 176 177 /* Build the SQL query. */ 178 $query = sprintf('DELETE FROM %s WHERE session_id = ?', 179 $this->_params['table']); 180 $values = array($id); 181 182 /* Log the query at a DEBUG log level. */ 183 Horde::logMessage(sprintf('SQL Query by SessionHandler_sql::destroy(): query = "%s"', $query), 184 __FILE__, __LINE__, PEAR_LOG_DEBUG); 185 186 /* Execute the query. */ 187 $result = $this->_db->query($query, $values); 188 if (is_a($result, 'PEAR_Error')) { 189 Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); 190 return false; 191 } 192 193 $result = $this->_db->commit(); 194 if (is_a($result, 'PEAR_Error')) { 195 Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); 196 return false; 197 } 198 199 return true; 200 } 201 202 /** 203 * Garbage collect stale sessions from the SessionHandler backend. 204 * 205 * @param integer $maxlifetime The maximum age of a session. 206 * 207 * @return boolean True on success, false otherwise. 208 */ 209 function gc($maxlifetime = 300) 210 { 211 /* Make sure we have a valid database connection. */ 212 $this->_connect(); 213 214 /* Build the SQL query. */ 215 $query = sprintf('DELETE FROM %s WHERE session_lastmodified < ?', 216 $this->_params['table']); 217 $values = array(time() - $maxlifetime); 218 219 /* Log the query at a DEBUG log level. */ 220 Horde::logMessage(sprintf('SQL Query by SessionHandler_sql::gc(): query = "%s"', $query), 221 __FILE__, __LINE__, PEAR_LOG_DEBUG); 222 223 /* Execute the query. */ 224 $result = $this->_db->query($query, $values); 225 if (is_a($result, 'PEAR_Error')) { 226 Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); 227 return false; 228 } 229 230 return true; 231 } 232 233 /** 234 * Get a list of the valid session identifiers. 235 * 236 * @return array A list of valid session identifiers. 237 */ 238 function getSessionIDs() 239 { 240 /* Make sure we have a valid database connection. */ 241 $this->_connect(); 242 243 $timeout = time() - ini_get('session.gc_maxlifetime'); 244 245 /* Build the SQL query. */ 246 $query = 'SELECT session_id FROM ' . $this->_params['table'] . 247 ' WHERE session_lastmodified > ?'; 248 $values = array($timeout); 249 250 /* Log the query at a DEBUG log level. */ 251 Horde::logMessage(sprintf('SQL Query by SessionHandler_sql::getSessionIDs(): query = "%s"', $query), 252 __FILE__, __LINE__, PEAR_LOG_DEBUG); 253 254 /* Execute the query. */ 255 $result = $this->_db->getCol($query, 0, $values); 256 if (is_a($result, 'PEAR_Error')) { 257 Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); 258 return false; 259 } 260 261 return $result; 262 } 263 264 /** 265 * Attempts to open a connection to the SQL server. 266 * 267 * @return boolean True on success; exits (Horde::fatal()) on error. 268 */ 269 function _connect() 270 { 271 if (!$this->_connected) { 272 Horde::assertDriverConfig($this->_params, 'sessionhandler', 273 array('phptype'), 274 'session handler SQL'); 275 276 if (!isset($this->_params['database'])) { 277 $this->_params['database'] = ''; 278 } 279 if (!isset($this->_params['username'])) { 280 $this->_params['username'] = ''; 281 } 282 if (!isset($this->_params['hostspec'])) { 283 $this->_params['hostspec'] = ''; 284 } 285 if (empty($this->_params['table'])) { 286 $this->_params['table'] = 'horde_sessionhandler'; 287 } 288 289 /* Connect to the SQL server using the supplied 290 * parameters. */ 291 include_once 'DB.php'; 292 $this->_db = &DB::connect($this->_params, 293 array('persistent' => !empty($this->_params['persistent']))); 294 if (is_a($this->_db, 'PEAR_Error')) { 295 Horde::fatal($this->_db, __FILE__, __LINE__); 296 } 297 298 $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS); 299 300 $this->_connected = true; 301 } 302 303 return true; 304 } 305 306 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |