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 // Make sure that this fgets is larger than any of the pref strings
53 // could ever be. 1024 is too short
54 $pref = trim(fgets($file, 65536));
55 $equalsAt = strpos($pref, '=');
57 $key = substr($pref, 0, $equalsAt);
58 $value = substr($pref, $equalsAt +
1);
59 /* this is to 'rescue' old-style highlighting rules. */
60 if (substr($key, 0, 9) == 'highlight') {
61 $key = 'highlight' . $highlight_num;
66 $prefs_cache[$key] = $value;
72 $prefs_are_cached = TRUE;
74 sqsession_register($prefs_cache, 'prefs_cache');
75 sqsession_register($prefs_are_cached, 'prefs_are_cached');
79 * Return the value for the prefernce given by $string.
81 function getPref($data_dir, $username, $string, $default = '') {
84 $result = do_hook_function('get_pref_override',array($username,$string));
86 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));
100 * Save the preferences for this user.
102 function savePrefValues($data_dir, $username) {
105 $filename = getHashedFile($username, $data_dir, "$username.pref");
107 /* Open the file for writing, or else display an error to the user. */
108 if(!$file = @fopen
($filename.'.tmp', 'w'))
110 include_once(SM_PATH
. 'functions/display_messages.php');
111 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
114 foreach ($prefs_cache as $Key => $Value) {
116 $tmpwrite = @fwrite
($file, $Key . '=' . $Value . "\n");
117 if ($tmpwrite == -1) {
118 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
124 @copy
($filename . '.tmp',$filename);
125 @unlink
($filename . '.tmp');
126 chmod($filename, 0600);
130 * Remove a preference for the current user.
132 function removePref($data_dir, $username, $string) {
135 cachePrefValues($data_dir, $username);
137 if (isset($prefs_cache[$string])) {
138 unset($prefs_cache[$string]);
141 savePrefValues($data_dir, $username);
145 * Set a there preference $string to $value.
147 function setPref($data_dir, $username, $string, $value) {
150 cachePrefValues($data_dir, $username);
151 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
156 removePref($data_dir, $username, $string);
160 $prefs_cache[$string] = $value;
161 savePrefValues($data_dir, $username);
165 * Check for a preferences file. If one can not be found, create it.
167 function checkForPrefs($data_dir, $username, $filename = '') {
168 /* First, make sure we have the filename. */
169 if ($filename == '') {
170 $filename = getHashedFile($username, $data_dir, "$username.pref");
173 /* Then, check if the file exists. */
174 if (!@file_exists
($filename) ) {
175 /* First, check the $data_dir for the default preference file. */
176 $default_pref = $data_dir . 'default_pref';
178 /* If it is not there, check the internal data directory. */
179 if (!@file_exists
($default_pref)) {
180 $default_pref = SM_PATH
. 'data/default_pref';
183 /* Otherwise, report an error. */
184 $errTitle = sprintf( _("Error opening %s"), $default_pref );
185 if (!is_readable($default_pref)) {
186 $errString = $errTitle . "<br>\n" .
187 _("Default preference file not found or not readable!") . "<br>\n" .
188 _("Please contact your system administrator and report this error.") . "<br>\n";
189 include_once(SM_PATH
. 'functions/display_messages.php' );
190 logout_error( $errString, $errTitle );
192 } else if (!@copy
($default_pref, $filename)) {
194 if (function_exists('posix_getuid')){
195 $user_data = posix_getpwuid(posix_getuid());
196 $uid = $user_data['name'];
198 $errString = $errTitle . '<br>' .
199 _("Could not create initial preference file!") . "<br>\n" .
200 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) .
201 "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n";
202 include_once(SM_PATH
. 'functions/display_messages.php' );
203 logout_error( $errString, $errTitle );
210 * Write the User Signature.
212 function setSig($data_dir, $username, $number, $value) {
213 $filename = getHashedFile($username, $data_dir, "$username.si$number");
214 /* Open the file for writing, or else display an error to the user. */
215 if(!$file = @fopen
("$filename.tmp", 'w')) {
216 include_once( '../functions/display_messages.php' );
217 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
220 $tmpwrite = @fwrite
($file, $value);
221 if ($tmpwrite == -1) {
222 include_once( '../functions/display_messages.php' );
223 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
227 @copy
($filename . '.tmp',$filename);
228 @unlink
($filename . '.tmp');
229 chmod($filename, 0600);
236 function getSig($data_dir, $username, $number) {
237 $filename = getHashedFile($username, $data_dir, "$username.si$number");
239 if (file_exists($filename)) {
240 /* Open the file, or else display an error to the user. */
241 if(!$file = @fopen
($filename, 'r'))
243 include_once(SM_PATH
. 'functions/display_messages.php');
244 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
247 while (!feof($file)) {
248 $sig .= fgets($file, 1024);