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