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