Add information about online documentation
[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 *
1977ab55 24 * @copyright 1999-2010 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
971c7b1a 281 $temp = array(&$user, &$key);
282 $result = do_hook('get_pref_override', $temp);
283 if (is_null($result)) {
284 cachePrefValues($user);
2d367c68 285
971c7b1a 286 if (isset($prefs_cache[$key])) {
287 $result = $prefs_cache[$key];
62337234 288 } else {
971c7b1a 289//FIXME: is there a justification for having two prefs hooks so close? who uses them?
290 $temp = array(&$user, &$key);
291 $result = do_hook('get_pref', $temp);
292 if (is_null($result)) {
293 if (isset($this->default[$key])) {
294 $result = $this->default[$key];
295 } else {
296 $result = $default;
297 }
298 }
62337234 299 }
2d367c68 300 }
971c7b1a 301 return $result;
370059dd 302 }
2d367c68 303
ace4c62c 304 /**
305 * Delete user's prefs setting
37b11ab0 306 *
202bcbcc 307 * @param string $user user name
37b11ab0 308 * @param string $key preference name
309 *
ace4c62c 310 * @return boolean
37b11ab0 311 *
ace4c62c 312 */
370059dd 313 function deleteKey($user, $key) {
314 global $prefs_cache;
82474746 315
b279d7f4 316 if (!$this->open()) {
317 return false;
318 }
99a6c222 319 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
370059dd 320 $this->table,
99a6c222 321 $this->user_field,
370059dd 322 $this->dbh->quoteString($user),
99a6c222 323 $this->key_field,
370059dd 324 $this->dbh->quoteString($key));
82474746 325
2d367c68 326 $res = $this->dbh->simpleQuery($query);
370059dd 327 if(DB::isError($res)) {
2d367c68 328 $this->failQuery($res);
370059dd 329 }
330
331 unset($prefs_cache[$key]);
82474746 332
2d367c68 333 return true;
370059dd 334 }
82474746 335
ace4c62c 336 /**
337 * Set user's preference
37b11ab0 338 *
339 * @param string $user user name
340 * @param string $key preference name
341 * @param mixed $value preference value
342 *
ace4c62c 343 * @return boolean
37b11ab0 344 *
ace4c62c 345 */
370059dd 346 function setKey($user, $key, $value) {
b279d7f4 347 if (!$this->open()) {
348 return false;
349 }
06316c07 350
351 /**
352 * Check if username fits into db field
353 */
354 if (strlen($user) > $this->user_size) {
355 $this->error = "Oversized username value."
5e07597f 356 ." Your preferences can't be saved."
6f4c512c 357 ." See the administrator's manual or contact your system administrator.";
06316c07 358
359 /**
202bcbcc 360 * Debugging function. Can be used to log all issues that trigger
361 * oversized field errors. Function should be enabled in all three
06316c07 362 * strlen checks. See http://www.php.net/error-log
363 */
364 // error_log($user.'|'.$key.'|'.$value."\n",3,'/tmp/oversized_log');
365
366 // error is fatal
367 $this->failQuery(null);
368 }
369 /**
370 * Check if preference key fits into db field
371 */
372 if (strlen($key) > $this->key_size) {
373 $err_msg = "Oversized user's preference key."
5e07597f 374 ." Some preferences were not saved."
6f4c512c 375 ." See the administrator's manual or contact your system administrator.";
06316c07 376 // error is not fatal. Only some preference is not saved.
377 trigger_error($err_msg,E_USER_WARNING);
378 return false;
379 }
380 /**
381 * Check if preference value fits into db field
382 */
383 if (strlen($value) > $this->val_size) {
384 $err_msg = "Oversized user's preference value."
5e07597f 385 ." Some preferences were not saved."
6f4c512c 386 ." See the administrator's manual or contact your system administrator.";
06316c07 387 // error is not fatal. Only some preference is not saved.
388 trigger_error($err_msg,E_USER_WARNING);
389 return false;
390 }
391
392
98749983 393 if ($this->db_type == SMDB_MYSQL) {
394 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
395 "VALUES('%s','%s','%s')",
396 $this->table,
397 $this->user_field,
398 $this->key_field,
399 $this->val_field,
400 $this->dbh->quoteString($user),
401 $this->dbh->quoteString($key),
402 $this->dbh->quoteString($value));
403
404 $res = $this->dbh->simpleQuery($query);
405 if(DB::isError($res)) {
406 $this->failQuery($res);
407 }
408 } elseif ($this->db_type == SMDB_PGSQL) {
409 $this->dbh->simpleQuery("BEGIN TRANSACTION");
410 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
411 $this->table,
412 $this->user_field,
413 $this->dbh->quoteString($user),
414 $this->key_field,
415 $this->dbh->quoteString($key));
416 $res = $this->dbh->simpleQuery($query);
417 if (DB::isError($res)) {
418 $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
419 $this->failQuery($res);
420 }
421 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
422 $this->table,
423 $this->user_field,
424 $this->key_field,
425 $this->val_field,
426 $this->dbh->quoteString($user),
427 $this->dbh->quoteString($key),
428 $this->dbh->quoteString($value));
429 $res = $this->dbh->simpleQuery($query);
430 if (DB::isError($res)) {
431 $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
432 $this->failQuery($res);
433 }
434 $this->dbh->simpleQuery("COMMIT TRANSACTION");
435 } else {
436 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
437 $this->table,
438 $this->user_field,
439 $this->dbh->quoteString($user),
440 $this->key_field,
441 $this->dbh->quoteString($key));
442 $res = $this->dbh->simpleQuery($query);
443 if (DB::isError($res)) {
444 $this->failQuery($res);
445 }
446 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
447 $this->table,
448 $this->user_field,
449 $this->key_field,
450 $this->val_field,
451 $this->dbh->quoteString($user),
452 $this->dbh->quoteString($key),
453 $this->dbh->quoteString($value));
454 $res = $this->dbh->simpleQuery($query);
455 if (DB::isError($res)) {
456 $this->failQuery($res);
457 }
370059dd 458 }
2d367c68 459
460 return true;
370059dd 461 }
82474746 462
ace4c62c 463 /**
464 * Fill preference cache array
37b11ab0 465 *
ace4c62c 466 * @param string $user user name
37b11ab0 467 *
ace4c62c 468 * @since 1.2.3
37b11ab0 469 *
ace4c62c 470 */
370059dd 471 function fillPrefsCache($user) {
472 global $prefs_cache;
2d367c68 473
b279d7f4 474 if (!$this->open()) {
475 return;
476 }
370059dd 477
478 $prefs_cache = array();
99a6c222 479 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
480 "WHERE %s = '%s'",
481 $this->key_field,
482 $this->val_field,
370059dd 483 $this->table,
99a6c222 484 $this->user_field,
370059dd 485 $this->dbh->quoteString($user));
486 $res = $this->dbh->query($query);
487 if (DB::isError($res)) {
488 $this->failQuery($res);
489 }
490
491 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
492 $prefs_cache[$row['prefkey']] = $row['prefval'];
493 }
494 }
495
370059dd 496} /* end class dbPrefs */
82474746 497
498
4d30c1b7 499/**
37b11ab0 500 * Returns the value for the requested preference
4d30c1b7 501 * @ignore
502 */
37b11ab0 503function getPref($data_dir, $username, $pref_name, $default = '') {
370059dd 504 $db = new dbPrefs;
505 if(isset($db->error)) {
2d367c68 506 printf( _("Preference database error (%s). Exiting abnormally"),
370059dd 507 $db->error);
2d367c68 508 exit;
370059dd 509 }
510
37b11ab0 511 return $db->getKey($username, $pref_name, $default);
370059dd 512}
513
4d30c1b7 514/**
37b11ab0 515 * Remove the desired preference setting ($pref_name)
4d30c1b7 516 * @ignore
517 */
37b11ab0 518function removePref($data_dir, $username, $pref_name) {
1fa62ab9 519 global $prefs_cache;
370059dd 520 $db = new dbPrefs;
521 if(isset($db->error)) {
522 $db->failQuery();
523 }
524
37b11ab0 525 $db->deleteKey($username, $pref_name);
88a99543 526
37b11ab0 527 if (isset($prefs_cache[$pref_name])) {
528 unset($prefs_cache[$pref_name]);
88a99543 529 }
530
531 sqsession_register($prefs_cache , 'prefs_cache');
370059dd 532 return;
533}
534
4d30c1b7 535/**
37b11ab0 536 * Sets the desired preference setting ($pref_name) to whatever is in $value
4d30c1b7 537 * @ignore
538 */
37b11ab0 539function setPref($data_dir, $username, $pref_name, $value) {
370059dd 540 global $prefs_cache;
541
37b11ab0 542 if (isset($prefs_cache[$pref_name]) && ($prefs_cache[$pref_name] == $value)) {
1fa62ab9 543 return;
370059dd 544 }
545
37b11ab0 546 if ($value === '') {
547 removePref($data_dir, $username, $pref_name);
370059dd 548 return;
549 }
550
551 $db = new dbPrefs;
552 if(isset($db->error)) {
553 $db->failQuery();
554 }
555
37b11ab0 556 $db->setKey($username, $pref_name, $value);
557 $prefs_cache[$pref_name] = $value;
370059dd 558 assert_options(ASSERT_ACTIVE, 1);
559 assert_options(ASSERT_BAIL, 1);
37b11ab0 560 assert ('$value == $prefs_cache[$pref_name]');
88a99543 561 sqsession_register($prefs_cache , 'prefs_cache');
370059dd 562 return;
563}
564
4d30c1b7 565/**
566 * This checks if the prefs are available
567 * @ignore
568 */
370059dd 569function checkForPrefs($data_dir, $username) {
570 $db = new dbPrefs;
571 if(isset($db->error)) {
572 $db->failQuery();
573 }
574}
575
4d30c1b7 576/**
577 * Writes the Signature
578 * @ignore
579 */
37b11ab0 580function setSig($data_dir, $username, $number, $value) {
16e5635d 581 if ($number == "g") {
582 $key = '___signature___';
583 } else {
584 $key = sprintf('___sig%s___', $number);
585 }
37b11ab0 586 setPref($data_dir, $username, $key, $value);
370059dd 587 return;
588}
589
4d30c1b7 590/**
591 * Gets the signature
592 * @ignore
593 */
16e5635d 594function getSig($data_dir, $username, $number) {
16e5635d 595 if ($number == "g") {
596 $key = '___signature___';
597 } else {
598 $key = sprintf('___sig%d___', $number);
599 }
57f1d1c1 600 return getPref($data_dir, $username, $key);
370059dd 601}