6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains functions for manipulating user preferences
10 * stored in a database, accessed though the Pear DB layer.
15 * The preferences table should have three columns:
20 * CREATE TABLE userprefs (user CHAR(128) NOT NULL DEFAULT '',
21 * prefkey CHAR(64) NOT NULL DEFAULT '',
22 * prefval BLOB NOT NULL DEFAULT '',
23 * primary key (user,prefkey));
25 * Configuration of databasename, username and password is done
26 * by using conf.pl or the administrator plugin
29 * @package squirrelmail
35 if (!defined('SM_PATH')) define('SM_PATH','../');
37 /** Unknown database */
38 define('SMDB_UNKNOWN', 0);
40 define('SMDB_MYSQL', 1);
42 define('SMDB_PGSQL', 2);
44 require_once(SM_PATH
. 'config/config.php');
45 if (!include_once('DB.php')) {
46 // same error also in abook_database.php
47 require_once(SM_PATH
. 'functions/display_messages.php');
48 $error = _("Could not include PEAR database functions required for the database backend.") . "<br />\n";
49 $error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
50 '<tt>DB.php</tt>') . "<br />\n";
51 $error .= _("Please contact your system administrator and report this error.");
52 error_box($error, $color);
56 global $prefs_are_cached, $prefs_cache;
61 function cachePrefValues($username) {
62 global $prefs_are_cached, $prefs_cache;
64 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION
);
65 if ($prefs_are_cached) {
66 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION
);
70 sqsession_unregister('prefs_cache');
71 sqsession_unregister('prefs_are_cached');
74 if(isset($db->error
)) {
75 printf( _("Preference database error (%s). Exiting abnormally"),
80 $db->fillPrefsCache($username);
81 if (isset($db->error
)) {
82 printf( _("Preference database error (%s). Exiting abnormally"),
87 $prefs_are_cached = true;
89 sqsession_register($prefs_cache, 'prefs_cache');
90 sqsession_register($prefs_are_cached, 'prefs_are_cached');
94 * Class used to handle connections to prefs database and operations with preferences
95 * @package squirrelmail
101 * Table used to store preferences
104 var $table = 'userprefs';
106 * Field used to store owner of preference
109 var $user_field = 'user';
111 * Field used to store preference name
114 var $key_field = 'prefkey';
116 * Field used to store preference value
119 var $val_field = 'prefval';
122 * Database connection object
132 * Database type (SMDB_* constants)
133 * Is used in setKey().
136 var $db_type = SMDB_UNKNOWN
;
139 * Default preferences
142 var $default = Array('theme_default' => 0,
143 'show_html_default' => '0');
146 * initialize DB connection object
147 * @return boolean true, if object is initialized
150 global $prefs_dsn, $prefs_table;
151 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
153 if(isset($this->dbh
)) {
157 if (preg_match('/^mysql/', $prefs_dsn)) {
158 $this->db_type
= SMDB_MYSQL
;
159 } elseif (preg_match('/^pgsql/', $prefs_dsn)) {
160 $this->db_type
= SMDB_PGSQL
;
163 if (!empty($prefs_table)) {
164 $this->table
= $prefs_table;
166 if (!empty($prefs_user_field)) {
167 $this->user_field
= $prefs_user_field;
169 if (!empty($prefs_key_field)) {
170 $this->key_field
= $prefs_key_field;
172 if (!empty($prefs_val_field)) {
173 $this->val_field
= $prefs_val_field;
175 $dbh = DB
::connect($prefs_dsn, true);
177 if(DB
::isError($dbh)) {
178 $this->error
= DB
::errorMessage($dbh);
187 * Function used to handle database connection errors
188 * @param object PEAR Error object
190 function failQuery($res = NULL) {
192 printf(_("Preference database error (%s). Exiting abnormally"),
195 printf(_("Preference database error (%s). Exiting abnormally"),
196 DB
::errorMessage($res));
202 * Get user's prefs setting
203 * @param string $user user name
204 * @param string $key preference name
205 * @param mixed $default (since 1.2.5) default value
206 * @return mixed preference value
208 function getKey($user, $key, $default = '') {
211 cachePrefValues($user);
213 if (isset($prefs_cache[$key])) {
214 return $prefs_cache[$key];
216 if (isset($this->default[$key])) {
217 return $this->default[$key];
225 * Delete user's prefs setting
226 * @param string $user user name
227 * @param string $key preference name
230 function deleteKey($user, $key) {
233 if (!$this->open()) {
236 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
239 $this->dbh
->quoteString($user),
241 $this->dbh
->quoteString($key));
243 $res = $this->dbh
->simpleQuery($query);
244 if(DB
::isError($res)) {
245 $this->failQuery($res);
248 unset($prefs_cache[$key]);
254 * Set user's preference
255 * @param string $user user name
256 * @param string $key preference name
257 * @param mixed $value preference value
260 function setKey($user, $key, $value) {
261 if (!$this->open()) {
264 if ($this->db_type
== SMDB_MYSQL
) {
265 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
266 "VALUES('%s','%s','%s')",
271 $this->dbh
->quoteString($user),
272 $this->dbh
->quoteString($key),
273 $this->dbh
->quoteString($value));
275 $res = $this->dbh
->simpleQuery($query);
276 if(DB
::isError($res)) {
277 $this->failQuery($res);
279 } elseif ($this->db_type
== SMDB_PGSQL
) {
280 $this->dbh
->simpleQuery("BEGIN TRANSACTION");
281 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
284 $this->dbh
->quoteString($user),
286 $this->dbh
->quoteString($key));
287 $res = $this->dbh
->simpleQuery($query);
288 if (DB
::isError($res)) {
289 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
290 $this->failQuery($res);
292 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
297 $this->dbh
->quoteString($user),
298 $this->dbh
->quoteString($key),
299 $this->dbh
->quoteString($value));
300 $res = $this->dbh
->simpleQuery($query);
301 if (DB
::isError($res)) {
302 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
303 $this->failQuery($res);
305 $this->dbh
->simpleQuery("COMMIT TRANSACTION");
307 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
310 $this->dbh
->quoteString($user),
312 $this->dbh
->quoteString($key));
313 $res = $this->dbh
->simpleQuery($query);
314 if (DB
::isError($res)) {
315 $this->failQuery($res);
317 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
322 $this->dbh
->quoteString($user),
323 $this->dbh
->quoteString($key),
324 $this->dbh
->quoteString($value));
325 $res = $this->dbh
->simpleQuery($query);
326 if (DB
::isError($res)) {
327 $this->failQuery($res);
335 * Fill preference cache array
336 * @param string $user user name
339 function fillPrefsCache($user) {
342 if (!$this->open()) {
346 $prefs_cache = array();
347 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
353 $this->dbh
->quoteString($user));
354 $res = $this->dbh
->query($query);
355 if (DB
::isError($res)) {
356 $this->failQuery($res);
359 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
360 $prefs_cache[$row['prefkey']] = $row['prefval'];
364 } /* end class dbPrefs */
368 * returns the value for the pref $string
371 function getPref($data_dir, $username, $string, $default = '') {
373 if(isset($db->error
)) {
374 printf( _("Preference database error (%s). Exiting abnormally"),
379 return $db->getKey($username, $string, $default);
383 * Remove the pref $string
386 function removePref($data_dir, $username, $string) {
389 if(isset($db->error
)) {
393 $db->deleteKey($username, $string);
395 if (isset($prefs_cache[$string])) {
396 unset($prefs_cache[$string]);
399 sqsession_register($prefs_cache , 'prefs_cache');
404 * sets the pref, $string, to $set_to
407 function setPref($data_dir, $username, $string, $set_to) {
410 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
414 if ($set_to === '') {
415 removePref($data_dir, $username, $string);
420 if(isset($db->error
)) {
424 $db->setKey($username, $string, $set_to);
425 $prefs_cache[$string] = $set_to;
426 assert_options(ASSERT_ACTIVE
, 1);
427 assert_options(ASSERT_BAIL
, 1);
428 assert ('$set_to == $prefs_cache[$string]');
429 sqsession_register($prefs_cache , 'prefs_cache');
434 * This checks if the prefs are available
437 function checkForPrefs($data_dir, $username) {
439 if(isset($db->error
)) {
445 * Writes the Signature
448 function setSig($data_dir, $username, $number, $string) {
449 if ($number == "g") {
450 $key = '___signature___';
452 $key = sprintf('___sig%s___', $number);
454 setPref($data_dir, $username, $key, $string);
462 function getSig($data_dir, $username, $number) {
463 if ($number == "g") {
464 $key = '___signature___';
466 $key = sprintf('___sig%d___', $number);
468 return getPref($data_dir, $username, $key);