Added signature into multiple identities.
[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
b279d7f4 131 if (!$this->open()) {
132 return false;
133 }
2d367c68 134 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
370059dd 135 $this->table,
136 $this->dbh->quoteString($user),
137 $this->dbh->quoteString($key));
82474746 138
2d367c68 139 $res = $this->dbh->simpleQuery($query);
370059dd 140 if(DB::isError($res)) {
2d367c68 141 $this->failQuery($res);
370059dd 142 }
143
144 unset($prefs_cache[$key]);
82474746 145
2d367c68 146 if(substr($key, 0, 9) == 'highlight') {
147 $this->renumberHighlightList($user);
148 }
82474746 149
2d367c68 150 return true;
370059dd 151 }
82474746 152
370059dd 153 function setKey($user, $key, $value) {
b279d7f4 154 if (!$this->open()) {
155 return false;
156 }
2d367c68 157 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
370059dd 158 "VALUES('%s','%s','%s')",
159 $this->table,
160 $this->dbh->quoteString($user),
161 $this->dbh->quoteString($key),
162 $this->dbh->quoteString($value));
163
164 $res = $this->dbh->simpleQuery($query);
165 if(DB::isError($res)) {
2d367c68 166 $this->failQuery($res);
370059dd 167 }
2d367c68 168
169 return true;
370059dd 170 }
82474746 171
370059dd 172 function fillPrefsCache($user) {
173 global $prefs_cache;
2d367c68 174
b279d7f4 175 if (!$this->open()) {
176 return;
177 }
370059dd 178
179 $prefs_cache = array();
180 $query = sprintf("SELECT prefkey, prefval FROM %s ".
181 "WHERE user = '%s'",
182 $this->table,
183 $this->dbh->quoteString($user));
184 $res = $this->dbh->query($query);
185 if (DB::isError($res)) {
186 $this->failQuery($res);
187 }
188
189 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
190 $prefs_cache[$row['prefkey']] = $row['prefval'];
191 }
192 }
193
194 /*
195 * When a highlight option is deleted the preferences module
196 * must renumber the list. This should be done somewhere else,
197 * but it is not, so....
198 */
199 function renumberHighlightList($user) {
b279d7f4 200 if (!$this->open()) {
201 return;
202 }
2d367c68 203 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
370059dd 204 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
205 $this->table,
206 $this->dbh->quoteString($user));
2d367c68 207
208 $res = $this->dbh->query($query);
370059dd 209 if(DB::isError($res)) {
2d367c68 210 $this->failQuery($res);
370059dd 211 }
2d367c68 212
370059dd 213 /* Store old data in array */
2d367c68 214 $rows = Array();
370059dd 215 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
2d367c68 216 $rows[] = $row;
370059dd 217 }
2d367c68 218
370059dd 219 /* Renumber keys of old data */
2d367c68 220 $hilinum = 0;
221 for($i = 0; $i < count($rows) ; $i++) {
222 $oldkey = $rows[$i]['prefkey'];
223 $newkey = substr($oldkey, 0, 9) . $hilinum;
224 $hilinum++;
225
226 if($oldkey != $newkey) {
370059dd 227 $query = sprintf("UPDATE %s SET prefkey='%s' ".
228 "WHERE user ='%s' AND prefkey='%s'",
229 $this->table,
230 $this->dbh->quoteString($newkey),
231 $this->dbh->quoteString($user),
232 $this->dbh->quoteString($oldkey));
233
234 $res = $this->dbh->simpleQuery($query);
235 if(DB::isError($res)) {
236 $this->failQuery($res);
237 }
2d367c68 238 }
239 }
240
241 return;
370059dd 242 }
82474746 243
370059dd 244} /* end class dbPrefs */
82474746 245
246
370059dd 247/* returns the value for the pref $string */
248function getPref($data_dir, $username, $string, $default = '') {
249 $db = new dbPrefs;
250 if(isset($db->error)) {
2d367c68 251 printf( _("Preference database error (%s). Exiting abnormally"),
370059dd 252 $db->error);
2d367c68 253 exit;
370059dd 254 }
255
256 return $db->getKey($username, $string, $default);
257}
258
259/* Remove the pref $string */
260function removePref($data_dir, $username, $string) {
261 $db = new dbPrefs;
262 if(isset($db->error)) {
263 $db->failQuery();
264 }
265
266 $db->deleteKey($username, $string);
267 return;
268}
269
270/* sets the pref, $string, to $set_to */
271function setPref($data_dir, $username, $string, $set_to) {
272 global $prefs_cache;
273
4b7dd3d9 274 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
370059dd 275 return;
276 }
277
278 if ($set_to == '') {
279 removePref($data_dir, $username, $string);
280 return;
281 }
282
283 $db = new dbPrefs;
284 if(isset($db->error)) {
285 $db->failQuery();
286 }
287
288 $db->setKey($username, $string, $set_to);
289 $prefs_cache[$string] = $set_to;
290 assert_options(ASSERT_ACTIVE, 1);
291 assert_options(ASSERT_BAIL, 1);
292 assert ('$set_to == $prefs_cache[$string]');
293
294 return;
295}
296
297/* This checks if the prefs are available */
298function checkForPrefs($data_dir, $username) {
299 $db = new dbPrefs;
300 if(isset($db->error)) {
301 $db->failQuery();
302 }
303}
304
305/* Writes the Signature */
306function setSig($data_dir, $username, $string) {
307 $db = new dbPrefs;
308 if(isset($db->error)) {
309 $db->failQuery();
310 }
311
312 $db->setKey($username, '___signature___', $string);
313 return;
314}
315
316/* Gets the signature */
317function getSig($data_dir, $username) {
318 $db = new dbPrefs;
319 if(isset($db->error)) {
320 $db->failQuery();
321 }
322
323 return $db->getKey($username, '___signature___');
324}
325
82474746 326?>