Пример #1
0
 public static function GetPHPMailMark()
 {
     $addr = "*****@*****.**";
     $subj = "Bitrix server test";
     $body = "This is test message. Delete it.";
     $s1 = getmicrotime();
     bxmail($addr, $subj, $body);
     $e1 = getmicrotime();
     $t1 = $e1 - $s1;
     return $t1;
 }
Пример #2
0
 function HandleEvent($arEvent)
 {
     global $DB;
     $flag = "0";
     // no templates
     $arResult = array("Success" => false, "Fail" => false, "Was" => false);
     $eol = CAllEvent::GetMailEOL();
     $ar = CAllEvent::ExtractMailFields($arEvent["C_FIELDS"]);
     $arSites = explode(",", $arEvent["LID"]);
     foreach ($arSites as $key => $value) {
         $value = trim($value);
         if (strlen($value) > 0) {
             $arSites[$key] = "'" . $DB->ForSql($value, 2) . "'";
         } else {
             unset($arSites[$key]);
         }
     }
     if (count($arSites) <= 0) {
         return $flag;
     }
     $strSites = implode(", ", $arSites);
     $strSql = "SELECT CHARSET FROM b_lang WHERE LID IN (" . $strSites . ") ORDER BY DEF DESC, SORT";
     $dbCharset = $DB->Query($strSql, false, "FILE: " . __FILE__ . "<br>LINE: " . __LINE__);
     $arCharset = $dbCharset->Fetch();
     if (!$arCharset) {
         return $flag;
     }
     $charset = $arCharset["CHARSET"];
     $strWhere = "";
     $MESSAGE_ID = intval($arEvent["MESSAGE_ID"]);
     if ($MESSAGE_ID > 0) {
         $strSql = "SELECT 'x' FROM b_event_message M WHERE M.ID=" . $MESSAGE_ID;
         $z = $DB->Query($strSql);
         if ($z->Fetch()) {
             $strWhere = "WHERE M.ID=" . $MESSAGE_ID . " and M.ACTIVE='Y'";
         }
     }
     $strSql = "\n\t\t\tSELECT DISTINCT ID\n\t\t\tFROM b_event_message M\n\t\t\t" . ($strWhere == "" ? ", b_event_message_site MS\n\t\t\t\tWHERE M.ID=MS.EVENT_MESSAGE_ID\n\t\t\t\tAND M.ACTIVE='Y'\n\t\t\t\tAND M.EVENT_NAME='" . $DB->ForSql($arEvent["EVENT_NAME"]) . "'\n\t\t\t\tAND MS.SITE_ID IN (" . $strSites . ")" : $strWhere) . "\n\t\t";
     $db_mail_result = $DB->Query($strSql);
     while ($db_mail_result_array = $db_mail_result->Fetch()) {
         $rsMail = $DB->Query("\n\t\t\t\tSELECT ID, SUBJECT, MESSAGE, EMAIL_FROM, EMAIL_TO, BODY_TYPE, BCC, CC, REPLY_TO, IN_REPLY_TO, PRIORITY, FIELD1_NAME, FIELD1_VALUE, FIELD2_NAME, FIELD2_VALUE\n\t\t\t\tFROM b_event_message M\n\t\t\t\tWHERE M.ID = " . intval($db_mail_result_array["ID"]) . "\n\t\t\t");
         $db_mail_result_array = $rsMail->Fetch();
         if (!$db_mail_result_array) {
             continue;
         }
         $strSqlMLid = "\n\t\t\t\tSELECT MS.SITE_ID\n\t\t\t\tFROM b_event_message_site MS\n\t\t\t\tWHERE MS.EVENT_MESSAGE_ID = " . $db_mail_result_array["ID"] . "\n\t\t\t\tAND MS.SITE_ID IN (" . $strSites . ")\n\t\t\t";
         $dbr_mlid = $DB->Query($strSqlMLid);
         if ($ar_mlid = $dbr_mlid->Fetch()) {
             $arFields = $ar + CAllEvent::GetSiteFieldsArray($ar_mlid["SITE_ID"]);
         } else {
             $arFields = $ar + CAllEvent::GetSiteFieldsArray(false);
         }
         foreach (GetModuleEvents("main", "OnBeforeEventSend", true) as $event) {
             ExecuteModuleEventEx($event, array(&$arFields, &$db_mail_result_array));
         }
         $arMailFields = array();
         $arMailFields["From"] = CAllEvent::ReplaceTemplate($db_mail_result_array["EMAIL_FROM"], $arFields);
         if ($db_mail_result_array["BCC"] != '') {
             $bcc = CAllEvent::ReplaceTemplate($db_mail_result_array["BCC"], $arFields);
             if (strpos($bcc, "@") !== false) {
                 $arMailFields["BCC"] = $bcc;
             }
         }
         if ($db_mail_result_array["CC"] != '') {
             $arMailFields["CC"] = CAllEvent::ReplaceTemplate($db_mail_result_array["CC"], $arFields);
         }
         if ($db_mail_result_array["REPLY_TO"] != '') {
             $arMailFields["Reply-To"] = CAllEvent::ReplaceTemplate($db_mail_result_array["REPLY_TO"], $arFields);
         } else {
             $arMailFields["Reply-To"] = preg_replace("/(.*)\\<(.*)\\>/i", '$2', $arMailFields["From"]);
         }
         if ($db_mail_result_array["IN_REPLY_TO"] != '') {
             $arMailFields["In-Reply-To"] = CAllEvent::ReplaceTemplate($db_mail_result_array["IN_REPLY_TO"], $arFields);
         }
         if ($db_mail_result_array['FIELD1_NAME'] != '' && $db_mail_result_array['FIELD1_VALUE'] != '') {
             $arMailFields[$db_mail_result_array['FIELD1_NAME']] = CAllEvent::ReplaceTemplate($db_mail_result_array["FIELD1_VALUE"], $arFields);
         }
         if ($db_mail_result_array['FIELD2_NAME'] != '' && $db_mail_result_array['FIELD2_VALUE'] != '') {
             $arMailFields[$db_mail_result_array['FIELD2_NAME']] = CAllEvent::ReplaceTemplate($db_mail_result_array["FIELD2_VALUE"], $arFields);
         }
         if ($db_mail_result_array["PRIORITY"] != '') {
             $arMailFields["X-Priority"] = CAllEvent::ReplaceTemplate($db_mail_result_array["PRIORITY"], $arFields);
         } else {
             $arMailFields["X-Priority"] = '3 (Normal)';
         }
         foreach ($ar as $f => $v) {
             if (substr($f, 0, 1) == "=") {
                 $arMailFields[substr($f, 1)] = $v;
             }
         }
         foreach ($arMailFields as $k => $v) {
             $arMailFields[$k] = trim($v, "\r\n");
         }
         //add those who want to receive all emails
         if ($arEvent["DUPLICATE"] == "Y") {
             $all_bcc = COption::GetOptionString("main", "all_bcc", "");
             if (strpos($all_bcc, "@") !== false) {
                 $arMailFields["BCC"] .= strlen($all_bcc) > 0 ? (strlen($arMailFields["BCC"]) > 0 ? "," : "") . $all_bcc : "";
             }
         }
         $email_to = CAllEvent::ReplaceTemplate($db_mail_result_array["EMAIL_TO"], $arFields);
         $subject = CAllEvent::ReplaceTemplate($db_mail_result_array["SUBJECT"], $arFields);
         if (COption::GetOptionString("main", "convert_mail_header", "Y") == "Y") {
             foreach ($arMailFields as $k => $v) {
                 if ($k == 'From' || $k == 'CC') {
                     $arMailFields[$k] = CAllEvent::EncodeHeaderFrom($v, $charset);
                 } else {
                     $arMailFields[$k] = CAllEvent::EncodeMimeString($v, $charset);
                 }
             }
             $email_to = CAllEvent::EncodeHeaderFrom($email_to, $charset);
             $subject = CAllEvent::EncodeMimeString($subject, $charset);
         }
         if (defined("BX_MS_SMTP") && BX_MS_SMTP === true) {
             $email_to = preg_replace("/(.*)\\<(.*)\\>/i", '$2', $email_to);
             if ($arMailFields["From"] != '') {
                 $arMailFields["From"] = preg_replace("/(.*)\\<(.*)\\>/i", '$2', $arMailFields["From"]);
             }
             if ($arMailFields["To"] != '') {
                 $arMailFields["To"] = preg_replace("/(.*)\\<(.*)\\>/i", '$2', $arMailFields["To"]);
             }
         }
         if (COption::GetOptionString("main", "fill_to_mail", "N") == "Y") {
             $arMailFields["To"] = $email_to;
         }
         $header = "";
         foreach ($arMailFields as $k => $v) {
             $header .= $k . ': ' . $v . $eol;
         }
         $header .= "X-MID: " . $arEvent["ID"] . "." . $db_mail_result_array["ID"] . " (" . $arEvent["DATE_INSERT"] . ")" . $eol . "X-EVENT_NAME: " . $arEvent["EVENT_NAME"] . $eol . ($db_mail_result_array["BODY_TYPE"] == "html" ? "Content-Type: text/html; charset=" . $charset . $eol : "Content-Type: text/plain; charset=" . $charset . $eol) . "Content-Transfer-Encoding: 8bit";
         $bNewLineToBreak = $db_mail_result_array["BODY_TYPE"] == "html";
         $message = CAllEvent::ReplaceTemplate($db_mail_result_array["MESSAGE"], $arFields, $bNewLineToBreak);
         if (COption::GetOptionString("main", "send_mid", "N") == "Y") {
             $message .= ($db_mail_result_array["BODY_TYPE"] == "html" ? "<br><br>" : "\n\n") . "MID #" . $arEvent["ID"] . "." . $db_mail_result_array["ID"] . " (" . $arEvent["DATE_INSERT"] . ")\n";
         }
         $message = str_replace("\r\n", "\n", $message);
         if (COption::GetOptionString("main", "CONVERT_UNIX_NEWLINE_2_WINDOWS", "N") == "Y") {
             $message = str_replace("\n", "\r\n", $message);
         }
         if (defined("ONLY_EMAIL") && $email_to != ONLY_EMAIL) {
             $arResult["Success"] = true;
         } elseif (bxmail($email_to, $subject, $message, $header, COption::GetOptionString("main", "mail_additional_parameters", ""))) {
             $arResult["Success"] = true;
         } else {
             $arResult["Fail"] = true;
         }
         $arResult["Was"] = true;
     }
     if ($arResult["Was"]) {
         if ($arResult["Success"]) {
             if ($arResult["Fail"]) {
                 $flag = "P";
             } else {
                 $flag = "Y";
             }
             // all sent
         } else {
             if ($arResult["Fail"]) {
                 $flag = "F";
             }
             // all templates failed
         }
     }
     return $flag;
 }
