3499f99f |
1 | <?php |
2 | |
3 | /** |
4 | * file_prefs.php |
5 | * |
3499f99f |
6 | * This contains functions for manipulating user preferences in files |
7 | * |
ae5dddc0 |
8 | * @copyright 1999-2011 The SquirrelMail Project Team |
4b4abf93 |
9 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
31841a9e |
10 | * @version $Id$ |
d6c32258 |
11 | * @package squirrelmail |
ace4c62c |
12 | * @subpackage prefs |
13 | * @since 1.2.5 |
3499f99f |
14 | */ |
15 | |
b94bfe13 |
16 | |
3499f99f |
17 | /** |
18 | * Check the preferences into the session cache. |
37b11ab0 |
19 | * |
ace4c62c |
20 | * @param string $data_dir |
21 | * @param string $username |
37b11ab0 |
22 | * |
ace4c62c |
23 | * @since 1.1.3 |
37b11ab0 |
24 | * |
3499f99f |
25 | */ |
26 | function cachePrefValues($data_dir, $username) { |
27 | global $prefs_are_cached, $prefs_cache; |
324ac3c5 |
28 | |
37d5278d |
29 | sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION ); |
3499f99f |
30 | if ( isset($prefs_are_cached) && $prefs_are_cached) { |
0d8a462c |
31 | sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION ); |
202bcbcc |
32 | // sm_print_r($prefs_cache); |
33 | // exit; |
2b2967d4 |
34 | return; |
3499f99f |
35 | } |
324ac3c5 |
36 | |
9eb0fbd4 |
37 | sqsession_unregister('prefs_cache'); |
38 | sqsession_unregister('prefs_are_cached'); |
324ac3c5 |
39 | |
3499f99f |
40 | /* Calculate the filename for the user's preference file */ |
41 | $filename = getHashedFile($username, $data_dir, "$username.pref"); |
42 | |
43 | /* A call to checkForPrefs here should take eliminate the need for */ |
44 | /* this to be called throughout the rest of the SquirrelMail code. */ |
45 | checkForPrefs($data_dir, $username, $filename); |
46 | |
47 | /* Make sure that the preference file now DOES exist. */ |
48 | if (!file_exists($filename)) { |
9be8198d |
49 | logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) ); |
3499f99f |
50 | exit; |
51 | } |
52 | |
35b5fa13 |
53 | /* Open the file, or else display an error to the user. */ |
54 | if(!$file = @fopen($filename, 'r')) |
55 | { |
35b5fa13 |
56 | logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) ); |
57 | exit; |
58 | } |
3499f99f |
59 | |
60 | /* Read in the preferences. */ |
61 | $highlight_num = 0; |
62 | while (! feof($file)) { |
710a8254 |
63 | $pref = ''; |
64 | /* keep reading a pref until we reach an eol (\n (or \r for macs)) */ |
65 | while($read = fgets($file, 1024)) |
66 | { |
67 | $pref .= $read; |
68 | if(strpos($read,"\n") || strpos($read,"\r")) |
69 | break; |
70 | } |
71 | $pref = trim($pref); |
3499f99f |
72 | $equalsAt = strpos($pref, '='); |
73 | if ($equalsAt > 0) { |
74 | $key = substr($pref, 0, $equalsAt); |
75 | $value = substr($pref, $equalsAt + 1); |
37b11ab0 |
76 | |
77 | //FIXME: this code is not in db_prefs.php that I can see |
1c159927 |
78 | /* this is to 'rescue' old-style highlighting rules. */ |
3499f99f |
79 | if (substr($key, 0, 9) == 'highlight') { |
80 | $key = 'highlight' . $highlight_num; |
81 | $highlight_num ++; |
82 | } |
83 | |
37b11ab0 |
84 | //FIXME: this code is not in db_prefs.php that I can see |
3499f99f |
85 | if ($value != '') { |
86 | $prefs_cache[$key] = $value; |
87 | } |
88 | } |
491696a8 |
89 | } |
90 | fclose($file); |
3499f99f |
91 | |
92 | $prefs_are_cached = TRUE; |
93 | |
9eb0fbd4 |
94 | sqsession_register($prefs_cache, 'prefs_cache'); |
95 | sqsession_register($prefs_are_cached, 'prefs_are_cached'); |
3499f99f |
96 | } |
324ac3c5 |
97 | |
3499f99f |
98 | /** |
37b11ab0 |
99 | * Return the value for the desired preference. |
100 | * |
101 | * @param string $data_dir data directory |
102 | * @param string $username user name |
103 | * @param string $pref_name preference name |
104 | * @param string $default (since 1.2.0) default preference value |
105 | * |
ace4c62c |
106 | * @return mixed |
37b11ab0 |
107 | * |
3499f99f |
108 | */ |
37b11ab0 |
109 | function getPref($data_dir, $username, $pref_name, $default = '') { |
3499f99f |
110 | global $prefs_cache; |
397a9d7b |
111 | |
81cc6a19 |
112 | $temp = array(&$username, &$pref_name); |
113 | $result = do_hook('get_pref_override', $temp); |
dfc5b26c |
114 | if (is_null($result)) { |
324ac3c5 |
115 | cachePrefValues($data_dir, $username); |
37b11ab0 |
116 | if (isset($prefs_cache[$pref_name])) { |
117 | $result = $prefs_cache[$pref_name]; |
324ac3c5 |
118 | } else { |
dfc5b26c |
119 | //FIXME: is there a justification for having two prefs hooks so close? who uses them? |
81cc6a19 |
120 | $temp = array(&$username, &$pref_name); |
121 | $result = do_hook('get_pref', $temp); |
dfc5b26c |
122 | if (is_null($result)) { |
324ac3c5 |
123 | $result = $default; |
124 | } |
125 | } |
3499f99f |
126 | } |
3499f99f |
127 | return ($result); |
128 | } |
129 | |
130 | /** |
131 | * Save the preferences for this user. |
37b11ab0 |
132 | * |
ace4c62c |
133 | * @param string $data_dir data directory |
134 | * @param string $username user name |
37b11ab0 |
135 | * |
ace4c62c |
136 | * @since 1.1.3 |
37b11ab0 |
137 | * |
3499f99f |
138 | */ |
2b2967d4 |
139 | function savePrefValues($data_dir, $username) { |
140 | global $prefs_cache; |
324ac3c5 |
141 | |
3499f99f |
142 | $filename = getHashedFile($username, $data_dir, "$username.pref"); |
143 | |
35b5fa13 |
144 | /* Open the file for writing, or else display an error to the user. */ |
be6f5c46 |
145 | if(!$file = @fopen($filename.'.tmp', 'w')) |
35b5fa13 |
146 | { |
be6f5c46 |
147 | logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') ); |
35b5fa13 |
148 | exit; |
149 | } |
3499f99f |
150 | foreach ($prefs_cache as $Key => $Value) { |
151 | if (isset($Value)) { |
3ecad5e6 |
152 | if ( sq_fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) { |
dabef6fd |
153 | logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') ); |
154 | exit; |
155 | } |
3499f99f |
156 | } |
157 | } |
158 | fclose($file); |
1d44a937 |
159 | if (! @copy($filename . '.tmp',$filename) ) { |
b94bfe13 |
160 | logout_error( sprintf( _("Preference file, %s, could not be copied from temporary file, %s. Contact your system administrator to resolve this issue."), $filename, $filename . '.tmp') ); |
161 | exit; |
162 | } |
dabef6fd |
163 | @unlink($filename . '.tmp'); |
4623cba9 |
164 | @chmod($filename, 0600); |
c4f32485 |
165 | sqsession_register($prefs_cache , 'prefs_cache'); |
3499f99f |
166 | } |
167 | |
168 | /** |
169 | * Remove a preference for the current user. |
37b11ab0 |
170 | * |
171 | * @param string $data_dir data directory |
172 | * @param string $username user name |
173 | * @param string $pref_name preference name |
174 | * |
3499f99f |
175 | */ |
37b11ab0 |
176 | function removePref($data_dir, $username, $pref_name) { |
3499f99f |
177 | global $prefs_cache; |
178 | |
2b2967d4 |
179 | cachePrefValues($data_dir, $username); |
324ac3c5 |
180 | |
37b11ab0 |
181 | if (isset($prefs_cache[$pref_name])) { |
182 | unset($prefs_cache[$pref_name]); |
3499f99f |
183 | } |
324ac3c5 |
184 | |
2b2967d4 |
185 | savePrefValues($data_dir, $username); |
3499f99f |
186 | } |
187 | |
188 | /** |
37b11ab0 |
189 | * Set the desired preference setting ($pref_name) |
190 | * to whatever is in $value. |
191 | * |
192 | * @param string $data_dir data directory |
193 | * @param string $username user name |
194 | * @param string $pref_name preference name |
195 | * @param mixed $value preference value |
196 | * |
3499f99f |
197 | */ |
37b11ab0 |
198 | function setPref($data_dir, $username, $pref_name, $value) { |
3499f99f |
199 | global $prefs_cache; |
200 | |
2b2967d4 |
201 | cachePrefValues($data_dir, $username); |
37b11ab0 |
202 | if (isset($prefs_cache[$pref_name]) && ($prefs_cache[$pref_name] == $value)) { |
3499f99f |
203 | return; |
204 | } |
205 | |
206 | if ($value === '') { |
37b11ab0 |
207 | removePref($data_dir, $username, $pref_name); |
2b2967d4 |
208 | return; |
3499f99f |
209 | } |
2b2967d4 |
210 | |
37b11ab0 |
211 | $prefs_cache[$pref_name] = $value; |
2b2967d4 |
212 | savePrefValues($data_dir, $username); |
3499f99f |
213 | } |
214 | |
215 | /** |
216 | * Check for a preferences file. If one can not be found, create it. |
37b11ab0 |
217 | * |
ace4c62c |
218 | * @param string $data_dir data directory |
219 | * @param string $username user name |
220 | * @param string $filename (since 1.2.0) preference file name. |
37b11ab0 |
221 | * (OPTIONAL; default is an empty string, |
222 | * in which case the file name is |
223 | * automatically detected) |
224 | * |
3499f99f |
225 | */ |
226 | function checkForPrefs($data_dir, $username, $filename = '') { |
227 | /* First, make sure we have the filename. */ |
228 | if ($filename == '') { |
bd1180c1 |
229 | $filename = getHashedFile($username, $data_dir, "$username.pref"); |
3499f99f |
230 | } |
231 | |
232 | /* Then, check if the file exists. */ |
233 | if (!@file_exists($filename) ) { |
a04a3c20 |
234 | |
235 | /* If it does not exist, check for default_prefs */ |
f8a1ed5a |
236 | |
a04a3c20 |
237 | /* First, check legacy locations: data dir */ |
e8c533f6 |
238 | if(substr($data_dir,-1) != '/') { |
239 | $data_dir .= '/'; |
240 | } |
241 | $default_pref = $data_dir . 'default_pref'; |
3499f99f |
242 | |
a04a3c20 |
243 | /* or legacy location: internal data dir */ |
3499f99f |
244 | if (!@file_exists($default_pref)) { |
bd9c880b |
245 | $default_pref = SM_PATH . 'data/default_pref'; |
3499f99f |
246 | } |
247 | |
a04a3c20 |
248 | /* If no legacies, check where we'd expect it to be located: |
249 | * under config/ */ |
250 | if (!@file_exists($default_pref)) { |
251 | $default_pref = SM_PATH . 'config/default_pref'; |
252 | } |
f8a1ed5a |
253 | |
a04a3c20 |
254 | /* If a default_pref file found, try to copy it, if none found, |
255 | * try to create an empty one. If that fails, report an error. |
256 | */ |
257 | if ( |
258 | ( is_readable($default_pref) && !@copy($default_pref, $filename) ) || |
259 | !@touch($filename) |
260 | ) { |
bea235b5 |
261 | $uid = 'that the web server is running as'; |
305a1012 |
262 | if (function_exists('posix_getuid')){ |
263 | $user_data = posix_getpwuid(posix_getuid()); |
264 | $uid = $user_data['name']; |
265 | } |
a04a3c20 |
266 | $errTitle = _("Could not create initial preference file!"); |
ae13f72f |
267 | $errString = $errTitle . "\n" . |
bea235b5 |
268 | sprintf( _("%s should be writable by the user %s."), $data_dir, $uid ) . "\n" . |
ae13f72f |
269 | _("Please contact your system administrator and report this error.") ; |
9be8198d |
270 | logout_error( $errString, $errTitle ); |
3499f99f |
271 | exit; |
272 | } |
273 | } |
274 | } |
275 | |
276 | /** |
277 | * Write the User Signature. |
37b11ab0 |
278 | * |
ace4c62c |
279 | * @param string $data_dir data directory |
280 | * @param string $username user name |
37b11ab0 |
281 | * @param integer $number (since 1.2.5) identity number. |
282 | * (before 1.2.5., this parameter |
283 | * was used for the signature value) |
284 | * @param string $value (since 1.2.5) signature value |
285 | * |
3499f99f |
286 | */ |
01265fba |
287 | function setSig($data_dir, $username, $number, $value) { |
288df1a0 |
288 | // Limit signature size to 64KB (database BLOB limit) |
289 | if (strlen($value)>65536) { |
290 | error_option_save(_("Signature is too big.")); |
291 | return; |
292 | } |
01265fba |
293 | $filename = getHashedFile($username, $data_dir, "$username.si$number"); |
35b5fa13 |
294 | /* Open the file for writing, or else display an error to the user. */ |
dabef6fd |
295 | if(!$file = @fopen("$filename.tmp", 'w')) { |
dabef6fd |
296 | logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') ); |
35b5fa13 |
297 | exit; |
298 | } |
3ecad5e6 |
299 | if ( sq_fwrite($file, $value) === FALSE ) { |
dabef6fd |
300 | logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp')); |
301 | exit; |
302 | } |
3499f99f |
303 | fclose($file); |
1d44a937 |
304 | if (! @copy($filename . '.tmp',$filename) ) { |
305 | logout_error( sprintf( _("Signature file, %s, could not be copied from temporary file, %s. Contact your system administrator to resolve this issue."), $filename, $filename . '.tmp') ); |
306 | exit; |
307 | } |
dabef6fd |
308 | @unlink($filename . '.tmp'); |
4623cba9 |
309 | @chmod($filename, 0600); |
dabef6fd |
310 | |
3499f99f |
311 | } |
312 | |
313 | /** |
314 | * Get the signature. |
37b11ab0 |
315 | * |
316 | * @param string $data_dir data directory |
317 | * @param string $username user name |
318 | * @param integer $number (since 1.2.5) identity number |
319 | * |
ace4c62c |
320 | * @return string signature |
37b11ab0 |
321 | * |
3499f99f |
322 | */ |
01265fba |
323 | function getSig($data_dir, $username, $number) { |
01265fba |
324 | $filename = getHashedFile($username, $data_dir, "$username.si$number"); |
3499f99f |
325 | $sig = ''; |
326 | if (file_exists($filename)) { |
35b5fa13 |
327 | /* Open the file, or else display an error to the user. */ |
328 | if(!$file = @fopen($filename, 'r')) |
329 | { |
35b5fa13 |
330 | logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) ); |
331 | exit; |
332 | } |
3499f99f |
333 | while (!feof($file)) { |
334 | $sig .= fgets($file, 1024); |
335 | } |
336 | fclose($file); |
337 | } |
338 | return $sig; |
339 | } |
37d5278d |
340 | |
341 | // vim: et ts=4 |