fe1d4253e764f6cfa7b1a0723747bae7e3e5a876
[squirrelmail.git] / functions / db_prefs.php
1 <?php
2
3 /**
4 * db_prefs.php
5 *
6 * Copyright (c) 1999-2005 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 * @subpackage prefs
31 * @since 1.1.3
32 */
33
34 /** @ignore */
35 if (!defined('SM_PATH')) define('SM_PATH','../');
36
37 /** Unknown database */
38 define('SMDB_UNKNOWN', 0);
39 /** MySQL */
40 define('SMDB_MYSQL', 1);
41 /** PostgreSQL */
42 define('SMDB_PGSQL', 2);
43
44 require_once(SM_PATH . 'config/config.php');
45 if (!include_once('DB.php')) {
46 // same error also in abook_database.php
47 require_once(SM_PATH . 'functions/display_messages.php');
48 $error = _("Could not include PEAR database functions required for the database backend.") . "<br />\n";
49 $error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
50 '<tt>DB.php</tt>') . "<br />\n";
51 $error .= _("Please contact your system administrator and report this error.");
52 error_box($error, $color);
53 exit;
54 }
55
56 global $prefs_are_cached, $prefs_cache;
57
58 /**
59 * @ignore
60 */
61 function cachePrefValues($username) {
62 global $prefs_are_cached, $prefs_cache;
63
64 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
65 if ($prefs_are_cached) {
66 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
67 return;
68 }
69
70 sqsession_unregister('prefs_cache');
71 sqsession_unregister('prefs_are_cached');
72
73 $db = new dbPrefs;
74 if(isset($db->error)) {
75 printf( _("Preference database error (%s). Exiting abnormally"),
76 $db->error);
77 exit;
78 }
79
80 $db->fillPrefsCache($username);
81 if (isset($db->error)) {
82 printf( _("Preference database error (%s). Exiting abnormally"),
83 $db->error);
84 exit;
85 }
86
87 $prefs_are_cached = true;
88
89 sqsession_register($prefs_cache, 'prefs_cache');
90 sqsession_register($prefs_are_cached, 'prefs_are_cached');
91 }
92
93 /**
94 * Class used to handle connections to prefs database and operations with preferences
95 * @package squirrelmail
96 * @subpackage prefs
97 * @since 1.1.3
98 */
99 class dbPrefs {
100 /**
101 * Table used to store preferences
102 * @var string
103 */
104 var $table = 'userprefs';
105 /**
106 * Field used to store owner of preference
107 * @var string
108 */
109 var $user_field = 'user';
110 /**
111 * Field used to store preference name
112 * @var string
113 */
114 var $key_field = 'prefkey';
115 /**
116 * Field used to store preference value
117 * @var string
118 */
119 var $val_field = 'prefval';
120
121 /**
122 * Database connection object
123 * @var object
124 */
125 var $dbh = NULL;
126 /**
127 * Error messages
128 * @var string
129 */
130 var $error = NULL;
131 /**
132 * Database type (SMDB_* constants)
133 * Is used in setKey().
134 * @var integer
135 */
136 var $db_type = SMDB_UNKNOWN;
137
138 /**
139 * Default preferences
140 * @var array
141 */
142 var $default = Array('theme_default' => 0,
143 'show_html_default' => '0');
144
145 /**
146 * initialize DB connection object
147 * @return boolean true, if object is initialized
148 */
149 function open() {
150 global $prefs_dsn, $prefs_table;
151 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
152
153 if(isset($this->dbh)) {
154 return true;
155 }
156
157 if (preg_match('/^mysql/', $prefs_dsn)) {
158 $this->db_type = SMDB_MYSQL;
159 } elseif (preg_match('/^pgsql/', $prefs_dsn)) {
160 $this->db_type = SMDB_PGSQL;
161 }
162
163 if (!empty($prefs_table)) {
164 $this->table = $prefs_table;
165 }
166 if (!empty($prefs_user_field)) {
167 $this->user_field = $prefs_user_field;
168 }
169 if (!empty($prefs_key_field)) {
170 $this->key_field = $prefs_key_field;
171 }
172 if (!empty($prefs_val_field)) {
173 $this->val_field = $prefs_val_field;
174 }
175 $dbh = DB::connect($prefs_dsn, true);
176
177 if(DB::isError($dbh)) {
178 $this->error = DB::errorMessage($dbh);
179 return false;
180 }
181
182 $this->dbh = $dbh;
183 return true;
184 }
185
186 /**
187 * Function used to handle database connection errors
188 * @param object PEAR Error object
189 */
190 function failQuery($res = NULL) {
191 if($res == NULL) {
192 printf(_("Preference database error (%s). Exiting abnormally"),
193 $this->error);
194 } else {
195 printf(_("Preference database error (%s). Exiting abnormally"),
196 DB::errorMessage($res));
197 }
198 exit;
199 }
200
201 /**
202 * Get user's prefs setting
203 * @param string $user user name
204 * @param string $key preference name
205 * @param mixed $default (since 1.2.5) default value
206 * @return mixed preference value
207 */
208 function getKey($user, $key, $default = '') {
209 global $prefs_cache;
210
211 cachePrefValues($user);
212
213 if (isset($prefs_cache[$key])) {
214 return $prefs_cache[$key];
215 } else {
216 if (isset($this->default[$key])) {
217 return $this->default[$key];
218 } else {
219 return $default;
220 }
221 }
222 }
223
224 /**
225 * Delete user's prefs setting
226 * @param string $user user name
227 * @param string $key preference name
228 * @return boolean
229 */
230 function deleteKey($user, $key) {
231 global $prefs_cache;
232
233 if (!$this->open()) {
234 return false;
235 }
236 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
237 $this->table,
238 $this->user_field,
239 $this->dbh->quoteString($user),
240 $this->key_field,
241 $this->dbh->quoteString($key));
242
243 $res = $this->dbh->simpleQuery($query);
244 if(DB::isError($res)) {
245 $this->failQuery($res);
246 }
247
248 unset($prefs_cache[$key]);
249
250 return true;
251 }
252
253 /**
254 * Set user's preference
255 * @param string $user user name
256 * @param string $key preference name
257 * @param mixed $value preference value
258 * @return boolean
259 */
260 function setKey($user, $key, $value) {
261 if (!$this->open()) {
262 return false;
263 }
264 if ($this->db_type == SMDB_MYSQL) {
265 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
266 "VALUES('%s','%s','%s')",
267 $this->table,
268 $this->user_field,
269 $this->key_field,
270 $this->val_field,
271 $this->dbh->quoteString($user),
272 $this->dbh->quoteString($key),
273 $this->dbh->quoteString($value));
274
275 $res = $this->dbh->simpleQuery($query);
276 if(DB::isError($res)) {
277 $this->failQuery($res);
278 }
279 } elseif ($this->db_type == SMDB_PGSQL) {
280 $this->dbh->simpleQuery("BEGIN TRANSACTION");
281 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
282 $this->table,
283 $this->user_field,
284 $this->dbh->quoteString($user),
285 $this->key_field,
286 $this->dbh->quoteString($key));
287 $res = $this->dbh->simpleQuery($query);
288 if (DB::isError($res)) {
289 $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
290 $this->failQuery($res);
291 }
292 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
293 $this->table,
294 $this->user_field,
295 $this->key_field,
296 $this->val_field,
297 $this->dbh->quoteString($user),
298 $this->dbh->quoteString($key),
299 $this->dbh->quoteString($value));
300 $res = $this->dbh->simpleQuery($query);
301 if (DB::isError($res)) {
302 $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
303 $this->failQuery($res);
304 }
305 $this->dbh->simpleQuery("COMMIT TRANSACTION");
306 } else {
307 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
308 $this->table,
309 $this->user_field,
310 $this->dbh->quoteString($user),
311 $this->key_field,
312 $this->dbh->quoteString($key));
313 $res = $this->dbh->simpleQuery($query);
314 if (DB::isError($res)) {
315 $this->failQuery($res);
316 }
317 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
318 $this->table,
319 $this->user_field,
320 $this->key_field,
321 $this->val_field,
322 $this->dbh->quoteString($user),
323 $this->dbh->quoteString($key),
324 $this->dbh->quoteString($value));
325 $res = $this->dbh->simpleQuery($query);
326 if (DB::isError($res)) {
327 $this->failQuery($res);
328 }
329 }
330
331 return true;
332 }
333
334 /**
335 * Fill preference cache array
336 * @param string $user user name
337 * @since 1.2.3
338 */
339 function fillPrefsCache($user) {
340 global $prefs_cache;
341
342 if (!$this->open()) {
343 return;
344 }
345
346 $prefs_cache = array();
347 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
348 "WHERE %s = '%s'",
349 $this->key_field,
350 $this->val_field,
351 $this->table,
352 $this->user_field,
353 $this->dbh->quoteString($user));
354 $res = $this->dbh->query($query);
355 if (DB::isError($res)) {
356 $this->failQuery($res);
357 }
358
359 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
360 $prefs_cache[$row['prefkey']] = $row['prefval'];
361 }
362 }
363
364 } /* end class dbPrefs */
365
366
367 /**
368 * returns the value for the pref $string
369 * @ignore
370 */
371 function getPref($data_dir, $username, $string, $default = '') {
372 $db = new dbPrefs;
373 if(isset($db->error)) {
374 printf( _("Preference database error (%s). Exiting abnormally"),
375 $db->error);
376 exit;
377 }
378
379 return $db->getKey($username, $string, $default);
380 }
381
382 /**
383 * Remove the pref $string
384 * @ignore
385 */
386 function removePref($data_dir, $username, $string) {
387 global $prefs_cache;
388 $db = new dbPrefs;
389 if(isset($db->error)) {
390 $db->failQuery();
391 }
392
393 $db->deleteKey($username, $string);
394
395 if (isset($prefs_cache[$string])) {
396 unset($prefs_cache[$string]);
397 }
398
399 sqsession_register($prefs_cache , 'prefs_cache');
400 return;
401 }
402
403 /**
404 * sets the pref, $string, to $set_to
405 * @ignore
406 */
407 function setPref($data_dir, $username, $string, $set_to) {
408 global $prefs_cache;
409
410 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
411 return;
412 }
413
414 if ($set_to === '') {
415 removePref($data_dir, $username, $string);
416 return;
417 }
418
419 $db = new dbPrefs;
420 if(isset($db->error)) {
421 $db->failQuery();
422 }
423
424 $db->setKey($username, $string, $set_to);
425 $prefs_cache[$string] = $set_to;
426 assert_options(ASSERT_ACTIVE, 1);
427 assert_options(ASSERT_BAIL, 1);
428 assert ('$set_to == $prefs_cache[$string]');
429 sqsession_register($prefs_cache , 'prefs_cache');
430 return;
431 }
432
433 /**
434 * This checks if the prefs are available
435 * @ignore
436 */
437 function checkForPrefs($data_dir, $username) {
438 $db = new dbPrefs;
439 if(isset($db->error)) {
440 $db->failQuery();
441 }
442 }
443
444 /**
445 * Writes the Signature
446 * @ignore
447 */
448 function setSig($data_dir, $username, $number, $string) {
449 if ($number == "g") {
450 $key = '___signature___';
451 } else {
452 $key = sprintf('___sig%s___', $number);
453 }
454 setPref($data_dir, $username, $key, $string);
455 return;
456 }
457
458 /**
459 * Gets the signature
460 * @ignore
461 */
462 function getSig($data_dir, $username, $number) {
463 if ($number == "g") {
464 $key = '___signature___';
465 } else {
466 $key = sprintf('___sig%d___', $number);
467 }
468 return getPref($data_dir, $username, $key);
469 }
470
471 // vim: et ts=4
472 ?>