Пример #3
0
 function __DataHandler()
 {
     if (strpos($this->readBuffer, "\r\n.\r\n") === false) {
         return false;
     }
     $this->readBuffer = substr($this->readBuffer, 0, -5);
     $this->readBuffer = str_replace("\r\n..", "\r\n.", $this->readBuffer);
     // Добавление сообщения куда надо
     $message = $this->readBuffer;
     $this->arMsg["MSG"] = $message;
     $this->WriteToLog('[' . $this->arMsg["LOCAL_ID"] . '] Start processing mail...', 7);
     $p = strpos($message, "\r\n\r\n");
     if ($p > 0) {
         $message_header = substr($message, 0, $p);
         $message_text = substr($message, $p + 2);
         $arLocalTo = array();
         foreach ($this->arMsg["TO"] as $to) {
             if (is_array($this->arMsg["FOR_RELAY"]) && in_array($to, $this->arMsg["FOR_RELAY"])) {
                 $message_header_add = "Received: from " . $this->host . " by " . $this->server->arFields["SERVER"] . " with Bitrix SMTP Server \r\n" . "\t" . date("r") . "\r\n" . "\tfor <" . $to . ">; \r\n" . "Return-Path: <" . $this->arMsg["FROM"] . ">\r\n";
                 $subject = "";
                 $message_header_new = $message_header;
                 if (preg_match('/(Subject:\\s*([^\\r\\n]*\\r\\n(\\t[^\\r\\n]*\\r\\n)*))\\S/is', $message_header_new . "\r\nx", $reg)) {
                     $message_header_new = trim(str_replace($reg[1], "", $message_header_new . "\r\n"));
                     $subject = trim($reg[2]);
                 }
                 $r = bxmail($to, $subject, $message_text, $message_header_add . $message_header_new);
                 $this->WriteToLog('[' . $this->arMsg["LOCAL_ID"] . '] Relay message to ' . $to . ' (' . ($r ? 'OK' : 'FAILED') . ')', 7);
             } else {
                 $arLocalTo[] = $to;
             }
         }
         if (count($arLocalTo) > 0) {
             $message_header_add = "Received: from " . $this->host . " by " . $this->server->arFields["SERVER"] . " with Bitrix SMTP Server \r\n" . "\t" . date("r") . "\r\n" . "Return-Path: <" . $this->arMsg["FROM"] . ">\r\n" . "X-Original-Rcpt-to: " . implode(", ", $arLocalTo) . "\r\n";
             $this->WriteToLog('[' . $this->arMsg["LOCAL_ID"] . '] Message add: ' . $message_header_add . $message, 9);
             if ($this->server->arFields["CHARSET"] != '') {
                 $charset = $this->server->arFields["CHARSET"];
             } else {
                 $charset = $this->server->arFields["LANG_CHARSET"];
             }
             $message_id = CMailMessage::AddMessage($this->server->arFields["ID"], $message_header_add . $message, $charset);
             $this->WriteToLog('[' . $this->arMsg["LOCAL_ID"] . '] Message sent to ' . implode(", ", $arLocalTo) . ' (' . $message_id . ')', 7);
         }
         $this->Send('250', $message_id . ' Message accepted for delivery');
     } else {
         $this->Send('554', ' Bad message format');
     }
     $this->WriteToLog('[' . $this->arMsg["LOCAL_ID"] . '] End processing mail...', 7);
     $this->readBuffer = "";
     $this->__listenFunc = false;
     $this->arMsg = array('LOCAL_ID' => md5(uniqid()));
     $this->msgCount++;
     $this->server->msgCount++;
     return true;
 }
