po update
[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 *
19 * The preferences table should have tree columns:
20 * username char \ primary
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
35586184 35require_once('DB.php');
3499f99f 36require_once('../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
370059dd 47 session_unregister('prefs_cache');
48 session_unregister('prefs_are_cached');
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
66 session_register('prefs_cache');
67 session_register('prefs_are_cached');
68}
69
70class dbPrefs {
370059dd 71 var $table = 'userprefs';
72
73 var $dbh = NULL;
74 var $error = NULL;
75
76 var $default = Array('chosen_theme' => '../themes/default_theme.php',
77 'show_html_default' => '0');
78
79 function open() {
3499f99f 80 global $prefs_dsn, $prefs_table;
81
370059dd 82 if(isset($this->dbh)) {
83 return true;
84 }
3499f99f 85
86 if (!empty($prefs_table)) {
87 $this->table = $prefs_table;
88 }
89 $dbh = DB::connect($prefs_dsn);
2d367c68 90
91 if(DB::isError($dbh) || DB::isWarning($dbh)) {
92 $this->error = DB::errorMessage($dbh);
93 return false;
94 }
95
96 $this->dbh = $dbh;
97 return true;
370059dd 98 }
82474746 99
370059dd 100 function failQuery($res = NULL) {
2d367c68 101 if($res == NULL) {
102 printf(_("Preference database error (%s). Exiting abnormally"),
370059dd 103 $this->error);
2d367c68 104 } else {
105 printf(_("Preference database error (%s). Exiting abnormally"),
370059dd 106 DB::errorMessage($res));
2d367c68 107 }
108 exit;
370059dd 109 }
82474746 110
111
370059dd 112 function getKey($user, $key, $default = '') {
113 global $prefs_cache;
2d367c68 114
370059dd 115 cachePrefValues($user);
2d367c68 116
370059dd 117 if (isset($prefs_cache[$key])) {
118 return $prefs_cache[$key];
2d367c68 119 } else {
62337234 120 if (isset($this->default[$key])) {
121 return $this->default[$key];
122 } else {
123 return $default;
124 }
2d367c68 125 }
370059dd 126 }
2d367c68 127
370059dd 128 function deleteKey($user, $key) {
129 global $prefs_cache;
82474746 130
2d367c68 131 $this->open();
132 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
370059dd 133 $this->table,
134 $this->dbh->quoteString($user),
135 $this->dbh->quoteString($key));
82474746 136
2d367c68 137 $res = $this->dbh->simpleQuery($query);
370059dd 138 if(DB::isError($res)) {
2d367c68 139 $this->failQuery($res);
370059dd 140 }
141
142 unset($prefs_cache[$key]);
82474746 143
2d367c68 144 if(substr($key, 0, 9) == 'highlight') {
145 $this->renumberHighlightList($user);
146 }
82474746 147
2d367c68 148 return true;
370059dd 149 }
82474746 150
370059dd 151 function setKey($user, $key, $value) {
2d367c68 152 $this->open();
153 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
370059dd 154 "VALUES('%s','%s','%s')",
155 $this->table,
156 $this->dbh->quoteString($user),
157 $this->dbh->quoteString($key),
158 $this->dbh->quoteString($value));
159
160 $res = $this->dbh->simpleQuery($query);
161 if(DB::isError($res)) {
2d367c68 162 $this->failQuery($res);
370059dd 163 }
2d367c68 164
165 return true;
370059dd 166 }
82474746 167
370059dd 168 function fillPrefsCache($user) {
169 global $prefs_cache;
2d367c68 170
370059dd 171 $this->open();
172
173 $prefs_cache = array();
174 $query = sprintf("SELECT prefkey, prefval FROM %s ".
175 "WHERE user = '%s'",
176 $this->table,
177 $this->dbh->quoteString($user));
178 $res = $this->dbh->query($query);
179 if (DB::isError($res)) {
180 $this->failQuery($res);
181 }
182
183 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
184 $prefs_cache[$row['prefkey']] = $row['prefval'];
185 }
186 }
187
188 /*
189 * When a highlight option is deleted the preferences module
190 * must renumber the list. This should be done somewhere else,
191 * but it is not, so....
192 */
193 function renumberHighlightList($user) {
2d367c68 194 $this->open();
195 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
370059dd 196 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
197 $this->table,
198 $this->dbh->quoteString($user));
2d367c68 199
200 $res = $this->dbh->query($query);
370059dd 201 if(DB::isError($res)) {
2d367c68 202 $this->failQuery($res);
370059dd 203 }
2d367c68 204
370059dd 205 /* Store old data in array */
2d367c68 206 $rows = Array();
370059dd 207 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
2d367c68 208 $rows[] = $row;
370059dd 209 }
2d367c68 210
370059dd 211 /* Renumber keys of old data */
2d367c68 212 $hilinum = 0;
213 for($i = 0; $i < count($rows) ; $i++) {
214 $oldkey = $rows[$i]['prefkey'];
215 $newkey = substr($oldkey, 0, 9) . $hilinum;
216 $hilinum++;
217
218 if($oldkey != $newkey) {
370059dd 219 $query = sprintf("UPDATE %s SET prefkey='%s' ".
220 "WHERE user ='%s' AND prefkey='%s'",
221 $this->table,
222 $this->dbh->quoteString($newkey),
223 $this->dbh->quoteString($user),
224 $this->dbh->quoteString($oldkey));
225
226 $res = $this->dbh->simpleQuery($query);
227 if(DB::isError($res)) {
228 $this->failQuery($res);
229 }
2d367c68 230 }
231 }
232
233 return;
370059dd 234 }
82474746 235
370059dd 236} /* end class dbPrefs */
82474746 237
238
370059dd 239/* returns the value for the pref $string */
240function getPref($data_dir, $username, $string, $default = '') {
241 $db = new dbPrefs;
242 if(isset($db->error)) {
2d367c68 243 printf( _("Preference database error (%s). Exiting abnormally"),
370059dd 244 $db->error);
2d367c68 245 exit;
370059dd 246 }
247
248 return $db->getKey($username, $string, $default);
249}
250
251/* Remove the pref $string */
252function removePref($data_dir, $username, $string) {
253 $db = new dbPrefs;
254 if(isset($db->error)) {
255 $db->failQuery();
256 }
257
258 $db->deleteKey($username, $string);
259 return;
260}
261
262/* sets the pref, $string, to $set_to */
263function setPref($data_dir, $username, $string, $set_to) {
264 global $prefs_cache;
265
4b7dd3d9 266 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
370059dd 267 return;
268 }
269
270 if ($set_to == '') {
271 removePref($data_dir, $username, $string);
272 return;
273 }
274
275 $db = new dbPrefs;
276 if(isset($db->error)) {
277 $db->failQuery();
278 }
279
280 $db->setKey($username, $string, $set_to);
281 $prefs_cache[$string] = $set_to;
282 assert_options(ASSERT_ACTIVE, 1);
283 assert_options(ASSERT_BAIL, 1);
284 assert ('$set_to == $prefs_cache[$string]');
285
286 return;
287}
288
289/* This checks if the prefs are available */
290function checkForPrefs($data_dir, $username) {
291 $db = new dbPrefs;
292 if(isset($db->error)) {
293 $db->failQuery();
294 }
295}
296
297/* Writes the Signature */
298function setSig($data_dir, $username, $string) {
299 $db = new dbPrefs;
300 if(isset($db->error)) {
301 $db->failQuery();
302 }
303
304 $db->setKey($username, '___signature___', $string);
305 return;
306}
307
308/* Gets the signature */
309function getSig($data_dir, $username) {
310 $db = new dbPrefs;
311 if(isset($db->error)) {
312 $db->failQuery();
313 }
314
315 return $db->getKey($username, '___signature___');
316}
317
82474746 318?>