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