po update
[squirrelmail.git] / functions / db_prefs.php
... / ...
CommitLineData
1<?php
2
3/*
4 * db_prefs.php
5 *
6 * Copyright (c) 1999-2002 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 * 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 *
24 * CREATE TABLE userprefs (user CHAR(128) NOT NULL DEFAULT '',
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
30 * by using conf.pl or the administrator plugin
31 *
32 * $Id$
33 */
34
35require_once('DB.php');
36require_once('../config/config.php');
37
38global $prefs_are_cached, $prefs_cache;
39
40function cachePrefValues($username) {
41 global $prefs_are_cached, $prefs_cache;
42
43 if ($prefs_are_cached) {
44 return;
45 }
46
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 }
56
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 {
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() {
80 global $prefs_dsn, $prefs_table;
81
82 if(isset($this->dbh)) {
83 return true;
84 }
85
86 if (!empty($prefs_table)) {
87 $this->table = $prefs_table;
88 }
89 $dbh = DB::connect($prefs_dsn);
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;
98 }
99
100 function failQuery($res = NULL) {
101 if($res == NULL) {
102 printf(_("Preference database error (%s). Exiting abnormally"),
103 $this->error);
104 } else {
105 printf(_("Preference database error (%s). Exiting abnormally"),
106 DB::errorMessage($res));
107 }
108 exit;
109 }
110
111
112 function getKey($user, $key, $default = '') {
113 global $prefs_cache;
114
115 cachePrefValues($user);
116
117 if (isset($prefs_cache[$key])) {
118 return $prefs_cache[$key];
119 } else {
120 if (isset($this->default[$key])) {
121 return $this->default[$key];
122 } else {
123 return $default;
124 }
125 }
126 }
127
128 function deleteKey($user, $key) {
129 global $prefs_cache;
130
131 $this->open();
132 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
133 $this->table,
134 $this->dbh->quoteString($user),
135 $this->dbh->quoteString($key));
136
137 $res = $this->dbh->simpleQuery($query);
138 if(DB::isError($res)) {
139 $this->failQuery($res);
140 }
141
142 unset($prefs_cache[$key]);
143
144 if(substr($key, 0, 9) == 'highlight') {
145 $this->renumberHighlightList($user);
146 }
147
148 return true;
149 }
150
151 function setKey($user, $key, $value) {
152 $this->open();
153 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
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)) {
162 $this->failQuery($res);
163 }
164
165 return true;
166 }
167
168 function fillPrefsCache($user) {
169 global $prefs_cache;
170
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) {
194 $this->open();
195 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
196 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
197 $this->table,
198 $this->dbh->quoteString($user));
199
200 $res = $this->dbh->query($query);
201 if(DB::isError($res)) {
202 $this->failQuery($res);
203 }
204
205 /* Store old data in array */
206 $rows = Array();
207 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
208 $rows[] = $row;
209 }
210
211 /* Renumber keys of old data */
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) {
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 }
230 }
231 }
232
233 return;
234 }
235
236} /* end class dbPrefs */
237
238
239/* returns the value for the pref $string */
240function getPref($data_dir, $username, $string, $default = '') {
241 $db = new dbPrefs;
242 if(isset($db->error)) {
243 printf( _("Preference database error (%s). Exiting abnormally"),
244 $db->error);
245 exit;
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
266 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
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
318?>