| 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-2011, The Roundcube Dev Team | |
|---|
| 9 | | Copyright (C) 2011, Kolab Systems AG | |
|---|
| 10 | | | |
|---|
| 11 | | Licensed under the GNU General Public License version 3 or | |
|---|
| 12 | | any later version with exceptions for skins & plugins. | |
|---|
| 13 | | See the README file for a full license statement. | |
|---|
| 14 | | | |
|---|
| 15 | | PURPOSE: | |
|---|
| 16 | | Application class providing core functions and holding | |
|---|
| 17 | | instances of all 'global' objects like db- and imap-connections | |
|---|
| 18 | +-----------------------------------------------------------------------+ |
|---|
| 19 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 20 | +-----------------------------------------------------------------------+ |
|---|
| 21 | |
|---|
| 22 | $Id$ |
|---|
| 23 | |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * Application class of Roundcube Webmail |
|---|
| 29 | * implemented as singleton |
|---|
| 30 | * |
|---|
| 31 | * @package Core |
|---|
| 32 | */ |
|---|
| 33 | class rcmail |
|---|
| 34 | { |
|---|
| 35 | /** |
|---|
| 36 | * Main tasks. |
|---|
| 37 | * |
|---|
| 38 | * @var array |
|---|
| 39 | */ |
|---|
| 40 | static public $main_tasks = array('mail','settings','addressbook','login','logout','utils','dummy'); |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * Singleton instace of rcmail |
|---|
| 44 | * |
|---|
| 45 | * @var rcmail |
|---|
| 46 | */ |
|---|
| 47 | static private $instance; |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * Stores instance of rcube_config. |
|---|
| 51 | * |
|---|
| 52 | * @var rcube_config |
|---|
| 53 | */ |
|---|
| 54 | public $config; |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * Stores rcube_user instance. |
|---|
| 58 | * |
|---|
| 59 | * @var rcube_user |
|---|
| 60 | */ |
|---|
| 61 | public $user; |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Instace of database class. |
|---|
| 65 | * |
|---|
| 66 | * @var rcube_mdb2 |
|---|
| 67 | */ |
|---|
| 68 | public $db; |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * Instace of Memcache class. |
|---|
| 72 | * |
|---|
| 73 | * @var rcube_mdb2 |
|---|
| 74 | */ |
|---|
| 75 | public $memcache; |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * Instace of rcube_session class. |
|---|
| 79 | * |
|---|
| 80 | * @var rcube_session |
|---|
| 81 | */ |
|---|
| 82 | public $session; |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * Instance of rcube_smtp class. |
|---|
| 86 | * |
|---|
| 87 | * @var rcube_smtp |
|---|
| 88 | */ |
|---|
| 89 | public $smtp; |
|---|
| 90 | |
|---|
| 91 | /** |
|---|
| 92 | * Instance of rcube_storage class. |
|---|
| 93 | * |
|---|
| 94 | * @var rcube_storage |
|---|
| 95 | */ |
|---|
| 96 | public $storage; |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * Instance of rcube_template class. |
|---|
| 100 | * |
|---|
| 101 | * @var rcube_template |
|---|
| 102 | */ |
|---|
| 103 | public $output; |
|---|
| 104 | |
|---|
| 105 | /** |
|---|
| 106 | * Instance of rcube_plugin_api. |
|---|
| 107 | * |
|---|
| 108 | * @var rcube_plugin_api |
|---|
| 109 | */ |
|---|
| 110 | public $plugins; |
|---|
| 111 | |
|---|
| 112 | /** |
|---|
| 113 | * Current task. |
|---|
| 114 | * |
|---|
| 115 | * @var string |
|---|
| 116 | */ |
|---|
| 117 | public $task; |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | * Current action. |
|---|
| 121 | * |
|---|
| 122 | * @var string |
|---|
| 123 | */ |
|---|
| 124 | public $action = ''; |
|---|
| 125 | public $comm_path = './'; |
|---|
| 126 | |
|---|
| 127 | private $texts; |
|---|
| 128 | private $address_books = array(); |
|---|
| 129 | private $caches = array(); |
|---|
| 130 | private $action_map = array(); |
|---|
| 131 | private $shutdown_functions = array(); |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * This implements the 'singleton' design pattern |
|---|
| 136 | * |
|---|
| 137 | * @return rcmail The one and only instance |
|---|
| 138 | */ |
|---|
| 139 | static function get_instance() |
|---|
| 140 | { |
|---|
| 141 | if (!self::$instance) { |
|---|
| 142 | self::$instance = new rcmail(); |
|---|
| 143 | self::$instance->startup(); // init AFTER object was linked with self::$instance |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | return self::$instance; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | /** |
|---|
| 151 | * Private constructor |
|---|
| 152 | */ |
|---|
| 153 | private function __construct() |
|---|
| 154 | { |
|---|
| 155 | // load configuration |
|---|
| 156 | $this->config = new rcube_config(); |
|---|
| 157 | |
|---|
| 158 | register_shutdown_function(array($this, 'shutdown')); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | /** |
|---|
| 163 | * Initial startup function |
|---|
| 164 | * to register session, create database and imap connections |
|---|
| 165 | */ |
|---|
| 166 | private function startup() |
|---|
| 167 | { |
|---|
| 168 | // initialize syslog |
|---|
| 169 | if ($this->config->get('log_driver') == 'syslog') { |
|---|
| 170 | $syslog_id = $this->config->get('syslog_id', 'roundcube'); |
|---|
| 171 | $syslog_facility = $this->config->get('syslog_facility', LOG_USER); |
|---|
| 172 | openlog($syslog_id, LOG_ODELAY, $syslog_facility); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | // connect to database |
|---|
| 176 | $this->get_dbh(); |
|---|
| 177 | |
|---|
| 178 | // start session |
|---|
| 179 | $this->session_init(); |
|---|
| 180 | |
|---|
| 181 | // create user object |
|---|
| 182 | $this->set_user(new rcube_user($_SESSION['user_id'])); |
|---|
| 183 | |
|---|
| 184 | // configure session (after user config merge!) |
|---|
| 185 | $this->session_configure(); |
|---|
| 186 | |
|---|
| 187 | // set task and action properties |
|---|
| 188 | $this->set_task(get_input_value('_task', RCUBE_INPUT_GPC)); |
|---|
| 189 | $this->action = asciiwords(get_input_value('_action', RCUBE_INPUT_GPC)); |
|---|
| 190 | |
|---|
| 191 | // reset some session parameters when changing task |
|---|
| 192 | if ($this->task != 'utils') { |
|---|
| 193 | if ($this->session && $_SESSION['task'] != $this->task) |
|---|
| 194 | $this->session->remove('page'); |
|---|
| 195 | // set current task to session |
|---|
| 196 | $_SESSION['task'] = $this->task; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | // init output class |
|---|
| 200 | if (!empty($_REQUEST['_remote'])) |
|---|
| 201 | $GLOBALS['OUTPUT'] = $this->json_init(); |
|---|
| 202 | else |
|---|
| 203 | $GLOBALS['OUTPUT'] = $this->load_gui(!empty($_REQUEST['_framed'])); |
|---|
| 204 | |
|---|
| 205 | // create plugin API and load plugins |
|---|
| 206 | $this->plugins = rcube_plugin_api::get_instance(); |
|---|
| 207 | |
|---|
| 208 | // init plugins |
|---|
| 209 | $this->plugins->init(); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | /** |
|---|
| 214 | * Setter for application task |
|---|
| 215 | * |
|---|
| 216 | * @param string Task to set |
|---|
| 217 | */ |
|---|
| 218 | public function set_task($task) |
|---|
| 219 | { |
|---|
| 220 | $task = asciiwords($task); |
|---|
| 221 | |
|---|
| 222 | if ($this->user && $this->user->ID) |
|---|
| 223 | $task = !$task ? 'mail' : $task; |
|---|
| 224 | else |
|---|
| 225 | $task = 'login'; |
|---|
| 226 | |
|---|
| 227 | $this->task = $task; |
|---|
| 228 | $this->comm_path = $this->url(array('task' => $this->task)); |
|---|
| 229 | |
|---|
| 230 | if ($this->output) |
|---|
| 231 | $this->output->set_env('task', $this->task); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | /** |
|---|
| 236 | * Setter for system user object |
|---|
| 237 | * |
|---|
| 238 | * @param rcube_user Current user instance |
|---|
| 239 | */ |
|---|
| 240 | public function set_user($user) |
|---|
| 241 | { |
|---|
| 242 | if (is_object($user)) { |
|---|
| 243 | $this->user = $user; |
|---|
| 244 | |
|---|
| 245 | // overwrite config with user preferences |
|---|
| 246 | $this->config->set_user_prefs((array)$this->user->get_prefs()); |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | $_SESSION['language'] = $this->user->language = $this->language_prop($this->config->get('language', $_SESSION['language'])); |
|---|
| 250 | |
|---|
| 251 | // set localization |
|---|
| 252 | setlocale(LC_ALL, $_SESSION['language'] . '.utf8', 'en_US.utf8'); |
|---|
| 253 | |
|---|
| 254 | // workaround for http://bugs.php.net/bug.php?id=18556 |
|---|
| 255 | if (in_array($_SESSION['language'], array('tr_TR', 'ku', 'az_AZ'))) |
|---|
| 256 | setlocale(LC_CTYPE, 'en_US' . '.utf8'); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | /** |
|---|
| 261 | * Check the given string and return a valid language code |
|---|
| 262 | * |
|---|
| 263 | * @param string Language code |
|---|
| 264 | * @return string Valid language code |
|---|
| 265 | */ |
|---|
| 266 | private function language_prop($lang) |
|---|
| 267 | { |
|---|
| 268 | static $rcube_languages, $rcube_language_aliases; |
|---|
| 269 | |
|---|
| 270 | // user HTTP_ACCEPT_LANGUAGE if no language is specified |
|---|
| 271 | if (empty($lang) || $lang == 'auto') { |
|---|
| 272 | $accept_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); |
|---|
| 273 | $lang = str_replace('-', '_', $accept_langs[0]); |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | if (empty($rcube_languages)) { |
|---|
| 277 | @include(INSTALL_PATH . 'program/localization/index.inc'); |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | // check if we have an alias for that language |
|---|
| 281 | if (!isset($rcube_languages[$lang]) && isset($rcube_language_aliases[$lang])) { |
|---|
| 282 | $lang = $rcube_language_aliases[$lang]; |
|---|
| 283 | } |
|---|
| 284 | // try the first two chars |
|---|
| 285 | else if (!isset($rcube_languages[$lang])) { |
|---|
| 286 | $short = substr($lang, 0, 2); |
|---|
| 287 | |
|---|
| 288 | // check if we have an alias for the short language code |
|---|
| 289 | if (!isset($rcube_languages[$short]) && isset($rcube_language_aliases[$short])) { |
|---|
| 290 | $lang = $rcube_language_aliases[$short]; |
|---|
| 291 | } |
|---|
| 292 | // expand 'nn' to 'nn_NN' |
|---|
| 293 | else if (!isset($rcube_languages[$short])) { |
|---|
| 294 | $lang = $short.'_'.strtoupper($short); |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | if (!isset($rcube_languages[$lang]) || !is_dir(INSTALL_PATH . 'program/localization/' . $lang)) { |
|---|
| 299 | $lang = 'en_US'; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | return $lang; |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | /** |
|---|
| 307 | * Get the current database connection |
|---|
| 308 | * |
|---|
| 309 | * @return rcube_mdb2 Database connection object |
|---|
| 310 | */ |
|---|
| 311 | public function get_dbh() |
|---|
| 312 | { |
|---|
| 313 | if (!$this->db) { |
|---|
| 314 | $config_all = $this->config->all(); |
|---|
| 315 | |
|---|
| 316 | $this->db = new rcube_mdb2($config_all['db_dsnw'], $config_all['db_dsnr'], $config_all['db_persistent']); |
|---|
| 317 | $this->db->sqlite_initials = INSTALL_PATH . 'SQL/sqlite.initial.sql'; |
|---|
| 318 | $this->db->set_debug((bool)$config_all['sql_debug']); |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | return $this->db; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | /** |
|---|
| 326 | * Get global handle for memcache access |
|---|
| 327 | * |
|---|
| 328 | * @return object Memcache |
|---|
| 329 | */ |
|---|
| 330 | public function get_memcache() |
|---|
| 331 | { |
|---|
| 332 | if (!isset($this->memcache)) { |
|---|
| 333 | // no memcache support in PHP |
|---|
| 334 | if (!class_exists('Memcache')) { |
|---|
| 335 | $this->memcache = false; |
|---|
| 336 | return false; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | $this->memcache = new Memcache; |
|---|
| 340 | $this->mc_available = 0; |
|---|
| 341 | |
|---|
| 342 | // add alll configured hosts to pool |
|---|
| 343 | $pconnect = $this->config->get('memcache_pconnect', true); |
|---|
| 344 | foreach ($this->config->get('memcache_hosts', array()) as $host) { |
|---|
| 345 | list($host, $port) = explode(':', $host); |
|---|
| 346 | if (!$port) $port = 11211; |
|---|
| 347 | $this->mc_available += intval($this->memcache->addServer($host, $port, $pconnect, 1, 1, 15, false, array($this, 'memcache_failure'))); |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | // test connection and failover (will result in $this->mc_available == 0 on complete failure) |
|---|
| 351 | $this->memcache->increment('__CONNECTIONTEST__', 1); // NOP if key doesn't exist |
|---|
| 352 | |
|---|
| 353 | if (!$this->mc_available) |
|---|
| 354 | $this->memcache = false; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | return $this->memcache; |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | /** |
|---|
| 361 | * Callback for memcache failure |
|---|
| 362 | */ |
|---|
| 363 | public function memcache_failure($host, $port) |
|---|
| 364 | { |
|---|
| 365 | static $seen = array(); |
|---|
| 366 | |
|---|
| 367 | // only report once |
|---|
| 368 | if (!$seen["$host:$port"]++) { |
|---|
| 369 | $this->mc_available--; |
|---|
| 370 | raise_error(array('code' => 604, 'type' => 'db', |
|---|
| 371 | 'line' => __LINE__, 'file' => __FILE__, |
|---|
| 372 | 'message' => "Memcache failure on host $host:$port"), |
|---|
| 373 | true, false); |
|---|
| 374 | } |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | /** |
|---|
| 379 | * Initialize and get cache object |
|---|
| 380 | * |
|---|
| 381 | * @param string $name Cache identifier |
|---|
| 382 | * @param string $type Cache type ('db', 'apc' or 'memcache') |
|---|
| 383 | * @param int $ttl Expiration time for cache items in seconds |
|---|
| 384 | * @param bool $packed Enables/disables data serialization |
|---|
| 385 | * |
|---|
| 386 | * @return rcube_cache Cache object |
|---|
| 387 | */ |
|---|
| 388 | public function get_cache($name, $type='db', $ttl=0, $packed=true) |
|---|
| 389 | { |
|---|
| 390 | if (!isset($this->caches[$name])) { |
|---|
| 391 | $this->caches[$name] = new rcube_cache($type, $_SESSION['user_id'], $name, $ttl, $packed); |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | return $this->caches[$name]; |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | /** |
|---|
| 399 | * Return instance of the internal address book class |
|---|
| 400 | * |
|---|
| 401 | * @param string Address book identifier |
|---|
| 402 | * @param boolean True if the address book needs to be writeable |
|---|
| 403 | * |
|---|
| 404 | * @return rcube_contacts Address book object |
|---|
| 405 | */ |
|---|
| 406 | public function get_address_book($id, $writeable = false) |
|---|
| 407 | { |
|---|
| 408 | $contacts = null; |
|---|
| 409 | $ldap_config = (array)$this->config->get('ldap_public'); |
|---|
| 410 | $abook_type = strtolower($this->config->get('address_book_type')); |
|---|
| 411 | |
|---|
| 412 | // 'sql' is the alias for '0' used by autocomplete |
|---|
| 413 | if ($id == 'sql') |
|---|
| 414 | $id = '0'; |
|---|
| 415 | |
|---|
| 416 | // use existing instance |
|---|
| 417 | if (isset($this->address_books[$id]) && is_object($this->address_books[$id]) |
|---|
| 418 | && is_a($this->address_books[$id], 'rcube_addressbook') |
|---|
| 419 | && (!$writeable || !$this->address_books[$id]->readonly) |
|---|
| 420 | ) { |
|---|
| 421 | $contacts = $this->address_books[$id]; |
|---|
| 422 | } |
|---|
| 423 | else if ($id && $ldap_config[$id]) { |
|---|
| 424 | $contacts = new rcube_ldap($ldap_config[$id], $this->config->get('ldap_debug'), $this->config->mail_domain($_SESSION['storage_host'])); |
|---|
| 425 | } |
|---|
| 426 | else if ($id === '0') { |
|---|
| 427 | $contacts = new rcube_contacts($this->db, $this->user->ID); |
|---|
| 428 | } |
|---|
| 429 | else { |
|---|
| 430 | $plugin = $this->plugins->exec_hook('addressbook_get', array('id' => $id, 'writeable' => $writeable)); |
|---|
| 431 | |
|---|
| 432 | // plugin returned instance of a rcube_addressbook |
|---|
| 433 | if ($plugin['instance'] instanceof rcube_addressbook) { |
|---|
| 434 | $contacts = $plugin['instance']; |
|---|
| 435 | } |
|---|
| 436 | // get first source from the list |
|---|
| 437 | else if (!$id) { |
|---|
| 438 | $source = reset($this->get_address_sources($writeable)); |
|---|
| 439 | if (!empty($source)) { |
|---|
| 440 | $contacts = $this->get_address_book($source['id']); |
|---|
| 441 | if ($contacts) |
|---|
| 442 | $id = $source['id']; |
|---|
| 443 | } |
|---|
| 444 | } |
|---|
| 445 | } |
|---|
| 446 | |
|---|
| 447 | if (!$contacts) { |
|---|
| 448 | raise_error(array( |
|---|
| 449 | 'code' => 700, 'type' => 'php', |
|---|
| 450 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 451 | 'message' => "Addressbook source ($id) not found!"), |
|---|
| 452 | true, true); |
|---|
| 453 | } |
|---|
| 454 | |
|---|
| 455 | // set configured sort order |
|---|
| 456 | if ($sort_col = $this->config->get('addressbook_sort_col')) |
|---|
| 457 | $contacts->set_sort_order($sort_col); |
|---|
| 458 | |
|---|
| 459 | // add to the 'books' array for shutdown function |
|---|
| 460 | $this->address_books[$id] = $contacts; |
|---|
| 461 | |
|---|
| 462 | return $contacts; |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | /** |
|---|
| 467 | * Return address books list |
|---|
| 468 | * |
|---|
| 469 | * @param boolean True if the address book needs to be writeable |
|---|
| 470 | * |
|---|
| 471 | * @return array Address books array |
|---|
| 472 | */ |
|---|
| 473 | public function get_address_sources($writeable = false) |
|---|
| 474 | { |
|---|
| 475 | $abook_type = strtolower($this->config->get('address_book_type')); |
|---|
| 476 | $ldap_config = $this->config->get('ldap_public'); |
|---|
| 477 | $autocomplete = (array) $this->config->get('autocomplete_addressbooks'); |
|---|
| 478 | $list = array(); |
|---|
| 479 | |
|---|
| 480 | // We are using the DB address book |
|---|
| 481 | if ($abook_type != 'ldap') { |
|---|
| 482 | if (!isset($this->address_books['0'])) |
|---|
| 483 | $this->address_books['0'] = new rcube_contacts($this->db, $this->user->ID); |
|---|
| 484 | $list['0'] = array( |
|---|
| 485 | 'id' => '0', |
|---|
| 486 | 'name' => rcube_label('personaladrbook'), |
|---|
| 487 | 'groups' => $this->address_books['0']->groups, |
|---|
| 488 | 'readonly' => $this->address_books['0']->readonly, |
|---|
| 489 | 'autocomplete' => in_array('sql', $autocomplete), |
|---|
| 490 | 'undelete' => $this->address_books['0']->undelete && $this->config->get('undo_timeout'), |
|---|
| 491 | ); |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | if ($ldap_config) { |
|---|
| 495 | $ldap_config = (array) $ldap_config; |
|---|
| 496 | foreach ($ldap_config as $id => $prop) { |
|---|
| 497 | // handle misconfiguration |
|---|
| 498 | if (empty($prop) || !is_array($prop)) { |
|---|
| 499 | continue; |
|---|
| 500 | } |
|---|
| 501 | $list[$id] = array( |
|---|
| 502 | 'id' => $id, |
|---|
| 503 | 'name' => $prop['name'], |
|---|
| 504 | 'groups' => is_array($prop['groups']), |
|---|
| 505 | 'readonly' => !$prop['writable'], |
|---|
| 506 | 'hidden' => $prop['hidden'], |
|---|
| 507 | 'autocomplete' => in_array($id, $autocomplete) |
|---|
| 508 | ); |
|---|
| 509 | } |
|---|
| 510 | } |
|---|
| 511 | |
|---|
| 512 | $plugin = $this->plugins->exec_hook('addressbooks_list', array('sources' => $list)); |
|---|
| 513 | $list = $plugin['sources']; |
|---|
| 514 | |
|---|
| 515 | foreach ($list as $idx => $item) { |
|---|
| 516 | // register source for shutdown function |
|---|
| 517 | if (!is_object($this->address_books[$item['id']])) |
|---|
| 518 | $this->address_books[$item['id']] = $item; |
|---|
| 519 | // remove from list if not writeable as requested |
|---|
| 520 | if ($writeable && $item['readonly']) |
|---|
| 521 | unset($list[$idx]); |
|---|
| 522 | } |
|---|
| 523 | |
|---|
| 524 | return $list; |
|---|
| 525 | } |
|---|
| 526 | |
|---|
| 527 | |
|---|
| 528 | /** |
|---|
| 529 | * Init output object for GUI and add common scripts. |
|---|
| 530 | * This will instantiate a rcmail_template object and set |
|---|
| 531 | * environment vars according to the current session and configuration |
|---|
| 532 | * |
|---|
| 533 | * @param boolean True if this request is loaded in a (i)frame |
|---|
| 534 | * @return rcube_template Reference to HTML output object |
|---|
| 535 | */ |
|---|
| 536 | public function load_gui($framed = false) |
|---|
| 537 | { |
|---|
| 538 | // init output page |
|---|
| 539 | if (!($this->output instanceof rcube_template)) |
|---|
| 540 | $this->output = new rcube_template($this->task, $framed); |
|---|
| 541 | |
|---|
| 542 | // set keep-alive/check-recent interval |
|---|
| 543 | if ($this->session && ($keep_alive = $this->session->get_keep_alive())) { |
|---|
| 544 | $this->output->set_env('keep_alive', $keep_alive); |
|---|
| 545 | } |
|---|
| 546 | |
|---|
| 547 | if ($framed) { |
|---|
| 548 | $this->comm_path .= '&_framed=1'; |
|---|
| 549 | $this->output->set_env('framed', true); |
|---|
| 550 | } |
|---|
| 551 | |
|---|
| 552 | $this->output->set_env('task', $this->task); |
|---|
| 553 | $this->output->set_env('action', $this->action); |
|---|
| 554 | $this->output->set_env('comm_path', $this->comm_path); |
|---|
| 555 | $this->output->set_charset(RCMAIL_CHARSET); |
|---|
| 556 | |
|---|
| 557 | // add some basic labels to client |
|---|
| 558 | $this->output->add_label('loading', 'servererror'); |
|---|
| 559 | |
|---|
| 560 | return $this->output; |
|---|
| 561 | } |
|---|
| 562 | |
|---|
| 563 | |
|---|
| 564 | /** |
|---|
| 565 | * Create an output object for JSON responses |
|---|
| 566 | * |
|---|
| 567 | * @return rcube_json_output Reference to JSON output object |
|---|
| 568 | */ |
|---|
| 569 | public function json_init() |
|---|
| 570 | { |
|---|
| 571 | if (!($this->output instanceof rcube_json_output)) |
|---|
| 572 | $this->output = new rcube_json_output($this->task); |
|---|
| 573 | |
|---|
| 574 | return $this->output; |
|---|
| 575 | } |
|---|
| 576 | |
|---|
| 577 | |
|---|
| 578 | /** |
|---|
| 579 | * Create SMTP object and connect to server |
|---|
| 580 | * |
|---|
| 581 | * @param boolean True if connection should be established |
|---|
| 582 | */ |
|---|
| 583 | public function smtp_init($connect = false) |
|---|
| 584 | { |
|---|
| 585 | $this->smtp = new rcube_smtp(); |
|---|
| 586 | |
|---|
| 587 | if ($connect) |
|---|
| 588 | $this->smtp->connect(); |
|---|
| 589 | } |
|---|
| 590 | |
|---|
| 591 | |
|---|
| 592 | /** |
|---|
| 593 | * Initialize and get storage object |
|---|
| 594 | * |
|---|
| 595 | * @return rcube_storage Storage object |
|---|
| 596 | */ |
|---|
| 597 | public function get_storage() |
|---|
| 598 | { |
|---|
| 599 | // already initialized |
|---|
| 600 | if (!is_object($this->storage)) { |
|---|
| 601 | $this->storage_init(); |
|---|
| 602 | } |
|---|
| 603 | |
|---|
| 604 | return $this->storage; |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | |
|---|
| 608 | /** |
|---|
| 609 | * Connect to the IMAP server with stored session data. |
|---|
| 610 | * |
|---|
| 611 | * @return bool True on success, False on error |
|---|
| 612 | * @deprecated |
|---|
| 613 | */ |
|---|
| 614 | public function imap_connect() |
|---|
| 615 | { |
|---|
| 616 | return $this->storage_connect(); |
|---|
| 617 | } |
|---|
| 618 | |
|---|
| 619 | |
|---|
| 620 | /** |
|---|
| 621 | * Initialize IMAP object. |
|---|
| 622 | * |
|---|
| 623 | * @deprecated |
|---|
| 624 | */ |
|---|
| 625 | public function imap_init() |
|---|
| 626 | { |
|---|
| 627 | $this->storage_init(); |
|---|
| 628 | } |
|---|
| 629 | |
|---|
| 630 | |
|---|
| 631 | /** |
|---|
| 632 | * Initialize storage object |
|---|
| 633 | */ |
|---|
| 634 | public function storage_init() |
|---|
| 635 | { |
|---|
| 636 | // already initialized |
|---|
| 637 | if (is_object($this->storage)) { |
|---|
| 638 | return; |
|---|
| 639 | } |
|---|
| 640 | |
|---|
| 641 | $driver = $this->config->get('storage_driver', 'imap'); |
|---|
| 642 | $driver_class = "rcube_{$driver}"; |
|---|
| 643 | |
|---|
| 644 | if (!class_exists($driver_class)) { |
|---|
| 645 | raise_error(array( |
|---|
| 646 | 'code' => 700, 'type' => 'php', |
|---|
| 647 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 648 | 'message' => "Storage driver class ($driver) not found!"), |
|---|
| 649 | true, true); |
|---|
| 650 | } |
|---|
| 651 | |
|---|
| 652 | // Initialize storage object |
|---|
| 653 | $this->storage = new $driver_class; |
|---|
| 654 | |
|---|
| 655 | // for backward compat. (deprecated, will be removed) |
|---|
| 656 | $this->imap = $this->storage; |
|---|
| 657 | |
|---|
| 658 | // enable caching of mail data |
|---|
| 659 | $storage_cache = $this->config->get("{$driver}_cache"); |
|---|
| 660 | $messages_cache = $this->config->get('messages_cache'); |
|---|
| 661 | // for backward compatybility |
|---|
| 662 | if ($storage_cache === null && $messages_cache === null && $this->config->get('enable_caching')) { |
|---|
| 663 | $storage_cache = 'db'; |
|---|
| 664 | $messages_cache = true; |
|---|
| 665 | } |
|---|
| 666 | |
|---|
| 667 | if ($storage_cache) |
|---|
| 668 | $this->storage->set_caching($storage_cache); |
|---|
| 669 | if ($messages_cache) |
|---|
| 670 | $this->storage->set_messages_caching(true); |
|---|
| 671 | |
|---|
| 672 | // set pagesize from config |
|---|
| 673 | $pagesize = $this->config->get('mail_pagesize'); |
|---|
| 674 | if (!$pagesize) { |
|---|
| 675 | $pagesize = $this->config->get('pagesize', 50); |
|---|
| 676 | } |
|---|
| 677 | $this->storage->set_pagesize($pagesize); |
|---|
| 678 | |
|---|
| 679 | // set class options |
|---|
| 680 | $options = array( |
|---|
| 681 | 'auth_type' => $this->config->get("{$driver}_auth_type", 'check'), |
|---|
| 682 | 'auth_cid' => $this->config->get("{$driver}_auth_cid"), |
|---|
| 683 | 'auth_pw' => $this->config->get("{$driver}_auth_pw"), |
|---|
| 684 | 'debug' => (bool) $this->config->get("{$driver}_debug"), |
|---|
| 685 | 'force_caps' => (bool) $this->config->get("{$driver}_force_caps"), |
|---|
| 686 | 'timeout' => (int) $this->config->get("{$driver}_timeout"), |
|---|
| 687 | 'skip_deleted' => (bool) $this->config->get('skip_deleted'), |
|---|
| 688 | 'driver' => $driver, |
|---|
| 689 | ); |
|---|
| 690 | |
|---|
| 691 | if (!empty($_SESSION['storage_host'])) { |
|---|
| 692 | $options['host'] = $_SESSION['storage_host']; |
|---|
| 693 | $options['user'] = $_SESSION['username']; |
|---|
| 694 | $options['port'] = $_SESSION['storage_port']; |
|---|
| 695 | $options['ssl'] = $_SESSION['storage_ssl']; |
|---|
| 696 | $options['password'] = $this->decrypt($_SESSION['password']); |
|---|
| 697 | // set 'imap_host' for backwards compatibility |
|---|
| 698 | $_SESSION[$driver.'_host'] = &$_SESSION['storage_host']; |
|---|
| 699 | } |
|---|
| 700 | |
|---|
| 701 | $options = $this->plugins->exec_hook("storage_init", $options); |
|---|
| 702 | |
|---|
| 703 | $this->storage->set_options($options); |
|---|
| 704 | $this->set_storage_prop(); |
|---|
| 705 | } |
|---|
| 706 | |
|---|
| 707 | |
|---|
| 708 | /** |
|---|
| 709 | * Connect to the mail storage server with stored session data |
|---|
| 710 | * |
|---|
| 711 | * @return bool True on success, False on error |
|---|
| 712 | */ |
|---|
| 713 | public function storage_connect() |
|---|
| 714 | { |
|---|
| 715 | $storage = $this->get_storage(); |
|---|
| 716 | |
|---|
| 717 | if ($_SESSION['storage_host'] && !$storage->is_connected()) { |
|---|
| 718 | $host = $_SESSION['storage_host']; |
|---|
| 719 | $user = $_SESSION['username']; |
|---|
| 720 | $port = $_SESSION['storage_port']; |
|---|
| 721 | $ssl = $_SESSION['storage_ssl']; |
|---|
| 722 | $pass = $this->decrypt($_SESSION['password']); |
|---|
| 723 | |
|---|
| 724 | if (!$storage->connect($host, $user, $pass, $port, $ssl)) { |
|---|
| 725 | if ($this->output) |
|---|
| 726 | $this->output->show_message($storage->get_error_code() == -1 ? 'storageerror' : 'sessionerror', 'error'); |
|---|
| 727 | } |
|---|
| 728 | else { |
|---|
| 729 | $this->set_storage_prop(); |
|---|
| 730 | return $storage->is_connected(); |
|---|
| 731 | } |
|---|
| 732 | } |
|---|
| 733 | |
|---|
| 734 | return false; |
|---|
| 735 | } |
|---|
| 736 | |
|---|
| 737 | |
|---|
| 738 | /** |
|---|
| 739 | * Create session object and start the session. |
|---|
| 740 | */ |
|---|
| 741 | public function session_init() |
|---|
| 742 | { |
|---|
| 743 | // session started (Installer?) |
|---|
| 744 | if (session_id()) |
|---|
| 745 | return; |
|---|
| 746 | |
|---|
| 747 | $sess_name = $this->config->get('session_name'); |
|---|
| 748 | $sess_domain = $this->config->get('session_domain'); |
|---|
| 749 | $lifetime = $this->config->get('session_lifetime', 0) * 60; |
|---|
| 750 | |
|---|
| 751 | // set session domain |
|---|
| 752 | if ($sess_domain) { |
|---|
| 753 | ini_set('session.cookie_domain', $sess_domain); |
|---|
| 754 | } |
|---|
| 755 | // set session garbage collecting time according to session_lifetime |
|---|
| 756 | if ($lifetime) { |
|---|
| 757 | ini_set('session.gc_maxlifetime', $lifetime * 2); |
|---|
| 758 | } |
|---|
| 759 | |
|---|
| 760 | ini_set('session.cookie_secure', rcube_https_check()); |
|---|
| 761 | ini_set('session.name', $sess_name ? $sess_name : 'roundcube_sessid'); |
|---|
| 762 | ini_set('session.use_cookies', 1); |
|---|
| 763 | ini_set('session.use_only_cookies', 1); |
|---|
| 764 | ini_set('session.serialize_handler', 'php'); |
|---|
| 765 | |
|---|
| 766 | // use database for storing session data |
|---|
| 767 | $this->session = new rcube_session($this->get_dbh(), $this->config); |
|---|
| 768 | |
|---|
| 769 | $this->session->register_gc_handler('rcmail_temp_gc'); |
|---|
| 770 | if ($this->config->get('enable_caching')) |
|---|
| 771 | $this->session->register_gc_handler('rcmail_cache_gc'); |
|---|
| 772 | |
|---|
| 773 | // start PHP session (if not in CLI mode) |
|---|
| 774 | if ($_SERVER['REMOTE_ADDR']) |
|---|
| 775 | session_start(); |
|---|
| 776 | |
|---|
| 777 | // set initial session vars |
|---|
| 778 | if (!$_SESSION['user_id']) |
|---|
| 779 | $_SESSION['temp'] = true; |
|---|
| 780 | } |
|---|
| 781 | |
|---|
| 782 | |
|---|
| 783 | /** |
|---|
| 784 | * Configure session object internals |
|---|
| 785 | */ |
|---|
| 786 | public function session_configure() |
|---|
| 787 | { |
|---|
| 788 | if (!$this->session) |
|---|
| 789 | return; |
|---|
| 790 | |
|---|
| 791 | $lifetime = $this->config->get('session_lifetime', 0) * 60; |
|---|
| 792 | |
|---|
| 793 | // set keep-alive/check-recent interval |
|---|
| 794 | if ($keep_alive = $this->config->get('keep_alive')) { |
|---|
| 795 | // be sure that it's less than session lifetime |
|---|
| 796 | if ($lifetime) |
|---|
| 797 | $keep_alive = min($keep_alive, $lifetime - 30); |
|---|
| 798 | $keep_alive = max(60, $keep_alive); |
|---|
| 799 | $this->session->set_keep_alive($keep_alive); |
|---|
| 800 | } |
|---|
| 801 | |
|---|
| 802 | $this->session->set_secret($this->config->get('des_key') . $_SERVER['HTTP_USER_AGENT']); |
|---|
| 803 | $this->session->set_ip_check($this->config->get('ip_check')); |
|---|
| 804 | } |
|---|
| 805 | |
|---|
| 806 | |
|---|
| 807 | /** |
|---|
| 808 | * Perfom login to the mail server and to the webmail service. |
|---|
| 809 | * This will also create a new user entry if auto_create_user is configured. |
|---|
| 810 | * |
|---|
| 811 | * @param string Mail storage (IMAP) user name |
|---|
| 812 | * @param string Mail storage (IMAP) password |
|---|
| 813 | * @param string Mail storage (IMAP) host |
|---|
| 814 | * |
|---|
| 815 | * @return boolean True on success, False on failure |
|---|
| 816 | */ |
|---|
| 817 | function login($username, $pass, $host=NULL) |
|---|
| 818 | { |
|---|
| 819 | if (empty($username)) { |
|---|
| 820 | return false; |
|---|
| 821 | } |
|---|
| 822 | |
|---|
| 823 | $config = $this->config->all(); |
|---|
| 824 | |
|---|
| 825 | if (!$host) |
|---|
| 826 | $host = $config['default_host']; |
|---|
| 827 | |
|---|
| 828 | // Validate that selected host is in the list of configured hosts |
|---|
| 829 | if (is_array($config['default_host'])) { |
|---|
| 830 | $allowed = false; |
|---|
| 831 | foreach ($config['default_host'] as $key => $host_allowed) { |
|---|
| 832 | if (!is_numeric($key)) |
|---|
| 833 | $host_allowed = $key; |
|---|
| 834 | if ($host == $host_allowed) { |
|---|
| 835 | $allowed = true; |
|---|
| 836 | break; |
|---|
| 837 | } |
|---|
| 838 | } |
|---|
| 839 | if (!$allowed) |
|---|
| 840 | return false; |
|---|
| 841 | } |
|---|
| 842 | else if (!empty($config['default_host']) && $host != rcube_parse_host($config['default_host'])) |
|---|
| 843 | return false; |
|---|
| 844 | |
|---|
| 845 | // parse $host URL |
|---|
| 846 | $a_host = parse_url($host); |
|---|
| 847 | if ($a_host['host']) { |
|---|
| 848 | $host = $a_host['host']; |
|---|
| 849 | $ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? $a_host['scheme'] : null; |
|---|
| 850 | if (!empty($a_host['port'])) |
|---|
| 851 | $port = $a_host['port']; |
|---|
| 852 | else if ($ssl && $ssl != 'tls' && (!$config['default_port'] || $config['default_port'] == 143)) |
|---|
| 853 | $port = 993; |
|---|
| 854 | } |
|---|
| 855 | |
|---|
| 856 | if (!$port) { |
|---|
| 857 | $port = $config['default_port']; |
|---|
| 858 | } |
|---|
| 859 | |
|---|
| 860 | /* Modify username with domain if required |
|---|
| 861 | Inspired by Marco <P0L0_notspam_binware.org> |
|---|
| 862 | */ |
|---|
| 863 | // Check if we need to add domain |
|---|
| 864 | if (!empty($config['username_domain']) && strpos($username, '@') === false) { |
|---|
| 865 | if (is_array($config['username_domain']) && isset($config['username_domain'][$host])) |
|---|
| 866 | $username .= '@'.rcube_parse_host($config['username_domain'][$host], $host); |
|---|
| 867 | else if (is_string($config['username_domain'])) |
|---|
| 868 | $username .= '@'.rcube_parse_host($config['username_domain'], $host); |
|---|
| 869 | } |
|---|
| 870 | |
|---|
| 871 | // Convert username to lowercase. If storage backend |
|---|
| 872 | // is case-insensitive we need to store always the same username (#1487113) |
|---|
| 873 | if ($config['login_lc']) { |
|---|
| 874 | $username = mb_strtolower($username); |
|---|
| 875 | } |
|---|
| 876 | |
|---|
| 877 | // try to resolve email address from virtuser table |
|---|
| 878 | if (strpos($username, '@') && ($virtuser = rcube_user::email2user($username))) { |
|---|
| 879 | $username = $virtuser; |
|---|
| 880 | } |
|---|
| 881 | |
|---|
| 882 | // Here we need IDNA ASCII |
|---|
| 883 | // Only rcube_contacts class is using domain names in Unicode |
|---|
| 884 | $host = rcube_idn_to_ascii($host); |
|---|
| 885 | if (strpos($username, '@')) { |
|---|
| 886 | // lowercase domain name |
|---|
| 887 | list($local, $domain) = explode('@', $username); |
|---|
| 888 | $username = $local . '@' . mb_strtolower($domain); |
|---|
| 889 | $username = rcube_idn_to_ascii($username); |
|---|
| 890 | } |
|---|
| 891 | |
|---|
| 892 | // user already registered -> overwrite username |
|---|
| 893 | if ($user = rcube_user::query($username, $host)) |
|---|
| 894 | $username = $user->data['username']; |
|---|
| 895 | |
|---|
| 896 | if (!$this->storage) |
|---|
| 897 | $this->storage_init(); |
|---|
| 898 | |
|---|
| 899 | // try to log in |
|---|
| 900 | if (!($login = $this->storage->connect($host, $username, $pass, $port, $ssl))) { |
|---|
| 901 | // try with lowercase |
|---|
| 902 | $username_lc = mb_strtolower($username); |
|---|
| 903 | if ($username_lc != $username) { |
|---|
| 904 | // try to find user record again -> overwrite username |
|---|
| 905 | if (!$user && ($user = rcube_user::query($username_lc, $host))) |
|---|
| 906 | $username_lc = $user->data['username']; |
|---|
| 907 | |
|---|
| 908 | if ($login = $this->storage->connect($host, $username_lc, $pass, $port, $ssl)) |
|---|
| 909 | $username = $username_lc; |
|---|
| 910 | } |
|---|
| 911 | } |
|---|
| 912 | |
|---|
| 913 | // exit if login failed |
|---|
| 914 | if (!$login) { |
|---|
| 915 | return false; |
|---|
| 916 | } |
|---|
| 917 | |
|---|
| 918 | // user already registered -> update user's record |
|---|
| 919 | if (is_object($user)) { |
|---|
| 920 | // update last login timestamp |
|---|
| 921 | $user->touch(); |
|---|
| 922 | } |
|---|
| 923 | // create new system user |
|---|
| 924 | else if ($config['auto_create_user']) { |
|---|
| 925 | if ($created = rcube_user::create($username, $host)) { |
|---|
| 926 | $user = $created; |
|---|
| 927 | } |
|---|
| 928 | else { |
|---|
| 929 | raise_error(array( |
|---|
| 930 | 'code' => 620, 'type' => 'php', |
|---|
| 931 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 932 | 'message' => "Failed to create a user record. Maybe aborted by a plugin?" |
|---|
| 933 | ), true, false); |
|---|
| 934 | } |
|---|
| 935 | } |
|---|
| 936 | else { |
|---|
| 937 | raise_error(array( |
|---|
| 938 | 'code' => 621, 'type' => 'php', |
|---|
| 939 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 940 | 'message' => "Access denied for new user $username. 'auto_create_user' is disabled" |
|---|
| 941 | ), true, false); |
|---|
| 942 | } |
|---|
| 943 | |
|---|
| 944 | // login succeeded |
|---|
| 945 | if (is_object($user) && $user->ID) { |
|---|
| 946 | // Configure environment |
|---|
| 947 | $this->set_user($user); |
|---|
| 948 | $this->set_storage_prop(); |
|---|
| 949 | $this->session_configure(); |
|---|
| 950 | |
|---|
| 951 | // fix some old settings according to namespace prefix |
|---|
| 952 | $this->fix_namespace_settings($user); |
|---|
| 953 | |
|---|
| 954 | // create default folders on first login |
|---|
| 955 | if ($config['create_default_folders'] && (!empty($created) || empty($user->data['last_login']))) { |
|---|
| 956 | $this->storage->create_default_folders(); |
|---|
| 957 | } |
|---|
| 958 | |
|---|
| 959 | // set session vars |
|---|
| 960 | $_SESSION['user_id'] = $user->ID; |
|---|
| 961 | $_SESSION['username'] = $user->data['username']; |
|---|
| 962 | $_SESSION['storage_host'] = $host; |
|---|
| 963 | $_SESSION['storage_port'] = $port; |
|---|
| 964 | $_SESSION['storage_ssl'] = $ssl; |
|---|
| 965 | $_SESSION['password'] = $this->encrypt($pass); |
|---|
| 966 | $_SESSION['login_time'] = mktime(); |
|---|
| 967 | |
|---|
| 968 | if (isset($_REQUEST['_timezone']) && $_REQUEST['_timezone'] != '_default_') |
|---|
| 969 | $_SESSION['timezone'] = floatval($_REQUEST['_timezone']); |
|---|
| 970 | if (isset($_REQUEST['_dstactive']) && $_REQUEST['_dstactive'] != '_default_') |
|---|
| 971 | $_SESSION['dst_active'] = intval($_REQUEST['_dstactive']); |
|---|
| 972 | |
|---|
| 973 | // force reloading complete list of subscribed mailboxes |
|---|
| 974 | $this->storage->clear_cache('mailboxes', true); |
|---|
| 975 | |
|---|
| 976 | return true; |
|---|
| 977 | } |
|---|
| 978 | |
|---|
| 979 | return false; |
|---|
| 980 | } |
|---|
| 981 | |
|---|
| 982 | |
|---|
| 983 | /** |
|---|
| 984 | * Set storage parameters. |
|---|
| 985 | * This must be done AFTER connecting to the server! |
|---|
| 986 | */ |
|---|
| 987 | private function set_storage_prop() |
|---|
| 988 | { |
|---|
| 989 | $storage = $this->get_storage(); |
|---|
| 990 | |
|---|
| 991 | $storage->set_charset($this->config->get('default_charset', RCMAIL_CHARSET)); |
|---|
| 992 | |
|---|
| 993 | if ($default_folders = $this->config->get('default_folders')) { |
|---|
| 994 | $storage->set_default_folders($default_folders); |
|---|
| 995 | } |
|---|
| 996 | if (isset($_SESSION['mbox'])) { |
|---|
| 997 | $storage->set_folder($_SESSION['mbox']); |
|---|
| 998 | } |
|---|
| 999 | if (isset($_SESSION['page'])) { |
|---|
| 1000 | $storage->set_page($_SESSION['page']); |
|---|
| 1001 | } |
|---|
| 1002 | } |
|---|
| 1003 | |
|---|
| 1004 | |
|---|
| 1005 | /** |
|---|
| 1006 | * Auto-select IMAP host based on the posted login information |
|---|
| 1007 | * |
|---|
| 1008 | * @return string Selected IMAP host |
|---|
| 1009 | */ |
|---|
| 1010 | public function autoselect_host() |
|---|
| 1011 | { |
|---|
| 1012 | $default_host = $this->config->get('default_host'); |
|---|
| 1013 | $host = null; |
|---|
| 1014 | |
|---|
| 1015 | if (is_array($default_host)) { |
|---|
| 1016 | $post_host = get_input_value('_host', RCUBE_INPUT_POST); |
|---|
| 1017 | |
|---|
| 1018 | // direct match in default_host array |
|---|
| 1019 | if ($default_host[$post_host] || in_array($post_host, array_values($default_host))) { |
|---|
| 1020 | $host = $post_host; |
|---|
| 1021 | } |
|---|
| 1022 | |
|---|
| 1023 | // try to select host by mail domain |
|---|
| 1024 | list($user, $domain) = explode('@', get_input_value('_user', RCUBE_INPUT_POST)); |
|---|
| 1025 | if (!empty($domain)) { |
|---|
| 1026 | foreach ($default_host as $storage_host => $mail_domains) { |
|---|
| 1027 | if (is_array($mail_domains) && in_array_nocase($domain, $mail_domains)) { |
|---|
| 1028 | $host = $storage_host; |
|---|
| 1029 | break; |
|---|
| 1030 | } |
|---|
| 1031 | else if (stripos($storage_host, $domain) !== false || stripos(strval($mail_domains), $domain) !== false) { |
|---|
| 1032 | $host = is_numeric($storage_host) ? $mail_domains : $storage_host; |
|---|
| 1033 | break; |
|---|
| 1034 | } |
|---|
| 1035 | } |
|---|
| 1036 | } |
|---|
| 1037 | |
|---|
| 1038 | // take the first entry if $host is still not set |
|---|
| 1039 | if (empty($host)) { |
|---|
| 1040 | list($key, $val) = each($default_host); |
|---|
| 1041 | $host = is_numeric($key) ? $val : $key; |
|---|
| 1042 | } |
|---|
| 1043 | } |
|---|
| 1044 | else if (empty($default_host)) { |
|---|
| 1045 | $host = get_input_value('_host', RCUBE_INPUT_POST); |
|---|
| 1046 | } |
|---|
| 1047 | else |
|---|
| 1048 | $host = rcube_parse_host($default_host); |
|---|
| 1049 | |
|---|
| 1050 | return $host; |
|---|
| 1051 | } |
|---|
| 1052 | |
|---|
| 1053 | |
|---|
| 1054 | /** |
|---|
| 1055 | * Get localized text in the desired language |
|---|
| 1056 | * |
|---|
| 1057 | * @param mixed $attrib Named parameters array or label name |
|---|
| 1058 | * @param string $domain Label domain (plugin) name |
|---|
| 1059 | * |
|---|
| 1060 | * @return string Localized text |
|---|
| 1061 | */ |
|---|
| 1062 | public function gettext($attrib, $domain=null) |
|---|
| 1063 | { |
|---|
| 1064 | // load localization files if not done yet |
|---|
| 1065 | if (empty($this->texts)) |
|---|
| 1066 | $this->load_language(); |
|---|
| 1067 | |
|---|
| 1068 | // extract attributes |
|---|
| 1069 | if (is_string($attrib)) |
|---|
| 1070 | $attrib = array('name' => $attrib); |
|---|
| 1071 | |
|---|
| 1072 | $name = $attrib['name'] ? $attrib['name'] : ''; |
|---|
| 1073 | |
|---|
| 1074 | // attrib contain text values: use them from now |
|---|
| 1075 | if (($setval = $attrib[strtolower($_SESSION['language'])]) || ($setval = $attrib['en_us'])) |
|---|
| 1076 | $this->texts[$name] = $setval; |
|---|
| 1077 | |
|---|
| 1078 | // check for text with domain |
|---|
| 1079 | if ($domain && ($text = $this->texts[$domain.'.'.$name])) |
|---|
| 1080 | ; |
|---|
| 1081 | // text does not exist |
|---|
| 1082 | else if (!($text = $this->texts[$name])) { |
|---|
| 1083 | return "[$name]"; |
|---|
| 1084 | } |
|---|
| 1085 | |
|---|
| 1086 | // replace vars in text |
|---|
| 1087 | if (is_array($attrib['vars'])) { |
|---|
| 1088 | foreach ($attrib['vars'] as $var_key => $var_value) |
|---|
| 1089 | $text = str_replace($var_key[0]!='$' ? '$'.$var_key : $var_key, $var_value, $text); |
|---|
| 1090 | } |
|---|
| 1091 | |
|---|
| 1092 | // format output |
|---|
| 1093 | if (($attrib['uppercase'] && strtolower($attrib['uppercase']=='first')) || $attrib['ucfirst']) |
|---|
| 1094 | return ucfirst($text); |
|---|
| 1095 | else if ($attrib['uppercase']) |
|---|
| 1096 | return mb_strtoupper($text); |
|---|
| 1097 | else if ($attrib['lowercase']) |
|---|
| 1098 | return mb_strtolower($text); |
|---|
| 1099 | |
|---|
| 1100 | return strtr($text, array('\n' => "\n")); |
|---|
| 1101 | } |
|---|
| 1102 | |
|---|
| 1103 | |
|---|
| 1104 | /** |
|---|
| 1105 | * Check if the given text label exists |
|---|
| 1106 | * |
|---|
| 1107 | * @param string $name Label name |
|---|
| 1108 | * @param string $domain Label domain (plugin) name or '*' for all domains |
|---|
| 1109 | * @param string $ref_domain Sets domain name if label is found |
|---|
| 1110 | * |
|---|
| 1111 | * @return boolean True if text exists (either in the current language or in en_US) |
|---|
| 1112 | */ |
|---|
| 1113 | public function text_exists($name, $domain = null, &$ref_domain = null) |
|---|
| 1114 | { |
|---|
| 1115 | // load localization files if not done yet |
|---|
| 1116 | if (empty($this->texts)) |
|---|
| 1117 | $this->load_language(); |
|---|
| 1118 | |
|---|
| 1119 | if (isset($this->texts[$name])) { |
|---|
| 1120 | $ref_domain = ''; |
|---|
| 1121 | return true; |
|---|
| 1122 | } |
|---|
| 1123 | |
|---|
| 1124 | // any of loaded domains (plugins) |
|---|
| 1125 | if ($domain == '*') { |
|---|
| 1126 | foreach ($this->plugins->loaded_plugins() as $domain) |
|---|
| 1127 | if (isset($this->texts[$domain.'.'.$name])) { |
|---|
| 1128 | $ref_domain = $domain; |
|---|
| 1129 | return true; |
|---|
| 1130 | } |
|---|
| 1131 | } |
|---|
| 1132 | // specified domain |
|---|
| 1133 | else if ($domain) { |
|---|
| 1134 | $ref_domain = $domain; |
|---|
| 1135 | return isset($this->texts[$domain.'.'.$name]); |
|---|
| 1136 | } |
|---|
| 1137 | |
|---|
| 1138 | return false; |
|---|
| 1139 | } |
|---|
| 1140 | |
|---|
| 1141 | /** |
|---|
| 1142 | * Load a localization package |
|---|
| 1143 | * |
|---|
| 1144 | * @param string Language ID |
|---|
| 1145 | */ |
|---|
| 1146 | public function load_language($lang = null, $add = array()) |
|---|
| 1147 | { |
|---|
| 1148 | $lang = $this->language_prop(($lang ? $lang : $_SESSION['language'])); |
|---|
| 1149 | |
|---|
| 1150 | // load localized texts |
|---|
| 1151 | if (empty($this->texts) || $lang != $_SESSION['language']) { |
|---|
| 1152 | $this->texts = array(); |
|---|
| 1153 | |
|---|
| 1154 | // handle empty lines after closing PHP tag in localization files |
|---|
| 1155 | ob_start(); |
|---|
| 1156 | |
|---|
| 1157 | // get english labels (these should be complete) |
|---|
| 1158 | @include(INSTALL_PATH . 'program/localization/en_US/labels.inc'); |
|---|
| 1159 | @include(INSTALL_PATH . 'program/localization/en_US/messages.inc'); |
|---|
| 1160 | |
|---|
| 1161 | if (is_array($labels)) |
|---|
| 1162 | $this->texts = $labels; |
|---|
| 1163 | if (is_array($messages)) |
|---|
| 1164 | $this->texts = array_merge($this->texts, $messages); |
|---|
| 1165 | |
|---|
| 1166 | // include user language files |
|---|
| 1167 | if ($lang != 'en' && is_dir(INSTALL_PATH . 'program/localization/' . $lang)) { |
|---|
| 1168 | include_once(INSTALL_PATH . 'program/localization/' . $lang . '/labels.inc'); |
|---|
| 1169 | include_once(INSTALL_PATH . 'program/localization/' . $lang . '/messages.inc'); |
|---|
| 1170 | |
|---|
| 1171 | if (is_array($labels)) |
|---|
| 1172 | $this->texts = array_merge($this->texts, $labels); |
|---|
| 1173 | if (is_array($messages)) |
|---|
| 1174 | $this->texts = array_merge($this->texts, $messages); |
|---|
| 1175 | } |
|---|
| 1176 | |
|---|
| 1177 | ob_end_clean(); |
|---|
| 1178 | |
|---|
| 1179 | $_SESSION['language'] = $lang; |
|---|
| 1180 | } |
|---|
| 1181 | |
|---|
| 1182 | // append additional texts (from plugin) |
|---|
| 1183 | if (is_array($add) && !empty($add)) |
|---|
| 1184 | $this->texts += $add; |
|---|
| 1185 | } |
|---|
| 1186 | |
|---|
| 1187 | |
|---|
| 1188 | /** |
|---|
| 1189 | * Read directory program/localization and return a list of available languages |
|---|
| 1190 | * |
|---|
| 1191 | * @return array List of available localizations |
|---|
| 1192 | */ |
|---|
| 1193 | public function list_languages() |
|---|
| 1194 | { |
|---|
| 1195 | static $sa_languages = array(); |
|---|
| 1196 | |
|---|
| 1197 | if (!sizeof($sa_languages)) { |
|---|
| 1198 | @include(INSTALL_PATH . 'program/localization/index.inc'); |
|---|
| 1199 | |
|---|
| 1200 | if ($dh = @opendir(INSTALL_PATH . 'program/localization')) { |
|---|
| 1201 | while (($name = readdir($dh)) !== false) { |
|---|
| 1202 | if ($name[0] == '.' || !is_dir(INSTALL_PATH . 'program/localization/' . $name)) |
|---|
| 1203 | continue; |
|---|
| 1204 | |
|---|
| 1205 | if ($label = $rcube_languages[$name]) |
|---|
| 1206 | $sa_languages[$name] = $label; |
|---|
| 1207 | } |
|---|
| 1208 | closedir($dh); |
|---|
| 1209 | } |
|---|
| 1210 | } |
|---|
| 1211 | |
|---|
| 1212 | return $sa_languages; |
|---|
| 1213 | } |
|---|
| 1214 | |
|---|
| 1215 | |
|---|
| 1216 | /** |
|---|
| 1217 | * Destroy session data and remove cookie |
|---|
| 1218 | */ |
|---|
| 1219 | public function kill_session() |
|---|
| 1220 | { |
|---|
| 1221 | $this->plugins->exec_hook('session_destroy'); |
|---|
| 1222 | |
|---|
| 1223 | $this->session->kill(); |
|---|
| 1224 | $_SESSION = array('language' => $this->user->language, 'temp' => true); |
|---|
| 1225 | $this->user->reset(); |
|---|
| 1226 | } |
|---|
| 1227 | |
|---|
| 1228 | |
|---|
| 1229 | /** |
|---|
| 1230 | * Do server side actions on logout |
|---|
| 1231 | */ |
|---|
| 1232 | public function logout_actions() |
|---|
| 1233 | { |
|---|
| 1234 | $config = $this->config->all(); |
|---|
| 1235 | |
|---|
| 1236 | // on logout action we're not connected to imap server |
|---|
| 1237 | if (($config['logout_purge'] && !empty($config['trash_mbox'])) || $config['logout_expunge']) { |
|---|
| 1238 | if (!$this->session->check_auth()) |
|---|
| 1239 | return; |
|---|
| 1240 | |
|---|
| 1241 | $this->storage_connect(); |
|---|
| 1242 | } |
|---|
| 1243 | |
|---|
| 1244 | if ($config['logout_purge'] && !empty($config['trash_mbox'])) { |
|---|
| 1245 | $this->storage->clear_folder($config['trash_mbox']); |
|---|
| 1246 | } |
|---|
| 1247 | |
|---|
| 1248 | if ($config['logout_expunge']) { |
|---|
| 1249 | $this->storage->expunge_folder('INBOX'); |
|---|
| 1250 | } |
|---|
| 1251 | |
|---|
| 1252 | // Try to save unsaved user preferences |
|---|
| 1253 | if (!empty($_SESSION['preferences'])) { |
|---|
| 1254 | $this->user->save_prefs(unserialize($_SESSION['preferences'])); |
|---|
| 1255 | } |
|---|
| 1256 | } |
|---|
| 1257 | |
|---|
| 1258 | |
|---|
| 1259 | /** |
|---|
| 1260 | * Function to be executed in script shutdown |
|---|
| 1261 | * Registered with register_shutdown_function() |
|---|
| 1262 | */ |
|---|
| 1263 | public function shutdown() |
|---|
| 1264 | { |
|---|
| 1265 | foreach ($this->shutdown_functions as $function) |
|---|
| 1266 | call_user_func($function); |
|---|
| 1267 | |
|---|
| 1268 | if (is_object($this->smtp)) |
|---|
| 1269 | $this->smtp->disconnect(); |
|---|
| 1270 | |
|---|
| 1271 | foreach ($this->address_books as $book) { |
|---|
| 1272 | if (is_object($book) && is_a($book, 'rcube_addressbook')) |
|---|
| 1273 | $book->close(); |
|---|
| 1274 | } |
|---|
| 1275 | |
|---|
| 1276 | foreach ($this->caches as $cache) { |
|---|
| 1277 | if (is_object($cache)) |
|---|
| 1278 | $cache->close(); |
|---|
| 1279 | } |
|---|
| 1280 | |
|---|
| 1281 | if (is_object($this->storage)) |
|---|
| 1282 | $this->storage->close(); |
|---|
| 1283 | |
|---|
| 1284 | // before closing the database connection, write session data |
|---|
| 1285 | if ($_SERVER['REMOTE_ADDR'] && is_object($this->session)) { |
|---|
| 1286 | session_write_close(); |
|---|
| 1287 | } |
|---|
| 1288 | |
|---|
| 1289 | // write performance stats to logs/console |
|---|
| 1290 | if ($this->config->get('devel_mode')) { |
|---|
| 1291 | if (function_exists('memory_get_usage')) |
|---|
| 1292 | $mem = show_bytes(memory_get_usage()); |
|---|
| 1293 | if (function_exists('memory_get_peak_usage')) |
|---|
| 1294 | $mem .= '/'.show_bytes(memory_get_peak_usage()); |
|---|
| 1295 | |
|---|
| 1296 | $log = $this->task . ($this->action ? '/'.$this->action : '') . ($mem ? " [$mem]" : ''); |
|---|
| 1297 | if (defined('RCMAIL_START')) |
|---|
| 1298 | rcube_print_time(RCMAIL_START, $log); |
|---|
| 1299 | else |
|---|
| 1300 | console($log); |
|---|
| 1301 | } |
|---|
| 1302 | } |
|---|
| 1303 | |
|---|
| 1304 | |
|---|
| 1305 | /** |
|---|
| 1306 | * Registers shutdown function to be executed on shutdown. |
|---|
| 1307 | * The functions will be executed before destroying any |
|---|
| 1308 | * objects like smtp, imap, session, etc. |
|---|
| 1309 | * |
|---|
| 1310 | * @param callback Function callback |
|---|
| 1311 | */ |
|---|
| 1312 | public function add_shutdown_function($function) |
|---|
| 1313 | { |
|---|
| 1314 | $this->shutdown_functions[] = $function; |
|---|
| 1315 | } |
|---|
| 1316 | |
|---|
| 1317 | |
|---|
| 1318 | /** |
|---|
| 1319 | * Generate a unique token to be used in a form request |
|---|
| 1320 | * |
|---|
| 1321 | * @return string The request token |
|---|
| 1322 | */ |
|---|
| 1323 | public function get_request_token() |
|---|
| 1324 | { |
|---|
| 1325 | $sess_id = $_COOKIE[ini_get('session.name')]; |
|---|
| 1326 | if (!$sess_id) $sess_id = session_id(); |
|---|
| 1327 | $plugin = $this->plugins->exec_hook('request_token', array('value' => md5('RT' . $this->user->ID . $this->config->get('des_key') . $sess_id))); |
|---|
| 1328 | return $plugin['value']; |
|---|
| 1329 | } |
|---|
| 1330 | |
|---|
| 1331 | |
|---|
| 1332 | /** |
|---|
| 1333 | * Check if the current request contains a valid token |
|---|
| 1334 | * |
|---|
| 1335 | * @param int Request method |
|---|
| 1336 | * @return boolean True if request token is valid false if not |
|---|
| 1337 | */ |
|---|
| 1338 | public function check_request($mode = RCUBE_INPUT_POST) |
|---|
| 1339 | { |
|---|
| 1340 | $token = get_input_value('_token', $mode); |
|---|
| 1341 | $sess_id = $_COOKIE[ini_get('session.name')]; |
|---|
| 1342 | return !empty($sess_id) && $token == $this->get_request_token(); |
|---|
| 1343 | } |
|---|
| 1344 | |
|---|
| 1345 | |
|---|
| 1346 | /** |
|---|
| 1347 | * Create unique authorization hash |
|---|
| 1348 | * |
|---|
| 1349 | * @param string Session ID |
|---|
| 1350 | * @param int Timestamp |
|---|
| 1351 | * @return string The generated auth hash |
|---|
| 1352 | */ |
|---|
| 1353 | private function get_auth_hash($sess_id, $ts) |
|---|
| 1354 | { |
|---|
| 1355 | $auth_string = sprintf('rcmail*sess%sR%s*Chk:%s;%s', |
|---|
| 1356 | $sess_id, |
|---|
| 1357 | $ts, |
|---|
| 1358 | $this->config->get('ip_check') ? $_SERVER['REMOTE_ADDR'] : '***.***.***.***', |
|---|
| 1359 | $_SERVER['HTTP_USER_AGENT']); |
|---|
| 1360 | |
|---|
| 1361 | if (function_exists('sha1')) |
|---|
| 1362 | return sha1($auth_string); |
|---|
| 1363 | else |
|---|
| 1364 | return md5($auth_string); |
|---|
| 1365 | } |
|---|
| 1366 | |
|---|
| 1367 | |
|---|
| 1368 | /** |
|---|
| 1369 | * Encrypt using 3DES |
|---|
| 1370 | * |
|---|
| 1371 | * @param string $clear clear text input |
|---|
| 1372 | * @param string $key encryption key to retrieve from the configuration, defaults to 'des_key' |
|---|
| 1373 | * @param boolean $base64 whether or not to base64_encode() the result before returning |
|---|
| 1374 | * |
|---|
| 1375 | * @return string encrypted text |
|---|
| 1376 | */ |
|---|
| 1377 | public function encrypt($clear, $key = 'des_key', $base64 = true) |
|---|
| 1378 | { |
|---|
| 1379 | if (!$clear) |
|---|
| 1380 | return ''; |
|---|
| 1381 | /*- |
|---|
| 1382 | * Add a single canary byte to the end of the clear text, which |
|---|
| 1383 | * will help find out how much of padding will need to be removed |
|---|
| 1384 | * upon decryption; see http://php.net/mcrypt_generic#68082 |
|---|
| 1385 | */ |
|---|
| 1386 | $clear = pack("a*H2", $clear, "80"); |
|---|
| 1387 | |
|---|
| 1388 | if (function_exists('mcrypt_module_open') && |
|---|
| 1389 | ($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_CBC, ""))) |
|---|
| 1390 | { |
|---|
| 1391 | $iv = $this->create_iv(mcrypt_enc_get_iv_size($td)); |
|---|
| 1392 | mcrypt_generic_init($td, $this->config->get_crypto_key($key), $iv); |
|---|
| 1393 | $cipher = $iv . mcrypt_generic($td, $clear); |
|---|
| 1394 | mcrypt_generic_deinit($td); |
|---|
| 1395 | mcrypt_module_close($td); |
|---|
| 1396 | } |
|---|
| 1397 | else { |
|---|
| 1398 | @include_once 'des.inc'; |
|---|
| 1399 | |
|---|
| 1400 | if (function_exists('des')) { |
|---|
| 1401 | $des_iv_size = 8; |
|---|
| 1402 | $iv = $this->create_iv($des_iv_size); |
|---|
| 1403 | $cipher = $iv . des($this->config->get_crypto_key($key), $clear, 1, 1, $iv); |
|---|
| 1404 | } |
|---|
| 1405 | else { |
|---|
| 1406 | raise_error(array( |
|---|
| 1407 | 'code' => 500, 'type' => 'php', |
|---|
| 1408 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 1409 | 'message' => "Could not perform encryption; make sure Mcrypt is installed or lib/des.inc is available" |
|---|
| 1410 | ), true, true); |
|---|
| 1411 | } |
|---|
| 1412 | } |
|---|
| 1413 | |
|---|
| 1414 | return $base64 ? base64_encode($cipher) : $cipher; |
|---|
| 1415 | } |
|---|
| 1416 | |
|---|
| 1417 | /** |
|---|
| 1418 | * Decrypt 3DES-encrypted string |
|---|
| 1419 | * |
|---|
| 1420 | * @param string $cipher encrypted text |
|---|
| 1421 | * @param string $key encryption key to retrieve from the configuration, defaults to 'des_key' |
|---|
| 1422 | * @param boolean $base64 whether or not input is base64-encoded |
|---|
| 1423 | * |
|---|
| 1424 | * @return string decrypted text |
|---|
| 1425 | */ |
|---|
| 1426 | public function decrypt($cipher, $key = 'des_key', $base64 = true) |
|---|
| 1427 | { |
|---|
| 1428 | if (!$cipher) |
|---|
| 1429 | return ''; |
|---|
| 1430 | |
|---|
| 1431 | $cipher = $base64 ? base64_decode($cipher) : $cipher; |
|---|
| 1432 | |
|---|
| 1433 | if (function_exists('mcrypt_module_open') && |
|---|
| 1434 | ($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_CBC, ""))) |
|---|
| 1435 | { |
|---|
| 1436 | $iv_size = mcrypt_enc_get_iv_size($td); |
|---|
| 1437 | $iv = substr($cipher, 0, $iv_size); |
|---|
| 1438 | |
|---|
| 1439 | // session corruption? (#1485970) |
|---|
| 1440 | if (strlen($iv) < $iv_size) |
|---|
| 1441 | return ''; |
|---|
| 1442 | |
|---|
| 1443 | $cipher = substr($cipher, $iv_size); |
|---|
| 1444 | mcrypt_generic_init($td, $this->config->get_crypto_key($key), $iv); |
|---|
| 1445 | $clear = mdecrypt_generic($td, $cipher); |
|---|
| 1446 | mcrypt_generic_deinit($td); |
|---|
| 1447 | mcrypt_module_close($td); |
|---|
| 1448 | } |
|---|
| 1449 | else { |
|---|
| 1450 | @include_once 'des.inc'; |
|---|
| 1451 | |
|---|
| 1452 | if (function_exists('des')) { |
|---|
| 1453 | $des_iv_size = 8; |
|---|
| 1454 | $iv = substr($cipher, 0, $des_iv_size); |
|---|
| 1455 | $cipher = substr($cipher, $des_iv_size); |
|---|
| 1456 | $clear = des($this->config->get_crypto_key($key), $cipher, 0, 1, $iv); |
|---|
| 1457 | } |
|---|
| 1458 | else { |
|---|
| 1459 | raise_error(array( |
|---|
| 1460 | 'code' => 500, 'type' => 'php', |
|---|
| 1461 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 1462 | 'message' => "Could not perform decryption; make sure Mcrypt is installed or lib/des.inc is available" |
|---|
| 1463 | ), true, true); |
|---|
| 1464 | } |
|---|
| 1465 | } |
|---|
| 1466 | |
|---|
| 1467 | /*- |
|---|
| 1468 | * Trim PHP's padding and the canary byte; see note in |
|---|
| 1469 | * rcmail::encrypt() and http://php.net/mcrypt_generic#68082 |
|---|
| 1470 | */ |
|---|
| 1471 | $clear = substr(rtrim($clear, "\0"), 0, -1); |
|---|
| 1472 | |
|---|
| 1473 | return $clear; |
|---|
| 1474 | } |
|---|
| 1475 | |
|---|
| 1476 | /** |
|---|
| 1477 | * Generates encryption initialization vector (IV) |
|---|
| 1478 | * |
|---|
| 1479 | * @param int Vector size |
|---|
| 1480 | * @return string Vector string |
|---|
| 1481 | */ |
|---|
| 1482 | private function create_iv($size) |
|---|
| 1483 | { |
|---|
| 1484 | // mcrypt_create_iv() can be slow when system lacks entrophy |
|---|
| 1485 | // we'll generate IV vector manually |
|---|
| 1486 | $iv = ''; |
|---|
| 1487 | for ($i = 0; $i < $size; $i++) |
|---|
| 1488 | $iv .= chr(mt_rand(0, 255)); |
|---|
| 1489 | return $iv; |
|---|
| 1490 | } |
|---|
| 1491 | |
|---|
| 1492 | /** |
|---|
| 1493 | * Build a valid URL to this instance of Roundcube |
|---|
| 1494 | * |
|---|
| 1495 | * @param mixed Either a string with the action or url parameters as key-value pairs |
|---|
| 1496 | * @return string Valid application URL |
|---|
| 1497 | */ |
|---|
| 1498 | public function url($p) |
|---|
| 1499 | { |
|---|
| 1500 | if (!is_array($p)) |
|---|
| 1501 | $p = array('_action' => @func_get_arg(0)); |
|---|
| 1502 | |
|---|
| 1503 | $task = $p['_task'] ? $p['_task'] : ($p['task'] ? $p['task'] : $this->task); |
|---|
| 1504 | $p['_task'] = $task; |
|---|
| 1505 | unset($p['task']); |
|---|
| 1506 | |
|---|
| 1507 | $url = './'; |
|---|
| 1508 | $delm = '?'; |
|---|
| 1509 | foreach (array_reverse($p) as $key => $val) { |
|---|
| 1510 | if ($val !== '' && $val !== null) { |
|---|
| 1511 | $par = $key[0] == '_' ? $key : '_'.$key; |
|---|
| 1512 | $url .= $delm.urlencode($par).'='.urlencode($val); |
|---|
| 1513 | $delm = '&'; |
|---|
| 1514 | } |
|---|
| 1515 | } |
|---|
| 1516 | return $url; |
|---|
| 1517 | } |
|---|
| 1518 | |
|---|
| 1519 | |
|---|
| 1520 | /** |
|---|
| 1521 | * Use imagemagick or GD lib to read image properties |
|---|
| 1522 | * |
|---|
| 1523 | * @param string Absolute file path |
|---|
| 1524 | * @return mixed Hash array with image props like type, width, height or False on error |
|---|
| 1525 | */ |
|---|
| 1526 | public static function imageprops($filepath) |
|---|
| 1527 | { |
|---|
| 1528 | $rcmail = rcmail::get_instance(); |
|---|
| 1529 | if ($cmd = $rcmail->config->get('im_identify_path', false)) { |
|---|
| 1530 | list(, $type, $size) = explode(' ', strtolower(rcmail::exec($cmd. ' 2>/dev/null {in}', array('in' => $filepath)))); |
|---|
| 1531 | if ($size) |
|---|
| 1532 | list($width, $height) = explode('x', $size); |
|---|
| 1533 | } |
|---|
| 1534 | else if (function_exists('getimagesize')) { |
|---|
| 1535 | $imsize = @getimagesize($filepath); |
|---|
| 1536 | $width = $imsize[0]; |
|---|
| 1537 | $height = $imsize[1]; |
|---|
| 1538 | $type = preg_replace('!image/!', '', $imsize['mime']); |
|---|
| 1539 | } |
|---|
| 1540 | |
|---|
| 1541 | return $type ? array('type' => $type, 'width' => $width, 'height' => $height) : false; |
|---|
| 1542 | } |
|---|
| 1543 | |
|---|
| 1544 | |
|---|
| 1545 | /** |
|---|
| 1546 | * Convert an image to a given size and type using imagemagick (ensures input is an image) |
|---|
| 1547 | * |
|---|
| 1548 | * @param $p['in'] Input filename (mandatory) |
|---|
| 1549 | * @param $p['out'] Output filename (mandatory) |
|---|
| 1550 | * @param $p['size'] Width x height of resulting image, e.g. "160x60" |
|---|
| 1551 | * @param $p['type'] Output file type, e.g. "jpg" |
|---|
| 1552 | * @param $p['-opts'] Custom command line options to ImageMagick convert |
|---|
| 1553 | * @return Success of convert as true/false |
|---|
| 1554 | */ |
|---|
| 1555 | public static function imageconvert($p) |
|---|
| 1556 | { |
|---|
| 1557 | $result = false; |
|---|
| 1558 | $rcmail = rcmail::get_instance(); |
|---|
| 1559 | $convert = $rcmail->config->get('im_convert_path', false); |
|---|
| 1560 | $identify = $rcmail->config->get('im_identify_path', false); |
|---|
| 1561 | |
|---|
| 1562 | // imagemagick is required for this |
|---|
| 1563 | if (!$convert) |
|---|
| 1564 | return false; |
|---|
| 1565 | |
|---|
| 1566 | if (!(($imagetype = @exif_imagetype($p['in'])) && ($type = image_type_to_extension($imagetype, false)))) |
|---|
| 1567 | list(, $type) = explode(' ', strtolower(rcmail::exec($identify . ' 2>/dev/null {in}', $p))); # for things like eps |
|---|
| 1568 | |
|---|
| 1569 | $type = strtr($type, array("jpeg" => "jpg", "tiff" => "tif", "ps" => "eps", "ept" => "eps")); |
|---|
| 1570 | $p += array('type' => $type, 'types' => "bmp,eps,gif,jp2,jpg,png,svg,tif", 'quality' => 75); |
|---|
| 1571 | $p['-opts'] = array('-resize' => $p['size'].'>') + (array)$p['-opts']; |
|---|
| 1572 | |
|---|
| 1573 | if (in_array($type, explode(',', $p['types']))) # Valid type? |
|---|
| 1574 | $result = rcmail::exec($convert . ' 2>&1 -flatten -auto-orient -colorspace RGB -quality {quality} {-opts} {in} {type}:{out}', $p) === ""; |
|---|
| 1575 | |
|---|
| 1576 | return $result; |
|---|
| 1577 | } |
|---|
| 1578 | |
|---|
| 1579 | |
|---|
| 1580 | /** |
|---|
| 1581 | * Construct shell command, execute it and return output as string. |
|---|
| 1582 | * Keywords {keyword} are replaced with arguments |
|---|
| 1583 | * |
|---|
| 1584 | * @param $cmd Format string with {keywords} to be replaced |
|---|
| 1585 | * @param $values (zero, one or more arrays can be passed) |
|---|
| 1586 | * @return output of command. shell errors not detectable |
|---|
| 1587 | */ |
|---|
| 1588 | public static function exec(/* $cmd, $values1 = array(), ... */) |
|---|
| 1589 | { |
|---|
| 1590 | $args = func_get_args(); |
|---|
| 1591 | $cmd = array_shift($args); |
|---|
| 1592 | $values = $replacements = array(); |
|---|
| 1593 | |
|---|
| 1594 | // merge values into one array |
|---|
| 1595 | foreach ($args as $arg) |
|---|
| 1596 | $values += (array)$arg; |
|---|
| 1597 | |
|---|
| 1598 | preg_match_all('/({(-?)([a-z]\w*)})/', $cmd, $matches, PREG_SET_ORDER); |
|---|
| 1599 | foreach ($matches as $tags) { |
|---|
| 1600 | list(, $tag, $option, $key) = $tags; |
|---|
| 1601 | $parts = array(); |
|---|
| 1602 | |
|---|
| 1603 | if ($option) { |
|---|
| 1604 | foreach ((array)$values["-$key"] as $key => $value) { |
|---|
| 1605 | if ($value === true || $value === false || $value === null) |
|---|
| 1606 | $parts[] = $value ? $key : ""; |
|---|
| 1607 | else foreach ((array)$value as $val) |
|---|
| 1608 | $parts[] = "$key " . escapeshellarg($val); |
|---|
| 1609 | } |
|---|
| 1610 | } |
|---|
| 1611 | else { |
|---|
| 1612 | foreach ((array)$values[$key] as $value) |
|---|
| 1613 | $parts[] = escapeshellarg($value); |
|---|
| 1614 | } |
|---|
| 1615 | |
|---|
| 1616 | $replacements[$tag] = join(" ", $parts); |
|---|
| 1617 | } |
|---|
| 1618 | |
|---|
| 1619 | // use strtr behaviour of going through source string once |
|---|
| 1620 | $cmd = strtr($cmd, $replacements); |
|---|
| 1621 | |
|---|
| 1622 | return (string)shell_exec($cmd); |
|---|
| 1623 | } |
|---|
| 1624 | |
|---|
| 1625 | |
|---|
| 1626 | /** |
|---|
| 1627 | * Helper method to set a cookie with the current path and host settings |
|---|
| 1628 | * |
|---|
| 1629 | * @param string Cookie name |
|---|
| 1630 | * @param string Cookie value |
|---|
| 1631 | * @param string Expiration time |
|---|
| 1632 | */ |
|---|
| 1633 | public static function setcookie($name, $value, $exp = 0) |
|---|
| 1634 | { |
|---|
| 1635 | if (headers_sent()) |
|---|
| 1636 | return; |
|---|
| 1637 | |
|---|
| 1638 | $cookie = session_get_cookie_params(); |
|---|
| 1639 | |
|---|
| 1640 | setcookie($name, $value, $exp, $cookie['path'], $cookie['domain'], |
|---|
| 1641 | rcube_https_check(), true); |
|---|
| 1642 | } |
|---|
| 1643 | |
|---|
| 1644 | /** |
|---|
| 1645 | * Registers action aliases for current task |
|---|
| 1646 | * |
|---|
| 1647 | * @param array $map Alias-to-filename hash array |
|---|
| 1648 | */ |
|---|
| 1649 | public function register_action_map($map) |
|---|
| 1650 | { |
|---|
| 1651 | if (is_array($map)) { |
|---|
| 1652 | foreach ($map as $idx => $val) { |
|---|
| 1653 | $this->action_map[$idx] = $val; |
|---|
| 1654 | } |
|---|
| 1655 | } |
|---|
| 1656 | } |
|---|
| 1657 | |
|---|
| 1658 | /** |
|---|
| 1659 | * Returns current action filename |
|---|
| 1660 | * |
|---|
| 1661 | * @param array $map Alias-to-filename hash array |
|---|
| 1662 | */ |
|---|
| 1663 | public function get_action_file() |
|---|
| 1664 | { |
|---|
| 1665 | if (!empty($this->action_map[$this->action])) { |
|---|
| 1666 | return $this->action_map[$this->action]; |
|---|
| 1667 | } |
|---|
| 1668 | |
|---|
| 1669 | return strtr($this->action, '-', '_') . '.inc'; |
|---|
| 1670 | } |
|---|
| 1671 | |
|---|
| 1672 | /** |
|---|
| 1673 | * Fixes some user preferences according to namespace handling change. |
|---|
| 1674 | * Old Roundcube versions were using folder names with removed namespace prefix. |
|---|
| 1675 | * Now we need to add the prefix on servers where personal namespace has prefix. |
|---|
| 1676 | * |
|---|
| 1677 | * @param rcube_user $user User object |
|---|
| 1678 | */ |
|---|
| 1679 | private function fix_namespace_settings($user) |
|---|
| 1680 | { |
|---|
| 1681 | $prefix = $this->storage->get_namespace('prefix'); |
|---|
| 1682 | $prefix_len = strlen($prefix); |
|---|
| 1683 | |
|---|
| 1684 | if (!$prefix_len) |
|---|
| 1685 | return; |
|---|
| 1686 | |
|---|
| 1687 | $prefs = $this->config->all(); |
|---|
| 1688 | if (!empty($prefs['namespace_fixed'])) |
|---|
| 1689 | return; |
|---|
| 1690 | |
|---|
| 1691 | // Build namespace prefix regexp |
|---|
| 1692 | $ns = $this->storage->get_namespace(); |
|---|
| 1693 | $regexp = array(); |
|---|
| 1694 | |
|---|
| 1695 | foreach ($ns as $entry) { |
|---|
| 1696 | if (!empty($entry)) { |
|---|
| 1697 | foreach ($entry as $item) { |
|---|
| 1698 | if (strlen($item[0])) { |
|---|
| 1699 | $regexp[] = preg_quote($item[0], '/'); |
|---|
| 1700 | } |
|---|
| 1701 | } |
|---|
| 1702 | } |
|---|
| 1703 | } |
|---|
| 1704 | $regexp = '/^('. implode('|', $regexp).')/'; |
|---|
| 1705 | |
|---|
| 1706 | // Fix preferences |
|---|
| 1707 | $opts = array('drafts_mbox', 'junk_mbox', 'sent_mbox', 'trash_mbox', 'archive_mbox'); |
|---|
| 1708 | foreach ($opts as $opt) { |
|---|
| 1709 | if ($value = $prefs[$opt]) { |
|---|
| 1710 | if ($value != 'INBOX' && !preg_match($regexp, $value)) { |
|---|
| 1711 | $prefs[$opt] = $prefix.$value; |
|---|
| 1712 | } |
|---|
| 1713 | } |
|---|
| 1714 | } |
|---|
| 1715 | |
|---|
| 1716 | if (!empty($prefs['default_folders'])) { |
|---|
| 1717 | foreach ($prefs['default_folders'] as $idx => $name) { |
|---|
| 1718 | if ($name != 'INBOX' && !preg_match($regexp, $name)) { |
|---|
| 1719 | $prefs['default_folders'][$idx] = $prefix.$name; |
|---|
| 1720 | } |
|---|
| 1721 | } |
|---|
| 1722 | } |
|---|
| 1723 | |
|---|
| 1724 | if (!empty($prefs['search_mods'])) { |
|---|
| 1725 | $folders = array(); |
|---|
| 1726 | foreach ($prefs['search_mods'] as $idx => $value) { |
|---|
| 1727 | if ($idx != 'INBOX' && $idx != '*' && !preg_match($regexp, $idx)) { |
|---|
| 1728 | $idx = $prefix.$idx; |
|---|
| 1729 | } |
|---|
| 1730 | $folders[$idx] = $value; |
|---|
| 1731 | } |
|---|
| 1732 | $prefs['search_mods'] = $folders; |
|---|
| 1733 | } |
|---|
| 1734 | |
|---|
| 1735 | if (!empty($prefs['message_threading'])) { |
|---|
| 1736 | $folders = array(); |
|---|
| 1737 | foreach ($prefs['message_threading'] as $idx => $value) { |
|---|
| 1738 | if ($idx != 'INBOX' && !preg_match($regexp, $idx)) { |
|---|
| 1739 | $idx = $prefix.$idx; |
|---|
| 1740 | } |
|---|
| 1741 | $folders[$prefix.$idx] = $value; |
|---|
| 1742 | } |
|---|
| 1743 | $prefs['message_threading'] = $folders; |
|---|
| 1744 | } |
|---|
| 1745 | |
|---|
| 1746 | if (!empty($prefs['collapsed_folders'])) { |
|---|
| 1747 | $folders = explode('&&', $prefs['collapsed_folders']); |
|---|
| 1748 | $count = count($folders); |
|---|
| 1749 | $folders_str = ''; |
|---|
| 1750 | |
|---|
| 1751 | if ($count) { |
|---|
| 1752 | $folders[0] = substr($folders[0], 1); |
|---|
| 1753 | $folders[$count-1] = substr($folders[$count-1], 0, -1); |
|---|
| 1754 | } |
|---|
| 1755 | |
|---|
| 1756 | foreach ($folders as $value) { |
|---|
| 1757 | if ($value != 'INBOX' && !preg_match($regexp, $value)) { |
|---|
| 1758 | $value = $prefix.$value; |
|---|
| 1759 | } |
|---|
| 1760 | $folders_str .= '&'.$value.'&'; |
|---|
| 1761 | } |
|---|
| 1762 | $prefs['collapsed_folders'] = $folders_str; |
|---|
| 1763 | } |
|---|
| 1764 | |
|---|
| 1765 | $prefs['namespace_fixed'] = true; |
|---|
| 1766 | |
|---|
| 1767 | // save updated preferences and reset imap settings (default folders) |
|---|
| 1768 | $user->save_prefs($prefs); |
|---|
| 1769 | $this->set_storage_prop(); |
|---|
| 1770 | } |
|---|
| 1771 | |
|---|
| 1772 | } |
|---|