3499f99f |
1 | <?php |
2 | |
3 | /** |
4 | * file_prefs.php |
5 | * |
76911253 |
6 | * Copyright (c) 1999-2003 The SquirrelMail Project Team |
3499f99f |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * This contains functions for manipulating user preferences in files |
10 | * |
11 | * $Id$ |
12 | */ |
13 | |
b94bfe13 |
14 | /* include this for error messages */ |
15 | include_once(SM_PATH . 'functions/display_messages.php'); |
16 | |
3499f99f |
17 | /** |
18 | * Check the preferences into the session cache. |
19 | */ |
20 | function cachePrefValues($data_dir, $username) { |
21 | global $prefs_are_cached, $prefs_cache; |
22 | |
23 | if ( isset($prefs_are_cached) && $prefs_are_cached) { |
2b2967d4 |
24 | return; |
3499f99f |
25 | } |
26 | |
9eb0fbd4 |
27 | sqsession_unregister('prefs_cache'); |
28 | sqsession_unregister('prefs_are_cached'); |
3499f99f |
29 | |
30 | /* Calculate the filename for the user's preference file */ |
31 | $filename = getHashedFile($username, $data_dir, "$username.pref"); |
32 | |
33 | /* A call to checkForPrefs here should take eliminate the need for */ |
34 | /* this to be called throughout the rest of the SquirrelMail code. */ |
35 | checkForPrefs($data_dir, $username, $filename); |
36 | |
37 | /* Make sure that the preference file now DOES exist. */ |
38 | if (!file_exists($filename)) { |
9be8198d |
39 | logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) ); |
3499f99f |
40 | exit; |
41 | } |
42 | |
35b5fa13 |
43 | /* Open the file, or else display an error to the user. */ |
44 | if(!$file = @fopen($filename, 'r')) |
45 | { |
35b5fa13 |
46 | logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) ); |
47 | exit; |
48 | } |
3499f99f |
49 | |
50 | /* Read in the preferences. */ |
51 | $highlight_num = 0; |
52 | while (! feof($file)) { |
710a8254 |
53 | $pref = ''; |
54 | /* keep reading a pref until we reach an eol (\n (or \r for macs)) */ |
55 | while($read = fgets($file, 1024)) |
56 | { |
57 | $pref .= $read; |
58 | if(strpos($read,"\n") || strpos($read,"\r")) |
59 | break; |
60 | } |
61 | $pref = trim($pref); |
3499f99f |
62 | $equalsAt = strpos($pref, '='); |
63 | if ($equalsAt > 0) { |
64 | $key = substr($pref, 0, $equalsAt); |
65 | $value = substr($pref, $equalsAt + 1); |
1c159927 |
66 | /* this is to 'rescue' old-style highlighting rules. */ |
3499f99f |
67 | if (substr($key, 0, 9) == 'highlight') { |
68 | $key = 'highlight' . $highlight_num; |
69 | $highlight_num ++; |
70 | } |
71 | |
72 | if ($value != '') { |
73 | $prefs_cache[$key] = $value; |
74 | } |
75 | } |
491696a8 |
76 | } |
77 | fclose($file); |
3499f99f |
78 | |
79 | $prefs_are_cached = TRUE; |
80 | |
9eb0fbd4 |
81 | sqsession_register($prefs_cache, 'prefs_cache'); |
82 | sqsession_register($prefs_are_cached, 'prefs_are_cached'); |
3499f99f |
83 | } |
84 | |
85 | /** |
b94bfe13 |
86 | * Return the value for the preference given by $string. |
3499f99f |
87 | */ |
88 | function getPref($data_dir, $username, $string, $default = '') { |
89 | global $prefs_cache; |
397a9d7b |
90 | |
be6f5c46 |
91 | $result = do_hook_function('get_pref_override',array($username,$string)); |
92 | if (!$result) { |
2b2967d4 |
93 | cachePrefValues($data_dir, $username); |
be6f5c46 |
94 | if (isset($prefs_cache[$string])) { |
95 | $result = $prefs_cache[$string]; |
96 | } else { |
97 | $result = do_hook_function('get_pref', array($username,$string)); |
98 | if (!$result) { |
99 | $result = $default; |
100 | } |
101 | } |
3499f99f |
102 | } |
3499f99f |
103 | return ($result); |
104 | } |
105 | |
106 | /** |
107 | * Save the preferences for this user. |
108 | */ |
2b2967d4 |
109 | function savePrefValues($data_dir, $username) { |
110 | global $prefs_cache; |
3499f99f |
111 | |
112 | $filename = getHashedFile($username, $data_dir, "$username.pref"); |
113 | |
35b5fa13 |
114 | /* Open the file for writing, or else display an error to the user. */ |
be6f5c46 |
115 | if(!$file = @fopen($filename.'.tmp', 'w')) |
35b5fa13 |
116 | { |
be6f5c46 |
117 | logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') ); |
35b5fa13 |
118 | exit; |
119 | } |
3499f99f |
120 | foreach ($prefs_cache as $Key => $Value) { |
121 | if (isset($Value)) { |
491696a8 |
122 | if ( @fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) { |
dabef6fd |
123 | logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') ); |
124 | exit; |
125 | } |
3499f99f |
126 | } |
127 | } |
128 | fclose($file); |
1d44a937 |
129 | if (! @copy($filename . '.tmp',$filename) ) { |
b94bfe13 |
130 | 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') ); |
131 | exit; |
132 | } |
dabef6fd |
133 | @unlink($filename . '.tmp'); |
4623cba9 |
134 | @chmod($filename, 0600); |
c4f32485 |
135 | sqsession_register($prefs_cache , 'prefs_cache'); |
3499f99f |
136 | } |
137 | |
138 | /** |
139 | * Remove a preference for the current user. |
140 | */ |
141 | function removePref($data_dir, $username, $string) { |
142 | global $prefs_cache; |
143 | |
2b2967d4 |
144 | cachePrefValues($data_dir, $username); |
145 | |
3499f99f |
146 | if (isset($prefs_cache[$string])) { |
147 | unset($prefs_cache[$string]); |
148 | } |
149 | |
2b2967d4 |
150 | savePrefValues($data_dir, $username); |
3499f99f |
151 | } |
152 | |
153 | /** |
154 | * Set a there preference $string to $value. |
155 | */ |
156 | function setPref($data_dir, $username, $string, $value) { |
157 | global $prefs_cache; |
158 | |
2b2967d4 |
159 | cachePrefValues($data_dir, $username); |
3499f99f |
160 | if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) { |
161 | return; |
162 | } |
163 | |
164 | if ($value === '') { |
2b2967d4 |
165 | removePref($data_dir, $username, $string); |
166 | return; |
3499f99f |
167 | } |
2b2967d4 |
168 | |
169 | $prefs_cache[$string] = $value; |
170 | savePrefValues($data_dir, $username); |
3499f99f |
171 | } |
172 | |
173 | /** |
174 | * Check for a preferences file. If one can not be found, create it. |
175 | */ |
176 | function checkForPrefs($data_dir, $username, $filename = '') { |
177 | /* First, make sure we have the filename. */ |
178 | if ($filename == '') { |
bd1180c1 |
179 | $filename = getHashedFile($username, $data_dir, "$username.pref"); |
3499f99f |
180 | } |
181 | |
182 | /* Then, check if the file exists. */ |
183 | if (!@file_exists($filename) ) { |
184 | /* First, check the $data_dir for the default preference file. */ |
e8c533f6 |
185 | if(substr($data_dir,-1) != '/') { |
186 | $data_dir .= '/'; |
187 | } |
188 | $default_pref = $data_dir . 'default_pref'; |
3499f99f |
189 | |
190 | /* If it is not there, check the internal data directory. */ |
191 | if (!@file_exists($default_pref)) { |
bd9c880b |
192 | $default_pref = SM_PATH . 'data/default_pref'; |
3499f99f |
193 | } |
194 | |
195 | /* Otherwise, report an error. */ |
9be8198d |
196 | $errTitle = sprintf( _("Error opening %s"), $default_pref ); |
921bea1a |
197 | if (!is_readable($default_pref)) { |
9be8198d |
198 | $errString = $errTitle . "<br>\n" . |
921bea1a |
199 | _("Default preference file not found or not readable!") . "<br>\n" . |
9be8198d |
200 | _("Please contact your system administrator and report this error.") . "<br>\n"; |
9be8198d |
201 | logout_error( $errString, $errTitle ); |
3499f99f |
202 | exit; |
203 | } else if (!@copy($default_pref, $filename)) { |
305a1012 |
204 | $uid = 'httpd'; |
205 | if (function_exists('posix_getuid')){ |
206 | $user_data = posix_getpwuid(posix_getuid()); |
207 | $uid = $user_data['name']; |
208 | } |
9be8198d |
209 | $errString = $errTitle . '<br>' . |
210 | _("Could not create initial preference file!") . "<br>\n" . |
211 | sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) . |
212 | "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n"; |
9be8198d |
213 | logout_error( $errString, $errTitle ); |
3499f99f |
214 | exit; |
215 | } |
216 | } |
217 | } |
218 | |
219 | /** |
220 | * Write the User Signature. |
221 | */ |
01265fba |
222 | function setSig($data_dir, $username, $number, $value) { |
223 | $filename = getHashedFile($username, $data_dir, "$username.si$number"); |
35b5fa13 |
224 | /* Open the file for writing, or else display an error to the user. */ |
dabef6fd |
225 | if(!$file = @fopen("$filename.tmp", 'w')) { |
dabef6fd |
226 | logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') ); |
35b5fa13 |
227 | exit; |
228 | } |
491696a8 |
229 | if ( @fwrite($file, $value) === FALSE ) { |
dabef6fd |
230 | logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp')); |
231 | exit; |
232 | } |
3499f99f |
233 | fclose($file); |
1d44a937 |
234 | if (! @copy($filename . '.tmp',$filename) ) { |
235 | 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') ); |
236 | exit; |
237 | } |
dabef6fd |
238 | @unlink($filename . '.tmp'); |
4623cba9 |
239 | @chmod($filename, 0600); |
dabef6fd |
240 | |
3499f99f |
241 | } |
242 | |
243 | /** |
244 | * Get the signature. |
245 | */ |
01265fba |
246 | function getSig($data_dir, $username, $number) { |
01265fba |
247 | $filename = getHashedFile($username, $data_dir, "$username.si$number"); |
3499f99f |
248 | $sig = ''; |
249 | if (file_exists($filename)) { |
35b5fa13 |
250 | /* Open the file, or else display an error to the user. */ |
251 | if(!$file = @fopen($filename, 'r')) |
252 | { |
35b5fa13 |
253 | logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) ); |
254 | exit; |
255 | } |
3499f99f |
256 | while (!feof($file)) { |
257 | $sig .= fgets($file, 1024); |
258 | } |
259 | fclose($file); |
260 | } |
261 | return $sig; |
262 | } |