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