Fixed a couple of small bugs.
[squirrelmail.git] / functions / prefs.php
index 35e7860717d9859ca3f8129efd0adfb6e9b6b9e8..e71d4e0531d3ca9872372d3f207296f66925fe3d 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * prefs.php
  *
- * Copyright (c) 1999-2001 The SquirrelMail Development Team
+ * 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
@@ -27,8 +27,14 @@ function cachePrefValues($data_dir, $username) {
         return;
     }
 
+    /* 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)) {
         printf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename);
         exit;
@@ -137,8 +143,13 @@ function setPref($data_dir, $username, $string, $value) {
 /**
  * Check for a preferences file. If one can not be found, create it.
  */
-function checkForPrefs($data_dir, $username) {
-    $filename = getHashedFile($username, $data_dir, "$username.pref");
+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) ) {
         if (!copy($data_dir . 'default_pref', $filename)) {
             echo _("Error opening ") . $filename;
@@ -177,6 +188,11 @@ function getSig($data_dir, $username) {
 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);
 
@@ -197,7 +213,7 @@ function getHashedFile($username, $dir, $datafile, $hash_search = true) {
             $check_hash_dir = $dir;
             for ($h = 0; $h < 4; ++$h) {
                 $check_hash_dir .= '/' . $hash_dirs[$h];
-                if (is_readable("$check_hash_dir/$datafile")) {
+                if (@is_readable("$check_hash_dir/$datafile")) {
                     rename("$check_hash_dir/$datafile", $result);
                     break;
                 }