validator.w3.org fails if id attribute contains brackets
[squirrelmail.git] / functions / db_prefs.php
CommitLineData
82474746 1<?php
15e6162e 2
d6c32258 3/**
35586184 4 * db_prefs.php
5 *
35586184 6 * This contains functions for manipulating user preferences
7 * stored in a database, accessed though the Pear DB layer.
8 *
35586184 9 * Database:
35586184 10 *
99a6c222 11 * The preferences table should have three columns:
12 * user char \ primary
35586184 13 * prefkey char / key
14 * prefval blob
15 *
4b7dd3d9 16 * CREATE TABLE userprefs (user CHAR(128) NOT NULL DEFAULT '',
35586184 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
3499f99f 22 * by using conf.pl or the administrator plugin
35586184 23 *
47ccfad4 24 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
4b4abf93 25 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
31841a9e 26 * @version $Id$
d6c32258 27 * @package squirrelmail
ace4c62c 28 * @subpackage prefs
29 * @since 1.1.3
35586184 30 */
31
ace4c62c 32/** @ignore */
33if (!defined('SM_PATH')) define('SM_PATH','../');
34
d6c32258 35/** Unknown database */
98749983 36define('SMDB_UNKNOWN', 0);
d6c32258 37/** MySQL */
98749983 38define('SMDB_MYSQL', 1);
d6c32258 39/** PostgreSQL */
98749983 40define('SMDB_PGSQL', 2);
41
202bcbcc 42
c9fcea56 43if (!include_once('DB.php')) {
44 // same error also in abook_database.php
c9fcea56 45 $error = _("Could not include PEAR database functions required for the database backend.") . "<br />\n";
2ad4cea9 46 $error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
47 '<tt>DB.php</tt>') . "<br />\n";
c9fcea56 48 $error .= _("Please contact your system administrator and report this error.");
49 error_box($error, $color);
17fca61d 50 exit;
c9fcea56 51}
35586184 52
370059dd 53global $prefs_are_cached, $prefs_cache;
2d367c68 54
4d30c1b7 55/**
56 * @ignore
57 */
370059dd 58function cachePrefValues($username) {
59 global $prefs_are_cached, $prefs_cache;
60
37d5278d 61 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
370059dd 62 if ($prefs_are_cached) {
37d5278d 63 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
370059dd 64 return;
65 }
2d367c68 66
9eb0fbd4 67 sqsession_unregister('prefs_cache');
68 sqsession_unregister('prefs_are_cached');
370059dd 69
70 $db = new dbPrefs;
71 if(isset($db->error)) {
72 printf( _("Preference database error (%s). Exiting abnormally"),
73 $db->error);
74 exit;
75 }
2d367c68 76
370059dd 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
9eb0fbd4 86 sqsession_register($prefs_cache, 'prefs_cache');
87 sqsession_register($prefs_are_cached, 'prefs_are_cached');
370059dd 88}
89
d6c32258 90/**
ace4c62c 91 * Class used to handle connections to prefs database and operations with preferences
d6c32258 92 * @package squirrelmail
ace4c62c 93 * @subpackage prefs
94 * @since 1.1.3
d6c32258 95 */
370059dd 96class dbPrefs {
ace4c62c 97 /**
98 * Table used to store preferences
99 * @var string
100 */
370059dd 101 var $table = 'userprefs';
ace4c62c 102 /**
103 * Field used to store owner of preference
104 * @var string
105 */
99a6c222 106 var $user_field = 'user';
ace4c62c 107 /**
108 * Field used to store preference name
109 * @var string
110 */
99a6c222 111 var $key_field = 'prefkey';
ace4c62c 112 /**
113 * Field used to store preference value
114 * @var string
115 */
99a6c222 116 var $val_field = 'prefval';
370059dd 117
ace4c62c 118 /**
119 * Database connection object
120 * @var object
121 */
370059dd 122 var $dbh = NULL;
ace4c62c 123 /**
124 * Error messages
125 * @var string
126 */
370059dd 127 var $error = NULL;
ace4c62c 128 /**
129 * Database type (SMDB_* constants)
130 * Is used in setKey().
131 * @var integer
132 */
98749983 133 var $db_type = SMDB_UNKNOWN;
370059dd 134
ace4c62c 135 /**
136 * Default preferences
137 * @var array
138 */
2ea6df85 139 var $default = Array('theme_default' => 0,
370059dd 140 'show_html_default' => '0');
141
ace4c62c 142 /**
06316c07 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 /**
ace4c62c 162 * initialize DB connection object
163 * @return boolean true, if object is initialized
164 */
370059dd 165 function open() {
3499f99f 166 global $prefs_dsn, $prefs_table;
98749983 167 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
06316c07 168 global $prefs_user_size, $prefs_key_size, $prefs_val_size;
3499f99f 169
370059dd 170 if(isset($this->dbh)) {
171 return true;
172 }
3499f99f 173
98749983 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
3499f99f 180 if (!empty($prefs_table)) {
181 $this->table = $prefs_table;
182 }
99a6c222 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 }
06316c07 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 }
70561170 201 $dbh = DB::connect($prefs_dsn, true);
2d367c68 202
286fe80b 203 if(DB::isError($dbh)) {
2d367c68 204 $this->error = DB::errorMessage($dbh);
205 return false;
206 }
207
208 $this->dbh = $dbh;
209 return true;
370059dd 210 }
82474746 211
ace4c62c 212 /**
213 * Function used to handle database connection errors
202bcbcc 214 * @param object PEAR Error object
ace4c62c 215 */
370059dd 216 function failQuery($res = NULL) {
2d367c68 217 if($res == NULL) {
218 printf(_("Preference database error (%s). Exiting abnormally"),
370059dd 219 $this->error);
2d367c68 220 } else {
221 printf(_("Preference database error (%s). Exiting abnormally"),
370059dd 222 DB::errorMessage($res));
2d367c68 223 }
224 exit;
370059dd 225 }
82474746 226
ace4c62c 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 */
370059dd 234 function getKey($user, $key, $default = '') {
235 global $prefs_cache;
2d367c68 236
370059dd 237 cachePrefValues($user);
2d367c68 238
370059dd 239 if (isset($prefs_cache[$key])) {
240 return $prefs_cache[$key];
2d367c68 241 } else {
62337234 242 if (isset($this->default[$key])) {
243 return $this->default[$key];
244 } else {
245 return $default;
246 }
2d367c68 247 }
370059dd 248 }
2d367c68 249
ace4c62c 250 /**
251 * Delete user's prefs setting
202bcbcc 252 * @param string $user user name
ace4c62c 253 * @param string $key preference name
254 * @return boolean
255 */
370059dd 256 function deleteKey($user, $key) {
257 global $prefs_cache;
82474746 258
b279d7f4 259 if (!$this->open()) {
260 return false;
261 }
99a6c222 262 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
370059dd 263 $this->table,
99a6c222 264 $this->user_field,
370059dd 265 $this->dbh->quoteString($user),
99a6c222 266 $this->key_field,
370059dd 267 $this->dbh->quoteString($key));
82474746 268
2d367c68 269 $res = $this->dbh->simpleQuery($query);
370059dd 270 if(DB::isError($res)) {
2d367c68 271 $this->failQuery($res);
370059dd 272 }
273
274 unset($prefs_cache[$key]);
82474746 275
2d367c68 276 return true;
370059dd 277 }
82474746 278
ace4c62c 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 */
370059dd 286 function setKey($user, $key, $value) {
b279d7f4 287 if (!$this->open()) {
288 return false;
289 }
06316c07 290
291 /**
292 * Check if username fits into db field
293 */
294 if (strlen($user) > $this->user_size) {
295 $this->error = "Oversized username value."
5e07597f 296 ." Your preferences can't be saved."
297 ." See doc/db-backend.txt or contact your system administrator.";
06316c07 298
299 /**
202bcbcc 300 * Debugging function. Can be used to log all issues that trigger
301 * oversized field errors. Function should be enabled in all three
06316c07 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."
5e07597f 314 ." Some preferences were not saved."
315 ." See doc/db-backend.txt or contact your system administrator.";
06316c07 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."
5e07597f 325 ." Some preferences were not saved."
326 ." See doc/db-backend.txt or contact your system administrator.";
06316c07 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
98749983 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 }
370059dd 398 }
2d367c68 399
400 return true;
370059dd 401 }
82474746 402
ace4c62c 403 /**
404 * Fill preference cache array
405 * @param string $user user name
406 * @since 1.2.3
407 */
370059dd 408 function fillPrefsCache($user) {
409 global $prefs_cache;
2d367c68 410
b279d7f4 411 if (!$this->open()) {
412 return;
413 }
370059dd 414
415 $prefs_cache = array();
99a6c222 416 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
417 "WHERE %s = '%s'",
418 $this->key_field,
419 $this->val_field,
370059dd 420 $this->table,
99a6c222 421 $this->user_field,
370059dd 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
370059dd 433} /* end class dbPrefs */
82474746 434
435
4d30c1b7 436/**
437 * returns the value for the pref $string
438 * @ignore
439 */
370059dd 440function getPref($data_dir, $username, $string, $default = '') {
441 $db = new dbPrefs;
442 if(isset($db->error)) {
2d367c68 443 printf( _("Preference database error (%s). Exiting abnormally"),
370059dd 444 $db->error);
2d367c68 445 exit;
370059dd 446 }
447
448 return $db->getKey($username, $string, $default);
449}
450
4d30c1b7 451/**
452 * Remove the pref $string
453 * @ignore
454 */
370059dd 455function removePref($data_dir, $username, $string) {
1fa62ab9 456 global $prefs_cache;
370059dd 457 $db = new dbPrefs;
458 if(isset($db->error)) {
459 $db->failQuery();
460 }
461
462 $db->deleteKey($username, $string);
88a99543 463
464 if (isset($prefs_cache[$string])) {
465 unset($prefs_cache[$string]);
466 }
467
468 sqsession_register($prefs_cache , 'prefs_cache');
370059dd 469 return;
470}
471
4d30c1b7 472/**
473 * sets the pref, $string, to $set_to
474 * @ignore
475 */
370059dd 476function setPref($data_dir, $username, $string, $set_to) {
477 global $prefs_cache;
478
4b7dd3d9 479 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
1fa62ab9 480 return;
370059dd 481 }
482
bce23c86 483 if ($set_to === '') {
370059dd 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]');
88a99543 498 sqsession_register($prefs_cache , 'prefs_cache');
370059dd 499 return;
500}
501
4d30c1b7 502/**
503 * This checks if the prefs are available
504 * @ignore
505 */
370059dd 506function checkForPrefs($data_dir, $username) {
507 $db = new dbPrefs;
508 if(isset($db->error)) {
509 $db->failQuery();
510 }
511}
512
4d30c1b7 513/**
514 * Writes the Signature
515 * @ignore
516 */
16e5635d 517function setSig($data_dir, $username, $number, $string) {
16e5635d 518 if ($number == "g") {
519 $key = '___signature___';
520 } else {
521 $key = sprintf('___sig%s___', $number);
522 }
57f1d1c1 523 setPref($data_dir, $username, $key, $string);
370059dd 524 return;
525}
526
4d30c1b7 527/**
528 * Gets the signature
529 * @ignore
530 */
16e5635d 531function getSig($data_dir, $username, $number) {
16e5635d 532 if ($number == "g") {
533 $key = '___signature___';
534 } else {
535 $key = sprintf('___sig%d___', $number);
536 }
57f1d1c1 537 return getPref($data_dir, $username, $key);
370059dd 538}
539
c9fcea56 540// vim: et ts=4
62f7daa5 541?>