[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * SessionHandler:: implementation for Oracle 8i (native). 4 * 5 * Required parameters:<pre> 6 * 'hostspec' The hostname of the database server. 7 * 'protocol' The communication protocol ('tcp', 'unix', etc.). 8 * 'username' The username with which to connect to the database. 9 * 'password' The password associated with 'username'. 10 * 'database' The name of the database. 11 * 'table' The name of the sessiondata table in 'database'.</pre> 12 * 13 * Required for some configurations:<pre> 14 * 'port' The port on which to connect to the database.</pre> 15 * 16 * Optional parameters:<pre> 17 * 'persistent' Use persistent DB connections? (boolean)</pre> 18 * 19 * The table structure for the SessionHandler can be found in 20 * horde/scripts/sql/horde_sessionhandler.oci8.sql. 21 * 22 * $Horde: framework/SessionHandler/SessionHandler/oci8.php,v 1.8.4.11 2006/02/10 18:46:53 jan Exp $ 23 * 24 * Copyright 2003-2006 Liam Hoekenga <liamr@umich.edu> 25 * 26 * See the enclosed file COPYING for license information (LGPL). If you 27 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 28 * 29 * @author Liam Hoekenga <liamr@umich.edu> 30 * @since Horde 2.2 31 * @package Horde_SessionHandler 32 */ 33 class SessionHandler_oci8 extends SessionHandler { 34 35 /** 36 * Handle for the current database connection. 37 * 38 * @var resource 39 */ 40 var $_db; 41 42 /** 43 * Are we connected to the SQL server. 44 * 45 * @var boolean 46 */ 47 var $_connected = false; 48 49 /** 50 * Close the SessionHandler backend. 51 * 52 * @return boolean True on success, false otherwise. 53 */ 54 function close() 55 { 56 if ($this->_connected) { 57 $this->_connected = false; 58 return OCILogOff($this->_db); 59 } 60 61 return true; 62 } 63 64 /** 65 * Read the data for a particular session identifier from the 66 * SessionHandler backend. 67 * 68 * @param string $id The session identifier. 69 * 70 * @return string The session data. 71 */ 72 function read($id) 73 { 74 /* Make sure we have a valid database connection. */ 75 $this->_connect(); 76 77 $select_query = sprintf('SELECT session_data FROM %s WHERE session_id = %s FOR UPDATE', 78 $this->_params['table'], $this->_quote($id)); 79 80 /* Log the query at a DEBUG log level. */ 81 Horde::logMessage(sprintf('SQL Query by SessionHandler_oci8::read(): query = "%s"', $select_query), 82 __FILE__, __LINE__, PEAR_LOG_DEBUG); 83 84 /* Execute query */ 85 $select_statement = OCIParse($this->_db, $select_query); 86 OCIExecute($select_statement); 87 if (!OCIFetchInto($select_statement, $result)) { 88 $insert_query = sprintf('INSERT INTO %s (session_id, session_lastmodified, session_data) VALUES (%s, %s, EMPTY_BLOB()) RETURNING session_data INTO :blob', 89 $this->_params['table'], 90 $this->_quote($id), 91 $this->_quote(time())); 92 $insert_statement = OCIParse($this->_db, $insert_query); 93 $lob = OCINewDescriptor($this->_db); 94 OCIBindByName($insert_statement, ':blob', $lob, -1, SQLT_BLOB); 95 OCIExecute($insert_statement, OCI_DEFAULT); 96 if ($session_data) { 97 $lob->save($session_data); 98 } 99 $result = OCICommit($this->_db); 100 OCIFreeStatement($insert_statement); 101 OCIExecute($select_statement); 102 OCIFetchInto($select_statement, $result); 103 } 104 $value = $result[0]->load(); 105 OCIFreeStatement($select_statement); 106 return($value); 107 } 108 109 /** 110 * Write session data to the SessionHandler backend. 111 * 112 * @param string $id The session identifier. 113 * @param string $session_data The session data. 114 * 115 * @return boolean True on success, false otherwise. 116 */ 117 function write($id, $session_data) 118 { 119 /* Make sure we have a valid database connection. */ 120 $this->_connect(); 121 122 /* Build the SQL query. */ 123 $query = sprintf('UPDATE %s SET session_lastmodified = %s, session_data = EMPTY_BLOB() WHERE session_id = %s RETURNING session_data INTO :blob', 124 $this->_params['table'], 125 $this->_quote(time()), 126 $this->_quote($id)); 127 128 /* Log the query at a DEBUG log level. */ 129 Horde::logMessage(sprintf('SQL Query by SessionHandler_oci8::write(): query = "%s"', $query), 130 __FILE__, __LINE__, PEAR_LOG_DEBUG); 131 132 /* Execute query */ 133 $statement = OCIParse($this->_db, $query); 134 $lob = OCINewDescriptor($this->_db); 135 OCIBindByName($statement, ':blob', $lob, -1, SQLT_BLOB); 136 OCIExecute($statement, OCI_DEFAULT); 137 if ($session_data) { 138 $lob->save($session_data); 139 } 140 $result = OCICommit($this->_db); 141 if (!$result) { 142 Horde::logMessage('Error writing session data', __FILE__, __LINE__, PEAR_LOG_ERR); 143 return false; 144 } 145 146 return true; 147 } 148 149 /** 150 * Destroy the data for a particular session identifier in the 151 * SessionHandler backend. 152 * 153 * @param string $id The session identifier. 154 * 155 * @return boolean True on success, false otherwise. 156 */ 157 function destroy($id) 158 { 159 /* Make sure we have a valid database connection. */ 160 $this->_connect(); 161 162 /* Build the SQL query. */ 163 $query = sprintf( 'DELETE FROM %s WHERE session_id = %s', 164 $this->_params['table'], $this->_quote($id)); 165 166 /* Log the query at a DEBUG log level. */ 167 Horde::logMessage(sprintf('SQL Query by SessionHandler_oci8::destroy(): query = "%s"', $query), 168 __FILE__, __LINE__, PEAR_LOG_DEBUG); 169 170 /* Execute the query. */ 171 $statement = OCIParse($this->_db, $query); 172 $result = OCIExecute($statement); 173 if (!$result) { 174 OCIFreeStatement($statement); 175 Horde::logMessage('Failed to delete session (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_ERR); 176 return false; 177 } 178 179 OCIFreeStatement($statement); 180 return true; 181 } 182 183 /** 184 * Garbage collect stale sessions from the SessionHandler backend. 185 * 186 * @param integer $maxlifetime The maximum age of a session. 187 * 188 * @return boolean True on success, false otherwise. 189 */ 190 function gc($maxlifetime = 1) 191 { 192 /* Make sure we have a valid database connection. */ 193 $this->_connect(); 194 195 /* Build the SQL query. */ 196 $query = sprintf('DELETE FROM %s WHERE session_lastmodified < %s', 197 $this->_params['table'], $this->_quote(time() - $maxlifetime)); 198 199 /* Log the query at a DEBUG log level. */ 200 Horde::logMessage(sprintf('SQL Query by SessionHandler_oci8::gc(): query = "%s"', $query), 201 __FILE__, __LINE__, PEAR_LOG_DEBUG); 202 203 /* Execute the query. */ 204 $statement = OCIParse($this->_db, $query); 205 $result = OCIExecute($statement); 206 if (!$result) { 207 OCIFreeStatement($statement); 208 Horde::logMessage('Error garbage collecting old sessions', __FILE__, __LINE__, PEAR_LOG_ERR); 209 return false; 210 } 211 212 OCIFreeStatement($statement); 213 return true; 214 } 215 216 /** 217 * Get a list of the valid session identifiers. 218 * 219 * @return array A list of valid session identifiers. 220 */ 221 function getSessionIDs() 222 { 223 /* Make sure we have a valid database connection. */ 224 $this->_connect(); 225 226 /* Session timeout, don't rely on garbage collection */ 227 $timeout = time() - ini_get('session.gc_maxlifetime'); 228 229 $query = sprintf('SELECT session_id FROM %s' . 230 ' WHERE session_lastmodified > %s', 231 $this->_params['table'], 232 $timeout); 233 234 /* Log the query at a DEBUG log level. */ 235 Horde::logMessage(sprintf('SQL Query by SessionHandler_oci8::getSessionIDs(): query = "%s"', $query), 236 __FILE__, __LINE__, PEAR_LOG_DEBUG); 237 238 /* Execute query */ 239 $statement = OCIParse($this->_db, $query); 240 OCIExecute($statement); 241 242 $sessions = array(); 243 while (OCIFetchInto($statement, $row)) { 244 $sessions[] = $row[0]; 245 } 246 247 OCIFreeStatement($statement); 248 return $sessions; 249 } 250 251 /** 252 * Escape a string for insertion. Stolen from PEAR::DB. 253 * @access private 254 * 255 * @param string $value The string to quote. 256 * 257 * @return string The quoted string. 258 */ 259 function _quote($value) 260 { 261 return ($value === null) ? 'NULL' : "'" . str_replace("'", "''", $value) . "'"; 262 } 263 264 /** 265 * Attempts to open a connection to the SQL server. 266 * 267 * @access private 268 */ 269 function _connect() 270 { 271 if ($this->_connected) { 272 return; 273 } 274 275 Horde::assertDriverConfig($this->_params, 'sessionhandler', 276 array('hostspec', 'username', 'password'), 277 'session handler Oracle'); 278 279 if (!isset($this->_params['table'])) { 280 $this->_params['table'] = 'horde_sessionhandler'; 281 } 282 283 if (function_exists('oci_connect')) { 284 if (empty($this->_params['persistent'])) { 285 $connect = 'oci_connect'; 286 } else { 287 $connect = 'oci_pconnect'; 288 } 289 } else { 290 if (empty($this->_params['persistent'])) { 291 $connect = 'OCILogon'; 292 } else { 293 $connect = 'OCIPLogon'; 294 } 295 } 296 297 if (!is_resource($this->_db = @$connect($this->_params['username'], 298 $this->_params['password'], 299 $this->_params['hostspec']))) { 300 Horde::fatal(PEAR::raiseError('Could not connect to database for SQL SessionHandler.'), __FILE__, __LINE__); 301 } 302 303 $this->_connected = 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 |