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 | |
397a9d7b |
82 | $result = do_hook_function('get_pref_override', array($username, $string)); |
3499f99f |
83 | |
397a9d7b |
84 | if ($result == '') { |
85 | cachePrefValues($data_dir, $username); |
86 | |
87 | if (isset($prefs_cache[$string])) { |
88 | $result = $prefs_cache[$string]; |
89 | } else { |
90 | $result = do_hook_function('get_pref', array($username, $string)); |
91 | if ($result == '') { |
92 | $result = $default; |
93 | } |
94 | } |
3499f99f |
95 | } |
96 | |
97 | return ($result); |
98 | } |
99 | |
100 | /** |
101 | * Save the preferences for this user. |
102 | */ |
103 | function savePrefValues($data_dir, $username) { |
104 | global $prefs_cache; |
105 | |
106 | $filename = getHashedFile($username, $data_dir, "$username.pref"); |
107 | |
35b5fa13 |
108 | /* Open the file for writing, or else display an error to the user. */ |
109 | if(!$file = @fopen($filename, 'w')) |
110 | { |
bd9c880b |
111 | include_once(SM_PATH . 'functions/display_messages.php'); |
35b5fa13 |
112 | logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) ); |
113 | exit; |
114 | } |
115 | |
3499f99f |
116 | foreach ($prefs_cache as $Key => $Value) { |
117 | if (isset($Value)) { |
118 | fwrite($file, $Key . '=' . $Value . "\n"); |
119 | } |
120 | } |
121 | fclose($file); |
122 | chmod($filename, 0600); |
123 | } |
124 | |
125 | /** |
126 | * Remove a preference for the current user. |
127 | */ |
128 | function removePref($data_dir, $username, $string) { |
129 | global $prefs_cache; |
130 | |
131 | cachePrefValues($data_dir, $username); |
132 | |
133 | if (isset($prefs_cache[$string])) { |
134 | unset($prefs_cache[$string]); |
135 | } |
136 | |
137 | savePrefValues($data_dir, $username); |
138 | } |
139 | |
140 | /** |
141 | * Set a there preference $string to $value. |
142 | */ |
143 | function setPref($data_dir, $username, $string, $value) { |
144 | global $prefs_cache; |
145 | |
146 | cachePrefValues($data_dir, $username); |
147 | if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) { |
148 | return; |
149 | } |
150 | |
151 | if ($value === '') { |
152 | removePref($data_dir, $username, $string); |
153 | return; |
154 | } |
155 | |
156 | $prefs_cache[$string] = $value; |
157 | savePrefValues($data_dir, $username); |
158 | } |
159 | |
160 | /** |
161 | * Check for a preferences file. If one can not be found, create it. |
162 | */ |
163 | function checkForPrefs($data_dir, $username, $filename = '') { |
164 | /* First, make sure we have the filename. */ |
165 | if ($filename == '') { |
bd1180c1 |
166 | $filename = getHashedFile($username, $data_dir, "$username.pref"); |
3499f99f |
167 | } |
168 | |
169 | /* Then, check if the file exists. */ |
170 | if (!@file_exists($filename) ) { |
171 | /* First, check the $data_dir for the default preference file. */ |
172 | $default_pref = $data_dir . 'default_pref'; |
173 | |
174 | /* If it is not there, check the internal data directory. */ |
175 | if (!@file_exists($default_pref)) { |
bd9c880b |
176 | $default_pref = SM_PATH . 'data/default_pref'; |
3499f99f |
177 | } |
178 | |
179 | /* Otherwise, report an error. */ |
9be8198d |
180 | $errTitle = sprintf( _("Error opening %s"), $default_pref ); |
3499f99f |
181 | if (!file_exists($default_pref)) { |
9be8198d |
182 | $errString = $errTitle . "<br>\n" . |
183 | _("Default preference file not found!") . "<br>\n" . |
184 | _("Please contact your system administrator and report this error.") . "<br>\n"; |
bd9c880b |
185 | include_once(SM_PATH . 'functions/display_messages.php' ); |
9be8198d |
186 | logout_error( $errString, $errTitle ); |
3499f99f |
187 | exit; |
188 | } else if (!@copy($default_pref, $filename)) { |
305a1012 |
189 | $uid = 'httpd'; |
190 | if (function_exists('posix_getuid')){ |
191 | $user_data = posix_getpwuid(posix_getuid()); |
192 | $uid = $user_data['name']; |
193 | } |
9be8198d |
194 | $errString = $errTitle . '<br>' . |
195 | _("Could not create initial preference file!") . "<br>\n" . |
196 | sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) . |
197 | "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n"; |
bd9c880b |
198 | include_once(SM_PATH . 'functions/display_messages.php' ); |
9be8198d |
199 | logout_error( $errString, $errTitle ); |
3499f99f |
200 | exit; |
201 | } |
202 | } |
203 | } |
204 | |
205 | /** |
206 | * Write the User Signature. |
207 | */ |
01265fba |
208 | function setSig($data_dir, $username, $number, $value) { |
209 | $filename = getHashedFile($username, $data_dir, "$username.si$number"); |
35b5fa13 |
210 | /* Open the file for writing, or else display an error to the user. */ |
211 | if(!$file = @fopen($filename, 'w')) |
212 | { |
bd9c880b |
213 | include_once(SM_PATH . '/functions/display_messages.php' ); |
35b5fa13 |
214 | logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) ); |
215 | exit; |
216 | } |
3499f99f |
217 | fwrite($file, $value); |
218 | fclose($file); |
219 | } |
220 | |
221 | /** |
222 | * Get the signature. |
223 | */ |
01265fba |
224 | function getSig($data_dir, $username, $number) { |
01265fba |
225 | $filename = getHashedFile($username, $data_dir, "$username.si$number"); |
3499f99f |
226 | $sig = ''; |
227 | if (file_exists($filename)) { |
35b5fa13 |
228 | /* Open the file, or else display an error to the user. */ |
229 | if(!$file = @fopen($filename, 'r')) |
230 | { |
bd9c880b |
231 | include_once(SM_PATH . 'functions/display_messages.php'); |
35b5fa13 |
232 | logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) ); |
233 | exit; |
234 | } |
3499f99f |
235 | while (!feof($file)) { |
236 | $sig .= fgets($file, 1024); |
237 | } |
238 | fclose($file); |
239 | } |
240 | return $sig; |
241 | } |