From 7fcf810ce44afc8f837cf5a4fbfe996c575db707 Mon Sep 17 00:00:00 2001 From: Tobias Merkl Date: Mon, 17 Aug 2020 12:38:19 +0200 Subject: [PATCH] initial 3.0.0 --- Application/Controller/CacheWarmer.php | 207 ------------------ .../views/admin/de/pscachewarmer_lang.php | 31 ++- Core/CacheWarmer.php | 153 +++++++++++++ README.md | 24 +- composer.json | 58 ++--- metadata.php | 87 ++++---- 6 files changed, 255 insertions(+), 305 deletions(-) delete mode 100644 Application/Controller/CacheWarmer.php create mode 100644 Core/CacheWarmer.php diff --git a/Application/Controller/CacheWarmer.php b/Application/Controller/CacheWarmer.php deleted file mode 100644 index c94ebad..0000000 --- a/Application/Controller/CacheWarmer.php +++ /dev/null @@ -1,207 +0,0 @@ -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 = $this->_runCurlConnect($sUrl); - $sMessage .= $this->_checkCurlResults($oCurl,$sUrl); - curl_close($oCurl); - } - } else { - $sMessage .= 'Keine Daten vorhanden!'; - } - } else { - $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 - * - * @return array urls - */ - protected function _getSitemapContent($sSitemapUrl = "") - { - $aUrls = array(); - if(empty($sSitemapUrl)) { - $sSitemapUrl = $this->_getSitemapUrl(); - } - - $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) { - $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() - { - $sSitemapUrl = Registry::getConfig()->getConfigParam('sShopURL'); - $sSitemapUrl .= Registry::getConfig()->getShopConfVar('psCacheWarmerSitemapUrl'); - return $sSitemapUrl; - } - - /** - * Checks authentification - * - * @return bool true|false - */ - protected function _checkAuthentification() - { - $oConfig = Registry::getConfig(); - $sKey = Registry::getConfig()->getRequestParameter("key"); - $sSavedKey = $oConfig->getShopConfVar('psCacheWarmerKey', $oConfig->getShopId()); - if($sSavedKey == $sKey) { - return true; - } - 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 360748d..b92e7ae 100644 --- a/Application/views/admin/de/pscachewarmer_lang.php +++ b/Application/views/admin/de/pscachewarmer_lang.php @@ -5,28 +5,25 @@ * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * - * @copyright (c) Proud Sourcing GmbH | 2016 + * @copyright (c) ProudCommerce | 2020 * @link www.proudcommerce.com * @package psCacheWarmer - * @version 1.0.1 + * @version 3.0.0 **/ -$sLangName = "Deutsch"; + +$sLangName = "Deutsch"; // ------------------------------- // RESOURCE IDENTITFIER = STRING // ------------------------------- -$aLang = array( - 'charset' => 'UTF-8', +$aLang = [ + 'charset' => 'UTF-8', - 'SHOP_MODULE_GROUP_psCacheWarmerConfig' => 'Einstellungen', - 'SHOP_MODULE_psCacheWarmerSitemapUrl' => 'XML-Sitemap URL', - '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', + 'SHOP_MODULE_GROUP_psCacheWarmerConfig' => 'Einstellungen', + 'SHOP_MODULE_psCacheWarmerSitemapUrl' => 'XML-Sitemap URL', + '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', -); - -/* -[{ oxmultilang ident="GENERAL_YOUWANTTODELETE" }] -*/ +]; diff --git a/Core/CacheWarmer.php b/Core/CacheWarmer.php new file mode 100644 index 0000000..8c1ccf4 --- /dev/null +++ b/Core/CacheWarmer.php @@ -0,0 +1,153 @@ +psCacheWarmer
" . $this->_getSitemapUrl() . "
---
"; + + $aUrls = $this->_getSitemapContent(); + if (!empty(Registry::getConfig()->getShopConfVar('psCacheWarmerSitemapUrl')) && count($aUrls) > 0) { + foreach ($aUrls as $sUrl) { + $oCurl = $this->_runCurlConnect($sUrl); + $sMessage .= $this->_checkCurlResults($oCurl, $sUrl); + curl_close($oCurl); + } + } else { + $sMessage .= 'Keine Daten vorhanden!'; + } + + 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; + } + + /** + * @param $oCurl + * @param $sUrl + * @return string + */ + 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), Registry::getConfig()->getShopConfVar('psCacheWarmerConfig'))) { + $sMessage .= 'OK ' . $httpStatus . ': ' . $sUrl . '
'; + $sStatusMsg = 'OK'; + } else { + $sMessage .= 'ERROR ' . $httpStatus . ': ' . $sUrl . '
'; + $sStatusMsg = 'ERROR'; + } + } + + if (Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsv') == true) { + $aTmp = [$sStatusMsg, $httpStatus, $sTmpText]; + + if (trim($httpStatus) == '200' && Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsvOnlyError') == true) { + $aTmp = []; + } + + if (count($aTmp)) { + Registry::getUtils()->writeToLog(implode(' | ', $aTmp) . "\r", 'tabslbillomat_' . date("d.m.Y H:i:s") . '.log'); + } + } + + return $sMessage; + } + + /** + * @param string $sSitemapUrl + * @return array + */ + protected function _getSitemapContent($sSitemapUrl = "") + { + $aUrls = []; + if (empty($sSitemapUrl)) { + $sSitemapUrl = $this->_getSitemapUrl(); + } + + $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) { + $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; + } + + /** + * @return string + */ + protected function _getSitemapUrl() + { + $sSitemapUrl = Registry::getConfig()->getConfigParam('sShopURL'); + $sSitemapUrl .= Registry::getConfig()->getShopConfVar('psCacheWarmerSitemapUrl'); + return $sSitemapUrl; + } + +} diff --git a/README.md b/README.md index 757733e..a87778a 100644 --- a/README.md +++ b/README.md @@ -6,23 +6,27 @@ Free module for OXID eshop 6. Features - - admin setting sitemap url - - admin setting security key (cronjob) - - admin setting basic auth user/password + - set sitemap url in admin + - optional basic auth user/password + - optional csv logfile Installation composer require proudcommerce/cachewarmer +Usage + php source/modules/pc/cachewarmer/Core/CacheWarmer.php + Changelog - 2020-08-06 2.2.1 fix for OXID 6.2 - 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 - 2016-08-25 1.0.0 module release for oxid 4.7, 4.8, 4.9, 4.10 + 2020-08-17 3.0.0 cli only, some improvements + 2020-08-06 2.2.1 fix for OXID 6.2 + 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 + 2016-08-25 1.0.0 module release for oxid 4.7, 4.8, 4.9, 4.10 License @@ -42,4 +46,4 @@ License Copyright - ProudCommerce { www.proudcommerce.com } \ No newline at end of file + ProudCommerce { www.proudcommerce.com } diff --git a/composer.json b/composer.json index 4d1ca7f..52e255a 100644 --- a/composer.json +++ b/composer.json @@ -1,32 +1,32 @@ { - "name": "proudcommerce/cachewarmer", - "description": "psCacheWarmer", - "type": "oxideshop-module", - "license": [ - "proprietary" - ], - "authors": [ - { - "name": "ProudCommerce", - "homepage": "https://www.proudcommerce.com/", - "email": "welcome@proudcommerce.com", - "role": "Developer" + "name": "proudcommerce/cachewarmer", + "description": "Website anhand der XML-Sitemap automatisch aufrufen, z. B. zum 'Aufwärmen' eines Caches.", + "type": "oxideshop-module", + "license": [ + "GPL-3.0-or-later" + ], + "authors": [ + { + "name": "ProudCommerce", + "homepage": "https://www.proudcommerce.com/", + "email": "welcome@proudcommerce.com", + "role": "Developer" + } + ], + "support": { + "email": "support@proudcommerce.com" + }, + "require": { + "php": ">= 7.0" + }, + "autoload": { + "psr-4": { + "ProudCommerce\\CacheWarmer\\": "../../../source/modules/pc/cachewarmer" + } + }, + "extra": { + "oxideshop": { + "target-directory": "pc/cachewarmer" + } } - ], - "support": { - "email": "support@proudcommerce.com" - }, - "require": { - "php": ">= 7.0" - }, - "autoload": { - "psr-4": { - "ProudCommerce\\CacheWarmer\\": "../../../source/modules/proudsourcing/psCacheWarmer" - } - }, - "extra": { - "oxideshop": { - "target-directory": "proudsourcing/psCacheWarmer" - } - } } diff --git a/metadata.php b/metadata.php index 2de2d91..de980b5 100644 --- a/metadata.php +++ b/metadata.php @@ -4,12 +4,12 @@ * 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 | 2020 + * + * @copyright (c) ProudCommerce | 2020 * @link www.proudcommerce.com * @package psCacheWarmer - * @version 2.2.1 -**/ + * @version 3.0.0 + **/ /** * Metadata version @@ -19,65 +19,68 @@ $sMetadataVersion = '2.0'; /** * Module information */ -$aModule = array( - 'id' => 'psCacheWarmer', - 'title' => 'psCacheWarmer', - 'description' => array( - 'de' => 'Website anhand der XML-Sitemap automatisch aufrufen, z. B. zum "Aufwärmen" eines Caches.
- URL: '.oxRegistry::getConfig()->getConfigParam('sShopURL').'?cl=psCacheWarmer&key='.oxRegistry::getConfig()->getShopConfVar('psCacheWarmerKey', oxRegistry::getConfig()->getShopId()).'', - 'en' => 'Automatically calling urls using the xml-sitemap, eg. for cache warming. - URL: '.oxRegistry::getConfig()->getConfigParam('sShopURL').'?cl=psCacheWarmer&key='.oxRegistry::getConfig()->getShopConfVar('psCacheWarmerKey', oxRegistry::getConfig()->getShopId()).'', - ), - 'thumbnail' => 'logo_pc-os.jpg', - 'version' => '2.2.1', - 'author' => 'Proud Sourcing GmbH', - 'url' => 'http://www.proudcommerce.com/', - 'email' => 'support@proudcommerce.com', - 'extend' => array( - ), - 'controllers' => array( - 'pscachewarmer' => \ProudCommerce\CacheWarmer\Application\Controller\CacheWarmer::class, - ), - 'templates' => array( - ), - 'blocks' => array( - ), - 'settings' => array( - array( +$aModule = [ + 'id' => 'psCacheWarmer', + 'title' => 'psCacheWarmer', + 'description' => [ + 'de' => 'Website anhand der XML-Sitemap automatisch aufrufen, z. B. zum "Aufwärmen" eines Caches.', + 'en' => 'Automatically calling urls using the xml-sitemap, eg. for cache warmup.', + ], + 'thumbnail' => 'logo_pc-os.jpg', + 'version' => '3.0.0', + 'author' => 'ProudCommerce', + 'url' => 'https://github.com/proudcommerce/psCacheWarmer', + 'email' => '', + 'extend' => [ + ], + 'controllers' => [ + ], + 'templates' => [ + ], + 'blocks' => [ + ], + 'settings' => [ + [ 'group' => 'psCacheWarmerConfig', 'name' => 'psCacheWarmerSitemapUrl', 'type' => 'str', 'value' => 'sitemap.xml', - ), - array( + ], + [ 'group' => 'psCacheWarmerConfig', 'name' => 'psCacheWarmerKey', 'type' => 'str', 'value' => md5(time()), - ), - array( + ], + [ 'group' => 'psCacheWarmerConfig', 'name' => 'psCacheWarmerUser', 'type' => 'str', 'value' => '', - ), - array( + ], + [ 'group' => 'psCacheWarmerConfig', 'name' => 'psCacheWarmerPass', 'type' => 'str', 'value' => '', - ), - array( + ], + [ + 'group' => 'psCacheWarmerConfig', + 'name' => 'psCacheWarmerHttpCodes', + 'type' => 'arr', + 'value' => [200, 302], + ], + [ 'group' => 'psCacheWarmerConfig', 'name' => 'psCacheWarmerWriteCsv', 'type' => 'bool', 'value' => false, - ), - array( + ], + [ 'group' => 'psCacheWarmerConfig', 'name' => 'psCacheWarmerWriteCsvOnlyError', 'type' => 'bool', 'value' => true, - ), - ), -); + ] + ], +];