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.
32 require_once('DB.php');
35 var $DSN = 'mysql://user@host/database';
36 var $table = 'userprefs';
41 var $default = Array('chosen_theme' => '../themes/default_theme.php',
42 'show_html_default' => '0');
49 if(isset($this->dbh
)) return true;
50 $dbh = DB
::connect($this->DSN
, true);
52 if(DB
::isError($dbh) || DB
::isWarning($dbh)) {
53 $this->error
= DB
::errorMessage($dbh);
62 function failQuery($res = NULL) {
64 printf(_("Preference database error (%s). Exiting abnormally"),
67 printf(_("Preference database error (%s). Exiting abnormally"),
68 DB
::errorMessage($res));
74 function getKey($user, $key) {
76 $query = sprintf("SELECT prefval FROM %s ".
77 "WHERE user='%s' AND prefkey='%s'",
79 $this->dbh
->quoteString($user),
80 $this->dbh
->quoteString($key));
82 $res = $this->dbh
->query($query);
84 $this->failQuery($res);
86 if($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
87 return $row['prefval'];
89 if(isset($this->default[$key])) {
90 return $this->default[$key];
99 function deleteKey($user, $key) {
101 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
103 $this->dbh
->quoteString($user),
104 $this->dbh
->quoteString($key));
106 $res = $this->dbh
->simpleQuery($query);
107 if(DB
::isError($res))
108 $this->failQuery($res);
110 if(substr($key, 0, 9) == 'highlight') {
111 $this->renumberHighlightList($user);
117 function setKey($user, $key, $value) {
119 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
120 "VALUES('%s','%s','%s')",
122 $this->dbh
->quoteString($user),
123 $this->dbh
->quoteString($key),
124 $this->dbh
->quoteString($value));
126 $res = $this->dbh
->simpleQuery($query);
127 if(DB
::isError($res))
128 $this->failQuery($res);
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....
139 function renumberHighlightList($user) {
141 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
142 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
144 $this->dbh
->quoteString($user));
146 $res = $this->dbh
->query($query);
147 if(DB
::isError($res))
148 $this->failQuery($res);
150 // Store old data in array
152 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC
))
155 // Renumber keys of old data
157 for($i = 0; $i < count($rows) ; $i++
) {
158 $oldkey = $rows[$i]['prefkey'];
159 $newkey = substr($oldkey, 0, 9) . $hilinum;
162 if($oldkey != $newkey) {
163 $query = sprintf("UPDATE %s SET prefkey='%s' WHERE user='%s' ".
166 $this->dbh
->quoteString($newkey),
167 $this->dbh
->quoteString($user),
168 $this->dbh
->quoteString($oldkey));
170 $res = $this->dbh
->simpleQuery($query);
171 if(DB
::isError($res))
172 $this->failQuery($res);
179 } // end class dbPrefs
182 /** returns the value for the pref $string **/
183 function getPref($data_dir, $username, $string) {
185 if(isset($db->error
)) {
186 printf(_("Preference database error (%s). Exiting abnormally"),
191 return $db->getKey($username, $string);
194 /** Remove the pref $string **/
195 function removePref($data_dir, $username, $string) {
197 if(isset($db->error
)) $db->failQuery();
199 $db->deleteKey($username, $string);
203 /** sets the pref, $string, to $set_to **/
204 function setPref($data_dir, $username, $string, $set_to) {
206 if(isset($db->error
))
209 $db->setKey($username, $string, $set_to);
213 /** This checks if the prefs are available **/
214 function checkForPrefs($data_dir, $username) {
216 if(isset($db->error
))
220 /** Writes the Signature **/
221 function setSig($data_dir, $username, $string) {
223 if(isset($db->error
))
226 $db->setKey($username, "___signature___", $string);
230 /** Gets the signature **/
231 function getSig($data_dir, $username) {
233 if(isset($db->error
))
236 return $db->getKey($username, "___signature___");