2016-08-25 15:26:37 +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) Proud Sourcing GmbH | 2016
|
|
|
|
* @link www.proudcommerce.com
|
|
|
|
* @package psCacheWarmer
|
2016-10-12 14:40:41 +02:00
|
|
|
* @version 1.0.1
|
2016-08-25 15:26:37 +02:00
|
|
|
**/
|
2019-06-26 20:44:36 +02:00
|
|
|
|
|
|
|
namespace ProudCommerce\CacheWarmer\Application\Controller;
|
|
|
|
|
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
|
|
|
use OxidEsales\Eshop\Core\Controller\BaseController;
|
|
|
|
|
|
|
|
class CacheWarmer extends BaseController
|
2016-08-25 15:26:37 +02:00
|
|
|
{
|
2019-07-17 17:17:28 +02:00
|
|
|
protected $_sFileAndPathToExportFile = '';
|
|
|
|
protected $_handle = null;
|
|
|
|
protected $_sSeparator = ";";
|
|
|
|
protected $_sEnclosure = "'";
|
|
|
|
|
|
|
|
// todo: add to config
|
|
|
|
protected $_aHttpCodesIsOkay = array('200','302');
|
2016-08-25 15:26:37 +02:00
|
|
|
/**
|
|
|
|
* Executes cache warmer
|
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
$sMessage = "<b>psCacheWarmer</b><br>".$this->_getSitemapUrl()."<br>---<br>";
|
|
|
|
|
2019-07-17 17:17:28 +02:00
|
|
|
$this->_sFileAndPathToExportFile = $this->_getPathWithFileName();
|
|
|
|
if(Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsv') == true)
|
|
|
|
{
|
|
|
|
$this->_handle = fopen( $this->_sFileAndPathToExportFile, "w+");;
|
|
|
|
}
|
|
|
|
|
2016-08-25 15:26:37 +02:00
|
|
|
if($this->_checkAuthentification()) {
|
|
|
|
$aUrls = $this->_getSitemapContent();
|
2019-06-26 20:44:36 +02:00
|
|
|
if(!empty(Registry::getConfig()->getShopConfVar('psCacheWarmerSitemapUrl')) && count($aUrls) > 0) {
|
2016-08-25 15:26:37 +02:00
|
|
|
foreach($aUrls as $sUrl) {
|
2019-07-17 17:17:28 +02:00
|
|
|
$oCurl = $this->_runCurlConnect($sUrl);
|
|
|
|
$sMessage .= $this->_checkCurlResults($oCurl,$sUrl);
|
2016-08-25 15:26:37 +02:00
|
|
|
curl_close($oCurl);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$sMessage .= '<span style="color: red;">Keine Daten vorhanden!</span>';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$sMessage .= '<span style="color: red;">Authentifizierung fehlgeschlagen!</span>';
|
|
|
|
}
|
|
|
|
|
2019-07-17 17:17:28 +02:00
|
|
|
if(Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsv') == true)
|
|
|
|
{
|
|
|
|
fclose($this->_handle);
|
|
|
|
}
|
2016-08-25 15:26:37 +02:00
|
|
|
echo '<pre>'.$sMessage.'</pre>';
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2019-07-17 17:17:28 +02:00
|
|
|
/**
|
|
|
|
* @param $sUrl
|
|
|
|
* @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);
|
|
|
|
$sUsername = Registry::getConfig()->getShopConfVar('psCacheWarmerUser');
|
|
|
|
$sPassword = Registry::getConfig()->getShopConfVar('psCacheWarmerPass');
|
|
|
|
curl_setopt($oCurl, CURLOPT_USERPWD, $sUsername . ":" . $sPassword);
|
|
|
|
curl_exec($oCurl);
|
|
|
|
return $oCurl;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _checkCurlResults($oCurl,$sUrl)
|
|
|
|
{
|
|
|
|
$sMessage = '';
|
|
|
|
$httpStatus = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
|
|
|
|
if(curl_error($oCurl)) {
|
|
|
|
$sMessage .= '<span style="color: orange;">ERROR '.$httpStatus.': ' . curl_error($oCurl) . '</span><br>';
|
|
|
|
$sStatusMsg = 'ERROR';
|
|
|
|
$sTmpText = curl_error($oCurl);
|
|
|
|
} else {
|
|
|
|
$sTmpText = $sUrl;
|
|
|
|
if(in_array(trim($httpStatus),$this->_aHttpCodesIsOkay))
|
|
|
|
{
|
|
|
|
$sMessage .= '<span style="color: green;">OK '.$httpStatus.': ' . $sUrl . '</span><br>';
|
|
|
|
$sStatusMsg = 'OK';
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$sMessage .= '<span style="color: red;">ERROR <b>'.$httpStatus.'</b>: ' . $sUrl. '</span><br>';
|
|
|
|
$sStatusMsg = 'ERROR';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsv') == true)
|
|
|
|
{
|
|
|
|
$aTmp = array($sStatusMsg,
|
|
|
|
$httpStatus,
|
|
|
|
$sTmpText
|
|
|
|
);
|
|
|
|
|
|
|
|
if(trim($httpStatus) == '200' && Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsvOnlyError') == true)
|
|
|
|
{
|
|
|
|
$aTmp = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count($aTmp)) {
|
|
|
|
fputcsv($this->_handle, $aTmp, $this->_sSeparator, $this->_sEnclosure);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sMessage;
|
|
|
|
}
|
|
|
|
|
2016-08-25 15:26:37 +02:00
|
|
|
/**
|
|
|
|
* Returens urls from sitemap
|
|
|
|
*
|
|
|
|
* @return array urls
|
|
|
|
*/
|
|
|
|
protected function _getSitemapContent($sSitemapUrl = "")
|
|
|
|
{
|
|
|
|
$aUrls = array();
|
|
|
|
if(empty($sSitemapUrl)) {
|
|
|
|
$sSitemapUrl = $this->_getSitemapUrl();
|
|
|
|
}
|
|
|
|
|
2019-06-26 20:44:36 +02:00
|
|
|
$sUsername = Registry::getConfig()->getShopConfVar('psCacheWarmerUser');
|
|
|
|
$sPassword = Registry::getConfig()->getShopConfVar('psCacheWarmerPass');
|
2016-10-12 14:40:41 +02:00
|
|
|
$sSitemapUrl = str_replace("://", "://".$sUsername.":".$sPassword."@", $sSitemapUrl);
|
|
|
|
|
2016-08-25 15:26:37 +02:00
|
|
|
$sSitemapXmlData = @file_get_contents($sSitemapUrl);
|
2016-10-12 14:40:41 +02:00
|
|
|
if($oSitemap = @simplexml_load_string($sSitemapXmlData)) {
|
2016-08-25 15:26:37 +02:00
|
|
|
if (count($oSitemap->sitemap) > 0) {
|
|
|
|
foreach ($oSitemap->sitemap as $oSubSitemap) {
|
|
|
|
$sNextSitemapUrl = (string)$oSubSitemap->loc;
|
|
|
|
$aUrls = array_merge($aUrls, $this->_getSitemapContent($sNextSitemapUrl));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count($oSitemap->url) > 0) {
|
|
|
|
foreach($oSitemap->url as $oSitemapUrl) {
|
|
|
|
$aUrls[] = (string)$oSitemapUrl->loc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#print_r($aUrls);
|
|
|
|
return $aUrls;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returens sitemap url
|
|
|
|
*
|
|
|
|
* @return string sitemap url
|
|
|
|
*/
|
|
|
|
protected function _getSitemapUrl()
|
|
|
|
{
|
2019-06-26 20:44:36 +02:00
|
|
|
$sSitemapUrl = Registry::getConfig()->getConfigParam('sShopURL');
|
|
|
|
$sSitemapUrl .= Registry::getConfig()->getShopConfVar('psCacheWarmerSitemapUrl');
|
2016-08-25 15:26:37 +02:00
|
|
|
return $sSitemapUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks authentification
|
|
|
|
*
|
|
|
|
* @return bool true|false
|
|
|
|
*/
|
|
|
|
protected function _checkAuthentification()
|
|
|
|
{
|
2019-06-26 20:44:36 +02:00
|
|
|
$oConfig = Registry::getConfig();
|
|
|
|
$sKey = Registry::getConfig()->getRequestParameter("key");
|
2016-08-25 15:26:37 +02:00
|
|
|
$sSavedKey = $oConfig->getShopConfVar('psCacheWarmerKey', $oConfig->getShopId());
|
|
|
|
if($sSavedKey == $sKey) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-17 17:17:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return Path with Filename, from /log
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function _getPathWithFileName()
|
|
|
|
{
|
|
|
|
return Registry::getConfig()->getConfigParam('sShopDir').'/log/'.$this->_getFileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return Filename, Formae psCacheWarmerReport_20190717-122345.csv
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function _getFileName()
|
|
|
|
{
|
|
|
|
return 'psCacheWarmerReport_'.date("Ymd-His").".csv";
|
|
|
|
}
|
|
|
|
|
2016-08-25 15:26:37 +02:00
|
|
|
}
|