Reverting strings (didn't work out in all languages)
[squirrelmail.git] / functions / file_prefs.php
CommitLineData
3499f99f 1<?php
2
3/**
4 * file_prefs.php
5 *
6c84ba1e 6 * Copyright (c) 1999-2005 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 *
31841a9e 11 * @version $Id$
d6c32258 12 * @package squirrelmail
ace4c62c 13 * @subpackage prefs
14 * @since 1.2.5
3499f99f 15 */
16
ace4c62c 17/** @ignore */
18if (! defined('SM_PATH')) define('SM_PATH','../');
19
d6c32258 20/** include this for error messages */
b94bfe13 21include_once(SM_PATH . 'functions/display_messages.php');
22
3499f99f 23/**
24 * Check the preferences into the session cache.
ace4c62c 25 * @param string $data_dir
26 * @param string $username
27 * @since 1.1.3
3499f99f 28 */
29function cachePrefValues($data_dir, $username) {
30 global $prefs_are_cached, $prefs_cache;
324ac3c5 31
37d5278d 32 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
3499f99f 33 if ( isset($prefs_are_cached) && $prefs_are_cached) {
0d8a462c 34 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
2b2967d4 35 return;
3499f99f 36 }
324ac3c5 37
9eb0fbd4 38 sqsession_unregister('prefs_cache');
39 sqsession_unregister('prefs_are_cached');
324ac3c5 40
3499f99f 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)) {
9be8198d 50 logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
3499f99f 51 exit;
52 }
53
35b5fa13 54 /* Open the file, or else display an error to the user. */
55 if(!$file = @fopen($filename, 'r'))
56 {
35b5fa13 57 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
58 exit;
59 }
3499f99f 60
61 /* Read in the preferences. */
62 $highlight_num = 0;
63 while (! feof($file)) {
710a8254 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);
3499f99f 73 $equalsAt = strpos($pref, '=');
74 if ($equalsAt > 0) {
75 $key = substr($pref, 0, $equalsAt);
76 $value = substr($pref, $equalsAt + 1);
1c159927 77 /* this is to 'rescue' old-style highlighting rules. */
3499f99f 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 }
491696a8 87 }
88 fclose($file);
3499f99f 89
90 $prefs_are_cached = TRUE;
91
9eb0fbd4 92 sqsession_register($prefs_cache, 'prefs_cache');
93 sqsession_register($prefs_are_cached, 'prefs_are_cached');
3499f99f 94}
324ac3c5 95
3499f99f 96/**
b94bfe13 97 * Return the value for the preference given by $string.
ace4c62c 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
3499f99f 103 */
104function getPref($data_dir, $username, $string, $default = '') {
105 global $prefs_cache;
397a9d7b 106
be6f5c46 107 $result = do_hook_function('get_pref_override',array($username,$string));
108 if (!$result) {
324ac3c5 109 cachePrefValues($data_dir, $username);
110 if (isset($prefs_cache[$string])) {
0d8a462c 111 $result = $prefs_cache[$string];
324ac3c5 112 } else {
113 $result = do_hook_function('get_pref', array($username,$string));
114 if (!$result) {
115 $result = $default;
116 }
117 }
3499f99f 118 }
3499f99f 119 return ($result);
120}
121
122/**
123 * Save the preferences for this user.
ace4c62c 124 * @param string $data_dir data directory
125 * @param string $username user name
126 * @since 1.1.3
3499f99f 127 */
2b2967d4 128function savePrefValues($data_dir, $username) {
129 global $prefs_cache;
324ac3c5 130
3499f99f 131 $filename = getHashedFile($username, $data_dir, "$username.pref");
132
35b5fa13 133 /* Open the file for writing, or else display an error to the user. */
be6f5c46 134 if(!$file = @fopen($filename.'.tmp', 'w'))
35b5fa13 135 {
be6f5c46 136 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
35b5fa13 137 exit;
138 }
3499f99f 139 foreach ($prefs_cache as $Key => $Value) {
140 if (isset($Value)) {
3ecad5e6 141 if ( sq_fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) {
dabef6fd 142 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
143 exit;
144 }
3499f99f 145 }
146 }
147 fclose($file);
1d44a937 148 if (! @copy($filename . '.tmp',$filename) ) {
b94bfe13 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 }
dabef6fd 152 @unlink($filename . '.tmp');
4623cba9 153 @chmod($filename, 0600);
c4f32485 154 sqsession_register($prefs_cache , 'prefs_cache');
3499f99f 155}
156
157/**
158 * Remove a preference for the current user.
ace4c62c 159 * @param string $data_dir data directory
160 * @param string $username user name
161 * @param string $string preference name
3499f99f 162 */
163function removePref($data_dir, $username, $string) {
164 global $prefs_cache;
165
2b2967d4 166 cachePrefValues($data_dir, $username);
324ac3c5 167
3499f99f 168 if (isset($prefs_cache[$string])) {
169 unset($prefs_cache[$string]);
170 }
324ac3c5 171
2b2967d4 172 savePrefValues($data_dir, $username);
3499f99f 173}
174
175/**
176 * Set a there preference $string to $value.
ace4c62c 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
3499f99f 181 */
182function setPref($data_dir, $username, $string, $value) {
183 global $prefs_cache;
184
2b2967d4 185 cachePrefValues($data_dir, $username);
3499f99f 186 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
187 return;
188 }
189
190 if ($value === '') {
2b2967d4 191 removePref($data_dir, $username, $string);
192 return;
3499f99f 193 }
2b2967d4 194
195 $prefs_cache[$string] = $value;
196 savePrefValues($data_dir, $username);
3499f99f 197}
198
199/**
200 * Check for a preferences file. If one can not be found, create it.
ace4c62c 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.
3499f99f 205 */
206function checkForPrefs($data_dir, $username, $filename = '') {
207 /* First, make sure we have the filename. */
208 if ($filename == '') {
bd1180c1 209 $filename = getHashedFile($username, $data_dir, "$username.pref");
3499f99f 210 }
211
212 /* Then, check if the file exists. */
213 if (!@file_exists($filename) ) {
a04a3c20 214
215 /* If it does not exist, check for default_prefs */
f8a1ed5a 216
a04a3c20 217 /* First, check legacy locations: data dir */
e8c533f6 218 if(substr($data_dir,-1) != '/') {
219 $data_dir .= '/';
220 }
221 $default_pref = $data_dir . 'default_pref';
3499f99f 222
a04a3c20 223 /* or legacy location: internal data dir */
3499f99f 224 if (!@file_exists($default_pref)) {
bd9c880b 225 $default_pref = SM_PATH . 'data/default_pref';
3499f99f 226 }
227
a04a3c20 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 }
f8a1ed5a 233
a04a3c20 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 ) {
305a1012 241 $uid = 'httpd';
242 if (function_exists('posix_getuid')){
243 $user_data = posix_getpwuid(posix_getuid());
244 $uid = $user_data['name'];
245 }
a04a3c20 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";
9be8198d 250 logout_error( $errString, $errTitle );
3499f99f 251 exit;
252 }
253 }
254}
255
256/**
257 * Write the User Signature.
ace4c62c 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
3499f99f 263 */
01265fba 264function setSig($data_dir, $username, $number, $value) {
288df1a0 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 }
01265fba 270 $filename = getHashedFile($username, $data_dir, "$username.si$number");
35b5fa13 271 /* Open the file for writing, or else display an error to the user. */
dabef6fd 272 if(!$file = @fopen("$filename.tmp", 'w')) {
dabef6fd 273 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
35b5fa13 274 exit;
275 }
3ecad5e6 276 if ( sq_fwrite($file, $value) === FALSE ) {
dabef6fd 277 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
278 exit;
279 }
3499f99f 280 fclose($file);
1d44a937 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 }
dabef6fd 285 @unlink($filename . '.tmp');
4623cba9 286 @chmod($filename, 0600);
dabef6fd 287
3499f99f 288}
289
290/**
291 * Get the signature.
ace4c62c 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
3499f99f 296 */
01265fba 297function getSig($data_dir, $username, $number) {
01265fba 298 $filename = getHashedFile($username, $data_dir, "$username.si$number");
3499f99f 299 $sig = '';
300 if (file_exists($filename)) {
35b5fa13 301 /* Open the file, or else display an error to the user. */
302 if(!$file = @fopen($filename, 'r'))
303 {
35b5fa13 304 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
305 exit;
306 }
3499f99f 307 while (!feof($file)) {
308 $sig .= fgets($file, 1024);
309 }
310 fclose($file);
311 }
312 return $sig;
313}
37d5278d 314
315// vim: et ts=4
f8a1ed5a 316?>