74c7848f237f7601d0cd7e6d4597d29ff07abe43
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains functions for manipulating user preferences
14 global $prefs_are_cached, $prefs_cache;
16 if ( !session_is_registered('prefs_are_cached') ||
17 !isset( $prefs_cache) ||
18 !is_array( $prefs_cache) ||
19 substr( phpversion(), 0, 3 ) == '4.1' ) {
20 $prefs_are_cached = false;
21 $prefs_cache = array();
25 * Check the preferences into the session cache.
27 function cachePrefValues($data_dir, $username) {
28 global $prefs_are_cached, $prefs_cache;
30 if ($prefs_are_cached) {
34 session_unregister('prefs_cache');
35 session_unregister('prefs_are_cached');
37 /* Calculate the filename for the user's preference file */
38 $filename = getHashedFile($username, $data_dir, "$username.pref");
40 /* A call to checkForPrefs here should take eliminate the need for */
41 /* this to be called throughout the rest of the SquirrelMail code. */
42 checkForPrefs($data_dir, $username, $filename);
44 /* Make sure that the preference file now DOES exist. */
45 if (!file_exists($filename)) {
46 echo sprintf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) . "<br>\n";
50 $file = fopen($filename, 'r');
52 /* Read in the preferences. */
54 while (! feof($file)) {
55 $pref = trim(fgets($file, 1024));
56 $equalsAt = strpos($pref, '=');
58 $key = substr($pref, 0, $equalsAt);
59 $value = substr($pref, $equalsAt +
1);
60 if (substr($key, 0, 9) == 'highlight') {
61 $key = 'highlight' . $highlight_num;
66 $prefs_cache[$key] = $value;
72 $prefs_are_cached = true;
74 session_register('prefs_cache');
75 session_register('prefs_are_cached');
79 * Return the value for the prefernce given by $string.
81 function getPref($data_dir, $username, $string, $default = '') {
85 cachePrefValues($data_dir, $username);
87 if (isset($prefs_cache[$string])) {
88 $result = $prefs_cache[$string];
97 * Save the preferences for this user.
99 function savePrefValues($data_dir, $username) {
102 $filename = getHashedFile($username, $data_dir, "$username.pref");
104 $file = fopen($filename, 'w');
105 foreach ($prefs_cache as $Key => $Value) {
107 fwrite($file, $Key . '=' . $Value . "\n");
111 chmod($filename, 0600);
115 * Remove a preference for the current user.
117 function removePref($data_dir, $username, $string) {
120 cachePrefValues($data_dir, $username);
122 if (isset($prefs_cache[$string])) {
123 unset($prefs_cache[$string]);
126 savePrefValues($data_dir, $username);
130 * Set a there preference $string to $value.
132 function setPref($data_dir, $username, $string, $value) {
135 cachePrefValues($data_dir, $username);
136 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
141 removePref($data_dir, $username, $string);
145 $prefs_cache[$string] = $value;
146 savePrefValues($data_dir, $username);
150 * Check for a preferences file. If one can not be found, create it.
152 function checkForPrefs($data_dir, $username, $filename = '') {
153 /* First, make sure we have the filename. */
154 if ($filename == '') {
155 $filename = getHashedFile($username, $data_dir, '$username.pref');
158 /* Then, check if the file exists. */
159 if (!@file_exists
($filename) ) {
160 /* First, check the $data_dir for the default preference file. */
161 $default_pref = $data_dir . 'default_pref';
163 /* If it is not there, check the internal data directory. */
164 if (!@file_exists
($default_pref)) {
165 $default_pref = '../data/default_pref';
168 /* Otherwise, report an error. */
169 if (!file_exists($default_pref)) {
170 echo _("Error opening ") . $default_pref . "<br>\n";
171 echo _("Default preference file not found!") . "<br>\n";
172 echo _("Please contact your system administrator and report this error.") . "<br>\n";
174 } else if (!@copy
($default_pref, $filename)) {
175 echo _("Error opening ") . $default_pref . '<br>';
176 echo _("Could not create initial preference file!") . "<br>\n";
177 echo _("Please contact your system administrator and report this error.") . "<br>\n";
184 * Write the User Signature.
186 function setSig($data_dir, $username, $value) {
187 $filename = getHashedFile($username, $data_dir, "$username.sig");
188 $file = fopen($filename, 'w');
189 fwrite($file, $value);
196 function getSig($data_dir, $username) {
197 #$filename = $data_dir . $username . '.sig';
198 $filename = getHashedFile($username, $data_dir, "$username.sig");
200 if (file_exists($filename)) {
201 $file = fopen($filename, 'r');
202 while (!feof($file)) {
203 $sig .= fgets($file, 1024);
210 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
211 global $dir_hash_level;
213 /* Remove trailing slash from $dir if found */
214 if (substr($dir, -1) == '/') {
215 $dir = substr($dir, 0, strlen($dir) - 1);
218 /* Compute the hash for this user and extract the hash directories. */
219 $hash_dirs = computeHashDirs($username);
221 /* First, get and make sure the full hash directory exists. */
222 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
224 /* Set the value of our real data file. */
225 $result = "$real_hash_dir/$datafile";
227 /* Check for this file in the real hash directory. */
228 if ($hash_search && !@file_exists
($result)) {
229 /* First check the base directory, the most common location. */
230 if (@file_exists
("$dir/$datafile")) {
231 rename("$dir/$datafile", $result);
233 /* Then check the full range of possible hash directories. */
235 $check_hash_dir = $dir;
236 for ($h = 0; $h < 4; ++
$h) {
237 $check_hash_dir .= '/' . $hash_dirs[$h];
238 if (@is_readable
("$check_hash_dir/$datafile")) {
239 rename("$check_hash_dir/$datafile", $result);
246 /* Return the full hashed datafile path. */
250 function getHashedDir($username, $dir, $hash_dirs = '') {
251 global $dir_hash_level;
253 /* Remove trailing slash from $dir if found */
254 if (substr($dir, -1) == '/') {
255 $dir = substr($dir, 0, strlen($dir) - 1);
258 /* If necessary, populate the hash dir variable. */
259 if ($hash_dirs == '') {
260 $hash_dirs = computeHashDirs($username);
263 /* Make sure the full hash directory exists. */
264 $real_hash_dir = $dir;
265 for ($h = 0; $h < $dir_hash_level; ++
$h) {
266 $real_hash_dir .= '/' . $hash_dirs[$h];
267 if (!@is_dir
($real_hash_dir)) {
268 if (!@mkdir
($real_hash_dir, 0770)) {
269 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' .
270 _("Could not create hashed directory structure!") . "<br>\n" .
271 _("Please contact your system administrator and report this error.") . "<br>\n";
277 /* And return that directory. */
278 return ($real_hash_dir);
281 function computeHashDirs($username) {
282 /* Compute the hash for this user and extract the hash directories. */
283 $hash = base_convert(crc32($username), 10, 16);
284 $hash_dirs = array();
285 for ($h = 0; $h < 4; ++
$h) {
286 $hash_dirs[] = substr($hash, $h, 1);
289 /* Return our array of hash directories. */