6 * Copyright (c) 1999-2004 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;
47 function cachePrefValues($username) {
48 global $prefs_are_cached, $prefs_cache;
50 if ($prefs_are_cached) {
54 sqsession_unregister('prefs_cache');
55 sqsession_unregister('prefs_are_cached');
58 if(isset($db->error
)) {
59 printf( _("Preference database error (%s). Exiting abnormally"),
64 $db->fillPrefsCache($username);
65 if (isset($db->error
)) {
66 printf( _("Preference database error (%s). Exiting abnormally"),
71 $prefs_are_cached = true;
73 sqsession_register($prefs_cache, 'prefs_cache');
74 sqsession_register($prefs_are_cached, 'prefs_are_cached');
78 * Completely undocumented class - someone document it!
79 * @package squirrelmail
82 var $table = 'userprefs';
83 var $user_field = 'user';
84 var $key_field = 'prefkey';
85 var $val_field = 'prefval';
89 var $db_type = SMDB_UNKNOWN
;
91 var $default = Array('theme_default' => 0,
92 'show_html_default' => '0');
95 global $prefs_dsn, $prefs_table;
96 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
98 if(isset($this->dbh
)) {
102 if (preg_match('/^mysql/', $prefs_dsn)) {
103 $this->db_type
= SMDB_MYSQL
;
104 } elseif (preg_match('/^pgsql/', $prefs_dsn)) {
105 $this->db_type
= SMDB_PGSQL
;
108 if (!empty($prefs_table)) {
109 $this->table
= $prefs_table;
111 if (!empty($prefs_user_field)) {
112 $this->user_field
= $prefs_user_field;
114 if (!empty($prefs_key_field)) {
115 $this->key_field
= $prefs_key_field;
117 if (!empty($prefs_val_field)) {
118 $this->val_field
= $prefs_val_field;
120 $dbh = DB
::connect($prefs_dsn, true);
122 if(DB
::isError($dbh)) {
123 $this->error
= DB
::errorMessage($dbh);
131 function failQuery($res = NULL) {
133 printf(_("Preference database error (%s). Exiting abnormally"),
136 printf(_("Preference database error (%s). Exiting abnormally"),
137 DB
::errorMessage($res));
143 function getKey($user, $key, $default = '') {
146 cachePrefValues($user);
148 if (isset($prefs_cache[$key])) {
149 return $prefs_cache[$key];
151 if (isset($this->default[$key])) {
152 return $this->default[$key];
159 function deleteKey($user, $key) {
162 if (!$this->open()) {
165 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
168 $this->dbh
->quoteString($user),
170 $this->dbh
->quoteString($key));
172 $res = $this->dbh
->simpleQuery($query);
173 if(DB
::isError($res)) {
174 $this->failQuery($res);
177 unset($prefs_cache[$key]);
182 function setKey($user, $key, $value) {
183 if (!$this->open()) {
186 if ($this->db_type
== SMDB_MYSQL
) {
187 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
188 "VALUES('%s','%s','%s')",
193 $this->dbh
->quoteString($user),
194 $this->dbh
->quoteString($key),
195 $this->dbh
->quoteString($value));
197 $res = $this->dbh
->simpleQuery($query);
198 if(DB
::isError($res)) {
199 $this->failQuery($res);
201 } elseif ($this->db_type
== SMDB_PGSQL
) {
202 $this->dbh
->simpleQuery("BEGIN TRANSACTION");
203 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
206 $this->dbh
->quoteString($user),
208 $this->dbh
->quoteString($key));
209 $res = $this->dbh
->simpleQuery($query);
210 if (DB
::isError($res)) {
211 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
212 $this->failQuery($res);
214 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
219 $this->dbh
->quoteString($user),
220 $this->dbh
->quoteString($key),
221 $this->dbh
->quoteString($value));
222 $res = $this->dbh
->simpleQuery($query);
223 if (DB
::isError($res)) {
224 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
225 $this->failQuery($res);
227 $this->dbh
->simpleQuery("COMMIT TRANSACTION");
229 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
232 $this->dbh
->quoteString($user),
234 $this->dbh
->quoteString($key));
235 $res = $this->dbh
->simpleQuery($query);
236 if (DB
::isError($res)) {
237 $this->failQuery($res);
239 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
244 $this->dbh
->quoteString($user),
245 $this->dbh
->quoteString($key),
246 $this->dbh
->quoteString($value));
247 $res = $this->dbh
->simpleQuery($query);
248 if (DB
::isError($res)) {
249 $this->failQuery($res);
256 function fillPrefsCache($user) {
259 if (!$this->open()) {
263 $prefs_cache = array();
264 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
270 $this->dbh
->quoteString($user));
271 $res = $this->dbh
->query($query);
272 if (DB
::isError($res)) {
273 $this->failQuery($res);
276 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
277 $prefs_cache[$row['prefkey']] = $row['prefval'];
281 } /* end class dbPrefs */
285 * returns the value for the pref $string
288 function getPref($data_dir, $username, $string, $default = '') {
290 if(isset($db->error
)) {
291 printf( _("Preference database error (%s). Exiting abnormally"),
296 return $db->getKey($username, $string, $default);
300 * Remove the pref $string
303 function removePref($data_dir, $username, $string) {
306 if(isset($db->error
)) {
310 $db->deleteKey($username, $string);
312 if (isset($prefs_cache[$string])) {
313 unset($prefs_cache[$string]);
316 sqsession_register($prefs_cache , 'prefs_cache');
321 * sets the pref, $string, to $set_to
324 function setPref($data_dir, $username, $string, $set_to) {
327 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
331 if ($set_to === '') {
332 removePref($data_dir, $username, $string);
337 if(isset($db->error
)) {
341 $db->setKey($username, $string, $set_to);
342 $prefs_cache[$string] = $set_to;
343 assert_options(ASSERT_ACTIVE
, 1);
344 assert_options(ASSERT_BAIL
, 1);
345 assert ('$set_to == $prefs_cache[$string]');
346 sqsession_register($prefs_cache , 'prefs_cache');
351 * This checks if the prefs are available
354 function checkForPrefs($data_dir, $username) {
356 if(isset($db->error
)) {
362 * Writes the Signature
365 function setSig($data_dir, $username, $number, $string) {
366 if ($number == "g") {
367 $key = '___signature___';
369 $key = sprintf('___sig%s___', $number);
371 setPref($data_dir, $username, $key, $string);
379 function getSig($data_dir, $username, $number) {
380 if ($number == "g") {
381 $key = '___signature___';
383 $key = sprintf('___sig%d___', $number);
385 return getPref($data_dir, $username, $key);