59177427 |
1 | <?php |
2ba13803 |
2 | |
35586184 |
3 | /** |
4 | * prefs.php |
5 | * |
15e6162e |
6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
35586184 |
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 | |
35586184 |
14 | global $prefs_are_cached, $prefs_cache; |
93b2364f |
15 | |
16 | if ( !session_is_registered('prefs_are_cached') || |
17 | !isset( $prefs_cache) || |
3499f99f |
18 | !is_array( $prefs_cache) || |
121fc115 |
19 | substr( phpversion(), 0, 3 ) == '4.1' || |
20 | substr( phpversion(), 0, 3 ) == '4.2' ) { |
35586184 |
21 | $prefs_are_cached = false; |
22 | $prefs_cache = array(); |
23 | } |
31ac7db8 |
24 | |
3499f99f |
25 | if (isset($prefs_dsn) && !empty($prefs_dsn)) { |
26 | require_once('../functions/db_prefs.php'); |
27 | } else { |
28 | require_once('../functions/file_prefs.php'); |
7b294953 |
29 | } |
30 | |
3499f99f |
31 | /* Hashing functions */ |
fb120846 |
32 | |
3392dc86 |
33 | function getHashedFile($username, $dir, $datafile, $hash_search = true) { |
34 | global $dir_hash_level; |
35 | |
311339a0 |
36 | /* Remove trailing slash from $dir if found */ |
37 | if (substr($dir, -1) == '/') { |
38 | $dir = substr($dir, 0, strlen($dir) - 1); |
39 | } |
40 | |
3392dc86 |
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. */ |
3a94810f |
51 | if ($hash_search && !@file_exists($result)) { |
3392dc86 |
52 | /* First check the base directory, the most common location. */ |
3a94810f |
53 | if (@file_exists("$dir/$datafile")) { |
3392dc86 |
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]; |
311339a0 |
61 | if (@is_readable("$check_hash_dir/$datafile")) { |
3392dc86 |
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 | |
3a94810f |
76 | /* Remove trailing slash from $dir if found */ |
77 | if (substr($dir, -1) == '/') { |
78 | $dir = substr($dir, 0, strlen($dir) - 1); |
79 | } |
80 | |
3392dc86 |
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]; |
3a94810f |
90 | if (!@is_dir($real_hash_dir)) { |
91 | if (!@mkdir($real_hash_dir, 0770)) { |
cab99c3a |
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"; |
3a94810f |
95 | exit; |
96 | } |
3392dc86 |
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 | |
35586184 |
116 | ?> |