| 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 | $prefs_are_cached = false; |
| 21 | $prefs_cache = array(); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Check the preferences into the session cache. |
| 26 | */ |
| 27 | function cachePrefValues($data_dir, $username) { |
| 28 | global $prefs_are_cached, $prefs_cache; |
| 29 | |
| 30 | if ($prefs_are_cached) { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | session_unregister('prefs_cache'); |
| 35 | session_unregister('prefs_are_cached'); |
| 36 | |
| 37 | /* Calculate the filename for the user's preference file */ |
| 38 | $filename = getHashedFile($username, $data_dir, "$username.pref"); |
| 39 | |
| 40 | /* A call to checkForPrefs here should take eliminate the need for */ |
| 41 | /* this to be called throughout the rest of the SquirrelMail code. */ |
| 42 | checkForPrefs($data_dir, $username, $filename); |
| 43 | |
| 44 | /* Make sure that the preference file now DOES exist. */ |
| 45 | if (!file_exists($filename)) { |
| 46 | echo sprintf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) . "<br>\n"; |
| 47 | exit; |
| 48 | } |
| 49 | |
| 50 | $file = fopen($filename, 'r'); |
| 51 | |
| 52 | /* Read in the preferences. */ |
| 53 | $highlight_num = 0; |
| 54 | while (! feof($file)) { |
| 55 | $pref = trim(fgets($file, 1024)); |
| 56 | $equalsAt = strpos($pref, '='); |
| 57 | if ($equalsAt > 0) { |
| 58 | $key = substr($pref, 0, $equalsAt); |
| 59 | $value = substr($pref, $equalsAt + 1); |
| 60 | if (substr($key, 0, 9) == 'highlight') { |
| 61 | $key = 'highlight' . $highlight_num; |
| 62 | $highlight_num ++; |
| 63 | } |
| 64 | |
| 65 | if ($value != '') { |
| 66 | $prefs_cache[$key] = $value; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | fclose($file); |
| 71 | |
| 72 | $prefs_are_cached = true; |
| 73 | |
| 74 | session_register('prefs_cache'); |
| 75 | session_register('prefs_are_cached'); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Return the value for the prefernce given by $string. |
| 80 | */ |
| 81 | function getPref($data_dir, $username, $string, $default = '') { |
| 82 | global $prefs_cache; |
| 83 | $result = ''; |
| 84 | |
| 85 | cachePrefValues($data_dir, $username); |
| 86 | |
| 87 | if (isset($prefs_cache[$string])) { |
| 88 | $result = $prefs_cache[$string]; |
| 89 | } else { |
| 90 | $result = $default; |
| 91 | } |
| 92 | |
| 93 | return ($result); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Save the preferences for this user. |
| 98 | */ |
| 99 | function savePrefValues($data_dir, $username) { |
| 100 | global $prefs_cache; |
| 101 | |
| 102 | $filename = getHashedFile($username, $data_dir, "$username.pref"); |
| 103 | |
| 104 | $file = fopen($filename, 'w'); |
| 105 | foreach ($prefs_cache as $Key => $Value) { |
| 106 | if (isset($Value)) { |
| 107 | fwrite($file, $Key . '=' . $Value . "\n"); |
| 108 | } |
| 109 | } |
| 110 | fclose($file); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Remove a preference for the current user. |
| 115 | */ |
| 116 | function removePref($data_dir, $username, $string) { |
| 117 | global $prefs_cache; |
| 118 | |
| 119 | cachePrefValues($data_dir, $username); |
| 120 | |
| 121 | if (isset($prefs_cache[$string])) { |
| 122 | unset($prefs_cache[$string]); |
| 123 | } |
| 124 | |
| 125 | savePrefValues($data_dir, $username); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Set a there preference $string to $value. |
| 130 | */ |
| 131 | function setPref($data_dir, $username, $string, $value) { |
| 132 | global $prefs_cache; |
| 133 | |
| 134 | cachePrefValues($data_dir, $username); |
| 135 | if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | if ($value === '') { |
| 140 | removePref($data_dir, $username, $string); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | $prefs_cache[$string] = $value; |
| 145 | savePrefValues($data_dir, $username); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Check for a preferences file. If one can not be found, create it. |
| 150 | */ |
| 151 | function checkForPrefs($data_dir, $username, $filename = '') { |
| 152 | /* First, make sure we have the filename. */ |
| 153 | if ($filename == '') { |
| 154 | $filename = getHashedFile($username, $data_dir, '$username.pref'); |
| 155 | } |
| 156 | |
| 157 | /* Then, check if the file exists. */ |
| 158 | if (!@file_exists($filename) ) { |
| 159 | /* First, check the $data_dir for the default preference file. */ |
| 160 | $default_pref = $data_dir . 'default_pref'; |
| 161 | |
| 162 | /* If it is not there, check the internal data directory. */ |
| 163 | if (!@file_exists($default_pref)) { |
| 164 | $default_pref = '../data/default_pref'; |
| 165 | } |
| 166 | |
| 167 | /* Otherwise, report an error. */ |
| 168 | if (!file_exists($default_pref)) { |
| 169 | echo _("Error opening ") . $default_pref . "<br>\n"; |
| 170 | echo _("Default preference file not found!") . "<br>\n"; |
| 171 | echo _("Please contact your system administrator and report this error.") . "<br>\n"; |
| 172 | exit; |
| 173 | } else if (!@copy($default_pref, $filename)) { |
| 174 | echo _("Error opening ") . $default_pref . '<br>'; |
| 175 | echo _("Could not create initial preference file!") . "<br>\n"; |
| 176 | echo _("Please contact your system administrator and report this error.") . "<br>\n"; |
| 177 | exit; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Write the User Signature. |
| 184 | */ |
| 185 | function setSig($data_dir, $username, $value) { |
| 186 | $filename = getHashedFile($username, $data_dir, "$username.sig"); |
| 187 | $file = fopen($filename, 'w'); |
| 188 | fwrite($file, $value); |
| 189 | fclose($file); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Get the signature. |
| 194 | */ |
| 195 | function getSig($data_dir, $username) { |
| 196 | #$filename = $data_dir . $username . '.sig'; |
| 197 | $filename = getHashedFile($username, $data_dir, "$username.sig"); |
| 198 | $sig = ''; |
| 199 | if (file_exists($filename)) { |
| 200 | $file = fopen($filename, 'r'); |
| 201 | while (!feof($file)) { |
| 202 | $sig .= fgets($file, 1024); |
| 203 | } |
| 204 | fclose($file); |
| 205 | } |
| 206 | return $sig; |
| 207 | } |
| 208 | |
| 209 | function getHashedFile($username, $dir, $datafile, $hash_search = true) { |
| 210 | global $dir_hash_level; |
| 211 | |
| 212 | /* Remove trailing slash from $dir if found */ |
| 213 | if (substr($dir, -1) == '/') { |
| 214 | $dir = substr($dir, 0, strlen($dir) - 1); |
| 215 | } |
| 216 | |
| 217 | /* Compute the hash for this user and extract the hash directories. */ |
| 218 | $hash_dirs = computeHashDirs($username); |
| 219 | |
| 220 | /* First, get and make sure the full hash directory exists. */ |
| 221 | $real_hash_dir = getHashedDir($username, $dir, $hash_dirs); |
| 222 | |
| 223 | /* Set the value of our real data file. */ |
| 224 | $result = "$real_hash_dir/$datafile"; |
| 225 | |
| 226 | /* Check for this file in the real hash directory. */ |
| 227 | if ($hash_search && !@file_exists($result)) { |
| 228 | /* First check the base directory, the most common location. */ |
| 229 | if (@file_exists("$dir/$datafile")) { |
| 230 | rename("$dir/$datafile", $result); |
| 231 | |
| 232 | /* Then check the full range of possible hash directories. */ |
| 233 | } else { |
| 234 | $check_hash_dir = $dir; |
| 235 | for ($h = 0; $h < 4; ++$h) { |
| 236 | $check_hash_dir .= '/' . $hash_dirs[$h]; |
| 237 | if (@is_readable("$check_hash_dir/$datafile")) { |
| 238 | rename("$check_hash_dir/$datafile", $result); |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /* Return the full hashed datafile path. */ |
| 246 | return ($result); |
| 247 | } |
| 248 | |
| 249 | function getHashedDir($username, $dir, $hash_dirs = '') { |
| 250 | global $dir_hash_level; |
| 251 | |
| 252 | /* Remove trailing slash from $dir if found */ |
| 253 | if (substr($dir, -1) == '/') { |
| 254 | $dir = substr($dir, 0, strlen($dir) - 1); |
| 255 | } |
| 256 | |
| 257 | /* If necessary, populate the hash dir variable. */ |
| 258 | if ($hash_dirs == '') { |
| 259 | $hash_dirs = computeHashDirs($username); |
| 260 | } |
| 261 | |
| 262 | /* Make sure the full hash directory exists. */ |
| 263 | $real_hash_dir = $dir; |
| 264 | for ($h = 0; $h < $dir_hash_level; ++$h) { |
| 265 | $real_hash_dir .= '/' . $hash_dirs[$h]; |
| 266 | if (!@is_dir($real_hash_dir)) { |
| 267 | if (!@mkdir($real_hash_dir, 0770)) { |
| 268 | echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>'; |
| 269 | echo _("Could not create hashed directory structure!") . "<br>\n"; |
| 270 | echo _("Please contact your system administrator and report this error.") . "<br>\n"; |
| 271 | exit; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /* And return that directory. */ |
| 277 | return ($real_hash_dir); |
| 278 | } |
| 279 | |
| 280 | function computeHashDirs($username) { |
| 281 | /* Compute the hash for this user and extract the hash directories. */ |
| 282 | $hash = base_convert(crc32($username), 10, 16); |
| 283 | $hash_dirs = array(); |
| 284 | for ($h = 0; $h < 4; ++ $h) { |
| 285 | $hash_dirs[] = substr($hash, $h, 1); |
| 286 | } |
| 287 | |
| 288 | /* Return our array of hash directories. */ |
| 289 | return ($hash_dirs); |
| 290 | } |
| 291 | |
| 292 | ?> |