8dacc32af1422d4e54f352877b9a0d20b4cbb736
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
15 * Check the preferences into the session cache.
17 function cachePrefValues($data_dir, $username) {
18 global $prefs_are_cached, $prefs_cache;
20 if ( isset($prefs_are_cached) && $prefs_are_cached) {
24 sqsession_unregister('prefs_cache');
25 sqsession_unregister('prefs_are_cached');
27 /* Calculate the filename for the user's preference file */
28 $filename = getHashedFile($username, $data_dir, "$username.pref");
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, $filename);
34 /* Make sure that the preference file now DOES exist. */
35 if (!file_exists($filename)) {
36 include_once(SM_PATH
. 'functions/display_messages.php');
37 logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
41 /* Open the file, or else display an error to the user. */
42 if(!$file = @fopen
($filename, 'r'))
44 include_once(SM_PATH
. 'functions/display_messages.php');
45 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
49 /* Read in the preferences. */
51 while (! feof($file)) {
52 $pref = trim(fgets($file, 1024));
53 $equalsAt = strpos($pref, '=');
55 $key = substr($pref, 0, $equalsAt);
56 $value = substr($pref, $equalsAt +
1);
57 if (substr($key, 0, 9) == 'highlight') {
58 $key = 'highlight' . $highlight_num;
63 $prefs_cache[$key] = $value;
69 $prefs_are_cached = TRUE;
71 sqsession_register($prefs_cache, 'prefs_cache');
72 sqsession_register($prefs_are_cached, 'prefs_are_cached');
76 * Return the value for the prefernce given by $string.
78 function getPref($data_dir, $username, $string, $default = '') {
82 $result = do_hook_function('get_pref_override', array($username, $string));
85 cachePrefValues($data_dir, $username);
87 if (isset($prefs_cache[$string])) {
88 $result = $prefs_cache[$string];
90 $result = do_hook_function('get_pref', array($username, $string));
101 * Save the preferences for this user.
103 function savePrefValues($data_dir, $username) {
106 $filename = getHashedFile($username, $data_dir, "$username.pref");
108 /* Open the file for writing, or else display an error to the user. */
109 if(!$file = @fopen
($filename.'.tmp', 'w'))
111 include_once(SM_PATH
. 'functions/display_messages.php');
112 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
116 foreach ($prefs_cache as $Key => $Value) {
118 fwrite($file, $Key . '=' . $Value . "\n");
122 copy($filename.'.tmp', $filename);
123 unlink($filename.'.tmp');
124 chmod($filename, 0600);
128 * Remove a preference for the current user.
130 function removePref($data_dir, $username, $string) {
133 cachePrefValues($data_dir, $username);
135 if (isset($prefs_cache[$string])) {
136 unset($prefs_cache[$string]);
139 savePrefValues($data_dir, $username);
143 * Set a there preference $string to $value.
145 function setPref($data_dir, $username, $string, $value) {
148 cachePrefValues($data_dir, $username);
149 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
154 removePref($data_dir, $username, $string);
158 $prefs_cache[$string] = $value;
159 savePrefValues($data_dir, $username);
163 * Check for a preferences file. If one can not be found, create it.
165 function checkForPrefs($data_dir, $username, $filename = '') {
166 /* First, make sure we have the filename. */
167 if ($filename == '') {
168 $filename = getHashedFile($username, $data_dir, "$username.pref");
171 /* Then, check if the file exists. */
172 if (!@file_exists
($filename) ) {
173 /* First, check the $data_dir for the default preference file. */
174 $default_pref = $data_dir . 'default_pref';
176 /* If it is not there, check the internal data directory. */
177 if (!@file_exists
($default_pref)) {
178 $default_pref = SM_PATH
. 'data/default_pref';
181 /* Otherwise, report an error. */
182 $errTitle = sprintf( _("Error opening %s"), $default_pref );
183 if (!file_exists($default_pref)) {
184 $errString = $errTitle . "<br>\n" .
185 _("Default preference file not found!") . "<br>\n" .
186 _("Please contact your system administrator and report this error.") . "<br>\n";
187 include_once(SM_PATH
. 'functions/display_messages.php' );
188 logout_error( $errString, $errTitle );
190 } else if (!@copy
($default_pref, $filename)) {
192 if (function_exists('posix_getuid')){
193 $user_data = posix_getpwuid(posix_getuid());
194 $uid = $user_data['name'];
196 $errString = $errTitle . '<br>' .
197 _("Could not create initial preference file!") . "<br>\n" .
198 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) .
199 "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n";
200 include_once(SM_PATH
. 'functions/display_messages.php' );
201 logout_error( $errString, $errTitle );
208 * Write the User Signature.
210 function setSig($data_dir, $username, $number, $value) {
211 $filename = getHashedFile($username, $data_dir, "$username.si$number");
212 /* Open the file for writing, or else display an error to the user. */
213 if(!$file = @fopen
($filename.'.tmp', 'w'))
215 include_once(SM_PATH
. '/functions/display_messages.php' );
216 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
219 fwrite($file, $value);
221 copy($filename.'.tmp',$filename);
222 unlink($filename.'.tmp');
228 function getSig($data_dir, $username, $number) {
229 $filename = getHashedFile($username, $data_dir, "$username.si$number");
231 if (file_exists($filename)) {
232 /* Open the file, or else display an error to the user. */
233 if(!$file = @fopen
($filename, 'r'))
235 include_once(SM_PATH
. 'functions/display_messages.php');
236 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
239 while (!feof($file)) {
240 $sig .= fgets($file, 1024);