6 * Copyright (c) 1999-2003 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
32 /** Unknown database */
33 define('SMDB_UNKNOWN', 0);
35 define('SMDB_MYSQL', 1);
37 define('SMDB_PGSQL', 2);
39 require_once('DB.php');
40 require_once(SM_PATH
. 'config/config.php');
42 global $prefs_are_cached, $prefs_cache;
44 function cachePrefValues($username) {
45 global $prefs_are_cached, $prefs_cache;
47 if ($prefs_are_cached) {
51 sqsession_unregister('prefs_cache');
52 sqsession_unregister('prefs_are_cached');
55 if(isset($db->error
)) {
56 printf( _("Preference database error (%s). Exiting abnormally"),
61 $db->fillPrefsCache($username);
62 if (isset($db->error
)) {
63 printf( _("Preference database error (%s). Exiting abnormally"),
68 $prefs_are_cached = true;
70 sqsession_register($prefs_cache, 'prefs_cache');
71 sqsession_register($prefs_are_cached, 'prefs_are_cached');
75 * Completely undocumented class - someone document it!
76 * @package squirrelmail
79 var $table = 'userprefs';
80 var $user_field = 'user';
81 var $key_field = 'prefkey';
82 var $val_field = 'prefval';
86 var $db_type = SMDB_UNKNOWN
;
88 var $default = Array('theme_default' => 0,
89 'show_html_default' => '0');
92 global $prefs_dsn, $prefs_table;
93 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
95 if(isset($this->dbh
)) {
99 if (preg_match('/^mysql/', $prefs_dsn)) {
100 $this->db_type
= SMDB_MYSQL
;
101 } elseif (preg_match('/^pgsql/', $prefs_dsn)) {
102 $this->db_type
= SMDB_PGSQL
;
105 if (!empty($prefs_table)) {
106 $this->table
= $prefs_table;
108 if (!empty($prefs_user_field)) {
109 $this->user_field
= $prefs_user_field;
111 if (!empty($prefs_key_field)) {
112 $this->key_field
= $prefs_key_field;
114 if (!empty($prefs_val_field)) {
115 $this->val_field
= $prefs_val_field;
117 $dbh = DB
::connect($prefs_dsn, true);
119 if(DB
::isError($dbh)) {
120 $this->error
= DB
::errorMessage($dbh);
128 function failQuery($res = NULL) {
130 printf(_("Preference database error (%s). Exiting abnormally"),
133 printf(_("Preference database error (%s). Exiting abnormally"),
134 DB
::errorMessage($res));
140 function getKey($user, $key, $default = '') {
143 cachePrefValues($user);
145 if (isset($prefs_cache[$key])) {
146 return $prefs_cache[$key];
148 if (isset($this->default[$key])) {
149 return $this->default[$key];
156 function deleteKey($user, $key) {
159 if (!$this->open()) {
162 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
165 $this->dbh
->quoteString($user),
167 $this->dbh
->quoteString($key));
169 $res = $this->dbh
->simpleQuery($query);
170 if(DB
::isError($res)) {
171 $this->failQuery($res);
174 unset($prefs_cache[$key]);
179 function setKey($user, $key, $value) {
180 if (!$this->open()) {
183 if ($this->db_type
== SMDB_MYSQL
) {
184 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
185 "VALUES('%s','%s','%s')",
190 $this->dbh
->quoteString($user),
191 $this->dbh
->quoteString($key),
192 $this->dbh
->quoteString($value));
194 $res = $this->dbh
->simpleQuery($query);
195 if(DB
::isError($res)) {
196 $this->failQuery($res);
198 } elseif ($this->db_type
== SMDB_PGSQL
) {
199 $this->dbh
->simpleQuery("BEGIN TRANSACTION");
200 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
203 $this->dbh
->quoteString($user),
205 $this->dbh
->quoteString($key));
206 $res = $this->dbh
->simpleQuery($query);
207 if (DB
::isError($res)) {
208 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
209 $this->failQuery($res);
211 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
216 $this->dbh
->quoteString($user),
217 $this->dbh
->quoteString($key),
218 $this->dbh
->quoteString($value));
219 $res = $this->dbh
->simpleQuery($query);
220 if (DB
::isError($res)) {
221 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
222 $this->failQuery($res);
224 $this->dbh
->simpleQuery("COMMIT TRANSACTION");
226 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
229 $this->dbh
->quoteString($user),
231 $this->dbh
->quoteString($key));
232 $res = $this->dbh
->simpleQuery($query);
233 if (DB
::isError($res)) {
234 $this->failQuery($res);
236 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
241 $this->dbh
->quoteString($user),
242 $this->dbh
->quoteString($key),
243 $this->dbh
->quoteString($value));
244 $res = $this->dbh
->simpleQuery($query);
245 if (DB
::isError($res)) {
246 $this->failQuery($res);
253 function fillPrefsCache($user) {
256 if (!$this->open()) {
260 $prefs_cache = array();
261 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
267 $this->dbh
->quoteString($user));
268 $res = $this->dbh
->query($query);
269 if (DB
::isError($res)) {
270 $this->failQuery($res);
273 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
274 $prefs_cache[$row['prefkey']] = $row['prefval'];
278 } /* end class dbPrefs */
281 /* returns the value for the pref $string */
282 function getPref($data_dir, $username, $string, $default = '') {
284 if(isset($db->error
)) {
285 printf( _("Preference database error (%s). Exiting abnormally"),
290 return $db->getKey($username, $string, $default);
293 /* Remove the pref $string */
294 function removePref($data_dir, $username, $string) {
297 if(isset($db->error
)) {
301 $db->deleteKey($username, $string);
303 if (isset($prefs_cache[$string])) {
304 unset($prefs_cache[$string]);
307 sqsession_register($prefs_cache , 'prefs_cache');
311 /* sets the pref, $string, to $set_to */
312 function setPref($data_dir, $username, $string, $set_to) {
315 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
319 if ($set_to === '') {
320 removePref($data_dir, $username, $string);
325 if(isset($db->error
)) {
329 $db->setKey($username, $string, $set_to);
330 $prefs_cache[$string] = $set_to;
331 assert_options(ASSERT_ACTIVE
, 1);
332 assert_options(ASSERT_BAIL
, 1);
333 assert ('$set_to == $prefs_cache[$string]');
334 sqsession_register($prefs_cache , 'prefs_cache');
338 /* This checks if the prefs are available */
339 function checkForPrefs($data_dir, $username) {
341 if(isset($db->error
)) {
346 /* Writes the Signature */
347 function setSig($data_dir, $username, $number, $string) {
348 if ($number == "g") {
349 $key = '___signature___';
351 $key = sprintf('___sig%s___', $number);
353 setPref($data_dir, $username, $key, $string);
357 /* Gets the signature */
358 function getSig($data_dir, $username, $number) {
359 if ($number == "g") {
360 $key = '___signature___';
362 $key = sprintf('___sig%d___', $number);
364 return getPref($data_dir, $username, $key);