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