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