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 tree columns:
20 * username char \ primary
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';
76 var $default = Array('chosen_theme' => '../themes/default_theme.php',
77 'show_html_default' => '0');
80 global $prefs_dsn, $prefs_table;
82 if(isset($this->dbh
)) {
86 if (!empty($prefs_table)) {
87 $this->table
= $prefs_table;
89 $dbh = DB
::connect($prefs_dsn);
91 if(DB
::isError($dbh) || DB
::isWarning($dbh)) {
92 $this->error
= DB
::errorMessage($dbh);
100 function failQuery($res = NULL) {
102 printf(_("Preference database error (%s). Exiting abnormally"),
105 printf(_("Preference database error (%s). Exiting abnormally"),
106 DB
::errorMessage($res));
112 function getKey($user, $key, $default = '') {
115 cachePrefValues($user);
117 if (isset($prefs_cache[$key])) {
118 return $prefs_cache[$key];
120 if (isset($this->default[$key])) {
121 return $this->default[$key];
128 function deleteKey($user, $key) {
132 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
134 $this->dbh
->quoteString($user),
135 $this->dbh
->quoteString($key));
137 $res = $this->dbh
->simpleQuery($query);
138 if(DB
::isError($res)) {
139 $this->failQuery($res);
142 unset($prefs_cache[$key]);
144 if(substr($key, 0, 9) == 'highlight') {
145 $this->renumberHighlightList($user);
151 function setKey($user, $key, $value) {
153 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
154 "VALUES('%s','%s','%s')",
156 $this->dbh
->quoteString($user),
157 $this->dbh
->quoteString($key),
158 $this->dbh
->quoteString($value));
160 $res = $this->dbh
->simpleQuery($query);
161 if(DB
::isError($res)) {
162 $this->failQuery($res);
168 function fillPrefsCache($user) {
173 $prefs_cache = array();
174 $query = sprintf("SELECT prefkey, prefval FROM %s ".
177 $this->dbh
->quoteString($user));
178 $res = $this->dbh
->query($query);
179 if (DB
::isError($res)) {
180 $this->failQuery($res);
183 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
184 $prefs_cache[$row['prefkey']] = $row['prefval'];
189 * When a highlight option is deleted the preferences module
190 * must renumber the list. This should be done somewhere else,
191 * but it is not, so....
193 function renumberHighlightList($user) {
195 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
196 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
198 $this->dbh
->quoteString($user));
200 $res = $this->dbh
->query($query);
201 if(DB
::isError($res)) {
202 $this->failQuery($res);
205 /* Store old data in array */
207 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
211 /* Renumber keys of old data */
213 for($i = 0; $i < count($rows) ; $i++
) {
214 $oldkey = $rows[$i]['prefkey'];
215 $newkey = substr($oldkey, 0, 9) . $hilinum;
218 if($oldkey != $newkey) {
219 $query = sprintf("UPDATE %s SET prefkey='%s' ".
220 "WHERE user ='%s' AND prefkey='%s'",
222 $this->dbh
->quoteString($newkey),
223 $this->dbh
->quoteString($user),
224 $this->dbh
->quoteString($oldkey));
226 $res = $this->dbh
->simpleQuery($query);
227 if(DB
::isError($res)) {
228 $this->failQuery($res);
236 } /* end class dbPrefs */
239 /* returns the value for the pref $string */
240 function getPref($data_dir, $username, $string, $default = '') {
242 if(isset($db->error
)) {
243 printf( _("Preference database error (%s). Exiting abnormally"),
248 return $db->getKey($username, $string, $default);
251 /* Remove the pref $string */
252 function removePref($data_dir, $username, $string) {
254 if(isset($db->error
)) {
258 $db->deleteKey($username, $string);
262 /* sets the pref, $string, to $set_to */
263 function setPref($data_dir, $username, $string, $set_to) {
266 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
271 removePref($data_dir, $username, $string);
276 if(isset($db->error
)) {
280 $db->setKey($username, $string, $set_to);
281 $prefs_cache[$string] = $set_to;
282 assert_options(ASSERT_ACTIVE
, 1);
283 assert_options(ASSERT_BAIL
, 1);
284 assert ('$set_to == $prefs_cache[$string]');
289 /* This checks if the prefs are available */
290 function checkForPrefs($data_dir, $username) {
292 if(isset($db->error
)) {
297 /* Writes the Signature */
298 function setSig($data_dir, $username, $string) {
300 if(isset($db->error
)) {
304 $db->setKey($username, '___signature___', $string);
308 /* Gets the signature */
309 function getSig($data_dir, $username) {
311 if(isset($db->error
)) {
315 return $db->getKey($username, '___signature___');