6 * Copyright (c) 1999-2004 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 .= _("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);
50 global $prefs_are_cached, $prefs_cache;
55 function cachePrefValues($username) {
56 global $prefs_are_cached, $prefs_cache;
58 if ($prefs_are_cached) {
62 sqsession_unregister('prefs_cache');
63 sqsession_unregister('prefs_are_cached');
66 if(isset($db->error
)) {
67 printf( _("Preference database error (%s). Exiting abnormally"),
72 $db->fillPrefsCache($username);
73 if (isset($db->error
)) {
74 printf( _("Preference database error (%s). Exiting abnormally"),
79 $prefs_are_cached = true;
81 sqsession_register($prefs_cache, 'prefs_cache');
82 sqsession_register($prefs_are_cached, 'prefs_are_cached');
86 * Completely undocumented class - someone document it!
87 * @package squirrelmail
90 var $table = 'userprefs';
91 var $user_field = 'user';
92 var $key_field = 'prefkey';
93 var $val_field = 'prefval';
97 var $db_type = SMDB_UNKNOWN
;
99 var $default = Array('theme_default' => 0,
100 'show_html_default' => '0');
103 global $prefs_dsn, $prefs_table;
104 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
106 if(isset($this->dbh
)) {
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
;
116 if (!empty($prefs_table)) {
117 $this->table
= $prefs_table;
119 if (!empty($prefs_user_field)) {
120 $this->user_field
= $prefs_user_field;
122 if (!empty($prefs_key_field)) {
123 $this->key_field
= $prefs_key_field;
125 if (!empty($prefs_val_field)) {
126 $this->val_field
= $prefs_val_field;
128 $dbh = DB
::connect($prefs_dsn, true);
130 if(DB
::isError($dbh)) {
131 $this->error
= DB
::errorMessage($dbh);
139 function failQuery($res = NULL) {
141 printf(_("Preference database error (%s). Exiting abnormally"),
144 printf(_("Preference database error (%s). Exiting abnormally"),
145 DB
::errorMessage($res));
151 function getKey($user, $key, $default = '') {
154 cachePrefValues($user);
156 if (isset($prefs_cache[$key])) {
157 return $prefs_cache[$key];
159 if (isset($this->default[$key])) {
160 return $this->default[$key];
167 function deleteKey($user, $key) {
170 if (!$this->open()) {
173 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
176 $this->dbh
->quoteString($user),
178 $this->dbh
->quoteString($key));
180 $res = $this->dbh
->simpleQuery($query);
181 if(DB
::isError($res)) {
182 $this->failQuery($res);
185 unset($prefs_cache[$key]);
190 function setKey($user, $key, $value) {
191 if (!$this->open()) {
194 if ($this->db_type
== SMDB_MYSQL
) {
195 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
196 "VALUES('%s','%s','%s')",
201 $this->dbh
->quoteString($user),
202 $this->dbh
->quoteString($key),
203 $this->dbh
->quoteString($value));
205 $res = $this->dbh
->simpleQuery($query);
206 if(DB
::isError($res)) {
207 $this->failQuery($res);
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'",
214 $this->dbh
->quoteString($user),
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);
222 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
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);
235 $this->dbh
->simpleQuery("COMMIT TRANSACTION");
237 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
240 $this->dbh
->quoteString($user),
242 $this->dbh
->quoteString($key));
243 $res = $this->dbh
->simpleQuery($query);
244 if (DB
::isError($res)) {
245 $this->failQuery($res);
247 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
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);
264 function fillPrefsCache($user) {
267 if (!$this->open()) {
271 $prefs_cache = array();
272 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
278 $this->dbh
->quoteString($user));
279 $res = $this->dbh
->query($query);
280 if (DB
::isError($res)) {
281 $this->failQuery($res);
284 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
285 $prefs_cache[$row['prefkey']] = $row['prefval'];
289 } /* end class dbPrefs */
293 * returns the value for the pref $string
296 function getPref($data_dir, $username, $string, $default = '') {
298 if(isset($db->error
)) {
299 printf( _("Preference database error (%s). Exiting abnormally"),
304 return $db->getKey($username, $string, $default);
308 * Remove the pref $string
311 function removePref($data_dir, $username, $string) {
314 if(isset($db->error
)) {
318 $db->deleteKey($username, $string);
320 if (isset($prefs_cache[$string])) {
321 unset($prefs_cache[$string]);
324 sqsession_register($prefs_cache , 'prefs_cache');
329 * sets the pref, $string, to $set_to
332 function setPref($data_dir, $username, $string, $set_to) {
335 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
339 if ($set_to === '') {
340 removePref($data_dir, $username, $string);
345 if(isset($db->error
)) {
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');
359 * This checks if the prefs are available
362 function checkForPrefs($data_dir, $username) {
364 if(isset($db->error
)) {
370 * Writes the Signature
373 function setSig($data_dir, $username, $number, $string) {
374 if ($number == "g") {
375 $key = '___signature___';
377 $key = sprintf('___sig%s___', $number);
379 setPref($data_dir, $username, $key, $string);
387 function getSig($data_dir, $username, $number) {
388 if ($number == "g") {
389 $key = '___signature___';
391 $key = sprintf('___sig%d___', $number);
393 return getPref($data_dir, $username, $key);