| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * db_prefs.php |
| 5 | * |
| 6 | * This contains functions for manipulating user preferences |
| 7 | * stored in a database, accessed though the Pear DB layer. |
| 8 | * |
| 9 | * Database: |
| 10 | * |
| 11 | * The preferences table should have three columns: |
| 12 | * user char \ primary |
| 13 | * prefkey char / key |
| 14 | * prefval blob |
| 15 | * |
| 16 | * CREATE TABLE userprefs (user CHAR(128) NOT NULL DEFAULT '', |
| 17 | * prefkey CHAR(64) NOT NULL DEFAULT '', |
| 18 | * prefval BLOB NOT NULL DEFAULT '', |
| 19 | * primary key (user,prefkey)); |
| 20 | * |
| 21 | * Configuration of databasename, username and password is done |
| 22 | * by using conf.pl or the administrator plugin |
| 23 | * |
| 24 | * @copyright © 1999-2007 The SquirrelMail Project Team |
| 25 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| 26 | * @version $Id$ |
| 27 | * @package squirrelmail |
| 28 | * @subpackage prefs |
| 29 | * @since 1.1.3 |
| 30 | */ |
| 31 | |
| 32 | /** @ignore */ |
| 33 | if (!defined('SM_PATH')) define('SM_PATH','../'); |
| 34 | |
| 35 | /** Unknown database */ |
| 36 | define('SMDB_UNKNOWN', 0); |
| 37 | /** MySQL */ |
| 38 | define('SMDB_MYSQL', 1); |
| 39 | /** PostgreSQL */ |
| 40 | define('SMDB_PGSQL', 2); |
| 41 | |
| 42 | /** |
| 43 | * don't display errors (no code execution in functions/*.php). |
| 44 | * will handle error in dbPrefs class. |
| 45 | */ |
| 46 | @include_once('DB.php'); |
| 47 | |
| 48 | global $prefs_are_cached, $prefs_cache; |
| 49 | |
| 50 | /** |
| 51 | * @ignore |
| 52 | */ |
| 53 | function cachePrefValues($username) { |
| 54 | global $prefs_are_cached, $prefs_cache; |
| 55 | |
| 56 | sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION ); |
| 57 | if ($prefs_are_cached) { |
| 58 | sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION ); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | sqsession_unregister('prefs_cache'); |
| 63 | sqsession_unregister('prefs_are_cached'); |
| 64 | |
| 65 | $db = new dbPrefs; |
| 66 | if(isset($db->error)) { |
| 67 | printf( _("Preference database error (%s). Exiting abnormally"), |
| 68 | $db->error); |
| 69 | exit; |
| 70 | } |
| 71 | |
| 72 | $db->fillPrefsCache($username); |
| 73 | if (isset($db->error)) { |
| 74 | printf( _("Preference database error (%s). Exiting abnormally"), |
| 75 | $db->error); |
| 76 | exit; |
| 77 | } |
| 78 | |
| 79 | $prefs_are_cached = true; |
| 80 | |
| 81 | sqsession_register($prefs_cache, 'prefs_cache'); |
| 82 | sqsession_register($prefs_are_cached, 'prefs_are_cached'); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Class used to handle connections to prefs database and operations with preferences |
| 87 | * |
| 88 | * @package squirrelmail |
| 89 | * @subpackage prefs |
| 90 | * @since 1.1.3 |
| 91 | * |
| 92 | */ |
| 93 | class dbPrefs { |
| 94 | /** |
| 95 | * Table used to store preferences |
| 96 | * @var string |
| 97 | */ |
| 98 | var $table = 'userprefs'; |
| 99 | |
| 100 | /** |
| 101 | * Field used to store owner of preference |
| 102 | * @var string |
| 103 | */ |
| 104 | var $user_field = 'user'; |
| 105 | |
| 106 | /** |
| 107 | * Field used to store preference name |
| 108 | * @var string |
| 109 | */ |
| 110 | var $key_field = 'prefkey'; |
| 111 | |
| 112 | /** |
| 113 | * Field used to store preference value |
| 114 | * @var string |
| 115 | */ |
| 116 | var $val_field = 'prefval'; |
| 117 | |
| 118 | /** |
| 119 | * Database connection object |
| 120 | * @var object |
| 121 | */ |
| 122 | var $dbh = NULL; |
| 123 | |
| 124 | /** |
| 125 | * Error messages |
| 126 | * @var string |
| 127 | */ |
| 128 | var $error = NULL; |
| 129 | |
| 130 | /** |
| 131 | * Database type (SMDB_* constants) |
| 132 | * Is used in setKey(). |
| 133 | * @var integer |
| 134 | */ |
| 135 | var $db_type = SMDB_UNKNOWN; |
| 136 | |
| 137 | /** |
| 138 | * Default preferences |
| 139 | * @var array |
| 140 | */ |
| 141 | var $default = Array('theme_default' => 0, |
| 142 | 'show_html_default' => '0'); |
| 143 | |
| 144 | /** |
| 145 | * Preference owner field size |
| 146 | * @var integer |
| 147 | * @since 1.5.1 |
| 148 | */ |
| 149 | var $user_size = 128; |
| 150 | |
| 151 | /** |
| 152 | * Preference key field size |
| 153 | * @var integer |
| 154 | * @since 1.5.1 |
| 155 | */ |
| 156 | var $key_size = 64; |
| 157 | |
| 158 | /** |
| 159 | * Preference value field size |
| 160 | * @var integer |
| 161 | * @since 1.5.1 |
| 162 | */ |
| 163 | var $val_size = 65536; |
| 164 | |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * initialize DB connection object |
| 169 | * |
| 170 | * @return boolean true, if object is initialized |
| 171 | * |
| 172 | */ |
| 173 | function open() { |
| 174 | global $prefs_dsn, $prefs_table; |
| 175 | global $prefs_user_field, $prefs_key_field, $prefs_val_field; |
| 176 | global $prefs_user_size, $prefs_key_size, $prefs_val_size; |
| 177 | |
| 178 | /* test if Pear DB class is available and freak out if it is not */ |
| 179 | if (! class_exists('DB')) { |
| 180 | // same error also in abook_database.php |
| 181 | $this->error = _("Could not include PEAR database functions required for the database backend.") . "\n"; |
| 182 | $this->error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"), |
| 183 | 'DB.php') . "\n"; |
| 184 | $this->error .= _("Please contact your system administrator and report this error."); |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | if(isset($this->dbh)) { |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | if (preg_match('/^mysql/', $prefs_dsn)) { |
| 193 | $this->db_type = SMDB_MYSQL; |
| 194 | } elseif (preg_match('/^pgsql/', $prefs_dsn)) { |
| 195 | $this->db_type = SMDB_PGSQL; |
| 196 | } |
| 197 | |
| 198 | if (!empty($prefs_table)) { |
| 199 | $this->table = $prefs_table; |
| 200 | } |
| 201 | if (!empty($prefs_user_field)) { |
| 202 | $this->user_field = $prefs_user_field; |
| 203 | } |
| 204 | if (!empty($prefs_key_field)) { |
| 205 | $this->key_field = $prefs_key_field; |
| 206 | } |
| 207 | if (!empty($prefs_val_field)) { |
| 208 | $this->val_field = $prefs_val_field; |
| 209 | } |
| 210 | if (!empty($prefs_user_size)) { |
| 211 | $this->user_size = (int) $prefs_user_size; |
| 212 | } |
| 213 | if (!empty($prefs_key_size)) { |
| 214 | $this->key_size = (int) $prefs_key_size; |
| 215 | } |
| 216 | if (!empty($prefs_val_size)) { |
| 217 | $this->val_size = (int) $prefs_val_size; |
| 218 | } |
| 219 | $dbh = DB::connect($prefs_dsn, true); |
| 220 | |
| 221 | if(DB::isError($dbh)) { |
| 222 | $this->error = DB::errorMessage($dbh); |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | $this->dbh = $dbh; |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Function used to handle database connection errors |
| 232 | * |
| 233 | * @param object PEAR Error object |
| 234 | * |
| 235 | */ |
| 236 | function failQuery($res = NULL) { |
| 237 | if($res == NULL) { |
| 238 | printf(_("Preference database error (%s). Exiting abnormally"), |
| 239 | $this->error); |
| 240 | } else { |
| 241 | printf(_("Preference database error (%s). Exiting abnormally"), |
| 242 | DB::errorMessage($res)); |
| 243 | } |
| 244 | exit; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Get user's prefs setting |
| 249 | * |
| 250 | * @param string $user user name |
| 251 | * @param string $key preference name |
| 252 | * @param mixed $default (since 1.2.5) default value |
| 253 | * |
| 254 | * @return mixed preference value |
| 255 | * |
| 256 | */ |
| 257 | function getKey($user, $key, $default = '') { |
| 258 | global $prefs_cache; |
| 259 | |
| 260 | cachePrefValues($user); |
| 261 | |
| 262 | if (isset($prefs_cache[$key])) { |
| 263 | return $prefs_cache[$key]; |
| 264 | } else { |
| 265 | if (isset($this->default[$key])) { |
| 266 | return $this->default[$key]; |
| 267 | } else { |
| 268 | return $default; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Delete user's prefs setting |
| 275 | * |
| 276 | * @param string $user user name |
| 277 | * @param string $key preference name |
| 278 | * |
| 279 | * @return boolean |
| 280 | * |
| 281 | */ |
| 282 | function deleteKey($user, $key) { |
| 283 | global $prefs_cache; |
| 284 | |
| 285 | if (!$this->open()) { |
| 286 | return false; |
| 287 | } |
| 288 | $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", |
| 289 | $this->table, |
| 290 | $this->user_field, |
| 291 | $this->dbh->quoteString($user), |
| 292 | $this->key_field, |
| 293 | $this->dbh->quoteString($key)); |
| 294 | |
| 295 | $res = $this->dbh->simpleQuery($query); |
| 296 | if(DB::isError($res)) { |
| 297 | $this->failQuery($res); |
| 298 | } |
| 299 | |
| 300 | unset($prefs_cache[$key]); |
| 301 | |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Set user's preference |
| 307 | * |
| 308 | * @param string $user user name |
| 309 | * @param string $key preference name |
| 310 | * @param mixed $value preference value |
| 311 | * |
| 312 | * @return boolean |
| 313 | * |
| 314 | */ |
| 315 | function setKey($user, $key, $value) { |
| 316 | if (!$this->open()) { |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Check if username fits into db field |
| 322 | */ |
| 323 | if (strlen($user) > $this->user_size) { |
| 324 | $this->error = "Oversized username value." |
| 325 | ." Your preferences can't be saved." |
| 326 | ." See the administrator's manual or contact your system administrator."; |
| 327 | |
| 328 | /** |
| 329 | * Debugging function. Can be used to log all issues that trigger |
| 330 | * oversized field errors. Function should be enabled in all three |
| 331 | * strlen checks. See http://www.php.net/error-log |
| 332 | */ |
| 333 | // error_log($user.'|'.$key.'|'.$value."\n",3,'/tmp/oversized_log'); |
| 334 | |
| 335 | // error is fatal |
| 336 | $this->failQuery(null); |
| 337 | } |
| 338 | /** |
| 339 | * Check if preference key fits into db field |
| 340 | */ |
| 341 | if (strlen($key) > $this->key_size) { |
| 342 | $err_msg = "Oversized user's preference key." |
| 343 | ." Some preferences were not saved." |
| 344 | ." See the administrator's manual or contact your system administrator."; |
| 345 | // error is not fatal. Only some preference is not saved. |
| 346 | trigger_error($err_msg,E_USER_WARNING); |
| 347 | return false; |
| 348 | } |
| 349 | /** |
| 350 | * Check if preference value fits into db field |
| 351 | */ |
| 352 | if (strlen($value) > $this->val_size) { |
| 353 | $err_msg = "Oversized user's preference value." |
| 354 | ." Some preferences were not saved." |
| 355 | ." See the administrator's manual or contact your system administrator."; |
| 356 | // error is not fatal. Only some preference is not saved. |
| 357 | trigger_error($err_msg,E_USER_WARNING); |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | |
| 362 | if ($this->db_type == SMDB_MYSQL) { |
| 363 | $query = sprintf("REPLACE INTO %s (%s, %s, %s) ". |
| 364 | "VALUES('%s','%s','%s')", |
| 365 | $this->table, |
| 366 | $this->user_field, |
| 367 | $this->key_field, |
| 368 | $this->val_field, |
| 369 | $this->dbh->quoteString($user), |
| 370 | $this->dbh->quoteString($key), |
| 371 | $this->dbh->quoteString($value)); |
| 372 | |
| 373 | $res = $this->dbh->simpleQuery($query); |
| 374 | if(DB::isError($res)) { |
| 375 | $this->failQuery($res); |
| 376 | } |
| 377 | } elseif ($this->db_type == SMDB_PGSQL) { |
| 378 | $this->dbh->simpleQuery("BEGIN TRANSACTION"); |
| 379 | $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", |
| 380 | $this->table, |
| 381 | $this->user_field, |
| 382 | $this->dbh->quoteString($user), |
| 383 | $this->key_field, |
| 384 | $this->dbh->quoteString($key)); |
| 385 | $res = $this->dbh->simpleQuery($query); |
| 386 | if (DB::isError($res)) { |
| 387 | $this->dbh->simpleQuery("ROLLBACK TRANSACTION"); |
| 388 | $this->failQuery($res); |
| 389 | } |
| 390 | $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')", |
| 391 | $this->table, |
| 392 | $this->user_field, |
| 393 | $this->key_field, |
| 394 | $this->val_field, |
| 395 | $this->dbh->quoteString($user), |
| 396 | $this->dbh->quoteString($key), |
| 397 | $this->dbh->quoteString($value)); |
| 398 | $res = $this->dbh->simpleQuery($query); |
| 399 | if (DB::isError($res)) { |
| 400 | $this->dbh->simpleQuery("ROLLBACK TRANSACTION"); |
| 401 | $this->failQuery($res); |
| 402 | } |
| 403 | $this->dbh->simpleQuery("COMMIT TRANSACTION"); |
| 404 | } else { |
| 405 | $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", |
| 406 | $this->table, |
| 407 | $this->user_field, |
| 408 | $this->dbh->quoteString($user), |
| 409 | $this->key_field, |
| 410 | $this->dbh->quoteString($key)); |
| 411 | $res = $this->dbh->simpleQuery($query); |
| 412 | if (DB::isError($res)) { |
| 413 | $this->failQuery($res); |
| 414 | } |
| 415 | $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')", |
| 416 | $this->table, |
| 417 | $this->user_field, |
| 418 | $this->key_field, |
| 419 | $this->val_field, |
| 420 | $this->dbh->quoteString($user), |
| 421 | $this->dbh->quoteString($key), |
| 422 | $this->dbh->quoteString($value)); |
| 423 | $res = $this->dbh->simpleQuery($query); |
| 424 | if (DB::isError($res)) { |
| 425 | $this->failQuery($res); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | return true; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Fill preference cache array |
| 434 | * |
| 435 | * @param string $user user name |
| 436 | * |
| 437 | * @since 1.2.3 |
| 438 | * |
| 439 | */ |
| 440 | function fillPrefsCache($user) { |
| 441 | global $prefs_cache; |
| 442 | |
| 443 | if (!$this->open()) { |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | $prefs_cache = array(); |
| 448 | $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ". |
| 449 | "WHERE %s = '%s'", |
| 450 | $this->key_field, |
| 451 | $this->val_field, |
| 452 | $this->table, |
| 453 | $this->user_field, |
| 454 | $this->dbh->quoteString($user)); |
| 455 | $res = $this->dbh->query($query); |
| 456 | if (DB::isError($res)) { |
| 457 | $this->failQuery($res); |
| 458 | } |
| 459 | |
| 460 | while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { |
| 461 | $prefs_cache[$row['prefkey']] = $row['prefval']; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | } /* end class dbPrefs */ |
| 466 | |
| 467 | |
| 468 | /** |
| 469 | * Returns the value for the requested preference |
| 470 | * @ignore |
| 471 | */ |
| 472 | function getPref($data_dir, $username, $pref_name, $default = '') { |
| 473 | $db = new dbPrefs; |
| 474 | if(isset($db->error)) { |
| 475 | printf( _("Preference database error (%s). Exiting abnormally"), |
| 476 | $db->error); |
| 477 | exit; |
| 478 | } |
| 479 | |
| 480 | return $db->getKey($username, $pref_name, $default); |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Remove the desired preference setting ($pref_name) |
| 485 | * @ignore |
| 486 | */ |
| 487 | function removePref($data_dir, $username, $pref_name) { |
| 488 | global $prefs_cache; |
| 489 | $db = new dbPrefs; |
| 490 | if(isset($db->error)) { |
| 491 | $db->failQuery(); |
| 492 | } |
| 493 | |
| 494 | $db->deleteKey($username, $pref_name); |
| 495 | |
| 496 | if (isset($prefs_cache[$pref_name])) { |
| 497 | unset($prefs_cache[$pref_name]); |
| 498 | } |
| 499 | |
| 500 | sqsession_register($prefs_cache , 'prefs_cache'); |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Sets the desired preference setting ($pref_name) to whatever is in $value |
| 506 | * @ignore |
| 507 | */ |
| 508 | function setPref($data_dir, $username, $pref_name, $value) { |
| 509 | global $prefs_cache; |
| 510 | |
| 511 | if (isset($prefs_cache[$pref_name]) && ($prefs_cache[$pref_name] == $value)) { |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | if ($value === '') { |
| 516 | removePref($data_dir, $username, $pref_name); |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | $db = new dbPrefs; |
| 521 | if(isset($db->error)) { |
| 522 | $db->failQuery(); |
| 523 | } |
| 524 | |
| 525 | $db->setKey($username, $pref_name, $value); |
| 526 | $prefs_cache[$pref_name] = $value; |
| 527 | assert_options(ASSERT_ACTIVE, 1); |
| 528 | assert_options(ASSERT_BAIL, 1); |
| 529 | assert ('$value == $prefs_cache[$pref_name]'); |
| 530 | sqsession_register($prefs_cache , 'prefs_cache'); |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * This checks if the prefs are available |
| 536 | * @ignore |
| 537 | */ |
| 538 | function checkForPrefs($data_dir, $username) { |
| 539 | $db = new dbPrefs; |
| 540 | if(isset($db->error)) { |
| 541 | $db->failQuery(); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Writes the Signature |
| 547 | * @ignore |
| 548 | */ |
| 549 | function setSig($data_dir, $username, $number, $value) { |
| 550 | if ($number == "g") { |
| 551 | $key = '___signature___'; |
| 552 | } else { |
| 553 | $key = sprintf('___sig%s___', $number); |
| 554 | } |
| 555 | setPref($data_dir, $username, $key, $value); |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Gets the signature |
| 561 | * @ignore |
| 562 | */ |
| 563 | function getSig($data_dir, $username, $number) { |
| 564 | if ($number == "g") { |
| 565 | $key = '___signature___'; |
| 566 | } else { |
| 567 | $key = sprintf('___sig%d___', $number); |
| 568 | } |
| 569 | return getPref($data_dir, $username, $key); |
| 570 | } |