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