065080f3d2b23bae51744050fdce06bc4a00ecd6
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 .= 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 if ($prefs_are_cached) {
63 sqsession_unregister('prefs_cache');
64 sqsession_unregister('prefs_are_cached');
67 if(isset($db->error
)) {
68 printf( _("Preference database error (%s). Exiting abnormally"),
73 $db->fillPrefsCache($username);
74 if (isset($db->error
)) {
75 printf( _("Preference database error (%s). Exiting abnormally"),
80 $prefs_are_cached = true;
82 sqsession_register($prefs_cache, 'prefs_cache');
83 sqsession_register($prefs_are_cached, 'prefs_are_cached');
87 * Completely undocumented class - someone document it!
88 * @package squirrelmail
91 var $table = 'userprefs';
92 var $user_field = 'user';
93 var $key_field = 'prefkey';
94 var $val_field = 'prefval';
98 var $db_type = SMDB_UNKNOWN
;
100 var $default = Array('theme_default' => 0,
101 'show_html_default' => '0');
104 global $prefs_dsn, $prefs_table;
105 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
107 if(isset($this->dbh
)) {
111 if (preg_match('/^mysql/', $prefs_dsn)) {
112 $this->db_type
= SMDB_MYSQL
;
113 } elseif (preg_match('/^pgsql/', $prefs_dsn)) {
114 $this->db_type
= SMDB_PGSQL
;
117 if (!empty($prefs_table)) {
118 $this->table
= $prefs_table;
120 if (!empty($prefs_user_field)) {
121 $this->user_field
= $prefs_user_field;
123 if (!empty($prefs_key_field)) {
124 $this->key_field
= $prefs_key_field;
126 if (!empty($prefs_val_field)) {
127 $this->val_field
= $prefs_val_field;
129 $dbh = DB
::connect($prefs_dsn, true);
131 if(DB
::isError($dbh)) {
132 $this->error
= DB
::errorMessage($dbh);
140 function failQuery($res = NULL) {
142 printf(_("Preference database error (%s). Exiting abnormally"),
145 printf(_("Preference database error (%s). Exiting abnormally"),
146 DB
::errorMessage($res));
152 function getKey($user, $key, $default = '') {
155 cachePrefValues($user);
157 if (isset($prefs_cache[$key])) {
158 return $prefs_cache[$key];
160 if (isset($this->default[$key])) {
161 return $this->default[$key];
168 function deleteKey($user, $key) {
171 if (!$this->open()) {
174 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
177 $this->dbh
->quoteString($user),
179 $this->dbh
->quoteString($key));
181 $res = $this->dbh
->simpleQuery($query);
182 if(DB
::isError($res)) {
183 $this->failQuery($res);
186 unset($prefs_cache[$key]);
191 function setKey($user, $key, $value) {
192 if (!$this->open()) {
195 if ($this->db_type
== SMDB_MYSQL
) {
196 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
197 "VALUES('%s','%s','%s')",
202 $this->dbh
->quoteString($user),
203 $this->dbh
->quoteString($key),
204 $this->dbh
->quoteString($value));
206 $res = $this->dbh
->simpleQuery($query);
207 if(DB
::isError($res)) {
208 $this->failQuery($res);
210 } elseif ($this->db_type
== SMDB_PGSQL
) {
211 $this->dbh
->simpleQuery("BEGIN TRANSACTION");
212 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
215 $this->dbh
->quoteString($user),
217 $this->dbh
->quoteString($key));
218 $res = $this->dbh
->simpleQuery($query);
219 if (DB
::isError($res)) {
220 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
221 $this->failQuery($res);
223 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
228 $this->dbh
->quoteString($user),
229 $this->dbh
->quoteString($key),
230 $this->dbh
->quoteString($value));
231 $res = $this->dbh
->simpleQuery($query);
232 if (DB
::isError($res)) {
233 $this->dbh
->simpleQuery("ROLLBACK TRANSACTION");
234 $this->failQuery($res);
236 $this->dbh
->simpleQuery("COMMIT TRANSACTION");
238 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
241 $this->dbh
->quoteString($user),
243 $this->dbh
->quoteString($key));
244 $res = $this->dbh
->simpleQuery($query);
245 if (DB
::isError($res)) {
246 $this->failQuery($res);
248 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
253 $this->dbh
->quoteString($user),
254 $this->dbh
->quoteString($key),
255 $this->dbh
->quoteString($value));
256 $res = $this->dbh
->simpleQuery($query);
257 if (DB
::isError($res)) {
258 $this->failQuery($res);
265 function fillPrefsCache($user) {
268 if (!$this->open()) {
272 $prefs_cache = array();
273 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
279 $this->dbh
->quoteString($user));
280 $res = $this->dbh
->query($query);
281 if (DB
::isError($res)) {
282 $this->failQuery($res);
285 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
286 $prefs_cache[$row['prefkey']] = $row['prefval'];
290 } /* end class dbPrefs */
294 * returns the value for the pref $string
297 function getPref($data_dir, $username, $string, $default = '') {
299 if(isset($db->error
)) {
300 printf( _("Preference database error (%s). Exiting abnormally"),
305 return $db->getKey($username, $string, $default);
309 * Remove the pref $string
312 function removePref($data_dir, $username, $string) {
315 if(isset($db->error
)) {
319 $db->deleteKey($username, $string);
321 if (isset($prefs_cache[$string])) {
322 unset($prefs_cache[$string]);
325 sqsession_register($prefs_cache , 'prefs_cache');
330 * sets the pref, $string, to $set_to
333 function setPref($data_dir, $username, $string, $set_to) {
336 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
340 if ($set_to === '') {
341 removePref($data_dir, $username, $string);
346 if(isset($db->error
)) {
350 $db->setKey($username, $string, $set_to);
351 $prefs_cache[$string] = $set_to;
352 assert_options(ASSERT_ACTIVE
, 1);
353 assert_options(ASSERT_BAIL
, 1);
354 assert ('$set_to == $prefs_cache[$string]');
355 sqsession_register($prefs_cache , 'prefs_cache');
360 * This checks if the prefs are available
363 function checkForPrefs($data_dir, $username) {
365 if(isset($db->error
)) {
371 * Writes the Signature
374 function setSig($data_dir, $username, $number, $string) {
375 if ($number == "g") {
376 $key = '___signature___';
378 $key = sprintf('___sig%s___', $number);
380 setPref($data_dir, $username, $key, $string);
388 function getSig($data_dir, $username, $number) {
389 if ($number == "g") {
390 $key = '___signature___';
392 $key = sprintf('___sig%d___', $number);
394 return getPref($data_dir, $username, $key);