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