| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * db_prefs.php |
| 5 | * |
| 6 | * This contains functions for manipulating user preferences |
| 7 | * stored in a database, accessed through the Pear DB layer |
| 8 | * or PDO, the latter taking precedence if available. |
| 9 | * |
| 10 | * Database: |
| 11 | * |
| 12 | * The preferences table should have three columns: |
| 13 | * user char \ primary |
| 14 | * prefkey char / key |
| 15 | * prefval blob |
| 16 | * |
| 17 | * CREATE TABLE userprefs (user CHAR(128) NOT NULL DEFAULT '', |
| 18 | * prefkey CHAR(64) NOT NULL DEFAULT '', |
| 19 | * prefval BLOB NOT NULL DEFAULT '', |
| 20 | * primary key (user,prefkey)); |
| 21 | * |
| 22 | * Configuration of databasename, username and password is done |
| 23 | * by using conf.pl or the administrator plugin |
| 24 | * |
| 25 | * Three settings that control PDO behavior can be specified in |
| 26 | * config/config_local.php if needed: |
| 27 | * boolean $disable_pdo SquirrelMail uses PDO by default to access the |
| 28 | * user preferences and address book databases, but |
| 29 | * setting this to TRUE will cause SquirrelMail to |
| 30 | * fall back to using Pear DB instead. |
| 31 | * boolean $pdo_show_sql_errors When database errors are encountered, |
| 32 | * setting this to TRUE causes the actual |
| 33 | * database error to be displayed, otherwise |
| 34 | * generic errors are displayed, preventing |
| 35 | * internal database information from being |
| 36 | * exposed. This should be enabled only for |
| 37 | * debugging purposes. |
| 38 | * string $pdo_identifier_quote_char By default, SquirrelMail will quote |
| 39 | * table and field names in database |
| 40 | * queries with what it thinks is the |
| 41 | * appropriate quote character for the |
| 42 | * database type being used (backtick |
| 43 | * for MySQL (and thus MariaDB), double |
| 44 | * quotes for all others), but you can |
| 45 | * override the character used by |
| 46 | * putting it here, or tell SquirrelMail |
| 47 | * NOT to quote identifiers by setting |
| 48 | * this to "none" |
| 49 | * |
| 50 | * @copyright 1999-2018 The SquirrelMail Project Team |
| 51 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| 52 | * @version $Id$ |
| 53 | * @package squirrelmail |
| 54 | * @subpackage prefs |
| 55 | * @since 1.1.3 |
| 56 | */ |
| 57 | |
| 58 | /** @ignore */ |
| 59 | if (!defined('SM_PATH')) define('SM_PATH','../'); |
| 60 | |
| 61 | /** Unknown database */ |
| 62 | define('SMDB_UNKNOWN', 0); |
| 63 | /** MySQL */ |
| 64 | define('SMDB_MYSQL', 1); |
| 65 | /** PostgreSQL */ |
| 66 | define('SMDB_PGSQL', 2); |
| 67 | |
| 68 | /** |
| 69 | * Needs either PDO or the DB functions |
| 70 | * Don't display errors here. (no code execution in functions/*.php). |
| 71 | * will handle error in dbPrefs class. |
| 72 | */ |
| 73 | global $use_pdo, $disable_pdo; |
| 74 | if (empty($disable_pdo) && class_exists('PDO')) |
| 75 | $use_pdo = TRUE; |
| 76 | else |
| 77 | $use_pdo = FALSE; |
| 78 | |
| 79 | if (!$use_pdo) |
| 80 | @include_once('DB.php'); |
| 81 | |
| 82 | global $prefs_are_cached, $prefs_cache; |
| 83 | |
| 84 | /** |
| 85 | * @ignore |
| 86 | */ |
| 87 | function cachePrefValues($username) { |
| 88 | global $prefs_are_cached, $prefs_cache; |
| 89 | |
| 90 | sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION ); |
| 91 | if ($prefs_are_cached) { |
| 92 | sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION ); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | sqsession_unregister('prefs_cache'); |
| 97 | sqsession_unregister('prefs_are_cached'); |
| 98 | |
| 99 | $db = new dbPrefs; |
| 100 | if(isset($db->error)) { |
| 101 | printf( _("Preference database error (%s). Exiting abnormally"), |
| 102 | $db->error); |
| 103 | exit; |
| 104 | } |
| 105 | |
| 106 | $db->fillPrefsCache($username); |
| 107 | if (isset($db->error)) { |
| 108 | printf( _("Preference database error (%s). Exiting abnormally"), |
| 109 | $db->error); |
| 110 | exit; |
| 111 | } |
| 112 | |
| 113 | $prefs_are_cached = true; |
| 114 | |
| 115 | sqsession_register($prefs_cache, 'prefs_cache'); |
| 116 | sqsession_register($prefs_are_cached, 'prefs_are_cached'); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Class used to handle connections to prefs database and operations with preferences |
| 121 | * |
| 122 | * @package squirrelmail |
| 123 | * @subpackage prefs |
| 124 | * @since 1.1.3 |
| 125 | * |
| 126 | */ |
| 127 | class dbPrefs { |
| 128 | /** |
| 129 | * Table used to store preferences |
| 130 | * @var string |
| 131 | */ |
| 132 | var $table = 'userprefs'; |
| 133 | |
| 134 | /** |
| 135 | * Field used to store owner of preference |
| 136 | * @var string |
| 137 | */ |
| 138 | var $user_field = 'user'; |
| 139 | |
| 140 | /** |
| 141 | * Field used to store preference name |
| 142 | * @var string |
| 143 | */ |
| 144 | var $key_field = 'prefkey'; |
| 145 | |
| 146 | /** |
| 147 | * Field used to store preference value |
| 148 | * @var string |
| 149 | */ |
| 150 | var $val_field = 'prefval'; |
| 151 | |
| 152 | /** |
| 153 | * Database connection object |
| 154 | * @var object |
| 155 | */ |
| 156 | var $dbh = NULL; |
| 157 | |
| 158 | /** |
| 159 | * Error messages |
| 160 | * @var string |
| 161 | */ |
| 162 | var $error = NULL; |
| 163 | |
| 164 | /** |
| 165 | * Database type (SMDB_* constants) |
| 166 | * Is used in setKey(). |
| 167 | * @var integer |
| 168 | */ |
| 169 | var $db_type = SMDB_UNKNOWN; |
| 170 | |
| 171 | /** |
| 172 | * Character used to quote database table |
| 173 | * and field names |
| 174 | * @var string |
| 175 | */ |
| 176 | var $identifier_quote_char = ''; |
| 177 | |
| 178 | /** |
| 179 | * Default preferences |
| 180 | * @var array |
| 181 | */ |
| 182 | var $default = Array('theme_default' => 0, |
| 183 | 'include_self_reply_all' => '0', |
| 184 | 'do_not_reply_to_self' => '1', |
| 185 | 'show_html_default' => '0'); |
| 186 | |
| 187 | /** |
| 188 | * Preference owner field size |
| 189 | * @var integer |
| 190 | * @since 1.5.1 |
| 191 | */ |
| 192 | var $user_size = 128; |
| 193 | |
| 194 | /** |
| 195 | * Preference key field size |
| 196 | * @var integer |
| 197 | * @since 1.5.1 |
| 198 | */ |
| 199 | var $key_size = 64; |
| 200 | |
| 201 | /** |
| 202 | * Preference value field size |
| 203 | * @var integer |
| 204 | * @since 1.5.1 |
| 205 | */ |
| 206 | var $val_size = 65536; |
| 207 | |
| 208 | |
| 209 | |
| 210 | /** |
| 211 | * Constructor (PHP5 style, required in some future version of PHP) |
| 212 | * initialize the default preferences array. |
| 213 | * |
| 214 | */ |
| 215 | function __construct() { |
| 216 | // Try and read the default preferences file. |
| 217 | $default_pref = SM_PATH . 'config/default_pref'; |
| 218 | if (@file_exists($default_pref)) { |
| 219 | if ($file = @fopen($default_pref, 'r')) { |
| 220 | while (!feof($file)) { |
| 221 | $pref = fgets($file, 1024); |
| 222 | $i = strpos($pref, '='); |
| 223 | if ($i > 0) { |
| 224 | $this->default[trim(substr($pref, 0, $i))] = trim(substr($pref, $i + 1)); |
| 225 | } |
| 226 | } |
| 227 | fclose($file); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Constructor (PHP4 style, kept for compatibility reasons) |
| 234 | * initialize the default preferences array. |
| 235 | * |
| 236 | */ |
| 237 | function dbPrefs() { |
| 238 | self::__construct(); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * initialize DB connection object |
| 243 | * |
| 244 | * @return boolean true, if object is initialized |
| 245 | * |
| 246 | */ |
| 247 | function open() { |
| 248 | global $prefs_dsn, $prefs_table, $use_pdo, $pdo_identifier_quote_char; |
| 249 | global $prefs_user_field, $prefs_key_field, $prefs_val_field; |
| 250 | global $prefs_user_size, $prefs_key_size, $prefs_val_size; |
| 251 | |
| 252 | /* test if PDO or Pear DB classes are available and freak out if necessary */ |
| 253 | if (!$use_pdo && !class_exists('DB')) { |
| 254 | // same error also in abook_database.php |
| 255 | $error = _("Could not find or include PHP PDO or PEAR database functions required for the database backend.") . "\n"; |
| 256 | $error .= sprintf(_("PDO should come preinstalled with PHP version 5.1 or higher. Otherwise, is PEAR installed, and is the include path set correctly to find %s?"), 'DB.php') . "\n"; |
| 257 | $error .= _("Please contact your system administrator and report this error."); |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | if(isset($this->dbh)) { |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | if (strpos($prefs_dsn, 'mysql') === 0) { |
| 266 | $this->db_type = SMDB_MYSQL; |
| 267 | } else if (strpos($prefs_dsn, 'pgsql') === 0) { |
| 268 | $this->db_type = SMDB_PGSQL; |
| 269 | } |
| 270 | |
| 271 | // figure out identifier quoting (only used for PDO, though we could change that) |
| 272 | if (empty($pdo_identifier_quote_char)) { |
| 273 | if ($this->db_type == SMDB_MYSQL) |
| 274 | $this->identifier_quote_char = '`'; |
| 275 | else |
| 276 | $this->identifier_quote_char = '"'; |
| 277 | } else if ($pdo_identifier_quote_char === 'none') |
| 278 | $this->identifier_quote_char = ''; |
| 279 | else |
| 280 | $this->identifier_quote_char = $pdo_identifier_quote_char; |
| 281 | |
| 282 | if (!empty($prefs_table)) { |
| 283 | $this->table = $prefs_table; |
| 284 | } |
| 285 | if (!empty($prefs_user_field)) { |
| 286 | $this->user_field = $prefs_user_field; |
| 287 | } |
| 288 | |
| 289 | // the default user field is "user", which in PostgreSQL |
| 290 | // is an identifier and causes errors if not escaped |
| 291 | // |
| 292 | if ($this->db_type == SMDB_PGSQL) { |
| 293 | $this->user_field = '"' . $this->user_field . '"'; |
| 294 | } |
| 295 | |
| 296 | if (!empty($prefs_key_field)) { |
| 297 | $this->key_field = $prefs_key_field; |
| 298 | } |
| 299 | if (!empty($prefs_val_field)) { |
| 300 | $this->val_field = $prefs_val_field; |
| 301 | } |
| 302 | if (!empty($prefs_user_size)) { |
| 303 | $this->user_size = (int) $prefs_user_size; |
| 304 | } |
| 305 | if (!empty($prefs_key_size)) { |
| 306 | $this->key_size = (int) $prefs_key_size; |
| 307 | } |
| 308 | if (!empty($prefs_val_size)) { |
| 309 | $this->val_size = (int) $prefs_val_size; |
| 310 | } |
| 311 | |
| 312 | // connect, create database connection object |
| 313 | // |
| 314 | if ($use_pdo) { |
| 315 | // parse and convert DSN to PDO style |
| 316 | // Pear's full DSN syntax is one of the following: |
| 317 | // phptype(dbsyntax)://username:password@protocol+hostspec/database?option=value |
| 318 | // phptype(syntax)://user:pass@protocol(proto_opts)/database |
| 319 | // |
| 320 | // $matches will contain: |
| 321 | // 1: database type |
| 322 | // 2: username |
| 323 | // 3: password |
| 324 | // 4: hostname (and possible port number) OR protocol (and possible protocol options) |
| 325 | // 5: database name (and possible options) |
| 326 | // 6: port number (moved from match number 4) |
| 327 | // 7: options (moved from match number 5) |
| 328 | // 8: protocol (instead of hostname) |
| 329 | // 9: protocol options (moved from match number 4/8) |
| 330 | //TODO: do we care about supporting cases where no password is given? (this is a legal DSN, but causes an error below) |
| 331 | if (!preg_match('|^(.+)://(.+):(.+)@(.+)/(.+)$|i', $prefs_dsn, $matches)) { |
| 332 | $this->error = _("Could not parse prefs DSN"); |
| 333 | return false; |
| 334 | } |
| 335 | $matches[6] = NULL; |
| 336 | $matches[7] = NULL; |
| 337 | $matches[8] = NULL; |
| 338 | $matches[9] = NULL; |
| 339 | if (preg_match('|^(.+):(\d+)$|', $matches[4], $host_port_matches)) { |
| 340 | $matches[4] = $host_port_matches[1]; |
| 341 | $matches[6] = $host_port_matches[2]; |
| 342 | } |
| 343 | if (preg_match('|^(.+?)\((.+)\)$|', $matches[4], $protocol_matches)) { |
| 344 | $matches[8] = $protocol_matches[1]; |
| 345 | $matches[9] = $protocol_matches[2]; |
| 346 | $matches[4] = NULL; |
| 347 | $matches[6] = NULL; |
| 348 | } |
| 349 | //TODO: currently we just ignore options specified on the end of the DSN |
| 350 | if (preg_match('|^(.+?)\?(.+)$|', $matches[5], $database_name_options_matches)) { |
| 351 | $matches[5] = $database_name_options_matches[1]; |
| 352 | $matches[7] = $database_name_options_matches[2]; |
| 353 | } |
| 354 | if ($matches[8] === 'unix' && !empty($matches[9])) |
| 355 | $pdo_prefs_dsn = $matches[1] . ':unix_socket=' . $matches[9] . ';dbname=' . $matches[5]; |
| 356 | else |
| 357 | $pdo_prefs_dsn = $matches[1] . ':host=' . $matches[4] . (!empty($matches[6]) ? ';port=' . $matches[6] : '') . ';dbname=' . $matches[5]; |
| 358 | try { |
| 359 | $dbh = new PDO($pdo_prefs_dsn, $matches[2], $matches[3]); |
| 360 | } catch (Exception $e) { |
| 361 | $this->error = $e->getMessage(); |
| 362 | return false; |
| 363 | } |
| 364 | } else { |
| 365 | $dbh = DB::connect($prefs_dsn, true); |
| 366 | |
| 367 | if(DB::isError($dbh)) { |
| 368 | $this->error = DB::errorMessage($dbh); |
| 369 | return false; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | $this->dbh = $dbh; |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Function used to handle database connection errors |
| 379 | * |
| 380 | * @param object PEAR Error object |
| 381 | * |
| 382 | */ |
| 383 | function failQuery($res = NULL) { |
| 384 | global $use_pdo; |
| 385 | if($res == NULL) { |
| 386 | printf(_("Preference database error (%s). Exiting abnormally"), |
| 387 | $this->error); |
| 388 | } else { |
| 389 | printf(_("Preference database error (%s). Exiting abnormally"), |
| 390 | ($use_pdo ? implode(' - ', $res->errorInfo()) : DB::errorMessage($res))); |
| 391 | } |
| 392 | exit; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Get user's prefs setting |
| 397 | * |
| 398 | * @param string $user user name |
| 399 | * @param string $key preference name |
| 400 | * @param mixed $default (since 1.2.5) default value |
| 401 | * |
| 402 | * @return mixed preference value |
| 403 | * |
| 404 | */ |
| 405 | function getKey($user, $key, $default = '') { |
| 406 | global $prefs_cache; |
| 407 | |
| 408 | $temp = array(&$user, &$key); |
| 409 | $result = do_hook('get_pref_override', $temp); |
| 410 | if (is_null($result)) { |
| 411 | cachePrefValues($user); |
| 412 | |
| 413 | if (isset($prefs_cache[$key])) { |
| 414 | $result = $prefs_cache[$key]; |
| 415 | } else { |
| 416 | //FIXME: is there a justification for having two prefs hooks so close? who uses them? |
| 417 | $temp = array(&$user, &$key); |
| 418 | $result = do_hook('get_pref', $temp); |
| 419 | if (is_null($result)) { |
| 420 | if (isset($this->default[$key])) { |
| 421 | $result = $this->default[$key]; |
| 422 | } else { |
| 423 | $result = $default; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | return $result; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Delete user's prefs setting |
| 433 | * |
| 434 | * @param string $user user name |
| 435 | * @param string $key preference name |
| 436 | * |
| 437 | * @return boolean |
| 438 | * |
| 439 | */ |
| 440 | function deleteKey($user, $key) { |
| 441 | global $prefs_cache, $use_pdo, $pdo_show_sql_errors; |
| 442 | |
| 443 | if (!$this->open()) { |
| 444 | return false; |
| 445 | } |
| 446 | if ($use_pdo) { |
| 447 | if (!($sth = $this->dbh->prepare('DELETE FROM ' . $this->identifier_quote_char . $this->table . $this->identifier_quote_char . ' WHERE ' . $this->identifier_quote_char . $this->user_field . $this->identifier_quote_char . ' = ? AND ' . $this->identifier_quote_char . $this->key_field . $this->identifier_quote_char . ' = ?'))) { |
| 448 | if ($pdo_show_sql_errors) |
| 449 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
| 450 | else |
| 451 | $this->error = _("Could not prepare query"); |
| 452 | $this->failQuery(); |
| 453 | } |
| 454 | if (!($res = $sth->execute(array($user, $key)))) { |
| 455 | if ($pdo_show_sql_errors) |
| 456 | $this->error = implode(' - ', $sth->errorInfo()); |
| 457 | else |
| 458 | $this->error = _("Could not execute query"); |
| 459 | $this->failQuery(); |
| 460 | } |
| 461 | } else { |
| 462 | $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", |
| 463 | $this->table, |
| 464 | $this->user_field, |
| 465 | $this->dbh->quoteString($user), |
| 466 | $this->key_field, |
| 467 | $this->dbh->quoteString($key)); |
| 468 | |
| 469 | $res = $this->dbh->simpleQuery($query); |
| 470 | if(DB::isError($res)) { |
| 471 | $this->failQuery($res); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | unset($prefs_cache[$key]); |
| 476 | |
| 477 | return true; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Set user's preference |
| 482 | * |
| 483 | * @param string $user user name |
| 484 | * @param string $key preference name |
| 485 | * @param mixed $value preference value |
| 486 | * |
| 487 | * @return boolean |
| 488 | * |
| 489 | */ |
| 490 | function setKey($user, $key, $value) { |
| 491 | global $use_pdo, $pdo_show_sql_errors; |
| 492 | if (!$this->open()) { |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Check if username fits into db field |
| 498 | */ |
| 499 | if (strlen($user) > $this->user_size) { |
| 500 | $this->error = "Oversized username value." |
| 501 | ." Your preferences can't be saved." |
| 502 | ." See the administrator's manual or contact your system administrator."; |
| 503 | |
| 504 | /** |
| 505 | * Debugging function. Can be used to log all issues that trigger |
| 506 | * oversized field errors. Function should be enabled in all three |
| 507 | * strlen checks. See http://www.php.net/error-log |
| 508 | */ |
| 509 | // error_log($user.'|'.$key.'|'.$value."\n",3,'/tmp/oversized_log'); |
| 510 | |
| 511 | // error is fatal |
| 512 | $this->failQuery(null); |
| 513 | } |
| 514 | /** |
| 515 | * Check if preference key fits into db field |
| 516 | */ |
| 517 | if (strlen($key) > $this->key_size) { |
| 518 | $err_msg = "Oversized user's preference key." |
| 519 | ." Some preferences were not saved." |
| 520 | ." See the administrator's manual or contact your system administrator."; |
| 521 | // error is not fatal. Only some preference is not saved. |
| 522 | trigger_error($err_msg,E_USER_WARNING); |
| 523 | return false; |
| 524 | } |
| 525 | /** |
| 526 | * Check if preference value fits into db field |
| 527 | */ |
| 528 | if (strlen($value) > $this->val_size) { |
| 529 | $err_msg = "Oversized user's preference value." |
| 530 | ." Some preferences were not saved." |
| 531 | ." See the administrator's manual or contact your system administrator."; |
| 532 | // error is not fatal. Only some preference is not saved. |
| 533 | trigger_error($err_msg,E_USER_WARNING); |
| 534 | return false; |
| 535 | } |
| 536 | |
| 537 | |
| 538 | if ($this->db_type == SMDB_MYSQL) { |
| 539 | if ($use_pdo) { |
| 540 | if (!($sth = $this->dbh->prepare('REPLACE INTO ' . $this->identifier_quote_char . $this->table . $this->identifier_quote_char . ' (' . $this->identifier_quote_char . $this->user_field . $this->identifier_quote_char . ', ' . $this->identifier_quote_char . $this->key_field . $this->identifier_quote_char . ', ' . $this->identifier_quote_char . $this->val_field . $this->identifier_quote_char . ') VALUES (?, ?, ?)'))) { |
| 541 | if ($pdo_show_sql_errors) |
| 542 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
| 543 | else |
| 544 | $this->error = _("Could not prepare query"); |
| 545 | $this->failQuery(); |
| 546 | } |
| 547 | if (!($res = $sth->execute(array($user, $key, $value)))) { |
| 548 | if ($pdo_show_sql_errors) |
| 549 | $this->error = implode(' - ', $sth->errorInfo()); |
| 550 | else |
| 551 | $this->error = _("Could not execute query"); |
| 552 | $this->failQuery(); |
| 553 | } |
| 554 | } else { |
| 555 | $query = sprintf("REPLACE INTO %s (%s, %s, %s) ". |
| 556 | "VALUES('%s','%s','%s')", |
| 557 | $this->table, |
| 558 | $this->user_field, |
| 559 | $this->key_field, |
| 560 | $this->val_field, |
| 561 | $this->dbh->quoteString($user), |
| 562 | $this->dbh->quoteString($key), |
| 563 | $this->dbh->quoteString($value)); |
| 564 | |
| 565 | $res = $this->dbh->simpleQuery($query); |
| 566 | if(DB::isError($res)) { |
| 567 | $this->failQuery($res); |
| 568 | } |
| 569 | } |
| 570 | } elseif ($this->db_type == SMDB_PGSQL) { |
| 571 | if ($use_pdo) { |
| 572 | if ($this->dbh->exec('BEGIN TRANSACTION') === FALSE) { |
| 573 | if ($pdo_show_sql_errors) |
| 574 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
| 575 | else |
| 576 | $this->error = _("Could not execute query"); |
| 577 | $this->failQuery(); |
| 578 | } |
| 579 | if (!($sth = $this->dbh->prepare('DELETE FROM ' . $this->identifier_quote_char . $this->table . $this->identifier_quote_char . ' WHERE ' . $this->identifier_quote_char . $this->user_field . $this->identifier_quote_char . ' = ? AND ' . $this->identifier_quote_char . $this->key_field . $this->identifier_quote_char . ' = ?'))) { |
| 580 | if ($pdo_show_sql_errors) |
| 581 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
| 582 | else |
| 583 | $this->error = _("Could not prepare query"); |
| 584 | $this->failQuery(); |
| 585 | } |
| 586 | if (!($res = $sth->execute(array($user, $key)))) { |
| 587 | if ($pdo_show_sql_errors) |
| 588 | $this->error = implode(' - ', $sth->errorInfo()); |
| 589 | else |
| 590 | $this->error = _("Could not execute query"); |
| 591 | $this->dbh->exec('ROLLBACK TRANSACTION'); |
| 592 | $this->failQuery(); |
| 593 | } |
| 594 | if (!($sth = $this->dbh->prepare('INSERT INTO ' . $this->identifier_quote_char . $this->table . $this->identifier_quote_char . ' (' . $this->identifier_quote_char . $this->user_field . $this->identifier_quote_char . ', ' . $this->identifier_quote_char . $this->key_field . $this->identifier_quote_char . ', ' . $this->identifier_quote_char . $this->val_field . $this->identifier_quote_char . ') VALUES (?, ?, ?)'))) { |
| 595 | if ($pdo_show_sql_errors) |
| 596 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
| 597 | else |
| 598 | $this->error = _("Could not prepare query"); |
| 599 | $this->failQuery(); |
| 600 | } |
| 601 | if (!($res = $sth->execute(array($user, $key, $value)))) { |
| 602 | if ($pdo_show_sql_errors) |
| 603 | $this->error = implode(' - ', $sth->errorInfo()); |
| 604 | else |
| 605 | $this->error = _("Could not execute query"); |
| 606 | $this->dbh->exec('ROLLBACK TRANSACTION'); |
| 607 | $this->failQuery(); |
| 608 | } |
| 609 | if ($this->dbh->exec('COMMIT TRANSACTION') === FALSE) { |
| 610 | if ($pdo_show_sql_errors) |
| 611 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
| 612 | else |
| 613 | $this->error = _("Could not execute query"); |
| 614 | $this->failQuery(); |
| 615 | } |
| 616 | } else { |
| 617 | $this->dbh->simpleQuery("BEGIN TRANSACTION"); |
| 618 | $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", |
| 619 | $this->table, |
| 620 | $this->user_field, |
| 621 | $this->dbh->quoteString($user), |
| 622 | $this->key_field, |
| 623 | $this->dbh->quoteString($key)); |
| 624 | $res = $this->dbh->simpleQuery($query); |
| 625 | if (DB::isError($res)) { |
| 626 | $this->dbh->simpleQuery("ROLLBACK TRANSACTION"); |
| 627 | $this->failQuery($res); |
| 628 | } |
| 629 | $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')", |
| 630 | $this->table, |
| 631 | $this->user_field, |
| 632 | $this->key_field, |
| 633 | $this->val_field, |
| 634 | $this->dbh->quoteString($user), |
| 635 | $this->dbh->quoteString($key), |
| 636 | $this->dbh->quoteString($value)); |
| 637 | $res = $this->dbh->simpleQuery($query); |
| 638 | if (DB::isError($res)) { |
| 639 | $this->dbh->simpleQuery("ROLLBACK TRANSACTION"); |
| 640 | $this->failQuery($res); |
| 641 | } |
| 642 | $this->dbh->simpleQuery("COMMIT TRANSACTION"); |
| 643 | } |
| 644 | } else { |
| 645 | if ($use_pdo) { |
| 646 | if (!($sth = $this->dbh->prepare('DELETE FROM ' . $this->identifier_quote_char . $this->table . $this->identifier_quote_char . ' WHERE ' . $this->identifier_quote_char . $this->user_field . $this->identifier_quote_char . ' = ? AND ' . $this->identifier_quote_char . $this->key_field . $this->identifier_quote_char . ' = ?'))) { |
| 647 | if ($pdo_show_sql_errors) |
| 648 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
| 649 | else |
| 650 | $this->error = _("Could not prepare query"); |
| 651 | $this->failQuery(); |
| 652 | } |
| 653 | if (!($res = $sth->execute(array($user, $key)))) { |
| 654 | if ($pdo_show_sql_errors) |
| 655 | $this->error = implode(' - ', $sth->errorInfo()); |
| 656 | else |
| 657 | $this->error = _("Could not execute query"); |
| 658 | $this->failQuery(); |
| 659 | } |
| 660 | if (!($sth = $this->dbh->prepare('INSERT INTO ' . $this->identifier_quote_char . $this->table . $this->identifier_quote_char . ' (' . $this->identifier_quote_char . $this->user_field . $this->identifier_quote_char . ', ' . $this->identifier_quote_char . $this->key_field . $this->identifier_quote_char . ', ' . $this->identifier_quote_char . $this->val_field . $this->identifier_quote_char . ') VALUES (?, ?, ?)'))) { |
| 661 | if ($pdo_show_sql_errors) |
| 662 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
| 663 | else |
| 664 | $this->error = _("Could not prepare query"); |
| 665 | $this->failQuery(); |
| 666 | } |
| 667 | if (!($res = $sth->execute(array($user, $key, $value)))) { |
| 668 | if ($pdo_show_sql_errors) |
| 669 | $this->error = implode(' - ', $sth->errorInfo()); |
| 670 | else |
| 671 | $this->error = _("Could not execute query"); |
| 672 | $this->failQuery(); |
| 673 | } |
| 674 | } else { |
| 675 | $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", |
| 676 | $this->table, |
| 677 | $this->user_field, |
| 678 | $this->dbh->quoteString($user), |
| 679 | $this->key_field, |
| 680 | $this->dbh->quoteString($key)); |
| 681 | $res = $this->dbh->simpleQuery($query); |
| 682 | if (DB::isError($res)) { |
| 683 | $this->failQuery($res); |
| 684 | } |
| 685 | $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')", |
| 686 | $this->table, |
| 687 | $this->user_field, |
| 688 | $this->key_field, |
| 689 | $this->val_field, |
| 690 | $this->dbh->quoteString($user), |
| 691 | $this->dbh->quoteString($key), |
| 692 | $this->dbh->quoteString($value)); |
| 693 | $res = $this->dbh->simpleQuery($query); |
| 694 | if (DB::isError($res)) { |
| 695 | $this->failQuery($res); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | return true; |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * Fill preference cache array |
| 705 | * |
| 706 | * @param string $user user name |
| 707 | * |
| 708 | * @since 1.2.3 |
| 709 | * |
| 710 | */ |
| 711 | function fillPrefsCache($user) { |
| 712 | global $prefs_cache, $use_pdo, $pdo_show_sql_errors; |
| 713 | |
| 714 | if (!$this->open()) { |
| 715 | return; |
| 716 | } |
| 717 | |
| 718 | $prefs_cache = array(); |
| 719 | if ($use_pdo) { |
| 720 | if (!($sth = $this->dbh->prepare('SELECT ' . $this->identifier_quote_char . $this->key_field . $this->identifier_quote_char . ' AS prefkey, ' . $this->identifier_quote_char . $this->val_field . $this->identifier_quote_char . ' AS prefval FROM ' . $this->identifier_quote_char . $this->table . $this->identifier_quote_char . ' WHERE ' . $this->identifier_quote_char . $this->user_field . $this->identifier_quote_char . ' = ?'))) { |
| 721 | if ($pdo_show_sql_errors) |
| 722 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
| 723 | else |
| 724 | $this->error = _("Could not prepare query"); |
| 725 | $this->failQuery(); |
| 726 | } |
| 727 | if (!($res = $sth->execute(array($user)))) { |
| 728 | if ($pdo_show_sql_errors) |
| 729 | $this->error = implode(' - ', $sth->errorInfo()); |
| 730 | else |
| 731 | $this->error = _("Could not execute query"); |
| 732 | $this->failQuery(); |
| 733 | } |
| 734 | |
| 735 | while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { |
| 736 | $prefs_cache[$row['prefkey']] = $row['prefval']; |
| 737 | } |
| 738 | } else { |
| 739 | $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ". |
| 740 | "WHERE %s = '%s'", |
| 741 | $this->key_field, |
| 742 | $this->val_field, |
| 743 | $this->table, |
| 744 | $this->user_field, |
| 745 | $this->dbh->quoteString($user)); |
| 746 | $res = $this->dbh->query($query); |
| 747 | if (DB::isError($res)) { |
| 748 | $this->failQuery($res); |
| 749 | } |
| 750 | |
| 751 | while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { |
| 752 | $prefs_cache[$row['prefkey']] = $row['prefval']; |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | } /* end class dbPrefs */ |
| 758 | |
| 759 | |
| 760 | /** |
| 761 | * Returns the value for the requested preference |
| 762 | * @ignore |
| 763 | */ |
| 764 | function getPref($data_dir, $username, $pref_name, $default = '') { |
| 765 | $db = new dbPrefs; |
| 766 | if(isset($db->error)) { |
| 767 | printf( _("Preference database error (%s). Exiting abnormally"), |
| 768 | $db->error); |
| 769 | exit; |
| 770 | } |
| 771 | |
| 772 | return $db->getKey($username, $pref_name, $default); |
| 773 | } |
| 774 | |
| 775 | /** |
| 776 | * Remove the desired preference setting ($pref_name) |
| 777 | * @ignore |
| 778 | */ |
| 779 | function removePref($data_dir, $username, $pref_name) { |
| 780 | global $prefs_cache; |
| 781 | $db = new dbPrefs; |
| 782 | if(isset($db->error)) { |
| 783 | $db->failQuery(); |
| 784 | } |
| 785 | |
| 786 | $db->deleteKey($username, $pref_name); |
| 787 | |
| 788 | if (isset($prefs_cache[$pref_name])) { |
| 789 | unset($prefs_cache[$pref_name]); |
| 790 | } |
| 791 | |
| 792 | sqsession_register($prefs_cache , 'prefs_cache'); |
| 793 | return; |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * Sets the desired preference setting ($pref_name) to whatever is in $value |
| 798 | * @ignore |
| 799 | */ |
| 800 | function setPref($data_dir, $username, $pref_name, $value) { |
| 801 | global $prefs_cache; |
| 802 | |
| 803 | if (isset($prefs_cache[$pref_name]) && ($prefs_cache[$pref_name] == $value)) { |
| 804 | return; |
| 805 | } |
| 806 | |
| 807 | if ($value === '') { |
| 808 | removePref($data_dir, $username, $pref_name); |
| 809 | return; |
| 810 | } |
| 811 | |
| 812 | $db = new dbPrefs; |
| 813 | if(isset($db->error)) { |
| 814 | $db->failQuery(); |
| 815 | } |
| 816 | |
| 817 | $db->setKey($username, $pref_name, $value); |
| 818 | $prefs_cache[$pref_name] = $value; |
| 819 | assert_options(ASSERT_ACTIVE, 1); |
| 820 | assert_options(ASSERT_BAIL, 1); |
| 821 | assert ('$value == $prefs_cache[$pref_name]'); |
| 822 | sqsession_register($prefs_cache , 'prefs_cache'); |
| 823 | return; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * This checks if the prefs are available |
| 828 | * @ignore |
| 829 | */ |
| 830 | function checkForPrefs($data_dir, $username) { |
| 831 | $db = new dbPrefs; |
| 832 | if(isset($db->error)) { |
| 833 | $db->failQuery(); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * Writes the Signature |
| 839 | * @ignore |
| 840 | */ |
| 841 | function setSig($data_dir, $username, $number, $value) { |
| 842 | if ($number == "g") { |
| 843 | $key = '___signature___'; |
| 844 | } else { |
| 845 | $key = sprintf('___sig%s___', $number); |
| 846 | } |
| 847 | setPref($data_dir, $username, $key, $value); |
| 848 | return; |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * Gets the signature |
| 853 | * @ignore |
| 854 | */ |
| 855 | function getSig($data_dir, $username, $number) { |
| 856 | if ($number == "g") { |
| 857 | $key = '___signature___'; |
| 858 | } else { |
| 859 | $key = sprintf('___sig%d___', $number); |
| 860 | } |
| 861 | return getPref($data_dir, $username, $key); |
| 862 | } |