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