6 * Copyright (c) 1999-2002 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
31 define('SMDB_UNKNOWN', 0);
32 define('SMDB_MYSQL', 1);
33 define('SMDB_PGSQL', 2);
35 require_once('DB.php');
36 require_once(SM_PATH
. 'config/config.php');
38 global $prefs_are_cached, $prefs_cache;
40 function cachePrefValues($username) {
41 global $prefs_are_cached, $prefs_cache;
43 if ($prefs_are_cached) {
47 sqsession_unregister('prefs_cache');
48 sqsession_unregister('prefs_are_cached');
51 if(isset($db->error
)) {
52 printf( _("Preference database error (%s). Exiting abnormally"),
57 $db->fillPrefsCache($username);
58 if (isset($db->error
)) {
59 printf( _("Preference database error (%s). Exiting abnormally"),
64 $prefs_are_cached = true;
66 sqsession_register($prefs_cache, 'prefs_cache');
67 sqsession_register($prefs_are_cached, 'prefs_are_cached');
71 var $table = 'userprefs';
72 var $user_field = 'user';
73 var $key_field = 'prefkey';
74 var $val_field = 'prefval';
78 var $db_type = SMDB_UNKNOWN
;
80 var $default = Array('theme_default' => 0,
81 'show_html_default' => '0');
84 global $prefs_dsn, $prefs_table;
85 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
87 if(isset($this->dbh
)) {
91 if (preg_match('/^mysql/', $prefs_dsn)) {
92 $this->db_type
= SMDB_MYSQL
;
93 } elseif (preg_match('/^pgsql/', $prefs_dsn)) {
94 $this->db_type
= SMDB_PGSQL
;
97 if (!empty($prefs_table)) {
98 $this->table
= $prefs_table;
100 if (!empty($prefs_user_field)) {
101 $this->user_field
= $prefs_user_field;
103 if (!empty($prefs_key_field)) {
104 $this->key_field
= $prefs_key_field;
106 if (!empty($prefs_val_field)) {
107 $this->val_field
= $prefs_val_field;
109 $dbh = DB
::connect($prefs_dsn, true);
111 if(DB
::isError($dbh)) {
112 $this->error
= DB
::errorMessage($dbh);
120 function failQuery($res = NULL) {
122 printf(_("Preference database error (%s). Exiting abnormally"),
125 printf(_("Preference database error (%s). Exiting abnormally"),
126 DB
::errorMessage($res));
132 function getKey($user, $key, $default = '') {
135 cachePrefValues($user);
137 if (isset($prefs_cache[$key])) {
138 return $prefs_cache[$key];
140 if (isset($this->default[$key])) {
141 return $this->default[$key];
148 function deleteKey($user, $key) {
151 if (!$this->open()) {
154 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
157 $this->dbh
->quoteString($user),
159 $this->dbh
->quoteString($key));
161 $res = $this->dbh
->simpleQuery($query);
162 if(DB
::isError($res)) {
163 $this->failQuery($res);
166 unset($prefs_cache[$key]);
171 function setKey($user, $key, $value) {
172 if (!$this->open()) {
175 if ($this->db_type
== SMDB_MYSQL
) {
176 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
177 "VALUES('%s','%s','%s')",
182 $this->dbh
->quoteString($user),
183 $this->dbh
->quoteString($key),
184 $this->dbh
->quoteString($value));
186 $res = $this->dbh
->simpleQuery($query);
187 if(DB
::isError($res)) {
188 $this->failQuery($res);
190 } elseif ($this->db_type
== SMDB_PGSQL
) {
191 $this->dbh
->simpleQuery("BEGIN TRANSACTION");
192 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
195 $this->dbh
->quoteString($user),
197 $this->dbh
->quoteString($key));
198 $res = $this->dbh
->simpleQuery($query);
199 if (DB
::isError($res)) {
200 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
201 $this->failQuery($res);
203 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
208 $this->dbh
->quoteString($user),
209 $this->dbh
->quoteString($key),
210 $this->dbh
->quoteString($value));
211 $res = $this->dbh
->simpleQuery($query);
212 if (DB
::isError($res)) {
213 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
214 $this->failQuery($res);
216 $this->dbh
->simpleQuery("COMMIT TRANSACTION");
218 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
221 $this->dbh
->quoteString($user),
223 $this->dbh
->quoteString($key));
224 $res = $this->dbh
->simpleQuery($query);
225 if (DB
::isError($res)) {
226 $this->failQuery($res);
228 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
233 $this->dbh
->quoteString($user),
234 $this->dbh
->quoteString($key),
235 $this->dbh
->quoteString($value));
236 $res = $this->dbh
->simpleQuery($query);
237 if (DB
::isError($res)) {
238 $this->failQuery($res);
245 function fillPrefsCache($user) {
248 if (!$this->open()) {
252 $prefs_cache = array();
253 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
259 $this->dbh
->quoteString($user));
260 $res = $this->dbh
->query($query);
261 if (DB
::isError($res)) {
262 $this->failQuery($res);
265 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
266 $prefs_cache[$row['prefkey']] = $row['prefval'];
270 } /* end class dbPrefs */
273 /* returns the value for the pref $string */
274 function getPref($data_dir, $username, $string, $default = '') {
276 if(isset($db->error
)) {
277 printf( _("Preference database error (%s). Exiting abnormally"),
282 return $db->getKey($username, $string, $default);
285 /* Remove the pref $string */
286 function removePref($data_dir, $username, $string) {
288 if(isset($db->error
)) {
292 $db->deleteKey($username, $string);
296 /* sets the pref, $string, to $set_to */
297 function setPref($data_dir, $username, $string, $set_to) {
300 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
304 if ($set_to === '') {
305 removePref($data_dir, $username, $string);
310 if(isset($db->error
)) {
314 $db->setKey($username, $string, $set_to);
315 $prefs_cache[$string] = $set_to;
316 assert_options(ASSERT_ACTIVE
, 1);
317 assert_options(ASSERT_BAIL
, 1);
318 assert ('$set_to == $prefs_cache[$string]');
323 /* This checks if the prefs are available */
324 function checkForPrefs($data_dir, $username) {
326 if(isset($db->error
)) {
331 /* Writes the Signature */
332 function setSig($data_dir, $username, $number, $string) {
333 if ($number == "g") {
334 $key = '___signature___';
336 $key = sprintf('___sig%s___', $number);
338 setPref($data_dir, $username, $key, $string);
342 /* Gets the signature */
343 function getSig($data_dir, $username, $number) {
344 if ($number == "g") {
345 $key = '___signature___';
347 $key = sprintf('___sig%d___', $number);
349 return getPref($data_dir, $username, $key);