6 * Copyright (c) 1999-2001 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 $filename = getHashedFile($username, $data_dir, "$username.pref");
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);
37 $file = fopen($filename, 'r');
39 /* Read in the preferences. */
41 while (! feof($file)) {
42 $pref = trim(fgets($file, 1024));
43 $equalsAt = strpos($pref, '=');
45 $key = substr($pref, 0, $equalsAt);
46 $value = substr($pref, $equalsAt +
1);
47 if (substr($key, 0, 9) == 'highlight') {
48 $key = 'highlight' . $highlight_num;
53 $prefs_cache[$key] = $value;
59 session_unregister('prefs_cache');
60 session_register('prefs_cache');
62 $prefs_are_cached = true;
63 session_unregister('prefs_are_cached');
64 session_register('prefs_are_cached');
68 * Return the value for the prefernce given by $string.
70 function getPref($data_dir, $username, $string, $default = '') {
74 cachePrefValues($data_dir, $username);
76 if (isset($prefs_cache[$string])) {
77 $result = $prefs_cache[$string];
86 * Save the preferences for this user.
88 function savePrefValues($data_dir, $username) {
91 $filename = getHashedFile($username, $data_dir, "$username.pref");
93 $file = fopen($filename, 'w');
94 foreach ($prefs_cache as $Key => $Value) {
96 fwrite($file, $Key . '=' . $Value . "\n");
103 * Remove a preference for the current user.
105 function removePref($data_dir, $username, $string) {
108 cachePrefValues($data_dir, $username);
110 if (isset($prefs_cache[$string])) {
111 unset($prefs_cache[$string]);
114 savePrefValues($data_dir, $username);
118 * Set a there preference $string to $value.
120 function setPref($data_dir, $username, $string, $value) {
123 cachePrefValues($data_dir, $username);
124 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
129 removePref($data_dir, $username, $string);
133 $prefs_cache[$string] = $value;
134 savePrefValues($data_dir, $username);
138 * Check for a preferences file. If one can not be found, create it.
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;
151 * Write the User Signature.
153 function setSig($data_dir, $username, $value) {
154 $filename = getHashedFile($username, $data_dir, "$username.sig");
155 $file = fopen($filename, 'w');
156 fwrite($file, $value);
163 function getSig($data_dir, $username) {
164 #$filename = $data_dir . $username . '.sig';
165 $filename = getHashedFile($username, $data_dir, "$username.sig");
167 if (file_exists($filename)) {
168 $file = fopen($filename, 'r');
169 while (!feof($file)) {
170 $sig .= fgets($file, 1024);
177 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
178 global $dir_hash_level;
180 /* Remove trailing slash from $dir if found */
181 if (substr($dir, -1) == '/') {
182 $dir = substr($dir, 0, strlen($dir) - 1);
185 /* Compute the hash for this user and extract the hash directories. */
186 $hash_dirs = computeHashDirs($username);
188 /* First, get and make sure the full hash directory exists. */
189 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
191 /* Set the value of our real data file. */
192 $result = "$real_hash_dir/$datafile";
194 /* Check for this file in the real hash directory. */
195 if ($hash_search && !file_exists($result)) {
196 /* First check the base directory, the most common location. */
197 if (file_exists("$dir/$datafile")) {
198 rename("$dir/$datafile", $result);
200 /* Then check the full range of possible hash directories. */
202 $check_hash_dir = $dir;
203 for ($h = 0; $h < 4; ++
$h) {
204 $check_hash_dir .= '/' . $hash_dirs[$h];
205 if (@is_readable
("$check_hash_dir/$datafile")) {
206 rename("$check_hash_dir/$datafile", $result);
213 /* Return the full hashed datafile path. */
217 function getHashedDir($username, $dir, $hash_dirs = '') {
218 global $dir_hash_level;
220 /* If necessary, populate the hash dir variable. */
221 if ($hash_dirs == '') {
222 $hash_dirs = computeHashDirs($username);
225 /* Make sure the full hash directory exists. */
226 $real_hash_dir = $dir;
227 for ($h = 0; $h < $dir_hash_level; ++
$h) {
228 $real_hash_dir .= '/' . $hash_dirs[$h];
229 if (!is_dir($real_hash_dir)) {
230 mkdir($real_hash_dir, 0770);
234 /* And return that directory. */
235 return ($real_hash_dir);
238 function computeHashDirs($username) {
239 /* Compute the hash for this user and extract the hash directories. */
240 $hash = base_convert(crc32($username), 10, 16);
241 $hash_dirs = array();
242 for ($h = 0; $h < 4; ++
$h) {
243 $hash_dirs[] = substr($hash, $h, 1);
246 /* Return our array of hash directories. */