From 3499f99feb029991182c20537d7e8f8fc83c3a5d Mon Sep 17 00:00:00 2001 From: simond Date: Mon, 4 Feb 2002 12:53:26 +0000 Subject: [PATCH] Allow easy selection of database backed prefs git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@2353 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- config/conf.pl | 47 +++++++++ config/config_default.php | 4 + doc/db-backend.txt | 21 ++-- functions/db_prefs.php | 105 ++------------------ functions/file_prefs.php | 200 ++++++++++++++++++++++++++++++++++++++ functions/prefs.php | 192 ++---------------------------------- 6 files changed, 280 insertions(+), 289 deletions(-) create mode 100644 functions/file_prefs.php diff --git a/config/conf.pl b/config/conf.pl index e7e73ff3..86d8662d 100755 --- a/config/conf.pl +++ b/config/conf.pl @@ -455,6 +455,8 @@ while (($command ne "q") && ($command ne "Q")) { print $WHT."Database\n".$NRM; print "1. DSN for Address Book : $WHT$addrbook_dsn$NRM\n"; print "2. Table for Address Book : $WHT$addrbook_table$NRM\n"; + print "3. DSN for Preferences : $WHT$prefs_dsn$NRM\n"; + print "4. Table for Preferences : $WHT$prefs_table$NRM\n"; print "\n"; print "S Save data\n"; print "R Return to Main Menu\n"; @@ -566,6 +568,8 @@ while (($command ne "q") && ($command ne "Q")) { } elsif ($menu == 9) { if ($command == 1) { $addrbook_dsn = command91(); } elsif ($command == 2) { $addrbook_table = command92(); } + elsif ($command == 3) { $prefs_dsn = command93(); } + elsif ($command == 4) { $prefs_table = command94(); } } } } @@ -1864,6 +1868,46 @@ sub command92 { return $new_table; } +sub command93 { + print "If you want to store your users preferences in a database then\n"; + print "you need to set this DSN to a valid value. The format for this is:\n"; + print "mysql://user:pass\@hostname/dbname\n"; + print "Where mysql can be one of the databases PHP supports, the most common\n"; + print "of these are mysql, msql and pgsql\n"; + print "If the DSN is left empty (hit space and then return) the database\n"; + print "related code for address books will not be used\n"; + print "\n"; + + if ($prefs_dsn eq "") { + $default_value = "Disabled"; + } else { + $default_value = $prefs_dsn; + } + print "[$WHT$prefs_dsn$NRM]: $WHT"; + $new_dsn = ; + if ($new_dsn eq "\n") { + $new_dsn = ""; + } else { + $new_dsn =~ s/[\r|\n]//g; + $new_dsn =~ s/^\s+$//g; + } + return $new_dsn; +} + +sub command94 { + print "This is the name of the table you want to store the preferences\n"; + print "data in, it defaults to 'address'\n"; + print "\n"; + print "[$WHT$prefs_table$NRM]: $WHT"; + $new_table = ; + if ($new_table eq "\n") { + $new_table = $prefs_table; + } else { + $new_table =~ s/[\r|\n]//g; + } + return $new_table; +} + sub save_data { $tab = " "; if(open (CF, ">config.php")) @@ -2011,6 +2055,9 @@ sub save_data { print CF "\nglobal \$addrbook_dsn, \$addrbook_table;\n"; print CF "\$addrbook_dsn = '$addrbook_dsn';\n"; print CF "\$addrbook_table = '$addrbook_table';\n\n"; + print CF "\nglobal \$prefs_dsn, \$prefs_table;\n"; + print CF "\$prefs_dsn = '$prefs_dsn';\n"; + print CF "\$prefs_table = '$prefs_table';\n"; print CF "/**\n"; print CF " * Make sure there are no characters after the PHP closing\n"; diff --git a/config/config_default.php b/config/config_default.php index a8c2d8ff..e4c761cb 100644 --- a/config/config_default.php +++ b/config/config_default.php @@ -386,6 +386,10 @@ global $addrbook_dsn, $addrbook_table; $addrbook_dsn = ''; $addrbook_table = 'address'; +global $prefs_dsn, $prefs_table; +$prefs_dsn = ''; +$prefs_table = 'userprefs' + /** * Users may search their addressbook via either a plain HTML or Javascript * enhanced user interface. This option allows you to set the default choice. diff --git a/doc/db-backend.txt b/doc/db-backend.txt index 5a587980..3712c376 100644 --- a/doc/db-backend.txt +++ b/doc/db-backend.txt @@ -60,7 +60,7 @@ Next, edit your configuration so that the address book DSN (Data Source Name) is specified, this can be done using either conf.pl or via the administration plugin. The DSN should look something like: - $addrbook_dsn = 'mysql://squirreluser:sqpassword@localhost/squirrelmail'; + mysql://squirreluser:sqpassword@localhost/squirrelmail From now on all users' personal addressbooks will be stored in a database. @@ -70,11 +70,10 @@ database. Configuring preferences in database ----------------------------------- -There is no easy way to do this yet. You will have to remove -functions/prefs.php and replace it with functions/db_prefs.php. Then -edit the new functions/prefs.php (db_prefs.php) and change the $DSN to -point to a database you create (can be the same you use for -addressbooks). Create a table similar to this (for MySQL): +This is done in much the same way as it is for storing your address +books in a database. + +The table structure should be similar to this (for MySQL): CREATE TABLE userprefs ( user varchar(128) DEFAULT '' NOT NULL, @@ -83,7 +82,15 @@ addressbooks). Create a table similar to this (for MySQL): PRIMARY KEY (user,prefkey) ); +Next, edit your configuration so that the preferences DSN (Data Source +Name) is specified, this can be done using either conf.pl or via the +administration plugin. The DSN should look something like: + + mysql://squirreluser:sqpassword@localhost/squirrelmail + +From now on all users' personal preferences will be stored in a +database. Default preferences can be set by altering the $default array in -prefs.php (db_prefs.php). +db_prefs.php. diff --git a/functions/db_prefs.php b/functions/db_prefs.php index 7bc0800a..029c9354 100644 --- a/functions/db_prefs.php +++ b/functions/db_prefs.php @@ -27,23 +27,16 @@ * primary key (user,prefkey)); * * Configuration of databasename, username and password is done - * by changing $DSN below. + * by using conf.pl or the administrator plugin * * $Id$ */ require_once('DB.php'); +require_once('../config/config.php'); global $prefs_are_cached, $prefs_cache; -if ( !session_is_registered('prefs_are_cached') || - !isset( $prefs_cache) || - !is_array( $prefs_cache) || - substr( phpversion(), 0, 3 ) == '4.1' ) { - $prefs_are_cached = false; - $prefs_cache = array(); -} - function cachePrefValues($username) { global $prefs_are_cached, $prefs_cache; @@ -75,7 +68,6 @@ function cachePrefValues($username) { } class dbPrefs { - var $DSN = 'mysql://user:passwd@host/database'; var $table = 'userprefs'; var $dbh = NULL; @@ -85,10 +77,16 @@ class dbPrefs { 'show_html_default' => '0'); function open() { + global $prefs_dsn, $prefs_table; + if(isset($this->dbh)) { return true; } - $dbh = DB::connect($this->DSN, true); + + if (!empty($prefs_table)) { + $this->table = $prefs_table; + } + $dbh = DB::connect($prefs_dsn); if(DB::isError($dbh) || DB::isWarning($dbh)) { $this->error = DB::errorMessage($dbh); @@ -317,89 +315,4 @@ function getSig($data_dir, $username) { return $db->getKey($username, '___signature___'); } -/* Hashing functions */ - -function getHashedFile($username, $dir, $datafile, $hash_search = true) { - global $dir_hash_level; - - /* Remove trailing slash from $dir if found */ - if (substr($dir, -1) == '/') { - $dir = substr($dir, 0, strlen($dir) - 1); - } - - /* Compute the hash for this user and extract the hash directories. */ - $hash_dirs = computeHashDirs($username); - - /* First, get and make sure the full hash directory exists. */ - $real_hash_dir = getHashedDir($username, $dir, $hash_dirs); - - /* Set the value of our real data file. */ - $result = "$real_hash_dir/$datafile"; - - /* Check for this file in the real hash directory. */ - if ($hash_search && !@file_exists($result)) { - /* First check the base directory, the most common location. */ - if (@file_exists("$dir/$datafile")) { - rename("$dir/$datafile", $result); - - /* Then check the full range of possible hash directories. */ - } else { - $check_hash_dir = $dir; - for ($h = 0; $h < 4; ++$h) { - $check_hash_dir .= '/' . $hash_dirs[$h]; - if (@is_readable("$check_hash_dir/$datafile")) { - rename("$check_hash_dir/$datafile", $result); - break; - } - } - } - } - - /* Return the full hashed datafile path. */ - return ($result); -} - -function getHashedDir($username, $dir, $hash_dirs = '') { - global $dir_hash_level; - - /* Remove trailing slash from $dir if found */ - if (substr($dir, -1) == '/') { - $dir = substr($dir, 0, strlen($dir) - 1); - } - - /* If necessary, populate the hash dir variable. */ - if ($hash_dirs == '') { - $hash_dirs = computeHashDirs($username); - } - - /* Make sure the full hash directory exists. */ - $real_hash_dir = $dir; - for ($h = 0; $h < $dir_hash_level; ++$h) { - $real_hash_dir .= '/' . $hash_dirs[$h]; - if (!@is_dir($real_hash_dir)) { - if (!@mkdir($real_hash_dir, 0770)) { - echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '
'; - echo _("Could not create hashed directory structure!") . "
\n"; - echo _("Please contact your system administrator and report this error.") . "
\n"; - exit; - } - } - } - - /* And return that directory. */ - return ($real_hash_dir); -} - -function computeHashDirs($username) { - /* Compute the hash for this user and extract the hash directories. */ - $hash = base_convert(crc32($username), 10, 16); - $hash_dirs = array(); - for ($h = 0; $h < 4; ++ $h) { - $hash_dirs[] = substr($hash, $h, 1); - } - - /* Return our array of hash directories. */ - return ($hash_dirs); -} - ?> diff --git a/functions/file_prefs.php b/functions/file_prefs.php new file mode 100644 index 00000000..74bf5bdd --- /dev/null +++ b/functions/file_prefs.php @@ -0,0 +1,200 @@ +\n"; + exit; + } + + $file = fopen($filename, 'r'); + + /* Read in the preferences. */ + $highlight_num = 0; + while (! feof($file)) { + $pref = trim(fgets($file, 1024)); + $equalsAt = strpos($pref, '='); + if ($equalsAt > 0) { + $key = substr($pref, 0, $equalsAt); + $value = substr($pref, $equalsAt + 1); + if (substr($key, 0, 9) == 'highlight') { + $key = 'highlight' . $highlight_num; + $highlight_num ++; + } + + if ($value != '') { + $prefs_cache[$key] = $value; + } + } + } + fclose($file); + + $prefs_are_cached = TRUE; + + session_register('prefs_cache'); + session_register('prefs_are_cached'); +} + +/** + * Return the value for the prefernce given by $string. + */ +function getPref($data_dir, $username, $string, $default = '') { + global $prefs_cache; + $result = ''; + + cachePrefValues($data_dir, $username); + + if (isset($prefs_cache[$string])) { + $result = $prefs_cache[$string]; + } else { + $result = $default; + } + + return ($result); +} + +/** + * Save the preferences for this user. + */ +function savePrefValues($data_dir, $username) { + global $prefs_cache; + + $filename = getHashedFile($username, $data_dir, "$username.pref"); + + $file = fopen($filename, 'w'); + foreach ($prefs_cache as $Key => $Value) { + if (isset($Value)) { + fwrite($file, $Key . '=' . $Value . "\n"); + } + } + fclose($file); + chmod($filename, 0600); +} + +/** + * Remove a preference for the current user. + */ +function removePref($data_dir, $username, $string) { + global $prefs_cache; + + cachePrefValues($data_dir, $username); + + if (isset($prefs_cache[$string])) { + unset($prefs_cache[$string]); + } + + savePrefValues($data_dir, $username); +} + +/** + * Set a there preference $string to $value. + */ +function setPref($data_dir, $username, $string, $value) { + global $prefs_cache; + + cachePrefValues($data_dir, $username); + if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) { + return; + } + + if ($value === '') { + removePref($data_dir, $username, $string); + return; + } + + $prefs_cache[$string] = $value; + savePrefValues($data_dir, $username); +} + +/** + * Check for a preferences file. If one can not be found, create it. + */ +function checkForPrefs($data_dir, $username, $filename = '') { + /* First, make sure we have the filename. */ + if ($filename == '') { + $filename = getHashedFile($username, $data_dir, '$username.pref'); + } + + /* Then, check if the file exists. */ + if (!@file_exists($filename) ) { + /* First, check the $data_dir for the default preference file. */ + $default_pref = $data_dir . 'default_pref'; + + /* If it is not there, check the internal data directory. */ + if (!@file_exists($default_pref)) { + $default_pref = '../data/default_pref'; + } + + /* Otherwise, report an error. */ + if (!file_exists($default_pref)) { + echo _("Error opening ") . $default_pref . "
\n"; + echo _("Default preference file not found!") . "
\n"; + echo _("Please contact your system administrator and report this error.") . "
\n"; + exit; + } else if (!@copy($default_pref, $filename)) { + echo _("Error opening ") . $default_pref . '
'; + echo _("Could not create initial preference file!") . "
\n"; + echo _("Please contact your system administrator and report this error.") . "
\n"; + exit; + } + } +} + +/** + * Write the User Signature. + */ +function setSig($data_dir, $username, $value) { + $filename = getHashedFile($username, $data_dir, "$username.sig"); + $file = fopen($filename, 'w'); + fwrite($file, $value); + fclose($file); +} + +/** + * Get the signature. + */ +function getSig($data_dir, $username) { + #$filename = $data_dir . $username . '.sig'; + $filename = getHashedFile($username, $data_dir, "$username.sig"); + $sig = ''; + if (file_exists($filename)) { + $file = fopen($filename, 'r'); + while (!feof($file)) { + $sig .= fgets($file, 1024); + } + fclose($file); + } + return $sig; +} diff --git a/functions/prefs.php b/functions/prefs.php index 5d2b38fe..67dbd445 100644 --- a/functions/prefs.php +++ b/functions/prefs.php @@ -11,203 +11,23 @@ * $Id$ */ -/* global $prefs_are_cached, $prefs_cache; if ( !session_is_registered('prefs_are_cached') || !isset( $prefs_cache) || - !is_array( $prefs_cache) || + !is_array( $prefs_cache) || substr( phpversion(), 0, 3 ) == '4.1' ) { $prefs_are_cached = false; $prefs_cache = array(); } -*/ -/** - * Check the preferences into the session cache. - */ -function cachePrefValues($data_dir, $username) { - global $prefs_are_cached, $prefs_cache; - - if ( isset($prefs_are_cached) && $prefs_are_cached) { - return; - } - - session_unregister('prefs_cache'); - session_unregister('prefs_are_cached'); - - /* Calculate the filename for the user's preference file */ - $filename = getHashedFile($username, $data_dir, "$username.pref"); - - /* A call to checkForPrefs here should take eliminate the need for */ - /* this to be called throughout the rest of the SquirrelMail code. */ - checkForPrefs($data_dir, $username, $filename); - - /* Make sure that the preference file now DOES exist. */ - if (!file_exists($filename)) { - echo sprintf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) . "
\n"; - exit; - } - - $file = fopen($filename, 'r'); - - /* Read in the preferences. */ - $highlight_num = 0; - while (! feof($file)) { - $pref = trim(fgets($file, 1024)); - $equalsAt = strpos($pref, '='); - if ($equalsAt > 0) { - $key = substr($pref, 0, $equalsAt); - $value = substr($pref, $equalsAt + 1); - if (substr($key, 0, 9) == 'highlight') { - $key = 'highlight' . $highlight_num; - $highlight_num ++; - } - - if ($value != '') { - $prefs_cache[$key] = $value; - } - } - } - fclose($file); - - $prefs_are_cached = TRUE; - - session_register('prefs_cache'); - session_register('prefs_are_cached'); +if (isset($prefs_dsn) && !empty($prefs_dsn)) { + require_once('../functions/db_prefs.php'); +} else { + require_once('../functions/file_prefs.php'); } - -/** - * Return the value for the prefernce given by $string. - */ -function getPref($data_dir, $username, $string, $default = '') { - global $prefs_cache; - $result = ''; - - cachePrefValues($data_dir, $username); - - if (isset($prefs_cache[$string])) { - $result = $prefs_cache[$string]; - } else { - $result = $default; - } - return ($result); -} - -/** - * Save the preferences for this user. - */ -function savePrefValues($data_dir, $username) { - global $prefs_cache; - - $filename = getHashedFile($username, $data_dir, "$username.pref"); - - $file = fopen($filename, 'w'); - foreach ($prefs_cache as $Key => $Value) { - if (isset($Value)) { - fwrite($file, $Key . '=' . $Value . "\n"); - } - } - fclose($file); - chmod($filename, 0600); -} - -/** - * Remove a preference for the current user. - */ -function removePref($data_dir, $username, $string) { - global $prefs_cache; - - cachePrefValues($data_dir, $username); - - if (isset($prefs_cache[$string])) { - unset($prefs_cache[$string]); - } - - savePrefValues($data_dir, $username); -} - -/** - * Set a there preference $string to $value. - */ -function setPref($data_dir, $username, $string, $value) { - global $prefs_cache; - - cachePrefValues($data_dir, $username); - if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) { - return; - } - - if ($value === '') { - removePref($data_dir, $username, $string); - return; - } - - $prefs_cache[$string] = $value; - savePrefValues($data_dir, $username); -} - -/** - * Check for a preferences file. If one can not be found, create it. - */ -function checkForPrefs($data_dir, $username, $filename = '') { - /* First, make sure we have the filename. */ - if ($filename == '') { - $filename = getHashedFile($username, $data_dir, '$username.pref'); - } - - /* Then, check if the file exists. */ - if (!@file_exists($filename) ) { - /* First, check the $data_dir for the default preference file. */ - $default_pref = $data_dir . 'default_pref'; - - /* If it is not there, check the internal data directory. */ - if (!@file_exists($default_pref)) { - $default_pref = '../data/default_pref'; - } - - /* Otherwise, report an error. */ - if (!file_exists($default_pref)) { - echo _("Error opening ") . $default_pref . "
\n"; - echo _("Default preference file not found!") . "
\n"; - echo _("Please contact your system administrator and report this error.") . "
\n"; - exit; - } else if (!@copy($default_pref, $filename)) { - echo _("Error opening ") . $default_pref . '
'; - echo _("Could not create initial preference file!") . "
\n"; - echo _("Please contact your system administrator and report this error.") . "
\n"; - exit; - } - } -} - -/** - * Write the User Signature. - */ -function setSig($data_dir, $username, $value) { - $filename = getHashedFile($username, $data_dir, "$username.sig"); - $file = fopen($filename, 'w'); - fwrite($file, $value); - fclose($file); -} - -/** - * Get the signature. - */ -function getSig($data_dir, $username) { - #$filename = $data_dir . $username . '.sig'; - $filename = getHashedFile($username, $data_dir, "$username.sig"); - $sig = ''; - if (file_exists($filename)) { - $file = fopen($filename, 'r'); - while (!feof($file)) { - $sig .= fgets($file, 1024); - } - fclose($file); - } - return $sig; -} +/* Hashing functions */ function getHashedFile($username, $dir, $datafile, $hash_search = true) { global $dir_hash_level; -- 2.25.1