Disable the show images stuff while reading html messages.
[squirrelmail.git] / functions / db_prefs.php
CommitLineData
82474746 1<?php
15e6162e 2
35586184 3/**
4 * db_prefs.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains functions for manipulating user preferences
10 * stored in a database, accessed though the Pear DB layer.
11 *
12 * To use this instead of the regular prefs.php, create a
13 * database as described below, and replace prefs.php
14 * with this file.
15 *
16 * Database:
17 * ---------
18 *
19 * The preferences table should have tree columns:
20 * username char \ primary
21 * prefkey char / key
22 * prefval blob
23 *
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));
28 *
29 * Configuration of databasename, username and password is done
30 * by changing $DSN below.
31 *
32 * $Id$
33 */
34
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/*****************************************************************/
50
51require_once('DB.php');
52
53class dbPrefs {
82474746 54 var $DSN = 'mysql://user@host/database';
55 var $table = 'userprefs';
2d367c68 56
82474746 57 var $dbh = NULL;
58 var $error = NULL;
59
60 var $default = Array('chosen_theme' => '../themes/default_theme.php',
2d367c68 61 'show_html_default' => '0');
62
82474746 63 function dbPrefs() {
2d367c68 64 $this->open();
82474746 65 }
2d367c68 66
82474746 67 function open() {
2d367c68 68 if(isset($this->dbh)) return true;
69 $dbh = DB::connect($this->DSN, true);
70
71 if(DB::isError($dbh) || DB::isWarning($dbh)) {
72 $this->error = DB::errorMessage($dbh);
73 return false;
74 }
75
76 $this->dbh = $dbh;
77 return true;
82474746 78 }
2d367c68 79
82474746 80
81 function failQuery($res = NULL) {
2d367c68 82 if($res == NULL) {
83 printf(_("Preference database error (%s). Exiting abnormally"),
84 $this->error);
85 } else {
86 printf(_("Preference database error (%s). Exiting abnormally"),
87 DB::errorMessage($res));
88 }
89 exit;
82474746 90 }
91
92
93 function getKey($user, $key) {
2d367c68 94 $this->open();
95 $query = sprintf("SELECT prefval FROM %s ".
96 "WHERE user='%s' AND prefkey='%s'",
97 $this->table,
98 $this->dbh->quoteString($user),
99 $this->dbh->quoteString($key));
100
101 $res = $this->dbh->query($query);
102 if(DB::isError($res))
103 $this->failQuery($res);
104
105 if($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
106 return $row['prefval'];
107 } else {
108 if(isset($this->default[$key])) {
109 return $this->default[$key];
110 } else {
111 return '';
112 }
113 }
114
115 return '';
82474746 116 }
117
118 function deleteKey($user, $key) {
2d367c68 119 $this->open();
120 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
121 $this->table,
122 $this->dbh->quoteString($user),
123 $this->dbh->quoteString($key));
82474746 124
2d367c68 125 $res = $this->dbh->simpleQuery($query);
126 if(DB::isError($res))
127 $this->failQuery($res);
82474746 128
2d367c68 129 if(substr($key, 0, 9) == 'highlight') {
130 $this->renumberHighlightList($user);
131 }
82474746 132
2d367c68 133 return true;
82474746 134 }
135
136 function setKey($user, $key, $value) {
2d367c68 137 $this->open();
138 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
139 "VALUES('%s','%s','%s')",
140 $this->table,
141 $this->dbh->quoteString($user),
142 $this->dbh->quoteString($key),
143 $this->dbh->quoteString($value));
144
145 $res = $this->dbh->simpleQuery($query);
146 if(DB::isError($res))
147 $this->failQuery($res);
148
149 return true;
82474746 150 }
151
2d367c68 152
82474746 153 /**
154 ** When a highlight option is deleted the preferences module
155 ** must renumber the list. This should be done somewhere else,
2d367c68 156 ** but it is not, so....
82474746 157 **/
158 function renumberHighlightList($user) {
2d367c68 159 $this->open();
160 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
161 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
162 $this->table,
163 $this->dbh->quoteString($user));
164
165 $res = $this->dbh->query($query);
166 if(DB::isError($res))
167 $this->failQuery($res);
168
169 // Store old data in array
170 $rows = Array();
171 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
172 $rows[] = $row;
173
174 // Renumber keys of old data
175 $hilinum = 0;
176 for($i = 0; $i < count($rows) ; $i++) {
177 $oldkey = $rows[$i]['prefkey'];
178 $newkey = substr($oldkey, 0, 9) . $hilinum;
179 $hilinum++;
180
181 if($oldkey != $newkey) {
182 $query = sprintf("UPDATE %s SET prefkey='%s' WHERE user='%s' ".
183 "AND prefkey='%s'",
184 $this->table,
185 $this->dbh->quoteString($newkey),
186 $this->dbh->quoteString($user),
187 $this->dbh->quoteString($oldkey));
188
189 $res = $this->dbh->simpleQuery($query);
190 if(DB::isError($res))
191 $this->failQuery($res);
192 }
193 }
194
195 return;
82474746 196 }
197
198 } // end class dbPrefs
199
200
201 /** returns the value for the pref $string **/
45cfc124 202 function getPref($data_dir, $username, $string, $default ) {
82474746 203 $db = new dbPrefs;
204 if(isset($db->error)) {
2d367c68 205 printf( _("Preference database error (%s). Exiting abnormally"),
206 $db->error);
207 exit;
82474746 208 }
209
210 return $db->getKey($username, $string);
211 }
212
213 /** Remove the pref $string **/
214 function removePref($data_dir, $username, $string) {
215 $db = new dbPrefs;
216 if(isset($db->error)) $db->failQuery();
217
218 $db->deleteKey($username, $string);
219 return;
220 }
2d367c68 221
82474746 222 /** sets the pref, $string, to $set_to **/
223 function setPref($data_dir, $username, $string, $set_to) {
224 $db = new dbPrefs;
225 if(isset($db->error))
2d367c68 226 $db->failQuery();
82474746 227
228 $db->setKey($username, $string, $set_to);
229 return;
230 }
231
232 /** This checks if the prefs are available **/
233 function checkForPrefs($data_dir, $username) {
234 $db = new dbPrefs;
235 if(isset($db->error))
2d367c68 236 $db->failQuery();
82474746 237 }
238
239 /** Writes the Signature **/
240 function setSig($data_dir, $username, $string) {
241 $db = new dbPrefs;
2d367c68 242 if(isset($db->error))
243 $db->failQuery();
82474746 244
245 $db->setKey($username, "___signature___", $string);
246 return;
247 }
248
249 /** Gets the signature **/
250 function getSig($data_dir, $username) {
251 $db = new dbPrefs;
252 if(isset($db->error))
2d367c68 253 $db->failQuery();
82474746 254
255 return $db->getKey($username, "___signature___");
256 }
257
258?>