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