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 in files
14 global $prefs_are_cached, $prefs_cache;
17 * Check the preferences into the session cache.
19 function cachePrefValues($data_dir, $username) {
20 global $prefs_are_cached, $prefs_cache;
22 if ( isset($prefs_are_cached) && $prefs_are_cached) {
26 session_unregister('prefs_cache');
27 session_unregister('prefs_are_cached');
29 /* Calculate the filename for the user's preference file */
30 $filename = getHashedFile($username, $data_dir, "$username.pref");
32 /* A call to checkForPrefs here should take eliminate the need for */
33 /* this to be called throughout the rest of the SquirrelMail code. */
34 checkForPrefs($data_dir, $username, $filename);
36 /* Make sure that the preference file now DOES exist. */
37 if (!file_exists($filename)) {
38 echo sprintf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) . "<br>\n";
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 $prefs_are_cached = TRUE;
66 session_register('prefs_cache');
67 session_register('prefs_are_cached');
71 * Return the value for the prefernce given by $string.
73 function getPref($data_dir, $username, $string, $default = '') {
77 cachePrefValues($data_dir, $username);
79 if (isset($prefs_cache[$string])) {
80 $result = $prefs_cache[$string];
89 * Save the preferences for this user.
91 function savePrefValues($data_dir, $username) {
94 $filename = getHashedFile($username, $data_dir, "$username.pref");
96 $file = fopen($filename, 'w');
97 foreach ($prefs_cache as $Key => $Value) {
99 fwrite($file, $Key . '=' . $Value . "\n");
103 chmod($filename, 0600);
107 * Remove a preference for the current user.
109 function removePref($data_dir, $username, $string) {
112 cachePrefValues($data_dir, $username);
114 if (isset($prefs_cache[$string])) {
115 unset($prefs_cache[$string]);
118 savePrefValues($data_dir, $username);
122 * Set a there preference $string to $value.
124 function setPref($data_dir, $username, $string, $value) {
127 cachePrefValues($data_dir, $username);
128 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
133 removePref($data_dir, $username, $string);
137 $prefs_cache[$string] = $value;
138 savePrefValues($data_dir, $username);
142 * Check for a preferences file. If one can not be found, create it.
144 function checkForPrefs($data_dir, $username, $filename = '') {
145 /* First, make sure we have the filename. */
146 if ($filename == '') {
147 $filename = getHashedFile($username, $data_dir, "$username.pref");
150 /* Then, check if the file exists. */
151 if (!@file_exists
($filename) ) {
152 /* First, check the $data_dir for the default preference file. */
153 $default_pref = $data_dir . 'default_pref';
155 /* If it is not there, check the internal data directory. */
156 if (!@file_exists
($default_pref)) {
157 $default_pref = '../data/default_pref';
160 /* Otherwise, report an error. */
161 if (!file_exists($default_pref)) {
162 echo _("Error opening ") . $default_pref . "<br>\n";
163 echo _("Default preference file not found!") . "<br>\n";
164 echo _("Please contact your system administrator and report this error.") . "<br>\n";
166 } else if (!@copy
($default_pref, $filename)) {
167 echo _("Error opening ") . $default_pref . '<br>';
168 echo _("Could not create initial preference file!") . "<br>\n";
169 $user_data = posix_getpwuid(posix_getuid());
170 $uid = $user_data['name'];
171 echo $data_dir . ' ' . _("should be writable by user") . ' ' . $uid . "<br>\n";
172 echo _("Please contact your system administrator and report this error.") . "<br>\n";
179 * Write the User Signature.
181 function setSig($data_dir, $username, $number, $value) {
182 $filename = getHashedFile($username, $data_dir, "$username.si$number");
183 $file = fopen($filename, 'w');
184 fwrite($file, $value);
191 function getSig($data_dir, $username, $number) {
192 #$filename = $data_dir . $username . '.si$number';
193 $filename = getHashedFile($username, $data_dir, "$username.si$number");
195 if (file_exists($filename)) {
196 $file = fopen($filename, 'r');
197 while (!feof($file)) {
198 $sig .= fgets($file, 1024);