| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * file_prefs.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2005 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 | * @version $Id$ |
| 12 | * @package squirrelmail |
| 13 | */ |
| 14 | |
| 15 | /** include this for error messages */ |
| 16 | include_once(SM_PATH . 'functions/display_messages.php'); |
| 17 | |
| 18 | /** |
| 19 | * Check the preferences into the session cache. |
| 20 | */ |
| 21 | function cachePrefValues($data_dir, $username) { |
| 22 | global $prefs_are_cached, $prefs_cache; |
| 23 | |
| 24 | sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION ); |
| 25 | if ( isset($prefs_are_cached) && $prefs_are_cached) { |
| 26 | sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION ); |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | sqsession_unregister('prefs_cache'); |
| 31 | sqsession_unregister('prefs_are_cached'); |
| 32 | |
| 33 | /* Calculate the filename for the user's preference file */ |
| 34 | $filename = getHashedFile($username, $data_dir, "$username.pref"); |
| 35 | |
| 36 | /* A call to checkForPrefs here should take eliminate the need for */ |
| 37 | /* this to be called throughout the rest of the SquirrelMail code. */ |
| 38 | checkForPrefs($data_dir, $username, $filename); |
| 39 | |
| 40 | /* Make sure that the preference file now DOES exist. */ |
| 41 | if (!file_exists($filename)) { |
| 42 | logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) ); |
| 43 | exit; |
| 44 | } |
| 45 | |
| 46 | /* Open the file, or else display an error to the user. */ |
| 47 | if(!$file = @fopen($filename, 'r')) |
| 48 | { |
| 49 | logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) ); |
| 50 | exit; |
| 51 | } |
| 52 | |
| 53 | /* Read in the preferences. */ |
| 54 | $highlight_num = 0; |
| 55 | while (! feof($file)) { |
| 56 | $pref = ''; |
| 57 | /* keep reading a pref until we reach an eol (\n (or \r for macs)) */ |
| 58 | while($read = fgets($file, 1024)) |
| 59 | { |
| 60 | $pref .= $read; |
| 61 | if(strpos($read,"\n") || strpos($read,"\r")) |
| 62 | break; |
| 63 | } |
| 64 | $pref = trim($pref); |
| 65 | $equalsAt = strpos($pref, '='); |
| 66 | if ($equalsAt > 0) { |
| 67 | $key = substr($pref, 0, $equalsAt); |
| 68 | $value = substr($pref, $equalsAt + 1); |
| 69 | /* this is to 'rescue' old-style highlighting rules. */ |
| 70 | if (substr($key, 0, 9) == 'highlight') { |
| 71 | $key = 'highlight' . $highlight_num; |
| 72 | $highlight_num ++; |
| 73 | } |
| 74 | |
| 75 | if ($value != '') { |
| 76 | $prefs_cache[$key] = $value; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | fclose($file); |
| 81 | |
| 82 | $prefs_are_cached = TRUE; |
| 83 | |
| 84 | sqsession_register($prefs_cache, 'prefs_cache'); |
| 85 | sqsession_register($prefs_are_cached, 'prefs_are_cached'); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Return the value for the preference given by $string. |
| 90 | */ |
| 91 | function getPref($data_dir, $username, $string, $default = '') { |
| 92 | global $prefs_cache; |
| 93 | |
| 94 | $result = do_hook_function('get_pref_override',array($username,$string)); |
| 95 | if (!$result) { |
| 96 | cachePrefValues($data_dir, $username); |
| 97 | if (isset($prefs_cache[$string])) { |
| 98 | $result = $prefs_cache[$string]; |
| 99 | } else { |
| 100 | $result = do_hook_function('get_pref', array($username,$string)); |
| 101 | if (!$result) { |
| 102 | $result = $default; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | return ($result); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Save the preferences for this user. |
| 111 | */ |
| 112 | function savePrefValues($data_dir, $username) { |
| 113 | global $prefs_cache; |
| 114 | |
| 115 | $filename = getHashedFile($username, $data_dir, "$username.pref"); |
| 116 | |
| 117 | /* Open the file for writing, or else display an error to the user. */ |
| 118 | if(!$file = @fopen($filename.'.tmp', 'w')) |
| 119 | { |
| 120 | logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') ); |
| 121 | exit; |
| 122 | } |
| 123 | foreach ($prefs_cache as $Key => $Value) { |
| 124 | if (isset($Value)) { |
| 125 | if ( sq_fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) { |
| 126 | logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') ); |
| 127 | exit; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | fclose($file); |
| 132 | if (! @copy($filename . '.tmp',$filename) ) { |
| 133 | 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') ); |
| 134 | exit; |
| 135 | } |
| 136 | @unlink($filename . '.tmp'); |
| 137 | @chmod($filename, 0600); |
| 138 | sqsession_register($prefs_cache , 'prefs_cache'); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Remove a preference for the current user. |
| 143 | */ |
| 144 | function removePref($data_dir, $username, $string) { |
| 145 | global $prefs_cache; |
| 146 | |
| 147 | cachePrefValues($data_dir, $username); |
| 148 | |
| 149 | if (isset($prefs_cache[$string])) { |
| 150 | unset($prefs_cache[$string]); |
| 151 | } |
| 152 | |
| 153 | savePrefValues($data_dir, $username); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Set a there preference $string to $value. |
| 158 | */ |
| 159 | function setPref($data_dir, $username, $string, $value) { |
| 160 | global $prefs_cache; |
| 161 | |
| 162 | cachePrefValues($data_dir, $username); |
| 163 | if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | if ($value === '') { |
| 168 | removePref($data_dir, $username, $string); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | $prefs_cache[$string] = $value; |
| 173 | savePrefValues($data_dir, $username); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Check for a preferences file. If one can not be found, create it. |
| 178 | */ |
| 179 | function checkForPrefs($data_dir, $username, $filename = '') { |
| 180 | /* First, make sure we have the filename. */ |
| 181 | if ($filename == '') { |
| 182 | $filename = getHashedFile($username, $data_dir, "$username.pref"); |
| 183 | } |
| 184 | |
| 185 | /* Then, check if the file exists. */ |
| 186 | if (!@file_exists($filename) ) { |
| 187 | /* First, check the $data_dir for the default preference file. */ |
| 188 | if(substr($data_dir,-1) != '/') { |
| 189 | $data_dir .= '/'; |
| 190 | } |
| 191 | $default_pref = $data_dir . 'default_pref'; |
| 192 | |
| 193 | /* If it is not there, check the internal data directory. */ |
| 194 | if (!@file_exists($default_pref)) { |
| 195 | $default_pref = SM_PATH . 'data/default_pref'; |
| 196 | } |
| 197 | |
| 198 | /* Otherwise, report an error. */ |
| 199 | $errTitle = sprintf( _("Error opening %s"), $default_pref ); |
| 200 | if (!is_readable($default_pref)) { |
| 201 | $errString = $errTitle . "<br />\n" . |
| 202 | _("Default preference file not found or not readable!") . "<br />\n" . |
| 203 | _("Please contact your system administrator and report this error.") . "<br />\n"; |
| 204 | logout_error( $errString, $errTitle ); |
| 205 | exit; |
| 206 | } else if (!@copy($default_pref, $filename)) { |
| 207 | $uid = 'httpd'; |
| 208 | if (function_exists('posix_getuid')){ |
| 209 | $user_data = posix_getpwuid(posix_getuid()); |
| 210 | $uid = $user_data['name']; |
| 211 | } |
| 212 | $errString = $errTitle . '<br />' . |
| 213 | _("Could not create initial preference file!") . "<br />\n" . |
| 214 | sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) . |
| 215 | "<br />\n" . _("Please contact your system administrator and report this error.") . "<br />\n"; |
| 216 | logout_error( $errString, $errTitle ); |
| 217 | exit; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Write the User Signature. |
| 224 | */ |
| 225 | function setSig($data_dir, $username, $number, $value) { |
| 226 | // Limit signature size to 64KB (database BLOB limit) |
| 227 | if (strlen($value)>65536) { |
| 228 | error_option_save(_("Signature is too big.")); |
| 229 | return; |
| 230 | } |
| 231 | $filename = getHashedFile($username, $data_dir, "$username.si$number"); |
| 232 | /* Open the file for writing, or else display an error to the user. */ |
| 233 | if(!$file = @fopen("$filename.tmp", 'w')) { |
| 234 | logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') ); |
| 235 | exit; |
| 236 | } |
| 237 | if ( sq_fwrite($file, $value) === FALSE ) { |
| 238 | logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp')); |
| 239 | exit; |
| 240 | } |
| 241 | fclose($file); |
| 242 | if (! @copy($filename . '.tmp',$filename) ) { |
| 243 | 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') ); |
| 244 | exit; |
| 245 | } |
| 246 | @unlink($filename . '.tmp'); |
| 247 | @chmod($filename, 0600); |
| 248 | |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Get the signature. |
| 253 | */ |
| 254 | function getSig($data_dir, $username, $number) { |
| 255 | $filename = getHashedFile($username, $data_dir, "$username.si$number"); |
| 256 | $sig = ''; |
| 257 | if (file_exists($filename)) { |
| 258 | /* Open the file, or else display an error to the user. */ |
| 259 | if(!$file = @fopen($filename, 'r')) |
| 260 | { |
| 261 | logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) ); |
| 262 | exit; |
| 263 | } |
| 264 | while (!feof($file)) { |
| 265 | $sig .= fgets($file, 1024); |
| 266 | } |
| 267 | fclose($file); |
| 268 | } |
| 269 | return $sig; |
| 270 | } |
| 271 | |
| 272 | // vim: et ts=4 |
| 273 | ?> |