private function _connect($mailbox_id, $arMAILBOX_PARAMS)
{
global $DB;
@set_time_limit(0);
// https://support.google.com/mail/answer/47948
if ($arMAILBOX_PARAMS["SERVER"] == 'pop.gmail.com')
$arMAILBOX_PARAMS["LOGIN"] = 'recent:' . $arMAILBOX_PARAMS["LOGIN"];
$server = $arMAILBOX_PARAMS["SERVER"];
if (($arMAILBOX_PARAMS['USE_TLS'] == 'Y' || $arMAILBOX_PARAMS['USE_TLS'] == 'S') && !preg_match('#^(tls|ssl)://#', $server))
$server = 'ssl://' . $server;
$skip_cert = $arMAILBOX_PARAMS['USE_TLS'] != 'Y';
$pop3_conn = &$this->pop3_conn;
$pop3_conn = stream_socket_client(
sprintf('%s:%s', $server, $arMAILBOX_PARAMS["PORT"]),
$errno, $errstr,
COption::getOptionInt('mail', 'connect_timeout', B_MAIL_TIMEOUT),
STREAM_CLIENT_CONNECT,
stream_context_create(array('ssl' => array('verify_peer' => !$skip_cert, 'verify_peer_name' => !$skip_cert)))
);
CMailLog::AddMessage(
Array(
"MAILBOX_ID"=>$mailbox_id,
"STATUS_GOOD"=>"Y",
"MESSAGE"=>GetMessage("MAIL_CL_CONNECT_TO")." ".$arMAILBOX_PARAMS["SERVER"]
)
);
if(!$pop3_conn || !is_resource($pop3_conn))
{
CMailLog::AddMessage(
Array(
"MAILBOX_ID"=>$mailbox_id,
"STATUS_GOOD"=>"N",
"MESSAGE"=>GetMessage("MAIL_CL_TIMEOUT")
)
);
return CMailError::SetError("ERR_CONNECT_TIMEOUT", GetMessage("MAIL_CL_TIMEOUT"), "$errstr ($errno)");
}
$this->mailbox_id = $mailbox_id;
if($arMAILBOX_PARAMS["CHARSET"]!='')
$this->charset = $arMAILBOX_PARAMS["CHARSET"];
else
$this->charset = $arMAILBOX_PARAMS["LANG_CHARSET"];
$this->use_md5 = $arMAILBOX_PARAMS["USE_MD5"];
$session_id = md5(uniqid(""));
$this->GetResponse();
$greeting = $this->GetResponseString();
if($this->use_md5=="Y" && preg_match("'(<.+>)'", $greeting, $reg))
{
$this->SendCommand("APOP ".$arMAILBOX_PARAMS["LOGIN"]." ".md5($reg[1].$arMAILBOX_PARAMS["PASSWORD"]));
if(!$this->GetResponse())
return CMailError::SetError("ERR_AFTER_USER", GetMessage("MAIL_CL_ERR_APOP"), $this->GetResponseString());
}
else
{
$this->SendCommand("USER ".$arMAILBOX_PARAMS["LOGIN"]);
if(!$this->GetResponse())
return CMailError::SetError("ERR_AFTER_USER", GetMessage("MAIL_CL_ERR_USER"), $this->GetResponseString());
$this->SendCommand("PASS ".$arMAILBOX_PARAMS["PASSWORD"]);
if(!$this->GetResponse())
return CMailError::SetError("ERR_AFTER_PASS", GetMessage("MAIL_CL_ERR_PASSWORD"), $this->GetResponseString());
}
$this->SendCommand("STAT");
if(!$this->GetResponse())
return CMailError::SetError("ERR_AFTER_STAT", GetMessage("MAIL_CL_ERR_STAT"), $this->GetResponseString());
$stat = trim($this->GetResponseBody());
$arStat = explode(" ", $stat);
$this->mess_count = $arStat[1];
if($this->mess_count>0)
{
$this->mess_size = $arStat[2];
$arLIST = array();
if($arMAILBOX_PARAMS["MAX_MSG_SIZE"]>0)
{
$this->SendCommand("LIST");
if(!$this->GetResponse(true))
return CMailError::SetError("ERR_AFTER_LIST", "LIST command error", $this->GetResponseString());
$list = $this->GetResponseBody();
preg_match_all("'([0-9]+)[ ]+?(.+)'", $list, $arLIST_temp, PREG_SET_ORDER);
for($i = 0, $n = count($arLIST_temp); $i < $n; $i++)
$arLIST[intval($arLIST_temp[$i][1])] = intval($arLIST_temp[$i][2]);
}
$this->SendCommand("UIDL");
if(!$this->GetResponse(true))
return CMailError::SetError("ERR_AFTER_UIDL", GetMessage("MAIL_CL_ERR_UIDL"), $this->GetResponseString());
$uidl = $this->GetResponseBody();
preg_match_all("'([0-9]+)[ ]+?(.+)'", $uidl, $arUIDL_temp, PREG_SET_ORDER);
$arUIDL = array();
$cnt = count($arUIDL_temp);
for ($i = 0; $i < $cnt; $i++)
$arUIDL[md5($arUIDL_temp[$i][2])] = $arUIDL_temp[$i][1];
$skipOldUIDL = $cnt < $this->mess_count;
if ($skipOldUIDL)
{
AddMessage2Log(sprintf(
"%sn%s of %s",
$this->response, $cnt, $this->mess_count
), 'mail');
}
$arOldUIDL = array();
if (count($arUIDL) > 0)
{
$strSql = 'SELECT ID FROM b_mail_message_uid WHERE MAILBOX_ID = ' . $mailbox_id;
$db_res = $DB->query($strSql, false, 'File: '.__FILE__.'
Line: '.__LINE__);
while ($ar_res = $db_res->fetch())
{
if (isset($arUIDL[$ar_res['ID']]))
unset($arUIDL[$ar_res['ID']]);
else if (!$skipOldUIDL)
$arOldUIDL[] = $ar_res['ID'];
}
}
while (count($arOldUIDL) > 0)
{
$ids = "'" . join("','", array_splice($arOldUIDL, 0, 1000)) . "'";
// @TODO: make a log optional
/*$toLog = [
'filter'=>'CAllMailBox::_connect',
'removedMessages'=>$ids,
];
AddMessage2Log($toLog);*/
$strSql = 'DELETE FROM b_mail_message_uid WHERE MAILBOX_ID = ' . $mailbox_id . ' AND ID IN (' . $ids . ')';
$DB->query($strSql, false, 'File: '.__FILE__.'
Line: '.__LINE__);
}
$this->new_mess_count = 0;
$this->deleted_mess_count = 0;
$session_id = md5(uniqid(""));
foreach($arUIDL as $msguid=>$msgnum)
{
if($arMAILBOX_PARAMS["MAX_MSG_SIZE"]<=0 || $arLIST[$msgnum]<=$arMAILBOX_PARAMS["MAX_MSG_SIZE"])
$this->GetMessage($mailbox_id, $msgnum, $msguid, $session_id);
if($arMAILBOX_PARAMS["DELETE_MESSAGES"]=="Y")
{
$this->DeleteMessage($msgnum);
$this->deleted_mess_count++;
}
$this->new_mess_count++;
if($arMAILBOX_PARAMS["MAX_MSG_COUNT"]>0 && $arMAILBOX_PARAMS["MAX_MSG_COUNT"]<=$this->new_mess_count)
break;
}
}
$this->SendCommand("QUIT");
if(!$this->GetResponse())
return CMailError::SetError("ERR_AFTER_QUIT", GetMessage("MAIL_CL_ERR_DISCONNECT"), $this->GetResponseString());
fclose($pop3_conn);
return true;
}