function Connect($mailbox_id) { global $DB; $mailbox_id = IntVal($mailbox_id); $strSql = "SELECT MB.*, l.CHARSET as LANG_CHARSET " . "FROM b_mail_mailbox MB, b_lang l " . "WHERE MB.LID=l.LID " . "\tAND MB.ID=" . $mailbox_id; $dbr = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); $dbr = new _CMailBoxDBRes($dbr); if (!($arMAILBOX_PARAMS = $dbr->Fetch())) { return CMailError::SetError("ERR_MAILBOX_NOT_FOUND", GetMessage("MAIL_CL_ERR_MAILBOX_NOT_FOUND"), GetMessage("MAIL_CL_ERR_MAILBOX_NOT_FOUND")); } @set_time_limit(0); $server = $arMAILBOX_PARAMS["SERVER"]; if ($arMAILBOX_PARAMS['USE_TLS'] == 'Y' && strpos($server, 'tls://') === false) { $server = 'tls://' . $server; } $pop3_conn =& $this->pop3_conn; $pop3_conn = @fsockopen($server, $arMAILBOX_PARAMS["PORT"], $errno, $errstr, COption::GetOptionInt("mail", "connect_timeout", B_MAIL_TIMEOUT)); 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]; 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); $arLIST = array(); for ($i = 0; $i < count($arLIST_temp); $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); $arStrIDs = array(); $arUIDL = array(); $cnt = count($arUIDL_temp); for ($i = 0; $i < $cnt; $i++) { $msguid = md5($arUIDL_temp[$i][2]); $arUIDL[$msguid] = $arUIDL_temp[$i][1]; $arStrIDs[floor($i / 1000)] .= ", '" . $DB->ForSQL($msguid) . "'"; } if (count($arUIDL) > 0) { $cnt = count($arStrIDs); for ($i = 0; $i < $cnt; ++$i) { $ids = 'ID IN (' . substr($arStrIDs[$i], 2) . ')'; $strSql = "SELECT ID FROM b_mail_message_uid WHERE MAILBOX_ID=" . $mailbox_id . " AND ({$ids})"; $db_res = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); while ($ar_res = $db_res->Fetch()) { unset($arUIDL[$ar_res["ID"]]); } $strSql = "UPDATE b_mail_message_uid SET SESSION_ID = '" . $session_id . "' WHERE MAILBOX_ID=" . $mailbox_id . " AND ({$ids})"; $db_res = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } } $strSql = "DELETE FROM b_mail_message_uid WHERE MAILBOX_ID=" . $mailbox_id . " AND SESSION_ID <> '" . $session_id . "'"; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>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; }
function Connect($mailbox_id) { global $DB; $mailbox_id = IntVal($mailbox_id); $strSql = "SELECT MB.*, C.CHARSET as LANG_CHARSET ". "FROM b_mail_mailbox MB, b_lang L, b_culture C ". "WHERE MB.LID=L.LID AND C.ID=L.CULTURE_ID ". " AND MB.ID=".$mailbox_id; $dbr = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__); $dbr = new _CMailBoxDBRes($dbr); if(!$arMAILBOX_PARAMS = $dbr->Fetch()) return CMailError::SetError("ERR_MAILBOX_NOT_FOUND", GetMessage("MAIL_CL_ERR_MAILBOX_NOT_FOUND"), GetMessage("MAIL_CL_ERR_MAILBOX_NOT_FOUND")); @set_time_limit(0); // https://support.google.com/mail/answer/47948 if ($arMAILBOX_PARAMS["SERVER"] == 'pop.gmail.com') $arMAILBOX_PARAMS["LOGIN"] = '******' . $arMAILBOX_PARAMS["LOGIN"]; $server = $arMAILBOX_PARAMS["SERVER"]; if ($arMAILBOX_PARAMS['USE_TLS'] == 'Y' && strpos($server, 'tls://') === false) { $server = 'tls://' . $server; } $pop3_conn = &$this->pop3_conn; $pop3_conn = @fsockopen($server, $arMAILBOX_PARAMS["PORT"], $errno, $errstr, COption::GetOptionInt("mail", "connect_timeout", B_MAIL_TIMEOUT)); 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( "%s\n%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__.'<br>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)) . "'"; $strSql = 'DELETE FROM b_mail_message_uid WHERE MAILBOX_ID = ' . $mailbox_id . ' AND ID IN (' . $ids . ')'; $DB->query($strSql, false, 'File: '.__FILE__.'<br>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; }
function Connect($mailbox_id) { global $DB; $mailbox_id = IntVal($mailbox_id); $strSql = "SELECT MB.*, C.CHARSET as LANG_CHARSET " . "FROM b_mail_mailbox MB, b_lang L, b_culture C " . "WHERE MB.LID=L.LID AND C.ID=L.CULTURE_ID " . "\tAND MB.ID=" . $mailbox_id; $dbr = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); $dbr = new _CMailBoxDBRes($dbr); if (!($arMAILBOX_PARAMS = $dbr->Fetch())) { return CMailError::SetError("ERR_MAILBOX_NOT_FOUND", GetMessage("MAIL_CL_ERR_MAILBOX_NOT_FOUND"), GetMessage("MAIL_CL_ERR_MAILBOX_NOT_FOUND")); } if ($arMAILBOX_PARAMS['SYNC_LOCK'] > time() - 600) { return; } $DB->query('UPDATE b_mail_mailbox SET SYNC_LOCK = ' . time() . ' WHERE ID = ' . $mailbox_id); $result = $this->_connect($mailbox_id, $arMAILBOX_PARAMS); $DB->query('UPDATE b_mail_mailbox SET SYNC_LOCK = 0 WHERE ID = ' . $mailbox_id); return $result; }