| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * prefs.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2003 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 | * @package squirrelmail |
| 13 | */ |
| 14 | |
| 15 | /** Include global.php */ |
| 16 | require_once(SM_PATH . 'functions/global.php'); |
| 17 | |
| 18 | sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION ); |
| 19 | sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION ); |
| 20 | |
| 21 | $rg = ini_get('register_globals'); |
| 22 | |
| 23 | /* if php version >= 4.1 OR (4.0 AND $rg = off) */ |
| 24 | if ( !sqsession_is_registered('prefs_are_cached') || |
| 25 | !isset( $prefs_cache) || |
| 26 | !is_array( $prefs_cache) || |
| 27 | check_php_version(4,1) || |
| 28 | empty($rg) |
| 29 | ) { |
| 30 | $prefs_are_cached = false; |
| 31 | $prefs_cache = array(); |
| 32 | } |
| 33 | |
| 34 | if (isset($prefs_dsn) && !empty($prefs_dsn)) { |
| 35 | require_once(SM_PATH . 'functions/db_prefs.php'); |
| 36 | } else { |
| 37 | require_once(SM_PATH . 'functions/file_prefs.php'); |
| 38 | } |
| 39 | |
| 40 | /* Hashing functions */ |
| 41 | |
| 42 | /** |
| 43 | * Given a username and datafilename, this will return the path to the |
| 44 | * hashed location of that datafile. |
| 45 | * |
| 46 | * @param string username the username of the current user |
| 47 | * @param string dir the squirrelmail datadir |
| 48 | * @param string datafile the name of the file to open |
| 49 | * @param bool hash_seach default true |
| 50 | * @return string the hashed location of datafile |
| 51 | */ |
| 52 | function getHashedFile($username, $dir, $datafile, $hash_search = true) { |
| 53 | global $dir_hash_level; |
| 54 | |
| 55 | /* Remove trailing slash from $dir if found */ |
| 56 | if (substr($dir, -1) == '/') { |
| 57 | $dir = substr($dir, 0, strlen($dir) - 1); |
| 58 | } |
| 59 | |
| 60 | /* Compute the hash for this user and extract the hash directories. */ |
| 61 | $hash_dirs = computeHashDirs($username); |
| 62 | |
| 63 | /* First, get and make sure the full hash directory exists. */ |
| 64 | $real_hash_dir = getHashedDir($username, $dir, $hash_dirs); |
| 65 | |
| 66 | /* Set the value of our real data file, after we've removed unwanted characters. */ |
| 67 | $datafile = str_replace('/', '_', $datafile); |
| 68 | $result = "$real_hash_dir/$datafile"; |
| 69 | |
| 70 | /* Check for this file in the real hash directory. */ |
| 71 | if ($hash_search && !@file_exists($result)) { |
| 72 | /* First check the base directory, the most common location. */ |
| 73 | if (@file_exists("$dir/$datafile")) { |
| 74 | rename("$dir/$datafile", $result); |
| 75 | |
| 76 | /* Then check the full range of possible hash directories. */ |
| 77 | } else { |
| 78 | $check_hash_dir = $dir; |
| 79 | for ($h = 0; $h < 4; ++$h) { |
| 80 | $check_hash_dir .= '/' . $hash_dirs[$h]; |
| 81 | if (@is_readable("$check_hash_dir/$datafile")) { |
| 82 | rename("$check_hash_dir/$datafile", $result); |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /* Return the full hashed datafile path. */ |
| 90 | return ($result); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Helper function for getHashedFile, given a username returns the hashed |
| 95 | * dir for that username. |
| 96 | * |
| 97 | * @param string username the username of the current user |
| 98 | * @param string dir the squirrelmail datadir |
| 99 | * @param string hash_dirs default '' |
| 100 | * @return the path to the hash dir for username |
| 101 | */ |
| 102 | function getHashedDir($username, $dir, $hash_dirs = '') { |
| 103 | global $dir_hash_level; |
| 104 | |
| 105 | /* Remove trailing slash from $dir if found */ |
| 106 | if (substr($dir, -1) == '/') { |
| 107 | $dir = substr($dir, 0, strlen($dir) - 1); |
| 108 | } |
| 109 | |
| 110 | /* If necessary, populate the hash dir variable. */ |
| 111 | if ($hash_dirs == '') { |
| 112 | $hash_dirs = computeHashDirs($username); |
| 113 | } |
| 114 | |
| 115 | /* Make sure the full hash directory exists. */ |
| 116 | $real_hash_dir = $dir; |
| 117 | for ($h = 0; $h < $dir_hash_level; ++$h) { |
| 118 | $real_hash_dir .= '/' . $hash_dirs[$h]; |
| 119 | if (!@is_dir($real_hash_dir)) { |
| 120 | if (!@mkdir($real_hash_dir, 0770)) { |
| 121 | echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' . |
| 122 | _("Could not create hashed directory structure!") . "<br>\n" . |
| 123 | _("Please contact your system administrator and report this error.") . "<br>\n"; |
| 124 | exit; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /* And return that directory. */ |
| 130 | return ($real_hash_dir); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Helper function for getHashDir which does the actual hash calculation. |
| 135 | * |
| 136 | * @param string username the username to calculate the hash dir for |
| 137 | * @return array a list of hash dirs for this username |
| 138 | */ |
| 139 | function computeHashDirs($username) { |
| 140 | /* Compute the hash for this user and extract the hash directories. */ |
| 141 | $hash = base_convert(crc32($username), 10, 16); |
| 142 | $hash_dirs = array(); |
| 143 | for ($h = 0; $h < 4; ++ $h) { |
| 144 | $hash_dirs[] = substr($hash, $h, 1); |
| 145 | } |
| 146 | |
| 147 | /* Return our array of hash directories. */ |
| 148 | return ($hash_dirs); |
| 149 | } |
| 150 | |
| 151 | ?> |