public function init()
 {
     $rcmail = rcube::get_instance();
     $this->load_config();
     $this->add_texts('localization/');
     $this->user_name = $rcmail->config->get('globaladdressbook_user', '[global_addressbook_user]');
     $this->user_name = str_replace('%d', $rcmail->user->get_username('domain'), $this->user_name);
     $this->user_name = str_replace('%h', $_SESSION['storage_host'], $this->user_name);
     $this->groups = $rcmail->config->get('globaladdressbook_groups', false);
     $this->name = $this->gettext('globaladdressbook');
     $this->_set_permissions();
     // email2user hook can be used by other plugins to do post processing on usernames, not just virtual user lookup
     // matches process of user lookup and creation in the core
     if (strpos($this->user_name, '@') && ($virtuser = rcube_user::email2user($this->user_name))) {
         $this->user_name = $virtuser;
     }
     // check if the global address book user exists
     if (!($user = rcube_user::query($this->user_name, $this->host))) {
         // this action overrides the current user information so make a copy and then restore it
         $cur_user = $rcmail->user;
         $user = rcube_user::create($this->user_name, $this->host);
         $rcmail->user = $cur_user;
         // prevent new_user_dialog plugin from triggering
         $_SESSION['plugin.newuserdialog'] = false;
     }
     $this->user_id = $user->ID;
     // use this address book for autocompletion queries
     if ($rcmail->config->get('globaladdressbook_autocomplete')) {
         $sources = $rcmail->config->get('autocomplete_addressbooks', array('sql'));
         if (!in_array($this->abook_id, $sources)) {
             $sources[] = $this->abook_id;
             $rcmail->config->set('autocomplete_addressbooks', $sources);
         }
     }
     $this->add_hook('addressbooks_list', array($this, 'address_sources'));
     $this->add_hook('addressbook_get', array($this, 'get_address_book'));
     if ($rcmail->config->get('globaladdressbook_check_safe')) {
         $this->add_hook('message_check_safe', array($this, 'check_known_senders'));
     }
 }