Пример #4
0
function SendError($error)
{
	if(defined('ERROR_EMAIL') && ERROR_EMAIL <> '')
	{
		$from = (defined('ERROR_EMAIL_FROM') && ERROR_EMAIL_FROM <> ''? ERROR_EMAIL_FROM : '*****@*****.**');
		$reply_to = (defined('ERROR_EMAIL_REPLY_TO') && ERROR_EMAIL_REPLY_TO <> ''? ERROR_EMAIL_REPLY_TO : '*****@*****.**');
		bxmail(ERROR_EMAIL, $_SERVER['HTTP_HOST'].": Error!",
			$error.
			"HTTP_GET_VARS:\n".mydump($_GET)."\n\n".
			"HTTP_POST_VARS:\n".mydump($_POST)."\n\n".
			"HTTP_COOKIE_VARS:\n".mydump($_COOKIE)."\n\n".
			"HTTP_SERVER_VARS:\n".mydump($_SERVER)."\n\n",
			"From: ".$from."\r\n".
			"Reply-To: ".$reply_to."\r\n".
			"X-Mailer: PHP/" . phpversion()
		);
	}
}
Пример #5
0
 /**
  * Universal method for checking comment or new user for spam
  * It makes checking itself
  * Use it in your modules
  * You must call it from OnBefore* events
  * @param &array Entity to check (comment or new user)
  * @param boolean Notify admin about errors by email or not (default FALSE)
  * @return array|null Checking result or NULL when bad params
  */
 static function CheckAllBefore(&$arEntity, $bSendEmail = FALSE)
 {
     global $DB;
     if (!is_array($arEntity) || !array_key_exists('type', $arEntity)) {
         CEventLog::Add(array('SEVERITY' => 'SECURITY', 'AUDIT_TYPE_ID' => 'CLEANTALK_E_INTERNAL', 'MODULE_ID' => 'cleantalk.antispam', 'DESCRIPTION' => GetMessage('CLEANTALK_E_PARAM')));
         return;
     }
     $type = $arEntity['type'];
     if ($type != 'comment' && $type != 'register') {
         CEventLog::Add(array('SEVERITY' => 'SECURITY', 'AUDIT_TYPE_ID' => 'CLEANTALK_E_INTERNAL', 'MODULE_ID' => 'cleantalk.antispam', 'DESCRIPTION' => GetMessage('CLEANTALK_E_TYPE')));
         return;
     }
     require_once dirname(__FILE__) . '/classes/general/cleantalk.class.php';
     $ct_key = COption::GetOptionString('cleantalk.antispam', 'key', '0');
     $ct_ws = self::GetWorkServer();
     $ct_submit_time = NULL;
     if (isset($_SESSION['ct_submit_time'])) {
         $ct_submit_time = time() - $_SESSION['ct_submit_time'];
     }
     if (!isset($_COOKIE['ct_checkjs'])) {
         $checkjs = NULL;
     } elseif (in_array($_COOKIE['ct_checkjs'], self::GetCheckJSValues())) {
         $checkjs = 1;
     } else {
         $checkjs = 0;
     }
     if (isset($_SERVER['HTTP_USER_AGENT'])) {
         $user_agent = htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']);
     } else {
         $user_agent = NULL;
     }
     if (isset($_SERVER['HTTP_REFERER'])) {
         $refferrer = htmlspecialchars((string) $_SERVER['HTTP_REFERER']);
     } else {
         $refferrer = NULL;
     }
     $sender_info = array('cms_lang' => 'ru', 'REFFERRER' => $refferrer, 'post_url' => $refferrer, 'USER_AGENT' => $user_agent);
     $sender_info = json_encode($sender_info);
     $ct = new Cleantalk();
     $ct->work_url = $ct_ws['work_url'];
     $ct->server_url = $ct_ws['server_url'];
     $ct->server_ttl = $ct_ws['server_ttl'];
     $ct->server_changed = $ct_ws['server_changed'];
     if (defined('BX_UTF')) {
         $logicalEncoding = "utf-8";
     } elseif (defined("SITE_CHARSET") && strlen(SITE_CHARSET) > 0) {
         $logicalEncoding = SITE_CHARSET;
     } elseif (defined("LANG_CHARSET") && strlen(LANG_CHARSET) > 0) {
         $logicalEncoding = LANG_CHARSET;
     } elseif (defined("BX_DEFAULT_CHARSET")) {
         $logicalEncoding = BX_DEFAULT_CHARSET;
     } else {
         $logicalEncoding = "windows-1251";
     }
     $logicalEncoding = strtolower($logicalEncoding);
     $ct->data_codepage = $logicalEncoding == 'utf-8' ? NULL : $logicalEncoding;
     $ct_request = new CleantalkRequest();
     $ct_request->auth_key = $ct_key;
     $ct_request->sender_email = isset($arEntity['sender_email']) ? $arEntity['sender_email'] : '';
     $ct_request->sender_nickname = isset($arEntity['sender_nickname']) ? $arEntity['sender_nickname'] : '';
     $ct_request->sender_ip = $ct->ct_session_ip($_SERVER['REMOTE_ADDR']);
     $ct_request->agent = 'bitrix-370';
     $ct_request->response_lang = 'ru';
     $ct_request->js_on = $checkjs;
     $ct_request->sender_info = $sender_info;
     switch ($type) {
         case 'comment':
             $timelabels_key = 'mail_error_comment';
             $ct_request->submit_time = $ct_submit_time;
             $ct_request->message = isset($arEntity['message_title']) ? $arEntity['message_title'] : '';
             $ct_request->message .= "\n\n";
             $ct_request->message .= isset($arEntity['message_body']) ? $arEntity['message_body'] : '';
             $ct_request->example = isset($arEntity['example_title']) ? $arEntity['example_title'] : '';
             $ct_request->example .= empty($ct_request->example) ? '' : "\n\n";
             $ct_request->example .= isset($arEntity['example_body']) ? $arEntity['example_body'] : '';
             $ct_request->example .= empty($ct_request->example) ? '' : "\n\n";
             $ct_request->example .= isset($arEntity['example_comments']) ? $arEntity['example_comments'] : '';
             if (empty($ct_request->example)) {
                 $ct_request->example = NULL;
             }
             $a_post_info['comment_type'] = 'comment';
             $post_info = json_encode($a_post_info);
             if ($post_info === FALSE) {
                 $post_info = '';
             }
             $ct_request->post_info = $post_info;
             $ct_result = $ct->isAllowMessage($ct_request);
             break;
         case 'register':
             $timelabels_key = 'mail_error_reg';
             $ct_request->submit_time = $ct_submit_time;
             $ct_request->tz = isset($arEntity['user_timezone']) ? $arEntity['user_timezone'] : NULL;
             $ct_result = $ct->isAllowUser($ct_request);
     }
     $ret_val = array();
     $ret_val['ct_request_id'] = $ct_result->id;
     if ($ct->server_change) {
         self::SetWorkServer($ct->work_url, $ct->server_url, $ct->server_ttl, time());
     }
     // First check errstr flag.
     if (!empty($ct_result->errstr) || !empty($ct_result->inactive) && $ct_result->inactive == 1) {
         // Cleantalk error so we go default way (no action at all).
         $ret_val['errno'] = 1;
         // Just inform admin.
         $err_title = 'CleanTalk module error';
         if (!empty($ct_result->errstr)) {
             if (preg_match('//u', $ct_result->errstr)) {
                 $err_str = preg_replace('/^[^\\*]*?\\*\\*\\*|\\*\\*\\*[^\\*]*?$/iu', '', $ct_result->errstr);
             } else {
                 $err_str = preg_replace('/^[^\\*]*?\\*\\*\\*|\\*\\*\\*[^\\*]*?$/i', '', $ct_result->errstr);
             }
         } else {
             if (preg_match('//u', $ct_result->comment)) {
                 $err_str = preg_replace('/^[^\\*]*?\\*\\*\\*|\\*\\*\\*[^\\*]*?$/iu', '', $ct_result->comment);
             } else {
                 $err_str = preg_replace('/^[^\\*]*?\\*\\*\\*|\\*\\*\\*[^\\*]*?$/i', '', $ct_result->comment);
             }
         }
         $ret_val['errstr'] = $err_str;
         CEventLog::Add(array('SEVERITY' => 'SECURITY', 'AUDIT_TYPE_ID' => 'CLEANTALK_E_SERVER', 'MODULE_ID' => 'cleantalk.antispam', 'DESCRIPTION' => $err_str));
         if ($bSendEmail) {
             $send_flag = FALSE;
             $insert_flag = FALSE;
             $time = $DB->Query('SELECT ct_value FROM cleantalk_timelabels WHERE ct_key=\'' . $timelabels_key . '\'')->Fetch();
             if ($time === FALSE) {
                 $send_flag = TRUE;
                 $insert_flag = TRUE;
             } elseif (time() - 900 > $time['ct_value']) {
                 // 15 minutes
                 $send_flag = TRUE;
                 $insert_flag = FALSE;
             }
             if ($send_flag) {
                 if ($insert_flag) {
                     $arInsert = $DB->PrepareInsert('cleantalk_timelabels', array('ct_key' => $timelabels_key, 'ct_value' => time()));
                     $strSql = 'INSERT INTO cleantalk_timelabels(' . $arInsert[0] . ') VALUES (' . $arInsert[1] . ')';
                 } else {
                     $strUpdate = $DB->PrepareUpdate('cleantalk_timelabels', array('ct_value' => time()));
                     $strSql = 'UPDATE cleantalk_timelabels SET ' . $strUpdate . ' WHERE ct_key = \'' . $timelabels_key . '\'';
                 }
                 $DB->Query($strSql);
                 bxmail(COption::GetOptionString("main", "email_from"), $err_title, $err_str);
             }
         }
         return $ret_val;
     }
     $ret_val['errno'] = 0;
     if ($ct_result->allow == 1) {
         // Not spammer.
         $ret_val['allow'] = 1;
         $GLOBALS['ct_request_id'] = $ct_result->id;
     } else {
         $ret_val['allow'] = 0;
         $ret_val['ct_result_comment'] = $ct_result->comment;
         // Spammer.
         // Check stop_queue flag.
         if ($type == 'comment' && $ct_result->stop_queue == 0) {
             // Spammer and stop_queue == 0 - to manual approvement.
             $ret_val['stop_queue'] = 0;
             $GLOBALS['ct_request_id'] = $ct_result->id;
             $GLOBALS['ct_result_comment'] = $ct_result->comment;
         } else {
             // New user or Spammer and stop_queue == 1 - display message and exit.
             $ret_val['stop_queue'] = 1;
         }
     }
     return $ret_val;
 }
