Allow easy selection of database backed prefs
authorsimond <simond@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Mon, 4 Feb 2002 12:53:26 +0000 (12:53 +0000)
committersimond <simond@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Mon, 4 Feb 2002 12:53:26 +0000 (12:53 +0000)
git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@2353 7612ce4b-ef26-0410-bec9-ea0150e637f0

config/conf.pl
config/config_default.php
doc/db-backend.txt
functions/db_prefs.php
functions/file_prefs.php [new file with mode: 0644]
functions/prefs.php

index e7e73ff3ee31890cbe877cfb19afe8067b8dfe2e..86d8662da982bc666fe22046826de1fea9639ebb 100755 (executable)
@@ -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 = <STDIN>;
+    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 = <STDIN>;
+    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";
index a8c2d8ff9da0a748cded9284fe3b7aa71bb58322..e4c761cb537b9d9d408aa1ebddddace9236c5970 100644 (file)
@@ -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.
index 5a587980b9574b71233bb05570d70a2b8b6a35a1..3712c3765889afcf8a1b0f11f43a7b06366d4c2b 100644 (file)
@@ -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.
 
index 7bc0800a6e956525d21fbc879c3a7e9c2e78e24c..029c9354b158d4201e392694de400c6a239e7a0f 100644 (file)
  *                           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) . '<br>';
-                echo _("Could not create hashed directory structure!") . "<br>\n";
-                echo _("Please contact your system administrator and report this error.") . "<br>\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 (file)
index 0000000..74bf5bd
--- /dev/null
@@ -0,0 +1,200 @@
+<?php
+
+/**
+ * file_prefs.php
+ *
+ * Copyright (c) 1999-2002 The SquirrelMail Project Team
+ * Licensed under the GNU GPL. For full terms see the file COPYING.
+ *
+ * This contains functions for manipulating user preferences in files
+ *
+ * $Id$
+ */
+
+global $prefs_are_cached, $prefs_cache;
+
+/**
+ * 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) . "<br>\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 . "<br>\n";
+            echo _("Default preference file not found!") . "<br>\n";
+            echo _("Please contact your system administrator and report this error.") . "<br>\n";
+            exit;
+        } else if (!@copy($default_pref, $filename)) {
+            echo _("Error opening ") . $default_pref . '<br>';
+            echo _("Could not create initial preference file!") . "<br>\n";
+            echo _("Please contact your system administrator and report this error.") . "<br>\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;
+}
index 5d2b38fe52471ca8a38512d1ce48bca7a0bc131b..67dbd445152ad76475e266df0019d4db87dc27d6 100644 (file)
  * $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) . "<br>\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 . "<br>\n";
-            echo _("Default preference file not found!") . "<br>\n";
-            echo _("Please contact your system administrator and report this error.") . "<br>\n";
-            exit;
-        } else if (!@copy($default_pref, $filename)) {
-            echo _("Error opening ") . $default_pref . '<br>';
-            echo _("Could not create initial preference file!") . "<br>\n";
-            echo _("Please contact your system administrator and report this error.") . "<br>\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;