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