code dumping
[squirrelmail.git] / functions / db_prefs.php
CommitLineData
82474746 1<?php
15e6162e 2
d6c32258 3/**
35586184 4 * db_prefs.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 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
883c8f81 39require_once('DB.php');
b68edc75 40require_once(SM_PATH . 'config/config.php');
35586184 41
370059dd 42global $prefs_are_cached, $prefs_cache;
2d367c68 43
4d30c1b7 44/**
45 * @ignore
46 */
370059dd 47function cachePrefValues($username) {
48 global $prefs_are_cached, $prefs_cache;
49
50 if ($prefs_are_cached) {
51 return;
52 }
2d367c68 53
9eb0fbd4 54 sqsession_unregister('prefs_cache');
55 sqsession_unregister('prefs_are_cached');
370059dd 56
57 $db = new dbPrefs;
58 if(isset($db->error)) {
59 printf( _("Preference database error (%s). Exiting abnormally"),
60 $db->error);
61 exit;
62 }
2d367c68 63
370059dd 64 $db->fillPrefsCache($username);
65 if (isset($db->error)) {
66 printf( _("Preference database error (%s). Exiting abnormally"),
67 $db->error);
68 exit;
69 }
70
71 $prefs_are_cached = true;
72
9eb0fbd4 73 sqsession_register($prefs_cache, 'prefs_cache');
74 sqsession_register($prefs_are_cached, 'prefs_are_cached');
370059dd 75}
76
d6c32258 77/**
78 * Completely undocumented class - someone document it!
79 * @package squirrelmail
80 */
370059dd 81class dbPrefs {
370059dd 82 var $table = 'userprefs';
99a6c222 83 var $user_field = 'user';
84 var $key_field = 'prefkey';
85 var $val_field = 'prefval';
370059dd 86
87 var $dbh = NULL;
88 var $error = NULL;
98749983 89 var $db_type = SMDB_UNKNOWN;
370059dd 90
2ea6df85 91 var $default = Array('theme_default' => 0,
370059dd 92 'show_html_default' => '0');
93
94 function open() {
3499f99f 95 global $prefs_dsn, $prefs_table;
98749983 96 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
3499f99f 97
370059dd 98 if(isset($this->dbh)) {
99 return true;
100 }
3499f99f 101
98749983 102 if (preg_match('/^mysql/', $prefs_dsn)) {
103 $this->db_type = SMDB_MYSQL;
104 } elseif (preg_match('/^pgsql/', $prefs_dsn)) {
105 $this->db_type = SMDB_PGSQL;
106 }
107
3499f99f 108 if (!empty($prefs_table)) {
109 $this->table = $prefs_table;
110 }
99a6c222 111 if (!empty($prefs_user_field)) {
112 $this->user_field = $prefs_user_field;
113 }
114 if (!empty($prefs_key_field)) {
115 $this->key_field = $prefs_key_field;
116 }
117 if (!empty($prefs_val_field)) {
118 $this->val_field = $prefs_val_field;
119 }
70561170 120 $dbh = DB::connect($prefs_dsn, true);
2d367c68 121
286fe80b 122 if(DB::isError($dbh)) {
2d367c68 123 $this->error = DB::errorMessage($dbh);
124 return false;
125 }
126
127 $this->dbh = $dbh;
128 return true;
370059dd 129 }
82474746 130
370059dd 131 function failQuery($res = NULL) {
2d367c68 132 if($res == NULL) {
133 printf(_("Preference database error (%s). Exiting abnormally"),
370059dd 134 $this->error);
2d367c68 135 } else {
136 printf(_("Preference database error (%s). Exiting abnormally"),
370059dd 137 DB::errorMessage($res));
2d367c68 138 }
139 exit;
370059dd 140 }
82474746 141
142
370059dd 143 function getKey($user, $key, $default = '') {
144 global $prefs_cache;
2d367c68 145
370059dd 146 cachePrefValues($user);
2d367c68 147
370059dd 148 if (isset($prefs_cache[$key])) {
149 return $prefs_cache[$key];
2d367c68 150 } else {
62337234 151 if (isset($this->default[$key])) {
152 return $this->default[$key];
153 } else {
154 return $default;
155 }
2d367c68 156 }
370059dd 157 }
2d367c68 158
370059dd 159 function deleteKey($user, $key) {
160 global $prefs_cache;
82474746 161
b279d7f4 162 if (!$this->open()) {
163 return false;
164 }
99a6c222 165 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
370059dd 166 $this->table,
99a6c222 167 $this->user_field,
370059dd 168 $this->dbh->quoteString($user),
99a6c222 169 $this->key_field,
370059dd 170 $this->dbh->quoteString($key));
82474746 171
2d367c68 172 $res = $this->dbh->simpleQuery($query);
370059dd 173 if(DB::isError($res)) {
2d367c68 174 $this->failQuery($res);
370059dd 175 }
176
177 unset($prefs_cache[$key]);
82474746 178
2d367c68 179 return true;
370059dd 180 }
82474746 181
370059dd 182 function setKey($user, $key, $value) {
b279d7f4 183 if (!$this->open()) {
184 return false;
185 }
98749983 186 if ($this->db_type == SMDB_MYSQL) {
187 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
188 "VALUES('%s','%s','%s')",
189 $this->table,
190 $this->user_field,
191 $this->key_field,
192 $this->val_field,
193 $this->dbh->quoteString($user),
194 $this->dbh->quoteString($key),
195 $this->dbh->quoteString($value));
196
197 $res = $this->dbh->simpleQuery($query);
198 if(DB::isError($res)) {
199 $this->failQuery($res);
200 }
201 } elseif ($this->db_type == SMDB_PGSQL) {
202 $this->dbh->simpleQuery("BEGIN TRANSACTION");
203 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
204 $this->table,
205 $this->user_field,
206 $this->dbh->quoteString($user),
207 $this->key_field,
208 $this->dbh->quoteString($key));
209 $res = $this->dbh->simpleQuery($query);
210 if (DB::isError($res)) {
211 $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
212 $this->failQuery($res);
213 }
214 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
215 $this->table,
216 $this->user_field,
217 $this->key_field,
218 $this->val_field,
219 $this->dbh->quoteString($user),
220 $this->dbh->quoteString($key),
221 $this->dbh->quoteString($value));
222 $res = $this->dbh->simpleQuery($query);
223 if (DB::isError($res)) {
224 $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
225 $this->failQuery($res);
226 }
227 $this->dbh->simpleQuery("COMMIT TRANSACTION");
228 } else {
229 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
230 $this->table,
231 $this->user_field,
232 $this->dbh->quoteString($user),
233 $this->key_field,
234 $this->dbh->quoteString($key));
235 $res = $this->dbh->simpleQuery($query);
236 if (DB::isError($res)) {
237 $this->failQuery($res);
238 }
239 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
240 $this->table,
241 $this->user_field,
242 $this->key_field,
243 $this->val_field,
244 $this->dbh->quoteString($user),
245 $this->dbh->quoteString($key),
246 $this->dbh->quoteString($value));
247 $res = $this->dbh->simpleQuery($query);
248 if (DB::isError($res)) {
249 $this->failQuery($res);
250 }
370059dd 251 }
2d367c68 252
253 return true;
370059dd 254 }
82474746 255
370059dd 256 function fillPrefsCache($user) {
257 global $prefs_cache;
2d367c68 258
b279d7f4 259 if (!$this->open()) {
260 return;
261 }
370059dd 262
263 $prefs_cache = array();
99a6c222 264 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
265 "WHERE %s = '%s'",
266 $this->key_field,
267 $this->val_field,
370059dd 268 $this->table,
99a6c222 269 $this->user_field,
370059dd 270 $this->dbh->quoteString($user));
271 $res = $this->dbh->query($query);
272 if (DB::isError($res)) {
273 $this->failQuery($res);
274 }
275
276 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
277 $prefs_cache[$row['prefkey']] = $row['prefval'];
278 }
279 }
280
370059dd 281} /* end class dbPrefs */
82474746 282
283
4d30c1b7 284/**
285 * returns the value for the pref $string
286 * @ignore
287 */
370059dd 288function getPref($data_dir, $username, $string, $default = '') {
289 $db = new dbPrefs;
290 if(isset($db->error)) {
2d367c68 291 printf( _("Preference database error (%s). Exiting abnormally"),
370059dd 292 $db->error);
2d367c68 293 exit;
370059dd 294 }
295
296 return $db->getKey($username, $string, $default);
297}
298
4d30c1b7 299/**
300 * Remove the pref $string
301 * @ignore
302 */
370059dd 303function removePref($data_dir, $username, $string) {
1fa62ab9 304 global $prefs_cache;
370059dd 305 $db = new dbPrefs;
306 if(isset($db->error)) {
307 $db->failQuery();
308 }
309
310 $db->deleteKey($username, $string);
88a99543 311
312 if (isset($prefs_cache[$string])) {
313 unset($prefs_cache[$string]);
314 }
315
316 sqsession_register($prefs_cache , 'prefs_cache');
370059dd 317 return;
318}
319
4d30c1b7 320/**
321 * sets the pref, $string, to $set_to
322 * @ignore
323 */
370059dd 324function setPref($data_dir, $username, $string, $set_to) {
325 global $prefs_cache;
326
4b7dd3d9 327 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
1fa62ab9 328 return;
370059dd 329 }
330
bce23c86 331 if ($set_to === '') {
370059dd 332 removePref($data_dir, $username, $string);
333 return;
334 }
335
336 $db = new dbPrefs;
337 if(isset($db->error)) {
338 $db->failQuery();
339 }
340
341 $db->setKey($username, $string, $set_to);
342 $prefs_cache[$string] = $set_to;
343 assert_options(ASSERT_ACTIVE, 1);
344 assert_options(ASSERT_BAIL, 1);
345 assert ('$set_to == $prefs_cache[$string]');
88a99543 346 sqsession_register($prefs_cache , 'prefs_cache');
370059dd 347 return;
348}
349
4d30c1b7 350/**
351 * This checks if the prefs are available
352 * @ignore
353 */
370059dd 354function checkForPrefs($data_dir, $username) {
355 $db = new dbPrefs;
356 if(isset($db->error)) {
357 $db->failQuery();
358 }
359}
360
4d30c1b7 361/**
362 * Writes the Signature
363 * @ignore
364 */
16e5635d 365function setSig($data_dir, $username, $number, $string) {
16e5635d 366 if ($number == "g") {
367 $key = '___signature___';
368 } else {
369 $key = sprintf('___sig%s___', $number);
370 }
57f1d1c1 371 setPref($data_dir, $username, $key, $string);
370059dd 372 return;
373}
374
4d30c1b7 375/**
376 * Gets the signature
377 * @ignore
378 */
16e5635d 379function getSig($data_dir, $username, $number) {
16e5635d 380 if ($number == "g") {
381 $key = '___signature___';
382 } else {
383 $key = sprintf('___sig%d___', $number);
384 }
57f1d1c1 385 return getPref($data_dir, $username, $key);
370059dd 386}
387
4d30c1b7 388?>