4 +-----------------------------------------------------------------------+
5 | program/include/rcmail.php |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2008-2011, The Roundcube Dev Team |
9 | Copyright (C) 2011, Kolab Systems AG |
10 | Licensed under the GNU GPL |
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 +-----------------------------------------------------------------------+
19 $Id: rcmail.php 5897 2012-02-21 20:46:15Z thomasb $
25 * Application class of Roundcube Webmail
26 * implemented as singleton
37 static public $main_tasks = array('mail','settings','addressbook','login','logout','utils','dummy');
40 * Singleton instace of rcmail
44 static private $instance;
47 * Stores instance of rcube_config.
54 * Stores rcube_user instance.
61 * Instace of database class.
68 * Instace of Memcache class.
75 * Instace of rcube_session class.
82 * Instance of rcube_smtp class.
89 * Instance of rcube_imap class.
96 * Instance of rcube_template class.
103 * Instance of rcube_plugin_api.
105 * @var rcube_plugin_api
122 public $comm_path = './';
125 private $address_books = array();
126 private $caches = array();
127 private $action_map = array();
128 private $shutdown_functions = array();
132 * This implements the 'singleton' design pattern
134 * @return rcmail The one and only instance
136 static function get_instance()
138 if (!self::$instance) {
139 self::$instance = new rcmail();
140 self::$instance->startup(); // init AFTER object was linked with self::$instance
143 return self::$instance;
148 * Private constructor
150 private function __construct()
152 // load configuration
153 $this->config = new rcube_config();
155 register_shutdown_function(array($this, 'shutdown'));
160 * Initial startup function
161 * to register session, create database and imap connections
163 * @todo Remove global vars $DB, $USER
165 private function startup()
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);
174 // connect to database
175 $GLOBALS['DB'] = $this->get_dbh();
178 $this->session_init();
180 // create user object
181 $this->set_user(new rcube_user($_SESSION['user_id']));
183 // configure session (after user config merge!)
184 $this->session_configure();
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));
190 // reset some session parameters when changing task
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;
199 if (!empty($_REQUEST['_remote']))
200 $GLOBALS['OUTPUT'] = $this->json_init();
202 $GLOBALS['OUTPUT'] = $this->load_gui(!empty($_REQUEST['_framed']));
204 // create plugin API and load plugins
205 $this->plugins = rcube_plugin_api::get_instance();
208 $this->plugins->init();
213 * Setter for application task
215 * @param string Task to set
217 public function set_task($task)
219 $task = asciiwords($task);
221 if ($this->user && $this->user->ID)
222 $task = !$task ? 'mail' : $task;
227 $this->comm_path = $this->url(array('task' => $this->task));
230 $this->output->set_env('task', $this->task);
235 * Setter for system user object
237 * @param rcube_user Current user instance
239 public function set_user($user)
241 if (is_object($user)) {
243 $GLOBALS['USER'] = $this->user;
245 // overwrite config with user preferences
246 $this->config->set_user_prefs((array)$this->user->get_prefs());
249 $_SESSION['language'] = $this->user->language = $this->language_prop($this->config->get('language', $_SESSION['language']));
252 setlocale(LC_ALL, $_SESSION['language'] . '.utf8', 'en_US.utf8');
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');
261 * Check the given string and return a valid language code
263 * @param string Language code
264 * @return string Valid language code
266 private function language_prop($lang)
268 static $rcube_languages, $rcube_language_aliases;
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]);
276 if (empty($rcube_languages)) {
277 @include(INSTALL_PATH . 'program/localization/index.inc');
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];
284 // try the first two chars
285 else if (!isset($rcube_languages[$lang])) {
286 $short = substr($lang, 0, 2);
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];
292 // expand 'nn' to 'nn_NN'
293 else if (!isset($rcube_languages[$short])) {
294 $lang = $short.'_'.strtoupper($short);
298 if (!isset($rcube_languages[$lang]) || !is_dir(INSTALL_PATH . 'program/localization/' . $lang)) {
307 * Get the current database connection
309 * @return rcube_mdb2 Database connection object
311 public function get_dbh()
314 $config_all = $this->config->all();
316 $this->db = new rcube_mdb2($config_all['db_dsnw'], $config_all['db_dsnr'], $config_all['db_persistent']);
317 $this->db->sqlite_initials = INSTALL_PATH . 'SQL/sqlite.initial.sql';
318 $this->db->set_debug((bool)$config_all['sql_debug']);
326 * Get global handle for memcache access
328 * @return object Memcache
330 public function get_memcache()
332 if (!isset($this->memcache)) {
333 // no memcache support in PHP
334 if (!class_exists('Memcache')) {
335 $this->memcache = false;
339 $this->memcache = new Memcache;
340 $this->mc_available = 0;
342 // add alll configured hosts to pool
343 $pconnect = $this->config->get('memcache_pconnect', true);
344 foreach ($this->config->get('memcache_hosts', array()) as $host) {
345 list($host, $port) = explode(':', $host);
346 if (!$port) $port = 11211;
347 $this->mc_available += intval($this->memcache->addServer($host, $port, $pconnect, 1, 1, 15, false, array($this, 'memcache_failure')));
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
353 if (!$this->mc_available)
354 $this->memcache = false;
357 return $this->memcache;
361 * Callback for memcache failure
363 public function memcache_failure($host, $port)
365 static $seen = array();
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"),
379 * Initialize and get cache object
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
386 * @return rcube_cache Cache object
388 public function get_cache($name, $type='db', $ttl=0, $packed=true)
390 if (!isset($this->caches[$name])) {
391 $this->caches[$name] = new rcube_cache($type, $_SESSION['user_id'], $name, $ttl, $packed);
394 return $this->caches[$name];
399 * Return instance of the internal address book class
401 * @param string Address book identifier
402 * @param boolean True if the address book needs to be writeable
404 * @return rcube_contacts Address book object
406 public function get_address_book($id, $writeable = false)
409 $ldap_config = (array)$this->config->get('ldap_public');
410 $abook_type = strtolower($this->config->get('address_book_type'));
412 // 'sql' is the alias for '0' used by autocomplete
416 // use existing instance
417 if (isset($this->address_books[$id]) && is_object($this->address_books[$id])
418 && is_a($this->address_books[$id], 'rcube_addressbook')
419 && (!$writeable || !$this->address_books[$id]->readonly)
421 $contacts = $this->address_books[$id];
423 else if ($id && $ldap_config[$id]) {
424 $contacts = new rcube_ldap($ldap_config[$id], $this->config->get('ldap_debug'), $this->config->mail_domain($_SESSION['imap_host']));
426 else if ($id === '0') {
427 $contacts = new rcube_contacts($this->db, $this->user->ID);
430 $plugin = $this->plugins->exec_hook('addressbook_get', array('id' => $id, 'writeable' => $writeable));
432 // plugin returned instance of a rcube_addressbook
433 if ($plugin['instance'] instanceof rcube_addressbook) {
434 $contacts = $plugin['instance'];
436 // get first source from the list
438 $source = reset($this->get_address_sources($writeable));
439 if (!empty($source)) {
440 $contacts = $this->get_address_book($source['id']);
449 'code' => 700, 'type' => 'php',
450 'file' => __FILE__, 'line' => __LINE__,
451 'message' => "Addressbook source ($id) not found!"),
455 // set configured sort order
456 if ($sort_col = $this->config->get('addressbook_sort_col'))
457 $contacts->set_sort_order($sort_col);
459 // add to the 'books' array for shutdown function
460 $this->address_books[$id] = $contacts;
467 * Return address books list
469 * @param boolean True if the address book needs to be writeable
471 * @return array Address books array
473 public function get_address_sources($writeable = false)
475 $abook_type = strtolower($this->config->get('address_book_type'));
476 $ldap_config = $this->config->get('ldap_public');
477 $autocomplete = (array) $this->config->get('autocomplete_addressbooks');
480 // We are using the DB address book
481 if ($abook_type != 'ldap') {
482 if (!isset($this->address_books['0']))
483 $this->address_books['0'] = new rcube_contacts($this->db, $this->user->ID);
486 'name' => rcube_label('personaladrbook'),
487 'groups' => $this->address_books['0']->groups,
488 'readonly' => $this->address_books['0']->readonly,
489 'autocomplete' => in_array('sql', $autocomplete),
490 'undelete' => $this->address_books['0']->undelete && $this->config->get('undo_timeout'),
495 $ldap_config = (array) $ldap_config;
496 foreach ($ldap_config as $id => $prop)
499 'name' => $prop['name'],
500 'groups' => is_array($prop['groups']),
501 'readonly' => !$prop['writable'],
502 'hidden' => $prop['hidden'],
503 'autocomplete' => in_array($id, $autocomplete)
507 $plugin = $this->plugins->exec_hook('addressbooks_list', array('sources' => $list));
508 $list = $plugin['sources'];
510 foreach ($list as $idx => $item) {
511 // register source for shutdown function
512 if (!is_object($this->address_books[$item['id']]))
513 $this->address_books[$item['id']] = $item;
514 // remove from list if not writeable as requested
515 if ($writeable && $item['readonly'])
524 * Init output object for GUI and add common scripts.
525 * This will instantiate a rcmail_template object and set
526 * environment vars according to the current session and configuration
528 * @param boolean True if this request is loaded in a (i)frame
529 * @return rcube_template Reference to HTML output object
531 public function load_gui($framed = false)
534 if (!($this->output instanceof rcube_template))
535 $this->output = new rcube_template($this->task, $framed);
537 // set keep-alive/check-recent interval
538 if ($this->session && ($keep_alive = $this->session->get_keep_alive())) {
539 $this->output->set_env('keep_alive', $keep_alive);
543 $this->comm_path .= '&_framed=1';
544 $this->output->set_env('framed', true);
547 $this->output->set_env('task', $this->task);
548 $this->output->set_env('action', $this->action);
549 $this->output->set_env('comm_path', $this->comm_path);
550 $this->output->set_charset(RCMAIL_CHARSET);
552 // add some basic labels to client
553 $this->output->add_label('loading', 'servererror');
555 return $this->output;
560 * Create an output object for JSON responses
562 * @return rcube_json_output Reference to JSON output object
564 public function json_init()
566 if (!($this->output instanceof rcube_json_output))
567 $this->output = new rcube_json_output($this->task);
569 return $this->output;
574 * Create SMTP object and connect to server
576 * @param boolean True if connection should be established
578 public function smtp_init($connect = false)
580 $this->smtp = new rcube_smtp();
583 $this->smtp->connect();
588 * Create global IMAP object and connect to server
590 * @param boolean True if connection should be established
591 * @todo Remove global $IMAP
593 public function imap_init($connect = false)
595 // already initialized
596 if (is_object($this->imap))
599 $this->imap = new rcube_imap();
600 $this->imap->skip_deleted = $this->config->get('skip_deleted');
602 // enable caching of imap data
603 $imap_cache = $this->config->get('imap_cache');
604 $messages_cache = $this->config->get('messages_cache');
605 // for backward compatybility
606 if ($imap_cache === null && $messages_cache === null && $this->config->get('enable_caching')) {
608 $messages_cache = true;
611 $this->imap->set_caching($imap_cache);
613 $this->imap->set_messages_caching(true);
615 // set pagesize from config
616 $this->imap->set_pagesize($this->config->get('pagesize', 50));
618 // Setting root and delimiter before establishing the connection
619 // can save time detecting them using NAMESPACE and LIST
621 'auth_type' => $this->config->get('imap_auth_type', 'check'),
622 'auth_cid' => $this->config->get('imap_auth_cid'),
623 'auth_pw' => $this->config->get('imap_auth_pw'),
624 'debug' => (bool) $this->config->get('imap_debug', 0),
625 'force_caps' => (bool) $this->config->get('imap_force_caps'),
626 'timeout' => (int) $this->config->get('imap_timeout', 0),
629 $this->imap->set_options($options);
631 // set global object for backward compatibility
632 $GLOBALS['IMAP'] = $this->imap;
634 $hook = $this->plugins->exec_hook('imap_init', array('fetch_headers' => $this->imap->fetch_add_headers));
635 if ($hook['fetch_headers'])
636 $this->imap->fetch_add_headers = $hook['fetch_headers'];
638 // support this parameter for backward compatibility but log warning
640 $this->imap_connect();
642 'code' => 800, 'type' => 'imap',
643 'file' => __FILE__, 'line' => __LINE__,
644 'message' => "rcube::imap_init(true) is deprecated, use rcube::imap_connect() instead"),
651 * Connect to IMAP server with stored session data
653 * @return bool True on success, false on error
655 public function imap_connect()
660 if ($_SESSION['imap_host'] && !$this->imap->conn->connected()) {
661 if (!$this->imap->connect($_SESSION['imap_host'], $_SESSION['username'], $this->decrypt($_SESSION['password']), $_SESSION['imap_port'], $_SESSION['imap_ssl'])) {
663 $this->output->show_message($this->imap->get_error_code() == -1 ? 'imaperror' : 'sessionerror', 'error');
666 $this->set_imap_prop();
667 return $this->imap->conn;
676 * Create session object and start the session.
678 public function session_init()
680 // session started (Installer?)
684 $sess_name = $this->config->get('session_name');
685 $sess_domain = $this->config->get('session_domain');
686 $lifetime = $this->config->get('session_lifetime', 0) * 60;
688 // set session domain
690 ini_set('session.cookie_domain', $sess_domain);
692 // set session garbage collecting time according to session_lifetime
694 ini_set('session.gc_maxlifetime', $lifetime * 2);
697 ini_set('session.cookie_secure', rcube_https_check());
698 ini_set('session.name', $sess_name ? $sess_name : 'roundcube_sessid');
699 ini_set('session.use_cookies', 1);
700 ini_set('session.use_only_cookies', 1);
701 ini_set('session.serialize_handler', 'php');
703 // use database for storing session data
704 $this->session = new rcube_session($this->get_dbh(), $this->config);
706 $this->session->register_gc_handler('rcmail_temp_gc');
707 $this->session->register_gc_handler('rcmail_cache_gc');
709 // start PHP session (if not in CLI mode)
710 if ($_SERVER['REMOTE_ADDR'])
713 // set initial session vars
714 if (!$_SESSION['user_id'])
715 $_SESSION['temp'] = true;
720 * Configure session object internals
722 public function session_configure()
727 $lifetime = $this->config->get('session_lifetime', 0) * 60;
729 // set keep-alive/check-recent interval
730 if ($keep_alive = $this->config->get('keep_alive')) {
731 // be sure that it's less than session lifetime
733 $keep_alive = min($keep_alive, $lifetime - 30);
734 $keep_alive = max(60, $keep_alive);
735 $this->session->set_keep_alive($keep_alive);
738 $this->session->set_secret($this->config->get('des_key') . $_SERVER['HTTP_USER_AGENT']);
739 $this->session->set_ip_check($this->config->get('ip_check'));
744 * Perfom login to the IMAP server and to the webmail service.
745 * This will also create a new user entry if auto_create_user is configured.
747 * @param string IMAP user name
748 * @param string IMAP password
749 * @param string IMAP host
750 * @return boolean True on success, False on failure
752 function login($username, $pass, $host=NULL)
755 $config = $this->config->all();
758 $host = $config['default_host'];
760 // Validate that selected host is in the list of configured hosts
761 if (is_array($config['default_host'])) {
763 foreach ($config['default_host'] as $key => $host_allowed) {
764 if (!is_numeric($key))
765 $host_allowed = $key;
766 if ($host == $host_allowed) {
774 else if (!empty($config['default_host']) && $host != rcube_parse_host($config['default_host']))
778 $a_host = parse_url($host);
779 if ($a_host['host']) {
780 $host = $a_host['host'];
781 $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? $a_host['scheme'] : null;
782 if (!empty($a_host['port']))
783 $imap_port = $a_host['port'];
784 else if ($imap_ssl && $imap_ssl != 'tls' && (!$config['default_port'] || $config['default_port'] == 143))
788 $imap_port = $imap_port ? $imap_port : $config['default_port'];
790 /* Modify username with domain if required
791 Inspired by Marco <P0L0_notspam_binware.org>
793 // Check if we need to add domain
794 if (!empty($config['username_domain']) && strpos($username, '@') === false) {
795 if (is_array($config['username_domain']) && isset($config['username_domain'][$host]))
796 $username .= '@'.rcube_parse_host($config['username_domain'][$host], $host);
797 else if (is_string($config['username_domain']))
798 $username .= '@'.rcube_parse_host($config['username_domain'], $host);
801 // Convert username to lowercase. If IMAP backend
802 // is case-insensitive we need to store always the same username (#1487113)
803 if ($config['login_lc']) {
804 $username = mb_strtolower($username);
807 // try to resolve email address from virtuser table
808 if (strpos($username, '@') && ($virtuser = rcube_user::email2user($username))) {
809 $username = $virtuser;
812 // Here we need IDNA ASCII
813 // Only rcube_contacts class is using domain names in Unicode
814 $host = rcube_idn_to_ascii($host);
815 if (strpos($username, '@')) {
816 // lowercase domain name
817 list($local, $domain) = explode('@', $username);
818 $username = $local . '@' . mb_strtolower($domain);
819 $username = rcube_idn_to_ascii($username);
822 // user already registered -> overwrite username
823 if ($user = rcube_user::query($username, $host))
824 $username = $user->data['username'];
830 if (!($imap_login = $this->imap->connect($host, $username, $pass, $imap_port, $imap_ssl))) {
831 // try with lowercase
832 $username_lc = mb_strtolower($username);
833 if ($username_lc != $username) {
834 // try to find user record again -> overwrite username
835 if (!$user && ($user = rcube_user::query($username_lc, $host)))
836 $username_lc = $user->data['username'];
838 if ($imap_login = $this->imap->connect($host, $username_lc, $pass, $imap_port, $imap_ssl))
839 $username = $username_lc;
843 // exit if IMAP login failed
847 // user already registered -> update user's record
848 if (is_object($user)) {
849 // update last login timestamp
852 // create new system user
853 else if ($config['auto_create_user']) {
854 if ($created = rcube_user::create($username, $host)) {
859 'code' => 620, 'type' => 'php',
860 'file' => __FILE__, 'line' => __LINE__,
861 'message' => "Failed to create a user record. Maybe aborted by a plugin?"
867 'code' => 621, 'type' => 'php',
868 'file' => __FILE__, 'line' => __LINE__,
869 'message' => "Access denied for new user $username. 'auto_create_user' is disabled"
874 if (is_object($user) && $user->ID) {
875 // Configure environment
876 $this->set_user($user);
877 $this->set_imap_prop();
878 $this->session_configure();
880 // fix some old settings according to namespace prefix
881 $this->fix_namespace_settings($user);
883 // create default folders on first login
884 if ($config['create_default_folders'] && (!empty($created) || empty($user->data['last_login']))) {
885 $this->imap->create_default_folders();
889 $_SESSION['user_id'] = $user->ID;
890 $_SESSION['username'] = $user->data['username'];
891 $_SESSION['imap_host'] = $host;
892 $_SESSION['imap_port'] = $imap_port;
893 $_SESSION['imap_ssl'] = $imap_ssl;
894 $_SESSION['password'] = $this->encrypt($pass);
895 $_SESSION['login_time'] = mktime();
897 if (isset($_REQUEST['_timezone']) && $_REQUEST['_timezone'] != '_default_')
898 $_SESSION['timezone'] = floatval($_REQUEST['_timezone']);
899 if (isset($_REQUEST['_dstactive']) && $_REQUEST['_dstactive'] != '_default_')
900 $_SESSION['dst_active'] = intval($_REQUEST['_dstactive']);
902 // force reloading complete list of subscribed mailboxes
903 $this->imap->clear_cache('mailboxes', true);
913 * Set root dir and last stored mailbox
914 * This must be done AFTER connecting to the server!
916 public function set_imap_prop()
918 $this->imap->set_charset($this->config->get('default_charset', RCMAIL_CHARSET));
920 if ($default_folders = $this->config->get('default_imap_folders')) {
921 $this->imap->set_default_mailboxes($default_folders);
923 if (isset($_SESSION['mbox'])) {
924 $this->imap->set_mailbox($_SESSION['mbox']);
926 if (isset($_SESSION['page'])) {
927 $this->imap->set_page($_SESSION['page']);
933 * Auto-select IMAP host based on the posted login information
935 * @return string Selected IMAP host
937 public function autoselect_host()
939 $default_host = $this->config->get('default_host');
942 if (is_array($default_host)) {
943 $post_host = get_input_value('_host', RCUBE_INPUT_POST);
945 // direct match in default_host array
946 if ($default_host[$post_host] || in_array($post_host, array_values($default_host))) {
950 // try to select host by mail domain
951 list($user, $domain) = explode('@', get_input_value('_user', RCUBE_INPUT_POST));
952 if (!empty($domain)) {
953 foreach ($default_host as $imap_host => $mail_domains) {
954 if (is_array($mail_domains) && in_array($domain, $mail_domains)) {
961 // take the first entry if $host is still an array
963 $host = array_shift($default_host);
966 else if (empty($default_host)) {
967 $host = get_input_value('_host', RCUBE_INPUT_POST);
970 $host = rcube_parse_host($default_host);
977 * Get localized text in the desired language
979 * @param mixed $attrib Named parameters array or label name
980 * @param string $domain Label domain (plugin) name
982 * @return string Localized text
984 public function gettext($attrib, $domain=null)
986 // load localization files if not done yet
987 if (empty($this->texts))
988 $this->load_language();
990 // extract attributes
991 if (is_string($attrib))
992 $attrib = array('name' => $attrib);
994 $nr = is_numeric($attrib['nr']) ? $attrib['nr'] : 1;
995 $name = $attrib['name'] ? $attrib['name'] : '';
997 // attrib contain text values: use them from now
998 if (($setval = $attrib[strtolower($_SESSION['language'])]) || ($setval = $attrib['en_us']))
999 $this->texts[$name] = $setval;
1001 // check for text with domain
1002 if ($domain && ($text_item = $this->texts[$domain.'.'.$name]))
1004 // text does not exist
1005 else if (!($text_item = $this->texts[$name])) {
1009 // make text item array
1010 $a_text_item = is_array($text_item) ? $text_item : array('single' => $text_item);
1012 // decide which text to use
1014 $text = $a_text_item['single'];
1017 $text = $a_text_item['multiple'];
1019 else if ($nr == 0) {
1020 if ($a_text_item['none'])
1021 $text = $a_text_item['none'];
1022 else if ($a_text_item['single'])
1023 $text = $a_text_item['single'];
1024 else if ($a_text_item['multiple'])
1025 $text = $a_text_item['multiple'];
1028 // default text is single
1030 $text = $a_text_item['single'];
1033 // replace vars in text
1034 if (is_array($attrib['vars'])) {
1035 foreach ($attrib['vars'] as $var_key => $var_value)
1036 $text = str_replace($var_key[0]!='$' ? '$'.$var_key : $var_key, $var_value, $text);
1040 if (($attrib['uppercase'] && strtolower($attrib['uppercase']=='first')) || $attrib['ucfirst'])
1041 return ucfirst($text);
1042 else if ($attrib['uppercase'])
1043 return mb_strtoupper($text);
1044 else if ($attrib['lowercase'])
1045 return mb_strtolower($text);
1052 * Check if the given text label exists
1054 * @param string $name Label name
1055 * @param string $domain Label domain (plugin) name or '*' for all domains
1056 * @param string $ref_domain Sets domain name if label is found
1058 * @return boolean True if text exists (either in the current language or in en_US)
1060 public function text_exists($name, $domain = null, &$ref_domain = null)
1062 // load localization files if not done yet
1063 if (empty($this->texts))
1064 $this->load_language();
1066 if (isset($this->texts[$name])) {
1071 // any of loaded domains (plugins)
1072 if ($domain == '*') {
1073 foreach ($this->plugins->loaded_plugins() as $domain)
1074 if (isset($this->texts[$domain.'.'.$name])) {
1075 $ref_domain = $domain;
1081 $ref_domain = $domain;
1082 return isset($this->texts[$domain.'.'.$name]);
1089 * Load a localization package
1091 * @param string Language ID
1093 public function load_language($lang = null, $add = array())
1095 $lang = $this->language_prop(($lang ? $lang : $_SESSION['language']));
1097 // load localized texts
1098 if (empty($this->texts) || $lang != $_SESSION['language']) {
1099 $this->texts = array();
1101 // handle empty lines after closing PHP tag in localization files
1104 // get english labels (these should be complete)
1105 @include(INSTALL_PATH . 'program/localization/en_US/labels.inc');
1106 @include(INSTALL_PATH . 'program/localization/en_US/messages.inc');
1108 if (is_array($labels))
1109 $this->texts = $labels;
1110 if (is_array($messages))
1111 $this->texts = array_merge($this->texts, $messages);
1113 // include user language files
1114 if ($lang != 'en' && is_dir(INSTALL_PATH . 'program/localization/' . $lang)) {
1115 include_once(INSTALL_PATH . 'program/localization/' . $lang . '/labels.inc');
1116 include_once(INSTALL_PATH . 'program/localization/' . $lang . '/messages.inc');
1118 if (is_array($labels))
1119 $this->texts = array_merge($this->texts, $labels);
1120 if (is_array($messages))
1121 $this->texts = array_merge($this->texts, $messages);
1126 $_SESSION['language'] = $lang;
1129 // append additional texts (from plugin)
1130 if (is_array($add) && !empty($add))
1131 $this->texts += $add;
1136 * Read directory program/localization and return a list of available languages
1138 * @return array List of available localizations
1140 public function list_languages()
1142 static $sa_languages = array();
1144 if (!sizeof($sa_languages)) {
1145 @include(INSTALL_PATH . 'program/localization/index.inc');
1147 if ($dh = @opendir(INSTALL_PATH . 'program/localization')) {
1148 while (($name = readdir($dh)) !== false) {
1149 if ($name[0] == '.' || !is_dir(INSTALL_PATH . 'program/localization/' . $name))
1152 if ($label = $rcube_languages[$name])
1153 $sa_languages[$name] = $label;
1159 return $sa_languages;
1164 * Destroy session data and remove cookie
1166 public function kill_session()
1168 $this->plugins->exec_hook('session_destroy');
1170 $this->session->kill();
1171 $_SESSION = array('language' => $this->user->language, 'temp' => true);
1172 $this->user->reset();
1177 * Do server side actions on logout
1179 public function logout_actions()
1181 $config = $this->config->all();
1183 // on logout action we're not connected to imap server
1184 if (($config['logout_purge'] && !empty($config['trash_mbox'])) || $config['logout_expunge']) {
1185 if (!$this->session->check_auth())
1188 $this->imap_connect();
1191 if ($config['logout_purge'] && !empty($config['trash_mbox'])) {
1192 $this->imap->clear_mailbox($config['trash_mbox']);
1195 if ($config['logout_expunge']) {
1196 $this->imap->expunge('INBOX');
1199 // Try to save unsaved user preferences
1200 if (!empty($_SESSION['preferences'])) {
1201 $this->user->save_prefs(unserialize($_SESSION['preferences']));
1207 * Function to be executed in script shutdown
1208 * Registered with register_shutdown_function()
1210 public function shutdown()
1212 foreach ($this->shutdown_functions as $function)
1213 call_user_func($function);
1215 if (is_object($this->smtp))
1216 $this->smtp->disconnect();
1218 foreach ($this->address_books as $book) {
1219 if (is_object($book) && is_a($book, 'rcube_addressbook'))
1223 foreach ($this->caches as $cache) {
1224 if (is_object($cache))
1228 if (is_object($this->imap))
1229 $this->imap->close();
1231 // before closing the database connection, write session data
1232 if ($_SERVER['REMOTE_ADDR'] && is_object($this->session)) {
1233 session_write_close();
1236 // write performance stats to logs/console
1237 if ($this->config->get('devel_mode')) {
1238 if (function_exists('memory_get_usage'))
1239 $mem = show_bytes(memory_get_usage());
1240 if (function_exists('memory_get_peak_usage'))
1241 $mem .= '/'.show_bytes(memory_get_peak_usage());
1243 $log = $this->task . ($this->action ? '/'.$this->action : '') . ($mem ? " [$mem]" : '');
1244 if (defined('RCMAIL_START'))
1245 rcube_print_time(RCMAIL_START, $log);
1253 * Registers shutdown function to be executed on shutdown.
1254 * The functions will be executed before destroying any
1255 * objects like smtp, imap, session, etc.
1257 * @param callback Function callback
1259 public function add_shutdown_function($function)
1261 $this->shutdown_functions[] = $function;
1266 * Generate a unique token to be used in a form request
1268 * @return string The request token
1270 public function get_request_token()
1272 $sess_id = $_COOKIE[ini_get('session.name')];
1273 if (!$sess_id) $sess_id = session_id();
1274 $plugin = $this->plugins->exec_hook('request_token', array('value' => md5('RT' . $this->user->ID . $this->config->get('des_key') . $sess_id)));
1275 return $plugin['value'];
1280 * Check if the current request contains a valid token
1282 * @param int Request method
1283 * @return boolean True if request token is valid false if not
1285 public function check_request($mode = RCUBE_INPUT_POST)
1287 $token = get_input_value('_token', $mode);
1288 $sess_id = $_COOKIE[ini_get('session.name')];
1289 return !empty($sess_id) && $token == $this->get_request_token();
1294 * Create unique authorization hash
1296 * @param string Session ID
1297 * @param int Timestamp
1298 * @return string The generated auth hash
1300 private function get_auth_hash($sess_id, $ts)
1302 $auth_string = sprintf('rcmail*sess%sR%s*Chk:%s;%s',
1305 $this->config->get('ip_check') ? $_SERVER['REMOTE_ADDR'] : '***.***.***.***',
1306 $_SERVER['HTTP_USER_AGENT']);
1308 if (function_exists('sha1'))
1309 return sha1($auth_string);
1311 return md5($auth_string);
1316 * Encrypt using 3DES
1318 * @param string $clear clear text input
1319 * @param string $key encryption key to retrieve from the configuration, defaults to 'des_key'
1320 * @param boolean $base64 whether or not to base64_encode() the result before returning
1322 * @return string encrypted text
1324 public function encrypt($clear, $key = 'des_key', $base64 = true)
1329 * Add a single canary byte to the end of the clear text, which
1330 * will help find out how much of padding will need to be removed
1331 * upon decryption; see http://php.net/mcrypt_generic#68082
1333 $clear = pack("a*H2", $clear, "80");
1335 if (function_exists('mcrypt_module_open') &&
1336 ($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_CBC, "")))
1338 $iv = $this->create_iv(mcrypt_enc_get_iv_size($td));
1339 mcrypt_generic_init($td, $this->config->get_crypto_key($key), $iv);
1340 $cipher = $iv . mcrypt_generic($td, $clear);
1341 mcrypt_generic_deinit($td);
1342 mcrypt_module_close($td);
1345 // @include_once 'des.inc'; (not shipped with this distribution)
1347 if (function_exists('des')) {
1349 $iv = $this->create_iv($des_iv_size);
1350 $cipher = $iv . des($this->config->get_crypto_key($key), $clear, 1, 1, $iv);
1354 'code' => 500, 'type' => 'php',
1355 'file' => __FILE__, 'line' => __LINE__,
1356 'message' => "Could not perform encryption; make sure Mcrypt is installed or lib/des.inc is available"
1361 return $base64 ? base64_encode($cipher) : $cipher;
1365 * Decrypt 3DES-encrypted string
1367 * @param string $cipher encrypted text
1368 * @param string $key encryption key to retrieve from the configuration, defaults to 'des_key'
1369 * @param boolean $base64 whether or not input is base64-encoded
1371 * @return string decrypted text
1373 public function decrypt($cipher, $key = 'des_key', $base64 = true)
1378 $cipher = $base64 ? base64_decode($cipher) : $cipher;
1380 if (function_exists('mcrypt_module_open') &&
1381 ($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_CBC, "")))
1383 $iv_size = mcrypt_enc_get_iv_size($td);
1384 $iv = substr($cipher, 0, $iv_size);
1386 // session corruption? (#1485970)
1387 if (strlen($iv) < $iv_size)
1390 $cipher = substr($cipher, $iv_size);
1391 mcrypt_generic_init($td, $this->config->get_crypto_key($key), $iv);
1392 $clear = mdecrypt_generic($td, $cipher);
1393 mcrypt_generic_deinit($td);
1394 mcrypt_module_close($td);
1397 // @include_once 'des.inc'; (not shipped with this distribution)
1399 if (function_exists('des')) {
1401 $iv = substr($cipher, 0, $des_iv_size);
1402 $cipher = substr($cipher, $des_iv_size);
1403 $clear = des($this->config->get_crypto_key($key), $cipher, 0, 1, $iv);
1407 'code' => 500, 'type' => 'php',
1408 'file' => __FILE__, 'line' => __LINE__,
1409 'message' => "Could not perform decryption; make sure Mcrypt is installed or lib/des.inc is available"
1415 * Trim PHP's padding and the canary byte; see note in
1416 * rcmail::encrypt() and http://php.net/mcrypt_generic#68082
1418 $clear = substr(rtrim($clear, "\0"), 0, -1);
1424 * Generates encryption initialization vector (IV)
1426 * @param int Vector size
1427 * @return string Vector string
1429 private function create_iv($size)
1431 // mcrypt_create_iv() can be slow when system lacks entrophy
1432 // we'll generate IV vector manually
1434 for ($i = 0; $i < $size; $i++)
1435 $iv .= chr(mt_rand(0, 255));
1440 * Build a valid URL to this instance of Roundcube
1442 * @param mixed Either a string with the action or url parameters as key-value pairs
1443 * @return string Valid application URL
1445 public function url($p)
1448 $p = array('_action' => @func_get_arg(0));
1450 $task = $p['_task'] ? $p['_task'] : ($p['task'] ? $p['task'] : $this->task);
1451 $p['_task'] = $task;
1456 foreach (array_reverse($p) as $key => $val) {
1458 $par = $key[0] == '_' ? $key : '_'.$key;
1459 $url .= $delm.urlencode($par).'='.urlencode($val);
1468 * Use imagemagick or GD lib to read image properties
1470 * @param string Absolute file path
1471 * @return mixed Hash array with image props like type, width, height or False on error
1473 public static function imageprops($filepath)
1475 $rcmail = rcmail::get_instance();
1476 if ($cmd = $rcmail->config->get('im_identify_path', false)) {
1477 list(, $type, $size) = explode(' ', strtolower(rcmail::exec($cmd. ' 2>/dev/null {in}', array('in' => $filepath))));
1479 list($width, $height) = explode('x', $size);
1481 else if (function_exists('getimagesize')) {
1482 $imsize = @getimagesize($filepath);
1483 $width = $imsize[0];
1484 $height = $imsize[1];
1485 $type = preg_replace('!image/!', '', $imsize['mime']);
1488 return $type ? array('type' => $type, 'width' => $width, 'height' => $height) : false;
1493 * Convert an image to a given size and type using imagemagick (ensures input is an image)
1495 * @param $p['in'] Input filename (mandatory)
1496 * @param $p['out'] Output filename (mandatory)
1497 * @param $p['size'] Width x height of resulting image, e.g. "160x60"
1498 * @param $p['type'] Output file type, e.g. "jpg"
1499 * @param $p['-opts'] Custom command line options to ImageMagick convert
1500 * @return Success of convert as true/false
1502 public static function imageconvert($p)
1505 $rcmail = rcmail::get_instance();
1506 $convert = $rcmail->config->get('im_convert_path', false);
1507 $identify = $rcmail->config->get('im_identify_path', false);
1509 // imagemagick is required for this
1513 if (!(($imagetype = @exif_imagetype($p['in'])) && ($type = image_type_to_extension($imagetype, false))))
1514 list(, $type) = explode(' ', strtolower(rcmail::exec($identify . ' 2>/dev/null {in}', $p))); # for things like eps
1516 $type = strtr($type, array("jpeg" => "jpg", "tiff" => "tif", "ps" => "eps", "ept" => "eps"));
1517 $p += array('type' => $type, 'types' => "bmp,eps,gif,jp2,jpg,png,svg,tif", 'quality' => 75);
1518 $p['-opts'] = array('-resize' => $p['size'].'>') + (array)$p['-opts'];
1520 if (in_array($type, explode(',', $p['types']))) # Valid type?
1521 $result = rcmail::exec($convert . ' 2>&1 -flatten -auto-orient -colorspace RGB -quality {quality} {-opts} {in} {type}:{out}', $p) === "";
1528 * Construct shell command, execute it and return output as string.
1529 * Keywords {keyword} are replaced with arguments
1531 * @param $cmd Format string with {keywords} to be replaced
1532 * @param $values (zero, one or more arrays can be passed)
1533 * @return output of command. shell errors not detectable
1535 public static function exec(/* $cmd, $values1 = array(), ... */)
1537 $args = func_get_args();
1538 $cmd = array_shift($args);
1539 $values = $replacements = array();
1541 // merge values into one array
1542 foreach ($args as $arg)
1543 $values += (array)$arg;
1545 preg_match_all('/({(-?)([a-z]\w*)})/', $cmd, $matches, PREG_SET_ORDER);
1546 foreach ($matches as $tags) {
1547 list(, $tag, $option, $key) = $tags;
1551 foreach ((array)$values["-$key"] as $key => $value) {
1552 if ($value === true || $value === false || $value === null)
1553 $parts[] = $value ? $key : "";
1554 else foreach ((array)$value as $val)
1555 $parts[] = "$key " . escapeshellarg($val);
1559 foreach ((array)$values[$key] as $value)
1560 $parts[] = escapeshellarg($value);
1563 $replacements[$tag] = join(" ", $parts);
1566 // use strtr behaviour of going through source string once
1567 $cmd = strtr($cmd, $replacements);
1569 return (string)shell_exec($cmd);
1574 * Helper method to set a cookie with the current path and host settings
1576 * @param string Cookie name
1577 * @param string Cookie value
1578 * @param string Expiration time
1580 public static function setcookie($name, $value, $exp = 0)
1585 $cookie = session_get_cookie_params();
1587 setcookie($name, $value, $exp, $cookie['path'], $cookie['domain'],
1588 rcube_https_check(), true);
1592 * Registers action aliases for current task
1594 * @param array $map Alias-to-filename hash array
1596 public function register_action_map($map)
1598 if (is_array($map)) {
1599 foreach ($map as $idx => $val) {
1600 $this->action_map[$idx] = $val;
1606 * Returns current action filename
1608 * @param array $map Alias-to-filename hash array
1610 public function get_action_file()
1612 if (!empty($this->action_map[$this->action])) {
1613 return $this->action_map[$this->action];
1616 return strtr($this->action, '-', '_') . '.inc';
1620 * Fixes some user preferences according to namespace handling change.
1621 * Old Roundcube versions were using folder names with removed namespace prefix.
1622 * Now we need to add the prefix on servers where personal namespace has prefix.
1624 * @param rcube_user $user User object
1626 private function fix_namespace_settings($user)
1628 $prefix = $this->imap->get_namespace('prefix');
1629 $prefix_len = strlen($prefix);
1634 $prefs = $this->config->all();
1635 if (!empty($prefs['namespace_fixed']))
1638 // Build namespace prefix regexp
1639 $ns = $this->imap->get_namespace();
1642 foreach ($ns as $entry) {
1643 if (!empty($entry)) {
1644 foreach ($entry as $item) {
1645 if (strlen($item[0])) {
1646 $regexp[] = preg_quote($item[0], '/');
1651 $regexp = '/^('. implode('|', $regexp).')/';
1654 $opts = array('drafts_mbox', 'junk_mbox', 'sent_mbox', 'trash_mbox', 'archive_mbox');
1655 foreach ($opts as $opt) {
1656 if ($value = $prefs[$opt]) {
1657 if ($value != 'INBOX' && !preg_match($regexp, $value)) {
1658 $prefs[$opt] = $prefix.$value;
1663 if (!empty($prefs['default_imap_folders'])) {
1664 foreach ($prefs['default_imap_folders'] as $idx => $name) {
1665 if ($name != 'INBOX' && !preg_match($regexp, $name)) {
1666 $prefs['default_imap_folders'][$idx] = $prefix.$name;
1671 if (!empty($prefs['search_mods'])) {
1673 foreach ($prefs['search_mods'] as $idx => $value) {
1674 if ($idx != 'INBOX' && $idx != '*' && !preg_match($regexp, $idx)) {
1675 $idx = $prefix.$idx;
1677 $folders[$idx] = $value;
1679 $prefs['search_mods'] = $folders;
1682 if (!empty($prefs['message_threading'])) {
1684 foreach ($prefs['message_threading'] as $idx => $value) {
1685 if ($idx != 'INBOX' && !preg_match($regexp, $idx)) {
1686 $idx = $prefix.$idx;
1688 $folders[$prefix.$idx] = $value;
1690 $prefs['message_threading'] = $folders;
1693 if (!empty($prefs['collapsed_folders'])) {
1694 $folders = explode('&&', $prefs['collapsed_folders']);
1695 $count = count($folders);
1699 $folders[0] = substr($folders[0], 1);
1700 $folders[$count-1] = substr($folders[$count-1], 0, -1);
1703 foreach ($folders as $value) {
1704 if ($value != 'INBOX' && !preg_match($regexp, $value)) {
1705 $value = $prefix.$value;
1707 $folders_str .= '&'.$value.'&';
1709 $prefs['collapsed_folders'] = $folders_str;
1712 $prefs['namespace_fixed'] = true;
1714 // save updated preferences and reset imap settings (default folders)
1715 $user->save_prefs($prefs);
1716 $this->set_imap_prop();