| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * prefs.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * This contains functions for manipulating user preferences |
| 10 | * |
| 11 | * $Id$ |
| 12 | */ |
| 13 | |
| 14 | global $prefs_are_cached, $prefs_cache; |
| 15 | |
| 16 | if ( !session_is_registered('prefs_are_cached') || |
| 17 | !isset( $prefs_cache) || |
| 18 | !is_array( $prefs_cache) || |
| 19 | substr( phpversion(), 0, 3 ) == '4.1' || |
| 20 | substr( phpversion(), 0, 3 ) == '4.2' ) { |
| 21 | $prefs_are_cached = false; |
| 22 | $prefs_cache = array(); |
| 23 | } |
| 24 | |
| 25 | if (isset($prefs_dsn) && !empty($prefs_dsn)) { |
| 26 | require_once('../functions/db_prefs.php'); |
| 27 | } else { |
| 28 | require_once('../functions/file_prefs.php'); |
| 29 | } |
| 30 | |
| 31 | /* Hashing functions */ |
| 32 | |
| 33 | function getHashedFile($username, $dir, $datafile, $hash_search = true) { |
| 34 | global $dir_hash_level; |
| 35 | |
| 36 | /* Remove trailing slash from $dir if found */ |
| 37 | if (substr($dir, -1) == '/') { |
| 38 | $dir = substr($dir, 0, strlen($dir) - 1); |
| 39 | } |
| 40 | |
| 41 | /* Compute the hash for this user and extract the hash directories. */ |
| 42 | $hash_dirs = computeHashDirs($username); |
| 43 | |
| 44 | /* First, get and make sure the full hash directory exists. */ |
| 45 | $real_hash_dir = getHashedDir($username, $dir, $hash_dirs); |
| 46 | |
| 47 | /* Set the value of our real data file. */ |
| 48 | $result = "$real_hash_dir/$datafile"; |
| 49 | |
| 50 | /* Check for this file in the real hash directory. */ |
| 51 | if ($hash_search && !@file_exists($result)) { |
| 52 | /* First check the base directory, the most common location. */ |
| 53 | if (@file_exists("$dir/$datafile")) { |
| 54 | rename("$dir/$datafile", $result); |
| 55 | |
| 56 | /* Then check the full range of possible hash directories. */ |
| 57 | } else { |
| 58 | $check_hash_dir = $dir; |
| 59 | for ($h = 0; $h < 4; ++$h) { |
| 60 | $check_hash_dir .= '/' . $hash_dirs[$h]; |
| 61 | if (@is_readable("$check_hash_dir/$datafile")) { |
| 62 | rename("$check_hash_dir/$datafile", $result); |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /* Return the full hashed datafile path. */ |
| 70 | return ($result); |
| 71 | } |
| 72 | |
| 73 | function getHashedDir($username, $dir, $hash_dirs = '') { |
| 74 | global $dir_hash_level; |
| 75 | |
| 76 | /* Remove trailing slash from $dir if found */ |
| 77 | if (substr($dir, -1) == '/') { |
| 78 | $dir = substr($dir, 0, strlen($dir) - 1); |
| 79 | } |
| 80 | |
| 81 | /* If necessary, populate the hash dir variable. */ |
| 82 | if ($hash_dirs == '') { |
| 83 | $hash_dirs = computeHashDirs($username); |
| 84 | } |
| 85 | |
| 86 | /* Make sure the full hash directory exists. */ |
| 87 | $real_hash_dir = $dir; |
| 88 | for ($h = 0; $h < $dir_hash_level; ++$h) { |
| 89 | $real_hash_dir .= '/' . $hash_dirs[$h]; |
| 90 | if (!@is_dir($real_hash_dir)) { |
| 91 | if (!@mkdir($real_hash_dir, 0770)) { |
| 92 | echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' . |
| 93 | _("Could not create hashed directory structure!") . "<br>\n" . |
| 94 | _("Please contact your system administrator and report this error.") . "<br>\n"; |
| 95 | exit; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /* And return that directory. */ |
| 101 | return ($real_hash_dir); |
| 102 | } |
| 103 | |
| 104 | function computeHashDirs($username) { |
| 105 | /* Compute the hash for this user and extract the hash directories. */ |
| 106 | $hash = base_convert(crc32($username), 10, 16); |
| 107 | $hash_dirs = array(); |
| 108 | for ($h = 0; $h < 4; ++ $h) { |
| 109 | $hash_dirs[] = substr($hash, $h, 1); |
| 110 | } |
| 111 | |
| 112 | /* Return our array of hash directories. */ |
| 113 | return ($hash_dirs); |
| 114 | } |
| 115 | |
| 116 | ?> |