Updating templates.
[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 /**
ace4c62c 168 * initialize DB connection object
37b11ab0 169 *
ace4c62c 170 * @return boolean true, if object is initialized
37b11ab0 171 *
ace4c62c 172 */
370059dd 173 function open() {
3499f99f 174 global $prefs_dsn, $prefs_table;
98749983 175 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
06316c07 176 global $prefs_user_size, $prefs_key_size, $prefs_val_size;
3499f99f 177
099fea11 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
ae13f72f 181 $this->error = _("Could not include PEAR database functions required for the database backend.") . "\n";
099fea11 182 $this->error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
ae13f72f 183 'DB.php') . "\n";
099fea11 184 $this->error .= _("Please contact your system administrator and report this error.");
185 return false;
186 }
187
370059dd 188 if(isset($this->dbh)) {
189 return true;
190 }
3499f99f 191
98749983 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
3499f99f 198 if (!empty($prefs_table)) {
199 $this->table = $prefs_table;
200 }
99a6c222 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 }
06316c07 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 }
70561170 219 $dbh = DB::connect($prefs_dsn, true);
2d367c68 220
286fe80b 221 if(DB::isError($dbh)) {
2d367c68 222 $this->error = DB::errorMessage($dbh);
223 return false;
224 }
225
226 $this->dbh = $dbh;
227 return true;
370059dd 228 }
82474746 229
ace4c62c 230 /**
231 * Function used to handle database connection errors
37b11ab0 232 *
202bcbcc 233 * @param object PEAR Error object
37b11ab0 234 *
ace4c62c 235 */
370059dd 236 function failQuery($res = NULL) {
2d367c68 237 if($res == NULL) {
238 printf(_("Preference database error (%s). Exiting abnormally"),
370059dd 239 $this->error);
2d367c68 240 } else {
241 printf(_("Preference database error (%s). Exiting abnormally"),
370059dd 242 DB::errorMessage($res));
2d367c68 243 }
244 exit;
370059dd 245 }
82474746 246
ace4c62c 247 /**
248 * Get user's prefs setting
37b11ab0 249 *
ace4c62c 250 * @param string $user user name
251 * @param string $key preference name
252 * @param mixed $default (since 1.2.5) default value
37b11ab0 253 *
ace4c62c 254 * @return mixed preference value
37b11ab0 255 *
ace4c62c 256 */
370059dd 257 function getKey($user, $key, $default = '') {
258 global $prefs_cache;
2d367c68 259
370059dd 260 cachePrefValues($user);
2d367c68 261
370059dd 262 if (isset($prefs_cache[$key])) {
263 return $prefs_cache[$key];
2d367c68 264 } else {
62337234 265 if (isset($this->default[$key])) {
266 return $this->default[$key];
267 } else {
268 return $default;
269 }
2d367c68 270 }
370059dd 271 }
2d367c68 272
ace4c62c 273 /**
274 * Delete user's prefs setting
37b11ab0 275 *
202bcbcc 276 * @param string $user user name
37b11ab0 277 * @param string $key preference name
278 *
ace4c62c 279 * @return boolean
37b11ab0 280 *
ace4c62c 281 */
370059dd 282 function deleteKey($user, $key) {
283 global $prefs_cache;
82474746 284
b279d7f4 285 if (!$this->open()) {
286 return false;
287 }
99a6c222 288 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
370059dd 289 $this->table,
99a6c222 290 $this->user_field,
370059dd 291 $this->dbh->quoteString($user),
99a6c222 292 $this->key_field,
370059dd 293 $this->dbh->quoteString($key));
82474746 294
2d367c68 295 $res = $this->dbh->simpleQuery($query);
370059dd 296 if(DB::isError($res)) {
2d367c68 297 $this->failQuery($res);
370059dd 298 }
299
300 unset($prefs_cache[$key]);
82474746 301
2d367c68 302 return true;
370059dd 303 }
82474746 304
ace4c62c 305 /**
306 * Set user's preference
37b11ab0 307 *
308 * @param string $user user name
309 * @param string $key preference name
310 * @param mixed $value preference value
311 *
ace4c62c 312 * @return boolean
37b11ab0 313 *
ace4c62c 314 */
370059dd 315 function setKey($user, $key, $value) {
b279d7f4 316 if (!$this->open()) {
317 return false;
318 }
06316c07 319
320 /**
321 * Check if username fits into db field
322 */
323 if (strlen($user) > $this->user_size) {
324 $this->error = "Oversized username value."
5e07597f 325 ." Your preferences can't be saved."
6f4c512c 326 ." See the administrator's manual or contact your system administrator.";
06316c07 327
328 /**
202bcbcc 329 * Debugging function. Can be used to log all issues that trigger
330 * oversized field errors. Function should be enabled in all three
06316c07 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."
5e07597f 343 ." Some preferences were not saved."
6f4c512c 344 ." See the administrator's manual or contact your system administrator.";
06316c07 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."
5e07597f 354 ." Some preferences were not saved."
6f4c512c 355 ." See the administrator's manual or contact your system administrator.";
06316c07 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
98749983 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 }
370059dd 427 }
2d367c68 428
429 return true;
370059dd 430 }
82474746 431
ace4c62c 432 /**
433 * Fill preference cache array
37b11ab0 434 *
ace4c62c 435 * @param string $user user name
37b11ab0 436 *
ace4c62c 437 * @since 1.2.3
37b11ab0 438 *
ace4c62c 439 */
370059dd 440 function fillPrefsCache($user) {
441 global $prefs_cache;
2d367c68 442
b279d7f4 443 if (!$this->open()) {
444 return;
445 }
370059dd 446
447 $prefs_cache = array();
99a6c222 448 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
449 "WHERE %s = '%s'",
450 $this->key_field,
451 $this->val_field,
370059dd 452 $this->table,
99a6c222 453 $this->user_field,
370059dd 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
370059dd 465} /* end class dbPrefs */
82474746 466
467
4d30c1b7 468/**
37b11ab0 469 * Returns the value for the requested preference
4d30c1b7 470 * @ignore
471 */
37b11ab0 472function getPref($data_dir, $username, $pref_name, $default = '') {
370059dd 473 $db = new dbPrefs;
474 if(isset($db->error)) {
2d367c68 475 printf( _("Preference database error (%s). Exiting abnormally"),
370059dd 476 $db->error);
2d367c68 477 exit;
370059dd 478 }
479
37b11ab0 480 return $db->getKey($username, $pref_name, $default);
370059dd 481}
482
4d30c1b7 483/**
37b11ab0 484 * Remove the desired preference setting ($pref_name)
4d30c1b7 485 * @ignore
486 */
37b11ab0 487function removePref($data_dir, $username, $pref_name) {
1fa62ab9 488 global $prefs_cache;
370059dd 489 $db = new dbPrefs;
490 if(isset($db->error)) {
491 $db->failQuery();
492 }
493
37b11ab0 494 $db->deleteKey($username, $pref_name);
88a99543 495
37b11ab0 496 if (isset($prefs_cache[$pref_name])) {
497 unset($prefs_cache[$pref_name]);
88a99543 498 }
499
500 sqsession_register($prefs_cache , 'prefs_cache');
370059dd 501 return;
502}
503
4d30c1b7 504/**
37b11ab0 505 * Sets the desired preference setting ($pref_name) to whatever is in $value
4d30c1b7 506 * @ignore
507 */
37b11ab0 508function setPref($data_dir, $username, $pref_name, $value) {
370059dd 509 global $prefs_cache;
510
37b11ab0 511 if (isset($prefs_cache[$pref_name]) && ($prefs_cache[$pref_name] == $value)) {
1fa62ab9 512 return;
370059dd 513 }
514
37b11ab0 515 if ($value === '') {
516 removePref($data_dir, $username, $pref_name);
370059dd 517 return;
518 }
519
520 $db = new dbPrefs;
521 if(isset($db->error)) {
522 $db->failQuery();
523 }
524
37b11ab0 525 $db->setKey($username, $pref_name, $value);
526 $prefs_cache[$pref_name] = $value;
370059dd 527 assert_options(ASSERT_ACTIVE, 1);
528 assert_options(ASSERT_BAIL, 1);
37b11ab0 529 assert ('$value == $prefs_cache[$pref_name]');
88a99543 530 sqsession_register($prefs_cache , 'prefs_cache');
370059dd 531 return;
532}
533
4d30c1b7 534/**
535 * This checks if the prefs are available
536 * @ignore
537 */
370059dd 538function checkForPrefs($data_dir, $username) {
539 $db = new dbPrefs;
540 if(isset($db->error)) {
541 $db->failQuery();
542 }
543}
544
4d30c1b7 545/**
546 * Writes the Signature
547 * @ignore
548 */
37b11ab0 549function setSig($data_dir, $username, $number, $value) {
16e5635d 550 if ($number == "g") {
551 $key = '___signature___';
552 } else {
553 $key = sprintf('___sig%s___', $number);
554 }
37b11ab0 555 setPref($data_dir, $username, $key, $value);
370059dd 556 return;
557}
558
4d30c1b7 559/**
560 * Gets the signature
561 * @ignore
562 */
16e5635d 563function getSig($data_dir, $username, $number) {
16e5635d 564 if ($number == "g") {
565 $key = '___signature___';
566 } else {
567 $key = sprintf('___sig%d___', $number);
568 }
57f1d1c1 569 return getPref($data_dir, $username, $key);
370059dd 570}