In stead of just crashing, give a proper error when PEAR DB functions could
[squirrelmail.git] / functions / db_prefs.php
1 <?php
2
3 /**
4 * db_prefs.php
5 *
6 * Copyright (c) 1999-2004 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 * Database:
13 * ---------
14 *
15 * The preferences table should have three columns:
16 * user char \ primary
17 * prefkey char / key
18 * prefval blob
19 *
20 * CREATE TABLE userprefs (user CHAR(128) 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 using conf.pl or the administrator plugin
27 *
28 * @version $Id$
29 * @package squirrelmail
30 */
31
32 /** Unknown database */
33 define('SMDB_UNKNOWN', 0);
34 /** MySQL */
35 define('SMDB_MYSQL', 1);
36 /** PostgreSQL */
37 define('SMDB_PGSQL', 2);
38
39 require_once(SM_PATH . 'config/config.php');
40 if (!include_once('DB.php')) {
41 // same error also in abook_database.php
42 require_once(SM_PATH . 'functions/display_messages.php');
43 $error = _("Could not include PEAR database functions required for the database backend.") . "<br />\n";
44 $error .= _("Please contact your system administrator and report this error.");
45 error_box($error, $color);
46 }
47
48 global $prefs_are_cached, $prefs_cache;
49
50 /**
51 * @ignore
52 */
53 function cachePrefValues($username) {
54 global $prefs_are_cached, $prefs_cache;
55
56 if ($prefs_are_cached) {
57 return;
58 }
59
60 sqsession_unregister('prefs_cache');
61 sqsession_unregister('prefs_are_cached');
62
63 $db = new dbPrefs;
64 if(isset($db->error)) {
65 printf( _("Preference database error (%s). Exiting abnormally"),
66 $db->error);
67 exit;
68 }
69
70 $db->fillPrefsCache($username);
71 if (isset($db->error)) {
72 printf( _("Preference database error (%s). Exiting abnormally"),
73 $db->error);
74 exit;
75 }
76
77 $prefs_are_cached = true;
78
79 sqsession_register($prefs_cache, 'prefs_cache');
80 sqsession_register($prefs_are_cached, 'prefs_are_cached');
81 }
82
83 /**
84 * Completely undocumented class - someone document it!
85 * @package squirrelmail
86 */
87 class dbPrefs {
88 var $table = 'userprefs';
89 var $user_field = 'user';
90 var $key_field = 'prefkey';
91 var $val_field = 'prefval';
92
93 var $dbh = NULL;
94 var $error = NULL;
95 var $db_type = SMDB_UNKNOWN;
96
97 var $default = Array('theme_default' => 0,
98 'show_html_default' => '0');
99
100 function open() {
101 global $prefs_dsn, $prefs_table;
102 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
103
104 if(isset($this->dbh)) {
105 return true;
106 }
107
108 if (preg_match('/^mysql/', $prefs_dsn)) {
109 $this->db_type = SMDB_MYSQL;
110 } elseif (preg_match('/^pgsql/', $prefs_dsn)) {
111 $this->db_type = SMDB_PGSQL;
112 }
113
114 if (!empty($prefs_table)) {
115 $this->table = $prefs_table;
116 }
117 if (!empty($prefs_user_field)) {
118 $this->user_field = $prefs_user_field;
119 }
120 if (!empty($prefs_key_field)) {
121 $this->key_field = $prefs_key_field;
122 }
123 if (!empty($prefs_val_field)) {
124 $this->val_field = $prefs_val_field;
125 }
126 $dbh = DB::connect($prefs_dsn, true);
127
128 if(DB::isError($dbh)) {
129 $this->error = DB::errorMessage($dbh);
130 return false;
131 }
132
133 $this->dbh = $dbh;
134 return true;
135 }
136
137 function failQuery($res = NULL) {
138 if($res == NULL) {
139 printf(_("Preference database error (%s). Exiting abnormally"),
140 $this->error);
141 } else {
142 printf(_("Preference database error (%s). Exiting abnormally"),
143 DB::errorMessage($res));
144 }
145 exit;
146 }
147
148
149 function getKey($user, $key, $default = '') {
150 global $prefs_cache;
151
152 cachePrefValues($user);
153
154 if (isset($prefs_cache[$key])) {
155 return $prefs_cache[$key];
156 } else {
157 if (isset($this->default[$key])) {
158 return $this->default[$key];
159 } else {
160 return $default;
161 }
162 }
163 }
164
165 function deleteKey($user, $key) {
166 global $prefs_cache;
167
168 if (!$this->open()) {
169 return false;
170 }
171 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
172 $this->table,
173 $this->user_field,
174 $this->dbh->quoteString($user),
175 $this->key_field,
176 $this->dbh->quoteString($key));
177
178 $res = $this->dbh->simpleQuery($query);
179 if(DB::isError($res)) {
180 $this->failQuery($res);
181 }
182
183 unset($prefs_cache[$key]);
184
185 return true;
186 }
187
188 function setKey($user, $key, $value) {
189 if (!$this->open()) {
190 return false;
191 }
192 if ($this->db_type == SMDB_MYSQL) {
193 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
194 "VALUES('%s','%s','%s')",
195 $this->table,
196 $this->user_field,
197 $this->key_field,
198 $this->val_field,
199 $this->dbh->quoteString($user),
200 $this->dbh->quoteString($key),
201 $this->dbh->quoteString($value));
202
203 $res = $this->dbh->simpleQuery($query);
204 if(DB::isError($res)) {
205 $this->failQuery($res);
206 }
207 } elseif ($this->db_type == SMDB_PGSQL) {
208 $this->dbh->simpleQuery("BEGIN TRANSACTION");
209 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
210 $this->table,
211 $this->user_field,
212 $this->dbh->quoteString($user),
213 $this->key_field,
214 $this->dbh->quoteString($key));
215 $res = $this->dbh->simpleQuery($query);
216 if (DB::isError($res)) {
217 $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
218 $this->failQuery($res);
219 }
220 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
221 $this->table,
222 $this->user_field,
223 $this->key_field,
224 $this->val_field,
225 $this->dbh->quoteString($user),
226 $this->dbh->quoteString($key),
227 $this->dbh->quoteString($value));
228 $res = $this->dbh->simpleQuery($query);
229 if (DB::isError($res)) {
230 $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
231 $this->failQuery($res);
232 }
233 $this->dbh->simpleQuery("COMMIT TRANSACTION");
234 } else {
235 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
236 $this->table,
237 $this->user_field,
238 $this->dbh->quoteString($user),
239 $this->key_field,
240 $this->dbh->quoteString($key));
241 $res = $this->dbh->simpleQuery($query);
242 if (DB::isError($res)) {
243 $this->failQuery($res);
244 }
245 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
246 $this->table,
247 $this->user_field,
248 $this->key_field,
249 $this->val_field,
250 $this->dbh->quoteString($user),
251 $this->dbh->quoteString($key),
252 $this->dbh->quoteString($value));
253 $res = $this->dbh->simpleQuery($query);
254 if (DB::isError($res)) {
255 $this->failQuery($res);
256 }
257 }
258
259 return true;
260 }
261
262 function fillPrefsCache($user) {
263 global $prefs_cache;
264
265 if (!$this->open()) {
266 return;
267 }
268
269 $prefs_cache = array();
270 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
271 "WHERE %s = '%s'",
272 $this->key_field,
273 $this->val_field,
274 $this->table,
275 $this->user_field,
276 $this->dbh->quoteString($user));
277 $res = $this->dbh->query($query);
278 if (DB::isError($res)) {
279 $this->failQuery($res);
280 }
281
282 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
283 $prefs_cache[$row['prefkey']] = $row['prefval'];
284 }
285 }
286
287 } /* end class dbPrefs */
288
289
290 /**
291 * returns the value for the pref $string
292 * @ignore
293 */
294 function getPref($data_dir, $username, $string, $default = '') {
295 $db = new dbPrefs;
296 if(isset($db->error)) {
297 printf( _("Preference database error (%s). Exiting abnormally"),
298 $db->error);
299 exit;
300 }
301
302 return $db->getKey($username, $string, $default);
303 }
304
305 /**
306 * Remove the pref $string
307 * @ignore
308 */
309 function removePref($data_dir, $username, $string) {
310 global $prefs_cache;
311 $db = new dbPrefs;
312 if(isset($db->error)) {
313 $db->failQuery();
314 }
315
316 $db->deleteKey($username, $string);
317
318 if (isset($prefs_cache[$string])) {
319 unset($prefs_cache[$string]);
320 }
321
322 sqsession_register($prefs_cache , 'prefs_cache');
323 return;
324 }
325
326 /**
327 * sets the pref, $string, to $set_to
328 * @ignore
329 */
330 function setPref($data_dir, $username, $string, $set_to) {
331 global $prefs_cache;
332
333 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
334 return;
335 }
336
337 if ($set_to === '') {
338 removePref($data_dir, $username, $string);
339 return;
340 }
341
342 $db = new dbPrefs;
343 if(isset($db->error)) {
344 $db->failQuery();
345 }
346
347 $db->setKey($username, $string, $set_to);
348 $prefs_cache[$string] = $set_to;
349 assert_options(ASSERT_ACTIVE, 1);
350 assert_options(ASSERT_BAIL, 1);
351 assert ('$set_to == $prefs_cache[$string]');
352 sqsession_register($prefs_cache , 'prefs_cache');
353 return;
354 }
355
356 /**
357 * This checks if the prefs are available
358 * @ignore
359 */
360 function checkForPrefs($data_dir, $username) {
361 $db = new dbPrefs;
362 if(isset($db->error)) {
363 $db->failQuery();
364 }
365 }
366
367 /**
368 * Writes the Signature
369 * @ignore
370 */
371 function setSig($data_dir, $username, $number, $string) {
372 if ($number == "g") {
373 $key = '___signature___';
374 } else {
375 $key = sprintf('___sig%s___', $number);
376 }
377 setPref($data_dir, $username, $key, $string);
378 return;
379 }
380
381 /**
382 * Gets the signature
383 * @ignore
384 */
385 function getSig($data_dir, $username, $number) {
386 if ($number == "g") {
387 $key = '___signature___';
388 } else {
389 $key = sprintf('___sig%d___', $number);
390 }
391 return getPref($data_dir, $username, $key);
392 }
393
394 // vim: et ts=4
395 ?>