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) {
131 if (!$this->open()) {
134 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
136 $this->dbh
->quoteString($user),
137 $this->dbh
->quoteString($key));
139 $res = $this->dbh
->simpleQuery($query);
140 if(DB
::isError($res)) {
141 $this->failQuery($res);
144 unset($prefs_cache[$key]);
146 if(substr($key, 0, 9) == 'highlight') {
147 $this->renumberHighlightList($user);
153 function setKey($user, $key, $value) {
154 if (!$this->open()) {
157 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
158 "VALUES('%s','%s','%s')",
160 $this->dbh
->quoteString($user),
161 $this->dbh
->quoteString($key),
162 $this->dbh
->quoteString($value));
164 $res = $this->dbh
->simpleQuery($query);
165 if(DB
::isError($res)) {
166 $this->failQuery($res);
172 function fillPrefsCache($user) {
175 if (!$this->open()) {
179 $prefs_cache = array();
180 $query = sprintf("SELECT prefkey, prefval FROM %s ".
183 $this->dbh
->quoteString($user));
184 $res = $this->dbh
->query($query);
185 if (DB
::isError($res)) {
186 $this->failQuery($res);
189 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
190 $prefs_cache[$row['prefkey']] = $row['prefval'];
195 * When a highlight option is deleted the preferences module
196 * must renumber the list. This should be done somewhere else,
197 * but it is not, so....
199 function renumberHighlightList($user) {
200 if (!$this->open()) {
203 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
204 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
206 $this->dbh
->quoteString($user));
208 $res = $this->dbh
->query($query);
209 if(DB
::isError($res)) {
210 $this->failQuery($res);
213 /* Store old data in array */
215 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
219 /* Renumber keys of old data */
221 for($i = 0; $i < count($rows) ; $i++
) {
222 $oldkey = $rows[$i]['prefkey'];
223 $newkey = substr($oldkey, 0, 9) . $hilinum;
226 if($oldkey != $newkey) {
227 $query = sprintf("UPDATE %s SET prefkey='%s' ".
228 "WHERE user ='%s' AND prefkey='%s'",
230 $this->dbh
->quoteString($newkey),
231 $this->dbh
->quoteString($user),
232 $this->dbh
->quoteString($oldkey));
234 $res = $this->dbh
->simpleQuery($query);
235 if(DB
::isError($res)) {
236 $this->failQuery($res);
244 } /* end class dbPrefs */
247 /* returns the value for the pref $string */
248 function getPref($data_dir, $username, $string, $default = '') {
250 if(isset($db->error
)) {
251 printf( _("Preference database error (%s). Exiting abnormally"),
256 return $db->getKey($username, $string, $default);
259 /* Remove the pref $string */
260 function removePref($data_dir, $username, $string) {
262 if(isset($db->error
)) {
266 $db->deleteKey($username, $string);
270 /* sets the pref, $string, to $set_to */
271 function setPref($data_dir, $username, $string, $set_to) {
274 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
279 removePref($data_dir, $username, $string);
284 if(isset($db->error
)) {
288 $db->setKey($username, $string, $set_to);
289 $prefs_cache[$string] = $set_to;
290 assert_options(ASSERT_ACTIVE
, 1);
291 assert_options(ASSERT_BAIL
, 1);
292 assert ('$set_to == $prefs_cache[$string]');
297 /* This checks if the prefs are available */
298 function checkForPrefs($data_dir, $username) {
300 if(isset($db->error
)) {
305 /* Writes the Signature */
306 function setSig($data_dir, $username, $string) {
308 if(isset($db->error
)) {
312 $db->setKey($username, '___signature___', $string);
316 /* Gets the signature */
317 function getSig($data_dir, $username) {
319 if(isset($db->error
)) {
323 return $db->getKey($username, '___signature___');