validator.w3.org fails if id attribute contains brackets
[squirrelmail.git] / functions / db_prefs.php
1 <?php
2
3 /**
4 * db_prefs.php
5 *
6 * This contains functions for manipulating user preferences
7 * stored in a database, accessed though the Pear DB layer.
8 *
9 * Database:
10 *
11 * The preferences table should have three columns:
12 * user char \ primary
13 * prefkey char / key
14 * prefval blob
15 *
16 * CREATE TABLE userprefs (user CHAR(128) NOT NULL DEFAULT '',
17 * prefkey CHAR(64) NOT NULL DEFAULT '',
18 * prefval BLOB NOT NULL DEFAULT '',
19 * primary key (user,prefkey));
20 *
21 * Configuration of databasename, username and password is done
22 * by using conf.pl or the administrator plugin
23 *
24 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
25 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
26 * @version $Id$
27 * @package squirrelmail
28 * @subpackage prefs
29 * @since 1.1.3
30 */
31
32 /** @ignore */
33 if (!defined('SM_PATH')) define('SM_PATH','../');
34
35 /** Unknown database */
36 define('SMDB_UNKNOWN', 0);
37 /** MySQL */
38 define('SMDB_MYSQL', 1);
39 /** PostgreSQL */
40 define('SMDB_PGSQL', 2);
41
42
43 if (!include_once('DB.php')) {
44 // same error also in abook_database.php
45 $error = _("Could not include PEAR database functions required for the database backend.") . "<br />\n";
46 $error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
47 '<tt>DB.php</tt>') . "<br />\n";
48 $error .= _("Please contact your system administrator and report this error.");
49 error_box($error, $color);
50 exit;
51 }
52
53 global $prefs_are_cached, $prefs_cache;
54
55 /**
56 * @ignore
57 */
58 function cachePrefValues($username) {
59 global $prefs_are_cached, $prefs_cache;
60
61 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
62 if ($prefs_are_cached) {
63 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
64 return;
65 }
66
67 sqsession_unregister('prefs_cache');
68 sqsession_unregister('prefs_are_cached');
69
70 $db = new dbPrefs;
71 if(isset($db->error)) {
72 printf( _("Preference database error (%s). Exiting abnormally"),
73 $db->error);
74 exit;
75 }
76
77 $db->fillPrefsCache($username);
78 if (isset($db->error)) {
79 printf( _("Preference database error (%s). Exiting abnormally"),
80 $db->error);
81 exit;
82 }
83
84 $prefs_are_cached = true;
85
86 sqsession_register($prefs_cache, 'prefs_cache');
87 sqsession_register($prefs_are_cached, 'prefs_are_cached');
88 }
89
90 /**
91 * Class used to handle connections to prefs database and operations with preferences
92 * @package squirrelmail
93 * @subpackage prefs
94 * @since 1.1.3
95 */
96 class dbPrefs {
97 /**
98 * Table used to store preferences
99 * @var string
100 */
101 var $table = 'userprefs';
102 /**
103 * Field used to store owner of preference
104 * @var string
105 */
106 var $user_field = 'user';
107 /**
108 * Field used to store preference name
109 * @var string
110 */
111 var $key_field = 'prefkey';
112 /**
113 * Field used to store preference value
114 * @var string
115 */
116 var $val_field = 'prefval';
117
118 /**
119 * Database connection object
120 * @var object
121 */
122 var $dbh = NULL;
123 /**
124 * Error messages
125 * @var string
126 */
127 var $error = NULL;
128 /**
129 * Database type (SMDB_* constants)
130 * Is used in setKey().
131 * @var integer
132 */
133 var $db_type = SMDB_UNKNOWN;
134
135 /**
136 * Default preferences
137 * @var array
138 */
139 var $default = Array('theme_default' => 0,
140 'show_html_default' => '0');
141
142 /**
143 * Preference owner field size
144 * @var integer
145 * @since 1.5.1
146 */
147 var $user_size = 128;
148 /**
149 * Preference key field size
150 * @var integer
151 * @since 1.5.1
152 */
153 var $key_size = 64;
154 /**
155 * Preference value field size
156 * @var integer
157 * @since 1.5.1
158 */
159 var $val_size = 65536;
160
161 /**
162 * initialize DB connection object
163 * @return boolean true, if object is initialized
164 */
165 function open() {
166 global $prefs_dsn, $prefs_table;
167 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
168 global $prefs_user_size, $prefs_key_size, $prefs_val_size;
169
170 if(isset($this->dbh)) {
171 return true;
172 }
173
174 if (preg_match('/^mysql/', $prefs_dsn)) {
175 $this->db_type = SMDB_MYSQL;
176 } elseif (preg_match('/^pgsql/', $prefs_dsn)) {
177 $this->db_type = SMDB_PGSQL;
178 }
179
180 if (!empty($prefs_table)) {
181 $this->table = $prefs_table;
182 }
183 if (!empty($prefs_user_field)) {
184 $this->user_field = $prefs_user_field;
185 }
186 if (!empty($prefs_key_field)) {
187 $this->key_field = $prefs_key_field;
188 }
189 if (!empty($prefs_val_field)) {
190 $this->val_field = $prefs_val_field;
191 }
192 if (!empty($prefs_user_size)) {
193 $this->user_size = (int) $prefs_user_size;
194 }
195 if (!empty($prefs_key_size)) {
196 $this->key_size = (int) $prefs_key_size;
197 }
198 if (!empty($prefs_val_size)) {
199 $this->val_size = (int) $prefs_val_size;
200 }
201 $dbh = DB::connect($prefs_dsn, true);
202
203 if(DB::isError($dbh)) {
204 $this->error = DB::errorMessage($dbh);
205 return false;
206 }
207
208 $this->dbh = $dbh;
209 return true;
210 }
211
212 /**
213 * Function used to handle database connection errors
214 * @param object PEAR Error object
215 */
216 function failQuery($res = NULL) {
217 if($res == NULL) {
218 printf(_("Preference database error (%s). Exiting abnormally"),
219 $this->error);
220 } else {
221 printf(_("Preference database error (%s). Exiting abnormally"),
222 DB::errorMessage($res));
223 }
224 exit;
225 }
226
227 /**
228 * Get user's prefs setting
229 * @param string $user user name
230 * @param string $key preference name
231 * @param mixed $default (since 1.2.5) default value
232 * @return mixed preference value
233 */
234 function getKey($user, $key, $default = '') {
235 global $prefs_cache;
236
237 cachePrefValues($user);
238
239 if (isset($prefs_cache[$key])) {
240 return $prefs_cache[$key];
241 } else {
242 if (isset($this->default[$key])) {
243 return $this->default[$key];
244 } else {
245 return $default;
246 }
247 }
248 }
249
250 /**
251 * Delete user's prefs setting
252 * @param string $user user name
253 * @param string $key preference name
254 * @return boolean
255 */
256 function deleteKey($user, $key) {
257 global $prefs_cache;
258
259 if (!$this->open()) {
260 return false;
261 }
262 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
263 $this->table,
264 $this->user_field,
265 $this->dbh->quoteString($user),
266 $this->key_field,
267 $this->dbh->quoteString($key));
268
269 $res = $this->dbh->simpleQuery($query);
270 if(DB::isError($res)) {
271 $this->failQuery($res);
272 }
273
274 unset($prefs_cache[$key]);
275
276 return true;
277 }
278
279 /**
280 * Set user's preference
281 * @param string $user user name
282 * @param string $key preference name
283 * @param mixed $value preference value
284 * @return boolean
285 */
286 function setKey($user, $key, $value) {
287 if (!$this->open()) {
288 return false;
289 }
290
291 /**
292 * Check if username fits into db field
293 */
294 if (strlen($user) > $this->user_size) {
295 $this->error = "Oversized username value."
296 ." Your preferences can't be saved."
297 ." See doc/db-backend.txt or contact your system administrator.";
298
299 /**
300 * Debugging function. Can be used to log all issues that trigger
301 * oversized field errors. Function should be enabled in all three
302 * strlen checks. See http://www.php.net/error-log
303 */
304 // error_log($user.'|'.$key.'|'.$value."\n",3,'/tmp/oversized_log');
305
306 // error is fatal
307 $this->failQuery(null);
308 }
309 /**
310 * Check if preference key fits into db field
311 */
312 if (strlen($key) > $this->key_size) {
313 $err_msg = "Oversized user's preference key."
314 ." Some preferences were not saved."
315 ." See doc/db-backend.txt or contact your system administrator.";
316 // error is not fatal. Only some preference is not saved.
317 trigger_error($err_msg,E_USER_WARNING);
318 return false;
319 }
320 /**
321 * Check if preference value fits into db field
322 */
323 if (strlen($value) > $this->val_size) {
324 $err_msg = "Oversized user's preference value."
325 ." Some preferences were not saved."
326 ." See doc/db-backend.txt or contact your system administrator.";
327 // error is not fatal. Only some preference is not saved.
328 trigger_error($err_msg,E_USER_WARNING);
329 return false;
330 }
331
332
333 if ($this->db_type == SMDB_MYSQL) {
334 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
335 "VALUES('%s','%s','%s')",
336 $this->table,
337 $this->user_field,
338 $this->key_field,
339 $this->val_field,
340 $this->dbh->quoteString($user),
341 $this->dbh->quoteString($key),
342 $this->dbh->quoteString($value));
343
344 $res = $this->dbh->simpleQuery($query);
345 if(DB::isError($res)) {
346 $this->failQuery($res);
347 }
348 } elseif ($this->db_type == SMDB_PGSQL) {
349 $this->dbh->simpleQuery("BEGIN TRANSACTION");
350 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
351 $this->table,
352 $this->user_field,
353 $this->dbh->quoteString($user),
354 $this->key_field,
355 $this->dbh->quoteString($key));
356 $res = $this->dbh->simpleQuery($query);
357 if (DB::isError($res)) {
358 $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
359 $this->failQuery($res);
360 }
361 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
362 $this->table,
363 $this->user_field,
364 $this->key_field,
365 $this->val_field,
366 $this->dbh->quoteString($user),
367 $this->dbh->quoteString($key),
368 $this->dbh->quoteString($value));
369 $res = $this->dbh->simpleQuery($query);
370 if (DB::isError($res)) {
371 $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
372 $this->failQuery($res);
373 }
374 $this->dbh->simpleQuery("COMMIT TRANSACTION");
375 } else {
376 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
377 $this->table,
378 $this->user_field,
379 $this->dbh->quoteString($user),
380 $this->key_field,
381 $this->dbh->quoteString($key));
382 $res = $this->dbh->simpleQuery($query);
383 if (DB::isError($res)) {
384 $this->failQuery($res);
385 }
386 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
387 $this->table,
388 $this->user_field,
389 $this->key_field,
390 $this->val_field,
391 $this->dbh->quoteString($user),
392 $this->dbh->quoteString($key),
393 $this->dbh->quoteString($value));
394 $res = $this->dbh->simpleQuery($query);
395 if (DB::isError($res)) {
396 $this->failQuery($res);
397 }
398 }
399
400 return true;
401 }
402
403 /**
404 * Fill preference cache array
405 * @param string $user user name
406 * @since 1.2.3
407 */
408 function fillPrefsCache($user) {
409 global $prefs_cache;
410
411 if (!$this->open()) {
412 return;
413 }
414
415 $prefs_cache = array();
416 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
417 "WHERE %s = '%s'",
418 $this->key_field,
419 $this->val_field,
420 $this->table,
421 $this->user_field,
422 $this->dbh->quoteString($user));
423 $res = $this->dbh->query($query);
424 if (DB::isError($res)) {
425 $this->failQuery($res);
426 }
427
428 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
429 $prefs_cache[$row['prefkey']] = $row['prefval'];
430 }
431 }
432
433 } /* end class dbPrefs */
434
435
436 /**
437 * returns the value for the pref $string
438 * @ignore
439 */
440 function getPref($data_dir, $username, $string, $default = '') {
441 $db = new dbPrefs;
442 if(isset($db->error)) {
443 printf( _("Preference database error (%s). Exiting abnormally"),
444 $db->error);
445 exit;
446 }
447
448 return $db->getKey($username, $string, $default);
449 }
450
451 /**
452 * Remove the pref $string
453 * @ignore
454 */
455 function removePref($data_dir, $username, $string) {
456 global $prefs_cache;
457 $db = new dbPrefs;
458 if(isset($db->error)) {
459 $db->failQuery();
460 }
461
462 $db->deleteKey($username, $string);
463
464 if (isset($prefs_cache[$string])) {
465 unset($prefs_cache[$string]);
466 }
467
468 sqsession_register($prefs_cache , 'prefs_cache');
469 return;
470 }
471
472 /**
473 * sets the pref, $string, to $set_to
474 * @ignore
475 */
476 function setPref($data_dir, $username, $string, $set_to) {
477 global $prefs_cache;
478
479 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
480 return;
481 }
482
483 if ($set_to === '') {
484 removePref($data_dir, $username, $string);
485 return;
486 }
487
488 $db = new dbPrefs;
489 if(isset($db->error)) {
490 $db->failQuery();
491 }
492
493 $db->setKey($username, $string, $set_to);
494 $prefs_cache[$string] = $set_to;
495 assert_options(ASSERT_ACTIVE, 1);
496 assert_options(ASSERT_BAIL, 1);
497 assert ('$set_to == $prefs_cache[$string]');
498 sqsession_register($prefs_cache , 'prefs_cache');
499 return;
500 }
501
502 /**
503 * This checks if the prefs are available
504 * @ignore
505 */
506 function checkForPrefs($data_dir, $username) {
507 $db = new dbPrefs;
508 if(isset($db->error)) {
509 $db->failQuery();
510 }
511 }
512
513 /**
514 * Writes the Signature
515 * @ignore
516 */
517 function setSig($data_dir, $username, $number, $string) {
518 if ($number == "g") {
519 $key = '___signature___';
520 } else {
521 $key = sprintf('___sig%s___', $number);
522 }
523 setPref($data_dir, $username, $key, $string);
524 return;
525 }
526
527 /**
528 * Gets the signature
529 * @ignore
530 */
531 function getSig($data_dir, $username, $number) {
532 if ($number == "g") {
533 $key = '___signature___';
534 } else {
535 $key = sprintf('___sig%d___', $number);
536 }
537 return getPref($data_dir, $username, $key);
538 }
539
540 // vim: et ts=4
541 ?>