5243649f766c5c8785f6e6a74ec301793b3a05e6
5 * Copyright (c) 1999-2001 The Squirrelmail Development Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * This contains functions for manipulating user preferences
9 * stored in a database, accessed though the Pear DB layer.
11 * To use this instead of the regular prefs.php, create a
12 * database as described below, and replace prefs.php
18 * The preferences table should have tree columns:
19 * username char \ primary
23 * CREATE TABLE userprefs (user CHAR(32) NOT NULL DEFAULT '',
24 * prefkey CHAR(64) NOT NULL DEFAULT '',
25 * prefval BLOB NOT NULL DEFAULT '',
26 * primary key (user,prefkey));
28 * Configuration of databasename, username and password is done
29 * by changing $DSN below.
34 require_once('DB.php');
37 var $DSN = 'mysql://user@host/database';
38 var $table = 'userprefs';
43 var $default = Array('chosen_theme' => '../themes/default_theme.php',
44 'show_html_default' => '0');
51 if(isset($this->dbh
)) return true;
52 $dbh = DB
::connect($this->DSN
, true);
54 if(DB
::isError($dbh) || DB
::isWarning($dbh)) {
55 $this->error
= DB
::errorMessage($dbh);
64 function failQuery($res = NULL) {
66 printf(_("Preference database error (%s). Exiting abnormally"),
69 printf(_("Preference database error (%s). Exiting abnormally"),
70 DB
::errorMessage($res));
76 function getKey($user, $key) {
78 $query = sprintf("SELECT prefval FROM %s ".
79 "WHERE user='%s' AND prefkey='%s'",
81 $this->dbh
->quoteString($user),
82 $this->dbh
->quoteString($key));
84 $res = $this->dbh
->query($query);
86 $this->failQuery($res);
88 if($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
89 return $row['prefval'];
91 if(isset($this->default[$key])) {
92 return $this->default[$key];
101 function deleteKey($user, $key) {
103 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
105 $this->dbh
->quoteString($user),
106 $this->dbh
->quoteString($key));
108 $res = $this->dbh
->simpleQuery($query);
109 if(DB
::isError($res))
110 $this->failQuery($res);
112 if(substr($key, 0, 9) == 'highlight') {
113 $this->renumberHighlightList($user);
119 function setKey($user, $key, $value) {
121 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
122 "VALUES('%s','%s','%s')",
124 $this->dbh
->quoteString($user),
125 $this->dbh
->quoteString($key),
126 $this->dbh
->quoteString($value));
128 $res = $this->dbh
->simpleQuery($query);
129 if(DB
::isError($res))
130 $this->failQuery($res);
137 ** When a highlight option is deleted the preferences module
138 ** must renumber the list. This should be done somewhere else,
139 ** but it is not, so....
141 function renumberHighlightList($user) {
143 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
144 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
146 $this->dbh
->quoteString($user));
148 $res = $this->dbh
->query($query);
149 if(DB
::isError($res))
150 $this->failQuery($res);
152 // Store old data in array
154 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC
))
157 // Renumber keys of old data
159 for($i = 0; $i < count($rows) ; $i++
) {
160 $oldkey = $rows[$i]['prefkey'];
161 $newkey = substr($oldkey, 0, 9) . $hilinum;
164 if($oldkey != $newkey) {
165 $query = sprintf("UPDATE %s SET prefkey='%s' WHERE user='%s' ".
168 $this->dbh
->quoteString($newkey),
169 $this->dbh
->quoteString($user),
170 $this->dbh
->quoteString($oldkey));
172 $res = $this->dbh
->simpleQuery($query);
173 if(DB
::isError($res))
174 $this->failQuery($res);
181 } // end class dbPrefs
184 /** returns the value for the pref $string **/
185 function getPref($data_dir, $username, $string, $default ) {
187 if(isset($db->error
)) {
188 printf( _("Preference database error (%s). Exiting abnormally"),
193 return $db->getKey($username, $string);
196 /** Remove the pref $string **/
197 function removePref($data_dir, $username, $string) {
199 if(isset($db->error
)) $db->failQuery();
201 $db->deleteKey($username, $string);
205 /** sets the pref, $string, to $set_to **/
206 function setPref($data_dir, $username, $string, $set_to) {
208 if(isset($db->error
))
211 $db->setKey($username, $string, $set_to);
215 /** This checks if the prefs are available **/
216 function checkForPrefs($data_dir, $username) {
218 if(isset($db->error
))
222 /** Writes the Signature **/
223 function setSig($data_dir, $username, $string) {
225 if(isset($db->error
))
228 $db->setKey($username, "___signature___", $string);
232 /** Gets the signature **/
233 function getSig($data_dir, $username) {
235 if(isset($db->error
))
238 return $db->getKey($username, "___signature___");