Exemple #2
0
 /**
  * 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, $cookiecheck = false)
 {
     $this->login_error = null;
     if (empty($username)) {
         return false;
     }
     if ($cookiecheck && empty($_COOKIE)) {
         $this->login_error = self::ERROR_COOKIES_DISABLED;
         return false;
     }
     $default_host = $this->config->get('default_host');
     $default_port = $this->config->get('default_port');
     $username_domain = $this->config->get('username_domain');
     $login_lc = $this->config->get('login_lc', 2);
     // host is validated in rcmail::autoselect_host(), so here
     // we'll only handle unset host (if possible)
     if (!$host && !empty($default_host)) {
         if (is_array($default_host)) {
             list($key, $val) = each($default_host);
             $host = is_numeric($key) ? $val : $key;
         } else {
             $host = $default_host;
         }
         $host = rcube_utils::parse_host($host);
     }
     if (!$host) {
         $this->login_error = self::ERROR_INVALID_HOST;
         return false;
     }
     // parse $host URL
     $a_host = parse_url($host);
     if ($a_host['host']) {
         $host = $a_host['host'];
         $ssl = isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl', 'imaps', 'tls')) ? $a_host['scheme'] : null;
         if (!empty($a_host['port'])) {
             $port = $a_host['port'];
         } else {
             if ($ssl && $ssl != 'tls' && (!$default_port || $default_port == 143)) {
                 $port = 993;
             }
         }
     }
     if (!$port) {
         $port = $default_port;
     }
     // Check if we need to add/force domain to username
     if (!empty($username_domain)) {
         $domain = is_array($username_domain) ? $username_domain[$host] : $username_domain;
         if ($domain = rcube_utils::parse_host((string) $domain, $host)) {
             $pos = strpos($username, '@');
             // force configured domains
             if ($pos !== false && $this->config->get('username_domain_forced')) {
                 $username = substr($username, 0, $pos) . '@' . $domain;
             } else {
                 if ($pos === false) {
                     $username .= '@' . $domain;
                 }
             }
         }
     }
     // Convert username to lowercase. If storage backend
     // is case-insensitive we need to store always the same username (#1487113)
     if ($login_lc) {
         if ($login_lc == 2 || $login_lc === true) {
             $username = mb_strtolower($username);
         } else {
             if (strpos($username, '@')) {
                 // lowercase domain name
                 list($local, $domain) = explode('@', $username);
                 $username = $local . '@' . mb_strtolower($domain);
             }
         }
     }
     // try to resolve email address from virtuser table
     if (strpos($username, '@') && ($virtuser = rcube_user::email2user($username))) {
         $username = $virtuser;
     }
     // Here we need IDNA ASCII
     // Only rcube_contacts class is using domain names in Unicode
     $host = rcube_utils::idn_to_ascii($host);
     $username = rcube_utils::idn_to_ascii($username);
     // user already registered -> overwrite username
     if ($user = rcube_user::query($username, $host)) {
         $username = $user->data['username'];
     }
     $storage = $this->get_storage();
     // try to log in
     if (!$storage->connect($host, $username, $pass, $port, $ssl)) {
         return false;
     }
     // user already registered -> update user's record
     if (is_object($user)) {
         // update last login timestamp
         $user->touch();
     } else {
         if ($this->config->get('auto_create_user')) {
             if ($created = rcube_user::create($username, $host)) {
                 $user = $created;
             } else {
                 self::raise_error(array('code' => 620, 'file' => __FILE__, 'line' => __LINE__, 'message' => "Failed to create a user record. Maybe aborted by a plugin?"), true, false);
             }
         } else {
             self::raise_error(array('code' => 621, 'file' => __FILE__, 'line' => __LINE__, 'message' => "Access denied for new user {$username}. 'auto_create_user' is disabled"), true, false);
         }
     }
     // login succeeded
     if (is_object($user) && $user->ID) {
         // Configure environment
         $this->set_user($user);
         $this->set_storage_prop();
         // set session vars
         $_SESSION['user_id'] = $user->ID;
         $_SESSION['username'] = $user->data['username'];
         $_SESSION['storage_host'] = $host;
         $_SESSION['storage_port'] = $port;
         $_SESSION['storage_ssl'] = $ssl;
         $_SESSION['password'] = $this->encrypt($pass);
         $_SESSION['login_time'] = time();
         if (isset($_REQUEST['_timezone']) && $_REQUEST['_timezone'] != '_default_') {
             $_SESSION['timezone'] = rcube_utils::get_input_value('_timezone', rcube_utils::INPUT_GPC);
         }
         // fix some old settings according to namespace prefix
         $this->fix_namespace_settings($user);
         // set/create special folders
         $this->set_special_folders();
         // clear all mailboxes related cache(s)
         $storage->clear_cache('mailboxes', true);
         return true;
     }
     return false;
 }
Exemple #3
0
 /**
  * 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
  *
  * @return boolean True on success, False on failure
  */
 function login($username, $pass, $host = NULL)
 {
     if (empty($username)) {
         return false;
     }
     $config = $this->config->all();
     if (!$host) {
         $host = $config['default_host'];
     }
     // Validate that selected host is in the list of configured hosts
     if (is_array($config['default_host'])) {
         $allowed = false;
         foreach ($config['default_host'] as $key => $host_allowed) {
             if (!is_numeric($key)) {
                 $host_allowed = $key;
             }
             if ($host == $host_allowed) {
                 $allowed = true;
                 break;
             }
         }
         if (!$allowed) {
             return false;
         }
     } else {
         if (!empty($config['default_host']) && $host != rcube_utils::parse_host($config['default_host'])) {
             return false;
         }
     }
     // parse $host URL
     $a_host = parse_url($host);
     if ($a_host['host']) {
         $host = $a_host['host'];
         $ssl = isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl', 'imaps', 'tls')) ? $a_host['scheme'] : null;
         if (!empty($a_host['port'])) {
             $port = $a_host['port'];
         } else {
             if ($ssl && $ssl != 'tls' && (!$config['default_port'] || $config['default_port'] == 143)) {
                 $port = 993;
             }
         }
     }
     if (!$port) {
         $port = $config['default_port'];
     }
     /* Modify username with domain if required
           Inspired by Marco <P0L0_notspam_binware.org>
        */
     // Check if we need to add domain
     if (!empty($config['username_domain']) && strpos($username, '@') === false) {
         if (is_array($config['username_domain']) && isset($config['username_domain'][$host])) {
             $username .= '@' . rcube_utils::parse_host($config['username_domain'][$host], $host);
         } else {
             if (is_string($config['username_domain'])) {
                 $username .= '@' . rcube_utils::parse_host($config['username_domain'], $host);
             }
         }
     }
     // Convert username to lowercase. If storage backend
     // is case-insensitive we need to store always the same username (#1487113)
     if ($config['login_lc']) {
         $username = mb_strtolower($username);
     }
     // try to resolve email address from virtuser table
     if (strpos($username, '@') && ($virtuser = rcube_user::email2user($username))) {
         $username = $virtuser;
     }
     // Here we need IDNA ASCII
     // Only rcube_contacts class is using domain names in Unicode
     $host = rcube_utils::idn_to_ascii($host);
     if (strpos($username, '@')) {
         // lowercase domain name
         list($local, $domain) = explode('@', $username);
         $username = $local . '@' . mb_strtolower($domain);
         $username = rcube_utils::idn_to_ascii($username);
     }
     // user already registered -> overwrite username
     if ($user = rcube_user::query($username, $host)) {
         $username = $user->data['username'];
     }
     $storage = $this->get_storage();
     // try to log in
     if (!($login = $storage->connect($host, $username, $pass, $port, $ssl))) {
         // try with lowercase
         $username_lc = mb_strtolower($username);
         if ($username_lc != $username) {
             // try to find user record again -> overwrite username
             if (!$user && ($user = rcube_user::query($username_lc, $host))) {
                 $username_lc = $user->data['username'];
             }
             if ($login = $storage->connect($host, $username_lc, $pass, $port, $ssl)) {
                 $username = $username_lc;
             }
         }
     }
     // exit if login failed
     if (!$login) {
         return false;
     }
     // user already registered -> update user's record
     if (is_object($user)) {
         // update last login timestamp
         $user->touch();
     } else {
         if ($config['auto_create_user']) {
             if ($created = rcube_user::create($username, $host)) {
                 $user = $created;
             } else {
                 self::raise_error(array('code' => 620, 'type' => 'php', 'file' => __FILE__, 'line' => __LINE__, 'message' => "Failed to create a user record. Maybe aborted by a plugin?"), true, false);
             }
         } else {
             self::raise_error(array('code' => 621, 'type' => 'php', 'file' => __FILE__, 'line' => __LINE__, 'message' => "Access denied for new user {$username}. 'auto_create_user' is disabled"), true, false);
         }
     }
     // login succeeded
     if (is_object($user) && $user->ID) {
         // Configure environment
         $this->set_user($user);
         $this->set_storage_prop();
         $this->session_configure();
         // fix some old settings according to namespace prefix
         $this->fix_namespace_settings($user);
         // create default folders on first login
         if ($config['create_default_folders'] && (!empty($created) || empty($user->data['last_login']))) {
             $storage->create_default_folders();
         }
         // set session vars
         $_SESSION['user_id'] = $user->ID;
         $_SESSION['username'] = $user->data['username'];
         $_SESSION['storage_host'] = $host;
         $_SESSION['storage_port'] = $port;
         $_SESSION['storage_ssl'] = $ssl;
         $_SESSION['password'] = $this->encrypt($pass);
         $_SESSION['login_time'] = time();
         if (isset($_REQUEST['_timezone']) && $_REQUEST['_timezone'] != '_default_') {
             $_SESSION['timezone'] = floatval($_REQUEST['_timezone']);
         }
         if (isset($_REQUEST['_dstactive']) && $_REQUEST['_dstactive'] != '_default_') {
             $_SESSION['dst_active'] = intval($_REQUEST['_dstactive']);
         }
         // force reloading complete list of subscribed mailboxes
         $storage->clear_cache('mailboxes', true);
         return true;
     }
     return false;
 }
