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 include_once( '../functions/display_messages.php' );
39 logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
43 /* Open the file, or else display an error to the user. */
44 if(!$file = @fopen
($filename, 'r'))
46 include_once( '../functions/display_messages.php' );
47 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
51 /* Read in the preferences. */
53 while (! feof($file)) {
54 $pref = trim(fgets($file, 1024));
55 $equalsAt = strpos($pref, '=');
57 $key = substr($pref, 0, $equalsAt);
58 $value = substr($pref, $equalsAt +
1);
59 if (substr($key, 0, 9) == 'highlight') {
60 $key = 'highlight' . $highlight_num;
65 $prefs_cache[$key] = $value;
71 $prefs_are_cached = TRUE;
73 session_register('prefs_cache');
74 session_register('prefs_are_cached');
78 * Return the value for the prefernce given by $string.
80 function getPref($data_dir, $username, $string, $default = '') {
84 cachePrefValues($data_dir, $username);
86 if (isset($prefs_cache[$string])) {
87 $result = $prefs_cache[$string];
96 * Save the preferences for this user.
98 function savePrefValues($data_dir, $username) {
101 $filename = getHashedFile($username, $data_dir, "$username.pref");
103 /* Open the file for writing, or else display an error to the user. */
104 if(!$file = @fopen
($filename, 'w'))
106 include_once( '../functions/display_messages.php' );
107 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
111 foreach ($prefs_cache as $Key => $Value) {
113 fwrite($file, $Key . '=' . $Value . "\n");
117 chmod($filename, 0600);
121 * Remove a preference for the current user.
123 function removePref($data_dir, $username, $string) {
126 cachePrefValues($data_dir, $username);
128 if (isset($prefs_cache[$string])) {
129 unset($prefs_cache[$string]);
132 savePrefValues($data_dir, $username);
136 * Set a there preference $string to $value.
138 function setPref($data_dir, $username, $string, $value) {
141 cachePrefValues($data_dir, $username);
142 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
147 removePref($data_dir, $username, $string);
151 $prefs_cache[$string] = $value;
152 savePrefValues($data_dir, $username);
156 * Check for a preferences file. If one can not be found, create it.
158 function checkForPrefs($data_dir, $username, $filename = '') {
159 /* First, make sure we have the filename. */
160 if ($filename == '') {
161 $filename = getHashedFile($username, $data_dir, "$username.pref");
164 /* Then, check if the file exists. */
165 if (!@file_exists
($filename) ) {
166 /* First, check the $data_dir for the default preference file. */
167 $default_pref = $data_dir . 'default_pref';
169 /* If it is not there, check the internal data directory. */
170 if (!@file_exists
($default_pref)) {
171 $default_pref = '../data/default_pref';
174 /* Otherwise, report an error. */
175 $errTitle = sprintf( _("Error opening %s"), $default_pref );
176 if (!file_exists($default_pref)) {
177 $errString = $errTitle . "<br>\n" .
178 _("Default preference file not found!") . "<br>\n" .
179 _("Please contact your system administrator and report this error.") . "<br>\n";
180 include_once( '../functions/display_messages.php' );
181 logout_error( $errString, $errTitle );
183 } else if (!@copy
($default_pref, $filename)) {
185 if (function_exists('posix_getuid')){
186 $user_data = posix_getpwuid(posix_getuid());
187 $uid = $user_data['name'];
189 $errString = $errTitle . '<br>' .
190 _("Could not create initial preference file!") . "<br>\n" .
191 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) .
192 "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n";
193 include_once( '../functions/display_messages.php' );
194 logout_error( $errString, $errTitle );
201 * Write the User Signature.
203 function setSig($data_dir, $username, $number, $value) {
204 $filename = getHashedFile($username, $data_dir, "$username.si$number");
205 /* Open the file for writing, or else display an error to the user. */
206 if(!$file = @fopen
($filename, 'w'))
208 include_once( '../functions/display_messages.php' );
209 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
212 fwrite($file, $value);
219 function getSig($data_dir, $username, $number) {
220 $filename = getHashedFile($username, $data_dir, "$username.si$number");
222 if (file_exists($filename)) {
223 /* Open the file, or else display an error to the user. */
224 if(!$file = @fopen
($filename, 'r'))
226 include_once( '../functions/display_messages.php' );
227 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
230 while (!feof($file)) {
231 $sig .= fgets($file, 1024);