documenting form functions.
[squirrelmail.git] / functions / db_prefs.php
CommitLineData
82474746 1<?php
15e6162e 2
d6c32258 3/**
35586184 4 * db_prefs.php
5 *
6c84ba1e 6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains functions for manipulating user preferences
10 * stored in a database, accessed though the Pear DB layer.
11 *
35586184 12 * Database:
13 * ---------
14 *
99a6c222 15 * The preferences table should have three columns:
16 * user char \ primary
35586184 17 * prefkey char / key
18 * prefval blob
19 *
4b7dd3d9 20 * CREATE TABLE userprefs (user CHAR(128) NOT NULL DEFAULT '',
35586184 21 * prefkey CHAR(64) NOT NULL DEFAULT '',
22 * prefval BLOB NOT NULL DEFAULT '',
23 * primary key (user,prefkey));
24 *
25 * Configuration of databasename, username and password is done
3499f99f 26 * by using conf.pl or the administrator plugin
35586184 27 *
31841a9e 28 * @version $Id$
d6c32258 29 * @package squirrelmail
35586184 30 */
31
d6c32258 32/** Unknown database */
98749983 33define('SMDB_UNKNOWN', 0);
d6c32258 34/** MySQL */
98749983 35define('SMDB_MYSQL', 1);
d6c32258 36/** PostgreSQL */
98749983 37define('SMDB_PGSQL', 2);
38
b68edc75 39require_once(SM_PATH . 'config/config.php');
c9fcea56 40if (!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";
2ad4cea9 44 $error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
45 '<tt>DB.php</tt>') . "<br />\n";
c9fcea56 46 $error .= _("Please contact your system administrator and report this error.");
47 error_box($error, $color);
17fca61d 48 exit;
c9fcea56 49}
35586184 50
370059dd 51global $prefs_are_cached, $prefs_cache;
2d367c68 52
4d30c1b7 53/**
54 * @ignore
55 */
370059dd 56function cachePrefValues($username) {
57 global $prefs_are_cached, $prefs_cache;
58
37d5278d 59 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
370059dd 60 if ($prefs_are_cached) {
37d5278d 61 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
370059dd 62 return;
63 }
2d367c68 64
9eb0fbd4 65 sqsession_unregister('prefs_cache');
66 sqsession_unregister('prefs_are_cached');
370059dd 67
68 $db = new dbPrefs;
69 if(isset($db->error)) {
70 printf( _("Preference database error (%s). Exiting abnormally"),
71 $db->error);
72 exit;
73 }
2d367c68 74
370059dd 75 $db->fillPrefsCache($username);
76 if (isset($db->error)) {
77 printf( _("Preference database error (%s). Exiting abnormally"),
78 $db->error);
79 exit;
80 }
81
82 $prefs_are_cached = true;
83
9eb0fbd4 84 sqsession_register($prefs_cache, 'prefs_cache');
85 sqsession_register($prefs_are_cached, 'prefs_are_cached');
370059dd 86}
87
d6c32258 88/**
89 * Completely undocumented class - someone document it!
90 * @package squirrelmail
91 */
370059dd 92class dbPrefs {
370059dd 93 var $table = 'userprefs';
99a6c222 94 var $user_field = 'user';
95 var $key_field = 'prefkey';
96 var $val_field = 'prefval';
370059dd 97
98 var $dbh = NULL;
99 var $error = NULL;
98749983 100 var $db_type = SMDB_UNKNOWN;
370059dd 101
2ea6df85 102 var $default = Array('theme_default' => 0,
370059dd 103 'show_html_default' => '0');
104
105 function open() {
3499f99f 106 global $prefs_dsn, $prefs_table;
98749983 107 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
3499f99f 108
370059dd 109 if(isset($this->dbh)) {
110 return true;
111 }
3499f99f 112
98749983 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;
117 }
118
3499f99f 119 if (!empty($prefs_table)) {
120 $this->table = $prefs_table;
121 }
99a6c222 122 if (!empty($prefs_user_field)) {
123 $this->user_field = $prefs_user_field;
124 }
125 if (!empty($prefs_key_field)) {
126 $this->key_field = $prefs_key_field;
127 }
128 if (!empty($prefs_val_field)) {
129 $this->val_field = $prefs_val_field;
130 }
70561170 131 $dbh = DB::connect($prefs_dsn, true);
2d367c68 132
286fe80b 133 if(DB::isError($dbh)) {
2d367c68 134 $this->error = DB::errorMessage($dbh);
135 return false;
136 }
137
138 $this->dbh = $dbh;
139 return true;
370059dd 140 }
82474746 141
370059dd 142 function failQuery($res = NULL) {
2d367c68 143 if($res == NULL) {
144 printf(_("Preference database error (%s). Exiting abnormally"),
370059dd 145 $this->error);
2d367c68 146 } else {
147 printf(_("Preference database error (%s). Exiting abnormally"),
370059dd 148 DB::errorMessage($res));
2d367c68 149 }
150 exit;
370059dd 151 }
82474746 152
153
370059dd 154 function getKey($user, $key, $default = '') {
155 global $prefs_cache;
2d367c68 156
370059dd 157 cachePrefValues($user);
2d367c68 158
370059dd 159 if (isset($prefs_cache[$key])) {
160 return $prefs_cache[$key];
2d367c68 161 } else {
62337234 162 if (isset($this->default[$key])) {
163 return $this->default[$key];
164 } else {
165 return $default;
166 }
2d367c68 167 }
370059dd 168 }
2d367c68 169
370059dd 170 function deleteKey($user, $key) {
171 global $prefs_cache;
82474746 172
b279d7f4 173 if (!$this->open()) {
174 return false;
175 }
99a6c222 176 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
370059dd 177 $this->table,
99a6c222 178 $this->user_field,
370059dd 179 $this->dbh->quoteString($user),
99a6c222 180 $this->key_field,
370059dd 181 $this->dbh->quoteString($key));
82474746 182
2d367c68 183 $res = $this->dbh->simpleQuery($query);
370059dd 184 if(DB::isError($res)) {
2d367c68 185 $this->failQuery($res);
370059dd 186 }
187
188 unset($prefs_cache[$key]);
82474746 189
2d367c68 190 return true;
370059dd 191 }
82474746 192
370059dd 193 function setKey($user, $key, $value) {
b279d7f4 194 if (!$this->open()) {
195 return false;
196 }
98749983 197 if ($this->db_type == SMDB_MYSQL) {
198 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
199 "VALUES('%s','%s','%s')",
200 $this->table,
201 $this->user_field,
202 $this->key_field,
203 $this->val_field,
204 $this->dbh->quoteString($user),
205 $this->dbh->quoteString($key),
206 $this->dbh->quoteString($value));
207
208 $res = $this->dbh->simpleQuery($query);
209 if(DB::isError($res)) {
210 $this->failQuery($res);
211 }
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'",
215 $this->table,
216 $this->user_field,
217 $this->dbh->quoteString($user),
218 $this->key_field,
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);
224 }
225 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
226 $this->table,
227 $this->user_field,
228 $this->key_field,
229 $this->val_field,
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);
237 }
238 $this->dbh->simpleQuery("COMMIT TRANSACTION");
239 } else {
240 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
241 $this->table,
242 $this->user_field,
243 $this->dbh->quoteString($user),
244 $this->key_field,
245 $this->dbh->quoteString($key));
246 $res = $this->dbh->simpleQuery($query);
247 if (DB::isError($res)) {
248 $this->failQuery($res);
249 }
250 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
251 $this->table,
252 $this->user_field,
253 $this->key_field,
254 $this->val_field,
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);
261 }
370059dd 262 }
2d367c68 263
264 return true;
370059dd 265 }
82474746 266
370059dd 267 function fillPrefsCache($user) {
268 global $prefs_cache;
2d367c68 269
b279d7f4 270 if (!$this->open()) {
271 return;
272 }
370059dd 273
274 $prefs_cache = array();
99a6c222 275 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
276 "WHERE %s = '%s'",
277 $this->key_field,
278 $this->val_field,
370059dd 279 $this->table,
99a6c222 280 $this->user_field,
370059dd 281 $this->dbh->quoteString($user));
282 $res = $this->dbh->query($query);
283 if (DB::isError($res)) {
284 $this->failQuery($res);
285 }
286
287 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
288 $prefs_cache[$row['prefkey']] = $row['prefval'];
289 }
290 }
291
370059dd 292} /* end class dbPrefs */
82474746 293
294
4d30c1b7 295/**
296 * returns the value for the pref $string
297 * @ignore
298 */
370059dd 299function getPref($data_dir, $username, $string, $default = '') {
300 $db = new dbPrefs;
301 if(isset($db->error)) {
2d367c68 302 printf( _("Preference database error (%s). Exiting abnormally"),
370059dd 303 $db->error);
2d367c68 304 exit;
370059dd 305 }
306
307 return $db->getKey($username, $string, $default);
308}
309
4d30c1b7 310/**
311 * Remove the pref $string
312 * @ignore
313 */
370059dd 314function removePref($data_dir, $username, $string) {
1fa62ab9 315 global $prefs_cache;
370059dd 316 $db = new dbPrefs;
317 if(isset($db->error)) {
318 $db->failQuery();
319 }
320
321 $db->deleteKey($username, $string);
88a99543 322
323 if (isset($prefs_cache[$string])) {
324 unset($prefs_cache[$string]);
325 }
326
327 sqsession_register($prefs_cache , 'prefs_cache');
370059dd 328 return;
329}
330
4d30c1b7 331/**
332 * sets the pref, $string, to $set_to
333 * @ignore
334 */
370059dd 335function setPref($data_dir, $username, $string, $set_to) {
336 global $prefs_cache;
337
4b7dd3d9 338 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
1fa62ab9 339 return;
370059dd 340 }
341
bce23c86 342 if ($set_to === '') {
370059dd 343 removePref($data_dir, $username, $string);
344 return;
345 }
346
347 $db = new dbPrefs;
348 if(isset($db->error)) {
349 $db->failQuery();
350 }
351
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]');
88a99543 357 sqsession_register($prefs_cache , 'prefs_cache');
370059dd 358 return;
359}
360
4d30c1b7 361/**
362 * This checks if the prefs are available
363 * @ignore
364 */
370059dd 365function checkForPrefs($data_dir, $username) {
366 $db = new dbPrefs;
367 if(isset($db->error)) {
368 $db->failQuery();
369 }
370}
371
4d30c1b7 372/**
373 * Writes the Signature
374 * @ignore
375 */
16e5635d 376function setSig($data_dir, $username, $number, $string) {
16e5635d 377 if ($number == "g") {
378 $key = '___signature___';
379 } else {
380 $key = sprintf('___sig%s___', $number);
381 }
57f1d1c1 382 setPref($data_dir, $username, $key, $string);
370059dd 383 return;
384}
385
4d30c1b7 386/**
387 * Gets the signature
388 * @ignore
389 */
16e5635d 390function getSig($data_dir, $username, $number) {
16e5635d 391 if ($number == "g") {
392 $key = '___signature___';
393 } else {
394 $key = sprintf('___sig%d___', $number);
395 }
57f1d1c1 396 return getPref($data_dir, $username, $key);
370059dd 397}
398
c9fcea56 399// vim: et ts=4
62f7daa5 400?>