Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/goodde.yandexturboapi/lib/ |
Upload File : |
<? namespace Goodde\YandexTurbo; use Bitrix\Main, Bitrix\Main\Type, Bitrix\Main\Loader, Bitrix\Main\IO, Bitrix\Main\SiteTable, Bitrix\Main\Application, Bitrix\Main\Config\Option, Bitrix\Main\Web\HttpClient, Bitrix\Main\SystemException, Bitrix\Main\Localization\Loc, Goodde\YandexTurbo\TurboStaticFileTable; Loc::loadMessages(__FILE__); require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/goodde.yandexturboapi/admin/tools.php"); class TurboStaticFileExport extends \Goodde\Export\ProfileExport { protected $httpTimeout = 10; protected $arTurboYandexFile = array(); public function __construct($element = 0, $parameters = array()) { parent::__construct($element, $parameters); if(!$this->loadFeed()) { $this->arErrors[] = $GLOBALS['APPLICATION']->GetException(); return false; } $this->setDefaultParams(); $this->content = ''; $this->arTypes = Loc::getMessage('GOODDE_TYRBO_API_YML_TURBO'); } public function loadFeed() { global $APPLICATION; if ($this->feedId <= 0) { ShowError(Loc::getMessage("GOODDE_TYRBO_API_ERROR_FEED_ID")); $APPLICATION->ThrowException(Loc::getMessage("GOODDE_TYRBO_API_ERROR_FEED_ID")); return false; } if (!Loader::includeModule('seo')) { ShowError(Loc::getMessage("GOODDE_TYRBO_API_ERROR_SEO")); $APPLICATION->ThrowException(Loc::getMessage("GOODDE_TYRBO_API_ERROR_SEO")); return false; } if ($this->feed = TurboStaticFileTable::getById($this->feedId)->fetch()){ } else{ ShowError(Loc::getMessage("GOODDE_TYRBO_API_ERROR_NOT_FEED")); $APPLICATION->ThrowException(Loc::getMessage("GOODDE_TYRBO_API_ERROR_NOT_FEED")); return false; } if ($this->feed['ACTIVE'] != 'Y') { ShowError(Loc::getMessage("GOODDE_TYRBO_API_ERROR_NOT_FEED")); $APPLICATION->ThrowException(Loc::getMessage("GOODDE_TYRBO_API_ERROR_NOT_FEED")); return false; } return true; } protected function setDefaultParams() { /*bind events*/ foreach(GetModuleEvents("goodde.yandexturboapi", "OnBeforeStaticFileProfileExport", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array($this->feed['ID'], &$this->feed)); } $this->feed['SITE_ID'] = $this->feed['LID']; $this->feed['SERVER_ADDRESS'] = trim($this->feed['SERVER_ADDRESS']); $this->feed['SERVER_ADDRESS'] = rtrim($this->feed['SERVER_ADDRESS'], '/'); $this->feed['HOST'] = $this->feed['SERVER_ADDRESS']; $this->feed['ENCODING'] = SITE_CHARSET; $this->feed['STATUS'] = $this->feed['ITEM_STATUS'] == 'Y' ? 'false' : 'true'; $this->feed['MODE'] = $this->feed['IN_DEBUG'] == 'Y' ? 'debug' : 'production'; $this->feed['SETTINGS_RES'] = $this->prepareFilesData(); $this->feed['FILE_PATH'] = str_replace('#id#', $this->feed['ID'], \Goodde\YandexTurbo\TurboStaticFileTable::EXPORT_FILE_PATH); if($this->parameters['TMP_FILE_PATH']) { $this->feed['TMP_FILE_PATH'] = $this->parameters['TMP_FILE_PATH']; } else { $this->feed['TMP_FILE_PATH'] = $this->setTempBuffer(); } $this->feed['LANGUAGE_ID'] = LANGUAGE_ID; $this->feed['IS_EXTENDED_HTMLTAGS'] = $this->feed['SETTINGS']['IS_EXTENDED_HTMLTAGS'] == 'Y' ? 'Y' : 'N'; $this->feed['FIELDS']['IS_EXTENDED_HTML'] = $this->feed['SETTINGS']['IS_EXTENDED_HTML'] == 'Y' ? 'Y' : 'N'; $this->feed['IS_EXTENDED_HTML'] = $this->feed['SETTINGS']['IS_EXTENDED_HTML'] == 'Y' ? 'true' : 'false'; } protected function prepareFilesData() { $arSettingsRes = array(); $bRootChecked = isset($this->feed['SETTINGS']['DIR']['/']) && $this->feed['SETTINGS']['DIR']['/'] == 'Y'; $arSettingsRes[] = array( 'ITEM_TYPE' => 'D', 'ITEM_PATH' => '/', 'ACTIVE' => $bRootChecked ? 'Y' : 'N', ); unset($this->feed['SETTINGS']['DIR']['/']); foreach($this->feed['SETTINGS']['DIR'] as $k => $v) { if($v == 'Y') { $arSettingsRes[] = array( 'ITEM_TYPE' => 'D', 'ITEM_PATH' => $k, 'ACTIVE' => 'Y', ); } } return $arSettingsRes; } protected function getFileContent($url) { global $APPLICATION; $http = new HttpClient(array( "socketTimeout" => $this->httpTimeout, "disableSslVerification" => true )); $http->setHeader('User-Agent', $_SERVER['HTTP_USER_AGENT']); $result = $http->get($url.'?goodde_yandex_turbo=Y'); try { if($http->getStatus() != 200) { return false; } return $result; } catch(\Bitrix\Main\ArgumentException $e) { $APPLICATION->ThrowException($e->getMessage(), ''); return false; } return false; } public function getFilesData($arCurrentDir) { if($arCurrentDir['ACTIVE'] == 'Y') { $list = \CSeoUtils::getDirStructure(true, $this->feed['SITE_ID'], $arCurrentDir['ITEM_PATH']); foreach($list as $dir) { $dirKey = "/".ltrim($dir['DATA']['ABS_PATH'], "/"); if($dir['TYPE'] == 'F') { if(!isset($this->feed['SETTINGS']['FILE'][$dirKey]) || $this->feed['SETTINGS']['FILE'][$dirKey] == 'Y') { if(preg_match($this->feed['SETTINGS']['FILE_MASK_REGEXP'], $dir['FILE'])) { $filesrc = $this->getFileContent($this->feed['HOST'].$dirKey); preg_match_all('#<!--TurboContent-->(.+?)<!--/TurboContent-->#isU', $filesrc, $arContent); $content = implode($arContent[1]); $turboContent = $this->prepareTurboContent($content); if(is_string($turboContent) && $turboContent <> '') { $link = str_replace('index.php', '', $this->feed['HOST'].$dirKey); $description = $this->getPageDescription($filesrc); $pageTitle = $this->fullTextFormatting($this->getPageTitle($filesrc)); $title = $this->fullTextFormatting($dir['DATA']['LOGIC_NAME']); $this->arTurboYandexFile[$dirKey] = array( 'link' => $link, 'FIELDS' => array( 'status' => $this->feed['STATUS'], 'link' => $link, 'title' => $title, 'h1' => $pageTitle ? $pageTitle : $title, 'description' => $this->fullTextFormatting($description), 'pubDate' => date("r", $dir['DATA']['TIMESTAMP']), 'turbo_content' => $turboContent, ), ); } } } } } } else { $len = mb_strlen($arCurrentDir['ITEM_PATH']); if(!empty($this->feed['SETTINGS']['FILE'])) { foreach($this->feed['SETTINGS']['FILE'] as $dirKey => $checked) { if($checked == 'Y') { if(strncmp($arCurrentDir['ITEM_PATH'], $dirKey, $len) === 0) { $fileName = IO\Path::combine( SiteTable::getDocumentRoot($this->feed['SITE_ID']), $dirKey ); if(!is_dir($fileName)) { $f = new IO\File($fileName, $this->feed['SITE_ID']); if($f->isExists() && !$f->isSystem() && preg_match($this->feed['SETTINGS']['FILE_MASK_REGEXP'], $f->getName()) ) { if($this->arTurboYandexFile[$dirKey]['link'] == $dirKey) continue; $filesrc = $this->getFileContent($this->feed['HOST'].$dirKey); preg_match_all('#<!--TurboContent-->(.+?)<!--/TurboContent-->#isU', $filesrc, $arContent); $content = implode($arContent[1]); $turboContent = $this->prepareTurboContent($content); if(is_string($turboContent) && $turboContent <> '') { $title = \PHPParser::getPageTitle($filesrc); $title = $this->fullTextFormatting($title); $pageTitle = $this->fullTextFormatting($this->getPageTitle($filesrc)); $description = $this->getPageDescription($filesrc); $link = str_replace('index.php', '', $this->feed['HOST'].$dirKey); $this->arTurboYandexFile[$dirKey] = array( 'link' => $link, 'FIELDS' => array( 'status' => $this->feed['STATUS'], 'link' => $link, 'title' => $title, 'h1' => $pageTitle ? $pageTitle : $title, 'description' => $this->fullTextFormatting($description), 'pubDate' => date("r", filemtime($fileName)), 'turbo_content' => $turboContent, ), ); } } } } } } } } } protected function getPageDescription($filesrc) { if(!$description && preg_match('/<meta(?=[^>]* name *= *"?description"?) [^>]*?(?<= )content *= *"([^"]*)"[^>]*>/i', $filesrc, $regs)) $description = $regs[1]; return $description; } protected function getPageTitle($filesrc) { if(!$pageTitle && preg_match('!<h1>(.*)<\/h1>!Ui', $filesrc, $regs)) $pageTitle = $regs[1]; return $pageTitle; } public function execute($parameters = array()) { $arResult = $this->writeOffers(); return $arResult; } protected function getItems() { $offersCnt = 0; if($this->feed['SETTINGS_RES']) { foreach($this->feed['SETTINGS_RES'] as $arRes) { $offersCnt++; $this->getFilesData($arRes); } } $this->exportResult = array( 'LAST_OFFERS_COUNT' => intval($offersCnt), ); return $this->arTurboYandexFile; } public function writeHeader() { $content = $this->arTypes['XML_HEADER']; if($this->feed && $this->arTypes) { foreach($this->feed as $key => $val) { if($val && is_array($val)) continue; if($val) { $content = str_replace('#' . $key . '#', $val, $content); } } } unset($key, $val); $content = preg_replace('/\s*\r+/' . BX_UTF_PCRE_MODIFIER, "", $content); $this->content = $content; unset($content); $this->pushTempBuffer(); } public function writeOffers() { $arOffers = $this->getItems(); foreach($arOffers as $arOffer) { $content = $this->arTypes['XML_OFFER']; foreach($arOffer['FIELDS'] as $key => $val) { if($val <> '') { $content = str_replace('#' . $key . '#', $val, $content); } else { $content = str_replace('#' . $key . '#', '', $content); } } $content = preg_replace('/\s*\r+/' . BX_UTF_PCRE_MODIFIER, "", $content); $this->content .= "\n\t" . $content; } $this->content .= "\n"; unset($arOffers, $arOffer, $content, $key, $val); $this->pushTempBuffer(); return $this->exportResult; } public function uploadFileFeed() { global $APPLICATION; $runError = ''; $file = new \Bitrix\Main\IO\File($this->getTempBuffer()); if($file->isExists()) { $data = $file->getContents(); /*bind events*/ foreach(GetModuleEvents("goodde.yandexturboapi", "OnBeforeFileContentAdd", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array($this->feed, &$data)); } if($this->isGzip) { $data = gzencode($data, $this->gzipLevel); } $result = \Goodde\YandexTurbo\Model\Request::addFeed($this->feed['LID'], $this->feed['MODE'], $data, $this->isGzip); if(is_array($result) && isset($result['task_id'])) { $resultTask = \Goodde\YandexTurbo\TaskTable::add(array('FEED_ID' => $this->feed['ID'], 'LID' => $this->feed['LID'], 'TASK_ID' => $result['task_id'], 'MODE' => ToUpper($this->feed['MODE']))); if($resultTask->isSuccess()) { $taskId = $resultTask->getId(); $newName = 'goodde_static_file_export_'.$taskId.'.xml'; if(rename($file->getPhysicalPath(), $this->getPath().'/reports/'.$newName)) { if($this->isGzip) { $fileName = $this->addArchive($this->getPath().'/reports/'.$newName, $data); if($fileName <> '') { $newName = $fileName; } unset($fileName); } \Goodde\YandexTurbo\TaskTable::update($taskId, array('NAME' => $newName)); unset($newName); } } } else { $APPLICATION->ThrowException($result['error_code']. ' - '.$result['error_message']); return false; } } else { $runError = $APPLICATION->ThrowException(str_replace('#FILE#', $path, Loc::getMessage('GOODDE_TYRBO_API_FILE_OPEN_WRITING'))); return false; } return true; } } ?>