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.
12 * To use this instead of the regular prefs.php, create a
13 * database as described below, and replace prefs.php
19 * The preferences table should have three columns:
24 * CREATE TABLE userprefs (user CHAR(128) NOT NULL DEFAULT '',
25 * prefkey CHAR(64) NOT NULL DEFAULT '',
26 * prefval BLOB NOT NULL DEFAULT '',
27 * primary key (user,prefkey));
29 * Configuration of databasename, username and password is done
30 * by using conf.pl or the administrator plugin
35 require_once('DB.php');
36 require_once('../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';
79 var $default = Array('chosen_theme' => '../themes/default_theme.php',
80 'show_html_default' => '0');
83 global $prefs_dsn, $prefs_table;
85 if(isset($this->dbh
)) {
89 if (!empty($prefs_table)) {
90 $this->table
= $prefs_table;
92 if (!empty($prefs_user_field)) {
93 $this->user_field
= $prefs_user_field;
95 if (!empty($prefs_key_field)) {
96 $this->key_field
= $prefs_key_field;
98 if (!empty($prefs_val_field)) {
99 $this->val_field
= $prefs_val_field;
101 $dbh = DB
::connect($prefs_dsn, true);
103 if(DB
::isError($dbh) || DB
::isWarning($dbh)) {
104 $this->error
= DB
::errorMessage($dbh);
112 function failQuery($res = NULL) {
114 printf(_("Preference database error (%s). Exiting abnormally"),
117 printf(_("Preference database error (%s). Exiting abnormally"),
118 DB
::errorMessage($res));
124 function getKey($user, $key, $default = '') {
127 cachePrefValues($user);
129 if (isset($prefs_cache[$key])) {
130 return $prefs_cache[$key];
132 if (isset($this->default[$key])) {
133 return $this->default[$key];
140 function deleteKey($user, $key) {
143 if (!$this->open()) {
146 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
149 $this->dbh
->quoteString($user),
151 $this->dbh
->quoteString($key));
153 $res = $this->dbh
->simpleQuery($query);
154 if(DB
::isError($res)) {
155 $this->failQuery($res);
158 unset($prefs_cache[$key]);
160 if(substr($key, 0, 9) == 'highlight') {
161 $this->renumberHighlightList($user);
167 function setKey($user, $key, $value) {
168 if (!$this->open()) {
171 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
172 "VALUES('%s','%s','%s')",
177 $this->dbh
->quoteString($user),
178 $this->dbh
->quoteString($key),
179 $this->dbh
->quoteString($value));
181 $res = $this->dbh
->simpleQuery($query);
182 if(DB
::isError($res)) {
183 $this->failQuery($res);
189 function fillPrefsCache($user) {
192 if (!$this->open()) {
196 $prefs_cache = array();
197 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
203 $this->dbh
->quoteString($user));
204 $res = $this->dbh
->query($query);
205 if (DB
::isError($res)) {
206 $this->failQuery($res);
209 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
210 $prefs_cache[$row['prefkey']] = $row['prefval'];
215 * When a highlight option is deleted the preferences module
216 * must renumber the list. This should be done somewhere else,
217 * but it is not, so....
219 function renumberHighlightList($user) {
220 if (!$this->open()) {
223 $query = sprintf("SELECT %s, %s as prefkey, %s as prefval FROM %s WHERE %s='%s' ".
224 "AND %s LIKE 'highlight%%' ORDER BY %s",
230 $this->dbh
->quoteString($user),
234 $res = $this->dbh
->query($query);
235 if(DB
::isError($res)) {
236 $this->failQuery($res);
239 /* Store old data in array */
241 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
245 /* Renumber keys of old data */
247 for($i = 0; $i < count($rows) ; $i++
) {
248 $oldkey = $rows[$i]['prefkey'];
249 $newkey = substr($oldkey, 0, 9) . $hilinum;
252 if($oldkey != $newkey) {
253 $query = sprintf("UPDATE %s SET %s='%s' ".
254 "WHERE %s ='%s' AND %s='%s'",
257 $this->dbh
->quoteString($newkey),
259 $this->dbh
->quoteString($user),
261 $this->dbh
->quoteString($oldkey));
263 $res = $this->dbh
->simpleQuery($query);
264 if(DB
::isError($res)) {
265 $this->failQuery($res);
273 } /* end class dbPrefs */
276 /* returns the value for the pref $string */
277 function getPref($data_dir, $username, $string, $default = '') {
279 if(isset($db->error
)) {
280 printf( _("Preference database error (%s). Exiting abnormally"),
285 return $db->getKey($username, $string, $default);
288 /* Remove the pref $string */
289 function removePref($data_dir, $username, $string) {
291 if(isset($db->error
)) {
295 $db->deleteKey($username, $string);
299 /* sets the pref, $string, to $set_to */
300 function setPref($data_dir, $username, $string, $set_to) {
303 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
308 removePref($data_dir, $username, $string);
313 if(isset($db->error
)) {
317 $db->setKey($username, $string, $set_to);
318 $prefs_cache[$string] = $set_to;
319 assert_options(ASSERT_ACTIVE
, 1);
320 assert_options(ASSERT_BAIL
, 1);
321 assert ('$set_to == $prefs_cache[$string]');
326 /* This checks if the prefs are available */
327 function checkForPrefs($data_dir, $username) {
329 if(isset($db->error
)) {
334 /* Writes the Signature */
335 function setSig($data_dir, $username, $number, $string) {
337 if(isset($db->error
)) {
341 if ($number == "g") {
342 $key = '___signature___';
344 $key = sprintf('___sig%s___', $number);
346 $db->setKey($username, $key, $string);
350 /* Gets the signature */
351 function getSig($data_dir, $username, $number) {
353 if(isset($db->error
)) {
357 if ($number == "g") {
358 $key = '___signature___';
360 $key = sprintf('___sig%d___', $number);
362 return $db->getKey($username, $key);