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