squirrelmail-webmail  1.4.22
About: SquirrelMail is a standards-based webmail package with strong MIME support, address books, and folder manipulation (written in PHP4).
  Fossies Dox: squirrelmail-webmail-1.4.22.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

db_prefs.php
Go to the documentation of this file.
1 <?php
2 
33 define('SMDB_UNKNOWN', 0);
35 define('SMDB_MYSQL', 1);
37 define('SMDB_PGSQL', 2);
38 
39 if (!include_once('DB.php')) {
40  // same error also in abook_database.php
41  require_once(SM_PATH . 'functions/display_messages.php');
42  $error = _("Could not include PEAR database functions required for the database backend.") . "<br />\n";
43  $error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
44  '<tt>DB.php</tt>') . "<br />\n";
45  $error .= _("Please contact your system administrator and report this error.");
46  error_box($error, $color);
47  exit;
48 }
49 
51 
57 
58  sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
59  if ($prefs_are_cached) {
60  sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
61  return;
62  }
63 
64  sqsession_unregister('prefs_cache');
65  sqsession_unregister('prefs_are_cached');
66 
67  $db = new dbPrefs;
68  if(isset($db->error)) {
69  printf( _("Preference database error (%s). Exiting abnormally"),
70  $db->error);
71  exit;
72  }
73 
74  $db->fillPrefsCache($username);
75  if (isset($db->error)) {
76  printf( _("Preference database error (%s). Exiting abnormally"),
77  $db->error);
78  exit;
79  }
80 
81  $prefs_are_cached = true;
82 
83  sqsession_register($prefs_cache, 'prefs_cache');
84  sqsession_register($prefs_are_cached, 'prefs_are_cached');
85 }
86 
91 class dbPrefs {
92  var $table = 'userprefs';
93  var $user_field = 'user';
94  var $key_field = 'prefkey';
95  var $val_field = 'prefval';
96 
97  var $dbh = NULL;
98  var $error = NULL;
100 
101  var $default = Array('theme_default' => 0,
102  'show_html_default' => '0');
103 
104  function open() {
105  global $prefs_dsn, $prefs_table;
107 
108  if(isset($this->dbh)) {
109  return true;
110  }
111 
112  if (preg_match('/^mysql/', $prefs_dsn)) {
113  $this->db_type = SMDB_MYSQL;
114  } elseif (preg_match('/^pgsql/', $prefs_dsn)) {
115  $this->db_type = SMDB_PGSQL;
116  }
117 
118  if (!empty($prefs_table)) {
119  $this->table = $prefs_table;
120  }
121  if (!empty($prefs_user_field)) {
122  $this->user_field = $prefs_user_field;
123  }
124 
125  // the default user field is "user", which in PostgreSQL
126  // is an identifier and causes errors if not escaped
127  //
128  if ($this->db_type == SMDB_PGSQL) {
129  $this->user_field = '"' . $this->user_field . '"';
130  }
131 
132  if (!empty($prefs_key_field)) {
133  $this->key_field = $prefs_key_field;
134  }
135  if (!empty($prefs_val_field)) {
136  $this->val_field = $prefs_val_field;
137  }
138  $dbh = DB::connect($prefs_dsn, true);
139 
140  if(DB::isError($dbh)) {
141  $this->error = DB::errorMessage($dbh);
142  return false;
143  }
144 
145  $this->dbh = $dbh;
146  return true;
147  }
148 
149  function failQuery($res = NULL) {
150  if($res == NULL) {
151  printf(_("Preference database error (%s). Exiting abnormally"),
152  $this->error);
153  } else {
154  printf(_("Preference database error (%s). Exiting abnormally"),
155  DB::errorMessage($res));
156  }
157  exit;
158  }
159 
160 
161  function getKey($user, $key, $default = '') {
162  global $prefs_cache;
163 
164  $result = do_hook_function('get_pref_override', array($user, $key));
165 //FIXME: testing below for !$result means that a plugin cannot fetch its own pref value of 0, '0', '', FALSE, or anything else that evaluates to boolean FALSE.
166  if (!$result) {
167  cachePrefValues($user);
168 
169  if (isset($prefs_cache[$key])) {
170  $result = $prefs_cache[$key];
171  } else {
172 //FIXME: is there justification for having these TWO hooks so close together? who uses these?
173  $result = do_hook_function('get_pref', array($user, $key));
174 //FIXME: testing below for !$result means that a plugin cannot fetch its own pref value of 0, '0', '', FALSE, or anything else that evaluates to boolean FALSE.
175  if (!$result) {
176  if (isset($this->default[$key])) {
177  $result = $this->default[$key];
178  } else {
179  $result = $default;
180  }
181  }
182  }
183  }
184  return $result;
185  }
186 
187  function deleteKey($user, $key) {
188  global $prefs_cache;
189 
190  if (!$this->open()) {
191  return false;
192  }
193  $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
194  $this->table,
195  $this->user_field,
196  $this->dbh->quoteString($user),
198  $this->dbh->quoteString($key));
199 
200  $res = $this->dbh->simpleQuery($query);
201  if(DB::isError($res)) {
202  $this->failQuery($res);
203  }
204 
205  unset($prefs_cache[$key]);
206 
207  return true;
208  }
209 
210  function setKey($user, $key, $value) {
211  if (!$this->open()) {
212  return false;
213  }
214  if ($this->db_type == SMDB_MYSQL) {
215  $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
216  "VALUES('%s','%s','%s')",
217  $this->table,
218  $this->user_field,
219  $this->key_field,
220  $this->val_field,
221  $this->dbh->quoteString($user),
222  $this->dbh->quoteString($key),
223  $this->dbh->quoteString($value));
224 
225  $res = $this->dbh->simpleQuery($query);
226  if(DB::isError($res)) {
227  $this->failQuery($res);
228  }
229  } elseif ($this->db_type == SMDB_PGSQL) {
230  $this->dbh->simpleQuery("BEGIN TRANSACTION");
231  $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
232  $this->table,
233  $this->user_field,
234  $this->dbh->quoteString($user),
236  $this->dbh->quoteString($key));
237  $res = $this->dbh->simpleQuery($query);
238  if (DB::isError($res)) {
239  $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
240  $this->failQuery($res);
241  }
242  $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
243  $this->table,
244  $this->user_field,
245  $this->key_field,
246  $this->val_field,
247  $this->dbh->quoteString($user),
248  $this->dbh->quoteString($key),
249  $this->dbh->quoteString($value));
250  $res = $this->dbh->simpleQuery($query);
251  if (DB::isError($res)) {
252  $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
253  $this->failQuery($res);
254  }
255  $this->dbh->simpleQuery("COMMIT TRANSACTION");
256  } else {
257  $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
258  $this->table,
259  $this->user_field,
260  $this->dbh->quoteString($user),
262  $this->dbh->quoteString($key));
263  $res = $this->dbh->simpleQuery($query);
264  if (DB::isError($res)) {
265  $this->failQuery($res);
266  }
267  $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
268  $this->table,
269  $this->user_field,
270  $this->key_field,
271  $this->val_field,
272  $this->dbh->quoteString($user),
273  $this->dbh->quoteString($key),
274  $this->dbh->quoteString($value));
275  $res = $this->dbh->simpleQuery($query);
276  if (DB::isError($res)) {
277  $this->failQuery($res);
278  }
279  }
280 
281  return true;
282  }
283 
284  function fillPrefsCache($user) {
285  global $prefs_cache;
286 
287  if (!$this->open()) {
288  return;
289  }
290 
291  $prefs_cache = array();
292  $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
293  "WHERE %s = '%s'",
294  $this->key_field,
295  $this->val_field,
296  $this->table,
297  $this->user_field,
298  $this->dbh->quoteString($user));
299  $res = $this->dbh->query($query);
300  if (DB::isError($res)) {
301  $this->failQuery($res);
302  }
303 
304  while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
305  $prefs_cache[$row['prefkey']] = $row['prefval'];
306  }
307  }
308 
309 } /* end class dbPrefs */
310 
311 
316 function getPref($data_dir, $username, $string, $default = '') {
317  $db = new dbPrefs;
318  if(isset($db->error)) {
319  printf( _("Preference database error (%s). Exiting abnormally"),
320  $db->error);
321  exit;
322  }
323 
324  return $db->getKey($username, $string, $default);
325 }
326 
331 function removePref($data_dir, $username, $string) {
332  global $prefs_cache;
333  $db = new dbPrefs;
334  if(isset($db->error)) {
335  $db->failQuery();
336  }
337 
338  $db->deleteKey($username, $string);
339 
340  if (isset($prefs_cache[$string])) {
341  unset($prefs_cache[$string]);
342  }
343 
344  sqsession_register($prefs_cache , 'prefs_cache');
345  return;
346 }
347 
352 function setPref($data_dir, $username, $string, $set_to) {
353  global $prefs_cache;
354 
355  if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
356  return;
357  }
358 
359  if ($set_to === '') {
360  removePref($data_dir, $username, $string);
361  return;
362  }
363 
364  $db = new dbPrefs;
365  if(isset($db->error)) {
366  $db->failQuery();
367  }
368 
369  $db->setKey($username, $string, $set_to);
370  $prefs_cache[$string] = $set_to;
371  assert_options(ASSERT_ACTIVE, 1);
372  assert_options(ASSERT_BAIL, 1);
373  assert ('$set_to == $prefs_cache[$string]');
374  sqsession_register($prefs_cache , 'prefs_cache');
375  return;
376 }
377 
383  $db = new dbPrefs;
384  if(isset($db->error)) {
385  $db->failQuery();
386  }
387 }
388 
393 function setSig($data_dir, $username, $number, $string) {
394  if ($number == "g") {
395  $key = '___signature___';
396  } else {
397  $key = sprintf('___sig%s___', $number);
398  }
399  setPref($data_dir, $username, $key, $string);
400  return;
401 }
402 
407 function getSig($data_dir, $username, $number) {
408  if ($number == "g") {
409  $key = '___signature___';
410  } else {
411  $key = sprintf('___sig%d___', $number);
412  }
413  return getPref($data_dir, $username, $key);
414 }
415 
elseif
if(! sqgetGlobalVar('sound', $sound, SQ_GET)) elseif($sound=='(none)')
Definition: testsound.php:25
sqgetGlobalVar
sqgetGlobalVar($name, &$value, $search=SQ_INORDER)
Definition: global.php:344
getPref
getPref($data_dir, $username, $string, $default='')
Definition: db_prefs.php:316
getSig
getSig($data_dir, $username, $number)
Definition: db_prefs.php:407
dbPrefs\$table
$table
Definition: db_prefs.php:92
$data_dir
$data_dir
Definition: config_default.php:499
dbPrefs\$val_field
$val_field
Definition: db_prefs.php:95
$prefs_table
$prefs_table
Definition: config_default.php:971
SQ_SESSION
const SQ_SESSION
Definition: global.php:18
dbPrefs\$error
$error
Definition: db_prefs.php:98
setPref
setPref($data_dir, $username, $string, $set_to)
Definition: db_prefs.php:352
sprintf
powered by Systran sprintf(_("Number of supported language pairs: %s"), '36').' '
Definition: options.php:107
$prefs_key_field
$prefs_key_field
Definition: config_default.php:972
dbPrefs\$dbh
$dbh
Definition: db_prefs.php:97
$username
global $username
Definition: validate.php:55
dbPrefs\$db_type
$db_type
Definition: db_prefs.php:99
dbPrefs\$key_field
$key_field
Definition: db_prefs.php:94
$prefs_cache
if(!include_once('DB.php')) global $prefs_cache
Definition: db_prefs.php:39
dbPrefs\$user_field
$user_field
Definition: db_prefs.php:93
$prefs_user_field
$prefs_user_field
Definition: config_default.php:973
SMDB_MYSQL
const SMDB_MYSQL
Definition: db_prefs.php:35
$prefs_dsn
$prefs_dsn
Definition: config_default.php:970
dbPrefs\failQuery
failQuery($res=NULL)
Definition: db_prefs.php:149
dbPrefs\setKey
setKey($user, $key, $value)
Definition: db_prefs.php:210
SMDB_UNKNOWN
const SMDB_UNKNOWN
Definition: db_prefs.php:33
SMDB_PGSQL
const SMDB_PGSQL
Definition: db_prefs.php:37
do_hook_function
do_hook_function($name, $parm=NULL)
Definition: plugin.php:69
dbPrefs\getKey
getKey($user, $key, $default='')
Definition: db_prefs.php:161
dbPrefs\open
open()
Definition: db_prefs.php:104
removePref
removePref($data_dir, $username, $string)
Definition: db_prefs.php:331
dbPrefs
Definition: db_prefs.php:91
$prefs_val_field
$prefs_val_field
Definition: config_default.php:974
exit
exit
Definition: help.php:128
SM_PATH
const SM_PATH
Definition: decrypt_headers.php:16
sqsession_register
sqsession_register($var, $name)
Definition: global.php:289
_
_($str)
Definition: gettext.php:160
cachePrefValues
cachePrefValues($username)
Definition: db_prefs.php:55
checkForPrefs
checkForPrefs($data_dir, $username)
Definition: db_prefs.php:382
dbPrefs\fillPrefsCache
fillPrefsCache($user)
Definition: db_prefs.php:284
dbPrefs\$default
$default
Definition: db_prefs.php:101
$color
$color
Definition: load_prefs.php:28
sqsession_unregister
sqsession_unregister($name)
Definition: global.php:301
NULL
if(!defined('PAGE_NAME')) define('PAGE_NAME' NULL
Definition: validate.php:16
dbPrefs\deleteKey
deleteKey($user, $key)
Definition: db_prefs.php:187
$prefs_are_cached
if(!include_once('DB.php')) global $prefs_are_cached
Definition: db_prefs.php:39
setSig
setSig($data_dir, $username, $number, $string)
Definition: db_prefs.php:393
error_box
error_box($string, $color)
Definition: display_messages.php:116