d7279a31dcdfbb31347703c74c88cff41e3e7396
[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 * $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('DB.php');
40 require_once(SM_PATH . 'config/config.php');
41
42 global $prefs_are_cached, $prefs_cache;
43
44 /**
45 * @ignore
46 */
47 function cachePrefValues($username) {
48 global $prefs_are_cached, $prefs_cache;
49
50 if ($prefs_are_cached) {
51 return;
52 }
53
54 sqsession_unregister('prefs_cache');
55 sqsession_unregister('prefs_are_cached');
56
57 $db = new dbPrefs;
58 if(isset($db->error)) {
59 printf( _("Preference database error (%s). Exiting abnormally"),
60 $db->error);
61 exit;
62 }
63
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
73 sqsession_register($prefs_cache, 'prefs_cache');
74 sqsession_register($prefs_are_cached, 'prefs_are_cached');
75 }
76
77 /**
78 * Completely undocumented class - someone document it!
79 * @package squirrelmail
80 */
81 class dbPrefs {
82 var $table = 'userprefs';
83 var $user_field = 'user';
84 var $key_field = 'prefkey';
85 var $val_field = 'prefval';
86
87 var $dbh = NULL;
88 var $error = NULL;
89 var $db_type = SMDB_UNKNOWN;
90
91 var $default = Array('theme_default' => 0,
92 'show_html_default' => '0');
93
94 function open() {
95 global $prefs_dsn, $prefs_table;
96 global $prefs_user_field, $prefs_key_field, $prefs_val_field;
97
98 if(isset($this->dbh)) {
99 return true;
100 }
101
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
108 if (!empty($prefs_table)) {
109 $this->table = $prefs_table;
110 }
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 }
120 $dbh = DB::connect($prefs_dsn, true);
121
122 if(DB::isError($dbh)) {
123 $this->error = DB::errorMessage($dbh);
124 return false;
125 }
126
127 $this->dbh = $dbh;
128 return true;
129 }
130
131 function failQuery($res = NULL) {
132 if($res == NULL) {
133 printf(_("Preference database error (%s). Exiting abnormally"),
134 $this->error);
135 } else {
136 printf(_("Preference database error (%s). Exiting abnormally"),
137 DB::errorMessage($res));
138 }
139 exit;
140 }
141
142
143 function getKey($user, $key, $default = '') {
144 global $prefs_cache;
145
146 cachePrefValues($user);
147
148 if (isset($prefs_cache[$key])) {
149 return $prefs_cache[$key];
150 } else {
151 if (isset($this->default[$key])) {
152 return $this->default[$key];
153 } else {
154 return $default;
155 }
156 }
157 }
158
159 function deleteKey($user, $key) {
160 global $prefs_cache;
161
162 if (!$this->open()) {
163 return false;
164 }
165 $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
166 $this->table,
167 $this->user_field,
168 $this->dbh->quoteString($user),
169 $this->key_field,
170 $this->dbh->quoteString($key));
171
172 $res = $this->dbh->simpleQuery($query);
173 if(DB::isError($res)) {
174 $this->failQuery($res);
175 }
176
177 unset($prefs_cache[$key]);
178
179 return true;
180 }
181
182 function setKey($user, $key, $value) {
183 if (!$this->open()) {
184 return false;
185 }
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 }
251 }
252
253 return true;
254 }
255
256 function fillPrefsCache($user) {
257 global $prefs_cache;
258
259 if (!$this->open()) {
260 return;
261 }
262
263 $prefs_cache = array();
264 $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
265 "WHERE %s = '%s'",
266 $this->key_field,
267 $this->val_field,
268 $this->table,
269 $this->user_field,
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
281 } /* end class dbPrefs */
282
283
284 /**
285 * returns the value for the pref $string
286 * @ignore
287 */
288 function getPref($data_dir, $username, $string, $default = '') {
289 $db = new dbPrefs;
290 if(isset($db->error)) {
291 printf( _("Preference database error (%s). Exiting abnormally"),
292 $db->error);
293 exit;
294 }
295
296 return $db->getKey($username, $string, $default);
297 }
298
299 /**
300 * Remove the pref $string
301 * @ignore
302 */
303 function removePref($data_dir, $username, $string) {
304 global $prefs_cache;
305 $db = new dbPrefs;
306 if(isset($db->error)) {
307 $db->failQuery();
308 }
309
310 $db->deleteKey($username, $string);
311
312 if (isset($prefs_cache[$string])) {
313 unset($prefs_cache[$string]);
314 }
315
316 sqsession_register($prefs_cache , 'prefs_cache');
317 return;
318 }
319
320 /**
321 * sets the pref, $string, to $set_to
322 * @ignore
323 */
324 function setPref($data_dir, $username, $string, $set_to) {
325 global $prefs_cache;
326
327 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
328 return;
329 }
330
331 if ($set_to === '') {
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]');
346 sqsession_register($prefs_cache , 'prefs_cache');
347 return;
348 }
349
350 /**
351 * This checks if the prefs are available
352 * @ignore
353 */
354 function checkForPrefs($data_dir, $username) {
355 $db = new dbPrefs;
356 if(isset($db->error)) {
357 $db->failQuery();
358 }
359 }
360
361 /**
362 * Writes the Signature
363 * @ignore
364 */
365 function setSig($data_dir, $username, $number, $string) {
366 if ($number == "g") {
367 $key = '___signature___';
368 } else {
369 $key = sprintf('___sig%s___', $number);
370 }
371 setPref($data_dir, $username, $key, $string);
372 return;
373 }
374
375 /**
376 * Gets the signature
377 * @ignore
378 */
379 function getSig($data_dir, $username, $number) {
380 if ($number == "g") {
381 $key = '___signature___';
382 } else {
383 $key = sprintf('___sig%d___', $number);
384 }
385 return getPref($data_dir, $username, $key);
386 }
387
388 ?>