5 * Copyright (c) 1999-2001 The Squirrelmail Development Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * This contains functions for manipulating user preferences
9 * stored in a database, accessed though the Pear DB layer.
11 * To use this instead of the regular prefs.php, create a
12 * database as described below, and replace prefs.php
18 * The preferences table should have tree columns:
19 * username char \ primary
23 * CREATE TABLE userprefs (user CHAR(32) NOT NULL DEFAULT '',
24 * prefkey CHAR(64) NOT NULL DEFAULT '',
25 * prefval BLOB NOT NULL DEFAULT '',
26 * primary key (user,prefkey));
28 * Configuration of databasename, username and password is done
29 * by changing $DSN below.
34 /*****************************************************************/
35 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
36 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
37 /*** + Base level indent should begin at left margin, as ***/
38 /*** the require_once below. ***/
39 /*** + All identation should consist of four space blocks ***/
40 /*** + Tab characters are evil. ***/
41 /*** + all comments should use "slash-star ... star-slash" ***/
42 /*** style -- no pound characters, no slash-slash style ***/
43 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
44 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
45 /*** + Please use ' instead of ", when possible. Note " ***/
46 /*** should always be used in _( ) function calls. ***/
47 /*** Thank you for your help making the SM code more readable. ***/
48 /*****************************************************************/
50 require_once('DB.php');
53 var $DSN = 'mysql://user@host/database';
54 var $table = 'userprefs';
59 var $default = Array('chosen_theme' => '../themes/default_theme.php',
60 'show_html_default' => '0');
67 if(isset($this->dbh
)) return true;
68 $dbh = DB
::connect($this->DSN
, true);
70 if(DB
::isError($dbh) || DB
::isWarning($dbh)) {
71 $this->error
= DB
::errorMessage($dbh);
80 function failQuery($res = NULL) {
82 printf(_("Preference database error (%s). Exiting abnormally"),
85 printf(_("Preference database error (%s). Exiting abnormally"),
86 DB
::errorMessage($res));
92 function getKey($user, $key) {
94 $query = sprintf("SELECT prefval FROM %s ".
95 "WHERE user='%s' AND prefkey='%s'",
97 $this->dbh
->quoteString($user),
98 $this->dbh
->quoteString($key));
100 $res = $this->dbh
->query($query);
101 if(DB
::isError($res))
102 $this->failQuery($res);
104 if($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
105 return $row['prefval'];
107 if(isset($this->default[$key])) {
108 return $this->default[$key];
117 function deleteKey($user, $key) {
119 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
121 $this->dbh
->quoteString($user),
122 $this->dbh
->quoteString($key));
124 $res = $this->dbh
->simpleQuery($query);
125 if(DB
::isError($res))
126 $this->failQuery($res);
128 if(substr($key, 0, 9) == 'highlight') {
129 $this->renumberHighlightList($user);
135 function setKey($user, $key, $value) {
137 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
138 "VALUES('%s','%s','%s')",
140 $this->dbh
->quoteString($user),
141 $this->dbh
->quoteString($key),
142 $this->dbh
->quoteString($value));
144 $res = $this->dbh
->simpleQuery($query);
145 if(DB
::isError($res))
146 $this->failQuery($res);
153 ** When a highlight option is deleted the preferences module
154 ** must renumber the list. This should be done somewhere else,
155 ** but it is not, so....
157 function renumberHighlightList($user) {
159 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
160 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
162 $this->dbh
->quoteString($user));
164 $res = $this->dbh
->query($query);
165 if(DB
::isError($res))
166 $this->failQuery($res);
168 // Store old data in array
170 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC
))
173 // Renumber keys of old data
175 for($i = 0; $i < count($rows) ; $i++
) {
176 $oldkey = $rows[$i]['prefkey'];
177 $newkey = substr($oldkey, 0, 9) . $hilinum;
180 if($oldkey != $newkey) {
181 $query = sprintf("UPDATE %s SET prefkey='%s' WHERE user='%s' ".
184 $this->dbh
->quoteString($newkey),
185 $this->dbh
->quoteString($user),
186 $this->dbh
->quoteString($oldkey));
188 $res = $this->dbh
->simpleQuery($query);
189 if(DB
::isError($res))
190 $this->failQuery($res);
197 } // end class dbPrefs
200 /** returns the value for the pref $string **/
201 function getPref($data_dir, $username, $string, $default ) {
203 if(isset($db->error
)) {
204 printf( _("Preference database error (%s). Exiting abnormally"),
209 return $db->getKey($username, $string);
212 /** Remove the pref $string **/
213 function removePref($data_dir, $username, $string) {
215 if(isset($db->error
)) $db->failQuery();
217 $db->deleteKey($username, $string);
221 /** sets the pref, $string, to $set_to **/
222 function setPref($data_dir, $username, $string, $set_to) {
224 if(isset($db->error
))
227 $db->setKey($username, $string, $set_to);
231 /** This checks if the prefs are available **/
232 function checkForPrefs($data_dir, $username) {
234 if(isset($db->error
))
238 /** Writes the Signature **/
239 function setSig($data_dir, $username, $string) {
241 if(isset($db->error
))
244 $db->setKey($username, "___signature___", $string);
248 /** Gets the signature **/
249 function getSig($data_dir, $username) {
251 if(isset($db->error
))
254 return $db->getKey($username, "___signature___");