Source for file db_prefs.php
Documentation is available at db_prefs.php
* This contains functions for manipulating user preferences
* stored in a database, accessed though the Pear DB layer.
* The preferences table should have three columns:
* CREATE TABLE userprefs (user CHAR(128) NOT NULL DEFAULT '',
* prefkey CHAR(64) NOT NULL DEFAULT '',
* prefval BLOB NOT NULL DEFAULT '',
* primary key (user,prefkey));
* Configuration of databasename, username and password is done
* by using conf.pl or the administrator plugin
* @copyright © 1999-2006 The SquirrelMail Project Team
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version $Id: db_prefs.php,v 1.49 2006/07/15 12:00:44 tokul Exp $
* don't display errors (no code execution in functions/*.php).
* will handle error in dbPrefs class.
global $prefs_are_cached, $prefs_cache;
global $prefs_are_cached, $prefs_cache;
printf( _("Preference database error (%s). Exiting abnormally"),
$db->fillPrefsCache($username);
printf( _("Preference database error (%s). Exiting abnormally"),
$prefs_are_cached =
true;
* Class used to handle connections to prefs database and operations with preferences
* Table used to store preferences
* Field used to store owner of preference
* Field used to store preference name
* Field used to store preference value
* Database connection object
* Database type (SMDB_* constants)
var $default =
Array('theme_default' =>
0,
'show_html_default' =>
'0');
* Preference owner field size
* Preference key field size
* Preference value field size
* initialize DB connection object
* @return boolean true, if object is initialized
global $prefs_dsn, $prefs_table;
/* test if Pear DB class is available and freak out if it is not */
// same error also in abook_database.php
$this->error =
_("Could not include PEAR database functions required for the database backend.") .
"<br />\n";
$this->error .=
sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
'<tt>DB.php</tt>') .
"<br />\n";
$this->error .=
_("Please contact your system administrator and report this error.");
if (!empty($prefs_table)) {
$this->table =
$prefs_table;
if (!empty($prefs_user_field)) {
if (!empty($prefs_key_field)) {
if (!empty($prefs_val_field)) {
if (!empty($prefs_user_size)) {
if (!empty($prefs_key_size)) {
$this->key_size = (int)
$prefs_key_size;
if (!empty($prefs_val_size)) {
$this->val_size = (int)
$prefs_val_size;
$dbh =
DB::connect($prefs_dsn, true);
$this->error =
DB::errorMessage($dbh);
* Function used to handle database connection errors
* @param object PEAR Error object
printf(_("Preference database error (%s). Exiting abnormally"),
printf(_("Preference database error (%s). Exiting abnormally"),
* Get user's prefs setting
* @param string $user user name
* @param string $key preference name
* @param mixed $default (since 1.2.5) default value
* @return mixed preference value
function getKey($user, $key, $default =
'') {
if (isset
($prefs_cache[$key])) {
return $prefs_cache[$key];
* Delete user's prefs setting
* @param string $user user name
* @param string $key preference name
$query =
sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
$this->dbh->quoteString($user),
$this->dbh->quoteString($key));
$res =
$this->dbh->simpleQuery($query);
unset
($prefs_cache[$key]);
* @param string $user user name
* @param string $key preference name
* @param mixed $value preference value
function setKey($user, $key, $value) {
* Check if username fits into db field
$this->error =
"Oversized username value."
.
" Your preferences can't be saved."
.
" See doc/db-backend.txt or contact your system administrator.";
* Debugging function. Can be used to log all issues that trigger
* oversized field errors. Function should be enabled in all three
* strlen checks. See http://www.php.net/error-log
// error_log($user.'|'.$key.'|'.$value."\n",3,'/tmp/oversized_log');
* Check if preference key fits into db field
$err_msg =
"Oversized user's preference key."
.
" Some preferences were not saved."
.
" See doc/db-backend.txt or contact your system administrator.";
// error is not fatal. Only some preference is not saved.
* Check if preference value fits into db field
$err_msg =
"Oversized user's preference value."
.
" Some preferences were not saved."
.
" See doc/db-backend.txt or contact your system administrator.";
// error is not fatal. Only some preference is not saved.
$query =
sprintf("REPLACE INTO %s (%s, %s, %s) ".
"VALUES('%s','%s','%s')",
$this->dbh->quoteString($user),
$this->dbh->quoteString($key),
$this->dbh->quoteString($value));
$res =
$this->dbh->simpleQuery($query);
$this->dbh->simpleQuery("BEGIN TRANSACTION");
$query =
sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
$this->dbh->quoteString($user),
$this->dbh->quoteString($key));
$res =
$this->dbh->simpleQuery($query);
$this->dbh->simpleQuery("ROLLBACK TRANSACTION");
$query =
sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
$this->dbh->quoteString($user),
$this->dbh->quoteString($key),
$this->dbh->quoteString($value));
$res =
$this->dbh->simpleQuery($query);
$this->dbh->simpleQuery("ROLLBACK TRANSACTION");
$this->dbh->simpleQuery("COMMIT TRANSACTION");
$query =
sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
$this->dbh->quoteString($user),
$this->dbh->quoteString($key));
$res =
$this->dbh->simpleQuery($query);
$query =
sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
$this->dbh->quoteString($user),
$this->dbh->quoteString($key),
$this->dbh->quoteString($value));
$res =
$this->dbh->simpleQuery($query);
* Fill preference cache array
* @param string $user user name
$query =
sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
$this->dbh->quoteString($user));
$res =
$this->dbh->query($query);
while ($row =
$res->fetchRow(DB_FETCHMODE_ASSOC)) {
$prefs_cache[$row['prefkey']] =
$row['prefval'];
} /* end class dbPrefs */
* returns the value for the pref $string
function getPref($data_dir, $username, $string, $default =
'') {
printf( _("Preference database error (%s). Exiting abnormally"),
return $db->getKey($username, $string, $default);
* Remove the pref $string
function removePref($data_dir, $username, $string) {
if (isset
($prefs_cache[$string])) {
unset
($prefs_cache[$string]);
* sets the pref, $string, to $set_to
function setPref($data_dir, $username, $string, $set_to) {
if (isset
($prefs_cache[$string]) &&
($prefs_cache[$string] ==
$set_to)) {
$db->setKey($username, $string, $set_to);
$prefs_cache[$string] =
$set_to;
assert ('$set_to == $prefs_cache[$string]');
* This checks if the prefs are available
function setSig($data_dir, $username, $number, $string) {
$key =
'___signature___';
$key =
sprintf('___sig%s___', $number);
setPref($data_dir, $username, $key, $string);
function getSig($data_dir, $username, $number) {
$key =
'___signature___';
$key =
sprintf('___sig%d___', $number);
return getPref($data_dir, $username, $key);
Documentation generated on Sat, 07 Oct 2006 16:10:24 +0300 by phpDocumentor 1.3.0RC6