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