5 ** This contains functions for manipulating user preferences
6 ** stored in a database, accessed though the Pear DB layer.
8 ** To use this instead of the regular prefs.php, create a
9 ** database as described below, and replace prefs.php
15 ** The preferences table should have tree columns:
16 ** username char \ primary
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));
25 ** Configuration of databasename, username and password is done
26 ** by changing $DSN below.
31 require_once('DB.php');
34 var $DSN = 'mysql://user@host/database';
35 var $table = 'userprefs';
40 var $default = Array('chosen_theme' => '../themes/default_theme.php',
41 'show_html_default' => '0');
48 if(isset($this->dbh
)) return true;
49 $dbh = DB
::connect($this->DSN
, true);
51 if(DB
::isError($dbh) || DB
::isWarning($dbh)) {
52 $this->error
= DB
::errorMessage($dbh);
61 function failQuery($res = NULL) {
63 printf(_("Preference database error (%s). Exiting abnormally"),
66 printf(_("Preference database error (%s). Exiting abnormally"),
67 DB
::errorMessage($res));
73 function getKey($user, $key) {
75 $query = sprintf("SELECT prefval FROM %s ".
76 "WHERE user='%s' AND prefkey='%s'",
78 $this->dbh
->quoteString($user),
79 $this->dbh
->quoteString($key));
81 $res = $this->dbh
->query($query);
83 $this->failQuery($res);
85 if($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
86 return $row['prefval'];
88 if(isset($this->default[$key])) {
89 return $this->default[$key];
98 function deleteKey($user, $key) {
100 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
102 $this->dbh
->quoteString($user),
103 $this->dbh
->quoteString($key));
105 $res = $this->dbh
->simpleQuery($query);
106 if(DB
::isError($res))
107 $this->failQuery($res);
109 if(substr($key, 0, 9) == 'highlight') {
110 $this->renumberHighlightList($user);
116 function setKey($user, $key, $value) {
118 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
119 "VALUES('%s','%s','%s')",
121 $this->dbh
->quoteString($user),
122 $this->dbh
->quoteString($key),
123 $this->dbh
->quoteString($value));
125 $res = $this->dbh
->simpleQuery($query);
126 if(DB
::isError($res))
127 $this->failQuery($res);
134 ** When a highlight option is deleted the preferences module
135 ** must renumber the list. This should be done somewhere else,
136 ** but it is not, so....
138 function renumberHighlightList($user) {
140 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
141 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
143 $this->dbh
->quoteString($user));
145 $res = $this->dbh
->query($query);
146 if(DB
::isError($res))
147 $this->failQuery($res);
149 // Store old data in array
151 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC
))
154 // Renumber keys of old data
156 for($i = 0; $i < count($rows) ; $i++
) {
157 $oldkey = $rows[$i]['prefkey'];
158 $newkey = substr($oldkey, 0, 9) . $hilinum;
161 if($oldkey != $newkey) {
162 $query = sprintf("UPDATE %s SET prefkey='%s' WHERE user='%s' ".
165 $this->dbh
->quoteString($newkey),
166 $this->dbh
->quoteString($user),
167 $this->dbh
->quoteString($oldkey));
169 $res = $this->dbh
->simpleQuery($query);
170 if(DB
::isError($res))
171 $this->failQuery($res);
178 } // end class dbPrefs
181 /** returns the value for the pref $string **/
182 function getPref($data_dir, $username, $string, $default ) {
184 if(isset($db->error
)) {
185 printf( _("Preference database error (%s). Exiting abnormally"),
190 return $db->getKey($username, $string);
193 /** Remove the pref $string **/
194 function removePref($data_dir, $username, $string) {
196 if(isset($db->error
)) $db->failQuery();
198 $db->deleteKey($username, $string);
202 /** sets the pref, $string, to $set_to **/
203 function setPref($data_dir, $username, $string, $set_to) {
205 if(isset($db->error
))
208 $db->setKey($username, $string, $set_to);
212 /** This checks if the prefs are available **/
213 function checkForPrefs($data_dir, $username) {
215 if(isset($db->error
))
219 /** Writes the Signature **/
220 function setSig($data_dir, $username, $string) {
222 if(isset($db->error
))
225 $db->setKey($username, "___signature___", $string);
229 /** Gets the signature **/
230 function getSig($data_dir, $username) {
232 if(isset($db->error
))
235 return $db->getKey($username, "___signature___");