Exemplo n.º 1
0
 /**
  * Sort mailboxes first by default folders and then in alphabethical order
  * @access private
  */
 function _sort_mailbox_list($a_folders)
 {
     $a_out = $a_defaults = $folders = array();
     $delimiter = $this->get_hierarchy_delimiter();
     // find default folders and skip folders starting with '.'
     foreach ($a_folders as $i => $folder) {
         if ($folder[0] == '.') {
             continue;
         }
         if (($p = array_search(strtolower($folder), $this->default_folders_lc)) !== false && !$a_defaults[$p]) {
             $a_defaults[$p] = $folder;
         } else {
             $folders[$folder] = rc_strtolower(rcube_charset_convert($folder, 'UTF-7'));
         }
     }
     // sort folders and place defaults on the top
     asort($folders, SORT_LOCALE_STRING);
     ksort($a_defaults);
     $folders = array_merge($a_defaults, array_keys($folders));
     // finally we must rebuild the list to move
     // subfolders of default folders to their place...
     // ...also do this for the rest of folders because
     // asort() is not properly sorting case sensitive names
     while (list($key, $folder) = each($folders)) {
         // set the type of folder name variable (#1485527)
         $a_out[] = (string) $folder;
         unset($folders[$key]);
         $this->_rsort($folder, $delimiter, $folders, $a_out);
     }
     return $a_out;
 }
Exemplo n.º 2
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;
 }