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