Пример #6
0
 function SendMessage($ID, $timeout = 0, $maxcount = 0, $check_charset = false)
 {
     global $DB, $APPLICATION;
     $eol = CEvent::GetMailEOL();
     $ID = intval($ID);
     $timeout = intval($timeout);
     $start_time = getmicrotime();
     @set_time_limit(0);
     $this->LAST_ERROR = "";
     $post = $this->GetByID($ID);
     if (!($post_arr = $post->Fetch())) {
         $this->LAST_ERROR .= GetMessage("class_post_err_notfound");
         return false;
     }
     if ($post_arr["STATUS"] != "P") {
         $this->LAST_ERROR .= GetMessage("class_post_err_status") . "<br>";
         return false;
     }
     if ($check_charset && strlen($post_arr["MSG_CHARSET"]) > 0 && strtoupper($post_arr["MSG_CHARSET"]) != strtoupper(LANG_CHARSET)) {
         return "CONTINUE";
     }
     if (CPosting::Lock($ID) === false) {
         if ($e = $APPLICATION->GetException()) {
             $this->LAST_ERROR .= GetMessage("class_post_err_lock") . "<br>" . $e->GetString();
             if (strpos($this->LAST_ERROR, "PLS-00201") !== false && strpos($this->LAST_ERROR, "'DBMS_LOCK'") !== false) {
                 $this->LAST_ERROR .= "<br>" . GetMessage("class_post_err_lock_advice");
             }
             $APPLICATION->ResetException();
             return false;
         } else {
             return "CONTINUE";
         }
     }
     if ($post_arr["VERSION"] != '2') {
         if (is_string($post_arr["BCC_TO_SEND"]) && strlen($post_arr["BCC_TO_SEND"]) > 0) {
             $a = explode(",", $post_arr["BCC_TO_SEND"]);
             foreach ($a as $e) {
                 $DB->Query("INSERT INTO b_posting_email (POSTING_ID, STATUS, EMAIL) VALUES (" . $ID . ", 'Y', '" . $DB->ForSQL($e) . "')");
             }
         }
         if (is_string($post_arr["ERROR_EMAIL"]) && strlen($post_arr["ERROR_EMAIL"]) > 0) {
             $a = explode(",", $post_arr["ERROR_EMAIL"]);
             foreach ($a as $e) {
                 $DB->Query("INSERT INTO b_posting_email (POSTING_ID, STATUS, EMAIL) VALUES (" . $ID . ", 'E', '" . $DB->ForSQL($e) . "')");
             }
         }
         if (is_string($post_arr["SENT_BCC"]) && strlen($post_arr["SENT_BCC"]) > 0) {
             $a = explode(",", $post_arr["SENT_BCC"]);
             foreach ($a as $e) {
                 $DB->Query("INSERT INTO b_posting_email (POSTING_ID, STATUS, EMAIL) VALUES (" . $ID . ", 'N', '" . $DB->ForSQL($e) . "')");
             }
         }
         $DB->Query("UPDATE b_posting SET VERSION='2', BCC_TO_SEND=null, ERROR_EMAIL=null, SENT_BCC=null WHERE ID=" . $ID);
     }
     if (strlen($post_arr["CHARSET"]) > 0) {
         $from_charset = $post_arr["MSG_CHARSET"] ? $post_arr["MSG_CHARSET"] : SITE_CHARSET;
         $post_arr["BODY"] = $APPLICATION->ConvertCharset($post_arr["BODY"], $from_charset, $post_arr["CHARSET"]);
         $post_arr["SUBJECT"] = $APPLICATION->ConvertCharset($post_arr["SUBJECT"], $from_charset, $post_arr["CHARSET"]);
         $post_arr["FROM_FIELD"] = $APPLICATION->ConvertCharset($post_arr["FROM_FIELD"], $from_charset, $post_arr["CHARSET"]);
     }
     //Preparing message header, text, subject
     $sBody = str_replace("\r\n", "\n", $post_arr["BODY"]);
     if (COption::GetOptionString("main", "CONVERT_UNIX_NEWLINE_2_WINDOWS", "N") == "Y") {
         $sBody = str_replace("\n", "\r\n", $sBody);
     }
     if (COption::GetOptionString("subscribe", "allow_8bit_chars") != "Y") {
         $sSubject = CMailTools::EncodeSubject($post_arr["SUBJECT"], $post_arr["CHARSET"]);
         $sFrom = CMailTools::EncodeHeaderFrom($post_arr["FROM_FIELD"], $post_arr["CHARSET"]);
     } else {
         $sSubject = $post_arr["SUBJECT"];
         $sFrom = $post_arr["FROM_FIELD"];
     }
     if ($post_arr["BODY_TYPE"] == "html") {
         //URN2URI
         $tools = new CMailTools();
         $sBody = $tools->ReplaceHrefs($sBody);
     }
     $bHasAttachments = false;
     if ($post_arr["BODY_TYPE"] == "html" && COption::GetOptionString("subscribe", "attach_images") == "Y") {
         //MIME with attachments
         $tools = new CMailTools();
         $sBody = $tools->ReplaceImages($sBody);
         if (count($tools->aMatches) > 0) {
             $bHasAttachments = true;
             $sBoundary = "----------" . uniqid("");
             $sHeader = 'From: ' . $sFrom . $eol . 'X-Bitrix-Posting: ' . $post_arr["ID"] . $eol . 'MIME-Version: 1.0' . $eol . 'Content-Type: multipart/related; boundary="' . $sBoundary . '"' . $eol . 'Content-Transfer-Encoding: 8bit';
             $sBody = "--" . $sBoundary . $eol . "Content-Type: " . ($post_arr["BODY_TYPE"] == "html" ? "text/html" : "text/plain") . ($post_arr["CHARSET"] != "" ? "; charset=" . $post_arr["CHARSET"] : "") . $eol . "Content-Transfer-Encoding: 8bit" . $eol . $eol . $sBody . $eol;
             foreach ($tools->aMatches as $attachment) {
                 $aImage = CFile::GetImageSize($_SERVER["DOCUMENT_ROOT"] . $attachment["SRC"]);
                 if ($aImage === false) {
                     continue;
                 }
                 $filename = $_SERVER["DOCUMENT_ROOT"] . $attachment["SRC"];
                 $handle = fopen($filename, "rb");
                 $file = fread($handle, filesize($filename));
                 fclose($handle);
                 $sBody .= $eol . "--" . $sBoundary . $eol . "Content-Type: " . (function_exists("image_type_to_mime_type") ? image_type_to_mime_type($aImage[2]) : CMailTools::ImageTypeToMimeType($aImage[2])) . "; name=\"" . $attachment["DEST"] . "\"" . $eol . "Content-Transfer-Encoding: base64" . $eol . "Content-ID: <" . $attachment["ID"] . ">" . $eol . $eol . chunk_split(base64_encode($file), 72, $eol);
             }
         }
     }
     $rsFile = CPosting::GetFileList($ID);
     $arFile = $rsFile->Fetch();
     if ($arFile) {
         if (!$bHasAttachments) {
             $bHasAttachments = true;
             $sBoundary = "----------" . uniqid("");
             $sHeader = "From: " . $sFrom . $eol . 'X-Bitrix-Posting: ' . $post_arr["ID"] . $eol . "MIME-Version: 1.0" . $eol . "Content-Type: multipart/related; boundary=\"" . $sBoundary . "\"" . $eol . "Content-Transfer-Encoding: 8bit";
             $sBody = "--" . $sBoundary . $eol . "Content-Type: " . ($post_arr["BODY_TYPE"] == "html" ? "text/html" : "text/plain") . ($post_arr["CHARSET"] != "" ? "; charset=" . $post_arr["CHARSET"] : "") . $eol . "Content-Transfer-Encoding: 8bit" . $eol . $eol . $sBody . $eol;
         }
         do {
             if (strlen($post_arr["CHARSET"]) > 0) {
                 $from_charset = $post_arr["MSG_CHARSET"] ? $post_arr["MSG_CHARSET"] : SITE_CHARSET;
                 $file_name = $APPLICATION->ConvertCharset($arFile["ORIGINAL_NAME"], $from_charset, $post_arr["CHARSET"]);
             } else {
                 $file_name = $arFile["ORIGINAL_NAME"];
             }
             $sBody .= $eol . "--" . $sBoundary . $eol . "Content-Type: " . $arFile["CONTENT_TYPE"] . "; name=\"" . $file_name . "\"" . $eol . "Content-Transfer-Encoding: base64" . $eol . "Content-Disposition: attachment; filename=\"" . CMailTools::EncodeHeaderFrom($file_name, $post_arr["CHARSET"]) . "\"" . $eol . $eol;
             $arTempFile = CFile::MakeFileArray($arFile["ID"]);
             $sBody .= chunk_split(base64_encode(file_get_contents($arTempFile["tmp_name"])), 72, $eol);
         } while ($arFile = $rsFile->Fetch());
     }
     if ($bHasAttachments) {
         $sBody .= $eol . "--" . $sBoundary . "--" . $eol;
     } else {
         //plain message without MIME
         $sHeader = "From: " . $sFrom . $eol . 'X-Bitrix-Posting: ' . $post_arr["ID"] . $eol . "MIME-Version: 1.0" . $eol . "Content-Type: " . ($post_arr["BODY_TYPE"] == "html" ? "text/html" : "text/plain") . ($post_arr["CHARSET"] != "" ? "; charset=" . $post_arr["CHARSET"] : "") . $eol . "Content-Transfer-Encoding: 8bit";
     }
     $mail_additional_parameters = trim(COption::GetOptionString("subscribe", "mail_additional_parameters"));
     if ($post_arr["DIRECT_SEND"] == "Y") {
         //personal delivery
         $arEvents = array();
         $rsEvents = GetModuleEvents("subscribe", "BeforePostingSendMail");
         while ($arEvent = $rsEvents->Fetch()) {
             $arEvents[] = $arEvent;
         }
         $rsEmails = $DB->Query($DB->TopSql("\n\t\t\t\tSELECT *\n\t\t\t\tFROM b_posting_email\n\t\t\t\tWHERE POSTING_ID = " . $ID . " AND STATUS='Y'\n\t\t\t", $maxcount));
         while ($arEmail = $rsEmails->Fetch()) {
             //Event part
             $arFields = array("POSTING_ID" => $ID, "EMAIL" => $arEmail["EMAIL"], "SUBJECT" => $sSubject, "BODY" => $sBody, "HEADER" => $sHeader, "EMAIL_EX" => $arEmail);
             foreach ($arEvents as $arEvent) {
                 $arFields = ExecuteModuleEventEx($arEvent, array($arFields));
             }
             //Sending
             if (is_array($arFields)) {
                 $result = bxmail($arFields["EMAIL"], $arFields["SUBJECT"], $arFields["BODY"], $arFields["HEADER"], $mail_additional_parameters);
             } else {
                 $result = $arFields !== false;
             }
             //Result check and iteration
             if ($result) {
                 $DB->Query("UPDATE b_posting_email SET STATUS='N' WHERE ID = " . $arEmail["ID"]);
             } else {
                 $DB->Query("UPDATE b_posting_email SET STATUS='E' WHERE ID = " . $arEmail["ID"]);
             }
             if ($timeout > 0 && getmicrotime() - $start_time >= $timeout) {
                 break;
             }
             self::$current_emails_per_hit++;
         }
     } else {
         //BCC delivery
         $rsEmails = $DB->Query($DB->TopSql("\n\t\t\t\tSELECT *\n\t\t\t\tFROM b_posting_email\n\t\t\t\tWHERE POSTING_ID = " . $ID . " AND STATUS='Y'\n\t\t\t", COption::GetOptionString("subscribe", "max_bcc_count")));
         $aStep = array();
         while ($arEmail = $rsEmails->Fetch()) {
             $aStep[$arEmail["ID"]] = $arEmail["EMAIL"];
         }
         if (count($aStep) > 0) {
             $BCC = implode(",", $aStep);
             $sHeaderStep = $sHeader . $eol . "Bcc: " . $BCC;
             $result = bxmail($post_arr["TO_FIELD"], $sSubject, $sBody, $sHeaderStep, $mail_additional_parameters);
             if ($result) {
                 $DB->Query("UPDATE b_posting_email SET STATUS='N' WHERE ID in (" . implode(", ", array_keys($aStep)) . ")");
             } else {
                 $DB->Query("UPDATE b_posting_email SET STATUS='E' WHERE ID in (" . implode(", ", array_keys($aStep)) . ")");
                 $this->LAST_ERROR .= GetMessage("class_post_err_mail") . "<br>";
             }
         }
     }
     //set status and delivered and error emails
     $arStatuses = $this->GetEmailStatuses($ID);
     if (!array_key_exists("Y", $arStatuses)) {
         $STATUS = array_key_exists("E", $arStatuses) ? "E" : "S";
         $DATE = $DB->GetNowFunction();
     } else {
         $STATUS = "P";
         $DATE = "null";
     }
     CPosting::UnLock($ID);
     $DB->Query("UPDATE b_posting SET STATUS='" . $STATUS . "', DATE_SENT=" . $DATE . " WHERE ID=" . $ID);
     return $STATUS == "P" ? "CONTINUE" : true;
 }
