- Модуль: mail
- Путь к файлу: ~/bitrix/modules/mail/lib/imap.php
- Класс: BitrixMailImap
- Вызов: Imap::authenticate
public function authenticate(&$error)
{
$error = null;
if (!$this->connect($error))
return false;
if (in_array($this->sessState, array('auth', 'select')))
return true;
$mech = false;
$token = null;
if (preg_match('/ x20 AUTH=XOAUTH2 ( x20 | rn ) /ix', $this->sessCapability))
{
$token = HelperOAuth::getTokenByMeta($this->options['password']);
if (!empty($token))
{
$mech = 'oauth';
}
else if (false === $token)
{
$error = $this->errorMessage(array(Imap::ERR_AUTH, Imap::ERR_AUTH_OAUTH));
return false;
}
}
if ($mech == false)
{
if (preg_match('/ x20 AUTH=PLAIN ( x20 | rn ) /ix', $this->sessCapability))
$mech = 'plain';
elseif (!preg_match('/ x20 LOGINDISABLED ( x20 | rn ) /ix', $this->sessCapability))
$mech = 'login';
}
if (!$mech)
{
$error = $this->errorMessage(array(Imap::ERR_AUTH, Imap::ERR_AUTH_MECH));
return false;
}
if ($mech == 'oauth')
{
$response = $this->executeCommand('AUTHENTICATE XOAUTH2', $error);
if (mb_strpos($response, '+') !== 0)
{
$error = $error == Imap::ERR_COMMAND_REJECTED ? Imap::ERR_AUTH_MECH : $error;
$error = $this->errorMessage(array(Imap::ERR_AUTH, $error), $response);
return false;
}
$response = $this->exchange(base64_encode(sprintf(
"user=%sx01auth=Bearer %sx01x01", $this->options['login'], $token
)), $error);
if (mb_strpos($response, '+') === 0)
$response = $this->exchange("rn", $error);
}
elseif ($mech == 'plain')
{
$response = $this->executeCommand('AUTHENTICATE PLAIN', $error);
if (mb_strpos($response, '+') !== 0)
{
$error = $error == Imap::ERR_COMMAND_REJECTED ? Imap::ERR_AUTH_MECH : $error;
$error = $this->errorMessage(array(Imap::ERR_AUTH, $error), $response);
return false;
}
$response = $this->exchange(base64_encode(sprintf(
"x00%sx00%s",
Encoding::convertEncoding($this->options['login'], $this->options['encoding'], 'UTF-8'),
Encoding::convertEncoding($this->options['password'], $this->options['encoding'], 'UTF-8')
)), $error);
}
else // if ($mech == 'login')
{
$response = $this->executeCommand(sprintf(
'LOGIN %s %s',
static::prepareString($this->options['login']),
static::prepareString($this->options['password'])
), $error);
}
if ($error)
{
$error = $error == Imap::ERR_COMMAND_REJECTED ? null : $error;
$error = $this->errorMessage(array(Imap::ERR_AUTH, $error), $response);
return false;
}
$this->sessState = 'auth';
if (!$this->capability($error))
return false;
return true;
}