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