6 * Copyright (c) 1999-2004 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
12 * @package squirrelmail
15 /** include this for error messages */
16 include_once(SM_PATH
. 'functions/display_messages.php');
19 * Check the preferences into the session cache.
21 function cachePrefValues($data_dir, $username) {
22 global $prefs_are_cached, $prefs_cache;
24 if ( isset($prefs_are_cached) && $prefs_are_cached) {
28 sqsession_unregister('prefs_cache');
29 sqsession_unregister('prefs_are_cached');
31 /* Calculate the filename for the user's preference file */
32 $filename = getHashedFile($username, $data_dir, "$username.pref");
34 /* A call to checkForPrefs here should take eliminate the need for */
35 /* this to be called throughout the rest of the SquirrelMail code. */
36 checkForPrefs($data_dir, $username, $filename);
38 /* Make sure that the preference file now DOES exist. */
39 if (!file_exists($filename)) {
40 logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
44 /* Open the file, or else display an error to the user. */
45 if(!$file = @fopen
($filename, 'r'))
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)) {
55 /* keep reading a pref until we reach an eol (\n (or \r for macs)) */
56 while($read = fgets($file, 1024))
59 if(strpos($read,"\n") ||
strpos($read,"\r"))
63 $equalsAt = strpos($pref, '=');
65 $key = substr($pref, 0, $equalsAt);
66 $value = substr($pref, $equalsAt +
1);
67 /* this is to 'rescue' old-style highlighting rules. */
68 if (substr($key, 0, 9) == 'highlight') {
69 $key = 'highlight' . $highlight_num;
74 $prefs_cache[$key] = $value;
80 $prefs_are_cached = TRUE;
82 sqsession_register($prefs_cache, 'prefs_cache');
83 sqsession_register($prefs_are_cached, 'prefs_are_cached');
87 * Return the value for the preference given by $string.
89 function getPref($data_dir, $username, $string, $default = '') {
92 $result = do_hook_function('get_pref_override',array($username,$string));
94 cachePrefValues($data_dir, $username);
95 if (isset($prefs_cache[$string])) {
96 $result = $prefs_cache[$string];
98 $result = do_hook_function('get_pref', array($username,$string));
108 * Save the preferences for this user.
110 function savePrefValues($data_dir, $username) {
113 $filename = getHashedFile($username, $data_dir, "$username.pref");
115 /* Open the file for writing, or else display an error to the user. */
116 if(!$file = @fopen
($filename.'.tmp', 'w'))
118 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
121 foreach ($prefs_cache as $Key => $Value) {
123 if ( sq_fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) {
124 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
130 if (! @copy
($filename . '.tmp',$filename) ) {
131 logout_error( sprintf( _("Preference file, %s, could not be copied from temporary file, %s. Contact your system administrator to resolve this issue."), $filename, $filename . '.tmp') );
134 @unlink
($filename . '.tmp');
135 @chmod
($filename, 0600);
136 sqsession_register($prefs_cache , 'prefs_cache');
140 * Remove a preference for the current user.
142 function removePref($data_dir, $username, $string) {
145 cachePrefValues($data_dir, $username);
147 if (isset($prefs_cache[$string])) {
148 unset($prefs_cache[$string]);
151 savePrefValues($data_dir, $username);
155 * Set a there preference $string to $value.
157 function setPref($data_dir, $username, $string, $value) {
160 cachePrefValues($data_dir, $username);
161 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
166 removePref($data_dir, $username, $string);
170 $prefs_cache[$string] = $value;
171 savePrefValues($data_dir, $username);
175 * Check for a preferences file. If one can not be found, create it.
177 function checkForPrefs($data_dir, $username, $filename = '') {
178 /* First, make sure we have the filename. */
179 if ($filename == '') {
180 $filename = getHashedFile($username, $data_dir, "$username.pref");
183 /* Then, check if the file exists. */
184 if (!@file_exists
($filename) ) {
185 /* First, check the $data_dir for the default preference file. */
186 if(substr($data_dir,-1) != '/') {
189 $default_pref = $data_dir . 'default_pref';
191 /* If it is not there, check the internal data directory. */
192 if (!@file_exists
($default_pref)) {
193 $default_pref = SM_PATH
. 'data/default_pref';
196 /* Otherwise, report an error. */
197 $errTitle = sprintf( _("Error opening %s"), $default_pref );
198 if (!is_readable($default_pref)) {
199 $errString = $errTitle . "<br>\n" .
200 _("Default preference file not found or not readable!") . "<br>\n" .
201 _("Please contact your system administrator and report this error.") . "<br>\n";
202 logout_error( $errString, $errTitle );
204 } else if (!@copy
($default_pref, $filename)) {
206 if (function_exists('posix_getuid')){
207 $user_data = posix_getpwuid(posix_getuid());
208 $uid = $user_data['name'];
210 $errString = $errTitle . '<br>' .
211 _("Could not create initial preference file!") . "<br>\n" .
212 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) .
213 "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n";
214 logout_error( $errString, $errTitle );
221 * Write the User Signature.
223 function setSig($data_dir, $username, $number, $value) {
224 $filename = getHashedFile($username, $data_dir, "$username.si$number");
225 /* Open the file for writing, or else display an error to the user. */
226 if(!$file = @fopen
("$filename.tmp", 'w')) {
227 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
230 if ( sq_fwrite($file, $value) === FALSE ) {
231 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
235 if (! @copy
($filename . '.tmp',$filename) ) {
236 logout_error( sprintf( _("Signature file, %s, could not be copied from temporary file, %s. Contact your system administrator to resolve this issue."), $filename, $filename . '.tmp') );
239 @unlink
($filename . '.tmp');
240 @chmod
($filename, 0600);
247 function getSig($data_dir, $username, $number) {
248 $filename = getHashedFile($username, $data_dir, "$username.si$number");
250 if (file_exists($filename)) {
251 /* Open the file, or else display an error to the user. */
252 if(!$file = @fopen
($filename, 'r'))
254 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
257 while (!feof($file)) {
258 $sig .= fgets($file, 1024);