6 * Copyright (c) 1999-2005 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 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION
);
25 if ( isset($prefs_are_cached) && $prefs_are_cached) {
26 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION
);
30 sqsession_unregister('prefs_cache');
31 sqsession_unregister('prefs_are_cached');
33 /* Calculate the filename for the user's preference file */
34 $filename = getHashedFile($username, $data_dir, "$username.pref");
36 /* A call to checkForPrefs here should take eliminate the need for */
37 /* this to be called throughout the rest of the SquirrelMail code. */
38 checkForPrefs($data_dir, $username, $filename);
40 /* Make sure that the preference file now DOES exist. */
41 if (!file_exists($filename)) {
42 logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
46 /* Open the file, or else display an error to the user. */
47 if(!$file = @fopen
($filename, 'r'))
49 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
53 /* Read in the preferences. */
55 while (! feof($file)) {
57 /* keep reading a pref until we reach an eol (\n (or \r for macs)) */
58 while($read = fgets($file, 1024))
61 if(strpos($read,"\n") ||
strpos($read,"\r"))
65 $equalsAt = strpos($pref, '=');
67 $key = substr($pref, 0, $equalsAt);
68 $value = substr($pref, $equalsAt +
1);
69 /* this is to 'rescue' old-style highlighting rules. */
70 if (substr($key, 0, 9) == 'highlight') {
71 $key = 'highlight' . $highlight_num;
76 $prefs_cache[$key] = $value;
82 $prefs_are_cached = TRUE;
84 sqsession_register($prefs_cache, 'prefs_cache');
85 sqsession_register($prefs_are_cached, 'prefs_are_cached');
89 * Return the value for the preference given by $string.
91 function getPref($data_dir, $username, $string, $default = '') {
94 $result = do_hook_function('get_pref_override',array($username,$string));
96 cachePrefValues($data_dir, $username);
97 if (isset($prefs_cache[$string])) {
98 $result = $prefs_cache[$string];
100 $result = do_hook_function('get_pref', array($username,$string));
110 * Save the preferences for this user.
112 function savePrefValues($data_dir, $username) {
115 $filename = getHashedFile($username, $data_dir, "$username.pref");
117 /* Open the file for writing, or else display an error to the user. */
118 if(!$file = @fopen
($filename.'.tmp', 'w'))
120 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
123 foreach ($prefs_cache as $Key => $Value) {
125 if ( sq_fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) {
126 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
132 if (! @copy
($filename . '.tmp',$filename) ) {
133 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') );
136 @unlink
($filename . '.tmp');
137 @chmod
($filename, 0600);
138 sqsession_register($prefs_cache , 'prefs_cache');
142 * Remove a preference for the current user.
144 function removePref($data_dir, $username, $string) {
147 cachePrefValues($data_dir, $username);
149 if (isset($prefs_cache[$string])) {
150 unset($prefs_cache[$string]);
153 savePrefValues($data_dir, $username);
157 * Set a there preference $string to $value.
159 function setPref($data_dir, $username, $string, $value) {
162 cachePrefValues($data_dir, $username);
163 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
168 removePref($data_dir, $username, $string);
172 $prefs_cache[$string] = $value;
173 savePrefValues($data_dir, $username);
177 * Check for a preferences file. If one can not be found, create it.
179 function checkForPrefs($data_dir, $username, $filename = '') {
180 /* First, make sure we have the filename. */
181 if ($filename == '') {
182 $filename = getHashedFile($username, $data_dir, "$username.pref");
185 /* Then, check if the file exists. */
186 if (!@file_exists
($filename) ) {
188 /* If it does not exist, check for default_prefs */
190 /* First, check legacy locations: data dir */
191 if(substr($data_dir,-1) != '/') {
194 $default_pref = $data_dir . 'default_pref';
196 /* or legacy location: internal data dir */
197 if (!@file_exists
($default_pref)) {
198 $default_pref = SM_PATH
. 'data/default_pref';
201 /* If no legacies, check where we'd expect it to be located:
203 if (!@file_exists
($default_pref)) {
204 $default_pref = SM_PATH
. 'config/default_pref';
207 /* If a default_pref file found, try to copy it, if none found,
208 * try to create an empty one. If that fails, report an error.
211 ( is_readable($default_pref) && !@copy
($default_pref, $filename) ) ||
215 if (function_exists('posix_getuid')){
216 $user_data = posix_getpwuid(posix_getuid());
217 $uid = $user_data['name'];
219 $errTitle = _("Could not create initial preference file!");
220 $errString = $errTitle . "<br />\n" .
221 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) . "<br />\n" .
222 _("Please contact your system administrator and report this error.") . "<br />\n";
223 logout_error( $errString, $errTitle );
230 * Write the User Signature.
232 function setSig($data_dir, $username, $number, $value) {
233 // Limit signature size to 64KB (database BLOB limit)
234 if (strlen($value)>65536) {
235 error_option_save(_("Signature is too big."));
238 $filename = getHashedFile($username, $data_dir, "$username.si$number");
239 /* Open the file for writing, or else display an error to the user. */
240 if(!$file = @fopen
("$filename.tmp", 'w')) {
241 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
244 if ( sq_fwrite($file, $value) === FALSE ) {
245 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
249 if (! @copy
($filename . '.tmp',$filename) ) {
250 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') );
253 @unlink
($filename . '.tmp');
254 @chmod
($filename, 0600);
261 function getSig($data_dir, $username, $number) {
262 $filename = getHashedFile($username, $data_dir, "$username.si$number");
264 if (file_exists($filename)) {
265 /* Open the file, or else display an error to the user. */
266 if(!$file = @fopen
($filename, 'r'))
268 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
271 while (!feof($file)) {
272 $sig .= fgets($file, 1024);