74c7848f237f7601d0cd7e6d4597d29ff07abe43
[squirrelmail.git] / functions / prefs.php
1 <?php
2
3 /**
4 * 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
10 *
11 * $Id$
12 */
13
14 global $prefs_are_cached, $prefs_cache;
15
16 if ( !session_is_registered('prefs_are_cached') ||
17 !isset( $prefs_cache) ||
18 !is_array( $prefs_cache) ||
19 substr( phpversion(), 0, 3 ) == '4.1' ) {
20 $prefs_are_cached = false;
21 $prefs_cache = array();
22 }
23
24 /**
25 * Check the preferences into the session cache.
26 */
27 function cachePrefValues($data_dir, $username) {
28 global $prefs_are_cached, $prefs_cache;
29
30 if ($prefs_are_cached) {
31 return;
32 }
33
34 session_unregister('prefs_cache');
35 session_unregister('prefs_are_cached');
36
37 /* Calculate the filename for the user's preference file */
38 $filename = getHashedFile($username, $data_dir, "$username.pref");
39
40 /* A call to checkForPrefs here should take eliminate the need for */
41 /* this to be called throughout the rest of the SquirrelMail code. */
42 checkForPrefs($data_dir, $username, $filename);
43
44 /* Make sure that the preference file now DOES exist. */
45 if (!file_exists($filename)) {
46 echo sprintf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) . "<br>\n";
47 exit;
48 }
49
50 $file = fopen($filename, 'r');
51
52 /* Read in the preferences. */
53 $highlight_num = 0;
54 while (! feof($file)) {
55 $pref = trim(fgets($file, 1024));
56 $equalsAt = strpos($pref, '=');
57 if ($equalsAt > 0) {
58 $key = substr($pref, 0, $equalsAt);
59 $value = substr($pref, $equalsAt + 1);
60 if (substr($key, 0, 9) == 'highlight') {
61 $key = 'highlight' . $highlight_num;
62 $highlight_num ++;
63 }
64
65 if ($value != '') {
66 $prefs_cache[$key] = $value;
67 }
68 }
69 }
70 fclose($file);
71
72 $prefs_are_cached = true;
73
74 session_register('prefs_cache');
75 session_register('prefs_are_cached');
76 }
77
78 /**
79 * Return the value for the prefernce given by $string.
80 */
81 function getPref($data_dir, $username, $string, $default = '') {
82 global $prefs_cache;
83 $result = '';
84
85 cachePrefValues($data_dir, $username);
86
87 if (isset($prefs_cache[$string])) {
88 $result = $prefs_cache[$string];
89 } else {
90 $result = $default;
91 }
92
93 return ($result);
94 }
95
96 /**
97 * Save the preferences for this user.
98 */
99 function savePrefValues($data_dir, $username) {
100 global $prefs_cache;
101
102 $filename = getHashedFile($username, $data_dir, "$username.pref");
103
104 $file = fopen($filename, 'w');
105 foreach ($prefs_cache as $Key => $Value) {
106 if (isset($Value)) {
107 fwrite($file, $Key . '=' . $Value . "\n");
108 }
109 }
110 fclose($file);
111 chmod($filename, 0600);
112 }
113
114 /**
115 * Remove a preference for the current user.
116 */
117 function removePref($data_dir, $username, $string) {
118 global $prefs_cache;
119
120 cachePrefValues($data_dir, $username);
121
122 if (isset($prefs_cache[$string])) {
123 unset($prefs_cache[$string]);
124 }
125
126 savePrefValues($data_dir, $username);
127 }
128
129 /**
130 * Set a there preference $string to $value.
131 */
132 function setPref($data_dir, $username, $string, $value) {
133 global $prefs_cache;
134
135 cachePrefValues($data_dir, $username);
136 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
137 return;
138 }
139
140 if ($value === '') {
141 removePref($data_dir, $username, $string);
142 return;
143 }
144
145 $prefs_cache[$string] = $value;
146 savePrefValues($data_dir, $username);
147 }
148
149 /**
150 * Check for a preferences file. If one can not be found, create it.
151 */
152 function checkForPrefs($data_dir, $username, $filename = '') {
153 /* First, make sure we have the filename. */
154 if ($filename == '') {
155 $filename = getHashedFile($username, $data_dir, '$username.pref');
156 }
157
158 /* Then, check if the file exists. */
159 if (!@file_exists($filename) ) {
160 /* First, check the $data_dir for the default preference file. */
161 $default_pref = $data_dir . 'default_pref';
162
163 /* If it is not there, check the internal data directory. */
164 if (!@file_exists($default_pref)) {
165 $default_pref = '../data/default_pref';
166 }
167
168 /* Otherwise, report an error. */
169 if (!file_exists($default_pref)) {
170 echo _("Error opening ") . $default_pref . "<br>\n";
171 echo _("Default preference file not found!") . "<br>\n";
172 echo _("Please contact your system administrator and report this error.") . "<br>\n";
173 exit;
174 } else if (!@copy($default_pref, $filename)) {
175 echo _("Error opening ") . $default_pref . '<br>';
176 echo _("Could not create initial preference file!") . "<br>\n";
177 echo _("Please contact your system administrator and report this error.") . "<br>\n";
178 exit;
179 }
180 }
181 }
182
183 /**
184 * Write the User Signature.
185 */
186 function setSig($data_dir, $username, $value) {
187 $filename = getHashedFile($username, $data_dir, "$username.sig");
188 $file = fopen($filename, 'w');
189 fwrite($file, $value);
190 fclose($file);
191 }
192
193 /**
194 * Get the signature.
195 */
196 function getSig($data_dir, $username) {
197 #$filename = $data_dir . $username . '.sig';
198 $filename = getHashedFile($username, $data_dir, "$username.sig");
199 $sig = '';
200 if (file_exists($filename)) {
201 $file = fopen($filename, 'r');
202 while (!feof($file)) {
203 $sig .= fgets($file, 1024);
204 }
205 fclose($file);
206 }
207 return $sig;
208 }
209
210 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
211 global $dir_hash_level;
212
213 /* Remove trailing slash from $dir if found */
214 if (substr($dir, -1) == '/') {
215 $dir = substr($dir, 0, strlen($dir) - 1);
216 }
217
218 /* Compute the hash for this user and extract the hash directories. */
219 $hash_dirs = computeHashDirs($username);
220
221 /* First, get and make sure the full hash directory exists. */
222 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
223
224 /* Set the value of our real data file. */
225 $result = "$real_hash_dir/$datafile";
226
227 /* Check for this file in the real hash directory. */
228 if ($hash_search && !@file_exists($result)) {
229 /* First check the base directory, the most common location. */
230 if (@file_exists("$dir/$datafile")) {
231 rename("$dir/$datafile", $result);
232
233 /* Then check the full range of possible hash directories. */
234 } else {
235 $check_hash_dir = $dir;
236 for ($h = 0; $h < 4; ++$h) {
237 $check_hash_dir .= '/' . $hash_dirs[$h];
238 if (@is_readable("$check_hash_dir/$datafile")) {
239 rename("$check_hash_dir/$datafile", $result);
240 break;
241 }
242 }
243 }
244 }
245
246 /* Return the full hashed datafile path. */
247 return ($result);
248 }
249
250 function getHashedDir($username, $dir, $hash_dirs = '') {
251 global $dir_hash_level;
252
253 /* Remove trailing slash from $dir if found */
254 if (substr($dir, -1) == '/') {
255 $dir = substr($dir, 0, strlen($dir) - 1);
256 }
257
258 /* If necessary, populate the hash dir variable. */
259 if ($hash_dirs == '') {
260 $hash_dirs = computeHashDirs($username);
261 }
262
263 /* Make sure the full hash directory exists. */
264 $real_hash_dir = $dir;
265 for ($h = 0; $h < $dir_hash_level; ++$h) {
266 $real_hash_dir .= '/' . $hash_dirs[$h];
267 if (!@is_dir($real_hash_dir)) {
268 if (!@mkdir($real_hash_dir, 0770)) {
269 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' .
270 _("Could not create hashed directory structure!") . "<br>\n" .
271 _("Please contact your system administrator and report this error.") . "<br>\n";
272 exit;
273 }
274 }
275 }
276
277 /* And return that directory. */
278 return ($real_hash_dir);
279 }
280
281 function computeHashDirs($username) {
282 /* Compute the hash for this user and extract the hash directories. */
283 $hash = base_convert(crc32($username), 10, 16);
284 $hash_dirs = array();
285 for ($h = 0; $h < 4; ++ $h) {
286 $hash_dirs[] = substr($hash, $h, 1);
287 }
288
289 /* Return our array of hash directories. */
290 return ($hash_dirs);
291 }
292
293 ?>