From 6712d1441be1291de71736a11212f25dde605f1d Mon Sep 17 00:00:00 2001 From: Riny van Tiggelen Date: Sat, 12 Dec 2020 22:57:56 +0100 Subject: [PATCH 1/3] [FEATURE] Added global configuration for basic auth and disabling preview --- Classes/Backend/PageLayoutHeader.php | 7 ++++-- Classes/Controller/ModuleController.php | 2 -- Classes/Service/PreviewService.php | 25 ++++++++++++++++++- .../Configuration/SnippetPreview/Index.rst | 19 ++++++++++++++ ext_localconf.php | 8 +++++- 5 files changed, 55 insertions(+), 6 deletions(-) diff --git a/Classes/Backend/PageLayoutHeader.php b/Classes/Backend/PageLayoutHeader.php index d5dba9fb..c1dc689b 100644 --- a/Classes/Backend/PageLayoutHeader.php +++ b/Classes/Backend/PageLayoutHeader.php @@ -30,8 +30,6 @@ class PageLayoutHeader 'languageKeyToLocaleMapping' => [] ], 'menuActions' => [], - 'previewDomain' => null, - 'previewUrlTemplate' => '', 'viewSettings' => [] ]; @@ -148,6 +146,11 @@ protected function showSnippetPreview(): bool return false; } + if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['disablePreview']) && + $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['disablePreview'] === true) { + return false; + } + if ((bool)$GLOBALS['BE_USER']->uc['hideYoastInPageModule']) { return false; } diff --git a/Classes/Controller/ModuleController.php b/Classes/Controller/ModuleController.php index e5b84df1..af42d8e4 100644 --- a/Classes/Controller/ModuleController.php +++ b/Classes/Controller/ModuleController.php @@ -50,8 +50,6 @@ class ModuleController extends ActionController 'languageKeyToLocaleMapping' => [] ], 'menuActions' => [], - 'previewDomain' => null, - 'previewUrlTemplate' => '', 'viewSettings' => [] ]; diff --git a/Classes/Service/PreviewService.php b/Classes/Service/PreviewService.php index 04c73dde..e0b92c1b 100644 --- a/Classes/Service/PreviewService.php +++ b/Classes/Service/PreviewService.php @@ -65,7 +65,7 @@ public function getPreviewData($uriToCheck, $pageId) */ protected function getContentFromUrl($uriToCheck): string { - $GLOBALS['TYPO3_CONF_VARS']['HTTP']['verify'] = false; + $this->setHttpOptions(); $report = []; $content = GeneralUtility::getUrl( $uriToCheck, @@ -192,4 +192,27 @@ protected function stripTagsContent($text, $tags = '', $invert = false) return $text; } + + /** + * Set http options for the preview request + */ + protected function setHttpOptions() + { + $GLOBALS['TYPO3_CONF_VARS']['HTTP']['verify'] = false; + if (!isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['basicAuth'])) { + return; + } + + $basicAuth = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['basicAuth']; + if (!is_array($basicAuth) || !isset($basicAuth['username'], $basicAuth['password'])) { + return; + } + + if (!empty($basicAuth['username']) && !empty($basicAuth['password'])) { + $GLOBALS['TYPO3_CONF_VARS']['HTTP']['auth'] = [ + $basicAuth['username'], + $basicAuth['password'] + ]; + } + } } diff --git a/Documentation/Configuration/SnippetPreview/Index.rst b/Documentation/Configuration/SnippetPreview/Index.rst index bbcdf565..d346f2d3 100644 --- a/Documentation/Configuration/SnippetPreview/Index.rst +++ b/Documentation/Configuration/SnippetPreview/Index.rst @@ -37,3 +37,22 @@ also disable the snippet preview based on PageTs. Below an example to hide page disableSnippetPreview = 1 } [global] + +Disable snippet preview globally +-------------------------------- +If you want to disable the snippet preview completely, for example for a specific environment, it's possible to set this through :php:`$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['disablePreview']` + +.. code-block:: php + + $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['disablePreview'] = true; + +Basic auth configuration +------------------------ +For environments which are protected by Basic auth, it's possible to set username and password through :php:`$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['basicAuth']` + +.. code-block:: php + + $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['basicAuth'] = [ + 'username' => 'authUsername', + 'password' => 'authPassword' + ]; \ No newline at end of file diff --git a/ext_localconf.php b/ext_localconf.php index 73bd1409..132aa905 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -155,7 +155,13 @@ 'showSocialTab' => true, 'showAdvancedTab' => true ], - 'previewUrlTemplate' => '/?id=%d&type=%d&L=%d', + 'previewSettings' => [ + 'disablePreview' => false, + 'basicAuth' => [ + 'username' => '', + 'password' => '', + ] + ], 'overview_filters' => [ '10' => [ 'key' => 'cornerstone', From 7b46b23e61e71814d6060ace4e6bc393a88c66d8 Mon Sep 17 00:00:00 2001 From: Richard Haeser Date: Tue, 22 Dec 2020 13:30:58 +0100 Subject: [PATCH 2/3] [TASK] Refactored basicauth code a little bit --- Classes/Backend/PageLayoutHeader.php | 30 +--------------- Classes/Service/PreviewService.php | 2 ++ Classes/Utility/YoastUtility.php | 53 +++++++++++++++++----------- ext_localconf.php | 1 - 4 files changed, 36 insertions(+), 50 deletions(-) diff --git a/Classes/Backend/PageLayoutHeader.php b/Classes/Backend/PageLayoutHeader.php index c1dc689b..5235a672 100644 --- a/Classes/Backend/PageLayoutHeader.php +++ b/Classes/Backend/PageLayoutHeader.php @@ -3,7 +3,6 @@ use TYPO3\CMS\Backend\Controller\PageLayoutController; use TYPO3\CMS\Backend\Utility\BackendUtility; -use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; use TYPO3\CMS\Core\Page\PageRenderer; use TYPO3\CMS\Core\Utility\ArrayUtility; use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; @@ -74,12 +73,8 @@ public function render() { $moduleData = BackendUtility::getModuleData(['language'], [], 'web_layout'); $pageId = (int)GeneralUtility::_GET('id'); - - if (!$this->showSnippetPreview()) { - return ''; - } - $currentPage = $this->getCurrentPage($pageId, $moduleData); + if (!YoastUtility::snippetPreviewEnabled($pageId, $currentPage)) { return ''; } @@ -134,29 +129,6 @@ public function render() return ''; } - /** - * Check to see if snippet preview has to be shown - * - * @return bool - */ - protected function showSnippetPreview(): bool - { - if (!$GLOBALS['BE_USER'] instanceof BackendUserAuthentication || - !$GLOBALS['BE_USER']->check('non_exclude_fields', 'pages:tx_yoastseo_snippetpreview')) { - return false; - } - - if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['disablePreview']) && - $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['disablePreview'] === true) { - return false; - } - - if ((bool)$GLOBALS['BE_USER']->uc['hideYoastInPageModule']) { - return false; - } - return true; - } - /** * @param $pageId * @param $moduleData diff --git a/Classes/Service/PreviewService.php b/Classes/Service/PreviewService.php index e0b92c1b..71531a27 100644 --- a/Classes/Service/PreviewService.php +++ b/Classes/Service/PreviewService.php @@ -65,6 +65,7 @@ public function getPreviewData($uriToCheck, $pageId) */ protected function getContentFromUrl($uriToCheck): string { + $backupSettings = $GLOBALS['TYPO3_CONF_VARS']['HTTP']; $this->setHttpOptions(); $report = []; $content = GeneralUtility::getUrl( @@ -78,6 +79,7 @@ protected function getContentFromUrl($uriToCheck): string $report ); + $GLOBALS['TYPO3_CONF_VARS']['HTTP'] = $backupSettings; if ((int)$report['error'] === 0) { return $content; } diff --git a/Classes/Utility/YoastUtility.php b/Classes/Utility/YoastUtility.php index dbeaafc3..57efee7e 100644 --- a/Classes/Utility/YoastUtility.php +++ b/Classes/Utility/YoastUtility.php @@ -14,8 +14,16 @@ * The TYPO3 project - inspiring people to share! */ -use TYPO3\CMS; +use TYPO3\CMS\Backend\Utility\BackendUtility; +use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; +use TYPO3\CMS\Core\Database\ConnectionPool; +use TYPO3\CMS\Core\Package\Exception; +use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Core\Utility\VersionNumberUtility; +use TYPO3\CMS\Extbase\Configuration\ConfigurationManager; +use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; +use TYPO3\CMS\Extbase\Object\ObjectManager; /** * Class YoastUtility @@ -69,10 +77,17 @@ public static function getAllowedDoktypes($configuration = null, $returnInString */ public static function snippetPreviewEnabled($pageId, array $pageRecord, $pageTs = null) { - $showPreview = !$pageRecord['tx_yoastseo_hide_snippet_preview']; + if (!$GLOBALS['BE_USER'] instanceof BackendUserAuthentication || + !$GLOBALS['BE_USER']->check('non_exclude_fields', 'pages:tx_yoastseo_snippetpreview')) { + return false; + } + + if ((bool)$GLOBALS['BE_USER']->uc['hideYoastInPageModule']) { + return false; + } if ($pageTs === null) { - $pageTs = CMS\Backend\Utility\BackendUtility::getPagesTSconfig($pageId); + $pageTs = BackendUtility::getPagesTSconfig($pageId); } if (is_array($pageTs) && @@ -83,10 +98,10 @@ public static function snippetPreviewEnabled($pageId, array $pageRecord, $pageTs array_key_exists('disableSnippetPreview', $pageTs['mod.']['web_SeoPlugin.']) && (int)$pageTs['mod.']['web_SeoPlugin.']['disableSnippetPreview'] === 1 ) { - $showPreview = false; + return false; } - return $showPreview; + return ($pageRecord['tx_yoastseo_hide_snippet_preview']) ? false : true; } /** @@ -102,7 +117,7 @@ public static function getFocusKeywordOfPage($uid, $table = 'pages') return ''; } - $record = CMS\Backend\Utility\BackendUtility::getRecord($table, $uid); + $record = BackendUtility::getRecord($table, $uid); if (\is_array($record) && array_key_exists(self::COLUMN_NAME_FOCUSKEYWORD, $record)) { $focusKeyword = $record[self::COLUMN_NAME_FOCUSKEYWORD]; } @@ -117,7 +132,7 @@ public static function getFocusKeywordOfPage($uid, $table = 'pages') foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['get_focus_keyword'] as $_funcRef) { if ($_funcRef) { $tmp = new \stdClass(); - CMS\Core\Utility\GeneralUtility::callUserFunction($_funcRef, $params, $tmp); + GeneralUtility::callUserFunction($_funcRef, $params, $tmp); } } } @@ -133,7 +148,7 @@ public static function getFocusKeywordOfPage($uid, $table = 'pages') public static function getRelatedKeyphrases($parentTable, $parentId): array { $config = []; - $queryBuilder = CMS\Core\Utility\GeneralUtility::makeInstance(CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable('tx_yoast_seo_premium_focus_keywords'); + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_yoast_seo_premium_focus_keywords'); if ($queryBuilder) { $relatedKeyphrases = $queryBuilder->select('*') ->from('tx_yoast_seo_premium_focus_keywords') @@ -160,7 +175,7 @@ public static function getRelatedKeyphrases($parentTable, $parentId): array */ public static function isPremiumInstalled() { - return (bool)CMS\Core\Utility\ExtensionManagementUtility::isLoaded('yoast_seo_premium'); + return (bool)ExtensionManagementUtility::isLoaded('yoast_seo_premium'); } /** @@ -187,23 +202,21 @@ public static function inProductionMode($configuration = null) protected static function getTypoScriptConfiguration() { - /** @var CMS\Extbase\Object\ObjectManager $objectManager */ - $objectManager = CMS\Core\Utility\GeneralUtility::makeInstance(CMS\Extbase\Object\ObjectManager::class); - /** @var CMS\Extbase\Configuration\ConfigurationManager $configurationManager */ - $configurationManager = $objectManager->get(CMS\Extbase\Configuration\ConfigurationManager::class); - $configuration = $configurationManager->getConfiguration( - CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, + /** @var ObjectManager $objectManager */ + $objectManager = GeneralUtility::makeInstance(ObjectManager::class); + /** @var ConfigurationManager $configurationManager */ + $configurationManager = $objectManager->get(ConfigurationManager::class); + return $configurationManager->getConfiguration( + ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, 'yoastseo' ); - - return $configuration; } /** * @param string $utm_term * @param string $utm_content * @param string $utm_source * @return string - * @throws CMS\Core\Package\Exception + * @throws Exception */ public static function getYoastLink($utm_term = 'Go premium', $utm_content = '', $utm_source = 'yoast-seo-for-typo3') { @@ -216,9 +229,9 @@ public static function getYoastLink($utm_term = 'Go premium', $utm_content = '', 'utm_campaign' => 'typo3-ad', 'php_version' => $php_version[1] ?: 'unknown', 'platform' => 'TYPO3', - 'platform_version' => CMS\Core\Utility\VersionNumberUtility::getNumericTypo3Version(), + 'platform_version' => VersionNumberUtility::getNumericTypo3Version(), 'software' => self::isPremiumInstalled() ? 'premium' : 'free', - 'software_version' => CMS\Core\Utility\ExtensionManagementUtility::getExtensionVersion('yoast_seo'), + 'software_version' => ExtensionManagementUtility::getExtensionVersion('yoast_seo'), 'role' => '' ]; diff --git a/ext_localconf.php b/ext_localconf.php index 132aa905..7210b972 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -156,7 +156,6 @@ 'showAdvancedTab' => true ], 'previewSettings' => [ - 'disablePreview' => false, 'basicAuth' => [ 'username' => '', 'password' => '', From fdbd3a6140c667e5e3b9dda010fb40063a766ee6 Mon Sep 17 00:00:00 2001 From: Richard Haeser Date: Tue, 22 Dec 2020 13:32:29 +0100 Subject: [PATCH 3/3] [TASK] Removed documentation about non existing feature --- Documentation/Configuration/SnippetPreview/Index.rst | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Documentation/Configuration/SnippetPreview/Index.rst b/Documentation/Configuration/SnippetPreview/Index.rst index d346f2d3..e88a9042 100644 --- a/Documentation/Configuration/SnippetPreview/Index.rst +++ b/Documentation/Configuration/SnippetPreview/Index.rst @@ -38,14 +38,6 @@ also disable the snippet preview based on PageTs. Below an example to hide page } [global] -Disable snippet preview globally --------------------------------- -If you want to disable the snippet preview completely, for example for a specific environment, it's possible to set this through :php:`$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['disablePreview']` - -.. code-block:: php - - $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['disablePreview'] = true; - Basic auth configuration ------------------------ For environments which are protected by Basic auth, it's possible to set username and password through :php:`$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['basicAuth']`