| | |
| | | | Author: Thomas Bruederli <roundcube@gmail.com> | |
| | | | Author: Aleksander Machniak <alec@alec.pl> | |
| | | +-----------------------------------------------------------------------+ |
| | | |
| | | $Id$ |
| | | |
| | | */ |
| | | |
| | | |
| | |
| | | |
| | | |
| | | const JS_OBJECT_NAME = 'rcmail'; |
| | | |
| | | const ERROR_STORAGE = -2; |
| | | const ERROR_INVALID_REQUEST = 1; |
| | | const ERROR_INVALID_HOST = 2; |
| | | const ERROR_COOKIES_DISABLED = 3; |
| | | |
| | | |
| | | /** |
| | | * This implements the 'singleton' design pattern |
| | |
| | | /** |
| | | * Return instance of the internal address book class |
| | | * |
| | | * @param string Address book identifier |
| | | * @param string Address book identifier (-1 for default addressbook) |
| | | * @param boolean True if the address book needs to be writeable |
| | | * |
| | | * @return rcube_contacts Address book object |
| | |
| | | { |
| | | $contacts = null; |
| | | $ldap_config = (array)$this->config->get('ldap_public'); |
| | | $abook_type = strtolower($this->config->get('address_book_type')); |
| | | |
| | | // 'sql' is the alias for '0' used by autocomplete |
| | | if ($id == 'sql') |
| | | $id = '0'; |
| | | $id = '0'; |
| | | else if ($id == -1) { |
| | | $id = $this->config->get('default_addressbook'); |
| | | $default = true; |
| | | } |
| | | |
| | | // use existing instance |
| | | if (isset($this->address_books[$id]) && is_object($this->address_books[$id]) |
| | | && is_a($this->address_books[$id], 'rcube_addressbook') |
| | | && (!$writeable || !$this->address_books[$id]->readonly) |
| | | ) { |
| | | if (isset($this->address_books[$id]) && ($this->address_books[$id] instanceof rcube_addressbook)) { |
| | | $contacts = $this->address_books[$id]; |
| | | } |
| | | else if ($id && $ldap_config[$id]) { |
| | |
| | | if ($plugin['instance'] instanceof rcube_addressbook) { |
| | | $contacts = $plugin['instance']; |
| | | } |
| | | // get first source from the list |
| | | else if (!$id) { |
| | | $source = reset($this->get_address_sources($writeable)); |
| | | if (!empty($source)) { |
| | | $contacts = $this->get_address_book($source['id']); |
| | | if ($contacts) |
| | | $id = $source['id']; |
| | | } |
| | | } |
| | | |
| | | // Get first addressbook from the list if configured default doesn't exist |
| | | // This can happen when user deleted the addressbook (e.g. Kolab folder) |
| | | if (!$contacts && (!$id || $default)) { |
| | | $source = reset($this->get_address_sources($writeable)); |
| | | if (!empty($source)) { |
| | | $contacts = $this->get_address_book($source['id']); |
| | | if ($contacts) |
| | | $id = $source['id']; |
| | | } |
| | | } |
| | | |
| | |
| | | 'file' => __FILE__, 'line' => __LINE__, |
| | | 'message' => "Addressbook source ($id) not found!"), |
| | | true, true); |
| | | } |
| | | |
| | | if ($writeable && $contacts->readonly) { |
| | | return null; |
| | | } |
| | | |
| | | // set configured sort order |
| | |
| | | $this->output->set_charset(RCMAIL_CHARSET); |
| | | |
| | | // add some basic labels to client |
| | | $this->output->add_label('loading', 'servererror'); |
| | | $this->output->add_label('loading', 'servererror', 'requesttimedout'); |
| | | |
| | | return $this->output; |
| | | } |
| | |
| | | */ |
| | | public function session_init() |
| | | { |
| | | // session started (Installer?) |
| | | if (session_id()) |
| | | return; |
| | | |
| | | $sess_name = $this->config->get('session_name'); |
| | | $sess_domain = $this->config->get('session_domain'); |
| | | $lifetime = $this->config->get('session_lifetime', 0) * 60; |
| | | |
| | | // set session domain |
| | | if ($sess_domain) { |
| | | ini_set('session.cookie_domain', $sess_domain); |
| | | } |
| | | // set session garbage collecting time according to session_lifetime |
| | | if ($lifetime) { |
| | | ini_set('session.gc_maxlifetime', $lifetime * 2); |
| | | } |
| | | |
| | | ini_set('session.cookie_secure', rcube_utils::https_check()); |
| | | ini_set('session.name', $sess_name ? $sess_name : 'roundcube_sessid'); |
| | | ini_set('session.use_cookies', 1); |
| | | ini_set('session.use_only_cookies', 1); |
| | | ini_set('session.serialize_handler', 'php'); |
| | | |
| | | // use database for storing session data |
| | | $this->session = new rcube_session($this->get_dbh(), $this->config); |
| | | |
| | | $this->session->register_gc_handler(array($this, 'temp_gc')); |
| | | $this->session->register_gc_handler(array($this, 'cache_gc')); |
| | | |
| | | // start PHP session (if not in CLI mode) |
| | | if ($_SERVER['REMOTE_ADDR']) |
| | | session_start(); |
| | | parent::session_init(); |
| | | |
| | | // set initial session vars |
| | | if (!$_SESSION['user_id']) |
| | |
| | | |
| | | |
| | | /** |
| | | * Configure session object internals |
| | | */ |
| | | public function session_configure() |
| | | { |
| | | if (!$this->session) |
| | | return; |
| | | |
| | | $lifetime = $this->config->get('session_lifetime', 0) * 60; |
| | | |
| | | // set keep-alive/check-recent interval |
| | | if ($keep_alive = $this->config->get('keep_alive')) { |
| | | // be sure that it's less than session lifetime |
| | | if ($lifetime) |
| | | $keep_alive = min($keep_alive, $lifetime - 30); |
| | | $keep_alive = max(60, $keep_alive); |
| | | $this->session->set_keep_alive($keep_alive); |
| | | } |
| | | |
| | | $this->session->set_secret($this->config->get('des_key') . $_SERVER['HTTP_USER_AGENT']); |
| | | $this->session->set_ip_check($this->config->get('ip_check')); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Perfom login to the mail server and to the webmail service. |
| | | * This will also create a new user entry if auto_create_user is configured. |
| | | * |
| | | * @param string Mail storage (IMAP) user name |
| | | * @param string Mail storage (IMAP) password |
| | | * @param string Mail storage (IMAP) host |
| | | * @param bool Enables cookie check |
| | | * |
| | | * @return boolean True on success, False on failure |
| | | */ |
| | | function login($username, $pass, $host=NULL) |
| | | function login($username, $pass, $host = null, $cookiecheck = false) |
| | | { |
| | | $this->login_error = null; |
| | | |
| | | if (empty($username)) { |
| | | return false; |
| | | } |
| | | |
| | | if ($cookiecheck && empty($_COOKIE)) { |
| | | $this->login_error = self::ERROR_COOKIES_DISABLED; |
| | | return false; |
| | | } |
| | | |
| | |
| | | break; |
| | | } |
| | | } |
| | | if (!$allowed) |
| | | return false; |
| | | if (!$allowed) { |
| | | $host = null; |
| | | } |
| | | else if (!empty($config['default_host']) && $host != rcube_utils::parse_host($config['default_host'])) |
| | | } |
| | | else if (!empty($config['default_host']) && $host != rcube_utils::parse_host($config['default_host'])) { |
| | | $host = null; |
| | | } |
| | | |
| | | if (!$host) { |
| | | $this->login_error = self::ERROR_INVALID_HOST; |
| | | return false; |
| | | } |
| | | |
| | | // parse $host URL |
| | | $a_host = parse_url($host); |
| | |
| | | $_SESSION['storage_port'] = $port; |
| | | $_SESSION['storage_ssl'] = $ssl; |
| | | $_SESSION['password'] = $this->encrypt($pass); |
| | | $_SESSION['login_time'] = mktime(); |
| | | $_SESSION['login_time'] = time(); |
| | | |
| | | if (isset($_REQUEST['_timezone']) && $_REQUEST['_timezone'] != '_default_') |
| | | $_SESSION['timezone'] = floatval($_REQUEST['_timezone']); |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Returns error code of last login operation |
| | | * |
| | | * @return int Error code |
| | | */ |
| | | public function login_error() |
| | | { |
| | | if ($this->login_error) { |
| | | return $this->login_error; |
| | | } |
| | | |
| | | if ($this->storage && $this->storage->get_error_code() < -1) { |
| | | return self::ERROR_STORAGE; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Auto-select IMAP host based on the posted login information |
| | | * |
| | |
| | | |
| | | if (is_array($default_host)) { |
| | | $post_host = rcube_utils::get_input_value('_host', rcube_utils::INPUT_POST); |
| | | $post_user = rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST); |
| | | |
| | | list($user, $domain) = explode('@', $post_user); |
| | | |
| | | // direct match in default_host array |
| | | if ($default_host[$post_host] || in_array($post_host, array_values($default_host))) { |
| | | $host = $post_host; |
| | | } |
| | | |
| | | // try to select host by mail domain |
| | | list($user, $domain) = explode('@', rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST)); |
| | | if (!empty($domain)) { |
| | | else if (!empty($domain)) { |
| | | foreach ($default_host as $storage_host => $mail_domains) { |
| | | if (is_array($mail_domains) && in_array_nocase($domain, $mail_domains)) { |
| | | $host = $storage_host; |
| | |
| | | |
| | | |
| | | /** |
| | | * Garbage collector for cache entries. |
| | | * Set flag to expunge caches on shutdown |
| | | */ |
| | | function cache_gc() |
| | | { |
| | | // because this gc function is called before storage is initialized, |
| | | // we just set a flag to expunge storage cache on shutdown. |
| | | $this->expunge_cache = true; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Generate a unique token to be used in a form request |
| | | * |
| | | * @return string The request token |
| | |
| | | */ |
| | | public function url($p) |
| | | { |
| | | if (!is_array($p)) |
| | | if (!is_array($p)) { |
| | | if (strpos($p, 'http') === 0) |
| | | return $p; |
| | | |
| | | $p = array('_action' => @func_get_arg(0)); |
| | | } |
| | | |
| | | $task = $p['_task'] ? $p['_task'] : ($p['task'] ? $p['task'] : $this->task); |
| | | $p['_task'] = $task; |
| | |
| | | |
| | | |
| | | /** |
| | | * Garbage collector function for temp files. |
| | | * Remove temp files older than two days |
| | | */ |
| | | public function temp_gc() |
| | | { |
| | | $tmp = unslashify($this->config->get('temp_dir')); |
| | | $expire = mktime() - 172800; // expire in 48 hours |
| | | |
| | | if ($tmp && ($dir = opendir($tmp))) { |
| | | while (($fname = readdir($dir)) !== false) { |
| | | if ($fname{0} == '.') { |
| | | continue; |
| | | } |
| | | |
| | | if (filemtime($tmp.'/'.$fname) < $expire) { |
| | | @unlink($tmp.'/'.$fname); |
| | | } |
| | | } |
| | | |
| | | closedir($dir); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Create a HTML table based on the given data |
| | | * |
| | | * @param array Named table attributes |
| | |
| | | $attrib = $hook['attribs']; |
| | | |
| | | if ($type == 'select') { |
| | | $attrib['is_escaped'] = true; |
| | | $select = new html_select($attrib); |
| | | |
| | | // add no-selection option |
| | | if ($attrib['noselection']) { |
| | | $select->add($rcmail->gettext($attrib['noselection']), ''); |
| | | $select->add(html::quote($rcmail->gettext($attrib['noselection'])), ''); |
| | | } |
| | | |
| | | $rcmail->render_folder_tree_select($a_mailboxes, $mbox_name, $attrib['maxlength'], $select, $attrib['realnames']); |
| | |
| | | */ |
| | | public function folder_selector($p = array()) |
| | | { |
| | | $p += array('maxlength' => 100, 'realnames' => false); |
| | | $p += array('maxlength' => 100, 'realnames' => false, 'is_escaped' => true); |
| | | $a_mailboxes = array(); |
| | | $storage = $this->get_storage(); |
| | | |
| | |
| | | $select = new html_select($p); |
| | | |
| | | if ($p['noselection']) { |
| | | $select->add($p['noselection'], ''); |
| | | $select->add(html::quote($p['noselection']), ''); |
| | | } |
| | | |
| | | $this->render_folder_tree_select($a_mailboxes, $mbox, $p['maxlength'], $select, $p['realnames'], 0, $p); |
| | |
| | | if ($maxlength && $maxlength > 1) { |
| | | $foldername = abbreviate_string($foldername, $maxlength); |
| | | } |
| | | } |
| | | |
| | | $select->add(str_repeat(' ', $nestLevel*4) . $foldername, $folder['id']); |
| | | $select->add(str_repeat(' ', $nestLevel*4) . html::quote($foldername), $folder['id']); |
| | | |
| | | if (!empty($folder['folders'])) { |
| | | $out .= $this->render_folder_tree_select($folder['folders'], $mbox_name, $maxlength, |
| | | $select, $realnames, $nestLevel+1, $opts); |
| | | } |
| | | if (!empty($folder['folders'])) { |
| | | $out .= $this->render_folder_tree_select($folder['folders'], $mbox_name, $maxlength, |
| | | $select, $realnames, $nestLevel+1, $opts); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Returns real size (calculated) of the message part |
| | | * |
| | | * @param rcube_message_part Message part |
| | | * |
| | | * @return string Part size (and unit) |
| | | */ |
| | | public function message_part_size($part) |
| | | { |
| | | if (isset($part->d_parameters['size'])) { |
| | | $size = $this->show_bytes((int)$part->d_parameters['size']); |
| | | } |
| | | else { |
| | | $size = $part->size; |
| | | if ($part->encoding == 'base64') { |
| | | $size = $size / 1.33; |
| | | } |
| | | |
| | | $size = '~' . $this->show_bytes($size); |
| | | } |
| | | |
| | | return $size; |
| | | } |
| | | |
| | | |
| | | /************************************************************************ |
| | | ********* Deprecated methods (to be removed) ********* |
| | | ***********************************************************************/ |
| | |
| | | { |
| | | rcube_utils::setcookie($name, $value, $exp); |
| | | } |
| | | |
| | | public function imap_connect() |
| | | { |
| | | return $this->storage_connect(); |
| | | } |
| | | |
| | | public function imap_init() |
| | | { |
| | | return $this->storage_init(); |
| | | } |
| | | |
| | | /** |
| | | * Connect to the mail storage server with stored session data |
| | | * |
| | | * @return bool True on success, False on error |
| | | */ |
| | | public function storage_connect() |
| | | { |
| | | $storage = $this->get_storage(); |
| | | |
| | | if ($_SESSION['storage_host'] && !$storage->is_connected()) { |
| | | $host = $_SESSION['storage_host']; |
| | | $user = $_SESSION['username']; |
| | | $port = $_SESSION['storage_port']; |
| | | $ssl = $_SESSION['storage_ssl']; |
| | | $pass = $this->decrypt($_SESSION['password']); |
| | | |
| | | if (!$storage->connect($host, $user, $pass, $port, $ssl)) { |
| | | if (is_object($this->output)) { |
| | | $error = $storage->get_error_code() == -1 ? 'storageerror' : 'sessionerror'; |
| | | $this->output->show_message($error, 'error'); |
| | | } |
| | | } |
| | | else { |
| | | $this->set_storage_prop(); |
| | | return $storage->is_connected(); |
| | | } |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | } |