function Request($server, $page, $port, $params, $uri = false) { if ($uri && strlen($uri) > 0) { $strURI = $uri; } else { $strURI = "http://" . $server . (strlen($port) > 0 && intval($port) > 0 ? ":" . intval($port) : "") . (strlen($page) ? $page : "/") . (strlen($params) > 0 ? "?" . $params : ""); } $http = new \Bitrix\Main\Web\HttpClient(array("version" => "1.0", "socketTimeout" => 30, "streamTimeout" => 30, "redirect" => true, "redirectMax" => 5)); $strData = $http->get($strURI); $errors = $http->getError(); $arRSSResult = array(); if (!$strData && !empty($errors)) { $strError = ""; foreach ($errors as $errorCode => $errMes) { $strError .= $errorCode . ": " . $errMes; } \CEventLog::Add(array("SEVERITY" => "ERROR", "AUDIT_TYPE_ID" => "XDIMPORT_HTTP", "MODULE_ID" => "xdimport", "ITEM_ID" => "RSS_REQUEST", "DESCRIPTION" => $strError)); } if ($strData) { $rss_charset = "windows-1251"; if (preg_match("/<" . "\\?XML[^>]{1,}encoding=[\"']([^>\"']{1,})[\"'][^>]{0,}\\?" . ">/i", $strData, $matches)) { $rss_charset = Trim($matches[1]); } $strData = preg_replace("/<" . "\\?XML.*?\\?" . ">/i", "", $strData); $strData = $GLOBALS["APPLICATION"]->ConvertCharset($strData, $rss_charset, SITE_CHARSET); } if (strlen($strData) > 0) { require_once $_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/classes/general/xml.php"; $objXML = new CDataXML(); $res = $objXML->LoadString($strData); if ($res !== false) { $ar = $objXML->GetArray(); if (is_array($ar) && isset($ar["rss"]) && is_array($ar["rss"]) && isset($ar["rss"]["#"]) && is_array($ar["rss"]["#"]) && isset($ar["rss"]["#"]["channel"]) && is_array($ar["rss"]["#"]["channel"]) && isset($ar["rss"]["#"]["channel"][0]) && is_array($ar["rss"]["#"]["channel"][0]) && isset($ar["rss"]["#"]["channel"][0]["#"])) { $arRSSResult = $ar["rss"]["#"]["channel"][0]["#"]; } else { $arRSSResult = array(); } $arRSSResult["rss_charset"] = strtolower(SITE_CHARSET); } } if (is_array($arRSSResult) && !empty($arRSSResult)) { $arRSSResult = CXDILFSchemeRSS::FormatArray($arRSSResult); if (!empty($arRSSResult) && array_key_exists("item", $arRSSResult) && is_array($arRSSResult["item"]) && !empty($arRSSResult["item"])) { $arRSSResult["item"] = array_reverse($arRSSResult["item"]); } } return $arRSSResult; }
function CheckRequest() { global $DB; $result = ""; if (!CModule::IncludeModule("socialnetwork")) { return $result; } $current_time = time(); $time_of_exec = false; $rsScheme = CXDILFScheme::GetList(array(), array("ACTIVE" => "Y", "AUTO" => "Y")); while (($arScheme = $rsScheme->Fetch()) && $time_of_exec === false) { if ($arScheme["LAST_EXECUTED"] == '') { continue; } $last_executed = MakeTimeStamp(ConvertDateTime($arScheme["LAST_EXECUTED"], "DD.MM.YYYY HH:MI:SS"), "DD.MM.YYYY HH:MI:SS"); if ($last_executed <= 0) { continue; } $arEventTmp = CSocNetLogTools::FindLogEventByID($arScheme["EVENT_ID"]); if (array_key_exists("REAL_EVENT_ID", $arEventTmp) && strlen($arEventTmp["REAL_EVENT_ID"]) > 0) { $arScheme["EVENT_ID"] = $arEventTmp["REAL_EVENT_ID"]; } //parse schedule $arDoM = CXDImport::ParseDaysOfMonth($arScheme["DAYS_OF_MONTH"]); $arDoW = CXDImport::ParseDaysOfWeek($arScheme["DAYS_OF_WEEK"]); $arToD = CXDImport::ParseTimesOfDay($arScheme["TIMES_OF_DAY"]); if ($arToD) { sort($arToD, SORT_NUMERIC); } $arSDate = localtime($last_executed); //sdate = truncate(last_execute) $sdate = mktime(0, 0, 0, $arSDate[4] + 1, $arSDate[3], $arSDate[5] + 1900); while ($sdate < $current_time && $time_of_exec === false) { $arSDate = localtime($sdate); if ($arSDate[6] == 0) { $arSDate[6] = 7; } //determine if date is good for execution if ($arDoM) { $flag = array_search($arSDate[3], $arDoM); if ($arDoW) { $flag = array_search($arSDate[6], $arDoW); } } elseif ($arDoW) { $flag = array_search($arSDate[6], $arDoW); } else { $flag = false; } if ($flag !== false && $arToD) { foreach ($arToD as $intToD) { if ($sdate + $intToD > $last_executed && $sdate + $intToD <= $current_time) { $time_of_exec = $sdate + $intToD; break; } } } $sdate = mktime(0, 0, 0, date("m", $sdate), date("d", $sdate) + 1, date("Y", $sdate)); //next day } if ($time_of_exec !== false) { if ($arScheme["TYPE"] == "XML") { $arParams = array(); if (strlen($arScheme["PARAMS"]) > 0) { $arTmp = explode("&", $arScheme["PARAMS"]); if (is_array($arTmp) && count($arTmp) > 0) { foreach ($arTmp as $pair) { list($key, $value) = explode("=", $pair); $arParams[$key] = $value; } } } $arResponse = CXDILFSchemeXML::Request($arScheme["HOST"], $arScheme["PAGE"], $arScheme["PORT"], $arScheme["METHOD"], "http://" . $arScheme["HOST"], $arScheme["LOGIN"], $arScheme["PASSWORD"], $arParams); if ($arResponse && is_array($arResponse)) { if (XDI_DEBUG) { CXDImport::WriteToLog("Successful webservice response, scheme ID: " . $arScheme["ID"], "RXML"); } $entityName = $arScheme["NAME"]; if ($arScheme["EVENT_ID"] == "news") { $rsIBlock = CIBlock::GetList(array("ID" => "ASC"), array("ACTIVE" => "Y", "TYPE" => "news", "ID" => $arScheme["ENTITY_ID"])); if ($arIBlock = $rsIBlock->Fetch()) { $entityName = $arIBlock["NAME"]; } } $arLogParams = array("ENTITY_NAME" => $entityName, "ENTITY_URL" => $arResponse["URL"]); $arSonetFields = array("SITE_ID" => $arScheme["LID"], "ENTITY_TYPE" => $arScheme["ENTITY_TYPE"], "ENTITY_ID" => $arScheme["ENTITY_ID"], "EVENT_ID" => $arScheme["EVENT_ID"], "ENABLE_COMMENTS" => $arScheme["ENABLE_COMMENTS"], "=LOG_DATE" => $GLOBALS["DB"]->CurrentTimeFunction(), "TITLE_TEMPLATE" => $arResponse["TITLE"], "TITLE" => $arResponse["TITLE"], "MESSAGE" => $arResponse["MESSAGE"], "TEXT_MESSAGE" => $arResponse["TEXT_MESSAGE"], "URL" => $arResponse["URL"], "PARAMS" => serialize($arLogParams), "MODULE_ID" => false, "CALLBACK_FUNC" => false); $logID = CSocNetLog::Add($arSonetFields, false); if (intval($logID) > 0) { $arUpdateFields = array("TMP_ID" => $logID, "RATING_TYPE_ID" => "LOG_ENTRY", "RATING_ENTITY_ID" => $logID); CSocNetLog::Update($logID, $arUpdateFields); CXDILFScheme::SetSonetLogRights($logID, $arScheme["ENTITY_TYPE"], $arScheme["ENTITY_ID"], $arScheme["EVENT_ID"]); CSocNetLog::CounterIncrement($logID); if ($arScheme["ENTITY_TYPE"] == SONET_SUBSCRIBE_ENTITY_GROUP && intval($arScheme["ENTITY_ID"]) > 0) { $notify_title_tmp = str_replace(array("\r\n", "\n"), " ", $arScheme["NAME"]); $notify_title = TruncateText($notify_title_tmp, 100); $notify_title_out = TruncateText($notify_title_tmp, 255); $arNotifyParams = array("LOG_ID" => $logID, "GROUP_ID" => intval($arScheme["ENTITY_ID"]), "NOTIFY_MESSAGE" => "", "URL" => "", "MESSAGE" => GetMessage("LFP_CLASS_SCHEME_IM_ADD", array("#title#" => $notify_title)), "MESSAGE_OUT" => GetMessage("LFP_CLASS_SCHEME_IM_ADD", array("#title#" => $notify_title_out)), "EXCLUDE_USERS" => array()); CSocNetSubscription::NotifyGroup($arNotifyParams); } } } else { CXDImport::WriteToLog("ERROR: Incorrect webservice response. Scheme ID: " . $arScheme["ID"] . ", server: " . $arScheme["HOST"] . ", port: " . $arScheme["PORT"] . ", page: " . $arScheme["PAGE"] . ", method: " . $arScheme["METHOD"] . ", params: " . $arScheme["PARAMS"], "RXML"); } } elseif ($arScheme["TYPE"] == "RSS") { $arResponse = CXDILFSchemeRSS::Request($arScheme["HOST"], $arScheme["PAGE"], $arScheme["PORT"], $arScheme["PARAMS"], $arScheme["URI"]); if ($arResponse && is_array($arResponse) && array_key_exists("item", $arResponse) && is_array($arResponse["item"]) && count($arResponse["item"]) > 0) { if (XDI_DEBUG) { CXDImport::WriteToLog("Successful RSS response. Scheme ID: " . $arScheme["ID"], "RRSS"); } foreach ($arResponse["item"] as $arItem) { $checksum = md5(serialize($arItem)); $rsLogEvents = CSocNetLog::GetList(array(), array("SITE_ID" => $arScheme["LID"], "ENTITY_TYPE" => $arScheme["ENTITY_TYPE"], "ENTITY_ID" => $arScheme["ENTITY_ID"], "EVENT_ID" => $arScheme["EVENT_ID"], "EXTERNAL_ID" => $checksum), false, array("nTopCount" => 1)); $arLogEvent = $rsLogEvents->Fetch(); if (!$arLogEvent) { $entityName = $arScheme["NAME"]; if ($arScheme["EVENT_ID"] == "news") { $rsIBlock = CIBlock::GetList(array("ID" => "ASC"), array("ACTIVE" => "Y", "TYPE" => "news", "ID" => $arScheme["ENTITY_ID"])); if ($arIBlock = $rsIBlock->Fetch()) { $entityName = $arIBlock["NAME"]; } } $arLogParams = array("SCHEME_ID" => $arScheme["ID"], "ENTITY_NAME" => $entityName, "ENTITY_URL" => $arResponse["link"]); if (strlen($arItem["pubDate"]) > 0) { $arLogParams["SOURCE_TIMESTAMP"] = strtotime($arItem["pubDate"]); } $description = preg_replace("#^(.*?)([\\s]*<br[\\s]*/>)+[\\s]*[\n]*[\\s]*\$#is", "\\1", $arItem["description"]); $arSonetFields = array("SITE_ID" => $arScheme["LID"], "ENTITY_TYPE" => $arScheme["ENTITY_TYPE"], "ENTITY_ID" => $arScheme["ENTITY_ID"], "EVENT_ID" => $arScheme["EVENT_ID"], "ENABLE_COMMENTS" => $arScheme["ENABLE_COMMENTS"], "TITLE_TEMPLATE" => $arItem["title"], "TITLE" => $arItem["title"], "MESSAGE" => $description, "TEXT_MESSAGE" => "", "URL" => $arItem["link"], "PARAMS" => serialize($arLogParams), "MODULE_ID" => false, "CALLBACK_FUNC" => false, "EXTERNAL_ID" => $checksum, "=LOG_DATE" => $GLOBALS["DB"]->CurrentTimeFunction()); $logID = CSocNetLog::Add($arSonetFields, false); if (intval($logID) > 0) { $arUpdateFields = array("TMP_ID" => $logID, "RATING_TYPE_ID" => "LOG_ENTRY", "RATING_ENTITY_ID" => $logID); CSocNetLog::Update($logID, $arUpdateFields); CXDILFScheme::SetSonetLogRights($logID, $arScheme["ENTITY_TYPE"], $arScheme["ENTITY_ID"], $arScheme["EVENT_ID"]); CSocNetLog::CounterIncrement($logID); if ($arScheme["ENTITY_TYPE"] == SONET_SUBSCRIBE_ENTITY_GROUP && intval($arScheme["ENTITY_ID"]) > 0) { $notify_title_tmp = str_replace(array("\r\n", "\n"), " ", $arScheme["NAME"]); $notify_title = TruncateText($notify_title_tmp, 100); $notify_title_out = TruncateText($notify_title_tmp, 255); $arNotifyParams = array("LOG_ID" => $logID, "GROUP_ID" => intval($arScheme["ENTITY_ID"]), "NOTIFY_MESSAGE" => "", "URL" => "", "MESSAGE" => GetMessage("LFP_CLASS_SCHEME_IM_ADD", array("#title#" => $notify_title)), "MESSAGE_OUT" => GetMessage("LFP_CLASS_SCHEME_IM_ADD", array("#title#" => $notify_title_out)), "EXCLUDE_USERS" => array()); CSocNetSubscription::NotifyGroup($arNotifyParams); } } } elseif (XDI_DEBUG) { CXDImport::WriteToLog("RSS item is already in log. Scheme ID: " . $arScheme["ID"] . ", log ID: " . $arLogEvent["ID"], "RRSS"); } } } else { CXDImport::WriteToLog("ERROR: Incorrect RSS response. Scheme ID: " . $arScheme["ID"] . ", server: " . $arScheme["HOST"] . ", port: " . $arScheme["PORT"] . ", page: " . $arScheme["PAGE"] . ", params: " . $arScheme["PARAMS"], "RRSS"); } } $strSql = "UPDATE b_xdi_lf_scheme SET LAST_EXECUTED=" . $DB->GetNowFunction() . " WHERE ID=" . intval($arScheme["ID"]); $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } $result = "CXDILFScheme::CheckRequest();"; } return $result; }