6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains functions for manipulating user preferences
10 * stored in a database, accessed though the Pear DB layer.
12 * To use this instead of the regular prefs.php, create a
13 * database as described below, and replace prefs.php
19 * The preferences table should have tree columns:
20 * username char \ primary
24 * CREATE TABLE userprefs (user CHAR(32) NOT NULL DEFAULT '',
25 * prefkey CHAR(64) NOT NULL DEFAULT '',
26 * prefval BLOB NOT NULL DEFAULT '',
27 * primary key (user,prefkey));
29 * Configuration of databasename, username and password is done
30 * by changing $DSN below.
35 /*****************************************************************/
36 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
37 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
38 /*** + Base level indent should begin at left margin, as ***/
39 /*** the require_once below. ***/
40 /*** + All identation should consist of four space blocks ***/
41 /*** + Tab characters are evil. ***/
42 /*** + all comments should use "slash-star ... star-slash" ***/
43 /*** style -- no pound characters, no slash-slash style ***/
44 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
45 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
46 /*** + Please use ' instead of ", when possible. Note " ***/
47 /*** should always be used in _( ) function calls. ***/
48 /*** Thank you for your help making the SM code more readable. ***/
49 /*****************************************************************/
51 require_once('DB.php');
54 var $DSN = 'mysql://user@host/database';
55 var $table = 'userprefs';
60 var $default = Array('chosen_theme' => '../themes/default_theme.php',
61 'show_html_default' => '0');
68 if(isset($this->dbh
)) return true;
69 $dbh = DB
::connect($this->DSN
, true);
71 if(DB
::isError($dbh) || DB
::isWarning($dbh)) {
72 $this->error
= DB
::errorMessage($dbh);
81 function failQuery($res = NULL) {
83 printf(_("Preference database error (%s). Exiting abnormally"),
86 printf(_("Preference database error (%s). Exiting abnormally"),
87 DB
::errorMessage($res));
93 function getKey($user, $key) {
95 $query = sprintf("SELECT prefval FROM %s ".
96 "WHERE user='%s' AND prefkey='%s'",
98 $this->dbh
->quoteString($user),
99 $this->dbh
->quoteString($key));
101 $res = $this->dbh
->query($query);
102 if(DB
::isError($res))
103 $this->failQuery($res);
105 if($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
106 return $row['prefval'];
108 if(isset($this->default[$key])) {
109 return $this->default[$key];
118 function deleteKey($user, $key) {
120 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
122 $this->dbh
->quoteString($user),
123 $this->dbh
->quoteString($key));
125 $res = $this->dbh
->simpleQuery($query);
126 if(DB
::isError($res))
127 $this->failQuery($res);
129 if(substr($key, 0, 9) == 'highlight') {
130 $this->renumberHighlightList($user);
136 function setKey($user, $key, $value) {
138 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
139 "VALUES('%s','%s','%s')",
141 $this->dbh
->quoteString($user),
142 $this->dbh
->quoteString($key),
143 $this->dbh
->quoteString($value));
145 $res = $this->dbh
->simpleQuery($query);
146 if(DB
::isError($res))
147 $this->failQuery($res);
154 ** When a highlight option is deleted the preferences module
155 ** must renumber the list. This should be done somewhere else,
156 ** but it is not, so....
158 function renumberHighlightList($user) {
160 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
161 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
163 $this->dbh
->quoteString($user));
165 $res = $this->dbh
->query($query);
166 if(DB
::isError($res))
167 $this->failQuery($res);
169 // Store old data in array
171 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC
))
174 // Renumber keys of old data
176 for($i = 0; $i < count($rows) ; $i++
) {
177 $oldkey = $rows[$i]['prefkey'];
178 $newkey = substr($oldkey, 0, 9) . $hilinum;
181 if($oldkey != $newkey) {
182 $query = sprintf("UPDATE %s SET prefkey='%s' WHERE user='%s' ".
185 $this->dbh
->quoteString($newkey),
186 $this->dbh
->quoteString($user),
187 $this->dbh
->quoteString($oldkey));
189 $res = $this->dbh
->simpleQuery($query);
190 if(DB
::isError($res))
191 $this->failQuery($res);
198 } // end class dbPrefs
201 /** returns the value for the pref $string **/
202 function getPref($data_dir, $username, $string, $default ) {
204 if(isset($db->error
)) {
205 printf( _("Preference database error (%s). Exiting abnormally"),
210 return $db->getKey($username, $string);
213 /** Remove the pref $string **/
214 function removePref($data_dir, $username, $string) {
216 if(isset($db->error
)) $db->failQuery();
218 $db->deleteKey($username, $string);
222 /** sets the pref, $string, to $set_to **/
223 function setPref($data_dir, $username, $string, $set_to) {
225 if(isset($db->error
))
228 $db->setKey($username, $string, $set_to);
232 /** This checks if the prefs are available **/
233 function checkForPrefs($data_dir, $username) {
235 if(isset($db->error
))
239 /** Writes the Signature **/
240 function setSig($data_dir, $username, $string) {
242 if(isset($db->error
))
245 $db->setKey($username, "___signature___", $string);
249 /** Gets the signature **/
250 function getSig($data_dir, $username) {
252 if(isset($db->error
))
255 return $db->getKey($username, "___signature___");