| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * prefs.php |
| 5 | * |
| 6 | * This contains functions for filebased user prefs locations |
| 7 | * |
| 8 | * @copyright © 1999-2007 The SquirrelMail Project Team |
| 9 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| 10 | * @version $Id$ |
| 11 | * @package squirrelmail |
| 12 | * @subpackage prefs |
| 13 | */ |
| 14 | |
| 15 | |
| 16 | |
| 17 | /* Hashing functions */ |
| 18 | |
| 19 | /** |
| 20 | * Given a username and datafilename, this will return the path to the |
| 21 | * hashed location of that datafile. |
| 22 | * |
| 23 | * @param string username the username of the current user |
| 24 | * @param string dir the SquirrelMail datadir |
| 25 | * @param string datafile the name of the file to open |
| 26 | * @param bool hash_seach default true |
| 27 | * @return string the hashed location of datafile |
| 28 | * @since 1.2.0 |
| 29 | */ |
| 30 | function getHashedFile($username, $dir, $datafile, $hash_search = true) { |
| 31 | |
| 32 | /* Remove trailing slash from $dir if found */ |
| 33 | if (substr($dir, -1) == '/') { |
| 34 | $dir = substr($dir, 0, strlen($dir) - 1); |
| 35 | } |
| 36 | |
| 37 | /* Compute the hash for this user and extract the hash directories. */ |
| 38 | $hash_dirs = computeHashDirs($username); |
| 39 | |
| 40 | /* First, get and make sure the full hash directory exists. */ |
| 41 | $real_hash_dir = getHashedDir($username, $dir, $hash_dirs); |
| 42 | |
| 43 | /* Set the value of our real data file, after we've removed unwanted characters. */ |
| 44 | $datafile = str_replace('/', '_', $datafile); |
| 45 | $result = "$real_hash_dir/$datafile"; |
| 46 | |
| 47 | /* Check for this file in the real hash directory. */ |
| 48 | if ($hash_search && !@file_exists($result)) { |
| 49 | /* First check the base directory, the most common location. */ |
| 50 | if (@file_exists("$dir/$datafile")) { |
| 51 | rename("$dir/$datafile", $result); |
| 52 | |
| 53 | /* Then check the full range of possible hash directories. */ |
| 54 | } else { |
| 55 | $check_hash_dir = $dir; |
| 56 | for ($h = 0; $h < 4; ++$h) { |
| 57 | $check_hash_dir .= '/' . $hash_dirs[$h]; |
| 58 | if (@is_readable("$check_hash_dir/$datafile")) { |
| 59 | rename("$check_hash_dir/$datafile", $result); |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /* Return the full hashed datafile path. */ |
| 67 | return ($result); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Helper function for getHashedFile, given a username returns the hashed |
| 72 | * dir for that username. |
| 73 | * |
| 74 | * @param string username the username of the current user |
| 75 | * @param string dir the SquirrelMail datadir |
| 76 | * @param string hash_dirs default '' |
| 77 | * @return the path to the hash dir for username |
| 78 | * @since 1.2.0 |
| 79 | */ |
| 80 | function getHashedDir($username, $dir, $hash_dirs = '') { |
| 81 | global $dir_hash_level; |
| 82 | |
| 83 | /* Remove trailing slash from $dir if found */ |
| 84 | if (substr($dir, -1) == '/') { |
| 85 | $dir = substr($dir, 0, strlen($dir) - 1); |
| 86 | } |
| 87 | |
| 88 | /* If necessary, populate the hash dir variable. */ |
| 89 | if ($hash_dirs == '') { |
| 90 | $hash_dirs = computeHashDirs($username); |
| 91 | } |
| 92 | |
| 93 | /* Make sure the full hash directory exists. */ |
| 94 | $real_hash_dir = $dir; |
| 95 | for ($h = 0; $h < $dir_hash_level; ++$h) { |
| 96 | $real_hash_dir .= '/' . $hash_dirs[$h]; |
| 97 | if (!@is_dir($real_hash_dir)) { |
| 98 | //FIXME: When safe_mode is turned on, the error suppression below makes debugging safe_mode UID/GID restrictions tricky... for now, I will add a check in configtest |
| 99 | if (!@mkdir($real_hash_dir, 0770)) { |
| 100 | error_box ( sprintf(_("Error creating directory %s."), $real_hash_dir) . "\n" . |
| 101 | _("Could not create hashed directory structure!") . "\n" . |
| 102 | _("Please contact your system administrator and report this error.") ); |
| 103 | exit; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* And return that directory. */ |
| 109 | return ($real_hash_dir); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Helper function for getHashDir which does the actual hash calculation. |
| 114 | * |
| 115 | * @param string username the username to calculate the hash dir for |
| 116 | * @return array a list of hash dirs for this username |
| 117 | * @since 1.2.0 |
| 118 | */ |
| 119 | function computeHashDirs($username) { |
| 120 | /* Compute the hash for this user and extract the hash directories. */ |
| 121 | $hash = base_convert(crc32($username), 10, 16); |
| 122 | $hash_dirs = array(); |
| 123 | for ($h = 0; $h < 4; ++ $h) { |
| 124 | $hash_dirs[] = substr($hash, $h, 1); |
| 125 | } |
| 126 | |
| 127 | /* Return our array of hash directories. */ |
| 128 | return ($hash_dirs); |
| 129 | } |