| 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-2012 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 the default preferences array. |
| 169 | * |
| 170 | */ |
| 171 | function dbPrefs() { |
| 172 | // Try and read the default preferences file. |
| 173 | $default_pref = SM_PATH . 'config/default_pref'; |
| 174 | if (@file_exists($default_pref)) { |
| 175 | if ($file = @fopen($default_pref, 'r')) { |
| 176 | while (!feof($file)) { |
| 177 | $pref = fgets($file, 1024); |
| 178 | $i = strpos($pref, '='); |
| 179 | if ($i > 0) { |
| 180 | $this->default[trim(substr($pref, 0, $i))] = trim(substr($pref, $i + 1)); |
| 181 | } |
| 182 | } |
| 183 | fclose($file); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * initialize DB connection object |
| 190 | * |
| 191 | * @return boolean true, if object is initialized |
| 192 | * |
| 193 | */ |
| 194 | function open() { |
| 195 | global $prefs_dsn, $prefs_table; |
| 196 | global $prefs_user_field, $prefs_key_field, $prefs_val_field; |
| 197 | global $prefs_user_size, $prefs_key_size, $prefs_val_size; |
| 198 | |
| 199 | /* test if Pear DB class is available and freak out if it is not */ |
| 200 | if (! class_exists('DB')) { |
| 201 | // same error also in abook_database.php |
| 202 | $this->error = _("Could not include PEAR database functions required for the database backend.") . "\n"; |
| 203 | $this->error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"), |
| 204 | 'DB.php') . "\n"; |
| 205 | $this->error .= _("Please contact your system administrator and report this error."); |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | if(isset($this->dbh)) { |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | if (preg_match('/^mysql/', $prefs_dsn)) { |
| 214 | $this->db_type = SMDB_MYSQL; |
| 215 | } elseif (preg_match('/^pgsql/', $prefs_dsn)) { |
| 216 | $this->db_type = SMDB_PGSQL; |
| 217 | } |
| 218 | |
| 219 | if (!empty($prefs_table)) { |
| 220 | $this->table = $prefs_table; |
| 221 | } |
| 222 | if (!empty($prefs_user_field)) { |
| 223 | $this->user_field = $prefs_user_field; |
| 224 | } |
| 225 | |
| 226 | // the default user field is "user", which in PostgreSQL |
| 227 | // is an identifier and causes errors if not escaped |
| 228 | // |
| 229 | if ($this->db_type == SMDB_PGSQL) { |
| 230 | $this->user_field = '"' . $this->user_field . '"'; |
| 231 | } |
| 232 | |
| 233 | if (!empty($prefs_key_field)) { |
| 234 | $this->key_field = $prefs_key_field; |
| 235 | } |
| 236 | if (!empty($prefs_val_field)) { |
| 237 | $this->val_field = $prefs_val_field; |
| 238 | } |
| 239 | if (!empty($prefs_user_size)) { |
| 240 | $this->user_size = (int) $prefs_user_size; |
| 241 | } |
| 242 | if (!empty($prefs_key_size)) { |
| 243 | $this->key_size = (int) $prefs_key_size; |
| 244 | } |
| 245 | if (!empty($prefs_val_size)) { |
| 246 | $this->val_size = (int) $prefs_val_size; |
| 247 | } |
| 248 | $dbh = DB::connect($prefs_dsn, true); |
| 249 | |
| 250 | if(DB::isError($dbh)) { |
| 251 | $this->error = DB::errorMessage($dbh); |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | $this->dbh = $dbh; |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Function used to handle database connection errors |
| 261 | * |
| 262 | * @param object PEAR Error object |
| 263 | * |
| 264 | */ |
| 265 | function failQuery($res = NULL) { |
| 266 | if($res == NULL) { |
| 267 | printf(_("Preference database error (%s). Exiting abnormally"), |
| 268 | $this->error); |
| 269 | } else { |
| 270 | printf(_("Preference database error (%s). Exiting abnormally"), |
| 271 | DB::errorMessage($res)); |
| 272 | } |
| 273 | exit; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Get user's prefs setting |
| 278 | * |
| 279 | * @param string $user user name |
| 280 | * @param string $key preference name |
| 281 | * @param mixed $default (since 1.2.5) default value |
| 282 | * |
| 283 | * @return mixed preference value |
| 284 | * |
| 285 | */ |
| 286 | function getKey($user, $key, $default = '') { |
| 287 | global $prefs_cache; |
| 288 | |
| 289 | $temp = array(&$user, &$key); |
| 290 | $result = do_hook('get_pref_override', $temp); |
| 291 | if (is_null($result)) { |
| 292 | cachePrefValues($user); |
| 293 | |
| 294 | if (isset($prefs_cache[$key])) { |
| 295 | $result = $prefs_cache[$key]; |
| 296 | } else { |
| 297 | //FIXME: is there a justification for having two prefs hooks so close? who uses them? |
| 298 | $temp = array(&$user, &$key); |
| 299 | $result = do_hook('get_pref', $temp); |
| 300 | if (is_null($result)) { |
| 301 | if (isset($this->default[$key])) { |
| 302 | $result = $this->default[$key]; |
| 303 | } else { |
| 304 | $result = $default; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | return $result; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Delete user's prefs setting |
| 314 | * |
| 315 | * @param string $user user name |
| 316 | * @param string $key preference name |
| 317 | * |
| 318 | * @return boolean |
| 319 | * |
| 320 | */ |
| 321 | function deleteKey($user, $key) { |
| 322 | global $prefs_cache; |
| 323 | |
| 324 | if (!$this->open()) { |
| 325 | return false; |
| 326 | } |
| 327 | $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", |
| 328 | $this->table, |
| 329 | $this->user_field, |
| 330 | $this->dbh->quoteString($user), |
| 331 | $this->key_field, |
| 332 | $this->dbh->quoteString($key)); |
| 333 | |
| 334 | $res = $this->dbh->simpleQuery($query); |
| 335 | if(DB::isError($res)) { |
| 336 | $this->failQuery($res); |
| 337 | } |
| 338 | |
| 339 | unset($prefs_cache[$key]); |
| 340 | |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Set user's preference |
| 346 | * |
| 347 | * @param string $user user name |
| 348 | * @param string $key preference name |
| 349 | * @param mixed $value preference value |
| 350 | * |
| 351 | * @return boolean |
| 352 | * |
| 353 | */ |
| 354 | function setKey($user, $key, $value) { |
| 355 | if (!$this->open()) { |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Check if username fits into db field |
| 361 | */ |
| 362 | if (strlen($user) > $this->user_size) { |
| 363 | $this->error = "Oversized username value." |
| 364 | ." Your preferences can't be saved." |
| 365 | ." See the administrator's manual or contact your system administrator."; |
| 366 | |
| 367 | /** |
| 368 | * Debugging function. Can be used to log all issues that trigger |
| 369 | * oversized field errors. Function should be enabled in all three |
| 370 | * strlen checks. See http://www.php.net/error-log |
| 371 | */ |
| 372 | // error_log($user.'|'.$key.'|'.$value."\n",3,'/tmp/oversized_log'); |
| 373 | |
| 374 | // error is fatal |
| 375 | $this->failQuery(null); |
| 376 | } |
| 377 | /** |
| 378 | * Check if preference key fits into db field |
| 379 | */ |
| 380 | if (strlen($key) > $this->key_size) { |
| 381 | $err_msg = "Oversized user's preference key." |
| 382 | ." Some preferences were not saved." |
| 383 | ." See the administrator's manual or contact your system administrator."; |
| 384 | // error is not fatal. Only some preference is not saved. |
| 385 | trigger_error($err_msg,E_USER_WARNING); |
| 386 | return false; |
| 387 | } |
| 388 | /** |
| 389 | * Check if preference value fits into db field |
| 390 | */ |
| 391 | if (strlen($value) > $this->val_size) { |
| 392 | $err_msg = "Oversized user's preference value." |
| 393 | ." Some preferences were not saved." |
| 394 | ." See the administrator's manual or contact your system administrator."; |
| 395 | // error is not fatal. Only some preference is not saved. |
| 396 | trigger_error($err_msg,E_USER_WARNING); |
| 397 | return false; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | if ($this->db_type == SMDB_MYSQL) { |
| 402 | $query = sprintf("REPLACE INTO %s (%s, %s, %s) ". |
| 403 | "VALUES('%s','%s','%s')", |
| 404 | $this->table, |
| 405 | $this->user_field, |
| 406 | $this->key_field, |
| 407 | $this->val_field, |
| 408 | $this->dbh->quoteString($user), |
| 409 | $this->dbh->quoteString($key), |
| 410 | $this->dbh->quoteString($value)); |
| 411 | |
| 412 | $res = $this->dbh->simpleQuery($query); |
| 413 | if(DB::isError($res)) { |
| 414 | $this->failQuery($res); |
| 415 | } |
| 416 | } elseif ($this->db_type == SMDB_PGSQL) { |
| 417 | $this->dbh->simpleQuery("BEGIN TRANSACTION"); |
| 418 | $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", |
| 419 | $this->table, |
| 420 | $this->user_field, |
| 421 | $this->dbh->quoteString($user), |
| 422 | $this->key_field, |
| 423 | $this->dbh->quoteString($key)); |
| 424 | $res = $this->dbh->simpleQuery($query); |
| 425 | if (DB::isError($res)) { |
| 426 | $this->dbh->simpleQuery("ROLLBACK TRANSACTION"); |
| 427 | $this->failQuery($res); |
| 428 | } |
| 429 | $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')", |
| 430 | $this->table, |
| 431 | $this->user_field, |
| 432 | $this->key_field, |
| 433 | $this->val_field, |
| 434 | $this->dbh->quoteString($user), |
| 435 | $this->dbh->quoteString($key), |
| 436 | $this->dbh->quoteString($value)); |
| 437 | $res = $this->dbh->simpleQuery($query); |
| 438 | if (DB::isError($res)) { |
| 439 | $this->dbh->simpleQuery("ROLLBACK TRANSACTION"); |
| 440 | $this->failQuery($res); |
| 441 | } |
| 442 | $this->dbh->simpleQuery("COMMIT TRANSACTION"); |
| 443 | } else { |
| 444 | $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", |
| 445 | $this->table, |
| 446 | $this->user_field, |
| 447 | $this->dbh->quoteString($user), |
| 448 | $this->key_field, |
| 449 | $this->dbh->quoteString($key)); |
| 450 | $res = $this->dbh->simpleQuery($query); |
| 451 | if (DB::isError($res)) { |
| 452 | $this->failQuery($res); |
| 453 | } |
| 454 | $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')", |
| 455 | $this->table, |
| 456 | $this->user_field, |
| 457 | $this->key_field, |
| 458 | $this->val_field, |
| 459 | $this->dbh->quoteString($user), |
| 460 | $this->dbh->quoteString($key), |
| 461 | $this->dbh->quoteString($value)); |
| 462 | $res = $this->dbh->simpleQuery($query); |
| 463 | if (DB::isError($res)) { |
| 464 | $this->failQuery($res); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | return true; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Fill preference cache array |
| 473 | * |
| 474 | * @param string $user user name |
| 475 | * |
| 476 | * @since 1.2.3 |
| 477 | * |
| 478 | */ |
| 479 | function fillPrefsCache($user) { |
| 480 | global $prefs_cache; |
| 481 | |
| 482 | if (!$this->open()) { |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | $prefs_cache = array(); |
| 487 | $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ". |
| 488 | "WHERE %s = '%s'", |
| 489 | $this->key_field, |
| 490 | $this->val_field, |
| 491 | $this->table, |
| 492 | $this->user_field, |
| 493 | $this->dbh->quoteString($user)); |
| 494 | $res = $this->dbh->query($query); |
| 495 | if (DB::isError($res)) { |
| 496 | $this->failQuery($res); |
| 497 | } |
| 498 | |
| 499 | while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { |
| 500 | $prefs_cache[$row['prefkey']] = $row['prefval']; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | } /* end class dbPrefs */ |
| 505 | |
| 506 | |
| 507 | /** |
| 508 | * Returns the value for the requested preference |
| 509 | * @ignore |
| 510 | */ |
| 511 | function getPref($data_dir, $username, $pref_name, $default = '') { |
| 512 | $db = new dbPrefs; |
| 513 | if(isset($db->error)) { |
| 514 | printf( _("Preference database error (%s). Exiting abnormally"), |
| 515 | $db->error); |
| 516 | exit; |
| 517 | } |
| 518 | |
| 519 | return $db->getKey($username, $pref_name, $default); |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Remove the desired preference setting ($pref_name) |
| 524 | * @ignore |
| 525 | */ |
| 526 | function removePref($data_dir, $username, $pref_name) { |
| 527 | global $prefs_cache; |
| 528 | $db = new dbPrefs; |
| 529 | if(isset($db->error)) { |
| 530 | $db->failQuery(); |
| 531 | } |
| 532 | |
| 533 | $db->deleteKey($username, $pref_name); |
| 534 | |
| 535 | if (isset($prefs_cache[$pref_name])) { |
| 536 | unset($prefs_cache[$pref_name]); |
| 537 | } |
| 538 | |
| 539 | sqsession_register($prefs_cache , 'prefs_cache'); |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Sets the desired preference setting ($pref_name) to whatever is in $value |
| 545 | * @ignore |
| 546 | */ |
| 547 | function setPref($data_dir, $username, $pref_name, $value) { |
| 548 | global $prefs_cache; |
| 549 | |
| 550 | if (isset($prefs_cache[$pref_name]) && ($prefs_cache[$pref_name] == $value)) { |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | if ($value === '') { |
| 555 | removePref($data_dir, $username, $pref_name); |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | $db = new dbPrefs; |
| 560 | if(isset($db->error)) { |
| 561 | $db->failQuery(); |
| 562 | } |
| 563 | |
| 564 | $db->setKey($username, $pref_name, $value); |
| 565 | $prefs_cache[$pref_name] = $value; |
| 566 | assert_options(ASSERT_ACTIVE, 1); |
| 567 | assert_options(ASSERT_BAIL, 1); |
| 568 | assert ('$value == $prefs_cache[$pref_name]'); |
| 569 | sqsession_register($prefs_cache , 'prefs_cache'); |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * This checks if the prefs are available |
| 575 | * @ignore |
| 576 | */ |
| 577 | function checkForPrefs($data_dir, $username) { |
| 578 | $db = new dbPrefs; |
| 579 | if(isset($db->error)) { |
| 580 | $db->failQuery(); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Writes the Signature |
| 586 | * @ignore |
| 587 | */ |
| 588 | function setSig($data_dir, $username, $number, $value) { |
| 589 | if ($number == "g") { |
| 590 | $key = '___signature___'; |
| 591 | } else { |
| 592 | $key = sprintf('___sig%s___', $number); |
| 593 | } |
| 594 | setPref($data_dir, $username, $key, $value); |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Gets the signature |
| 600 | * @ignore |
| 601 | */ |
| 602 | function getSig($data_dir, $username, $number) { |
| 603 | if ($number == "g") { |
| 604 | $key = '___signature___'; |
| 605 | } else { |
| 606 | $key = sprintf('___sig%d___', $number); |
| 607 | } |
| 608 | return getPref($data_dir, $username, $key); |
| 609 | } |