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 = '') {
81 $result = do_hook_function('get_pref_override',array($username,$string));
83 cachePrefValues($data_dir, $username);
84 if (isset($prefs_cache[$string])) {
85 $result = $prefs_cache[$string];
87 $result = do_hook_function('get_pref', array($username,$string));
97 * Save the preferences for this user.
99 function savePrefValues($data_dir, $username) {
102 $filename = getHashedFile($username, $data_dir, "$username.pref");
104 /* Open the file for writing, or else display an error to the user. */
105 if(!$file = @fopen
($filename.'.tmp', 'w'))
107 include_once(SM_PATH
. 'functions/display_messages.php');
108 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
111 foreach ($prefs_cache as $Key => $Value) {
113 $tmpwrite = @fwrite
($file, $Key . '=' . $Value . "\n");
114 if ($tmpwrite == -1) {
115 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
121 @copy
($filename . '.tmp',$filename);
122 @unlink
($filename . '.tmp');
123 chmod($filename, 0600);
127 * Remove a preference for the current user.
129 function removePref($data_dir, $username, $string) {
132 cachePrefValues($data_dir, $username);
134 if (isset($prefs_cache[$string])) {
135 unset($prefs_cache[$string]);
138 savePrefValues($data_dir, $username);
142 * Set a there preference $string to $value.
144 function setPref($data_dir, $username, $string, $value) {
147 cachePrefValues($data_dir, $username);
148 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
153 removePref($data_dir, $username, $string);
157 $prefs_cache[$string] = $value;
158 savePrefValues($data_dir, $username);
162 * Check for a preferences file. If one can not be found, create it.
164 function checkForPrefs($data_dir, $username, $filename = '') {
165 /* First, make sure we have the filename. */
166 if ($filename == '') {
167 $filename = getHashedFile($username, $data_dir, "$username.pref");
170 /* Then, check if the file exists. */
171 if (!@file_exists
($filename) ) {
172 /* First, check the $data_dir for the default preference file. */
173 $default_pref = $data_dir . 'default_pref';
175 /* If it is not there, check the internal data directory. */
176 if (!@file_exists
($default_pref)) {
177 $default_pref = SM_PATH
. 'data/default_pref';
180 /* Otherwise, report an error. */
181 $errTitle = sprintf( _("Error opening %s"), $default_pref );
182 if (!is_readable($default_pref)) {
183 $errString = $errTitle . "<br>\n" .
184 _("Default preference file not found or not readable!") . "<br>\n" .
185 _("Please contact your system administrator and report this error.") . "<br>\n";
186 include_once(SM_PATH
. 'functions/display_messages.php' );
187 logout_error( $errString, $errTitle );
189 } else if (!@copy
($default_pref, $filename)) {
191 if (function_exists('posix_getuid')){
192 $user_data = posix_getpwuid(posix_getuid());
193 $uid = $user_data['name'];
195 $errString = $errTitle . '<br>' .
196 _("Could not create initial preference file!") . "<br>\n" .
197 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) .
198 "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n";
199 include_once(SM_PATH
. 'functions/display_messages.php' );
200 logout_error( $errString, $errTitle );
207 * Write the User Signature.
209 function setSig($data_dir, $username, $number, $value) {
210 $filename = getHashedFile($username, $data_dir, "$username.si$number");
211 /* Open the file for writing, or else display an error to the user. */
212 if(!$file = @fopen
("$filename.tmp", 'w')) {
213 include_once( '../functions/display_messages.php' );
214 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
217 $tmpwrite = @fwrite
($file, $value);
218 if ($tmpwrite == -1) {
219 include_once( '../functions/display_messages.php' );
220 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
224 @copy
($filename . '.tmp',$filename);
225 @unlink
($filename . '.tmp');
226 chmod($filename, 0600);
233 function getSig($data_dir, $username, $number) {
234 $filename = getHashedFile($username, $data_dir, "$username.si$number");
236 if (file_exists($filename)) {
237 /* Open the file, or else display an error to the user. */
238 if(!$file = @fopen
($filename, 'r'))
240 include_once(SM_PATH
. 'functions/display_messages.php');
241 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
244 while (!feof($file)) {
245 $sig .= fgets($file, 1024);