I should really have checked if the database is opened before using it...
[squirrelmail.git] / functions / db_prefs.php
1 <?php
2
3 /*
4 * db_prefs.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains functions for manipulating user preferences
10 * stored in a database, accessed though the Pear DB layer.
11 *
12 * To use this instead of the regular prefs.php, create a
13 * database as described below, and replace prefs.php
14 * with this file.
15 *
16 * Database:
17 * ---------
18 *
19 * The preferences table should have tree columns:
20 * username char \ primary
21 * prefkey char / key
22 * prefval blob
23 *
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));
28 *
29 * Configuration of databasename, username and password is done
30 * by using conf.pl or the administrator plugin
31 *
32 * $Id$
33 */
34
35 require_once('DB.php');
36 require_once('../config/config.php');
37
38 global $prefs_are_cached, $prefs_cache;
39
40 function cachePrefValues($username) {
41 global $prefs_are_cached, $prefs_cache;
42
43 if ($prefs_are_cached) {
44 return;
45 }
46
47 session_unregister('prefs_cache');
48 session_unregister('prefs_are_cached');
49
50 $db = new dbPrefs;
51 if(isset($db->error)) {
52 printf( _("Preference database error (%s). Exiting abnormally"),
53 $db->error);
54 exit;
55 }
56
57 $db->fillPrefsCache($username);
58 if (isset($db->error)) {
59 printf( _("Preference database error (%s). Exiting abnormally"),
60 $db->error);
61 exit;
62 }
63
64 $prefs_are_cached = true;
65
66 session_register('prefs_cache');
67 session_register('prefs_are_cached');
68 }
69
70 class dbPrefs {
71 var $table = 'userprefs';
72
73 var $dbh = NULL;
74 var $error = NULL;
75
76 var $default = Array('chosen_theme' => '../themes/default_theme.php',
77 'show_html_default' => '0');
78
79 function open() {
80 global $prefs_dsn, $prefs_table;
81
82 if(isset($this->dbh)) {
83 return true;
84 }
85
86 if (!empty($prefs_table)) {
87 $this->table = $prefs_table;
88 }
89 $dbh = DB::connect($prefs_dsn);
90
91 if(DB::isError($dbh) || DB::isWarning($dbh)) {
92 $this->error = DB::errorMessage($dbh);
93 return false;
94 }
95
96 $this->dbh = $dbh;
97 return true;
98 }
99
100 function failQuery($res = NULL) {
101 if($res == NULL) {
102 printf(_("Preference database error (%s). Exiting abnormally"),
103 $this->error);
104 } else {
105 printf(_("Preference database error (%s). Exiting abnormally"),
106 DB::errorMessage($res));
107 }
108 exit;
109 }
110
111
112 function getKey($user, $key, $default = '') {
113 global $prefs_cache;
114
115 cachePrefValues($user);
116
117 if (isset($prefs_cache[$key])) {
118 return $prefs_cache[$key];
119 } else {
120 if (isset($this->default[$key])) {
121 return $this->default[$key];
122 } else {
123 return $default;
124 }
125 }
126 }
127
128 function deleteKey($user, $key) {
129 global $prefs_cache;
130
131 if (!$this->open()) {
132 return false;
133 }
134 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
135 $this->table,
136 $this->dbh->quoteString($user),
137 $this->dbh->quoteString($key));
138
139 $res = $this->dbh->simpleQuery($query);
140 if(DB::isError($res)) {
141 $this->failQuery($res);
142 }
143
144 unset($prefs_cache[$key]);
145
146 if(substr($key, 0, 9) == 'highlight') {
147 $this->renumberHighlightList($user);
148 }
149
150 return true;
151 }
152
153 function setKey($user, $key, $value) {
154 if (!$this->open()) {
155 return false;
156 }
157 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
158 "VALUES('%s','%s','%s')",
159 $this->table,
160 $this->dbh->quoteString($user),
161 $this->dbh->quoteString($key),
162 $this->dbh->quoteString($value));
163
164 $res = $this->dbh->simpleQuery($query);
165 if(DB::isError($res)) {
166 $this->failQuery($res);
167 }
168
169 return true;
170 }
171
172 function fillPrefsCache($user) {
173 global $prefs_cache;
174
175 if (!$this->open()) {
176 return;
177 }
178
179 $prefs_cache = array();
180 $query = sprintf("SELECT prefkey, prefval FROM %s ".
181 "WHERE user = '%s'",
182 $this->table,
183 $this->dbh->quoteString($user));
184 $res = $this->dbh->query($query);
185 if (DB::isError($res)) {
186 $this->failQuery($res);
187 }
188
189 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
190 $prefs_cache[$row['prefkey']] = $row['prefval'];
191 }
192 }
193
194 /*
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....
198 */
199 function renumberHighlightList($user) {
200 if (!$this->open()) {
201 return;
202 }
203 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
204 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
205 $this->table,
206 $this->dbh->quoteString($user));
207
208 $res = $this->dbh->query($query);
209 if(DB::isError($res)) {
210 $this->failQuery($res);
211 }
212
213 /* Store old data in array */
214 $rows = Array();
215 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
216 $rows[] = $row;
217 }
218
219 /* Renumber keys of old data */
220 $hilinum = 0;
221 for($i = 0; $i < count($rows) ; $i++) {
222 $oldkey = $rows[$i]['prefkey'];
223 $newkey = substr($oldkey, 0, 9) . $hilinum;
224 $hilinum++;
225
226 if($oldkey != $newkey) {
227 $query = sprintf("UPDATE %s SET prefkey='%s' ".
228 "WHERE user ='%s' AND prefkey='%s'",
229 $this->table,
230 $this->dbh->quoteString($newkey),
231 $this->dbh->quoteString($user),
232 $this->dbh->quoteString($oldkey));
233
234 $res = $this->dbh->simpleQuery($query);
235 if(DB::isError($res)) {
236 $this->failQuery($res);
237 }
238 }
239 }
240
241 return;
242 }
243
244 } /* end class dbPrefs */
245
246
247 /* returns the value for the pref $string */
248 function getPref($data_dir, $username, $string, $default = '') {
249 $db = new dbPrefs;
250 if(isset($db->error)) {
251 printf( _("Preference database error (%s). Exiting abnormally"),
252 $db->error);
253 exit;
254 }
255
256 return $db->getKey($username, $string, $default);
257 }
258
259 /* Remove the pref $string */
260 function removePref($data_dir, $username, $string) {
261 $db = new dbPrefs;
262 if(isset($db->error)) {
263 $db->failQuery();
264 }
265
266 $db->deleteKey($username, $string);
267 return;
268 }
269
270 /* sets the pref, $string, to $set_to */
271 function setPref($data_dir, $username, $string, $set_to) {
272 global $prefs_cache;
273
274 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
275 return;
276 }
277
278 if ($set_to == '') {
279 removePref($data_dir, $username, $string);
280 return;
281 }
282
283 $db = new dbPrefs;
284 if(isset($db->error)) {
285 $db->failQuery();
286 }
287
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]');
293
294 return;
295 }
296
297 /* This checks if the prefs are available */
298 function checkForPrefs($data_dir, $username) {
299 $db = new dbPrefs;
300 if(isset($db->error)) {
301 $db->failQuery();
302 }
303 }
304
305 /* Writes the Signature */
306 function setSig($data_dir, $username, $string) {
307 $db = new dbPrefs;
308 if(isset($db->error)) {
309 $db->failQuery();
310 }
311
312 $db->setKey($username, '___signature___', $string);
313 return;
314 }
315
316 /* Gets the signature */
317 function getSig($data_dir, $username) {
318 $db = new dbPrefs;
319 if(isset($db->error)) {
320 $db->failQuery();
321 }
322
323 return $db->getKey($username, '___signature___');
324 }
325
326 ?>