| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * file_prefs.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * This contains functions for manipulating user preferences in files |
| 10 | * |
| 11 | * $Id$ |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * Check the preferences into the session cache. |
| 16 | */ |
| 17 | function cachePrefValues($data_dir, $username) { |
| 18 | global $prefs_are_cached, $prefs_cache; |
| 19 | |
| 20 | if ( isset($prefs_are_cached) && $prefs_are_cached) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | sqsession_unregister('prefs_cache'); |
| 25 | sqsession_unregister('prefs_are_cached'); |
| 26 | |
| 27 | /* Calculate the filename for the user's preference file */ |
| 28 | $filename = getHashedFile($username, $data_dir, "$username.pref"); |
| 29 | |
| 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); |
| 33 | |
| 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) ); |
| 38 | exit; |
| 39 | } |
| 40 | |
| 41 | /* Open the file, or else display an error to the user. */ |
| 42 | if(!$file = @fopen($filename, 'r')) |
| 43 | { |
| 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) ); |
| 46 | exit; |
| 47 | } |
| 48 | |
| 49 | /* Read in the preferences. */ |
| 50 | $highlight_num = 0; |
| 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, '='); |
| 56 | if ($equalsAt > 0) { |
| 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; |
| 62 | $highlight_num ++; |
| 63 | } |
| 64 | |
| 65 | if ($value != '') { |
| 66 | $prefs_cache[$key] = $value; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | fclose($file); |
| 71 | |
| 72 | $prefs_are_cached = TRUE; |
| 73 | |
| 74 | sqsession_register($prefs_cache, 'prefs_cache'); |
| 75 | sqsession_register($prefs_are_cached, 'prefs_are_cached'); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Return the value for the prefernce given by $string. |
| 80 | */ |
| 81 | function getPref($data_dir, $username, $string, $default = '') { |
| 82 | global $prefs_cache; |
| 83 | |
| 84 | $result = do_hook_function('get_pref_override',array($username,$string)); |
| 85 | if (!$result) { |
| 86 | cachePrefValues($data_dir, $username); |
| 87 | if (isset($prefs_cache[$string])) { |
| 88 | $result = $prefs_cache[$string]; |
| 89 | } else { |
| 90 | $result = do_hook_function('get_pref', array($username,$string)); |
| 91 | if (!$result) { |
| 92 | $result = $default; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | return ($result); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Save the preferences for this user. |
| 101 | */ |
| 102 | function savePrefValues($data_dir, $username) { |
| 103 | global $prefs_cache; |
| 104 | |
| 105 | $filename = getHashedFile($username, $data_dir, "$username.pref"); |
| 106 | |
| 107 | /* Open the file for writing, or else display an error to the user. */ |
| 108 | if(!$file = @fopen($filename.'.tmp', 'w')) |
| 109 | { |
| 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') ); |
| 112 | exit; |
| 113 | } |
| 114 | foreach ($prefs_cache as $Key => $Value) { |
| 115 | if (isset($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') ); |
| 119 | exit; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | fclose($file); |
| 124 | @copy($filename . '.tmp',$filename); |
| 125 | @unlink($filename . '.tmp'); |
| 126 | chmod($filename, 0600); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Remove a preference for the current user. |
| 131 | */ |
| 132 | function removePref($data_dir, $username, $string) { |
| 133 | global $prefs_cache; |
| 134 | |
| 135 | cachePrefValues($data_dir, $username); |
| 136 | |
| 137 | if (isset($prefs_cache[$string])) { |
| 138 | unset($prefs_cache[$string]); |
| 139 | } |
| 140 | |
| 141 | savePrefValues($data_dir, $username); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Set a there preference $string to $value. |
| 146 | */ |
| 147 | function setPref($data_dir, $username, $string, $value) { |
| 148 | global $prefs_cache; |
| 149 | |
| 150 | cachePrefValues($data_dir, $username); |
| 151 | if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) { |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | if ($value === '') { |
| 156 | removePref($data_dir, $username, $string); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | $prefs_cache[$string] = $value; |
| 161 | savePrefValues($data_dir, $username); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Check for a preferences file. If one can not be found, create it. |
| 166 | */ |
| 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"); |
| 171 | } |
| 172 | |
| 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'; |
| 177 | |
| 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'; |
| 181 | } |
| 182 | |
| 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 ); |
| 191 | exit; |
| 192 | } else if (!@copy($default_pref, $filename)) { |
| 193 | $uid = 'httpd'; |
| 194 | if (function_exists('posix_getuid')){ |
| 195 | $user_data = posix_getpwuid(posix_getuid()); |
| 196 | $uid = $user_data['name']; |
| 197 | } |
| 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 ); |
| 204 | exit; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Write the User Signature. |
| 211 | */ |
| 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') ); |
| 218 | exit; |
| 219 | } |
| 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')); |
| 224 | exit; |
| 225 | } |
| 226 | fclose($file); |
| 227 | @copy($filename . '.tmp',$filename); |
| 228 | @unlink($filename . '.tmp'); |
| 229 | chmod($filename, 0600); |
| 230 | |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Get the signature. |
| 235 | */ |
| 236 | function getSig($data_dir, $username, $number) { |
| 237 | $filename = getHashedFile($username, $data_dir, "$username.si$number"); |
| 238 | $sig = ''; |
| 239 | if (file_exists($filename)) { |
| 240 | /* Open the file, or else display an error to the user. */ |
| 241 | if(!$file = @fopen($filename, 'r')) |
| 242 | { |
| 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) ); |
| 245 | exit; |
| 246 | } |
| 247 | while (!feof($file)) { |
| 248 | $sig .= fgets($file, 1024); |
| 249 | } |
| 250 | fclose($file); |
| 251 | } |
| 252 | return $sig; |
| 253 | } |