1cde4c88ffd71b854266878faf6562eccdcc359b
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
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)) {
53 /* keep reading a pref until we reach an eol (\n (or \r for macs)) */
54 while($read = fgets($file, 1024))
57 if(strpos($read,"\n") ||
strpos($read,"\r"))
61 $equalsAt = strpos($pref, '=');
63 $key = substr($pref, 0, $equalsAt);
64 $value = substr($pref, $equalsAt +
1);
65 /* this is to 'rescue' old-style highlighting rules. */
66 if (substr($key, 0, 9) == 'highlight') {
67 $key = 'highlight' . $highlight_num;
72 $prefs_cache[$key] = $value;
78 $prefs_are_cached = TRUE;
80 sqsession_register($prefs_cache, 'prefs_cache');
81 sqsession_register($prefs_are_cached, 'prefs_are_cached');
86 * Return the value for the prefernce given by $string.
88 function getPref($data_dir, $username, $string, $default = '') {
91 $result = do_hook_function('get_pref_override',array($username,$string));
93 $prefs_cache = 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, $prefs_cache) {
111 $filename = getHashedFile($username, $data_dir, "$username.pref");
113 if (!is_array($prefs_cache) ||
count($prefs_cache) == 0) {
114 include_once(SM_PATH
. 'functions/display_messages.php');
115 logout_error( _("Houston we have a problem, the preference cache is lost!!!."));
119 /* Open the file for writing, or else display an error to the user. */
120 if(!$file = @fopen
($filename.'.tmp', 'w'))
122 include_once(SM_PATH
. 'functions/display_messages.php');
123 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
126 foreach ($prefs_cache as $Key => $Value) {
128 $tmpwrite = @fwrite
($file, $Key . '=' . $Value . "\n");
129 if ($tmpwrite == -1) {
130 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
136 @copy
($filename . '.tmp',$filename);
137 @unlink
($filename . '.tmp');
138 chmod($filename, 0600);
142 * Remove a preference for the current user.
144 function removePref($data_dir, $username, $string) {
147 if (isset($prefs_cache[$string])) {
148 unset($prefs_cache[$string]);
151 savePrefValues($data_dir, $username,$prefs_cache);
155 * Set a there preference $string to $value.
157 function setPref($data_dir, $username, $string, $value) {
160 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
165 unset($prefs_cache[$string]);
167 $prefs_cache[$string] = $value;
169 savePrefValues($data_dir, $username, $prefs_cache);
173 * Check for a preferences file. If one can not be found, create it.
175 function checkForPrefs($data_dir, $username, $filename = '') {
176 /* First, make sure we have the filename. */
177 if ($filename == '') {
178 $filename = getHashedFile($username, $data_dir, "$username.pref");
181 /* Then, check if the file exists. */
182 if (!@file_exists
($filename) ) {
183 /* First, check the $data_dir for the default preference file. */
184 $default_pref = $data_dir . '/default_pref';
186 /* If it is not there, check the internal data directory. */
187 if (!@file_exists
($default_pref)) {
188 $default_pref = SM_PATH
. 'data/default_pref';
191 /* Otherwise, report an error. */
192 $errTitle = sprintf( _("Error opening %s"), $default_pref );
193 if (!is_readable($default_pref)) {
194 $errString = $errTitle . "<br>\n" .
195 _("Default preference file not found or not readable!") . "<br>\n" .
196 _("Please contact your system administrator and report this error.") . "<br>\n";
197 include_once(SM_PATH
. 'functions/display_messages.php' );
198 logout_error( $errString, $errTitle );
200 } else if (!@copy
($default_pref, $filename)) {
202 if (function_exists('posix_getuid')){
203 $user_data = posix_getpwuid(posix_getuid());
204 $uid = $user_data['name'];
206 $errString = $errTitle . '<br>' .
207 _("Could not create initial preference file!") . "<br>\n" .
208 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) .
209 "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n";
210 include_once(SM_PATH
. 'functions/display_messages.php' );
211 logout_error( $errString, $errTitle );
218 * Write the User Signature.
220 function setSig($data_dir, $username, $number, $value) {
221 $filename = getHashedFile($username, $data_dir, "$username.si$number");
222 /* Open the file for writing, or else display an error to the user. */
223 if(!$file = @fopen
("$filename.tmp", 'w')) {
224 include_once( '../functions/display_messages.php' );
225 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
228 $tmpwrite = @fwrite
($file, $value);
229 if ($tmpwrite == -1) {
230 include_once( '../functions/display_messages.php' );
231 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
235 @copy
($filename . '.tmp',$filename);
236 @unlink
($filename . '.tmp');
237 chmod($filename, 0600);
244 function getSig($data_dir, $username, $number) {
245 $filename = getHashedFile($username, $data_dir, "$username.si$number");
247 if (file_exists($filename)) {
248 /* Open the file, or else display an error to the user. */
249 if(!$file = @fopen
($filename, 'r'))
251 include_once(SM_PATH
. 'functions/display_messages.php');
252 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
255 while (!feof($file)) {
256 $sig .= fgets($file, 1024);