Пример #7
0
 /**
  * @param $mailParams
  * @return bool
  */
 public static function send($mailParams)
 {
     $result = false;
     $event = new \Bitrix\Main\Event("main", "OnBeforeMailSend", array($mailParams));
     $event->send();
     foreach ($event->getResults() as $eventResult) {
         if ($eventResult->getType() == \Bitrix\Main\EventResult::ERROR) {
             return false;
         }
         $mailParams = array_merge($mailParams, $eventResult->getParameters());
     }
     if (defined("ONLY_EMAIL") && $mailParams['TO'] != ONLY_EMAIL) {
         $result = true;
     } else {
         $mail = static::createInstance($mailParams);
         $mailResult = bxmail($mail->getTo(), $mail->getSubject(), $mail->getBody(), $mail->getHeaders(), $mail->getAdditionalParameters());
         if ($mailResult) {
             $result = true;
         }
     }
     return $result;
 }
Пример #8
0
 /**
  * Send error message to the admin email
  *
  * @param \Exception $exception
  */
 protected function sendNotifyException($exception)
 {
     $adminEmail = Main\Config\Option::get('main', 'email_from');
     $logFile = Application::getDocumentRoot() . $this->__path . '/' . $this->exceptionLog;
     if (!is_file($logFile) && $adminEmail) {
         $date = date('Y-m-d H:m:s');
         bxmail($adminEmail, Loc::getMessage('BBC_COMPONENT_EXCEPTION_EMAIL_SUBJECT', array('#SITE_URL#' => SITE_SERVER_NAME)), Loc::getMessage('BBC_COMPONENT_EXCEPTION_EMAIL_TEXT', array('#URL#' => 'http://' . SITE_SERVER_NAME . Main\Context::getCurrent()->getRequest()->getRequestedPage(), '#DATE#' => $date, '#EXCEPTION_MESSAGE#' => $exception->getMessage(), '#EXCEPTION#' => $exception)), 'Content-Type: text/html; charset=utf-8');
         $log = fopen($logFile, 'w');
         fwrite($log, '[' . $date . '] Catch exception: ' . PHP_EOL . $exception);
         fclose($log);
     }
 }