Some fixup:
[squirrelmail.git] / functions / db_prefs.php
1 <?php
2 /**
3 ** db_prefs.php
4 **
5 ** This contains functions for manipulating user preferences
6 ** stored in a database, accessed though the Pear DB layer.
7 **
8 ** To use this instead of the regular prefs.php, create a
9 ** database as described below, and replace prefs.php
10 ** with this file.
11 **
12 ** Database:
13 ** ---------
14 **
15 ** The preferences table should have tree columns:
16 ** username char \ primary
17 ** prefkey char / key
18 ** prefval blob
19 **
20 ** CREATE TABLE userprefs (user CHAR(32) NOT NULL DEFAULT '',
21 ** prefkey CHAR(64) NOT NULL DEFAULT '',
22 ** prefval BLOB NOT NULL DEFAULT '',
23 ** primary key (user,prefkey));
24 **
25 ** Configuration of databasename, username and password is done
26 ** by changing $DSN below.
27 **
28 ** $Id$
29 **/
30
31 require_once('DB.php');
32
33 class dbPrefs {
34 var $DSN = 'mysql://user@host/database';
35 var $table = 'userprefs';
36
37 var $dbh = NULL;
38 var $error = NULL;
39
40 var $default = Array('chosen_theme' => '../themes/default_theme.php',
41 'show_html_default' => '0');
42
43 function dbPrefs() {
44 $this->open();
45 }
46
47 function open() {
48 if(isset($this->dbh)) return true;
49 $dbh = DB::connect($this->DSN, true);
50
51 if(DB::isError($dbh) || DB::isWarning($dbh)) {
52 $this->error = DB::errorMessage($dbh);
53 return false;
54 }
55
56 $this->dbh = $dbh;
57 return true;
58 }
59
60
61 function failQuery($res = NULL) {
62 if($res == NULL) {
63 printf(_("Preference database error (%s). Exiting abnormally"),
64 $this->error);
65 } else {
66 printf(_("Preference database error (%s). Exiting abnormally"),
67 DB::errorMessage($res));
68 }
69 exit;
70 }
71
72
73 function getKey($user, $key) {
74 $this->open();
75 $query = sprintf("SELECT prefval FROM %s ".
76 "WHERE user='%s' AND prefkey='%s'",
77 $this->table,
78 $this->dbh->quoteString($user),
79 $this->dbh->quoteString($key));
80
81 $res = $this->dbh->query($query);
82 if(DB::isError($res))
83 $this->failQuery($res);
84
85 if($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
86 return $row['prefval'];
87 } else {
88 if(isset($this->default[$key])) {
89 return $this->default[$key];
90 } else {
91 return '';
92 }
93 }
94
95 return '';
96 }
97
98 function deleteKey($user, $key) {
99 $this->open();
100 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
101 $this->table,
102 $this->dbh->quoteString($user),
103 $this->dbh->quoteString($key));
104
105 $res = $this->dbh->simpleQuery($query);
106 if(DB::isError($res))
107 $this->failQuery($res);
108
109 if(substr($key, 0, 9) == 'highlight') {
110 $this->renumberHighlightList($user);
111 }
112
113 return true;
114 }
115
116 function setKey($user, $key, $value) {
117 $this->open();
118 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
119 "VALUES('%s','%s','%s')",
120 $this->table,
121 $this->dbh->quoteString($user),
122 $this->dbh->quoteString($key),
123 $this->dbh->quoteString($value));
124
125 $res = $this->dbh->simpleQuery($query);
126 if(DB::isError($res))
127 $this->failQuery($res);
128
129 return true;
130 }
131
132
133 /**
134 ** When a highlight option is deleted the preferences module
135 ** must renumber the list. This should be done somewhere else,
136 ** but it is not, so....
137 **/
138 function renumberHighlightList($user) {
139 $this->open();
140 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
141 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
142 $this->table,
143 $this->dbh->quoteString($user));
144
145 $res = $this->dbh->query($query);
146 if(DB::isError($res))
147 $this->failQuery($res);
148
149 // Store old data in array
150 $rows = Array();
151 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
152 $rows[] = $row;
153
154 // Renumber keys of old data
155 $hilinum = 0;
156 for($i = 0; $i < count($rows) ; $i++) {
157 $oldkey = $rows[$i]['prefkey'];
158 $newkey = substr($oldkey, 0, 9) . $hilinum;
159 $hilinum++;
160
161 if($oldkey != $newkey) {
162 $query = sprintf("UPDATE %s SET prefkey='%s' WHERE user='%s' ".
163 "AND prefkey='%s'",
164 $this->table,
165 $this->dbh->quoteString($newkey),
166 $this->dbh->quoteString($user),
167 $this->dbh->quoteString($oldkey));
168
169 $res = $this->dbh->simpleQuery($query);
170 if(DB::isError($res))
171 $this->failQuery($res);
172 }
173 }
174
175 return;
176 }
177
178 } // end class dbPrefs
179
180
181 /** returns the value for the pref $string **/
182 function getPref($data_dir, $username, $string, $default ) {
183 $db = new dbPrefs;
184 if(isset($db->error)) {
185 printf( _("Preference database error (%s). Exiting abnormally"),
186 $db->error);
187 exit;
188 }
189
190 return $db->getKey($username, $string);
191 }
192
193 /** Remove the pref $string **/
194 function removePref($data_dir, $username, $string) {
195 $db = new dbPrefs;
196 if(isset($db->error)) $db->failQuery();
197
198 $db->deleteKey($username, $string);
199 return;
200 }
201
202 /** sets the pref, $string, to $set_to **/
203 function setPref($data_dir, $username, $string, $set_to) {
204 $db = new dbPrefs;
205 if(isset($db->error))
206 $db->failQuery();
207
208 $db->setKey($username, $string, $set_to);
209 return;
210 }
211
212 /** This checks if the prefs are available **/
213 function checkForPrefs($data_dir, $username) {
214 $db = new dbPrefs;
215 if(isset($db->error))
216 $db->failQuery();
217 }
218
219 /** Writes the Signature **/
220 function setSig($data_dir, $username, $string) {
221 $db = new dbPrefs;
222 if(isset($db->error))
223 $db->failQuery();
224
225 $db->setKey($username, "___signature___", $string);
226 return;
227 }
228
229 /** Gets the signature **/
230 function getSig($data_dir, $username) {
231 $db = new dbPrefs;
232 if(isset($db->error))
233 $db->failQuery();
234
235 return $db->getKey($username, "___signature___");
236 }
237
238 ?>