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