35e7860717d9859ca3f8129efd0adfb6e9b6b9e8
[squirrelmail.git] / functions / prefs.php
1 <?php
2
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
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 $filename = getHashedFile($username, $data_dir, "$username.pref");
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');
61
62 $prefs_are_cached = true;
63 session_unregister('prefs_are_cached');
64 session_register('prefs_are_cached');
65 }
66
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;
90
91 $filename = getHashedFile($username, $data_dir, "$username.pref");
92
93 $file = fopen($filename, 'w');
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 }
116
117 /**
118 * Set a there preference $string to $value.
119 */
120 function setPref($data_dir, $username, $string, $value) {
121 global $prefs_cache;
122
123 cachePrefValues($data_dir, $username);
124 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
125 return;
126 }
127
128 if ($value === '') {
129 removePref($data_dir, $username, $string);
130 return;
131 }
132
133 $prefs_cache[$string] = $value;
134 savePrefValues($data_dir, $username);
135 }
136
137 /**
138 * Check for a preferences file. If one can not be found, create it.
139 */
140 function checkForPrefs($data_dir, $username) {
141 $filename = getHashedFile($username, $data_dir, "$username.pref");
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) {
154 $filename = getHashedFile($username, $data_dir, "$username.sig");
155 $file = fopen($filename, 'w');
156 fwrite($file, $value);
157 fclose($file);
158 }
159
160 /**
161 * Get the signature.
162 */
163 function getSig($data_dir, $username) {
164 #$filename = $data_dir . $username . '.sig';
165 $filename = getHashedFile($username, $data_dir, "$username.sig");
166 $sig = '';
167 if (file_exists($filename)) {
168 $file = fopen($filename, 'r');
169 while (!feof($file)) {
170 $sig .= fgets($file, 1024);
171 }
172 fclose($file);
173 }
174 return $sig;
175 }
176
177 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
178 global $dir_hash_level;
179
180 /* Compute the hash for this user and extract the hash directories. */
181 $hash_dirs = computeHashDirs($username);
182
183 /* First, get and make sure the full hash directory exists. */
184 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
185
186 /* Set the value of our real data file. */
187 $result = "$real_hash_dir/$datafile";
188
189 /* Check for this file in the real hash directory. */
190 if ($hash_search && !file_exists($result)) {
191 /* First check the base directory, the most common location. */
192 if (file_exists("$dir/$datafile")) {
193 rename("$dir/$datafile", $result);
194
195 /* Then check the full range of possible hash directories. */
196 } else {
197 $check_hash_dir = $dir;
198 for ($h = 0; $h < 4; ++$h) {
199 $check_hash_dir .= '/' . $hash_dirs[$h];
200 if (is_readable("$check_hash_dir/$datafile")) {
201 rename("$check_hash_dir/$datafile", $result);
202 break;
203 }
204 }
205 }
206 }
207
208 /* Return the full hashed datafile path. */
209 return ($result);
210 }
211
212 function getHashedDir($username, $dir, $hash_dirs = '') {
213 global $dir_hash_level;
214
215 /* If necessary, populate the hash dir variable. */
216 if ($hash_dirs == '') {
217 $hash_dirs = computeHashDirs($username);
218 }
219
220 /* Make sure the full hash directory exists. */
221 $real_hash_dir = $dir;
222 for ($h = 0; $h < $dir_hash_level; ++$h) {
223 $real_hash_dir .= '/' . $hash_dirs[$h];
224 if (!is_dir($real_hash_dir)) {
225 mkdir($real_hash_dir, 0770);
226 }
227 }
228
229 /* And return that directory. */
230 return ($real_hash_dir);
231 }
232
233 function computeHashDirs($username) {
234 /* Compute the hash for this user and extract the hash directories. */
235 $hash = base_convert(crc32($username), 10, 16);
236 $hash_dirs = array();
237 for ($h = 0; $h < 4; ++ $h) {
238 $hash_dirs[] = substr($hash, $h, 1);
239 }
240
241 /* Return our array of hash directories. */
242 return ($hash_dirs);
243 }
244
245 ?>