psCacheWarmer/Core/CacheWarmer.php

153 lignes
4.7 KiB
PHP
Brut Lien permanent Vue normale Historique

2020-08-17 12:38:19 +02:00
<?php
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @copyright (c) ProudCommerce | 2020
2020-09-14 17:14:07 +02:00
* @link www.proudcommerce.com
* @package psCacheWarmer
2020-09-15 11:23:10 +02:00
* @version 3.1.1
2020-08-17 12:38:19 +02:00
**/
namespace ProudCommerce\CacheWarmer\Core;
use OxidEsales\Eshop\Core\Registry;
/**
* Class CacheWarmer
2020-09-14 17:14:07 +02:00
*
2020-08-17 12:38:19 +02:00
* @package ProudCommerce\CacheWarmer\Core
*/
2020-08-17 15:13:10 +02:00
class CacheWarmer
2020-08-17 12:38:19 +02:00
{
2021-03-17 15:10:26 +01:00
private string $sCliSiteMapFile = '';
2020-08-17 12:38:19 +02:00
/**
2020-08-17 15:13:10 +02:00
*
2020-08-17 12:38:19 +02:00
*/
2021-03-17 15:10:26 +01:00
public function run($sFileSitemap)
2020-08-17 12:38:19 +02:00
{
2021-03-17 15:10:26 +01:00
$this->sCliSiteMapFile = trim($sFileSitemap);
2020-08-17 12:38:19 +02:00
$aUrls = $this->_getSitemapContent();
2021-03-17 15:10:26 +01:00
if ((!empty(Registry::getConfig()->getShopConfVar('psCacheWarmerSitemapUrl'))
|| !empty($sFileSitemap))
&& !empty($aUrls)) {
2020-08-17 12:38:19 +02:00
foreach ($aUrls as $sUrl) {
$oCurl = $this->_runCurlConnect($sUrl);
2020-08-17 15:13:10 +02:00
$this->_checkCurlResults($oCurl, $sUrl);
2020-08-17 12:38:19 +02:00
curl_close($oCurl);
}
}
}
/**
* @param $sUrl
2020-09-14 17:14:07 +02:00
*
2020-08-17 12:38:19 +02:00
* @return false|resource
*/
protected function _runCurlConnect($sUrl)
{
$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, 25);
curl_setopt($oCurl, CURLOPT_HEADER, true);
2021-04-23 10:03:26 +02:00
curl_setopt($oCurl, CURLOPT_USERAGENT, 'CacheWarmer');
2020-08-17 12:38:19 +02:00
$sUsername = Registry::getConfig()->getShopConfVar('psCacheWarmerUser');
$sPassword = Registry::getConfig()->getShopConfVar('psCacheWarmerPass');
curl_setopt($oCurl, CURLOPT_USERPWD, $sUsername . ":" . $sPassword);
curl_exec($oCurl);
2020-09-14 17:14:07 +02:00
2020-08-17 12:38:19 +02:00
return $oCurl;
}
/**
* @param $oCurl
* @param $sUrl
*/
protected function _checkCurlResults($oCurl, $sUrl)
{
$httpStatus = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
if (curl_error($oCurl)) {
$sStatusMsg = 'ERROR';
2020-08-17 15:13:10 +02:00
$sMessage = curl_error($oCurl);
2020-08-17 12:38:19 +02:00
} else {
2020-08-17 15:13:10 +02:00
$sMessage = $sUrl;
if (in_array(trim($httpStatus), Registry::getConfig()->getShopConfVar('psCacheWarmerHttpCodes'))) {
2020-08-17 12:38:19 +02:00
$sStatusMsg = 'OK';
} else {
$sStatusMsg = 'ERROR';
}
}
2020-08-17 15:13:10 +02:00
$httpStatus = trim($httpStatus);
$aLog = [$sStatusMsg, $httpStatus, $sMessage];
print_r($aLog);
2020-08-17 12:38:19 +02:00
2020-08-17 15:13:10 +02:00
if (!empty($aLog) && ((Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsvOnlyError') == true && $httpStatus != '200') || Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsv') == true)) {
2020-09-14 17:14:07 +02:00
$logger = Logging::getLogger('psCacheWarmer', Registry::getConfig()->getLogsDir() . 'pscachewarmer_' . date("dmY_His") . '.log');
$logger->info(implode(' | ', $aLog) . "\r");
2020-08-17 12:38:19 +02:00
}
}
/**
* @param string $sSitemapUrl
2020-09-14 17:14:07 +02:00
*
2020-08-17 12:38:19 +02:00
* @return array
*/
protected function _getSitemapContent($sSitemapUrl = "")
{
$aUrls = [];
2021-03-17 15:10:26 +01:00
if (empty($sSitemapUrl) && $this->sCliSiteMapFile == '')
{
$sSitemapUrl = $this->_getSitemapUrl($sSitemapUrl);
}
elseif (empty($sSitemapUrl) && $this->sCliSiteMapFile != '')
{
$sSitemapUrl = $this->_getSitemapUrl($this->sCliSiteMapFile);
2020-08-17 12:38:19 +02:00
}
$sUsername = Registry::getConfig()->getShopConfVar('psCacheWarmerUser');
$sPassword = Registry::getConfig()->getShopConfVar('psCacheWarmerPass');
$sSitemapUrl = str_replace("://", "://" . $sUsername . ":" . $sPassword . "@", $sSitemapUrl);
$sSitemapXmlData = @file_get_contents($sSitemapUrl);
if ($oSitemap = @simplexml_load_string($sSitemapXmlData)) {
if (count($oSitemap->sitemap) > 0) {
foreach ($oSitemap->sitemap as $oSubSitemap) {
2020-09-14 17:14:07 +02:00
$sNextSitemapUrl = (string) $oSubSitemap->loc;
2020-08-17 12:38:19 +02:00
$aUrls = array_merge($aUrls, $this->_getSitemapContent($sNextSitemapUrl));
}
}
if (count($oSitemap->url) > 0) {
foreach ($oSitemap->url as $oSitemapUrl) {
2020-09-14 17:14:07 +02:00
$aUrls[] = (string) $oSitemapUrl->loc;
2020-08-17 12:38:19 +02:00
}
}
}
2020-09-14 17:14:07 +02:00
2020-08-17 12:38:19 +02:00
return $aUrls;
}
/**
* @return string
*/
2021-03-17 15:10:26 +01:00
protected function _getSitemapUrl($sSitemapFile)
2020-08-17 12:38:19 +02:00
{
2020-09-15 11:23:10 +02:00
$sSitemapUrl = Registry::getConfig()->getShopURL();
2021-03-17 15:10:26 +01:00
if($sSitemapFile != '')
{
$sSitemapUrl .= $sSitemapFile;
}
else{
$sSitemapUrl .= Registry::getConfig()->getShopConfVar('psCacheWarmerSitemapUrl');
}
2020-09-14 17:14:07 +02:00
2020-08-17 12:38:19 +02:00
return $sSitemapUrl;
}
}