[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * @package Horde_Prefs 4 */ 5 6 /** 7 * PEAR DB layer. 8 */ 9 require_once 'DB.php'; 10 11 /** 12 * Horde_String class. 13 */ 14 require_once 'Horde/String.php'; 15 16 /** 17 * Preferences storage implementation for PHP's PEAR database 18 * abstraction layer. 19 * 20 * Required parameters:<pre> 21 * 'phptype' The database type (ie. 'pgsql', 'mysql', etc.). 22 * 'charset' The database's internal charset.</pre> 23 * 24 * Optional parameters:<pre> 25 * 'table' The name of the preferences table in 'database'. 26 * DEFAULT: 'horde_prefs'</pre> 27 * 28 * Required by some database implementations:<pre> 29 * 'hostspec' The hostname of the database server. 30 * 'protocol' The communication protocol ('tcp', 'unix', etc.). 31 * 'database' The name of the database. 32 * 'username' The username with which to connect to the database. 33 * 'password' The password associated with 'username'. 34 * 'options' Additional options to pass to the database. 35 * 'port' The port on which to connect to the database. 36 * 'tty' The TTY on which to connect to the database.</pre> 37 * 38 * The table structure for the Prefs system is in 39 * scripts/sql/horde_prefs.sql. 40 * 41 * $Horde: framework/Prefs/Prefs/sql.php,v 1.91.10.20 2006/04/10 15:58:14 chuck Exp $ 42 * 43 * Copyright 1999-2006 Jon Parise <jon@horde.org> 44 * 45 * See the enclosed file COPYING for license information (LGPL). If you 46 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 47 * 48 * @author Jon Parise <jon@horde.org> 49 * @since Horde 1.3 50 * @package Horde_Prefs 51 */ 52 class Prefs_sql extends Prefs { 53 54 /** 55 * Hash containing connection parameters. 56 * 57 * @var array 58 */ 59 var $_params = array(); 60 61 /** 62 * Handle for the current database connection. 63 * 64 * @var DB 65 */ 66 var $_db; 67 68 /** 69 * Boolean indicating whether or not we're connected to the SQL server. 70 * 71 * @var boolean 72 */ 73 var $_connected = false; 74 75 /** 76 * Constructs a new SQL preferences object. 77 * 78 * @param string $user The user who owns these preferences. 79 * @param string $password The password associated with $user. (Unused) 80 * @param string $scope The current preferences scope. 81 * @param array $params A hash containing connection parameters. 82 * @param boolean $caching Should caching be used? 83 */ 84 function Prefs_sql($user, $password = '', $scope = '', 85 $params = array(), $caching = false) 86 { 87 $this->_user = $user; 88 $this->_scope = $scope; 89 $this->_params = $params; 90 $this->_caching = $caching; 91 92 parent::Prefs(); 93 } 94 95 /** 96 * Returns the charset used by the concrete preference backend. 97 * 98 * @return string The preference backend's charset. 99 */ 100 function getCharset() 101 { 102 return $this->_params['charset']; 103 } 104 105 /** 106 * Retrieves the requested set of preferences from the user's database 107 * entry. 108 * 109 * @param array $prefs An array listing the preferences to retrieve. If 110 * not specified, retrieve all of the preferences 111 * listed in the $prefs hash. 112 * 113 * @return mixed True on success or a PEAR_Error object on failure. 114 */ 115 function retrieve($prefs = array()) 116 { 117 /* Attempt to pull the values from the session cache first. */ 118 if ($this->cacheLookup()) { 119 return true; 120 } 121 122 /* Load defaults to make sure we have all preferences. */ 123 parent::retrieve(); 124 125 /* Make sure we're connected. */ 126 $this->_connect(); 127 128 /* Build the SQL query. */ 129 $query = 'SELECT pref_scope, pref_name, pref_value FROM '; 130 $query .= $this->_params['table'] . ' '; 131 $query .= 'WHERE pref_uid = ?'; 132 $query .= ' AND (pref_scope = ?'; 133 $query .= " OR pref_scope = 'horde') ORDER BY pref_scope"; 134 135 $values = array($this->_user, $this->_scope); 136 137 Horde::logMessage(sprintf('SQL Query by Prefs_sql::retrieve(): %s', $query), __FILE__, __LINE__, PEAR_LOG_DEBUG); 138 139 /* Execute the query. */ 140 $result = $this->_db->query($query, $values); 141 142 if (isset($result) && !is_a($result, 'PEAR_Error')) { 143 $row = $result->fetchRow(DB_FETCHMODE_ASSOC); 144 if (is_a($row, 'PEAR_Error')) { 145 Horde::logMessage($row, __FILE__, __LINE__, PEAR_LOG_ERR); 146 return; 147 } 148 149 /* Set the requested values in the $this->_prefs hash 150 * based on the contents of the SQL result. 151 * 152 * Note that Prefs::setValue() can't be used here because 153 * of the check for the "changeable" bit. We want to 154 * override that check when populating the $this->_prefs 155 * hash from the SQL server. */ 156 while ($row && !is_a($row, 'PEAR_Error')) { 157 $name = trim($row['pref_name']); 158 if (isset($this->_prefs[$name])) { 159 $this->_setValue($name, $row['pref_value'], false, false); 160 $this->setDirty($name, false); 161 } else { 162 $this->add($name, $row['pref_value'], $row['pref_scope'] == 'horde' ? _PREF_SHARED : 0); 163 } 164 $row = $result->fetchRow(DB_FETCHMODE_ASSOC); 165 } 166 167 /* Call hooks. */ 168 $this->_callHooks(); 169 } else { 170 Horde::logMessage('No preferences were retrieved.', __FILE__, __LINE__, PEAR_LOG_DEBUG); 171 return; 172 } 173 174 /* Update the session cache. */ 175 $this->cacheUpdate(); 176 177 return true; 178 } 179 180 /** 181 * Stores preferences to SQL server. 182 * 183 * @return mixed True on success or a PEAR_Error object on failure. 184 */ 185 function store() 186 { 187 /* Check for any "dirty" preferences. If no "dirty" 188 * preferences are found, there's no need to update the SQL 189 * server. Exit successfully. */ 190 $dirty_prefs = $this->_dirtyPrefs(); 191 if (!count($dirty_prefs)) { 192 return true; 193 } 194 195 /* Make sure we're connected. */ 196 $this->_connect(); 197 198 /* Loop through the "dirty" preferences. If a row already 199 * exists for this preference, attempt to update it. 200 * Otherwise, insert a new row. */ 201 foreach ($dirty_prefs as $name) { 202 // Don't store locked preferences. 203 if ($this->isLocked($name)) { 204 continue; 205 } 206 207 $scope = $this->getScope($name); 208 209 /* Does an entry already exist for this preference? */ 210 $query = 'SELECT 1 FROM '; 211 $query .= $this->_params['table'] . ' '; 212 $query .= 'WHERE pref_uid = ?'; 213 $query .= ' AND pref_name = ?'; 214 $query .= ' AND (pref_scope = ?'; 215 $query .= " OR pref_scope = 'horde')"; 216 217 $values = array($this->_user, $name, $scope); 218 219 /* Execute the query. */ 220 $check = $this->_db->getOne($query, $values); 221 222 /* Return an error if the query fails. */ 223 if (is_a($check, 'PEAR_Error')) { 224 Horde::logMessage('Failed retrieving prefs for ' . $this->_user, __FILE__, __LINE__, PEAR_LOG_ERR); 225 return PEAR::raiseError(_("Failed retrieving preferences.")); 226 } 227 228 /* Is there an existing row for this preference? */ 229 if (!empty($check)) { 230 /* Update the existing row. */ 231 $query = 'UPDATE ' . $this->_params['table'] . ' '; 232 $query .= 'SET pref_value = ?'; 233 $query .= ' WHERE pref_uid = ?'; 234 $query .= ' AND pref_name = ?'; 235 $query .= ' AND pref_scope = ?'; 236 237 $values = array((string)$this->getValue($name, false), 238 $this->_user, 239 $name, 240 $scope); 241 242 $result = $this->_db->query($query, $values); 243 244 /* Return an error if the update fails. */ 245 if (is_a($result, 'PEAR_Error')) { 246 Horde::fatal($result, __FILE__, __LINE__); 247 } 248 } else { 249 /* Insert a new row. */ 250 $query = 'INSERT INTO ' . $this->_params['table'] . ' '; 251 $query .= '(pref_uid, pref_scope, pref_name, pref_value) VALUES'; 252 $query .= '(?, ?, ?, ?)'; 253 254 $values = array($this->_user, 255 $scope, 256 $name, 257 (string)$this->getValue($name, false)); 258 259 $result = $this->_db->query($query, $values); 260 261 /* Return an error if the insert fails. */ 262 if (is_a($result, 'PEAR_Error')) { 263 Horde::fatal($result, __FILE__, __LINE__); 264 } 265 } 266 267 /* Mark this preference as "clean" now. */ 268 $this->setDirty($name, false); 269 } 270 271 /* Update the session cache. */ 272 $this->cacheUpdate(); 273 274 return true; 275 } 276 277 /** 278 * Perform cleanup operations. 279 * 280 * @param boolean $all Cleanup all Horde preferences. 281 */ 282 function cleanup($all = false) 283 { 284 /* Close the database connection. */ 285 $this->_disconnect(); 286 287 parent::cleanup($all); 288 } 289 290 /** 291 * Clears all preferences from the backend. 292 */ 293 function clear() 294 { 295 /* Make sure we're connected. */ 296 $this->_connect(); 297 298 /* Build the SQL query. */ 299 $query = 'DELETE FROM ' . $this->_params['table']; 300 $query .= ' WHERE pref_uid = ?'; 301 302 $values = array($this->_user); 303 304 Horde::logMessage(sprintf('SQL Query by Prefs_sql::clear(): %s', $query), __FILE__, __LINE__, PEAR_LOG_DEBUG); 305 306 /* Execute the query. */ 307 $result = $this->_db->query($query, $values); 308 309 /* Cleanup. */ 310 parent::clear(); 311 312 return $result; 313 } 314 315 /** 316 * Converts a value from the driver's charset to the specified charset. 317 * 318 * @param mixed $value A value to convert. 319 * @param string $charset The charset to convert to. 320 * 321 * @return mixed The converted value. 322 */ 323 function convertFromDriver($value, $charset) 324 { 325 static $converted = array(); 326 327 if (is_array($value)) { 328 return String::convertCharset($value, $this->_params['charset'], $charset); 329 } 330 331 if (is_bool($value)) { 332 return $value; 333 } 334 335 if (!isset($converted[$charset][$value])) { 336 $converted[$charset][$value] = String::convertCharset($value, $this->_params['charset'], $charset); 337 } 338 339 return $converted[$charset][$value]; 340 } 341 342 /** 343 * Converts a value from the specified charset to the driver's charset. 344 * 345 * @param mixed $value A value to convert. 346 * @param string $charset The charset to convert from. 347 * 348 * @return mixed The converted value. 349 */ 350 function convertToDriver($value, $charset) 351 { 352 return String::convertCharset($value, $charset, $this->_params['charset']); 353 } 354 355 /** 356 * Attempts to open a persistent connection to the SQL server. 357 * 358 * @access private 359 * 360 * @return mixed True on success or a PEAR_Error object on failure. 361 */ 362 function _connect() 363 { 364 /* Check to see if we are already connected. */ 365 if ($this->_connected) { 366 return true; 367 } 368 369 Horde::assertDriverConfig($this->_params, 'prefs', 370 array('phptype', 'charset'), 371 'preferences SQL'); 372 373 if (!isset($this->_params['database'])) { 374 $this->_params['database'] = ''; 375 } 376 if (!isset($this->_params['username'])) { 377 $this->_params['username'] = ''; 378 } 379 if (!isset($this->_params['password'])) { 380 $this->_params['password'] = ''; 381 } 382 if (!isset($this->_params['hostspec'])) { 383 $this->_params['hostspec'] = ''; 384 } 385 if (!isset($this->_params['table'])) { 386 $this->_params['table'] = 'horde_prefs'; 387 } 388 389 /* Connect to the SQL server using the supplied parameters. */ 390 $this->_db = &DB::connect($this->_params, 391 array('persistent' => !empty($this->_params['persistent']))); 392 if (is_a($this->_db, 'PEAR_Error')) { 393 Horde::fatal($this->_db, __FILE__, __LINE__); 394 } 395 396 // Set DB portability options. 397 switch ($this->_db->phptype) { 398 case 'mssql': 399 $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM); 400 break; 401 default: 402 $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS); 403 } 404 405 $this->_connected = true; 406 407 return true; 408 } 409 410 /** 411 * Disconnect from the SQL server and clean up the connection. 412 * 413 * @access private 414 * 415 * @return boolean True on success, false on failure. 416 */ 417 function _disconnect() 418 { 419 if ($this->_connected) { 420 $this->_connected = false; 421 return $this->_db->disconnect(); 422 } else { 423 return true; 424 } 425 } 426 427 }
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 |