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