84aae54d86a46b2fac5dad57d1a532afc5382ba5
[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
17 /**
18 * Check the preferences into the session cache.
19 * @param string $data_dir
20 * @param string $username
21 * @since 1.1.3
22 */
23 function cachePrefValues($data_dir, $username) {
24 global $prefs_are_cached, $prefs_cache;
25
26 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
27 if ( isset($prefs_are_cached) && $prefs_are_cached) {
28 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
29 // sm_print_r($prefs_cache);
30 // exit;
31 return;
32 }
33
34 sqsession_unregister('prefs_cache');
35 sqsession_unregister('prefs_are_cached');
36
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)) {
46 logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
47 exit;
48 }
49
50 /* Open the file, or else display an error to the user. */
51 if(!$file = @fopen($filename, 'r'))
52 {
53 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
54 exit;
55 }
56
57 /* Read in the preferences. */
58 $highlight_num = 0;
59 while (! feof($file)) {
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);
69 $equalsAt = strpos($pref, '=');
70 if ($equalsAt > 0) {
71 $key = substr($pref, 0, $equalsAt);
72 $value = substr($pref, $equalsAt + 1);
73 /* this is to 'rescue' old-style highlighting rules. */
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 }
83 }
84 fclose($file);
85
86 $prefs_are_cached = TRUE;
87
88 sqsession_register($prefs_cache, 'prefs_cache');
89 sqsession_register($prefs_are_cached, 'prefs_are_cached');
90 }
91
92 /**
93 * Return the value for the preference given by $string.
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
99 */
100 function getPref($data_dir, $username, $string, $default = '') {
101 global $prefs_cache;
102
103 $result = do_hook('get_pref_override', $temp=array(&$username, &$string));
104 if (!$result) {
105 cachePrefValues($data_dir, $username);
106 if (isset($prefs_cache[$string])) {
107 $result = $prefs_cache[$string];
108 } else {
109 $result = do_hook('get_pref', $temp=array(&$username, &$string));
110 if (!$result) {
111 $result = $default;
112 }
113 }
114 }
115 return ($result);
116 }
117
118 /**
119 * Save the preferences for this user.
120 * @param string $data_dir data directory
121 * @param string $username user name
122 * @since 1.1.3
123 */
124 function savePrefValues($data_dir, $username) {
125 global $prefs_cache;
126
127 $filename = getHashedFile($username, $data_dir, "$username.pref");
128
129 /* Open the file for writing, or else display an error to the user. */
130 if(!$file = @fopen($filename.'.tmp', 'w'))
131 {
132 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
133 exit;
134 }
135 foreach ($prefs_cache as $Key => $Value) {
136 if (isset($Value)) {
137 if ( sq_fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) {
138 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
139 exit;
140 }
141 }
142 }
143 fclose($file);
144 if (! @copy($filename . '.tmp',$filename) ) {
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 }
148 @unlink($filename . '.tmp');
149 @chmod($filename, 0600);
150 sqsession_register($prefs_cache , 'prefs_cache');
151 }
152
153 /**
154 * Remove a preference for the current user.
155 * @param string $data_dir data directory
156 * @param string $username user name
157 * @param string $string preference name
158 */
159 function removePref($data_dir, $username, $string) {
160 global $prefs_cache;
161
162 cachePrefValues($data_dir, $username);
163
164 if (isset($prefs_cache[$string])) {
165 unset($prefs_cache[$string]);
166 }
167
168 savePrefValues($data_dir, $username);
169 }
170
171 /**
172 * Set a there preference $string to $value.
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
177 */
178 function setPref($data_dir, $username, $string, $value) {
179 global $prefs_cache;
180
181 cachePrefValues($data_dir, $username);
182 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
183 return;
184 }
185
186 if ($value === '') {
187 removePref($data_dir, $username, $string);
188 return;
189 }
190
191 $prefs_cache[$string] = $value;
192 savePrefValues($data_dir, $username);
193 }
194
195 /**
196 * Check for a preferences file. If one can not be found, create it.
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.
201 */
202 function checkForPrefs($data_dir, $username, $filename = '') {
203 /* First, make sure we have the filename. */
204 if ($filename == '') {
205 $filename = getHashedFile($username, $data_dir, "$username.pref");
206 }
207
208 /* Then, check if the file exists. */
209 if (!@file_exists($filename) ) {
210
211 /* If it does not exist, check for default_prefs */
212
213 /* First, check legacy locations: data dir */
214 if(substr($data_dir,-1) != '/') {
215 $data_dir .= '/';
216 }
217 $default_pref = $data_dir . 'default_pref';
218
219 /* or legacy location: internal data dir */
220 if (!@file_exists($default_pref)) {
221 $default_pref = SM_PATH . 'data/default_pref';
222 }
223
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 }
229
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 ) {
237 $uid = 'httpd';
238 if (function_exists('posix_getuid')){
239 $user_data = posix_getpwuid(posix_getuid());
240 $uid = $user_data['name'];
241 }
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";
246 logout_error( $errString, $errTitle );
247 exit;
248 }
249 }
250 }
251
252 /**
253 * Write the User Signature.
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
259 */
260 function setSig($data_dir, $username, $number, $value) {
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 }
266 $filename = getHashedFile($username, $data_dir, "$username.si$number");
267 /* Open the file for writing, or else display an error to the user. */
268 if(!$file = @fopen("$filename.tmp", 'w')) {
269 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
270 exit;
271 }
272 if ( sq_fwrite($file, $value) === FALSE ) {
273 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
274 exit;
275 }
276 fclose($file);
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 }
281 @unlink($filename . '.tmp');
282 @chmod($filename, 0600);
283
284 }
285
286 /**
287 * Get the signature.
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
292 */
293 function getSig($data_dir, $username, $number) {
294 $filename = getHashedFile($username, $data_dir, "$username.si$number");
295 $sig = '';
296 if (file_exists($filename)) {
297 /* Open the file, or else display an error to the user. */
298 if(!$file = @fopen($filename, 'r'))
299 {
300 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
301 exit;
302 }
303 while (!feof($file)) {
304 $sig .= fgets($file, 1024);
305 }
306 fclose($file);
307 }
308 return $sig;
309 }
310
311 // vim: et ts=4