6 * Copyright (c) 1999-2002 The SquirrelMail Development 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 /* 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);
34 /* Calculate the filename for the user's preference file */
35 $filename = getHashedFile($username, $data_dir, "$username.pref");
37 if (!file_exists($filename)) {
38 printf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename);
42 $file = fopen($filename, 'r');
44 /* Read in the preferences. */
46 while (! feof($file)) {
47 $pref = trim(fgets($file, 1024));
48 $equalsAt = strpos($pref, '=');
50 $key = substr($pref, 0, $equalsAt);
51 $value = substr($pref, $equalsAt +
1);
52 if (substr($key, 0, 9) == 'highlight') {
53 $key = 'highlight' . $highlight_num;
58 $prefs_cache[$key] = $value;
64 session_unregister('prefs_cache');
65 session_register('prefs_cache');
67 $prefs_are_cached = true;
68 session_unregister('prefs_are_cached');
69 session_register('prefs_are_cached');
73 * Return the value for the prefernce given by $string.
75 function getPref($data_dir, $username, $string, $default = '') {
79 cachePrefValues($data_dir, $username);
81 if (isset($prefs_cache[$string])) {
82 $result = $prefs_cache[$string];
91 * Save the preferences for this user.
93 function savePrefValues($data_dir, $username) {
96 $filename = getHashedFile($username, $data_dir, "$username.pref");
98 $file = fopen($filename, 'w');
99 foreach ($prefs_cache as $Key => $Value) {
101 fwrite($file, $Key . '=' . $Value . "\n");
108 * Remove a preference for the current user.
110 function removePref($data_dir, $username, $string) {
113 cachePrefValues($data_dir, $username);
115 if (isset($prefs_cache[$string])) {
116 unset($prefs_cache[$string]);
119 savePrefValues($data_dir, $username);
123 * Set a there preference $string to $value.
125 function setPref($data_dir, $username, $string, $value) {
128 cachePrefValues($data_dir, $username);
129 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
134 removePref($data_dir, $username, $string);
138 $prefs_cache[$string] = $value;
139 savePrefValues($data_dir, $username);
143 * Check for a preferences file. If one can not be found, create it.
145 function checkForPrefs($data_dir, $username) {
146 $filename = getHashedFile($username, $data_dir, "$username.pref");
147 if (!file_exists($filename) ) {
148 if (!copy($data_dir . 'default_pref', $filename)) {
149 echo _("Error opening ") . $filename;
156 * Write the User Signature.
158 function setSig($data_dir, $username, $value) {
159 $filename = getHashedFile($username, $data_dir, "$username.sig");
160 $file = fopen($filename, 'w');
161 fwrite($file, $value);
168 function getSig($data_dir, $username) {
169 #$filename = $data_dir . $username . '.sig';
170 $filename = getHashedFile($username, $data_dir, "$username.sig");
172 if (file_exists($filename)) {
173 $file = fopen($filename, 'r');
174 while (!feof($file)) {
175 $sig .= fgets($file, 1024);
182 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
183 global $dir_hash_level;
185 /* Remove trailing slash from $dir if found */
186 if (substr($dir, -1) == '/') {
187 $dir = substr($dir, 0, strlen($dir) - 1);
190 /* Compute the hash for this user and extract the hash directories. */
191 $hash_dirs = computeHashDirs($username);
193 /* First, get and make sure the full hash directory exists. */
194 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
196 /* Set the value of our real data file. */
197 $result = "$real_hash_dir/$datafile";
199 /* Check for this file in the real hash directory. */
200 if ($hash_search && !file_exists($result)) {
201 /* First check the base directory, the most common location. */
202 if (file_exists("$dir/$datafile")) {
203 rename("$dir/$datafile", $result);
205 /* Then check the full range of possible hash directories. */
207 $check_hash_dir = $dir;
208 for ($h = 0; $h < 4; ++
$h) {
209 $check_hash_dir .= '/' . $hash_dirs[$h];
210 if (@is_readable
("$check_hash_dir/$datafile")) {
211 rename("$check_hash_dir/$datafile", $result);
218 /* Return the full hashed datafile path. */
222 function getHashedDir($username, $dir, $hash_dirs = '') {
223 global $dir_hash_level;
225 /* If necessary, populate the hash dir variable. */
226 if ($hash_dirs == '') {
227 $hash_dirs = computeHashDirs($username);
230 /* Make sure the full hash directory exists. */
231 $real_hash_dir = $dir;
232 for ($h = 0; $h < $dir_hash_level; ++
$h) {
233 $real_hash_dir .= '/' . $hash_dirs[$h];
234 if (!is_dir($real_hash_dir)) {
235 mkdir($real_hash_dir, 0770);
239 /* And return that directory. */
240 return ($real_hash_dir);
243 function computeHashDirs($username) {
244 /* Compute the hash for this user and extract the hash directories. */
245 $hash = base_convert(crc32($username), 10, 16);
246 $hash_dirs = array();
247 for ($h = 0; $h < 4; ++
$h) {
248 $hash_dirs[] = substr($hash, $h, 1);
251 /* Return our array of hash directories. */