3d478cf3b51148b313576975db77cd2e06704f19
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 if (defined('prefs_php'))
33 define('prefs_php', true);
35 require_once('DB.php');
38 var $DSN = 'mysql://user@host/database';
39 var $table = 'userprefs';
44 var $default = Array('chosen_theme' => '../themes/default_theme.php',
45 'show_html_default' => '0');
52 if(isset($this->dbh
)) return true;
53 $dbh = DB
::connect($this->DSN
, true);
55 if(DB
::isError($dbh) || DB
::isWarning($dbh)) {
56 $this->error
= DB
::errorMessage($dbh);
65 function failQuery($res = NULL) {
67 printf(_("Preference database error (%s). Exiting abnormally"),
70 printf(_("Preference database error (%s). Exiting abnormally"),
71 DB
::errorMessage($res));
77 function getKey($user, $key) {
79 $query = sprintf("SELECT prefval FROM %s ".
80 "WHERE user='%s' AND prefkey='%s'",
82 $this->dbh
->quoteString($user),
83 $this->dbh
->quoteString($key));
85 $res = $this->dbh
->query($query);
87 $this->failQuery($res);
89 if($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
90 return $row['prefval'];
92 if(isset($this->default[$key])) {
93 return $this->default[$key];
102 function deleteKey($user, $key) {
104 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
106 $this->dbh
->quoteString($user),
107 $this->dbh
->quoteString($key));
109 $res = $this->dbh
->simpleQuery($query);
110 if(DB
::isError($res))
111 $this->failQuery($res);
113 if(substr($key, 0, 9) == 'highlight') {
114 $this->renumberHighlightList($user);
120 function setKey($user, $key, $value) {
122 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
123 "VALUES('%s','%s','%s')",
125 $this->dbh
->quoteString($user),
126 $this->dbh
->quoteString($key),
127 $this->dbh
->quoteString($value));
129 $res = $this->dbh
->simpleQuery($query);
130 if(DB
::isError($res))
131 $this->failQuery($res);
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....
142 function renumberHighlightList($user) {
144 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
145 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
147 $this->dbh
->quoteString($user));
149 $res = $this->dbh
->query($query);
150 if(DB
::isError($res))
151 $this->failQuery($res);
153 // Store old data in array
155 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC
))
158 // Renumber keys of old data
160 for($i = 0; $i < count($rows) ; $i++
) {
161 $oldkey = $rows[$i]['prefkey'];
162 $newkey = substr($oldkey, 0, 9) . $hilinum;
165 if($oldkey != $newkey) {
166 $query = sprintf("UPDATE %s SET prefkey='%s' WHERE user='%s' ".
169 $this->dbh
->quoteString($newkey),
170 $this->dbh
->quoteString($user),
171 $this->dbh
->quoteString($oldkey));
173 $res = $this->dbh
->simpleQuery($query);
174 if(DB
::isError($res))
175 $this->failQuery($res);
182 } // end class dbPrefs
185 /** returns the value for the pref $string **/
186 function getPref($data_dir, $username, $string) {
188 if(isset($db->error
)) {
189 printf(_("Preference database error (%s). Exiting abnormally"),
194 return $db->getKey($username, $string);
197 /** Remove the pref $string **/
198 function removePref($data_dir, $username, $string) {
200 if(isset($db->error
)) $db->failQuery();
202 $db->deleteKey($username, $string);
206 /** sets the pref, $string, to $set_to **/
207 function setPref($data_dir, $username, $string, $set_to) {
209 if(isset($db->error
))
212 $db->setKey($username, $string, $set_to);
216 /** This checks if the prefs are available **/
217 function checkForPrefs($data_dir, $username) {
219 if(isset($db->error
))
223 /** Writes the Signature **/
224 function setSig($data_dir, $username, $string) {
226 if(isset($db->error
))
229 $db->setKey($username, "___signature___", $string);
233 /** Gets the signature **/
234 function getSig($data_dir, $username) {
236 if(isset($db->error
))
239 return $db->getKey($username, "___signature___");