From ac61f97eded20263422268a5c945767e58c09e80 Mon Sep 17 00:00:00 2001
From: Georgi Mateev <gmateev@exactag.com>
Date: Sun, 8 Aug 2021 20:55:20 +0300
Subject: [PATCH] [TASK] Add/fix demo mode checks

---
 Classes/Controller/RouteController.php        | 59 +++++++++++++++----
 Classes/Middleware/FrontendLinkResolver.php   |  6 +-
 Classes/Middleware/RedirectResolver.php       |  6 +-
 .../Private/Backend/Layouts/Default.html      |  3 +
 Resources/Private/Language/de.locallang.xlf   | 28 +++++++++
 Resources/Private/Language/locallang.xlf      | 21 +++++++
 Resources/Private/Partials/License.html       | 24 ++++++++
 ext_tables.php                                |  2 +-
 8 files changed, 130 insertions(+), 19 deletions(-)
 create mode 100644 Resources/Private/Partials/License.html

diff --git a/Classes/Controller/RouteController.php b/Classes/Controller/RouteController.php
index 1660b04..25bae80 100644
--- a/Classes/Controller/RouteController.php
+++ b/Classes/Controller/RouteController.php
@@ -36,6 +36,7 @@ use SGalinski\SgRoutes\Domain\Repository\LogRepository;
 use SGalinski\SgRoutes\Domain\Repository\PageNotFoundHandlingRepository;
 use SGalinski\SgRoutes\Domain\Repository\RoutehitRepository;
 use SGalinski\SgRoutes\Domain\Repository\RouteRepository;
+use SGalinski\SgRoutes\Service\LicenceCheckService;
 use SGalinski\SgRoutes\Service\LicensingService;
 use SGalinski\SgRoutes\Service\RoutingService;
 use TYPO3\CMS\Backend\Clipboard\Clipboard;
