Files
user_external/lib/imap.php
Jonas Sulzer 1008d41f42 🐛 FIX: variable groups not set in some cases
Signed-off-by: Jonas Sulzer <jonas@violoncello.ch>
2019-06-27 11:40:26 +02:00

109 lines
2.9 KiB
PHP

<?php
/**
* @author Robin Appelman <icewind@owncloud.com>
* @author Jonas Sulzer <jonas@violoncello.ch>
* @copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
use OCA\user_external\imap\imap_rcube;
/**
* User authentication against an IMAP mail server
*
* @category Apps
* @package UserExternal
* @author Robin Appelman <icewind@owncloud.com>
* @license http://www.gnu.org/licenses/agpl AGPL
* @link http://github.com/owncloud/apps
*/
class OC_User_IMAP extends \OCA\user_external\Base {
private $mailbox;
private $port;
private $sslmode;
private $domain;
private $stripeDomain;
private $groupDomain;
/**
* Create new IMAP authentication provider
*
* @param string $mailbox IMAP server domain/IP
* @param int $port IMAP server $port
* @param string $sslmode
* @param string $domain If provided, loging will be restricted to this domain
* @param boolean $stripeDomain (whether to stripe the domain part from the username or not)
* @param boolean $groupDomain (whether to add the usere to a group corresponding to the domain of the address)
*/
public function __construct($mailbox, $port = null, $sslmode = null, $domain = null, $stripeDomain = true, $groupDomain = false) {
parent::__construct($mailbox);
$this->mailbox = $mailbox;
$this->port = $port === null ? 143 : $port;
$this->sslmode = $sslmode;
$this->domain = $domain === null ? '' : $domain;
$this->stripeDomain = $stripeDomain;
$this->groupDomain = $groupDomain;
}
/**
* Check if the password is correct without logging in the user
*
* @param string $uid The username
* @param string $password The password
*
* @return true/false
*/
public function checkPassword($uid, $password) {
// Replace escaped @ symbol in uid (which is a mail address)
// but only if there is no @ symbol and if there is a %40 inside the uid
if (!(strpos($uid, '@') !== false) && (strpos($uid, '%40') !== false)) {
$uid = str_replace("%40","@",$uid);
}
$pieces = explode('@', $uid);
if ($this->domain !== '') {
if (count($pieces) === 1) {
$username = $uid . '@' . $this->domain;
} else if(count($pieces) === 2 && $pieces[1] === $this->domain) {
$username = $uid;
if ($this->stripeDomain) {
$uid = $pieces[0];
}
} else {
return false;
}
} else {
$username = $uid;
}
$groups = [];
if ($this->groupDomain && $pieces[1]) {
$groups[] = $pieces[1];
}
$rcube = new imap_rcube();
$params = ["port"=>$this->port, "timeout"=>10];
if ($this->sslmode !== null){
$params["ssl_mode"] = $this->sslmode;
}
$canconnect = $rcube->connect(
$this->mailbox,
$username,
$password,
$params
);
if($canconnect) {
$rcube->closeConnection();
$uid = mb_strtolower($uid);
$this->storeUser($uid, $groups);
return $uid;
}
return false;
}
}