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