Exemple #4
0
 /**
  * Perfom login to the IMAP server and to the webmail service.
  * This will also create a new user entry if auto_create_user is configured.
  *
  * @param string IMAP user name
  * @param string IMAP password
  * @param string IMAP host
  * @return boolean True on success, False on failure
  */
 function login($username, $pass, $host = NULL)
 {
     $user = NULL;
     $config = $this->config->all();
     if (!$host) {
         $host = $config['default_host'];
     }
     // Validate that selected host is in the list of configured hosts
     if (is_array($config['default_host'])) {
         $allowed = false;
         foreach ($config['default_host'] as $key => $host_allowed) {
             if (!is_numeric($key)) {
                 $host_allowed = $key;
             }
             if ($host == $host_allowed) {
                 $allowed = true;
                 break;
             }
         }
         if (!$allowed) {
             return false;
         }
     } else {
         if (!empty($config['default_host']) && $host != $config['default_host']) {
             return false;
         }
     }
     // parse $host URL
     $a_host = parse_url($host);
     if ($a_host['host']) {
         $host = $a_host['host'];
         $imap_ssl = isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl', 'imaps', 'tls')) ? $a_host['scheme'] : null;
         $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : $config['default_port']);
     } else {
         $imap_port = $config['default_port'];
     }
     /* Modify username with domain if required  
           Inspired by Marco <P0L0_notspam_binware.org>
        */
     // Check if we need to add domain
     if (!empty($config['username_domain']) && !strpos($username, '@')) {
         if (is_array($config['username_domain']) && isset($config['username_domain'][$host])) {
             $username .= '@' . $config['username_domain'][$host];
         } else {
             if (is_string($config['username_domain'])) {
                 $username .= '@' . $config['username_domain'];
             }
         }
     }
     // try to resolve email address from virtuser table
     if (!empty($config['virtuser_file']) && strpos($username, '@')) {
         $username = rcube_user::email2user($username);
     }
     // lowercase username if it's an e-mail address (#1484473)
     if (strpos($username, '@')) {
         $username = rc_strtolower($username);
     }
     // user already registered -> overwrite username
     if ($user = rcube_user::query($username, $host)) {
         $username = $user->data['username'];
     }
     // exit if IMAP login failed
     if (!($imap_login = $this->imap->connect($host, $username, $pass, $imap_port, $imap_ssl))) {
         return false;
     }
     // user already registered -> update user's record
     if (is_object($user)) {
         $user->touch();
     } else {
         if ($config['auto_create_user']) {
             if ($created = rcube_user::create($username, $host)) {
                 $user = $created;
                 // get existing mailboxes (but why?)
                 // $a_mailboxes = $this->imap->list_mailboxes();
             }
         } else {
             raise_error(array('code' => 600, 'type' => 'php', 'file' => RCMAIL_CONFIG_DIR . "/main.inc.php", 'message' => "Acces denied for new user {$username}. 'auto_create_user' is disabled"), true, false);
         }
     }
     // login succeeded
     if (is_object($user) && $user->ID) {
         $this->set_user($user);
         // set session vars
         $_SESSION['user_id'] = $user->ID;
         $_SESSION['username'] = $user->data['username'];
         $_SESSION['imap_host'] = $host;
         $_SESSION['imap_port'] = $imap_port;
         $_SESSION['imap_ssl'] = $imap_ssl;
         $_SESSION['password'] = $this->encrypt_passwd($pass);
         $_SESSION['login_time'] = mktime();
         if ($_REQUEST['_timezone'] != '_default_') {
             $_SESSION['timezone'] = floatval($_REQUEST['_timezone']);
         }
         // force reloading complete list of subscribed mailboxes
         $this->set_imap_prop();
         $this->imap->clear_cache('mailboxes');
         if ($config['create_default_folders']) {
             $this->imap->create_default_folders();
         }
         return true;
     }
     return false;
 }