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;
15 if (!session_is_registered('prefs_are_cached')) {
16 $prefs_are_cached = false;
17 $prefs_cache = array();
21 * Check the preferences into the session cache.
23 function cachePrefValues($data_dir, $username) {
24 global $prefs_are_cached, $prefs_cache;
26 if ($prefs_are_cached) {
30 /* Calculate the filename for the user's preference file */
31 $filename = getHashedFile($username, $data_dir, "$username.pref");
33 /* A call to checkForPrefs here should take eliminate the need for
34 /* this to be called throughout the rest of the SquirrelMail code.
35 checkForPrefs($data_dir, $username, $filename);
37 /* Make sure that the preference file now DOES exist. */
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);
43 $file = fopen($filename, 'r');
45 /* Read in the preferences. */
47 while (! feof($file)) {
48 $pref = trim(fgets($file, 1024));
49 $equalsAt = strpos($pref, '=');
51 $key = substr($pref, 0, $equalsAt);
52 $value = substr($pref, $equalsAt +
1);
53 if (substr($key, 0, 9) == 'highlight') {
54 $key = 'highlight' . $highlight_num;
59 $prefs_cache[$key] = $value;
65 session_unregister('prefs_cache');
66 session_register('prefs_cache');
68 $prefs_are_cached = true;
69 session_unregister('prefs_are_cached');
70 session_register('prefs_are_cached');
74 * Return the value for the prefernce given by $string.
76 function getPref($data_dir, $username, $string, $default = '') {
80 cachePrefValues($data_dir, $username);
82 if (isset($prefs_cache[$string])) {
83 $result = $prefs_cache[$string];
92 * Save the preferences for this user.
94 function savePrefValues($data_dir, $username) {
97 $filename = getHashedFile($username, $data_dir, "$username.pref");
99 $file = fopen($filename, 'w');
100 foreach ($prefs_cache as $Key => $Value) {
102 fwrite($file, $Key . '=' . $Value . "\n");
109 * Remove a preference for the current user.
111 function removePref($data_dir, $username, $string) {
114 cachePrefValues($data_dir, $username);
116 if (isset($prefs_cache[$string])) {
117 unset($prefs_cache[$string]);
120 savePrefValues($data_dir, $username);
124 * Set a there preference $string to $value.
126 function setPref($data_dir, $username, $string, $value) {
129 cachePrefValues($data_dir, $username);
130 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
135 removePref($data_dir, $username, $string);
139 $prefs_cache[$string] = $value;
140 savePrefValues($data_dir, $username);
144 * Check for a preferences file. If one can not be found, create it.
146 function checkForPrefs($data_dir, $username, $filename = '') {
147 /* First, make sure we have the filename. */
148 if ($filename == '') {
149 $filename = getHashedFile($username, $data_dir, "$username.pref");
152 /* Then, check if the file exists. */
153 if (!file_exists($filename) ) {
154 if (!copy($data_dir . 'default_pref', $filename)) {
155 echo _("Error opening ") . $filename;
162 * Write the User Signature.
164 function setSig($data_dir, $username, $value) {
165 $filename = getHashedFile($username, $data_dir, "$username.sig");
166 $file = fopen($filename, 'w');
167 fwrite($file, $value);
174 function getSig($data_dir, $username) {
175 #$filename = $data_dir . $username . '.sig';
176 $filename = getHashedFile($username, $data_dir, "$username.sig");
178 if (file_exists($filename)) {
179 $file = fopen($filename, 'r');
180 while (!feof($file)) {
181 $sig .= fgets($file, 1024);
188 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
189 global $dir_hash_level;
191 /* Remove trailing slash from $dir if found */
192 if (substr($dir, -1) == '/') {
193 $dir = substr($dir, 0, strlen($dir) - 1);
196 /* Compute the hash for this user and extract the hash directories. */
197 $hash_dirs = computeHashDirs($username);
199 /* First, get and make sure the full hash directory exists. */
200 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
202 /* Set the value of our real data file. */
203 $result = "$real_hash_dir/$datafile";
205 /* Check for this file in the real hash directory. */
206 if ($hash_search && !file_exists($result)) {
207 /* First check the base directory, the most common location. */
208 if (file_exists("$dir/$datafile")) {
209 rename("$dir/$datafile", $result);
211 /* Then check the full range of possible hash directories. */
213 $check_hash_dir = $dir;
214 for ($h = 0; $h < 4; ++
$h) {
215 $check_hash_dir .= '/' . $hash_dirs[$h];
216 if (@is_readable
("$check_hash_dir/$datafile")) {
217 rename("$check_hash_dir/$datafile", $result);
224 /* Return the full hashed datafile path. */
228 function getHashedDir($username, $dir, $hash_dirs = '') {
229 global $dir_hash_level;
231 /* If necessary, populate the hash dir variable. */
232 if ($hash_dirs == '') {
233 $hash_dirs = computeHashDirs($username);
236 /* Make sure the full hash directory exists. */
237 $real_hash_dir = $dir;
238 for ($h = 0; $h < $dir_hash_level; ++
$h) {
239 $real_hash_dir .= '/' . $hash_dirs[$h];
240 if (!is_dir($real_hash_dir)) {
241 mkdir($real_hash_dir, 0770);
245 /* And return that directory. */
246 return ($real_hash_dir);
249 function computeHashDirs($username) {
250 /* Compute the hash for this user and extract the hash directories. */
251 $hash = base_convert(crc32($username), 10, 16);
252 $hash_dirs = array();
253 for ($h = 0; $h < 4; ++
$h) {
254 $hash_dirs[] = substr($hash, $h, 1);
257 /* Return our array of hash directories. */