6 * This contains functions for manipulating user preferences
7 * stored in a database, accessed through the Pear DB layer
8 * or PDO, the latter taking precedence if available.
12 * The preferences table should have three columns:
17 * CREATE TABLE userprefs (user CHAR(128) NOT NULL DEFAULT '',
18 * prefkey CHAR(64) NOT NULL DEFAULT '',
19 * prefval BLOB NOT NULL DEFAULT '',
20 * primary key (user,prefkey));
22 * Configuration of databasename, username and password is done
23 * by using conf.pl or the administrator plugin
25 * Three settings that control PDO behavior can be specified in
26 * config/config_local.php if needed:
27 * boolean $disable_pdo SquirrelMail uses PDO by default to access the
28 * user preferences and address book databases, but
29 * setting this to TRUE will cause SquirrelMail to
30 * fall back to using Pear DB instead.
31 * boolean $pdo_show_sql_errors When database errors are encountered,
32 * setting this to TRUE causes the actual
33 * database error to be displayed, otherwise
34 * generic errors are displayed, preventing
35 * internal database information from being
36 * exposed. This should be enabled only for
38 * string $db_identifier_quote_char By default, SquirrelMail will quote
39 * table and field names in database
40 * queries with what it thinks is the
41 * appropriate quote character for the
42 * database type being used (backtick
43 * for MySQL (and thus MariaDB), double
44 * quotes for all others), but you can
45 * override the character used by
46 * putting it here, or tell SquirrelMail
47 * NOT to quote identifiers by setting
50 * If needed, you can also set $prefs_db_charset as a string
51 * (such as "utf8mb4") in config/config_local.php if your system
52 * does not default the SQL connection character set as expected
53 * (most sensible systems will do the right thing transparently).
55 * @copyright 1999-2024 The SquirrelMail Project Team
56 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
58 * @package squirrelmail
64 if (!defined('SM_PATH')) define('SM_PATH','../');
66 /** Unknown database */
67 define('SMDB_UNKNOWN', 0);
69 define('SMDB_MYSQL', 1);
71 define('SMDB_PGSQL', 2);
74 * Needs either PDO or the DB functions
75 * Don't display errors here. (no code execution in functions/*.php).
76 * will handle error in dbPrefs class.
78 global $use_pdo, $disable_pdo;
79 if (empty($disable_pdo) && class_exists('PDO'))
85 @include_once
('DB.php');
87 global $prefs_are_cached, $prefs_cache;
92 function cachePrefValues($username) {
93 global $prefs_are_cached, $prefs_cache;
95 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION
);
96 if ($prefs_are_cached) {
97 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION
);
101 sqsession_unregister('prefs_cache');
102 sqsession_unregister('prefs_are_cached');
105 if(isset($db->error
)) {
106 printf( _("Preference database error (%s). Exiting abnormally"),
111 $db->fillPrefsCache($username);
112 if (isset($db->error
)) {
113 printf( _("Preference database error (%s). Exiting abnormally"),
118 $prefs_are_cached = true;
120 sqsession_register($prefs_cache, 'prefs_cache');
121 sqsession_register($prefs_are_cached, 'prefs_are_cached');
125 * Class used to handle connections to prefs database and operations with preferences
127 * @package squirrelmail
134 * Table used to store preferences
137 var $table = 'userprefs';
140 * Field used to store owner of preference
143 var $user_field = 'user';
146 * Field used to store preference name
149 var $key_field = 'prefkey';
152 * Field used to store preference value
155 var $val_field = 'prefval';
158 * Database connection object
170 * Database type (SMDB_* constants)
171 * Is used in setKey().
174 var $db_type = SMDB_UNKNOWN
;
177 * Character used to quote database table
181 var $identifier_quote_char = '';
184 * Default preferences
187 var $default = Array('theme_default' => 0,
188 'include_self_reply_all' => '0',
189 'do_not_reply_to_self' => '1',
190 'show_html_default' => '0');
193 * Preference owner field size
197 var $user_size = 128;
200 * Preference key field size
207 * Preference value field size
211 var $val_size = 65536;
216 * Constructor (PHP5 style, required in some future version of PHP)
217 * initialize the default preferences array.
220 function __construct() {
221 // Try and read the default preferences file.
222 $default_pref = SM_PATH
. 'config/default_pref';
223 if (@file_exists
($default_pref)) {
224 if ($file = @fopen
($default_pref, 'r')) {
225 while (!feof($file)) {
226 $pref = fgets($file, 1024);
227 $i = strpos($pref, '=');
229 $this->default[trim(substr($pref, 0, $i))] = trim(substr($pref, $i +
1));
238 * Constructor (PHP4 style, kept for compatibility reasons)
239 * initialize the default preferences array.
247 * initialize DB connection object
249 * @return boolean true, if object is initialized
253 global $prefs_dsn, $prefs_db_charset, $prefs_table, $use_pdo, $db_identifier_quote_char;
254 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
255 global $prefs_user_size, $prefs_key_size, $prefs_val_size;
257 /* test if PDO or Pear DB classes are available and freak out if necessary */
258 if (!$use_pdo && !class_exists('DB')) {
259 // same error also in abook_database.php
260 $error = _("Could not find or include PHP PDO or PEAR database functions required for the database backend.") . "\n";
261 $error .= sprintf(_("PDO should come preinstalled with PHP version 5.1 or higher. Otherwise, is PEAR installed, and is the include path set correctly to find %s?"), 'DB.php') . "\n";
262 $error .= _("Please contact your system administrator and report this error.");
266 if(isset($this->dbh
)) {
270 if (strpos($prefs_dsn, 'mysql') === 0) {
271 $this->db_type
= SMDB_MYSQL
;
272 } else if (strpos($prefs_dsn, 'pgsql') === 0) {
273 $this->db_type
= SMDB_PGSQL
;
276 // figure out identifier quoting
277 if (empty($db_identifier_quote_char)) {
278 if ($this->db_type
== SMDB_MYSQL
)
279 $this->identifier_quote_char
= '`';
281 $this->identifier_quote_char
= '"';
282 } else if ($db_identifier_quote_char === 'none')
283 $this->identifier_quote_char
= '';
285 $this->identifier_quote_char
= $db_identifier_quote_char;
287 if (!empty($prefs_table)) {
288 $this->table
= $prefs_table;
290 if (!empty($prefs_user_field)) {
291 $this->user_field
= $prefs_user_field;
293 if (!empty($prefs_key_field)) {
294 $this->key_field
= $prefs_key_field;
296 if (!empty($prefs_val_field)) {
297 $this->val_field
= $prefs_val_field;
299 if (!empty($prefs_user_size)) {
300 $this->user_size
= (int) $prefs_user_size;
302 if (!empty($prefs_key_size)) {
303 $this->key_size
= (int) $prefs_key_size;
305 if (!empty($prefs_val_size)) {
306 $this->val_size
= (int) $prefs_val_size;
309 // connect, create database connection object
312 // parse and convert DSN to PDO style
313 // Pear's full DSN syntax is one of the following:
314 // phptype(dbsyntax)://username:password@protocol+hostspec/database?option=value
315 // phptype(syntax)://user:pass@protocol(proto_opts)/database
317 // $matches will contain:
321 // 4: hostname (and possible port number) OR protocol (and possible protocol options)
322 // 5: database name (and possible options)
323 // 6: port number (moved from match number 4)
324 // 7: options (moved from match number 5)
325 // 8: protocol (instead of hostname)
326 // 9: protocol options (moved from match number 4/8)
327 //TODO: do we care about supporting cases where no password is given? (this is a legal DSN, but causes an error below)
328 if (!preg_match('|^(.+)://(.+):(.+)@(.+)/(.+)$|i', $prefs_dsn, $matches)) {
329 $this->error
= _("Could not parse prefs DSN");
336 if (preg_match('|^(.+):(\d+)$|', $matches[4], $host_port_matches)) {
337 $matches[4] = $host_port_matches[1];
338 $matches[6] = $host_port_matches[2];
340 if (preg_match('|^(.+?)\((.+)\)$|', $matches[4], $protocol_matches)) {
341 $matches[8] = $protocol_matches[1];
342 $matches[9] = $protocol_matches[2];
346 //TODO: currently we just ignore options specified on the end of the DSN
347 if (preg_match('|^(.+?)\?(.+)$|', $matches[5], $database_name_options_matches)) {
348 $matches[5] = $database_name_options_matches[1];
349 $matches[7] = $database_name_options_matches[2];
351 if ($matches[8] === 'unix' && !empty($matches[9]))
352 $pdo_prefs_dsn = $matches[1] . ':unix_socket=' . $matches[9] . ';dbname=' . $matches[5];
354 $pdo_prefs_dsn = $matches[1] . ':host=' . $matches[4] . (!empty($matches[6]) ?
';port=' . $matches[6] : '') . ';dbname=' . $matches[5];
355 if (!empty($prefs_db_charset))
356 $pdo_prefs_dsn .= ';charset=' . $prefs_db_charset;
358 $dbh = new PDO($pdo_prefs_dsn, $matches[2], $matches[3]);
359 } catch (Exception
$e) {
360 $this->error
= $e->getMessage();
364 $dbh = DB
::connect($prefs_dsn, true);
366 if(DB
::isError($dbh)) {
367 $this->error
= DB
::errorMessage($dbh);
374 // Older versions of PHP are buggy with setting charset on the dsn so we also issue a SET NAMES
375 if (!empty($prefs_db_charset)) {
377 $res = $dbh->exec('SET NAMES \'' . $prefs_db_charset . '\'');
378 /* Purposefully not checking for errors; some setups reportedly botch this on queries like this
379 if ($res === FALSE) {
380 if ($pdo_show_sql_errors)
381 $this->error = implode(' - ', $sth->errorInfo());
383 $this->error = _("Could not execute query");
389 $res = $this->dbh
->simpleQuery('SET NAMES \'' . $prefs_db_charset . '\'');
390 /* Purposefully not checking for errors; some setups reportedly botch this on queries like this
391 if(DB::isError($res)) {
392 $this->failQuery($res);
402 * Function used to handle database connection errors
404 * @param object PEAR Error object
407 function failQuery($res = NULL) {
410 printf(_("Preference database error (%s). Exiting abnormally"),
413 printf(_("Preference database error (%s). Exiting abnormally"),
414 ($use_pdo ?
implode(' - ', $res->errorInfo()) : DB
::errorMessage($res)));
420 * Get user's prefs setting
422 * @param string $user user name
423 * @param string $key preference name
424 * @param mixed $default (since 1.2.5) default value
426 * @return mixed preference value
429 function getKey($user, $key, $default = '') {
432 $temp = array(&$user, &$key);
433 $result = do_hook('get_pref_override', $temp);
434 if (is_null($result)) {
435 cachePrefValues($user);
437 if (isset($prefs_cache[$key])) {
438 $result = $prefs_cache[$key];
440 //FIXME: is there a justification for having two prefs hooks so close? who uses them?
441 $temp = array(&$user, &$key);
442 $result = do_hook('get_pref', $temp);
443 if (is_null($result)) {
444 if (isset($this->default[$key])) {
445 $result = $this->default[$key];
456 * Delete user's prefs setting
458 * @param string $user user name
459 * @param string $key preference name
464 function deleteKey($user, $key) {
465 global $prefs_cache, $use_pdo, $pdo_show_sql_errors;
467 if (!$this->open()) {
471 if (!($sth = $this->dbh
->prepare('DELETE FROM ' . $this->identifier_quote_char
. $this->table
. $this->identifier_quote_char
. ' WHERE ' . $this->identifier_quote_char
. $this->user_field
. $this->identifier_quote_char
. ' = ? AND ' . $this->identifier_quote_char
. $this->key_field
. $this->identifier_quote_char
. ' = ?'))) {
472 if ($pdo_show_sql_errors)
473 $this->error
= implode(' - ', $this->dbh
->errorInfo());
475 $this->error
= _("Could not prepare query");
478 if (!($res = $sth->execute(array($user, $key)))) {
479 if ($pdo_show_sql_errors)
480 $this->error
= implode(' - ', $sth->errorInfo());
482 $this->error
= _("Could not execute query");
486 $query = sprintf("DELETE FROM %s%s%s WHERE %s%s%s='%s' AND %s%s%s='%s'",
487 $this->identifier_quote_char
,
489 $this->identifier_quote_char
,
490 $this->identifier_quote_char
,
492 $this->identifier_quote_char
,
493 $this->dbh
->quoteString($user),
494 $this->identifier_quote_char
,
496 $this->identifier_quote_char
,
497 $this->dbh
->quoteString($key));
499 $res = $this->dbh
->simpleQuery($query);
500 if(DB
::isError($res)) {
501 $this->failQuery($res);
505 unset($prefs_cache[$key]);
511 * Set user's preference
513 * @param string $user user name
514 * @param string $key preference name
515 * @param mixed $value preference value
520 function setKey($user, $key, $value) {
521 global $use_pdo, $pdo_show_sql_errors;
522 if (!$this->open()) {
527 * Check if username fits into db field
529 if (strlen($user) > $this->user_size
) {
530 $this->error
= "Oversized username value."
531 ." Your preferences can't be saved."
532 ." See the administrator's manual or contact your system administrator.";
535 * Debugging function. Can be used to log all issues that trigger
536 * oversized field errors. Function should be enabled in all three
537 * strlen checks. See http://www.php.net/error-log
539 // error_log($user.'|'.$key.'|'.$value."\n",3,'/tmp/oversized_log');
542 $this->failQuery(null);
545 * Check if preference key fits into db field
547 if (strlen($key) > $this->key_size
) {
548 $err_msg = "Oversized user's preference key."
549 ." Some preferences were not saved."
550 ." See the administrator's manual or contact your system administrator.";
551 // error is not fatal. Only some preference is not saved.
552 trigger_error($err_msg,E_USER_WARNING
);
556 * Check if preference value fits into db field
558 if (strlen($value) > $this->val_size
) {
559 $err_msg = "Oversized user's preference value."
560 ." Some preferences were not saved."
561 ." See the administrator's manual or contact your system administrator.";
562 // error is not fatal. Only some preference is not saved.
563 trigger_error($err_msg,E_USER_WARNING
);
568 if ($this->db_type
== SMDB_MYSQL
) {
570 if (!($sth = $this->dbh
->prepare('REPLACE INTO ' . $this->identifier_quote_char
. $this->table
. $this->identifier_quote_char
. ' (' . $this->identifier_quote_char
. $this->user_field
. $this->identifier_quote_char
. ', ' . $this->identifier_quote_char
. $this->key_field
. $this->identifier_quote_char
. ', ' . $this->identifier_quote_char
. $this->val_field
. $this->identifier_quote_char
. ') VALUES (?, ?, ?)'))) {
571 if ($pdo_show_sql_errors)
572 $this->error
= implode(' - ', $this->dbh
->errorInfo());
574 $this->error
= _("Could not prepare query");
577 if (!($res = $sth->execute(array($user, $key, $value)))) {
578 if ($pdo_show_sql_errors)
579 $this->error
= implode(' - ', $sth->errorInfo());
581 $this->error
= _("Could not execute query");
585 $query = sprintf("REPLACE INTO %s%s%s (%s%s%s, %s%s%s, %s%s%s) ".
586 "VALUES('%s','%s','%s')",
587 $this->identifier_quote_char
,
589 $this->identifier_quote_char
,
590 $this->identifier_quote_char
,
592 $this->identifier_quote_char
,
593 $this->identifier_quote_char
,
595 $this->identifier_quote_char
,
596 $this->identifier_quote_char
,
598 $this->identifier_quote_char
,
599 $this->dbh
->quoteString($user),
600 $this->dbh
->quoteString($key),
601 $this->dbh
->quoteString($value));
603 $res = $this->dbh
->simpleQuery($query);
604 if(DB
::isError($res)) {
605 $this->failQuery($res);
608 } elseif ($this->db_type
== SMDB_PGSQL
) {
610 if ($this->dbh
->exec('BEGIN TRANSACTION') === FALSE) {
611 if ($pdo_show_sql_errors)
612 $this->error
= implode(' - ', $this->dbh
->errorInfo());
614 $this->error
= _("Could not execute query");
617 if (!($sth = $this->dbh
->prepare('DELETE FROM ' . $this->identifier_quote_char
. $this->table
. $this->identifier_quote_char
. ' WHERE ' . $this->identifier_quote_char
. $this->user_field
. $this->identifier_quote_char
. ' = ? AND ' . $this->identifier_quote_char
. $this->key_field
. $this->identifier_quote_char
. ' = ?'))) {
618 if ($pdo_show_sql_errors)
619 $this->error
= implode(' - ', $this->dbh
->errorInfo());
621 $this->error
= _("Could not prepare query");
624 if (!($res = $sth->execute(array($user, $key)))) {
625 if ($pdo_show_sql_errors)
626 $this->error
= implode(' - ', $sth->errorInfo());
628 $this->error
= _("Could not execute query");
629 $this->dbh
->exec('ROLLBACK TRANSACTION');
632 if (!($sth = $this->dbh
->prepare('INSERT INTO ' . $this->identifier_quote_char
. $this->table
. $this->identifier_quote_char
. ' (' . $this->identifier_quote_char
. $this->user_field
. $this->identifier_quote_char
. ', ' . $this->identifier_quote_char
. $this->key_field
. $this->identifier_quote_char
. ', ' . $this->identifier_quote_char
. $this->val_field
. $this->identifier_quote_char
. ') VALUES (?, ?, ?)'))) {
633 if ($pdo_show_sql_errors)
634 $this->error
= implode(' - ', $this->dbh
->errorInfo());
636 $this->error
= _("Could not prepare query");
639 if (!($res = $sth->execute(array($user, $key, $value)))) {
640 if ($pdo_show_sql_errors)
641 $this->error
= implode(' - ', $sth->errorInfo());
643 $this->error
= _("Could not execute query");
644 $this->dbh
->exec('ROLLBACK TRANSACTION');
647 if ($this->dbh
->exec('COMMIT TRANSACTION') === FALSE) {
648 if ($pdo_show_sql_errors)
649 $this->error
= implode(' - ', $this->dbh
->errorInfo());
651 $this->error
= _("Could not execute query");
655 $this->dbh
->simpleQuery("BEGIN TRANSACTION");
656 $query = sprintf("DELETE FROM %s%s%s WHERE %s%s%s='%s' AND %s%s%s='%s'",
657 $this->identifier_quote_char
,
659 $this->identifier_quote_char
,
660 $this->identifier_quote_char
,
662 $this->identifier_quote_char
,
663 $this->dbh
->quoteString($user),
664 $this->identifier_quote_char
,
666 $this->identifier_quote_char
,
667 $this->dbh
->quoteString($key));
668 $res = $this->dbh
->simpleQuery($query);
669 if (DB
::isError($res)) {
670 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
671 $this->failQuery($res);
673 $query = sprintf("INSERT INTO %s%s%s (%s%s%s, %s%s%s, %s%s%s) VALUES ('%s', '%s', '%s')",
674 $this->identifier_quote_char
,
676 $this->identifier_quote_char
,
677 $this->identifier_quote_char
,
679 $this->identifier_quote_char
,
680 $this->identifier_quote_char
,
682 $this->identifier_quote_char
,
683 $this->identifier_quote_char
,
685 $this->identifier_quote_char
,
686 $this->dbh
->quoteString($user),
687 $this->dbh
->quoteString($key),
688 $this->dbh
->quoteString($value));
689 $res = $this->dbh
->simpleQuery($query);
690 if (DB
::isError($res)) {
691 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
692 $this->failQuery($res);
694 $this->dbh
->simpleQuery("COMMIT TRANSACTION");
698 if (!($sth = $this->dbh
->prepare('DELETE FROM ' . $this->identifier_quote_char
. $this->table
. $this->identifier_quote_char
. ' WHERE ' . $this->identifier_quote_char
. $this->user_field
. $this->identifier_quote_char
. ' = ? AND ' . $this->identifier_quote_char
. $this->key_field
. $this->identifier_quote_char
. ' = ?'))) {
699 if ($pdo_show_sql_errors)
700 $this->error
= implode(' - ', $this->dbh
->errorInfo());
702 $this->error
= _("Could not prepare query");
705 if (!($res = $sth->execute(array($user, $key)))) {
706 if ($pdo_show_sql_errors)
707 $this->error
= implode(' - ', $sth->errorInfo());
709 $this->error
= _("Could not execute query");
712 if (!($sth = $this->dbh
->prepare('INSERT INTO ' . $this->identifier_quote_char
. $this->table
. $this->identifier_quote_char
. ' (' . $this->identifier_quote_char
. $this->user_field
. $this->identifier_quote_char
. ', ' . $this->identifier_quote_char
. $this->key_field
. $this->identifier_quote_char
. ', ' . $this->identifier_quote_char
. $this->val_field
. $this->identifier_quote_char
. ') VALUES (?, ?, ?)'))) {
713 if ($pdo_show_sql_errors)
714 $this->error
= implode(' - ', $this->dbh
->errorInfo());
716 $this->error
= _("Could not prepare query");
719 if (!($res = $sth->execute(array($user, $key, $value)))) {
720 if ($pdo_show_sql_errors)
721 $this->error
= implode(' - ', $sth->errorInfo());
723 $this->error
= _("Could not execute query");
727 $query = sprintf("DELETE FROM %s%s%s WHERE %s%s%s='%s' AND %s%s%s='%s'",
728 $this->identifier_quote_char
,
730 $this->identifier_quote_char
,
731 $this->identifier_quote_char
,
733 $this->identifier_quote_char
,
734 $this->dbh
->quoteString($user),
735 $this->identifier_quote_char
,
737 $this->identifier_quote_char
,
738 $this->dbh
->quoteString($key));
739 $res = $this->dbh
->simpleQuery($query);
740 if (DB
::isError($res)) {
741 $this->failQuery($res);
743 $query = sprintf("INSERT INTO %s%s%s (%s%s%s, %s%s%s, %s%s%s) VALUES ('%s', '%s', '%s')",
744 $this->identifier_quote_char
,
746 $this->identifier_quote_char
,
747 $this->identifier_quote_char
,
749 $this->identifier_quote_char
,
750 $this->identifier_quote_char
,
752 $this->identifier_quote_char
,
753 $this->identifier_quote_char
,
755 $this->identifier_quote_char
,
756 $this->dbh
->quoteString($user),
757 $this->dbh
->quoteString($key),
758 $this->dbh
->quoteString($value));
759 $res = $this->dbh
->simpleQuery($query);
760 if (DB
::isError($res)) {
761 $this->failQuery($res);
770 * Fill preference cache array
772 * @param string $user user name
777 function fillPrefsCache($user) {
778 global $prefs_cache, $use_pdo, $pdo_show_sql_errors;
780 if (!$this->open()) {
784 $prefs_cache = array();
786 if (!($sth = $this->dbh
->prepare('SELECT ' . $this->identifier_quote_char
. $this->key_field
. $this->identifier_quote_char
. ' AS prefkey, ' . $this->identifier_quote_char
. $this->val_field
. $this->identifier_quote_char
. ' AS prefval FROM ' . $this->identifier_quote_char
. $this->table
. $this->identifier_quote_char
. ' WHERE ' . $this->identifier_quote_char
. $this->user_field
. $this->identifier_quote_char
. ' = ?'))) {
787 if ($pdo_show_sql_errors)
788 $this->error
= implode(' - ', $this->dbh
->errorInfo());
790 $this->error
= _("Could not prepare query");
793 if (!($res = $sth->execute(array($user)))) {
794 if ($pdo_show_sql_errors)
795 $this->error
= implode(' - ', $sth->errorInfo());
797 $this->error
= _("Could not execute query");
801 while ($row = $sth->fetch(PDO
::FETCH_ASSOC
)) {
802 $prefs_cache[$row['prefkey']] = $row['prefval'];
805 $query = sprintf("SELECT %s%s%s as prefkey, %s%s%s as prefval FROM %s%s%s ".
806 "WHERE %s%s%s = '%s'",
807 $this->identifier_quote_char
,
809 $this->identifier_quote_char
,
810 $this->identifier_quote_char
,
812 $this->identifier_quote_char
,
813 $this->identifier_quote_char
,
815 $this->identifier_quote_char
,
816 $this->identifier_quote_char
,
818 $this->identifier_quote_char
,
819 $this->dbh
->quoteString($user));
820 $res = $this->dbh
->query($query);
821 if (DB
::isError($res)) {
822 $this->failQuery($res);
825 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
826 $prefs_cache[$row['prefkey']] = $row['prefval'];
831 } /* end class dbPrefs */
835 * Returns the value for the requested preference
838 function getPref($data_dir, $username, $pref_name, $default = '') {
840 if(isset($db->error
)) {
841 printf( _("Preference database error (%s). Exiting abnormally"),
846 return $db->getKey($username, $pref_name, $default);
850 * Remove the desired preference setting ($pref_name)
853 function removePref($data_dir, $username, $pref_name) {
856 if(isset($db->error
)) {
860 $db->deleteKey($username, $pref_name);
862 if (isset($prefs_cache[$pref_name])) {
863 unset($prefs_cache[$pref_name]);
866 sqsession_register($prefs_cache , 'prefs_cache');
871 * Sets the desired preference setting ($pref_name) to whatever is in $value
874 function setPref($data_dir, $username, $pref_name, $value) {
877 if (isset($prefs_cache[$pref_name]) && ($prefs_cache[$pref_name] == $value)) {
882 removePref($data_dir, $username, $pref_name);
887 if(isset($db->error
)) {
891 $db->setKey($username, $pref_name, $value);
892 $prefs_cache[$pref_name] = $value;
893 assert_options(ASSERT_ACTIVE
, 1);
894 assert_options(ASSERT_BAIL
, 1);
895 assert ('$value == $prefs_cache[$pref_name]');
896 sqsession_register($prefs_cache , 'prefs_cache');
901 * This checks if the prefs are available
904 function checkForPrefs($data_dir, $username) {
906 if(isset($db->error
)) {
912 * Writes the Signature
915 function setSig($data_dir, $username, $number, $value) {
916 if ($number == "g") {
917 $key = '___signature___';
919 $key = sprintf('___sig%s___', $number);
921 setPref($data_dir, $username, $key, $value);
929 function getSig($data_dir, $username, $number) {
930 if ($number == "g") {
931 $key = '___signature___';
933 $key = sprintf('___sig%d___', $number);
935 return getPref($data_dir, $username, $key);