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