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