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