6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains functions for manipulating user preferences
10 * stored in a database, accessed though the Pear DB layer.
15 * The preferences table should have three columns:
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));
25 * Configuration of databasename, username and password is done
26 * by using conf.pl or the administrator plugin
29 * @package squirrelmail
32 /** Unknown database */
33 define('SMDB_UNKNOWN', 0);
35 define('SMDB_MYSQL', 1);
37 define('SMDB_PGSQL', 2);
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 .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
45 '<tt>DB.php</tt>') . "<br />\n";
46 $error .= _("Please contact your system administrator and report this error.");
47 error_box($error, $color);
51 global $prefs_are_cached, $prefs_cache;
56 function cachePrefValues($username) {
57 global $prefs_are_cached, $prefs_cache;
59 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION
);
60 if ($prefs_are_cached) {
61 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION
);
65 sqsession_unregister('prefs_cache');
66 sqsession_unregister('prefs_are_cached');
69 if(isset($db->error
)) {
70 printf( _("Preference database error (%s). Exiting abnormally"),
75 $db->fillPrefsCache($username);
76 if (isset($db->error
)) {
77 printf( _("Preference database error (%s). Exiting abnormally"),
82 $prefs_are_cached = true;
84 sqsession_register($prefs_cache, 'prefs_cache');
85 sqsession_register($prefs_are_cached, 'prefs_are_cached');
89 * Completely undocumented class - someone document it!
90 * @package squirrelmail
93 var $table = 'userprefs';
94 var $user_field = 'user';
95 var $key_field = 'prefkey';
96 var $val_field = 'prefval';
100 var $db_type = SMDB_UNKNOWN
;
102 var $default = Array('theme_default' => 0,
103 'show_html_default' => '0');
106 global $prefs_dsn, $prefs_table;
107 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
109 if(isset($this->dbh
)) {
113 if (preg_match('/^mysql/', $prefs_dsn)) {
114 $this->db_type
= SMDB_MYSQL
;
115 } elseif (preg_match('/^pgsql/', $prefs_dsn)) {
116 $this->db_type
= SMDB_PGSQL
;
119 if (!empty($prefs_table)) {
120 $this->table
= $prefs_table;
122 if (!empty($prefs_user_field)) {
123 $this->user_field
= $prefs_user_field;
125 if (!empty($prefs_key_field)) {
126 $this->key_field
= $prefs_key_field;
128 if (!empty($prefs_val_field)) {
129 $this->val_field
= $prefs_val_field;
131 $dbh = DB
::connect($prefs_dsn, true);
133 if(DB
::isError($dbh)) {
134 $this->error
= DB
::errorMessage($dbh);
142 function failQuery($res = NULL) {
144 printf(_("Preference database error (%s). Exiting abnormally"),
147 printf(_("Preference database error (%s). Exiting abnormally"),
148 DB
::errorMessage($res));
154 function getKey($user, $key, $default = '') {
157 cachePrefValues($user);
159 if (isset($prefs_cache[$key])) {
160 return $prefs_cache[$key];
162 if (isset($this->default[$key])) {
163 return $this->default[$key];
170 function deleteKey($user, $key) {
173 if (!$this->open()) {
176 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
179 $this->dbh
->quoteString($user),
181 $this->dbh
->quoteString($key));
183 $res = $this->dbh
->simpleQuery($query);
184 if(DB
::isError($res)) {
185 $this->failQuery($res);
188 unset($prefs_cache[$key]);
193 function setKey($user, $key, $value) {
194 if (!$this->open()) {
197 if ($this->db_type
== SMDB_MYSQL
) {
198 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
199 "VALUES('%s','%s','%s')",
204 $this->dbh
->quoteString($user),
205 $this->dbh
->quoteString($key),
206 $this->dbh
->quoteString($value));
208 $res = $this->dbh
->simpleQuery($query);
209 if(DB
::isError($res)) {
210 $this->failQuery($res);
212 } elseif ($this->db_type
== SMDB_PGSQL
) {
213 $this->dbh
->simpleQuery("BEGIN TRANSACTION");
214 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
217 $this->dbh
->quoteString($user),
219 $this->dbh
->quoteString($key));
220 $res = $this->dbh
->simpleQuery($query);
221 if (DB
::isError($res)) {
222 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
223 $this->failQuery($res);
225 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
230 $this->dbh
->quoteString($user),
231 $this->dbh
->quoteString($key),
232 $this->dbh
->quoteString($value));
233 $res = $this->dbh
->simpleQuery($query);
234 if (DB
::isError($res)) {
235 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
236 $this->failQuery($res);
238 $this->dbh
->simpleQuery("COMMIT TRANSACTION");
240 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
243 $this->dbh
->quoteString($user),
245 $this->dbh
->quoteString($key));
246 $res = $this->dbh
->simpleQuery($query);
247 if (DB
::isError($res)) {
248 $this->failQuery($res);
250 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
255 $this->dbh
->quoteString($user),
256 $this->dbh
->quoteString($key),
257 $this->dbh
->quoteString($value));
258 $res = $this->dbh
->simpleQuery($query);
259 if (DB
::isError($res)) {
260 $this->failQuery($res);
267 function fillPrefsCache($user) {
270 if (!$this->open()) {
274 $prefs_cache = array();
275 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
281 $this->dbh
->quoteString($user));
282 $res = $this->dbh
->query($query);
283 if (DB
::isError($res)) {
284 $this->failQuery($res);
287 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
288 $prefs_cache[$row['prefkey']] = $row['prefval'];
292 } /* end class dbPrefs */
296 * returns the value for the pref $string
299 function getPref($data_dir, $username, $string, $default = '') {
301 if(isset($db->error
)) {
302 printf( _("Preference database error (%s). Exiting abnormally"),
307 return $db->getKey($username, $string, $default);
311 * Remove the pref $string
314 function removePref($data_dir, $username, $string) {
317 if(isset($db->error
)) {
321 $db->deleteKey($username, $string);
323 if (isset($prefs_cache[$string])) {
324 unset($prefs_cache[$string]);
327 sqsession_register($prefs_cache , 'prefs_cache');
332 * sets the pref, $string, to $set_to
335 function setPref($data_dir, $username, $string, $set_to) {
338 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
342 if ($set_to === '') {
343 removePref($data_dir, $username, $string);
348 if(isset($db->error
)) {
352 $db->setKey($username, $string, $set_to);
353 $prefs_cache[$string] = $set_to;
354 assert_options(ASSERT_ACTIVE
, 1);
355 assert_options(ASSERT_BAIL
, 1);
356 assert ('$set_to == $prefs_cache[$string]');
357 sqsession_register($prefs_cache , 'prefs_cache');
362 * This checks if the prefs are available
365 function checkForPrefs($data_dir, $username) {
367 if(isset($db->error
)) {
373 * Writes the Signature
376 function setSig($data_dir, $username, $number, $string) {
377 if ($number == "g") {
378 $key = '___signature___';
380 $key = sprintf('___sig%s___', $number);
382 setPref($data_dir, $username, $key, $string);
390 function getSig($data_dir, $username, $number) {
391 if ($number == "g") {
392 $key = '___signature___';
394 $key = sprintf('___sig%d___', $number);
396 return getPref($data_dir, $username, $key);