diff --git a/Application/Controller/CacheWarmer.php b/Application/Controller/CacheWarmer.php index a505a53..c94ebad 100644 --- a/Application/Controller/CacheWarmer.php +++ b/Application/Controller/CacheWarmer.php @@ -18,6 +18,13 @@ use OxidEsales\Eshop\Core\Controller\BaseController; class CacheWarmer extends BaseController { + protected $_sFileAndPathToExportFile = ''; + protected $_handle = null; + protected $_sSeparator = ";"; + protected $_sEnclosure = "'"; + + // todo: add to config + protected $_aHttpCodesIsOkay = array('200','302'); /** * Executes cache warmer */ @@ -25,31 +32,18 @@ class CacheWarmer extends BaseController { $sMessage = "psCacheWarmer
".$this->_getSitemapUrl()."
---
"; + $this->_sFileAndPathToExportFile = $this->_getPathWithFileName(); + if(Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsv') == true) + { + $this->_handle = fopen( $this->_sFileAndPathToExportFile, "w+");; + } + if($this->_checkAuthentification()) { $aUrls = $this->_getSitemapContent(); if(!empty(Registry::getConfig()->getShopConfVar('psCacheWarmerSitemapUrl')) && count($aUrls) > 0) { foreach($aUrls as $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); - $httpStatus = curl_getinfo($oCurl, CURLINFO_HTTP_CODE); - if(curl_error($oCurl)) { - $sMessage .= 'ERROR '.$httpStatus.': ' . curl_error($oCurl) . '
'; - } else { - if(trim($httpStatus) == '500') - { - $sMessage .= 'ERROR '.$httpStatus.': ' . $sUrl. '
'; - } - else{ - $sMessage .= 'OK '.$httpStatus.': ' . $sUrl . '
'; - } - } + $oCurl = $this->_runCurlConnect($sUrl); + $sMessage .= $this->_checkCurlResults($oCurl,$sUrl); curl_close($oCurl); } } else { @@ -59,10 +53,73 @@ class CacheWarmer extends BaseController $sMessage .= 'Authentifizierung fehlgeschlagen!'; } + if(Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsv') == true) + { + fclose($this->_handle); + } echo '
'.$sMessage.'
'; exit; } + /** + * @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 .= 'ERROR '.$httpStatus.': ' . curl_error($oCurl) . '
'; + $sStatusMsg = 'ERROR'; + $sTmpText = curl_error($oCurl); + } else { + $sTmpText = $sUrl; + if(in_array(trim($httpStatus),$this->_aHttpCodesIsOkay)) + { + $sMessage .= 'OK '.$httpStatus.': ' . $sUrl . '
'; + $sStatusMsg = 'OK'; + } + else{ + $sMessage .= 'ERROR '.$httpStatus.': ' . $sUrl. '
'; + $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; + } + /** * Returens urls from sitemap * @@ -125,4 +182,26 @@ class CacheWarmer extends BaseController } return false; } + + + /** + * 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"; + } + } diff --git a/Application/views/admin/de/pscachewarmer_lang.php b/Application/views/admin/de/pscachewarmer_lang.php index 128988c..360748d 100644 --- a/Application/views/admin/de/pscachewarmer_lang.php +++ b/Application/views/admin/de/pscachewarmer_lang.php @@ -22,6 +22,8 @@ $aLang = array( 'SHOP_MODULE_psCacheWarmerKey' => 'Cronjob-Key', 'SHOP_MODULE_psCacheWarmerUser' => 'Basic-Auth Benutzer', 'SHOP_MODULE_psCacheWarmerPass' => 'Basic-Auth Kennwort', + 'SHOP_MODULE_psCacheWarmerWriteCsv' => 'Report als CSV-Datei im Log-Ordner abspeichern', + 'SHOP_MODULE_psCacheWarmerWriteCsvOnlyError' => 'Alle Ergebnisse ausser HTTP-Code 200 loggen', ); diff --git a/README.md b/README.md index 047299d..d616b97 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Installation Changelog + 2019-07-19 2.2.0 Write Report in a file (PR #3) 2019-07-17 2.1.0 add error 500 check (PR #2) 2019-06-26 2.0.0 OXID eShop 6 (PR #1) 2016-10-12 1.0.1 fix reading sitemap url with user/pass,fix checking sitemap object diff --git a/composer.json b/composer.json index 0c036b7..c1dde4f 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "proudcommerce/cachewarmer", - "version": "2.1.0", + "version": "2.2.0", "description": "psCacheWarmer", "type": "oxideshop-module", "license": [ diff --git a/metadata.php b/metadata.php index 853983b..ec6d70a 100644 --- a/metadata.php +++ b/metadata.php @@ -29,7 +29,7 @@ $aModule = array( URL: '.oxRegistry::getConfig()->getConfigParam('sShopURL').'?cl=psCacheWarmer&key='.oxRegistry::getConfig()->getShopConfVar('psCacheWarmerKey', oxRegistry::getConfig()->getShopId()).'', ), 'thumbnail' => 'logo_pc-os.jpg', - 'version' => '2.1.0', + 'version' => '2.2.0', 'author' => 'Proud Sourcing GmbH', 'url' => 'http://www.proudcommerce.com/', 'email' => 'support@proudcommerce.com', @@ -67,5 +67,17 @@ $aModule = array( 'type' => 'str', 'value' => '', ), + array( + 'group' => 'psCacheWarmerConfig', + 'name' => 'psCacheWarmerWriteCsv', + 'type' => 'bool', + 'value' => '', + ), + array( + 'group' => 'psCacheWarmerConfig', + 'name' => 'psCacheWarmerWriteCsvOnlyError', + 'type' => 'bool', + 'value' => '', + ), ), );