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