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