6 * Copyright (c) 1999-2003 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 /* include this for error messages */
15 include_once(SM_PATH
. 'functions/display_messages.php');
18 * Check the preferences into the session cache.
20 function cachePrefValues($data_dir, $username) {
21 global $prefs_are_cached, $prefs_cache;
23 if ( isset($prefs_are_cached) && $prefs_are_cached) {
27 sqsession_unregister('prefs_cache');
28 sqsession_unregister('prefs_are_cached');
30 /* Calculate the filename for the user's preference file */
31 $filename = getHashedFile($username, $data_dir, "$username.pref");
33 /* A call to checkForPrefs here should take eliminate the need for */
34 /* this to be called throughout the rest of the SquirrelMail code. */
35 checkForPrefs($data_dir, $username, $filename);
37 /* Make sure that the preference file now DOES exist. */
38 if (!file_exists($filename)) {
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 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
50 /* Read in the preferences. */
52 while (! feof($file)) {
54 /* keep reading a pref until we reach an eol (\n (or \r for macs)) */
55 while($read = fgets($file, 1024))
58 if(strpos($read,"\n") ||
strpos($read,"\r"))
62 $equalsAt = strpos($pref, '=');
64 $key = substr($pref, 0, $equalsAt);
65 $value = substr($pref, $equalsAt +
1);
66 /* this is to 'rescue' old-style highlighting rules. */
67 if (substr($key, 0, 9) == 'highlight') {
68 $key = 'highlight' . $highlight_num;
73 $prefs_cache[$key] = $value;
79 $prefs_are_cached = TRUE;
81 sqsession_register($prefs_cache, 'prefs_cache');
82 sqsession_register($prefs_are_cached, 'prefs_are_cached');
86 * Return the value for the preference given by $string.
88 function getPref($data_dir, $username, $string, $default = '') {
91 $result = do_hook_function('get_pref_override',array($username,$string));
93 cachePrefValues($data_dir, $username);
94 if (isset($prefs_cache[$string])) {
95 $result = $prefs_cache[$string];
97 $result = do_hook_function('get_pref', array($username,$string));
107 * Save the preferences for this user.
109 function savePrefValues($data_dir, $username) {
112 $filename = getHashedFile($username, $data_dir, "$username.pref");
114 /* Open the file for writing, or else display an error to the user. */
115 if(!$file = @fopen
($filename.'.tmp', 'w'))
117 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
120 foreach ($prefs_cache as $Key => $Value) {
122 if ( @fwrite
($file, $Key . '=' . $Value . "\n") === FALSE ) {
123 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
129 if (! @copy
($filename . '.tmp',$filename) ) {
130 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') );
133 @unlink
($filename . '.tmp');
134 chmod($filename, 0600);
135 sqsession_register($prefs_cache , 'prefs_cache');
139 * Remove a preference for the current user.
141 function removePref($data_dir, $username, $string) {
144 cachePrefValues($data_dir, $username);
146 if (isset($prefs_cache[$string])) {
147 unset($prefs_cache[$string]);
150 savePrefValues($data_dir, $username);
154 * Set a there preference $string to $value.
156 function setPref($data_dir, $username, $string, $value) {
159 cachePrefValues($data_dir, $username);
160 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
165 removePref($data_dir, $username, $string);
169 $prefs_cache[$string] = $value;
170 savePrefValues($data_dir, $username);
174 * Check for a preferences file. If one can not be found, create it.
176 function checkForPrefs($data_dir, $username, $filename = '') {
177 /* First, make sure we have the filename. */
178 if ($filename == '') {
179 $filename = getHashedFile($username, $data_dir, "$username.pref");
182 /* Then, check if the file exists. */
183 if (!@file_exists
($filename) ) {
184 /* First, check the $data_dir for the default preference file. */
185 if(substr($data_dir,-1) != '/') {
188 $default_pref = $data_dir . 'default_pref';
190 /* If it is not there, check the internal data directory. */
191 if (!@file_exists
($default_pref)) {
192 $default_pref = SM_PATH
. 'data/default_pref';
195 /* Otherwise, report an error. */
196 $errTitle = sprintf( _("Error opening %s"), $default_pref );
197 if (!is_readable($default_pref)) {
198 $errString = $errTitle . "<br>\n" .
199 _("Default preference file not found or not readable!") . "<br>\n" .
200 _("Please contact your system administrator and report this error.") . "<br>\n";
201 logout_error( $errString, $errTitle );
203 } else if (!@copy
($default_pref, $filename)) {
205 if (function_exists('posix_getuid')){
206 $user_data = posix_getpwuid(posix_getuid());
207 $uid = $user_data['name'];
209 $errString = $errTitle . '<br>' .
210 _("Could not create initial preference file!") . "<br>\n" .
211 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) .
212 "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n";
213 logout_error( $errString, $errTitle );
220 * Write the User Signature.
222 function setSig($data_dir, $username, $number, $value) {
223 $filename = getHashedFile($username, $data_dir, "$username.si$number");
224 /* Open the file for writing, or else display an error to the user. */
225 if(!$file = @fopen
("$filename.tmp", 'w')) {
226 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
229 if ( @fwrite
($file, $value) === FALSE ) {
230 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
234 if (! @copy
($filename . '.tmp',$filename) ) {
235 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') );
238 @unlink
($filename . '.tmp');
239 chmod($filename, 0600);
246 function getSig($data_dir, $username, $number) {
247 $filename = getHashedFile($username, $data_dir, "$username.si$number");
249 if (file_exists($filename)) {
250 /* Open the file, or else display an error to the user. */
251 if(!$file = @fopen
($filename, 'r'))
253 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
256 while (!feof($file)) {
257 $sig .= fgets($file, 1024);