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 session_unregister('prefs_cache');
48 session_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 session_register('prefs_cache');
67 session_register('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) || DB
::isWarning($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]);
168 if(substr($key, 0, 9) == 'highlight') {
169 $this->renumberHighlightList($user);
175 function setKey($user, $key, $value) {
176 if (!$this->open()) {
179 if ($this->db_type
== SMDB_MYSQL
) {
180 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
181 "VALUES('%s','%s','%s')",
186 $this->dbh
->quoteString($user),
187 $this->dbh
->quoteString($key),
188 $this->dbh
->quoteString($value));
190 $res = $this->dbh
->simpleQuery($query);
191 if(DB
::isError($res)) {
192 $this->failQuery($res);
194 } elseif ($this->db_type
== SMDB_PGSQL
) {
195 $this->dbh
->simpleQuery("BEGIN TRANSACTION");
196 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
199 $this->dbh
->quoteString($user),
201 $this->dbh
->quoteString($key));
202 $res = $this->dbh
->simpleQuery($query);
203 if (DB
::isError($res)) {
204 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
205 $this->failQuery($res);
207 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
212 $this->dbh
->quoteString($user),
213 $this->dbh
->quoteString($key),
214 $this->dbh
->quoteString($value));
215 $res = $this->dbh
->simpleQuery($query);
216 if (DB
::isError($res)) {
217 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
218 $this->failQuery($res);
220 $this->dbh
->simpleQuery("COMMIT TRANSACTION");
222 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
225 $this->dbh
->quoteString($user),
227 $this->dbh
->quoteString($key));
228 $res = $this->dbh
->simpleQuery($query);
229 if (DB
::isError($res)) {
230 $this->failQuery($res);
232 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
237 $this->dbh
->quoteString($user),
238 $this->dbh
->quoteString($key),
239 $this->dbh
->quoteString($value));
240 $res = $this->dbh
->simpleQuery($query);
241 if (DB
::isError($res)) {
242 $this->failQuery($res);
249 function fillPrefsCache($user) {
252 if (!$this->open()) {
256 $prefs_cache = array();
257 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
263 $this->dbh
->quoteString($user));
264 $res = $this->dbh
->query($query);
265 if (DB
::isError($res)) {
266 $this->failQuery($res);
269 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
270 $prefs_cache[$row['prefkey']] = $row['prefval'];
275 * When a highlight option is deleted the preferences module
276 * must renumber the list. This should be done somewhere else,
277 * but it is not, so....
279 function renumberHighlightList($user) {
280 if (!$this->open()) {
283 $query = sprintf("SELECT %s, %s as prefkey, %s as prefval FROM %s WHERE %s='%s' ".
284 "AND %s LIKE 'highlight%%' ORDER BY %s",
290 $this->dbh
->quoteString($user),
294 $res = $this->dbh
->query($query);
295 if(DB
::isError($res)) {
296 $this->failQuery($res);
299 /* Store old data in array */
301 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
305 /* Renumber keys of old data */
307 for($i = 0; $i < count($rows) ; $i++
) {
308 $oldkey = $rows[$i]['prefkey'];
309 $newkey = substr($oldkey, 0, 9) . $hilinum;
312 if($oldkey != $newkey) {
313 $query = sprintf("UPDATE %s SET %s='%s' ".
314 "WHERE %s ='%s' AND %s='%s'",
317 $this->dbh
->quoteString($newkey),
319 $this->dbh
->quoteString($user),
321 $this->dbh
->quoteString($oldkey));
323 $res = $this->dbh
->simpleQuery($query);
324 if(DB
::isError($res)) {
325 $this->failQuery($res);
333 } /* end class dbPrefs */
336 /* returns the value for the pref $string */
337 function getPref($data_dir, $username, $string, $default = '') {
339 if(isset($db->error
)) {
340 printf( _("Preference database error (%s). Exiting abnormally"),
345 return $db->getKey($username, $string, $default);
348 /* Remove the pref $string */
349 function removePref($data_dir, $username, $string) {
351 if(isset($db->error
)) {
355 $db->deleteKey($username, $string);
359 /* sets the pref, $string, to $set_to */
360 function setPref($data_dir, $username, $string, $set_to) {
363 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
368 removePref($data_dir, $username, $string);
373 if(isset($db->error
)) {
377 $db->setKey($username, $string, $set_to);
378 $prefs_cache[$string] = $set_to;
379 assert_options(ASSERT_ACTIVE
, 1);
380 assert_options(ASSERT_BAIL
, 1);
381 assert ('$set_to == $prefs_cache[$string]');
386 /* This checks if the prefs are available */
387 function checkForPrefs($data_dir, $username) {
389 if(isset($db->error
)) {
394 /* Writes the Signature */
395 function setSig($data_dir, $username, $number, $string) {
396 if ($number == "g") {
397 $key = '___signature___';
399 $key = sprintf('___sig%s___', $number);
401 setPref($data_dir, $username, $key, $string);
405 /* Gets the signature */
406 function getSig($data_dir, $username, $number) {
407 if ($number == "g") {
408 $key = '___signature___';
410 $key = sprintf('___sig%d___', $number);
412 return getPref($data_dir, $username, $key);