Fix problems with <PHP4.0.2 including every listed require file
[squirrelmail.git] / functions / db_prefs.php
CommitLineData
82474746 1<?php
15e6162e 2
370059dd 3/*
35586184 4 * db_prefs.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 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 *
12 * To use this instead of the regular prefs.php, create a
13 * database as described below, and replace prefs.php
14 * with this file.
15 *
16 * Database:
17 * ---------
18 *
99a6c222 19 * The preferences table should have three columns:
20 * user char \ primary
35586184 21 * prefkey char / key
22 * prefval blob
23 *
4b7dd3d9 24 * CREATE TABLE userprefs (user CHAR(128) NOT NULL DEFAULT '',
35586184 25 * prefkey CHAR(64) NOT NULL DEFAULT '',
26 * prefval BLOB NOT NULL DEFAULT '',
27 * primary key (user,prefkey));
28 *
29 * Configuration of databasename, username and password is done
3499f99f 30 * by using conf.pl or the administrator plugin
35586184 31 *
32 * $Id$
33 */
34
98749983 35define('SMDB_UNKNOWN', 0);
36define('SMDB_MYSQL', 1);
37define('SMDB_PGSQL', 2);
38
b4bf2ec1 39include_once('DB.php');
3499f99f 40require_once('../config/config.php');
35586184 41
370059dd 42global $prefs_are_cached, $prefs_cache;
2d367c68 43
370059dd 44function cachePrefValues($username) {
45 global $prefs_are_cached, $prefs_cache;
46
47 if ($prefs_are_cached) {
48 return;
49 }
2d367c68 50
370059dd 51 session_unregister('prefs_cache');
52 session_unregister('prefs_are_cached');
53
54 $db = new dbPrefs;
55 if(isset($db->error)) {
56 printf( _("Preference database error (%s). Exiting abnormally"),
57 $db->error);
58 exit;
59 }
2d367c68 60
370059dd 61 $db->fillPrefsCache($username);
62 if (isset($db->error)) {
63 printf( _("Preference database error (%s). Exiting abnormally"),
64 $db->error);
65 exit;
66 }
67
68 $prefs_are_cached = true;
69
70 session_register('prefs_cache');
71 session_register('prefs_are_cached');
72}
73
74class dbPrefs {
370059dd 75 var $table = 'userprefs';
99a6c222 76 var $user_field = 'user';
77 var $key_field = 'prefkey';
78 var $val_field = 'prefval';
370059dd 79
80 var $dbh = NULL;
81 var $error = NULL;
98749983 82 var $db_type = SMDB_UNKNOWN;
370059dd 83
84 var $default = Array('chosen_theme' => '../themes/default_theme.php',
85 'show_html_default' => '0');
86
87 function open() {
3499f99f 88 global $prefs_dsn, $prefs_table;
98749983 89 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
3499f99f 90
370059dd 91 if(isset($this->dbh)) {
92 return true;
93 }
3499f99f 94
98749983 95 if (preg_match('/^mysql/', $prefs_dsn)) {
96 $this->db_type = SMDB_MYSQL;
97 } elseif (preg_match('/^pgsql/', $prefs_dsn)) {
98 $this->db_type = SMDB_PGSQL;
99 }
100
3499f99f 101 if (!empty($prefs_table)) {
102 $this->table = $prefs_table;
103 }
99a6c222 104 if (!empty($prefs_user_field)) {
105 $this->user_field = $prefs_user_field;
106 }
107 if (!empty($prefs_key_field)) {
108 $this->key_field = $prefs_key_field;
109 }
110 if (!empty($prefs_val_field)) {
111 $this->val_field = $prefs_val_field;
112 }
70561170 113 $dbh = DB::connect($prefs_dsn, true);
2d367c68 114
115 if(DB::isError($dbh) || DB::isWarning($dbh)) {
116 $this->error = DB::errorMessage($dbh);
117 return false;
118 }
119
120 $this->dbh = $dbh;
121 return true;
370059dd 122 }
82474746 123
370059dd 124 function failQuery($res = NULL) {
2d367c68 125 if($res == NULL) {
126 printf(_("Preference database error (%s). Exiting abnormally"),
370059dd 127 $this->error);
2d367c68 128 } else {
129 printf(_("Preference database error (%s). Exiting abnormally"),
370059dd 130 DB::errorMessage($res));
2d367c68 131 }
132 exit;
370059dd 133 }
82474746 134
135
370059dd 136 function getKey($user, $key, $default = '') {
137 global $prefs_cache;
2d367c68 138
370059dd 139 cachePrefValues($user);
2d367c68 140
370059dd 141 if (isset($prefs_cache[$key])) {
142 return $prefs_cache[$key];
2d367c68 143 } else {
62337234 144 if (isset($this->default[$key])) {
145 return $this->default[$key];
146 } else {
147 return $default;
148 }
2d367c68 149 }
370059dd 150 }
2d367c68 151
370059dd 152 function deleteKey($user, $key) {
153 global $prefs_cache;
82474746 154
b279d7f4 155 if (!$this->open()) {
156 return false;
157 }
99a6c222 158 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
370059dd 159 $this->table,
99a6c222 160 $this->user_field,
370059dd 161 $this->dbh->quoteString($user),
99a6c222 162 $this->key_field,
370059dd 163 $this->dbh->quoteString($key));
82474746 164
2d367c68 165 $res = $this->dbh->simpleQuery($query);
370059dd 166 if(DB::isError($res)) {
2d367c68 167 $this->failQuery($res);
370059dd 168 }
169
170 unset($prefs_cache[$key]);
82474746 171
2d367c68 172 if(substr($key, 0, 9) == 'highlight') {
173 $this->renumberHighlightList($user);
174 }
82474746 175
2d367c68 176 return true;
370059dd 177 }
82474746 178
370059dd 179 function setKey($user, $key, $value) {
b279d7f4 180 if (!$this->open()) {
181 return false;
182 }
98749983 183 if ($this->db_type == SMDB_MYSQL) {
184 $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
185 "VALUES('%s','%s','%s')",
186 $this->table,
187 $this->user_field,
188 $this->key_field,
189 $this->val_field,
190 $this->dbh->quoteString($user),
191 $this->dbh->quoteString($key),
192 $this->dbh->quoteString($value));
193
194 $res = $this->dbh->simpleQuery($query);
195 if(DB::isError($res)) {
196 $this->failQuery($res);
197 }
198 } elseif ($this->db_type == SMDB_PGSQL) {
199 $this->dbh->simpleQuery("BEGIN TRANSACTION");
200 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
201 $this->table,
202 $this->user_field,
203 $this->dbh->quoteString($user),
204 $this->key_field,
205 $this->dbh->quoteString($key));
206 $res = $this->dbh->simpleQuery($query);
207 if (DB::isError($res)) {
208 $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
209 $this->failQuery($res);
210 }
211 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
212 $this->table,
213 $this->user_field,
214 $this->key_field,
215 $this->val_field,
216 $this->dbh->quoteString($user),
217 $this->dbh->quoteString($key),
218 $this->dbh->quoteString($value));
219 $res = $this->dbh->simpleQuery($query);
220 if (DB::isError($res)) {
221 $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
222 $this->failQuery($res);
223 }
224 $this->dbh->simpleQuery("COMMIT TRANSACTION");
225 } else {
226 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
227 $this->table,
228 $this->user_field,
229 $this->dbh->quoteString($user),
230 $this->key_field,
231 $this->dbh->quoteString($key));
232 $res = $this->dbh->simpleQuery($query);
233 if (DB::isError($res)) {
234 $this->failQuery($res);
235 }
236 $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
237 $this->table,
238 $this->user_field,
239 $this->key_field,
240 $this->val_field,
241 $this->dbh->quoteString($user),
242 $this->dbh->quoteString($key),
243 $this->dbh->quoteString($value));
244 $res = $this->dbh->simpleQuery($query);
245 if (DB::isError($res)) {
246 $this->failQuery($res);
247 }
370059dd 248 }
2d367c68 249
250 return true;
370059dd 251 }
82474746 252
370059dd 253 function fillPrefsCache($user) {
254 global $prefs_cache;
2d367c68 255
b279d7f4 256 if (!$this->open()) {
257 return;
258 }
370059dd 259
260 $prefs_cache = array();
99a6c222 261 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
262 "WHERE %s = '%s'",
263 $this->key_field,
264 $this->val_field,
370059dd 265 $this->table,
99a6c222 266 $this->user_field,
370059dd 267 $this->dbh->quoteString($user));
268 $res = $this->dbh->query($query);
269 if (DB::isError($res)) {
270 $this->failQuery($res);
271 }
272
273 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
274 $prefs_cache[$row['prefkey']] = $row['prefval'];
275 }
276 }
277
278 /*
279 * When a highlight option is deleted the preferences module
280 * must renumber the list. This should be done somewhere else,
281 * but it is not, so....
282 */
283 function renumberHighlightList($user) {
b279d7f4 284 if (!$this->open()) {
285 return;
286 }
99a6c222 287 $query = sprintf("SELECT %s, %s as prefkey, %s as prefval FROM %s WHERE %s='%s' ".
288 "AND %s LIKE 'highlight%%' ORDER BY %s",
289 $this->user_field,
290 $this->key_field,
291 $this->val_field,
370059dd 292 $this->table,
99a6c222 293 $this->user_field,
294 $this->dbh->quoteString($user),
295 $this->key_field,
296 $this->key_field);
2d367c68 297
298 $res = $this->dbh->query($query);
370059dd 299 if(DB::isError($res)) {
2d367c68 300 $this->failQuery($res);
370059dd 301 }
2d367c68 302
370059dd 303 /* Store old data in array */
2d367c68 304 $rows = Array();
370059dd 305 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
2d367c68 306 $rows[] = $row;
370059dd 307 }
2d367c68 308
370059dd 309 /* Renumber keys of old data */
2d367c68 310 $hilinum = 0;
311 for($i = 0; $i < count($rows) ; $i++) {
312 $oldkey = $rows[$i]['prefkey'];
313 $newkey = substr($oldkey, 0, 9) . $hilinum;
314 $hilinum++;
315
316 if($oldkey != $newkey) {
99a6c222 317 $query = sprintf("UPDATE %s SET %s='%s' ".
318 "WHERE %s ='%s' AND %s='%s'",
370059dd 319 $this->table,
99a6c222 320 $this->key_field,
370059dd 321 $this->dbh->quoteString($newkey),
99a6c222 322 $this->user_field,
370059dd 323 $this->dbh->quoteString($user),
99a6c222 324 $this->key_field,
370059dd 325 $this->dbh->quoteString($oldkey));
326
327 $res = $this->dbh->simpleQuery($query);
328 if(DB::isError($res)) {
329 $this->failQuery($res);
330 }
2d367c68 331 }
332 }
333
334 return;
370059dd 335 }
82474746 336
370059dd 337} /* end class dbPrefs */
82474746 338
339
370059dd 340/* returns the value for the pref $string */
341function getPref($data_dir, $username, $string, $default = '') {
342 $db = new dbPrefs;
343 if(isset($db->error)) {
2d367c68 344 printf( _("Preference database error (%s). Exiting abnormally"),
370059dd 345 $db->error);
2d367c68 346 exit;
370059dd 347 }
348
349 return $db->getKey($username, $string, $default);
350}
351
352/* Remove the pref $string */
353function removePref($data_dir, $username, $string) {
354 $db = new dbPrefs;
355 if(isset($db->error)) {
356 $db->failQuery();
357 }
358
359 $db->deleteKey($username, $string);
360 return;
361}
362
363/* sets the pref, $string, to $set_to */
364function setPref($data_dir, $username, $string, $set_to) {
365 global $prefs_cache;
366
4b7dd3d9 367 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
370059dd 368 return;
369 }
370
371 if ($set_to == '') {
372 removePref($data_dir, $username, $string);
373 return;
374 }
375
376 $db = new dbPrefs;
377 if(isset($db->error)) {
378 $db->failQuery();
379 }
380
381 $db->setKey($username, $string, $set_to);
382 $prefs_cache[$string] = $set_to;
383 assert_options(ASSERT_ACTIVE, 1);
384 assert_options(ASSERT_BAIL, 1);
385 assert ('$set_to == $prefs_cache[$string]');
386
387 return;
388}
389
390/* This checks if the prefs are available */
391function checkForPrefs($data_dir, $username) {
392 $db = new dbPrefs;
393 if(isset($db->error)) {
394 $db->failQuery();
395 }
396}
397
398/* Writes the Signature */
16e5635d 399function setSig($data_dir, $username, $number, $string) {
370059dd 400 $db = new dbPrefs;
401 if(isset($db->error)) {
402 $db->failQuery();
403 }
404
16e5635d 405 if ($number == "g") {
406 $key = '___signature___';
407 } else {
408 $key = sprintf('___sig%s___', $number);
409 }
410 $db->setKey($username, $key, $string);
370059dd 411 return;
412}
413
414/* Gets the signature */
16e5635d 415function getSig($data_dir, $username, $number) {
370059dd 416 $db = new dbPrefs;
417 if(isset($db->error)) {
418 $db->failQuery();
419 }
420
16e5635d 421 if ($number == "g") {
422 $key = '___signature___';
423 } else {
424 $key = sprintf('___sig%d___', $number);
425 }
426 return $db->getKey($username, $key);
370059dd 427}
428
82474746 429?>