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