Tyler: This is not very original but should do the trick
[squirrelmail.git] / functions / db_prefs.php
1 <?php
2 /**
3 * db_prefs.php
4 *
5 * Copyright (c) 1999-2001 The Squirrelmail Development Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * This contains functions for manipulating user preferences
9 * stored in a database, accessed though the Pear DB layer.
10 *
11 * To use this instead of the regular prefs.php, create a
12 * database as described below, and replace prefs.php
13 * with this file.
14 *
15 * Database:
16 * ---------
17 *
18 * The preferences table should have tree columns:
19 * username char \ primary
20 * prefkey char / key
21 * prefval blob
22 *
23 * CREATE TABLE userprefs (user CHAR(32) NOT NULL DEFAULT '',
24 * prefkey CHAR(64) NOT NULL DEFAULT '',
25 * prefval BLOB NOT NULL DEFAULT '',
26 * primary key (user,prefkey));
27 *
28 * Configuration of databasename, username and password is done
29 * by changing $DSN below.
30 *
31 * $Id$
32 */
33
34 require_once('DB.php');
35
36 class dbPrefs {
37 var $DSN = 'mysql://user@host/database';
38 var $table = 'userprefs';
39
40 var $dbh = NULL;
41 var $error = NULL;
42
43 var $default = Array('chosen_theme' => '../themes/default_theme.php',
44 'show_html_default' => '0');
45
46 function dbPrefs() {
47 $this->open();
48 }
49
50 function open() {
51 if(isset($this->dbh)) return true;
52 $dbh = DB::connect($this->DSN, true);
53
54 if(DB::isError($dbh) || DB::isWarning($dbh)) {
55 $this->error = DB::errorMessage($dbh);
56 return false;
57 }
58
59 $this->dbh = $dbh;
60 return true;
61 }
62
63
64 function failQuery($res = NULL) {
65 if($res == NULL) {
66 printf(_("Preference database error (%s). Exiting abnormally"),
67 $this->error);
68 } else {
69 printf(_("Preference database error (%s). Exiting abnormally"),
70 DB::errorMessage($res));
71 }
72 exit;
73 }
74
75
76 function getKey($user, $key) {
77 $this->open();
78 $query = sprintf("SELECT prefval FROM %s ".
79 "WHERE user='%s' AND prefkey='%s'",
80 $this->table,
81 $this->dbh->quoteString($user),
82 $this->dbh->quoteString($key));
83
84 $res = $this->dbh->query($query);
85 if(DB::isError($res))
86 $this->failQuery($res);
87
88 if($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
89 return $row['prefval'];
90 } else {
91 if(isset($this->default[$key])) {
92 return $this->default[$key];
93 } else {
94 return '';
95 }
96 }
97
98 return '';
99 }
100
101 function deleteKey($user, $key) {
102 $this->open();
103 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
104 $this->table,
105 $this->dbh->quoteString($user),
106 $this->dbh->quoteString($key));
107
108 $res = $this->dbh->simpleQuery($query);
109 if(DB::isError($res))
110 $this->failQuery($res);
111
112 if(substr($key, 0, 9) == 'highlight') {
113 $this->renumberHighlightList($user);
114 }
115
116 return true;
117 }
118
119 function setKey($user, $key, $value) {
120 $this->open();
121 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
122 "VALUES('%s','%s','%s')",
123 $this->table,
124 $this->dbh->quoteString($user),
125 $this->dbh->quoteString($key),
126 $this->dbh->quoteString($value));
127
128 $res = $this->dbh->simpleQuery($query);
129 if(DB::isError($res))
130 $this->failQuery($res);
131
132 return true;
133 }
134
135
136 /**
137 ** When a highlight option is deleted the preferences module
138 ** must renumber the list. This should be done somewhere else,
139 ** but it is not, so....
140 **/
141 function renumberHighlightList($user) {
142 $this->open();
143 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
144 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
145 $this->table,
146 $this->dbh->quoteString($user));
147
148 $res = $this->dbh->query($query);
149 if(DB::isError($res))
150 $this->failQuery($res);
151
152 // Store old data in array
153 $rows = Array();
154 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
155 $rows[] = $row;
156
157 // Renumber keys of old data
158 $hilinum = 0;
159 for($i = 0; $i < count($rows) ; $i++) {
160 $oldkey = $rows[$i]['prefkey'];
161 $newkey = substr($oldkey, 0, 9) . $hilinum;
162 $hilinum++;
163
164 if($oldkey != $newkey) {
165 $query = sprintf("UPDATE %s SET prefkey='%s' WHERE user='%s' ".
166 "AND prefkey='%s'",
167 $this->table,
168 $this->dbh->quoteString($newkey),
169 $this->dbh->quoteString($user),
170 $this->dbh->quoteString($oldkey));
171
172 $res = $this->dbh->simpleQuery($query);
173 if(DB::isError($res))
174 $this->failQuery($res);
175 }
176 }
177
178 return;
179 }
180
181 } // end class dbPrefs
182
183
184 /** returns the value for the pref $string **/
185 function getPref($data_dir, $username, $string, $default ) {
186 $db = new dbPrefs;
187 if(isset($db->error)) {
188 printf( _("Preference database error (%s). Exiting abnormally"),
189 $db->error);
190 exit;
191 }
192
193 return $db->getKey($username, $string);
194 }
195
196 /** Remove the pref $string **/
197 function removePref($data_dir, $username, $string) {
198 $db = new dbPrefs;
199 if(isset($db->error)) $db->failQuery();
200
201 $db->deleteKey($username, $string);
202 return;
203 }
204
205 /** sets the pref, $string, to $set_to **/
206 function setPref($data_dir, $username, $string, $set_to) {
207 $db = new dbPrefs;
208 if(isset($db->error))
209 $db->failQuery();
210
211 $db->setKey($username, $string, $set_to);
212 return;
213 }
214
215 /** This checks if the prefs are available **/
216 function checkForPrefs($data_dir, $username) {
217 $db = new dbPrefs;
218 if(isset($db->error))
219 $db->failQuery();
220 }
221
222 /** Writes the Signature **/
223 function setSig($data_dir, $username, $string) {
224 $db = new dbPrefs;
225 if(isset($db->error))
226 $db->failQuery();
227
228 $db->setKey($username, "___signature___", $string);
229 return;
230 }
231
232 /** Gets the signature **/
233 function getSig($data_dir, $username) {
234 $db = new dbPrefs;
235 if(isset($db->error))
236 $db->failQuery();
237
238 return $db->getKey($username, "___signature___");
239 }
240
241 ?>