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