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