We're living in 2004 now... perl is your friend for these kinds of things :)
[squirrelmail.git] / functions / file_prefs.php
1 <?php
2
3 /**
4 * file_prefs.php
5 *
6 * Copyright (c) 1999-2004 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 * @package squirrelmail
13 */
14
15 /** include this for error messages */
16 include_once(SM_PATH . 'functions/display_messages.php');
17
18 /**
19 * Check the preferences into the session cache.
20 */
21 function cachePrefValues($data_dir, $username) {
22 global $prefs_are_cached, $prefs_cache;
23
24 if ( isset($prefs_are_cached) && $prefs_are_cached) {
25 return;
26 }
27
28 sqsession_unregister('prefs_cache');
29 sqsession_unregister('prefs_are_cached');
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)) {
40 logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
41 exit;
42 }
43
44 /* Open the file, or else display an error to the user. */
45 if(!$file = @fopen($filename, 'r'))
46 {
47 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
48 exit;
49 }
50
51 /* Read in the preferences. */
52 $highlight_num = 0;
53 while (! feof($file)) {
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);
63 $equalsAt = strpos($pref, '=');
64 if ($equalsAt > 0) {
65 $key = substr($pref, 0, $equalsAt);
66 $value = substr($pref, $equalsAt + 1);
67 /* this is to 'rescue' old-style highlighting rules. */
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 }
77 }
78 fclose($file);
79
80 $prefs_are_cached = TRUE;
81
82 sqsession_register($prefs_cache, 'prefs_cache');
83 sqsession_register($prefs_are_cached, 'prefs_are_cached');
84 }
85
86 /**
87 * Return the value for the preference given by $string.
88 */
89 function getPref($data_dir, $username, $string, $default = '') {
90 global $prefs_cache;
91
92 $result = do_hook_function('get_pref_override',array($username,$string));
93 if (!$result) {
94 cachePrefValues($data_dir, $username);
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 }
103 }
104 return ($result);
105 }
106
107 /**
108 * Save the preferences for this user.
109 */
110 function savePrefValues($data_dir, $username) {
111 global $prefs_cache;
112
113 $filename = getHashedFile($username, $data_dir, "$username.pref");
114
115 /* Open the file for writing, or else display an error to the user. */
116 if(!$file = @fopen($filename.'.tmp', 'w'))
117 {
118 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
119 exit;
120 }
121 foreach ($prefs_cache as $Key => $Value) {
122 if (isset($Value)) {
123 if ( @fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) {
124 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
125 exit;
126 }
127 }
128 }
129 fclose($file);
130 if (! @copy($filename . '.tmp',$filename) ) {
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 }
134 @unlink($filename . '.tmp');
135 @chmod($filename, 0600);
136 sqsession_register($prefs_cache , 'prefs_cache');
137 }
138
139 /**
140 * Remove a preference for the current user.
141 */
142 function removePref($data_dir, $username, $string) {
143 global $prefs_cache;
144
145 cachePrefValues($data_dir, $username);
146
147 if (isset($prefs_cache[$string])) {
148 unset($prefs_cache[$string]);
149 }
150
151 savePrefValues($data_dir, $username);
152 }
153
154 /**
155 * Set a there preference $string to $value.
156 */
157 function setPref($data_dir, $username, $string, $value) {
158 global $prefs_cache;
159
160 cachePrefValues($data_dir, $username);
161 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
162 return;
163 }
164
165 if ($value === '') {
166 removePref($data_dir, $username, $string);
167 return;
168 }
169
170 $prefs_cache[$string] = $value;
171 savePrefValues($data_dir, $username);
172 }
173
174 /**
175 * Check for a preferences file. If one can not be found, create it.
176 */
177 function checkForPrefs($data_dir, $username, $filename = '') {
178 /* First, make sure we have the filename. */
179 if ($filename == '') {
180 $filename = getHashedFile($username, $data_dir, "$username.pref");
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. */
186 if(substr($data_dir,-1) != '/') {
187 $data_dir .= '/';
188 }
189 $default_pref = $data_dir . 'default_pref';
190
191 /* If it is not there, check the internal data directory. */
192 if (!@file_exists($default_pref)) {
193 $default_pref = SM_PATH . 'data/default_pref';
194 }
195
196 /* Otherwise, report an error. */
197 $errTitle = sprintf( _("Error opening %s"), $default_pref );
198 if (!is_readable($default_pref)) {
199 $errString = $errTitle . "<br>\n" .
200 _("Default preference file not found or not readable!") . "<br>\n" .
201 _("Please contact your system administrator and report this error.") . "<br>\n";
202 logout_error( $errString, $errTitle );
203 exit;
204 } else if (!@copy($default_pref, $filename)) {
205 $uid = 'httpd';
206 if (function_exists('posix_getuid')){
207 $user_data = posix_getpwuid(posix_getuid());
208 $uid = $user_data['name'];
209 }
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";
214 logout_error( $errString, $errTitle );
215 exit;
216 }
217 }
218 }
219
220 /**
221 * Write the User Signature.
222 */
223 function setSig($data_dir, $username, $number, $value) {
224 $filename = getHashedFile($username, $data_dir, "$username.si$number");
225 /* Open the file for writing, or else display an error to the user. */
226 if(!$file = @fopen("$filename.tmp", 'w')) {
227 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
228 exit;
229 }
230 if ( @fwrite($file, $value) === FALSE ) {
231 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
232 exit;
233 }
234 fclose($file);
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 }
239 @unlink($filename . '.tmp');
240 @chmod($filename, 0600);
241
242 }
243
244 /**
245 * Get the signature.
246 */
247 function getSig($data_dir, $username, $number) {
248 $filename = getHashedFile($username, $data_dir, "$username.si$number");
249 $sig = '';
250 if (file_exists($filename)) {
251 /* Open the file, or else display an error to the user. */
252 if(!$file = @fopen($filename, 'r'))
253 {
254 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
255 exit;
256 }
257 while (!feof($file)) {
258 $sig .= fgets($file, 1024);
259 }
260 fclose($file);
261 }
262 return $sig;
263 }