| 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-2009, 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$ |
|---|
| 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','dummy'); |
|---|
| 32 | |
|---|
| 33 | static private $instance; |
|---|
| 34 | |
|---|
| 35 | public $config; |
|---|
| 36 | public $user; |
|---|
| 37 | public $db; |
|---|
| 38 | public $smtp; |
|---|
| 39 | public $imap; |
|---|
| 40 | public $output; |
|---|
| 41 | public $plugins; |
|---|
| 42 | public $task; |
|---|
| 43 | public $action = ''; |
|---|
| 44 | public $comm_path = './'; |
|---|
| 45 | |
|---|
| 46 | private $texts; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * This implements the 'singleton' design pattern |
|---|
| 51 | * |
|---|
| 52 | * @return object rcmail The one and only instance |
|---|
| 53 | */ |
|---|
| 54 | static function get_instance() |
|---|
| 55 | { |
|---|
| 56 | if (!self::$instance) { |
|---|
| 57 | self::$instance = new rcmail(); |
|---|
| 58 | self::$instance->startup(); // init AFTER object was linked with self::$instance |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | return self::$instance; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * Private constructor |
|---|
| 67 | */ |
|---|
| 68 | private function __construct() |
|---|
| 69 | { |
|---|
| 70 | // load configuration |
|---|
| 71 | $this->config = new rcube_config(); |
|---|
| 72 | |
|---|
| 73 | register_shutdown_function(array($this, 'shutdown')); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * Initial startup function |
|---|
| 79 | * to register session, create database and imap connections |
|---|
| 80 | * |
|---|
| 81 | * @todo Remove global vars $DB, $USER |
|---|
| 82 | */ |
|---|
| 83 | private function startup() |
|---|
| 84 | { |
|---|
| 85 | $config_all = $this->config->all(); |
|---|
| 86 | |
|---|
| 87 | // initialize syslog |
|---|
| 88 | if ($this->config->get('log_driver') == 'syslog') { |
|---|
| 89 | $syslog_id = $this->config->get('syslog_id', 'roundcube'); |
|---|
| 90 | $syslog_facility = $this->config->get('syslog_facility', LOG_USER); |
|---|
| 91 | openlog($syslog_id, LOG_ODELAY, $syslog_facility); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | // connect to database |
|---|
| 95 | $GLOBALS['DB'] = $this->get_dbh(); |
|---|
| 96 | |
|---|
| 97 | // use database for storing session data |
|---|
| 98 | include_once('include/session.inc'); |
|---|
| 99 | |
|---|
| 100 | // set session domain |
|---|
| 101 | if (!empty($config_all['session_domain'])) { |
|---|
| 102 | ini_set('session.cookie_domain', $config_all['session_domain']); |
|---|
| 103 | } |
|---|
| 104 | // set session garbage collecting time according to session_lifetime |
|---|
| 105 | if (!empty($config_all['session_lifetime'])) { |
|---|
| 106 | ini_set('session.gc_maxlifetime', ($config_all['session_lifetime']) * 120); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | // start PHP session (if not in CLI mode) |
|---|
| 110 | if ($_SERVER['REMOTE_ADDR']) |
|---|
| 111 | session_start(); |
|---|
| 112 | |
|---|
| 113 | // set initial session vars |
|---|
| 114 | if (!isset($_SESSION['auth_time'])) { |
|---|
| 115 | $_SESSION['auth_time'] = time(); |
|---|
| 116 | $_SESSION['temp'] = true; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | // create user object |
|---|
| 120 | $this->set_user(new rcube_user($_SESSION['user_id'])); |
|---|
| 121 | |
|---|
| 122 | // set task and action properties |
|---|
| 123 | $this->set_task(get_input_value('_task', RCUBE_INPUT_GPC)); |
|---|
| 124 | $this->action = asciiwords(get_input_value('_action', RCUBE_INPUT_GPC)); |
|---|
| 125 | |
|---|
| 126 | // reset some session parameters when changing task |
|---|
| 127 | if ($_SESSION['task'] != $this->task) |
|---|
| 128 | rcube_sess_unset('page'); |
|---|
| 129 | |
|---|
| 130 | // set current task to session |
|---|
| 131 | $_SESSION['task'] = $this->task; |
|---|
| 132 | |
|---|
| 133 | // init output class |
|---|
| 134 | if (!empty($_REQUEST['_remote'])) |
|---|
| 135 | $GLOBALS['OUTPUT'] = $this->init_json(); |
|---|
| 136 | else |
|---|
| 137 | $GLOBALS['OUTPUT'] = $this->load_gui(!empty($_REQUEST['_framed'])); |
|---|
| 138 | |
|---|
| 139 | // create plugin API and load plugins |
|---|
| 140 | $this->plugins = rcube_plugin_api::get_instance(); |
|---|
| 141 | |
|---|
| 142 | // init plugins |
|---|
| 143 | $this->plugins->init(); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | /** |
|---|
| 148 | * Setter for application task |
|---|
| 149 | * |
|---|
| 150 | * @param string Task to set |
|---|
| 151 | */ |
|---|
| 152 | public function set_task($task) |
|---|
| 153 | { |
|---|
| 154 | $task = asciiwords($task); |
|---|
| 155 | |
|---|
| 156 | if ($this->user && $this->user->ID) |
|---|
| 157 | $task = !$task || $task == 'login' ? 'mail' : $task; |
|---|
| 158 | else |
|---|
| 159 | $task = 'login'; |
|---|
| 160 | |
|---|
| 161 | $this->task = $task; |
|---|
| 162 | $this->comm_path = $this->url(array('task' => $this->task)); |
|---|
| 163 | |
|---|
| 164 | if ($this->output) |
|---|
| 165 | $this->output->set_env('task', $this->task); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | /** |
|---|
| 170 | * Setter for system user object |
|---|
| 171 | * |
|---|
| 172 | * @param object rcube_user Current user instance |
|---|
| 173 | */ |
|---|
| 174 | public function set_user($user) |
|---|
| 175 | { |
|---|
| 176 | if (is_object($user)) { |
|---|
| 177 | $this->user = $user; |
|---|
| 178 | $GLOBALS['USER'] = $this->user; |
|---|
| 179 | |
|---|
| 180 | // overwrite config with user preferences |
|---|
| 181 | $this->config->merge((array)$this->user->get_prefs()); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | $_SESSION['language'] = $this->user->language = $this->language_prop($this->config->get('language', $_SESSION['language'])); |
|---|
| 185 | |
|---|
| 186 | // set localization |
|---|
| 187 | setlocale(LC_ALL, $_SESSION['language'] . '.utf8', 'en_US.utf8'); |
|---|
| 188 | |
|---|
| 189 | // workaround for http://bugs.php.net/bug.php?id=18556 |
|---|
| 190 | if (in_array($_SESSION['language'], array('tr_TR', 'ku', 'az_AZ'))) |
|---|
| 191 | setlocale(LC_CTYPE, 'en_US' . '.utf8'); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | /** |
|---|
| 196 | * Check the given string and return a valid language code |
|---|
| 197 | * |
|---|
| 198 | * @param string Language code |
|---|
| 199 | * @return string Valid language code |
|---|
| 200 | */ |
|---|
| 201 | private function language_prop($lang) |
|---|
| 202 | { |
|---|
| 203 | static $rcube_languages, $rcube_language_aliases; |
|---|
| 204 | |
|---|
| 205 | // user HTTP_ACCEPT_LANGUAGE if no language is specified |
|---|
| 206 | if (empty($lang) || $lang == 'auto') { |
|---|
| 207 | $accept_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); |
|---|
| 208 | $lang = str_replace('-', '_', $accept_langs[0]); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | if (empty($rcube_languages)) { |
|---|
| 212 | @include(INSTALL_PATH . 'program/localization/index.inc'); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | // check if we have an alias for that language |
|---|
| 216 | if (!isset($rcube_languages[$lang]) && isset($rcube_language_aliases[$lang])) { |
|---|
| 217 | $lang = $rcube_language_aliases[$lang]; |
|---|
| 218 | } |
|---|
| 219 | // try the first two chars |
|---|
| 220 | else if (!isset($rcube_languages[$lang])) { |
|---|
| 221 | $short = substr($lang, 0, 2); |
|---|
| 222 | |
|---|
| 223 | // check if we have an alias for the short language code |
|---|
| 224 | if (!isset($rcube_languages[$short]) && isset($rcube_language_aliases[$short])) { |
|---|
| 225 | $lang = $rcube_language_aliases[$short]; |
|---|
| 226 | } |
|---|
| 227 | // expand 'nn' to 'nn_NN' |
|---|
| 228 | else if (!isset($rcube_languages[$short])) { |
|---|
| 229 | $lang = $short.'_'.strtoupper($short); |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | if (!isset($rcube_languages[$lang]) || !is_dir(INSTALL_PATH . 'program/localization/' . $lang)) { |
|---|
| 234 | $lang = 'en_US'; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | return $lang; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | /** |
|---|
| 242 | * Get the current database connection |
|---|
| 243 | * |
|---|
| 244 | * @return object rcube_mdb2 Database connection object |
|---|
| 245 | */ |
|---|
| 246 | public function get_dbh() |
|---|
| 247 | { |
|---|
| 248 | if (!$this->db) { |
|---|
| 249 | $config_all = $this->config->all(); |
|---|
| 250 | |
|---|
| 251 | $this->db = new rcube_mdb2($config_all['db_dsnw'], $config_all['db_dsnr'], $config_all['db_persistent']); |
|---|
| 252 | $this->db->sqlite_initials = INSTALL_PATH . 'SQL/sqlite.initial.sql'; |
|---|
| 253 | $this->db->set_debug((bool)$config_all['sql_debug']); |
|---|
| 254 | $this->db->db_connect('w'); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | return $this->db; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | /** |
|---|
| 262 | * Return instance of the internal address book class |
|---|
| 263 | * |
|---|
| 264 | * @param boolean True if the address book needs to be writeable |
|---|
| 265 | * @return object rcube_contacts Address book object |
|---|
| 266 | */ |
|---|
| 267 | public function get_address_book($id, $writeable = false) |
|---|
| 268 | { |
|---|
| 269 | $contacts = null; |
|---|
| 270 | $ldap_config = (array)$this->config->get('ldap_public'); |
|---|
| 271 | $abook_type = strtolower($this->config->get('address_book_type')); |
|---|
| 272 | |
|---|
| 273 | $plugin = $this->plugins->exec_hook('get_address_book', array('id' => $id, 'writeable' => $writeable)); |
|---|
| 274 | |
|---|
| 275 | // plugin returned instance of a rcube_addressbook |
|---|
| 276 | if ($plugin['instance'] instanceof rcube_addressbook) { |
|---|
| 277 | $contacts = $plugin['instance']; |
|---|
| 278 | } |
|---|
| 279 | else if ($id && $ldap_config[$id]) { |
|---|
| 280 | $contacts = new rcube_ldap($ldap_config[$id], $this->config->get('ldap_debug'), $this->config->mail_domain($_SESSION['imap_host'])); |
|---|
| 281 | } |
|---|
| 282 | else if ($id === '0') { |
|---|
| 283 | $contacts = new rcube_contacts($this->db, $this->user->ID); |
|---|
| 284 | } |
|---|
| 285 | else if ($abook_type == 'ldap') { |
|---|
| 286 | // Use the first writable LDAP address book. |
|---|
| 287 | foreach ($ldap_config as $id => $prop) { |
|---|
| 288 | if (!$writeable || $prop['writable']) { |
|---|
| 289 | $contacts = new rcube_ldap($prop, $this->config->get('ldap_debug'), $this->config->mail_domain($_SESSION['imap_host'])); |
|---|
| 290 | break; |
|---|
| 291 | } |
|---|
| 292 | } |
|---|
| 293 | } |
|---|
| 294 | else { |
|---|
| 295 | $contacts = new rcube_contacts($this->db, $this->user->ID); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | return $contacts; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | /** |
|---|
| 303 | * Init output object for GUI and add common scripts. |
|---|
| 304 | * This will instantiate a rcmail_template object and set |
|---|
| 305 | * environment vars according to the current session and configuration |
|---|
| 306 | * |
|---|
| 307 | * @param boolean True if this request is loaded in a (i)frame |
|---|
| 308 | * @return object rcube_template Reference to HTML output object |
|---|
| 309 | */ |
|---|
| 310 | public function load_gui($framed = false) |
|---|
| 311 | { |
|---|
| 312 | // init output page |
|---|
| 313 | if (!($this->output instanceof rcube_template)) |
|---|
| 314 | $this->output = new rcube_template($this->task, $framed); |
|---|
| 315 | |
|---|
| 316 | // set keep-alive/check-recent interval |
|---|
| 317 | if ($keep_alive = $this->config->get('keep_alive')) { |
|---|
| 318 | // be sure that it's less than session lifetime |
|---|
| 319 | if ($session_lifetime = $this->config->get('session_lifetime')) |
|---|
| 320 | $keep_alive = min($keep_alive, $session_lifetime * 60 - 30); |
|---|
| 321 | $this->output->set_env('keep_alive', max(60, $keep_alive)); |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | if ($framed) { |
|---|
| 325 | $this->comm_path .= '&_framed=1'; |
|---|
| 326 | $this->output->set_env('framed', true); |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | $this->output->set_env('task', $this->task); |
|---|
| 330 | $this->output->set_env('action', $this->action); |
|---|
| 331 | $this->output->set_env('comm_path', $this->comm_path); |
|---|
| 332 | $this->output->set_charset(RCMAIL_CHARSET); |
|---|
| 333 | |
|---|
| 334 | // add some basic label to client |
|---|
| 335 | $this->output->add_label('loading', 'servererror'); |
|---|
| 336 | |
|---|
| 337 | return $this->output; |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | /** |
|---|
| 342 | * Create an output object for JSON responses |
|---|
| 343 | * |
|---|
| 344 | * @return object rcube_json_output Reference to JSON output object |
|---|
| 345 | */ |
|---|
| 346 | public function init_json() |
|---|
| 347 | { |
|---|
| 348 | if (!($this->output instanceof rcube_json_output)) |
|---|
| 349 | $this->output = new rcube_json_output($this->task); |
|---|
| 350 | |
|---|
| 351 | return $this->output; |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | /** |
|---|
| 356 | * Create SMTP object and connect to server |
|---|
| 357 | * |
|---|
| 358 | * @param boolean True if connection should be established |
|---|
| 359 | */ |
|---|
| 360 | public function smtp_init($connect = false) |
|---|
| 361 | { |
|---|
| 362 | $this->smtp = new rcube_smtp(); |
|---|
| 363 | |
|---|
| 364 | if ($connect) |
|---|
| 365 | $this->smtp->connect(); |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | /** |
|---|
| 370 | * Create global IMAP object and connect to server |
|---|
| 371 | * |
|---|
| 372 | * @param boolean True if connection should be established |
|---|
| 373 | * @todo Remove global $IMAP |
|---|
| 374 | */ |
|---|
| 375 | public function imap_init($connect = false) |
|---|
| 376 | { |
|---|
| 377 | $this->imap = new rcube_imap($this->db); |
|---|
| 378 | $this->imap->debug_level = $this->config->get('debug_level'); |
|---|
| 379 | $this->imap->skip_deleted = $this->config->get('skip_deleted'); |
|---|
| 380 | $this->imap->index_sort = $this->config->get('index_sort', true); |
|---|
| 381 | |
|---|
| 382 | // enable caching of imap data |
|---|
| 383 | if ($this->config->get('enable_caching')) { |
|---|
| 384 | $this->imap->set_caching(true); |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | // set pagesize from config |
|---|
| 388 | $this->imap->set_pagesize($this->config->get('pagesize', 50)); |
|---|
| 389 | |
|---|
| 390 | // Setting root and delimiter before iil_Connect can save time detecting them |
|---|
| 391 | // using NAMESPACE and LIST |
|---|
| 392 | $options = array( |
|---|
| 393 | 'auth_method' => $this->config->get('imap_auth_type', 'check'), |
|---|
| 394 | 'delimiter' => isset($_SESSION['imap_delimiter']) ? $_SESSION['imap_delimiter'] : $this->config->get('imap_delimiter'), |
|---|
| 395 | 'rootdir' => isset($_SESSION['imap_root']) ? $_SESSION['imap_root'] : $this->config->get('imap_root'), |
|---|
| 396 | 'debug_mode' => (bool) $this->config->get('imap_debug', 0), |
|---|
| 397 | ); |
|---|
| 398 | |
|---|
| 399 | $this->imap->set_options($options); |
|---|
| 400 | |
|---|
| 401 | // set global object for backward compatibility |
|---|
| 402 | $GLOBALS['IMAP'] = $this->imap; |
|---|
| 403 | |
|---|
| 404 | $hook = $this->plugins->exec_hook('imap_init', array('fetch_headers' => $this->imap->fetch_add_headers)); |
|---|
| 405 | if ($hook['fetch_headers']) |
|---|
| 406 | $this->imap->fetch_add_headers = $hook['fetch_headers']; |
|---|
| 407 | |
|---|
| 408 | if ($connect) |
|---|
| 409 | $this->imap_connect(); |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | /** |
|---|
| 414 | * Connect to IMAP server with stored session data |
|---|
| 415 | * |
|---|
| 416 | * @return bool True on success, false on error |
|---|
| 417 | */ |
|---|
| 418 | public function imap_connect() |
|---|
| 419 | { |
|---|
| 420 | $conn = false; |
|---|
| 421 | |
|---|
| 422 | if (!$this->imap) |
|---|
| 423 | $this->imap_init(); |
|---|
| 424 | |
|---|
| 425 | if ($_SESSION['imap_host'] && !$this->imap->conn) { |
|---|
| 426 | if (!($conn = $this->imap->connect($_SESSION['imap_host'], $_SESSION['username'], $this->decrypt($_SESSION['password']), $_SESSION['imap_port'], $_SESSION['imap_ssl']))) { |
|---|
| 427 | if ($this->output) |
|---|
| 428 | $this->output->show_message($this->imap->error_code == -1 ? 'imaperror' : 'sessionerror', 'error'); |
|---|
| 429 | } |
|---|
| 430 | |
|---|
| 431 | $this->set_imap_prop(); |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | return $conn; |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | /** |
|---|
| 439 | * Perfom login to the IMAP server and to the webmail service. |
|---|
| 440 | * This will also create a new user entry if auto_create_user is configured. |
|---|
| 441 | * |
|---|
| 442 | * @param string IMAP user name |
|---|
| 443 | * @param string IMAP password |
|---|
| 444 | * @param string IMAP host |
|---|
| 445 | * @return boolean True on success, False on failure |
|---|
| 446 | */ |
|---|
| 447 | function login($username, $pass, $host=NULL) |
|---|
| 448 | { |
|---|
| 449 | $user = NULL; |
|---|
| 450 | $config = $this->config->all(); |
|---|
| 451 | |
|---|
| 452 | if (!$host) |
|---|
| 453 | $host = $config['default_host']; |
|---|
| 454 | |
|---|
| 455 | // Validate that selected host is in the list of configured hosts |
|---|
| 456 | if (is_array($config['default_host'])) { |
|---|
| 457 | $allowed = false; |
|---|
| 458 | foreach ($config['default_host'] as $key => $host_allowed) { |
|---|
| 459 | if (!is_numeric($key)) |
|---|
| 460 | $host_allowed = $key; |
|---|
| 461 | if ($host == $host_allowed) { |
|---|
| 462 | $allowed = true; |
|---|
| 463 | break; |
|---|
| 464 | } |
|---|
| 465 | } |
|---|
| 466 | if (!$allowed) |
|---|
| 467 | return false; |
|---|
| 468 | } |
|---|
| 469 | else if (!empty($config['default_host']) && $host != $config['default_host']) |
|---|
| 470 | return false; |
|---|
| 471 | |
|---|
| 472 | // parse $host URL |
|---|
| 473 | $a_host = parse_url($host); |
|---|
| 474 | if ($a_host['host']) { |
|---|
| 475 | $host = $a_host['host']; |
|---|
| 476 | $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? $a_host['scheme'] : null; |
|---|
| 477 | if(!empty($a_host['port'])) |
|---|
| 478 | $imap_port = $a_host['port']; |
|---|
| 479 | else if ($imap_ssl && $imap_ssl != 'tls') |
|---|
| 480 | $imap_port = 993; |
|---|
| 481 | } |
|---|
| 482 | |
|---|
| 483 | $imap_port = $imap_port ? $imap_port : $config['default_port']; |
|---|
| 484 | |
|---|
| 485 | /* Modify username with domain if required |
|---|
| 486 | Inspired by Marco <P0L0_notspam_binware.org> |
|---|
| 487 | */ |
|---|
| 488 | // Check if we need to add domain |
|---|
| 489 | if (!empty($config['username_domain']) && !strpos($username, '@')) { |
|---|
| 490 | if (is_array($config['username_domain']) && isset($config['username_domain'][$host])) |
|---|
| 491 | $username .= '@'.$config['username_domain'][$host]; |
|---|
| 492 | else if (is_string($config['username_domain'])) |
|---|
| 493 | $username .= '@'.$config['username_domain']; |
|---|
| 494 | } |
|---|
| 495 | |
|---|
| 496 | // try to resolve email address from virtuser table |
|---|
| 497 | if (strpos($username, '@')) |
|---|
| 498 | if ($virtuser = rcube_user::email2user($username)) |
|---|
| 499 | $username = $virtuser; |
|---|
| 500 | |
|---|
| 501 | // lowercase username if it's an e-mail address (#1484473) |
|---|
| 502 | if (strpos($username, '@')) |
|---|
| 503 | $username = mb_strtolower($username); |
|---|
| 504 | |
|---|
| 505 | // user already registered -> overwrite username |
|---|
| 506 | if ($user = rcube_user::query($username, $host)) |
|---|
| 507 | $username = $user->data['username']; |
|---|
| 508 | |
|---|
| 509 | if (!$this->imap) |
|---|
| 510 | $this->imap_init(); |
|---|
| 511 | |
|---|
| 512 | // exit if IMAP login failed |
|---|
| 513 | if (!($imap_login = $this->imap->connect($host, $username, $pass, $imap_port, $imap_ssl))) |
|---|
| 514 | return false; |
|---|
| 515 | |
|---|
| 516 | $this->set_imap_prop(); |
|---|
| 517 | |
|---|
| 518 | // user already registered -> update user's record |
|---|
| 519 | if (is_object($user)) { |
|---|
| 520 | // create default folders on first login |
|---|
| 521 | if (!$user->data['last_login'] && $config['create_default_folders']) |
|---|
| 522 | $this->imap->create_default_folders(); |
|---|
| 523 | $user->touch(); |
|---|
| 524 | } |
|---|
| 525 | // create new system user |
|---|
| 526 | else if ($config['auto_create_user']) { |
|---|
| 527 | if ($created = rcube_user::create($username, $host)) { |
|---|
| 528 | $user = $created; |
|---|
| 529 | // create default folders on first login |
|---|
| 530 | if ($config['create_default_folders']) |
|---|
| 531 | $this->imap->create_default_folders(); |
|---|
| 532 | } |
|---|
| 533 | else { |
|---|
| 534 | raise_error(array( |
|---|
| 535 | 'code' => 600, 'type' => 'php', |
|---|
| 536 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 537 | 'message' => "Failed to create a user record. Maybe aborted by a plugin?" |
|---|
| 538 | ), true, false); |
|---|
| 539 | } |
|---|
| 540 | } |
|---|
| 541 | else { |
|---|
| 542 | raise_error(array( |
|---|
| 543 | 'code' => 600, 'type' => 'php', |
|---|
| 544 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 545 | 'message' => "Acces denied for new user $username. 'auto_create_user' is disabled" |
|---|
| 546 | ), true, false); |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | // login succeeded |
|---|
| 550 | if (is_object($user) && $user->ID) { |
|---|
| 551 | $this->set_user($user); |
|---|
| 552 | |
|---|
| 553 | // set session vars |
|---|
| 554 | $_SESSION['user_id'] = $user->ID; |
|---|
| 555 | $_SESSION['username'] = $user->data['username']; |
|---|
| 556 | $_SESSION['imap_host'] = $host; |
|---|
| 557 | $_SESSION['imap_port'] = $imap_port; |
|---|
| 558 | $_SESSION['imap_ssl'] = $imap_ssl; |
|---|
| 559 | $_SESSION['password'] = $this->encrypt($pass); |
|---|
| 560 | $_SESSION['login_time'] = mktime(); |
|---|
| 561 | |
|---|
| 562 | if ($_REQUEST['_timezone'] != '_default_') |
|---|
| 563 | $_SESSION['timezone'] = floatval($_REQUEST['_timezone']); |
|---|
| 564 | |
|---|
| 565 | // force reloading complete list of subscribed mailboxes |
|---|
| 566 | $this->imap->clear_cache('mailboxes'); |
|---|
| 567 | |
|---|
| 568 | return true; |
|---|
| 569 | } |
|---|
| 570 | |
|---|
| 571 | return false; |
|---|
| 572 | } |
|---|
| 573 | |
|---|
| 574 | |
|---|
| 575 | /** |
|---|
| 576 | * Set root dir and last stored mailbox |
|---|
| 577 | * This must be done AFTER connecting to the server! |
|---|
| 578 | */ |
|---|
| 579 | public function set_imap_prop() |
|---|
| 580 | { |
|---|
| 581 | $this->imap->set_charset($this->config->get('default_charset', RCMAIL_CHARSET)); |
|---|
| 582 | |
|---|
| 583 | if ($default_folders = $this->config->get('default_imap_folders')) { |
|---|
| 584 | $this->imap->set_default_mailboxes($default_folders); |
|---|
| 585 | } |
|---|
| 586 | if (!empty($_SESSION['mbox'])) { |
|---|
| 587 | $this->imap->set_mailbox($_SESSION['mbox']); |
|---|
| 588 | } |
|---|
| 589 | if (isset($_SESSION['page'])) { |
|---|
| 590 | $this->imap->set_page($_SESSION['page']); |
|---|
| 591 | } |
|---|
| 592 | |
|---|
| 593 | // cache IMAP root and delimiter in session for performance reasons |
|---|
| 594 | $_SESSION['imap_root'] = $this->imap->root_dir; |
|---|
| 595 | $_SESSION['imap_delimiter'] = $this->imap->delimiter; |
|---|
| 596 | } |
|---|
| 597 | |
|---|
| 598 | |
|---|
| 599 | /** |
|---|
| 600 | * Auto-select IMAP host based on the posted login information |
|---|
| 601 | * |
|---|
| 602 | * @return string Selected IMAP host |
|---|
| 603 | */ |
|---|
| 604 | public function autoselect_host() |
|---|
| 605 | { |
|---|
| 606 | $default_host = $this->config->get('default_host'); |
|---|
| 607 | $host = null; |
|---|
| 608 | |
|---|
| 609 | if (is_array($default_host)) { |
|---|
| 610 | $post_host = get_input_value('_host', RCUBE_INPUT_POST); |
|---|
| 611 | |
|---|
| 612 | // direct match in default_host array |
|---|
| 613 | if ($default_host[$post_host] || in_array($post_host, array_values($default_host))) { |
|---|
| 614 | $host = $post_host; |
|---|
| 615 | } |
|---|
| 616 | |
|---|
| 617 | // try to select host by mail domain |
|---|
| 618 | list($user, $domain) = explode('@', get_input_value('_user', RCUBE_INPUT_POST)); |
|---|
| 619 | if (!empty($domain)) { |
|---|
| 620 | foreach ($default_host as $imap_host => $mail_domains) { |
|---|
| 621 | if (is_array($mail_domains) && in_array($domain, $mail_domains)) { |
|---|
| 622 | $host = $imap_host; |
|---|
| 623 | break; |
|---|
| 624 | } |
|---|
| 625 | } |
|---|
| 626 | } |
|---|
| 627 | |
|---|
| 628 | // take the first entry if $host is still an array |
|---|
| 629 | if (empty($host)) { |
|---|
| 630 | $host = array_shift($default_host); |
|---|
| 631 | } |
|---|
| 632 | } |
|---|
| 633 | else if (empty($default_host)) { |
|---|
| 634 | $host = get_input_value('_host', RCUBE_INPUT_POST); |
|---|
| 635 | } |
|---|
| 636 | else |
|---|
| 637 | $host = $default_host; |
|---|
| 638 | |
|---|
| 639 | return $host; |
|---|
| 640 | } |
|---|
| 641 | |
|---|
| 642 | |
|---|
| 643 | /** |
|---|
| 644 | * Get localized text in the desired language |
|---|
| 645 | * |
|---|
| 646 | * @param mixed Named parameters array or label name |
|---|
| 647 | * @return string Localized text |
|---|
| 648 | */ |
|---|
| 649 | public function gettext($attrib, $domain=null) |
|---|
| 650 | { |
|---|
| 651 | // load localization files if not done yet |
|---|
| 652 | if (empty($this->texts)) |
|---|
| 653 | $this->load_language(); |
|---|
| 654 | |
|---|
| 655 | // extract attributes |
|---|
| 656 | if (is_string($attrib)) |
|---|
| 657 | $attrib = array('name' => $attrib); |
|---|
| 658 | |
|---|
| 659 | $nr = is_numeric($attrib['nr']) ? $attrib['nr'] : 1; |
|---|
| 660 | $name = $attrib['name'] ? $attrib['name'] : ''; |
|---|
| 661 | |
|---|
| 662 | // check for text with domain |
|---|
| 663 | if ($domain && ($text_item = $this->texts[$domain.'.'.$name])) |
|---|
| 664 | ; |
|---|
| 665 | // text does not exist |
|---|
| 666 | else if (!($text_item = $this->texts[$name])) { |
|---|
| 667 | return "[$name]"; |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | // make text item array |
|---|
| 671 | $a_text_item = is_array($text_item) ? $text_item : array('single' => $text_item); |
|---|
| 672 | |
|---|
| 673 | // decide which text to use |
|---|
| 674 | if ($nr == 1) { |
|---|
| 675 | $text = $a_text_item['single']; |
|---|
| 676 | } |
|---|
| 677 | else if ($nr > 0) { |
|---|
| 678 | $text = $a_text_item['multiple']; |
|---|
| 679 | } |
|---|
| 680 | else if ($nr == 0) { |
|---|
| 681 | if ($a_text_item['none']) |
|---|
| 682 | $text = $a_text_item['none']; |
|---|
| 683 | else if ($a_text_item['single']) |
|---|
| 684 | $text = $a_text_item['single']; |
|---|
| 685 | else if ($a_text_item['multiple']) |
|---|
| 686 | $text = $a_text_item['multiple']; |
|---|
| 687 | } |
|---|
| 688 | |
|---|
| 689 | // default text is single |
|---|
| 690 | if ($text == '') { |
|---|
| 691 | $text = $a_text_item['single']; |
|---|
| 692 | } |
|---|
| 693 | |
|---|
| 694 | // replace vars in text |
|---|
| 695 | if (is_array($attrib['vars'])) { |
|---|
| 696 | foreach ($attrib['vars'] as $var_key => $var_value) |
|---|
| 697 | $text = str_replace($var_key[0]!='$' ? '$'.$var_key : $var_key, $var_value, $text); |
|---|
| 698 | } |
|---|
| 699 | |
|---|
| 700 | // format output |
|---|
| 701 | if (($attrib['uppercase'] && strtolower($attrib['uppercase']=='first')) || $attrib['ucfirst']) |
|---|
| 702 | return ucfirst($text); |
|---|
| 703 | else if ($attrib['uppercase']) |
|---|
| 704 | return strtoupper($text); |
|---|
| 705 | else if ($attrib['lowercase']) |
|---|
| 706 | return strtolower($text); |
|---|
| 707 | |
|---|
| 708 | return $text; |
|---|
| 709 | } |
|---|
| 710 | |
|---|
| 711 | |
|---|
| 712 | /** |
|---|
| 713 | * Load a localization package |
|---|
| 714 | * |
|---|
| 715 | * @param string Language ID |
|---|
| 716 | */ |
|---|
| 717 | public function load_language($lang = null, $add = array()) |
|---|
| 718 | { |
|---|
| 719 | $lang = $this->language_prop(($lang ? $lang : $_SESSION['language'])); |
|---|
| 720 | |
|---|
| 721 | // load localized texts |
|---|
| 722 | if (empty($this->texts) || $lang != $_SESSION['language']) { |
|---|
| 723 | $this->texts = array(); |
|---|
| 724 | |
|---|
| 725 | // get english labels (these should be complete) |
|---|
| 726 | @include(INSTALL_PATH . 'program/localization/en_US/labels.inc'); |
|---|
| 727 | @include(INSTALL_PATH . 'program/localization/en_US/messages.inc'); |
|---|
| 728 | |
|---|
| 729 | if (is_array($labels)) |
|---|
| 730 | $this->texts = $labels; |
|---|
| 731 | if (is_array($messages)) |
|---|
| 732 | $this->texts = array_merge($this->texts, $messages); |
|---|
| 733 | |
|---|
| 734 | // include user language files |
|---|
| 735 | if ($lang != 'en' && is_dir(INSTALL_PATH . 'program/localization/' . $lang)) { |
|---|
| 736 | include_once(INSTALL_PATH . 'program/localization/' . $lang . '/labels.inc'); |
|---|
| 737 | include_once(INSTALL_PATH . 'program/localization/' . $lang . '/messages.inc'); |
|---|
| 738 | |
|---|
| 739 | if (is_array($labels)) |
|---|
| 740 | $this->texts = array_merge($this->texts, $labels); |
|---|
| 741 | if (is_array($messages)) |
|---|
| 742 | $this->texts = array_merge($this->texts, $messages); |
|---|
| 743 | } |
|---|
| 744 | |
|---|
| 745 | $_SESSION['language'] = $lang; |
|---|
| 746 | } |
|---|
| 747 | |
|---|
| 748 | // append additional texts (from plugin) |
|---|
| 749 | if (is_array($add) && !empty($add)) |
|---|
| 750 | $this->texts += $add; |
|---|
| 751 | } |
|---|
| 752 | |
|---|
| 753 | |
|---|
| 754 | /** |
|---|
| 755 | * Read directory program/localization and return a list of available languages |
|---|
| 756 | * |
|---|
| 757 | * @return array List of available localizations |
|---|
| 758 | */ |
|---|
| 759 | public function list_languages() |
|---|
| 760 | { |
|---|
| 761 | static $sa_languages = array(); |
|---|
| 762 | |
|---|
| 763 | if (!sizeof($sa_languages)) { |
|---|
| 764 | @include(INSTALL_PATH . 'program/localization/index.inc'); |
|---|
| 765 | |
|---|
| 766 | if ($dh = @opendir(INSTALL_PATH . 'program/localization')) { |
|---|
| 767 | while (($name = readdir($dh)) !== false) { |
|---|
| 768 | if ($name{0}=='.' || !is_dir(INSTALL_PATH . 'program/localization/' . $name)) |
|---|
| 769 | continue; |
|---|
| 770 | |
|---|
| 771 | if ($label = $rcube_languages[$name]) |
|---|
| 772 | $sa_languages[$name] = $label; |
|---|
| 773 | } |
|---|
| 774 | closedir($dh); |
|---|
| 775 | } |
|---|
| 776 | } |
|---|
| 777 | |
|---|
| 778 | return $sa_languages; |
|---|
| 779 | } |
|---|
| 780 | |
|---|
| 781 | |
|---|
| 782 | /** |
|---|
| 783 | * Check the auth hash sent by the client against the local session credentials |
|---|
| 784 | * |
|---|
| 785 | * @return boolean True if valid, False if not |
|---|
| 786 | */ |
|---|
| 787 | function authenticate_session() |
|---|
| 788 | { |
|---|
| 789 | global $SESS_CLIENT_IP, $SESS_CHANGED; |
|---|
| 790 | |
|---|
| 791 | // advanced session authentication |
|---|
| 792 | if ($this->config->get('double_auth')) { |
|---|
| 793 | $now = time(); |
|---|
| 794 | $valid = ($_COOKIE['sessauth'] == $this->get_auth_hash(session_id(), $_SESSION['auth_time']) || |
|---|
| 795 | $_COOKIE['sessauth'] == $this->get_auth_hash(session_id(), $_SESSION['last_auth'])); |
|---|
| 796 | |
|---|
| 797 | // renew auth cookie every 5 minutes (only for GET requests) |
|---|
| 798 | if (!$valid || ($_SERVER['REQUEST_METHOD']!='POST' && $now - $_SESSION['auth_time'] > 300)) { |
|---|
| 799 | $_SESSION['last_auth'] = $_SESSION['auth_time']; |
|---|
| 800 | $_SESSION['auth_time'] = $now; |
|---|
| 801 | rcmail::setcookie('sessauth', $this->get_auth_hash(session_id(), $now), 0); |
|---|
| 802 | } |
|---|
| 803 | } |
|---|
| 804 | else { |
|---|
| 805 | $valid = $this->config->get('ip_check') ? $_SERVER['REMOTE_ADDR'] == $SESS_CLIENT_IP : true; |
|---|
| 806 | } |
|---|
| 807 | |
|---|
| 808 | // check session filetime |
|---|
| 809 | $lifetime = $this->config->get('session_lifetime'); |
|---|
| 810 | if (!empty($lifetime) && isset($SESS_CHANGED) && $SESS_CHANGED + $lifetime*60 < time()) { |
|---|
| 811 | $valid = false; |
|---|
| 812 | } |
|---|
| 813 | |
|---|
| 814 | return $valid; |
|---|
| 815 | } |
|---|
| 816 | |
|---|
| 817 | |
|---|
| 818 | /** |
|---|
| 819 | * Destroy session data and remove cookie |
|---|
| 820 | */ |
|---|
| 821 | public function kill_session() |
|---|
| 822 | { |
|---|
| 823 | $this->plugins->exec_hook('kill_session'); |
|---|
| 824 | |
|---|
| 825 | rcube_sess_unset(); |
|---|
| 826 | $_SESSION = array('language' => $this->user->language, 'auth_time' => time(), 'temp' => true); |
|---|
| 827 | rcmail::setcookie('sessauth', '-del-', time() - 60); |
|---|
| 828 | $this->user->reset(); |
|---|
| 829 | } |
|---|
| 830 | |
|---|
| 831 | |
|---|
| 832 | /** |
|---|
| 833 | * Do server side actions on logout |
|---|
| 834 | */ |
|---|
| 835 | public function logout_actions() |
|---|
| 836 | { |
|---|
| 837 | $config = $this->config->all(); |
|---|
| 838 | |
|---|
| 839 | // on logout action we're not connected to imap server |
|---|
| 840 | if (($config['logout_purge'] && !empty($config['trash_mbox'])) || $config['logout_expunge']) { |
|---|
| 841 | if (!$this->authenticate_session()) |
|---|
| 842 | return; |
|---|
| 843 | |
|---|
| 844 | $this->imap_init(true); |
|---|
| 845 | } |
|---|
| 846 | |
|---|
| 847 | if ($config['logout_purge'] && !empty($config['trash_mbox'])) { |
|---|
| 848 | $this->imap->clear_mailbox($config['trash_mbox']); |
|---|
| 849 | } |
|---|
| 850 | |
|---|
| 851 | if ($config['logout_expunge']) { |
|---|
| 852 | $this->imap->expunge('INBOX'); |
|---|
| 853 | } |
|---|
| 854 | } |
|---|
| 855 | |
|---|
| 856 | |
|---|
| 857 | /** |
|---|
| 858 | * Function to be executed in script shutdown |
|---|
| 859 | * Registered with register_shutdown_function() |
|---|
| 860 | */ |
|---|
| 861 | public function shutdown() |
|---|
| 862 | { |
|---|
| 863 | if (is_object($this->imap)) { |
|---|
| 864 | $this->imap->close(); |
|---|
| 865 | $this->imap->write_cache(); |
|---|
| 866 | } |
|---|
| 867 | |
|---|
| 868 | if (is_object($this->smtp)) |
|---|
| 869 | $this->smtp->disconnect(); |
|---|
| 870 | |
|---|
| 871 | if (is_object($this->contacts)) |
|---|
| 872 | $this->contacts->close(); |
|---|
| 873 | |
|---|
| 874 | // before closing the database connection, write session data |
|---|
| 875 | if ($_SERVER['REMOTE_ADDR']) |
|---|
| 876 | session_write_close(); |
|---|
| 877 | |
|---|
| 878 | // write performance stats to logs/console |
|---|
| 879 | if ($this->config->get('devel_mode')) { |
|---|
| 880 | if (function_exists('memory_get_usage')) |
|---|
| 881 | $mem = show_bytes(memory_get_usage()); |
|---|
| 882 | if (function_exists('memory_get_peak_usage')) |
|---|
| 883 | $mem .= '/'.show_bytes(memory_get_peak_usage()); |
|---|
| 884 | |
|---|
| 885 | $log = $this->task . ($this->action ? '/'.$this->action : '') . ($mem ? " [$mem]" : ''); |
|---|
| 886 | rcube_print_time(RCMAIL_START, $log); |
|---|
| 887 | } |
|---|
| 888 | } |
|---|
| 889 | |
|---|
| 890 | |
|---|
| 891 | /** |
|---|
| 892 | * Generate a unique token to be used in a form request |
|---|
| 893 | * |
|---|
| 894 | * @return string The request token |
|---|
| 895 | */ |
|---|
| 896 | public function get_request_token() |
|---|
| 897 | { |
|---|
| 898 | $key = $this->task; |
|---|
| 899 | |
|---|
| 900 | if (!$_SESSION['request_tokens'][$key]) |
|---|
| 901 | $_SESSION['request_tokens'][$key] = md5(uniqid($key . mt_rand(), true)); |
|---|
| 902 | |
|---|
| 903 | return $_SESSION['request_tokens'][$key]; |
|---|
| 904 | } |
|---|
| 905 | |
|---|
| 906 | |
|---|
| 907 | /** |
|---|
| 908 | * Check if the current request contains a valid token |
|---|
| 909 | * |
|---|
| 910 | * @param int Request method |
|---|
| 911 | * @return boolean True if request token is valid false if not |
|---|
| 912 | */ |
|---|
| 913 | public function check_request($mode = RCUBE_INPUT_POST) |
|---|
| 914 | { |
|---|
| 915 | $token = get_input_value('_token', $mode); |
|---|
| 916 | return !empty($token) && $_SESSION['request_tokens'][$this->task] == $token; |
|---|
| 917 | } |
|---|
| 918 | |
|---|
| 919 | |
|---|
| 920 | /** |
|---|
| 921 | * Create unique authorization hash |
|---|
| 922 | * |
|---|
| 923 | * @param string Session ID |
|---|
| 924 | * @param int Timestamp |
|---|
| 925 | * @return string The generated auth hash |
|---|
| 926 | */ |
|---|
| 927 | private function get_auth_hash($sess_id, $ts) |
|---|
| 928 | { |
|---|
| 929 | $auth_string = sprintf('rcmail*sess%sR%s*Chk:%s;%s', |
|---|
| 930 | $sess_id, |
|---|
| 931 | $ts, |
|---|
| 932 | $this->config->get('ip_check') ? $_SERVER['REMOTE_ADDR'] : '***.***.***.***', |
|---|
| 933 | $_SERVER['HTTP_USER_AGENT']); |
|---|
| 934 | |
|---|
| 935 | if (function_exists('sha1')) |
|---|
| 936 | return sha1($auth_string); |
|---|
| 937 | else |
|---|
| 938 | return md5($auth_string); |
|---|
| 939 | } |
|---|
| 940 | |
|---|
| 941 | |
|---|
| 942 | /** |
|---|
| 943 | * Encrypt using 3DES |
|---|
| 944 | * |
|---|
| 945 | * @param string $clear clear text input |
|---|
| 946 | * @param string $key encryption key to retrieve from the configuration, defaults to 'des_key' |
|---|
| 947 | * @param boolean $base64 whether or not to base64_encode() the result before returning |
|---|
| 948 | * |
|---|
| 949 | * @return string encrypted text |
|---|
| 950 | */ |
|---|
| 951 | public function encrypt($clear, $key = 'des_key', $base64 = true) |
|---|
| 952 | { |
|---|
| 953 | if (!$clear) |
|---|
| 954 | return ''; |
|---|
| 955 | /*- |
|---|
| 956 | * Add a single canary byte to the end of the clear text, which |
|---|
| 957 | * will help find out how much of padding will need to be removed |
|---|
| 958 | * upon decryption; see http://php.net/mcrypt_generic#68082 |
|---|
| 959 | */ |
|---|
| 960 | $clear = pack("a*H2", $clear, "80"); |
|---|
| 961 | |
|---|
| 962 | if (function_exists('mcrypt_module_open') && |
|---|
| 963 | ($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_CBC, ""))) |
|---|
| 964 | { |
|---|
| 965 | $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); |
|---|
| 966 | mcrypt_generic_init($td, $this->config->get_crypto_key($key), $iv); |
|---|
| 967 | $cipher = $iv . mcrypt_generic($td, $clear); |
|---|
| 968 | mcrypt_generic_deinit($td); |
|---|
| 969 | mcrypt_module_close($td); |
|---|
| 970 | } |
|---|
| 971 | else if (function_exists('des')) |
|---|
| 972 | { |
|---|
| 973 | define('DES_IV_SIZE', 8); |
|---|
| 974 | $iv = ''; |
|---|
| 975 | for ($i = 0; $i < constant('DES_IV_SIZE'); $i++) |
|---|
| 976 | $iv .= sprintf("%c", mt_rand(0, 255)); |
|---|
| 977 | $cipher = $iv . des($this->config->get_crypto_key($key), $clear, 1, 1, $iv); |
|---|
| 978 | } |
|---|
| 979 | else |
|---|
| 980 | { |
|---|
| 981 | raise_error(array( |
|---|
| 982 | 'code' => 500, 'type' => 'php', |
|---|
| 983 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 984 | 'message' => "Could not perform encryption; make sure Mcrypt is installed or lib/des.inc is available" |
|---|
| 985 | ), true, true); |
|---|
| 986 | } |
|---|
| 987 | |
|---|
| 988 | return $base64 ? base64_encode($cipher) : $cipher; |
|---|
| 989 | } |
|---|
| 990 | |
|---|
| 991 | /** |
|---|
| 992 | * Decrypt 3DES-encrypted string |
|---|
| 993 | * |
|---|
| 994 | * @param string $cipher encrypted text |
|---|
| 995 | * @param string $key encryption key to retrieve from the configuration, defaults to 'des_key' |
|---|
| 996 | * @param boolean $base64 whether or not input is base64-encoded |
|---|
| 997 | * |
|---|
| 998 | * @return string decrypted text |
|---|
| 999 | */ |
|---|
| 1000 | public function decrypt($cipher, $key = 'des_key', $base64 = true) |
|---|
| 1001 | { |
|---|
| 1002 | if (!$cipher) |
|---|
| 1003 | return ''; |
|---|
| 1004 | |
|---|
| 1005 | $cipher = $base64 ? base64_decode($cipher) : $cipher; |
|---|
| 1006 | |
|---|
| 1007 | if (function_exists('mcrypt_module_open') && |
|---|
| 1008 | ($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_CBC, ""))) |
|---|
| 1009 | { |
|---|
| 1010 | $iv = substr($cipher, 0, mcrypt_enc_get_iv_size($td)); |
|---|
| 1011 | $cipher = substr($cipher, mcrypt_enc_get_iv_size($td)); |
|---|
| 1012 | mcrypt_generic_init($td, $this->config->get_crypto_key($key), $iv); |
|---|
| 1013 | $clear = mdecrypt_generic($td, $cipher); |
|---|
| 1014 | mcrypt_generic_deinit($td); |
|---|
| 1015 | mcrypt_module_close($td); |
|---|
| 1016 | } |
|---|
| 1017 | else if (function_exists('des')) |
|---|
| 1018 | { |
|---|
| 1019 | define('DES_IV_SIZE', 8); |
|---|
| 1020 | $iv = substr($cipher, 0, constant('DES_IV_SIZE')); |
|---|
| 1021 | $cipher = substr($cipher, constant('DES_IV_SIZE')); |
|---|
| 1022 | $clear = des($this->config->get_crypto_key($key), $cipher, 0, 1, $iv); |
|---|
| 1023 | } |
|---|
| 1024 | else |
|---|
| 1025 | { |
|---|
| 1026 | raise_error(array( |
|---|
| 1027 | 'code' => 500, 'type' => 'php', |
|---|
| 1028 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 1029 | 'message' => "Could not perform decryption; make sure Mcrypt is installed or lib/des.inc is available" |
|---|
| 1030 | ), true, true); |
|---|
| 1031 | } |
|---|
| 1032 | |
|---|
| 1033 | /*- |
|---|
| 1034 | * Trim PHP's padding and the canary byte; see note in |
|---|
| 1035 | * rcmail::encrypt() and http://php.net/mcrypt_generic#68082 |
|---|
| 1036 | */ |
|---|
| 1037 | $clear = substr(rtrim($clear, "\0"), 0, -1); |
|---|
| 1038 | |
|---|
| 1039 | return $clear; |
|---|
| 1040 | } |
|---|
| 1041 | |
|---|
| 1042 | /** |
|---|
| 1043 | * Build a valid URL to this instance of RoundCube |
|---|
| 1044 | * |
|---|
| 1045 | * @param mixed Either a string with the action or url parameters as key-value pairs |
|---|
| 1046 | * @return string Valid application URL |
|---|
| 1047 | */ |
|---|
| 1048 | public function url($p) |
|---|
| 1049 | { |
|---|
| 1050 | if (!is_array($p)) |
|---|
| 1051 | $p = array('_action' => @func_get_arg(0)); |
|---|
| 1052 | |
|---|
| 1053 | $task = $p['_task'] ? $p['_task'] : ($p['task'] ? $p['task'] : $this->task); |
|---|
| 1054 | $p['_task'] = $task; |
|---|
| 1055 | unset($p['task']); |
|---|
| 1056 | |
|---|
| 1057 | $url = './'; |
|---|
| 1058 | $delm = '?'; |
|---|
| 1059 | foreach (array_reverse($p) as $key => $val) |
|---|
| 1060 | { |
|---|
| 1061 | if (!empty($val)) { |
|---|
| 1062 | $par = $key[0] == '_' ? $key : '_'.$key; |
|---|
| 1063 | $url .= $delm.urlencode($par).'='.urlencode($val); |
|---|
| 1064 | $delm = '&'; |
|---|
| 1065 | } |
|---|
| 1066 | } |
|---|
| 1067 | return $url; |
|---|
| 1068 | } |
|---|
| 1069 | |
|---|
| 1070 | |
|---|
| 1071 | /** |
|---|
| 1072 | * Helper method to set a cookie with the current path and host settings |
|---|
| 1073 | * |
|---|
| 1074 | * @param string Cookie name |
|---|
| 1075 | * @param string Cookie value |
|---|
| 1076 | * @param string Expiration time |
|---|
| 1077 | */ |
|---|
| 1078 | public static function setcookie($name, $value, $exp = 0) |
|---|
| 1079 | { |
|---|
| 1080 | if (headers_sent()) |
|---|
| 1081 | return; |
|---|
| 1082 | |
|---|
| 1083 | $cookie = session_get_cookie_params(); |
|---|
| 1084 | |
|---|
| 1085 | setcookie($name, $value, $exp, $cookie['path'], $cookie['domain'], |
|---|
| 1086 | rcube_https_check(), true); |
|---|
| 1087 | } |
|---|
| 1088 | } |
|---|
| 1089 | |
|---|
| 1090 | |
|---|