| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| 5 | | program/include/rcmail.php | |
|---|
| 6 | | | |
|---|
| 7 | | This file is part of the RoundCube Webmail client | |
|---|
| 8 | | Copyright (C) 2008, RoundCube Dev. - Switzerland | |
|---|
| 9 | | Licensed under the GNU GPL | |
|---|
| 10 | | | |
|---|
| 11 | | PURPOSE: | |
|---|
| 12 | | Application class providing core functions and holding | |
|---|
| 13 | | instances of all 'global' objects like db- and imap-connections | |
|---|
| 14 | +-----------------------------------------------------------------------+ |
|---|
| 15 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 16 | +-----------------------------------------------------------------------+ |
|---|
| 17 | |
|---|
| 18 | $Id: rcube_browser.php 328 2006-08-30 17:41:21Z thomasb $ |
|---|
| 19 | |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * Application class of RoundCube Webmail |
|---|
| 25 | * implemented as singleton |
|---|
| 26 | * |
|---|
| 27 | * @package Core |
|---|
| 28 | */ |
|---|
| 29 | class rcmail |
|---|
| 30 | { |
|---|
| 31 | static public $main_tasks = array('mail','settings','addressbook','login','logout'); |
|---|
| 32 | |
|---|
| 33 | static private $instance; |
|---|
| 34 | |
|---|
| 35 | public $config; |
|---|
| 36 | public $user; |
|---|
| 37 | public $db; |
|---|
| 38 | public $imap; |
|---|
| 39 | public $output; |
|---|
| 40 | public $task = 'mail'; |
|---|
| 41 | public $action = ''; |
|---|
| 42 | public $comm_path = './'; |
|---|
| 43 | |
|---|
| 44 | private $texts; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * This implements the 'singleton' design pattern |
|---|
| 49 | * |
|---|
| 50 | * @return object qvert The one and only instance |
|---|
| 51 | */ |
|---|
| 52 | static function get_instance() |
|---|
| 53 | { |
|---|
| 54 | if (!self::$instance) { |
|---|
| 55 | self::$instance = new rcmail(); |
|---|
| 56 | self::$instance->startup(); // init AFTER object was linked with self::$instance |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | return self::$instance; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Private constructor |
|---|
| 65 | */ |
|---|
| 66 | private function __construct() |
|---|
| 67 | { |
|---|
| 68 | // load configuration |
|---|
| 69 | $this->config = new rcube_config(); |
|---|
| 70 | |
|---|
| 71 | register_shutdown_function(array($this, 'shutdown')); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * Initial startup function |
|---|
| 77 | * to register session, create database and imap connections |
|---|
| 78 | * |
|---|
| 79 | * @todo Remove global vars $DB, $USER |
|---|
| 80 | */ |
|---|
| 81 | private function startup() |
|---|
| 82 | { |
|---|
| 83 | $config_all = $this->config->all(); |
|---|
| 84 | |
|---|
| 85 | // set task and action properties |
|---|
| 86 | $this->set_task(strip_quotes(get_input_value('_task', RCUBE_INPUT_GPC))); |
|---|
| 87 | $this->action = asciiwords(get_input_value('_action', RCUBE_INPUT_GPC)); |
|---|
| 88 | |
|---|
| 89 | // connect to database |
|---|
| 90 | $GLOBALS['DB'] = $this->get_dbh(); |
|---|
| 91 | |
|---|
| 92 | // use database for storing session data |
|---|
| 93 | include_once('include/session.inc'); |
|---|
| 94 | |
|---|
| 95 | // set session domain |
|---|
| 96 | if (!empty($config_all['session_domain'])) { |
|---|
| 97 | ini_set('session.cookie_domain', $config_all['session_domain']); |
|---|
| 98 | } |
|---|
| 99 | // set session garbage collecting time according to session_lifetime |
|---|
| 100 | if (!empty($config_all['session_lifetime'])) { |
|---|
| 101 | ini_set('session.gc_maxlifetime', ($config_all['session_lifetime']) * 120); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | // start PHP session (if not in CLI mode) |
|---|
| 105 | if ($_SERVER['REMOTE_ADDR']) |
|---|
| 106 | session_start(); |
|---|
| 107 | |
|---|
| 108 | // set initial session vars |
|---|
| 109 | if (!isset($_SESSION['auth_time'])) { |
|---|
| 110 | $_SESSION['auth_time'] = time(); |
|---|
| 111 | $_SESSION['temp'] = true; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | // create user object |
|---|
| 116 | $this->set_user(new rcube_user($_SESSION['user_id'])); |
|---|
| 117 | |
|---|
| 118 | // reset some session parameters when changing task |
|---|
| 119 | if ($_SESSION['task'] != $this->task) |
|---|
| 120 | unset($_SESSION['page']); |
|---|
| 121 | |
|---|
| 122 | // set current task to session |
|---|
| 123 | $_SESSION['task'] = $this->task; |
|---|
| 124 | |
|---|
| 125 | // create IMAP object |
|---|
| 126 | if ($this->task == 'mail') |
|---|
| 127 | $this->imap_init(); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * Setter for application task |
|---|
| 133 | * |
|---|
| 134 | * @param string Task to set |
|---|
| 135 | */ |
|---|
| 136 | public function set_task($task) |
|---|
| 137 | { |
|---|
| 138 | if (!in_array($task, self::$main_tasks)) |
|---|
| 139 | $task = 'mail'; |
|---|
| 140 | |
|---|
| 141 | $this->task = $task; |
|---|
| 142 | $this->comm_path = $this->url(array('task' => $task)); |
|---|
| 143 | |
|---|
| 144 | if ($this->output) |
|---|
| 145 | $this->output->set_env('task', $task); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | /** |
|---|
| 150 | * Setter for system user object |
|---|
| 151 | * |
|---|
| 152 | * @param object rcube_user Current user instance |
|---|
| 153 | */ |
|---|
| 154 | public function set_user($user) |
|---|
| 155 | { |
|---|
| 156 | if (is_object($user)) { |
|---|
| 157 | $this->user = $user; |
|---|
| 158 | $GLOBALS['USER'] = $this->user; |
|---|
| 159 | |
|---|
| 160 | // overwrite config with user preferences |
|---|
| 161 | $this->config->merge((array)$this->user->get_prefs()); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | $_SESSION['language'] = $this->user->language = $this->language_prop($this->config->get('language')); |
|---|
| 165 | |
|---|
| 166 | // set localization |
|---|
| 167 | setlocale(LC_ALL, $_SESSION['language'] . '.utf8'); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | /** |
|---|
| 172 | * Check the given string and return a valid language code |
|---|
| 173 | * |
|---|
| 174 | * @param string Language code |
|---|
| 175 | * @return string Valid language code |
|---|
| 176 | */ |
|---|
| 177 | private function language_prop($lang) |
|---|
| 178 | { |
|---|
| 179 | static $rcube_languages, $rcube_language_aliases; |
|---|
| 180 | |
|---|
| 181 | if (empty($rcube_languages)) { |
|---|
| 182 | @include(INSTALL_PATH . 'program/localization/index.inc'); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | // check if we have an alias for that language |
|---|
| 186 | if (!isset($rcube_languages[$lang]) && isset($rcube_language_aliases[$lang])) { |
|---|
| 187 | $lang = $rcube_language_aliases[$lang]; |
|---|
| 188 | } |
|---|
| 189 | // try the first two chars |
|---|
| 190 | else if (!isset($rcube_languages[$lang])) { |
|---|
| 191 | $short = substr($lang, 0, 2); |
|---|
| 192 | |
|---|
| 193 | // check if we have an alias for the short language code |
|---|
| 194 | if (!isset($rcube_languages[$short]) && isset($rcube_language_aliases[$short])) { |
|---|
| 195 | $lang = $rcube_language_aliases[$short]; |
|---|
| 196 | } |
|---|
| 197 | // expand 'nn' to 'nn_NN' |
|---|
| 198 | else if (!isset($rcube_languages[$short])) { |
|---|
| 199 | $lang = $short.'_'.strtoupper($short); |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | if (!isset($rcube_languages[$lang]) || !is_dir(INSTALL_PATH . 'program/localization/' . $lang)) { |
|---|
| 204 | $lang = 'en_US'; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | return $lang; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | /** |
|---|
| 212 | * Get the current database connection |
|---|
| 213 | * |
|---|
| 214 | * @return object rcube_db Database connection object |
|---|
| 215 | */ |
|---|
| 216 | public function get_dbh() |
|---|
| 217 | { |
|---|
| 218 | if (!$this->db) { |
|---|
| 219 | $dbclass = "rcube_" . $this->config->get('db_backend', 'mdb2'); |
|---|
| 220 | $config_all = $this->config->all(); |
|---|
| 221 | |
|---|
| 222 | $this->db = new $dbclass($config_all['db_dsnw'], $config_all['db_dsnr'], $config_all['db_persistent']); |
|---|
| 223 | $this->db->sqlite_initials = INSTALL_PATH . 'SQL/sqlite.initial.sql'; |
|---|
| 224 | $this->db->set_debug((bool)$config_all['sql_debug']); |
|---|
| 225 | $this->db->db_connect('w'); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | return $this->db; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | /** |
|---|
| 233 | * Init output object for GUI and add common scripts. |
|---|
| 234 | * This will instantiate a rcmail_template object and set |
|---|
| 235 | * environment vars according to the current session and configuration |
|---|
| 236 | * |
|---|
| 237 | * @param boolean True if this request is loaded in a (i)frame |
|---|
| 238 | * @return object rcube_template Reference to HTML output object |
|---|
| 239 | */ |
|---|
| 240 | public function load_gui($framed = false) |
|---|
| 241 | { |
|---|
| 242 | // init output page |
|---|
| 243 | if (!($this->output instanceof rcube_template)) |
|---|
| 244 | $this->output = new rcube_template($this->task, $framed); |
|---|
| 245 | |
|---|
| 246 | foreach (array('flag_for_deletion') as $js_config_var) { |
|---|
| 247 | $this->output->set_env($js_config_var, $this->config->get($js_config_var)); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | if ($framed) { |
|---|
| 251 | $this->comm_path .= '&_framed=1'; |
|---|
| 252 | $this->output->set_env('framed', true); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | $this->output->set_env('task', $this->task); |
|---|
| 256 | $this->output->set_env('action', $this->action); |
|---|
| 257 | $this->output->set_env('comm_path', $this->comm_path); |
|---|
| 258 | $this->output->set_charset($this->config->get('charset', RCMAIL_CHARSET)); |
|---|
| 259 | |
|---|
| 260 | // add some basic label to client |
|---|
| 261 | $this->output->add_label('loading'); |
|---|
| 262 | |
|---|
| 263 | return $this->output; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | /** |
|---|
| 268 | * Create an output object for JSON responses |
|---|
| 269 | * |
|---|
| 270 | * @return object rcube_json_output Reference to JSON output object |
|---|
| 271 | */ |
|---|
| 272 | public function init_json() |
|---|
| 273 | { |
|---|
| 274 | if (!($this->output instanceof rcube_json_output)) |
|---|
| 275 | $this->output = new rcube_json_output($this->task); |
|---|
| 276 | |
|---|
| 277 | return $this->output; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | /** |
|---|
| 282 | * Create global IMAP object and connect to server |
|---|
| 283 | * |
|---|
| 284 | * @param boolean True if connection should be established |
|---|
| 285 | * @todo Remove global $IMAP |
|---|
| 286 | */ |
|---|
| 287 | public function imap_init($connect = false) |
|---|
| 288 | { |
|---|
| 289 | $this->imap = new rcube_imap($this->db); |
|---|
| 290 | $this->imap->debug_level = $this->config->get('debug_level'); |
|---|
| 291 | $this->imap->skip_deleted = $this->config->get('skip_deleted'); |
|---|
| 292 | |
|---|
| 293 | // enable caching of imap data |
|---|
| 294 | if ($this->config->get('enable_caching')) { |
|---|
| 295 | $this->imap->set_caching(true); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | // set pagesize from config |
|---|
| 299 | $this->imap->set_pagesize($this->config->get('pagesize', 50)); |
|---|
| 300 | |
|---|
| 301 | // set global object for backward compatibility |
|---|
| 302 | $GLOBALS['IMAP'] = $this->imap; |
|---|
| 303 | |
|---|
| 304 | if ($connect) |
|---|
| 305 | $this->imap_connect(); |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | /** |
|---|
| 310 | * Connect to IMAP server with stored session data |
|---|
| 311 | * |
|---|
| 312 | * @return bool True on success, false on error |
|---|
| 313 | */ |
|---|
| 314 | public function imap_connect() |
|---|
| 315 | { |
|---|
| 316 | $conn = false; |
|---|
| 317 | |
|---|
| 318 | if ($_SESSION['imap_host'] && !$this->imap->conn) { |
|---|
| 319 | if (!($conn = $this->imap->connect($_SESSION['imap_host'], $_SESSION['username'], $this->decrypt_passwd($_SESSION['password']), $_SESSION['imap_port'], $_SESSION['imap_ssl'], rcmail::get_instance()->config->get('imap_auth_type', 'check')))) { |
|---|
| 320 | if ($this->output) |
|---|
| 321 | $this->output->show_message($this->imap->error_code == -1 ? 'imaperror' : 'sessionerror', 'error'); |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | $this->set_imap_prop(); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | return $conn; |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | /** |
|---|
| 332 | * Perfom login to the IMAP server and to the webmail service. |
|---|
| 333 | * This will also create a new user entry if auto_create_user is configured. |
|---|
| 334 | * |
|---|
| 335 | * @param string IMAP user name |
|---|
| 336 | * @param string IMAP password |
|---|
| 337 | * @param string IMAP host |
|---|
| 338 | * @return boolean True on success, False on failure |
|---|
| 339 | */ |
|---|
| 340 | function login($username, $pass, $host=NULL) |
|---|
| 341 | { |
|---|
| 342 | $user = NULL; |
|---|
| 343 | $config = $this->config->all(); |
|---|
| 344 | |
|---|
| 345 | if (!$host) |
|---|
| 346 | $host = $config['default_host']; |
|---|
| 347 | |
|---|
| 348 | // Validate that selected host is in the list of configured hosts |
|---|
| 349 | if (is_array($config['default_host'])) { |
|---|
| 350 | $allowed = false; |
|---|
| 351 | foreach ($config['default_host'] as $key => $host_allowed) { |
|---|
| 352 | if (!is_numeric($key)) |
|---|
| 353 | $host_allowed = $key; |
|---|
| 354 | if ($host == $host_allowed) { |
|---|
| 355 | $allowed = true; |
|---|
| 356 | break; |
|---|
| 357 | } |
|---|
| 358 | } |
|---|
| 359 | if (!$allowed) |
|---|
| 360 | return false; |
|---|
| 361 | } |
|---|
| 362 | else if (!empty($config['default_host']) && $host != $config['default_host']) |
|---|
| 363 | return false; |
|---|
| 364 | |
|---|
| 365 | // parse $host URL |
|---|
| 366 | $a_host = parse_url($host); |
|---|
| 367 | if ($a_host['host']) { |
|---|
| 368 | $host = $a_host['host']; |
|---|
| 369 | $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? $a_host['scheme'] : null; |
|---|
| 370 | $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : $config['default_port']); |
|---|
| 371 | } |
|---|
| 372 | else |
|---|
| 373 | $imap_port = $config['default_port']; |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | /* Modify username with domain if required |
|---|
| 377 | Inspired by Marco <P0L0_notspam_binware.org> |
|---|
| 378 | */ |
|---|
| 379 | // Check if we need to add domain |
|---|
| 380 | if (!empty($config['username_domain']) && !strpos($username, '@')) { |
|---|
| 381 | if (is_array($config['username_domain']) && isset($config['username_domain'][$host])) |
|---|
| 382 | $username .= '@'.$config['username_domain'][$host]; |
|---|
| 383 | else if (is_string($config['username_domain'])) |
|---|
| 384 | $username .= '@'.$config['username_domain']; |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | // try to resolve email address from virtuser table |
|---|
| 388 | if (!empty($config['virtuser_file']) && strpos($username, '@')) |
|---|
| 389 | $username = rcube_user::email2user($username); |
|---|
| 390 | |
|---|
| 391 | // lowercase username if it's an e-mail address (#1484473) |
|---|
| 392 | if (strpos($username, '@')) |
|---|
| 393 | $username = strtolower($username); |
|---|
| 394 | |
|---|
| 395 | // user already registered -> overwrite username |
|---|
| 396 | if ($user = rcube_user::query($username, $host)) |
|---|
| 397 | $username = $user->data['username']; |
|---|
| 398 | |
|---|
| 399 | // exit if IMAP login failed |
|---|
| 400 | if (!($imap_login = $this->imap->connect($host, $username, $pass, $imap_port, $imap_ssl, $config['imap_auth_type']))) |
|---|
| 401 | return false; |
|---|
| 402 | |
|---|
| 403 | // user already registered -> update user's record |
|---|
| 404 | if (is_object($user)) { |
|---|
| 405 | $user->touch(); |
|---|
| 406 | } |
|---|
| 407 | // create new system user |
|---|
| 408 | else if ($config['auto_create_user']) { |
|---|
| 409 | if ($created = rcube_user::create($username, $host)) { |
|---|
| 410 | $user = $created; |
|---|
| 411 | |
|---|
| 412 | // get existing mailboxes (but why?) |
|---|
| 413 | // $a_mailboxes = $this->imap->list_mailboxes(); |
|---|
| 414 | } |
|---|
| 415 | } |
|---|
| 416 | else { |
|---|
| 417 | raise_error(array( |
|---|
| 418 | 'code' => 600, |
|---|
| 419 | 'type' => 'php', |
|---|
| 420 | 'file' => "config/main.inc.php", |
|---|
| 421 | 'message' => "Acces denied for new user $username. 'auto_create_user' is disabled" |
|---|
| 422 | ), true, false); |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | // login succeeded |
|---|
| 426 | if (is_object($user) && $user->ID) { |
|---|
| 427 | $this->set_user($user); |
|---|
| 428 | |
|---|
| 429 | // set session vars |
|---|
| 430 | $_SESSION['user_id'] = $user->ID; |
|---|
| 431 | $_SESSION['username'] = $user->data['username']; |
|---|
| 432 | $_SESSION['imap_host'] = $host; |
|---|
| 433 | $_SESSION['imap_port'] = $imap_port; |
|---|
| 434 | $_SESSION['imap_ssl'] = $imap_ssl; |
|---|
| 435 | $_SESSION['password'] = $this->encrypt_passwd($pass); |
|---|
| 436 | $_SESSION['login_time'] = mktime(); |
|---|
| 437 | |
|---|
| 438 | // force reloading complete list of subscribed mailboxes |
|---|
| 439 | $this->set_imap_prop(); |
|---|
| 440 | $this->imap->clear_cache('mailboxes'); |
|---|
| 441 | |
|---|
| 442 | if ($config['create_default_folders']) |
|---|
| 443 | $this->imap->create_default_folders(); |
|---|
| 444 | |
|---|
| 445 | return true; |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | return false; |
|---|
| 449 | } |
|---|
| 450 | |
|---|
| 451 | |
|---|
| 452 | /** |
|---|
| 453 | * Set root dir and last stored mailbox |
|---|
| 454 | * This must be done AFTER connecting to the server! |
|---|
| 455 | */ |
|---|
| 456 | public function set_imap_prop() |
|---|
| 457 | { |
|---|
| 458 | $this->imap->set_charset($this->config->get('default_charset', RCMAIL_CHARSET)); |
|---|
| 459 | |
|---|
| 460 | // set root dir from config |
|---|
| 461 | if ($imap_root = $this->config->get('imap_root')) { |
|---|
| 462 | $this->imap->set_rootdir($imap_root); |
|---|
| 463 | } |
|---|
| 464 | if ($default_folders = $this->config->get('default_imap_folders')) { |
|---|
| 465 | $this->imap->set_default_mailboxes($default_folders); |
|---|
| 466 | } |
|---|
| 467 | if (!empty($_SESSION['mbox'])) { |
|---|
| 468 | $this->imap->set_mailbox($_SESSION['mbox']); |
|---|
| 469 | } |
|---|
| 470 | if (isset($_SESSION['page'])) { |
|---|
| 471 | $this->imap->set_page($_SESSION['page']); |
|---|
| 472 | } |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | |
|---|
| 476 | /** |
|---|
| 477 | * Auto-select IMAP host based on the posted login information |
|---|
| 478 | * |
|---|
| 479 | * @return string Selected IMAP host |
|---|
| 480 | */ |
|---|
| 481 | public function autoselect_host() |
|---|
| 482 | { |
|---|
| 483 | $default_host = $this->config->get('default_host'); |
|---|
| 484 | $host = null; |
|---|
| 485 | |
|---|
| 486 | if (is_array($default_host)) { |
|---|
| 487 | $post_host = get_input_value('_host', RCUBE_INPUT_POST); |
|---|
| 488 | |
|---|
| 489 | // direct match in default_host array |
|---|
| 490 | if ($default_host[$post_host] || in_array($post_host, array_values($default_host))) { |
|---|
| 491 | $host = $post_host; |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | // try to select host by mail domain |
|---|
| 495 | list($user, $domain) = explode('@', get_input_value('_user', RCUBE_INPUT_POST)); |
|---|
| 496 | if (!empty($domain)) { |
|---|
| 497 | foreach ($default_host as $imap_host => $mail_domains) { |
|---|
| 498 | if (is_array($mail_domains) && in_array($domain, $mail_domains)) { |
|---|
| 499 | $host = $imap_host; |
|---|
| 500 | break; |
|---|
| 501 | } |
|---|
| 502 | } |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | // take the first entry if $host is still an array |
|---|
| 506 | if (empty($host)) { |
|---|
| 507 | $host = array_shift($default_host); |
|---|
| 508 | } |
|---|
| 509 | } |
|---|
| 510 | else if (empty($default_host)) { |
|---|
| 511 | $host = get_input_value('_host', RCUBE_INPUT_POST); |
|---|
| 512 | } |
|---|
| 513 | else |
|---|
| 514 | $host = $default_host; |
|---|
| 515 | |
|---|
| 516 | return $host; |
|---|
| 517 | } |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | /** |
|---|
| 521 | * Get localized text in the desired language |
|---|
| 522 | * |
|---|
| 523 | * @param mixed Named parameters array or label name |
|---|
| 524 | * @return string Localized text |
|---|
| 525 | */ |
|---|
| 526 | public function gettext($attrib) |
|---|
| 527 | { |
|---|
| 528 | // load localization files if not done yet |
|---|
| 529 | if (empty($this->texts)) |
|---|
| 530 | $this->load_language(); |
|---|
| 531 | |
|---|
| 532 | // extract attributes |
|---|
| 533 | if (is_string($attrib)) |
|---|
| 534 | $attrib = array('name' => $attrib); |
|---|
| 535 | |
|---|
| 536 | $nr = is_numeric($attrib['nr']) ? $attrib['nr'] : 1; |
|---|
| 537 | $vars = isset($attrib['vars']) ? $attrib['vars'] : ''; |
|---|
| 538 | |
|---|
| 539 | $command_name = !empty($attrib['command']) ? $attrib['command'] : NULL; |
|---|
| 540 | $alias = $attrib['name'] ? $attrib['name'] : ($command_name && $command_label_map[$command_name] ? $command_label_map[$command_name] : ''); |
|---|
| 541 | |
|---|
| 542 | // text does not exist |
|---|
| 543 | if (!($text_item = $this->texts[$alias])) { |
|---|
| 544 | /* |
|---|
| 545 | raise_error(array( |
|---|
| 546 | 'code' => 500, |
|---|
| 547 | 'type' => 'php', |
|---|
| 548 | 'line' => __LINE__, |
|---|
| 549 | 'file' => __FILE__, |
|---|
| 550 | 'message' => "Missing localized text for '$alias' in '$sess_user_lang'"), TRUE, FALSE); |
|---|
| 551 | */ |
|---|
| 552 | return "[$alias]"; |
|---|
| 553 | } |
|---|
| 554 | |
|---|
| 555 | // make text item array |
|---|
| 556 | $a_text_item = is_array($text_item) ? $text_item : array('single' => $text_item); |
|---|
| 557 | |
|---|
| 558 | // decide which text to use |
|---|
| 559 | if ($nr == 1) { |
|---|
| 560 | $text = $a_text_item['single']; |
|---|
| 561 | } |
|---|
| 562 | else if ($nr > 0) { |
|---|
| 563 | $text = $a_text_item['multiple']; |
|---|
| 564 | } |
|---|
| 565 | else if ($nr == 0) { |
|---|
| 566 | if ($a_text_item['none']) |
|---|
| 567 | $text = $a_text_item['none']; |
|---|
| 568 | else if ($a_text_item['single']) |
|---|
| 569 | $text = $a_text_item['single']; |
|---|
| 570 | else if ($a_text_item['multiple']) |
|---|
| 571 | $text = $a_text_item['multiple']; |
|---|
| 572 | } |
|---|
| 573 | |
|---|
| 574 | // default text is single |
|---|
| 575 | if ($text == '') { |
|---|
| 576 | $text = $a_text_item['single']; |
|---|
| 577 | } |
|---|
| 578 | |
|---|
| 579 | // replace vars in text |
|---|
| 580 | if (is_array($attrib['vars'])) { |
|---|
| 581 | foreach ($attrib['vars'] as $var_key => $var_value) |
|---|
| 582 | $a_replace_vars[$var_key{0}=='$' ? substr($var_key, 1) : $var_key] = $var_value; |
|---|
| 583 | } |
|---|
| 584 | |
|---|
| 585 | if ($a_replace_vars) |
|---|
| 586 | $text = preg_replace('/\$\{?([_a-z]{1}[_a-z0-9]*)\}?/ei', '$a_replace_vars["\1"]', $text); |
|---|
| 587 | |
|---|
| 588 | // format output |
|---|
| 589 | if (($attrib['uppercase'] && strtolower($attrib['uppercase']=='first')) || $attrib['ucfirst']) |
|---|
| 590 | return ucfirst($text); |
|---|
| 591 | else if ($attrib['uppercase']) |
|---|
| 592 | return strtoupper($text); |
|---|
| 593 | else if ($attrib['lowercase']) |
|---|
| 594 | return strtolower($text); |
|---|
| 595 | |
|---|
| 596 | return $text; |
|---|
| 597 | } |
|---|
| 598 | |
|---|
| 599 | |
|---|
| 600 | /** |
|---|
| 601 | * Load a localization package |
|---|
| 602 | * |
|---|
| 603 | * @param string Language ID |
|---|
| 604 | */ |
|---|
| 605 | public function load_language($lang = null) |
|---|
| 606 | { |
|---|
| 607 | $lang = $lang ? $this->language_prop($lang) : $_SESSION['language']; |
|---|
| 608 | |
|---|
| 609 | // load localized texts |
|---|
| 610 | if (empty($this->texts) || $lang != $_SESSION['language']) { |
|---|
| 611 | $this->texts = array(); |
|---|
| 612 | |
|---|
| 613 | // get english labels (these should be complete) |
|---|
| 614 | @include(INSTALL_PATH . 'program/localization/en_US/labels.inc'); |
|---|
| 615 | @include(INSTALL_PATH . 'program/localization/en_US/messages.inc'); |
|---|
| 616 | |
|---|
| 617 | if (is_array($labels)) |
|---|
| 618 | $this->texts = $labels; |
|---|
| 619 | if (is_array($messages)) |
|---|
| 620 | $this->texts = array_merge($this->texts, $messages); |
|---|
| 621 | |
|---|
| 622 | // include user language files |
|---|
| 623 | if ($lang != 'en' && is_dir(INSTALL_PATH . 'program/localization/' . $lang)) { |
|---|
| 624 | include_once(INSTALL_PATH . 'program/localization/' . $lang . '/labels.inc'); |
|---|
| 625 | include_once(INSTALL_PATH . 'program/localization/' . $lang . '/messages.inc'); |
|---|
| 626 | |
|---|
| 627 | if (is_array($labels)) |
|---|
| 628 | $this->texts = array_merge($this->texts, $labels); |
|---|
| 629 | if (is_array($messages)) |
|---|
| 630 | $this->texts = array_merge($this->texts, $messages); |
|---|
| 631 | } |
|---|
| 632 | |
|---|
| 633 | $_SESSION['language'] = $lang; |
|---|
| 634 | } |
|---|
| 635 | } |
|---|
| 636 | |
|---|
| 637 | |
|---|
| 638 | /** |
|---|
| 639 | * Read directory program/localization and return a list of available languages |
|---|
| 640 | * |
|---|
| 641 | * @return array List of available localizations |
|---|
| 642 | */ |
|---|
| 643 | public function list_languages() |
|---|
| 644 | { |
|---|
| 645 | static $sa_languages = array(); |
|---|
| 646 | |
|---|
| 647 | if (!sizeof($sa_languages)) { |
|---|
| 648 | @include(INSTALL_PATH . 'program/localization/index.inc'); |
|---|
| 649 | |
|---|
| 650 | if ($dh = @opendir(INSTALL_PATH . 'program/localization')) { |
|---|
| 651 | while (($name = readdir($dh)) !== false) { |
|---|
| 652 | if ($name{0}=='.' || !is_dir(INSTALL_PATH . 'program/localization/' . $name)) |
|---|
| 653 | continue; |
|---|
| 654 | |
|---|
| 655 | if ($label = $rcube_languages[$name]) |
|---|
| 656 | $sa_languages[$name] = $label ? $label : $name; |
|---|
| 657 | } |
|---|
| 658 | closedir($dh); |
|---|
| 659 | } |
|---|
| 660 | } |
|---|
| 661 | |
|---|
| 662 | return $sa_languages; |
|---|
| 663 | } |
|---|
| 664 | |
|---|
| 665 | |
|---|
| 666 | /** |
|---|
| 667 | * Check the auth hash sent by the client against the local session credentials |
|---|
| 668 | * |
|---|
| 669 | * @return boolean True if valid, False if not |
|---|
| 670 | */ |
|---|
| 671 | function authenticate_session() |
|---|
| 672 | { |
|---|
| 673 | global $SESS_CLIENT_IP, $SESS_CHANGED; |
|---|
| 674 | |
|---|
| 675 | // advanced session authentication |
|---|
| 676 | if ($this->config->get('double_auth')) { |
|---|
| 677 | $now = time(); |
|---|
| 678 | $valid = ($_COOKIE['sessauth'] == $this->get_auth_hash(session_id(), $_SESSION['auth_time']) || |
|---|
| 679 | $_COOKIE['sessauth'] == $this->get_auth_hash(session_id(), $_SESSION['last_auth'])); |
|---|
| 680 | |
|---|
| 681 | // renew auth cookie every 5 minutes (only for GET requests) |
|---|
| 682 | if (!$valid || ($_SERVER['REQUEST_METHOD']!='POST' && $now - $_SESSION['auth_time'] > 300)) { |
|---|
| 683 | $_SESSION['last_auth'] = $_SESSION['auth_time']; |
|---|
| 684 | $_SESSION['auth_time'] = $now; |
|---|
| 685 | setcookie('sessauth', $this->get_auth_hash(session_id(), $now)); |
|---|
| 686 | } |
|---|
| 687 | } |
|---|
| 688 | else { |
|---|
| 689 | $valid = $this->config->get('ip_check') ? $_SERVER['REMOTE_ADDR'] == $SESS_CLIENT_IP : true; |
|---|
| 690 | } |
|---|
| 691 | |
|---|
| 692 | // check session filetime |
|---|
| 693 | $lifetime = $this->config->get('session_lifetime'); |
|---|
| 694 | if (!empty($lifetime) && isset($SESS_CHANGED) && $SESS_CHANGED + $lifetime*60 < time()) { |
|---|
| 695 | $valid = false; |
|---|
| 696 | } |
|---|
| 697 | |
|---|
| 698 | return $valid; |
|---|
| 699 | } |
|---|
| 700 | |
|---|
| 701 | |
|---|
| 702 | /** |
|---|
| 703 | * Destroy session data and remove cookie |
|---|
| 704 | */ |
|---|
| 705 | public function kill_session() |
|---|
| 706 | { |
|---|
| 707 | $user_prefs = $this->user->get_prefs(); |
|---|
| 708 | |
|---|
| 709 | if ((isset($_SESSION['sort_col']) && $_SESSION['sort_col'] != $user_prefs['message_sort_col']) || |
|---|
| 710 | (isset($_SESSION['sort_order']) && $_SESSION['sort_order'] != $user_prefs['message_sort_order'])) { |
|---|
| 711 | $this->user->save_prefs(array('message_sort_col' => $_SESSION['sort_col'], 'message_sort_order' => $_SESSION['sort_order'])); |
|---|
| 712 | } |
|---|
| 713 | |
|---|
| 714 | $_SESSION = array('language' => $USER->language, 'auth_time' => time(), 'temp' => true); |
|---|
| 715 | setcookie('sessauth', '-del-', time() - 60); |
|---|
| 716 | $this->user->reset(); |
|---|
| 717 | } |
|---|
| 718 | |
|---|
| 719 | |
|---|
| 720 | /** |
|---|
| 721 | * Do server side actions on logout |
|---|
| 722 | */ |
|---|
| 723 | public function logout_actions() |
|---|
| 724 | { |
|---|
| 725 | $config = $this->config->all(); |
|---|
| 726 | |
|---|
| 727 | // on logout action we're not connected to imap server |
|---|
| 728 | if (($config['logout_purge'] && !empty($config['trash_mbox'])) || $config['logout_expunge']) { |
|---|
| 729 | if (!$this->authenticate_session()) |
|---|
| 730 | return; |
|---|
| 731 | |
|---|
| 732 | $this->imap_init(true); |
|---|
| 733 | } |
|---|
| 734 | |
|---|
| 735 | if ($config['logout_purge'] && !empty($config['trash_mbox'])) { |
|---|
| 736 | $this->imap->clear_mailbox($config['trash_mbox']); |
|---|
| 737 | } |
|---|
| 738 | |
|---|
| 739 | if ($config['logout_expunge']) { |
|---|
| 740 | $this->imap->expunge('INBOX'); |
|---|
| 741 | } |
|---|
| 742 | } |
|---|
| 743 | |
|---|
| 744 | |
|---|
| 745 | /** |
|---|
| 746 | * Function to be executed in script shutdown |
|---|
| 747 | * Registered with register_shutdown_function() |
|---|
| 748 | */ |
|---|
| 749 | public function shutdown() |
|---|
| 750 | { |
|---|
| 751 | if (is_object($this->imap)) { |
|---|
| 752 | $this->imap->close(); |
|---|
| 753 | $this->imap->write_cache(); |
|---|
| 754 | } |
|---|
| 755 | |
|---|
| 756 | if (is_object($this->contacts)) |
|---|
| 757 | $this->contacts->close(); |
|---|
| 758 | |
|---|
| 759 | // before closing the database connection, write session data |
|---|
| 760 | if ($_SERVER['REMOTE_ADDR']) |
|---|
| 761 | session_write_close(); |
|---|
| 762 | } |
|---|
| 763 | |
|---|
| 764 | |
|---|
| 765 | /** |
|---|
| 766 | * Create unique authorization hash |
|---|
| 767 | * |
|---|
| 768 | * @param string Session ID |
|---|
| 769 | * @param int Timestamp |
|---|
| 770 | * @return string The generated auth hash |
|---|
| 771 | */ |
|---|
| 772 | private function get_auth_hash($sess_id, $ts) |
|---|
| 773 | { |
|---|
| 774 | $auth_string = sprintf('rcmail*sess%sR%s*Chk:%s;%s', |
|---|
| 775 | $sess_id, |
|---|
| 776 | $ts, |
|---|
| 777 | $this->config->get('ip_check') ? $_SERVER['REMOTE_ADDR'] : '***.***.***.***', |
|---|
| 778 | $_SERVER['HTTP_USER_AGENT']); |
|---|
| 779 | |
|---|
| 780 | if (function_exists('sha1')) |
|---|
| 781 | return sha1($auth_string); |
|---|
| 782 | else |
|---|
| 783 | return md5($auth_string); |
|---|
| 784 | } |
|---|
| 785 | |
|---|
| 786 | /** |
|---|
| 787 | * Encrypt IMAP password using DES encryption |
|---|
| 788 | * |
|---|
| 789 | * @param string Password to encrypt |
|---|
| 790 | * @return string Encryprted string |
|---|
| 791 | */ |
|---|
| 792 | public function encrypt_passwd($pass) |
|---|
| 793 | { |
|---|
| 794 | if (function_exists('mcrypt_module_open') && ($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, ""))) { |
|---|
| 795 | $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); |
|---|
| 796 | mcrypt_generic_init($td, $this->config->get_des_key(), $iv); |
|---|
| 797 | $cypher = mcrypt_generic($td, $pass); |
|---|
| 798 | mcrypt_generic_deinit($td); |
|---|
| 799 | mcrypt_module_close($td); |
|---|
| 800 | } |
|---|
| 801 | else if (function_exists('des')) { |
|---|
| 802 | $cypher = des($this->config->get_des_key(), $pass, 1, 0, NULL); |
|---|
| 803 | } |
|---|
| 804 | else { |
|---|
| 805 | $cypher = $pass; |
|---|
| 806 | |
|---|
| 807 | raise_error(array( |
|---|
| 808 | 'code' => 500, |
|---|
| 809 | 'type' => 'php', |
|---|
| 810 | 'file' => __FILE__, |
|---|
| 811 | 'message' => "Could not convert encrypt password. Make sure Mcrypt is installed or lib/des.inc is available" |
|---|
| 812 | ), true, false); |
|---|
| 813 | } |
|---|
| 814 | |
|---|
| 815 | return base64_encode($cypher); |
|---|
| 816 | } |
|---|
| 817 | |
|---|
| 818 | |
|---|
| 819 | /** |
|---|
| 820 | * Decrypt IMAP password using DES encryption |
|---|
| 821 | * |
|---|
| 822 | * @param string Encrypted password |
|---|
| 823 | * @return string Plain password |
|---|
| 824 | */ |
|---|
| 825 | public function decrypt_passwd($cypher) |
|---|
| 826 | { |
|---|
| 827 | if (function_exists('mcrypt_module_open') && ($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, ""))) { |
|---|
| 828 | $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); |
|---|
| 829 | mcrypt_generic_init($td, $this->config->get_des_key(), $iv); |
|---|
| 830 | $pass = mdecrypt_generic($td, base64_decode($cypher)); |
|---|
| 831 | mcrypt_generic_deinit($td); |
|---|
| 832 | mcrypt_module_close($td); |
|---|
| 833 | } |
|---|
| 834 | else if (function_exists('des')) { |
|---|
| 835 | $pass = des($this->config->get_des_key(), base64_decode($cypher), 0, 0, NULL); |
|---|
| 836 | } |
|---|
| 837 | else { |
|---|
| 838 | $pass = base64_decode($cypher); |
|---|
| 839 | } |
|---|
| 840 | |
|---|
| 841 | return preg_replace('/\x00/', '', $pass); |
|---|
| 842 | } |
|---|
| 843 | |
|---|
| 844 | |
|---|
| 845 | /** |
|---|
| 846 | * Build a valid URL to this instance of RoundCube |
|---|
| 847 | * |
|---|
| 848 | * @param mixed Either a string with the action or url parameters as key-value pairs |
|---|
| 849 | * @return string Valid application URL |
|---|
| 850 | */ |
|---|
| 851 | public function url($p) |
|---|
| 852 | { |
|---|
| 853 | if (!is_array($p)) |
|---|
| 854 | $p = array('_action' => @func_get_arg(0)); |
|---|
| 855 | |
|---|
| 856 | if ($p['task'] && in_array($p['task'], rcmail::$main_tasks)) |
|---|
| 857 | $url = './?_task='.$p['task']; |
|---|
| 858 | else |
|---|
| 859 | $url = $this->comm_path; |
|---|
| 860 | |
|---|
| 861 | unset($p['task']); |
|---|
| 862 | foreach ($p as $par => $val) |
|---|
| 863 | $url .= '&'.urlencode($par).'='.urlencode($val); |
|---|
| 864 | |
|---|
| 865 | return $url; |
|---|
| 866 | } |
|---|
| 867 | } |
|---|
| 868 | |
|---|
| 869 | |
|---|