@@ -174,7 +175,6 @@ class RouteController extends ActionController {
 		$this->command = GeneralUtility::_GP('cmd');
 		$this->clipboardCommandArray = GeneralUtility::_GP('CB');
 		$this->initClipboard();
-		$this->initDemoMode();
 	}
 
 	/**
@@ -260,6 +260,8 @@ class RouteController extends ActionController {
 				FlashMessage::ERROR
 			);
 		}
+
+		$this->initDemoMode();
 	}
 
 	/**
@@ -595,53 +597,90 @@ class RouteController extends ActionController {
 		return $taskExists;
 	}
 
+	/**
+	 * Initializes the demo mode
+	 */
 	protected function initDemoMode() {
 		$typo3Version = VersionNumberUtility::convertVersionNumberToInteger(TYPO3_version);
 		$keyState = DemoModeService::checkKey();
+		$hasValidLicense = LicenceCheckService::hasValidLicense();
 		$isInDemoMode = DemoModeService::isInDemoMode();
-		if ($keyState !== DemoModeService::STATE_LICENSE_VALID && $isInDemoMode) {
+		if (!$hasValidLicense && $isInDemoMode) {
 			// - 1 because the flash message would show 00:00:00 instead of 23:59:59
 			$this->addFlashMessage(
 				LocalizationUtility::translate(
-					'backend.licenseKey.isInDemoMode.description', 'sg_cookie_optin', [
+					'backend.licenseKey.isInDemoMode.description', 'sg_routes', [
 						date('H:i:s', mktime(0, 0, DemoModeService::getRemainingTimeInDemoMode() - 1))
 					]
 				),
-				LocalizationUtility::translate('backend.licenseKey.isInDemoMode.header', 'sg_cookie_optin'),
+				LocalizationUtility::translate('backend.licenseKey.isInDemoMode.header', 'sg_routes'),
 				AbstractMessage::INFO
 			);
 		} elseif ($keyState === DemoModeService::STATE_LICENSE_INVALID) {
 			if ($typo3Version < 9000000) {
 				$description = LocalizationUtility::translate(
-					'backend.licenseKey.invalid.description', 'sg_cookie_optin'
+					'backend.licenseKey.invalid.description', 'sg_routes'
 				);
 			} else {
 				$description = LocalizationUtility::translate(
-					'backend.licenseKey.invalid.descriptionTYPO3-9', 'sg_cookie_optin'
+					'backend.licenseKey.invalid.descriptionTYPO3-9', 'sg_routes'
 				);
 			}
 
 			$this->addFlashMessage(
 				$description,
-				LocalizationUtility::translate('backend.licenseKey.invalid.header', 'sg_cookie_optin'),
+				LocalizationUtility::translate('backend.licenseKey.invalid.header', 'sg_routes'),
 				AbstractMessage::ERROR
 			);
 		} elseif ($keyState === DemoModeService::STATE_LICENSE_NOT_SET) {
 			if ($typo3Version < 9000000) {
 				$description = LocalizationUtility::translate(
-					'backend.licenseKey.notSet.description', 'sg_cookie_optin'
+					'backend.licenseKey.notSet.description', 'sg_routes'
 				);
 			} else {
 				$description = LocalizationUtility::translate(
-					'backend.licenseKey.notSet.descriptionTYPO3-9', 'sg_cookie_optin'
+					'backend.licenseKey.notSet.descriptionTYPO3-9', 'sg_routes'
 				);
 			}
 
 			$this->addFlashMessage(
 				$description,
-				LocalizationUtility::translate('backend.licenseKey.notSet.header', 'sg_cookie_optin'),
+				LocalizationUtility::translate('backend.licenseKey.notSet.header', 'sg_routes'),
 				AbstractMessage::WARNING
 			);
 		}
+
+		$this->view->assign('invalidKey', !$hasValidLicense);
+		$this->view->assign('showDemoButton', !$isInDemoMode && DemoModeService::isDemoModeAcceptable());
+	}
+
+	/**
+	 * Add a flash message
+	 *
+	  * @param string $messageBody
+	  * @param string $messageTitle
+	  * @param int $severity
+	  * @param bool $storeInSession
+	  */
+	 public function addFlashMessage($messageBody, $messageTitle = '', $severity = AbstractMessage::OK, $storeInSession = true)
+	 {
+	     if (null === $this->controllerContext) {
+	         $this->controllerContext = $this->buildControllerContext();
+	     }
+	     parent::addFlashMessage($messageBody, $messageTitle, $severity, $storeInSession);
+	 }
+
+	/**
+	 * Activates the demo mode for the given instance.
+	 *
+	 * @throws StopActionException
+	 */
+	public function activateDemoModeAction() {
+		if (DemoModeService::isInDemoMode() || !DemoModeService::isDemoModeAcceptable()) {
+			$this->redirect('list');
+		}
+
+		DemoModeService::activateDemoMode();
+		$this->redirect('list');
 	}
 }
diff --git a/Classes/Middleware/FrontendLinkResolver.php b/Classes/Middleware/FrontendLinkResolver.php
index 4d1c72d..05dcfb9 100644
--- a/Classes/Middleware/FrontendLinkResolver.php
+++ b/Classes/Middleware/FrontendLinkResolver.php
@@ -32,6 +32,7 @@ use Psr\Http\Message\ServerRequestInterface;
 use Psr\Http\Server\MiddlewareInterface;
 use Psr\Http\Server\RequestHandlerInterface;
 use SGalinski\SgRoutes\Service\DemoModeService;
+use SGalinski\SgRoutes\Service\LicenceCheckService;
 use TYPO3\CMS\Core\Http\Response;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
@@ -52,10 +53,7 @@ class FrontendLinkResolver implements MiddlewareInterface {
 	 * request handler to do so.
 	 */
 	public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
-		$keyState = DemoModeService::checkKey();
-		$isInDemoMode = DemoModeService::isInDemoMode();
-
-		if ($keyState !== DemoModeService::STATE_LICENSE_VALID && $isInDemoMode) {
+		if (!LicenceCheckService::hasValidLicense() && !DemoModeService::isInDemoMode()) {
 			return $handler->handle($request);
 		}
 
diff --git a/Classes/Middleware/RedirectResolver.php b/Classes/Middleware/RedirectResolver.php
index 9199083..949ae44 100644
--- a/Classes/Middleware/RedirectResolver.php
+++ b/Classes/Middleware/RedirectResolver.php
@@ -33,6 +33,7 @@ use Psr\Http\Server\MiddlewareInterface;
 use Psr\Http\Server\RequestHandlerInterface;
 use SGalinski\SgRoutes\Domain\Repository\RouteRepository;
 use SGalinski\SgRoutes\Service\DemoModeService;
+use SGalinski\SgRoutes\Service\LicenceCheckService;
 use SGalinski\SgRoutes\Service\LicensingService;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
@@ -83,10 +84,7 @@ class RedirectResolver implements MiddlewareInterface {
 	 * @throws \TYPO3\CMS\Core\Error\Http\PageNotFoundException
 	 */
 	public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
-		$keyState = DemoModeService::checkKey();
-		$isInDemoMode = DemoModeService::isInDemoMode();
-
-		if ($keyState !== DemoModeService::STATE_LICENSE_VALID && $isInDemoMode) {
+		if (!LicenceCheckService::hasValidLicense() && !DemoModeService::isInDemoMode()) {
 			return $handler->handle($request);
 		}
 
diff --git a/Resources/Private/Backend/Layouts/Default.html b/Resources/Private/Backend/Layouts/Default.html
index ef47db3..3350879 100644
--- a/Resources/Private/Backend/Layouts/Default.html
+++ b/Resources/Private/Backend/Layouts/Default.html
@@ -35,6 +35,9 @@
 						<f:render section="iconButtons" />
 					</div>
 				</div>
+				<div class="module-docheader-bar-column-left">
+					<f:render partial="License" arguments="{_all}" />
+				</div>
 				<div class="module-docheader-bar-column-right">
 					<f:render partial="ButtonBar" arguments="{buttons:docHeader.buttons.right}" />
 				</div>
diff --git a/Resources/Private/Language/de.locallang.xlf b/Resources/Private/Language/de.locallang.xlf
index 79cf19a..4dfe3fc 100644
--- a/Resources/Private/Language/de.locallang.xlf
+++ b/Resources/Private/Language/de.locallang.xlf
@@ -357,6 +357,34 @@
 				<source><![CDATA[Missing license key]]></source>
 				<target><![CDATA[Fehlender Lizenzschlüssel]]></target>
 			</trans-unit>
+			<trans-unit id="backend.activateDemoMode" approved="yes">
+				<source><![CDATA[Activate Demo]]></source>
+				<target><![CDATA[Demo aktivieren]]></target>
+			</trans-unit>
+			<trans-unit id="backend.buyLicense" approved="yes">
+				<source><![CDATA[Buy license key]]></source>
+				<target><![CDATA[Lizenzschlüssel kaufen]]></target>
+			</trans-unit>
+			<trans-unit id="backend.buyLicense.url" approved="yes">
+				<source><![CDATA[https://www.sgalinski.de/en/typo3-products-web-development/seo-redirects/]]></source>
+				<target><![CDATA[https://www.sgalinski.de/typo3-produkte-webentwicklung/seo-redirects/]]></target>
+			</trans-unit>
+			<trans-unit id="backend.documentation" approved="yes">
+				<source><![CDATA[Documentation]]></source>
+				<target><![CDATA[Dokumentation]]></target>
+			</trans-unit>
+			<trans-unit id="backend.documentation.url" approved="yes">
+				<source><![CDATA[https://www.sgalinski.de/en/typo3-products-web-development/seo-redirects/#c5490]]></source>
+				<target><![CDATA[https://www.sgalinski.de/typo3-produkte-webentwicklung/seo-redirects/#c5490]]></target>
+			</trans-unit>
+			<trans-unit id="backend.productInformation" approved="yes">
+				<source><![CDATA[Product Information]]></source>
+				<target><![CDATA[Produkt-Informationen]]></target>
+			</trans-unit>
+			<trans-unit id="backend.productInformation.url" approved="yes">
+				<source><![CDATA[https://www.sgalinski.de/en/typo3-products-web-development/seo-redirects/]]></source>
+				<target><![CDATA[https://www.sgalinski.de/typo3-produkte-webentwicklung/seo-redirects/]]></target>
+			</trans-unit>
 		</body>
 	</file>
 </xliff>
diff --git a/Resources/Private/Language/locallang.xlf b/Resources/Private/Language/locallang.xlf
index 7b78613..b27a144 100644
--- a/Resources/Private/Language/locallang.xlf
+++ b/Resources/Private/Language/locallang.xlf
@@ -260,6 +260,27 @@
 			<trans-unit id="backend.licenseKey.notSet.header" approved="yes">
 				<source><![CDATA[Missing license key]]></source>
 			</trans-unit>
+			<trans-unit id="backend.activateDemoMode" approved="yes">
+				<source><![CDATA[Activate Demo]]></source>
+			</trans-unit>
+			<trans-unit id="backend.buyLicense" approved="yes">
+				<source><![CDATA[Buy license key]]></source>
+			</trans-unit>
+			<trans-unit id="backend.buyLicense.url" approved="yes">
+				<source><![CDATA[https://www.sgalinski.de/en/typo3-products-web-development/seo-redirects/]]></source>
+			</trans-unit>
+			<trans-unit id="backend.documentation" approved="yes">
+				<source><![CDATA[Documentation]]></source>
+			</trans-unit>
+			<trans-unit id="backend.documentation.url" approved="yes">
+				<source><![CDATA[https://www.sgalinski.de/en/typo3-products-web-development/seo-redirects/#c5490]]></source>
+			</trans-unit>
+			<trans-unit id="backend.productInformation" approved="yes">
+				<source><![CDATA[Product Information]]></source>
+			</trans-unit>
+			<trans-unit id="backend.productInformation.url" approved="yes">
+				<source><![CDATA[https://www.sgalinski.de/en/typo3-products-web-development/seo-redirects/]]></source>
+			</trans-unit>
 		</body>
 	</file>
 </xliff>
diff --git a/Resources/Private/Partials/License.html b/Resources/Private/Partials/License.html
new file mode 100644
index 0000000..d83b577
--- /dev/null
+++ b/Resources/Private/Partials/License.html
@@ -0,0 +1,24 @@
+{namespace sg=SGalinski\SgRoutes\ViewHelpers}
+
+<f:if condition="{invalidKey}">
+	<f:then>
+		<a href="{f:translate(key: 'backend.buyLicense.url')}" target="_bĺank" class="btn btn-default btn-sm btn-danger btn-sm">
+			<f:translate key="backend.buyLicense" />
+		</a>
+
+		<f:if condition="{showDemoButton}">
+			<f:link.action class="btn btn-default btn-sm btn-info" action="activateDemoMode">
+				<f:translate key="backend.activateDemoMode" />
+			</f:link.action>
+		</f:if>
+	</f:then>
+	<f:else>
+		<a href="{f:translate(key: 'backend.productInformation.url')}" target="_bĺank" class="btn btn-default btn-sm">
+			<f:translate key="backend.productInformation" />
+		</a>
+	</f:else>
+</f:if>
+
+<a href="{f:translate(key: 'backend.documentation.url')}" target="_blank" class="btn btn-default btn-sm">
+	<f:translate key="backend.documentation" />
+</a>
diff --git a/ext_tables.php b/ext_tables.php
index 5968f2c..6c8a6b0 100644
--- a/ext_tables.php
+++ b/ext_tables.php
@@ -43,7 +43,7 @@ call_user_func(
 				'Route',
 				'',
 				[
-					'Route' => 'list, update, create, delete, deleteAll, htaccess, log, pageNotFoundHandling, export, resetHits',
+					'Route' => 'list, activateDemoMode, update, create, delete, deleteAll, htaccess, log, pageNotFoundHandling, export, resetHits',
 				],
 				[
 					'access' => 'user,group',
-- 
GitLab