Added All messages display
[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 /*****************************************************************/
35 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
36 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
37 /*** + Base level indent should begin at left margin, as ***/
38 /*** the require_once below. ***/
39 /*** + All identation should consist of four space blocks ***/
40 /*** + Tab characters are evil. ***/
41 /*** + all comments should use "slash-star ... star-slash" ***/
42 /*** style -- no pound characters, no slash-slash style ***/
43 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
44 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
45 /*** + Please use ' instead of ", when possible. Note " ***/
46 /*** should always be used in _( ) function calls. ***/
47 /*** Thank you for your help making the SM code more readable. ***/
48 /*****************************************************************/
49
50 require_once('DB.php');
51
52 class dbPrefs {
53 var $DSN = 'mysql://user@host/database';
54 var $table = 'userprefs';
55
56 var $dbh = NULL;
57 var $error = NULL;
58
59 var $default = Array('chosen_theme' => '../themes/default_theme.php',
60 'show_html_default' => '0');
61
62 function dbPrefs() {
63 $this->open();
64 }
65
66 function open() {
67 if(isset($this->dbh)) return true;
68 $dbh = DB::connect($this->DSN, true);
69
70 if(DB::isError($dbh) || DB::isWarning($dbh)) {
71 $this->error = DB::errorMessage($dbh);
72 return false;
73 }
74
75 $this->dbh = $dbh;
76 return true;
77 }
78
79
80 function failQuery($res = NULL) {
81 if($res == NULL) {
82 printf(_("Preference database error (%s). Exiting abnormally"),
83 $this->error);
84 } else {
85 printf(_("Preference database error (%s). Exiting abnormally"),
86 DB::errorMessage($res));
87 }
88 exit;
89 }
90
91
92 function getKey($user, $key) {
93 $this->open();
94 $query = sprintf("SELECT prefval FROM %s ".
95 "WHERE user='%s' AND prefkey='%s'",
96 $this->table,
97 $this->dbh->quoteString($user),
98 $this->dbh->quoteString($key));
99
100 $res = $this->dbh->query($query);
101 if(DB::isError($res))
102 $this->failQuery($res);
103
104 if($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
105 return $row['prefval'];
106 } else {
107 if(isset($this->default[$key])) {
108 return $this->default[$key];
109 } else {
110 return '';
111 }
112 }
113
114 return '';
115 }
116
117 function deleteKey($user, $key) {
118 $this->open();
119 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
120 $this->table,
121 $this->dbh->quoteString($user),
122 $this->dbh->quoteString($key));
123
124 $res = $this->dbh->simpleQuery($query);
125 if(DB::isError($res))
126 $this->failQuery($res);
127
128 if(substr($key, 0, 9) == 'highlight') {
129 $this->renumberHighlightList($user);
130 }
131
132 return true;
133 }
134
135 function setKey($user, $key, $value) {
136 $this->open();
137 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
138 "VALUES('%s','%s','%s')",
139 $this->table,
140 $this->dbh->quoteString($user),
141 $this->dbh->quoteString($key),
142 $this->dbh->quoteString($value));
143
144 $res = $this->dbh->simpleQuery($query);
145 if(DB::isError($res))
146 $this->failQuery($res);
147
148 return true;
149 }
150
151
152 /**
153 ** When a highlight option is deleted the preferences module
154 ** must renumber the list. This should be done somewhere else,
155 ** but it is not, so....
156 **/
157 function renumberHighlightList($user) {
158 $this->open();
159 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
160 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
161 $this->table,
162 $this->dbh->quoteString($user));
163
164 $res = $this->dbh->query($query);
165 if(DB::isError($res))
166 $this->failQuery($res);
167
168 // Store old data in array
169 $rows = Array();
170 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
171 $rows[] = $row;
172
173 // Renumber keys of old data
174 $hilinum = 0;
175 for($i = 0; $i < count($rows) ; $i++) {
176 $oldkey = $rows[$i]['prefkey'];
177 $newkey = substr($oldkey, 0, 9) . $hilinum;
178 $hilinum++;
179
180 if($oldkey != $newkey) {
181 $query = sprintf("UPDATE %s SET prefkey='%s' WHERE user='%s' ".
182 "AND prefkey='%s'",
183 $this->table,
184 $this->dbh->quoteString($newkey),
185 $this->dbh->quoteString($user),
186 $this->dbh->quoteString($oldkey));
187
188 $res = $this->dbh->simpleQuery($query);
189 if(DB::isError($res))
190 $this->failQuery($res);
191 }
192 }
193
194 return;
195 }
196
197 } // end class dbPrefs
198
199
200 /** returns the value for the pref $string **/
201 function getPref($data_dir, $username, $string, $default ) {
202 $db = new dbPrefs;
203 if(isset($db->error)) {
204 printf( _("Preference database error (%s). Exiting abnormally"),
205 $db->error);
206 exit;
207 }
208
209 return $db->getKey($username, $string);
210 }
211
212 /** Remove the pref $string **/
213 function removePref($data_dir, $username, $string) {
214 $db = new dbPrefs;
215 if(isset($db->error)) $db->failQuery();
216
217 $db->deleteKey($username, $string);
218 return;
219 }
220
221 /** sets the pref, $string, to $set_to **/
222 function setPref($data_dir, $username, $string, $set_to) {
223 $db = new dbPrefs;
224 if(isset($db->error))
225 $db->failQuery();
226
227 $db->setKey($username, $string, $set_to);
228 return;
229 }
230
231 /** This checks if the prefs are available **/
232 function checkForPrefs($data_dir, $username) {
233 $db = new dbPrefs;
234 if(isset($db->error))
235 $db->failQuery();
236 }
237
238 /** Writes the Signature **/
239 function setSig($data_dir, $username, $string) {
240 $db = new dbPrefs;
241 if(isset($db->error))
242 $db->failQuery();
243
244 $db->setKey($username, "___signature___", $string);
245 return;
246 }
247
248 /** Gets the signature **/
249 function getSig($data_dir, $username) {
250 $db = new dbPrefs;
251 if(isset($db->error))
252 $db->failQuery();
253
254 return $db->getKey($username, "___signature___");
255 }
256
257 ?>