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