From 7139165bba730bcf33285eae470d932cdb4b9630 Mon Sep 17 00:00:00 2001
From: Fabian Galinski <fabian@sgalinski.de>
Date: Thu, 14 Sep 2017 20:42:26 +0200
Subject: [PATCH] [TASK] Code improvements

---
 Classes/Form/Element/RichTextElement.php |   8 +-
 Classes/Service/LicensingService.php     | 123 +++++++++++++++++++++++
 Configuration/Backend/AjaxRoutes.php     |   4 +
 3 files changed, 134 insertions(+), 1 deletion(-)
 create mode 100644 Classes/Service/LicensingService.php

diff --git a/Classes/Form/Element/RichTextElement.php b/Classes/Form/Element/RichTextElement.php
index 09935d7..dfbda5d 100644
--- a/Classes/Form/Element/RichTextElement.php
+++ b/Classes/Form/Element/RichTextElement.php
@@ -647,12 +647,18 @@ class RichTextElement extends AbstractFormElement {
 	 * @return string RTE initialization inline JavaScript code
 	 */
 	protected function getRteInitJsCode() {
+		$ajaxPingJavaScriptCode = '(function($) {
+			$(document).ready(function() {
+				$.get(TYPO3.settings.ajaxUrls[\'tinymce4_rte::ajaxPing\']);
+			});
+		})(TYPO3.jQuery);';
+
 		return 'if (typeof RTEarea === "undefined") {
 			RTEarea = new Object();
 			RTEarea[0] = new Object();
 			RTEarea[0].version = "' . $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['tinymce4_rte']['version'] . '";
 			RTEarea[0].editorUrl = "' . ExtensionManagementUtility::extRelPath('tinymce4_rte') . '";
-		}';
+		}' . $ajaxPingJavaScriptCode;
 	}
 
 	/**
diff --git a/Classes/Service/LicensingService.php b/Classes/Service/LicensingService.php
new file mode 100644
index 0000000..924c010
--- /dev/null
+++ b/Classes/Service/LicensingService.php
@@ -0,0 +1,123 @@
+<?php
+
+namespace SGalinski\Tinymce4Rte\Service;
+
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) sgalinski Internet Services (https://www.sgalinski.de)
+ *
+ *  All rights reserved
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project 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.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Class SGalinski\SgRoutes\Service\LicensingService
+ */
+class LicensingService {
+	/**
+	 * Licensing Service Url
+	 */
+	const URL = 'https://www.sgalinski.de/?eID=sgLicensing';
+
+	/**
+	 * The key name of the uc array inside of the backend user.
+	 */
+	const BACKEND_USER_UC_KEY = 'tinymce4_rte_pinged';
+
+	/**
+	 * Licensing Service Url
+	 */
+	const EXTENSION_KEY = 'tinymce4_rte';
+
+	/** @var bool|NULL */
+	private static $isLicenseKeyValid;
+
+	/**
+	 * @return boolean
+	 */
+	public static function checkKey(): bool {
+		if (static::$isLicenseKeyValid === NULL) {
+			static::$isLicenseKeyValid = FALSE;
+			$configuration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][self::EXTENSION_KEY], [FALSE]);
+			if (isset($configuration['key']) && $key = trim($configuration['key'])) {
+				static::$isLicenseKeyValid = (bool) preg_match('/^([A-Z\d]{6}-?){4}$/', $key);
+			}
+		}
+
+		return static::$isLicenseKeyValid;
+	}
+
+	/**
+	 * Licensing Service ping
+	 *
+	 * @param boolean $returnUrl
+	 * @return string
+	 */
+	public static function ping($returnUrl = FALSE): string {
+		try {
+			$configuration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][self::EXTENSION_KEY], [FALSE]);
+			$key = '';
+			if (isset($configuration['key'])) {
+				$key = trim($configuration['key']);
+			}
+			$params = [
+				'extension' => self::EXTENSION_KEY,
+				'host' => GeneralUtility::getIndpEnv('HTTP_HOST'),
+				// @todo Enable this, when inserting a licensing.
+				//'key' => $key
+			];
+			$params = http_build_query($params);
+			$pingUrl = self::URL;
+			$pingUrl .= $params !== '' ? (strpos($pingUrl, '?') === FALSE ? '?' : '&') . $params : '';
+			if ($returnUrl) {
+				return $pingUrl;
+			}
+
+			GeneralUtility::getUrl($pingUrl);
+		} catch (\Exception $exception) {
+		}
+
+		return '';
+	}
+
+	/**
+	 * Generates a random password string based on the configured password policies.
+	 *
+	 * @param ServerRequestInterface $request
+	 * @param ResponseInterface $response
+	 * @return ResponseInterface
+	 * @throws \InvalidArgumentException
+	 */
+	public function ajaxPing(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {
+		/** @var BackendUserAuthentication $backendUser */
+		$backendUser = $GLOBALS['BE_USER'];
+		$backendUserUC = $backendUser->uc;
+		if ($backendUser && !isset($backendUserUC[self::BACKEND_USER_UC_KEY])) {
+			$backendUserUC[self::BACKEND_USER_UC_KEY] = TRUE;
+			$backendUser->writeUC($backendUserUC);
+			self::ping();
+		}
+		return $response;
+	}
+}
diff --git a/Configuration/Backend/AjaxRoutes.php b/Configuration/Backend/AjaxRoutes.php
index 580549e..80e9444 100644
--- a/Configuration/Backend/AjaxRoutes.php
+++ b/Configuration/Backend/AjaxRoutes.php
@@ -13,4 +13,8 @@ return [
         'path' => '/rte/insert-image',
         'target' => \SGalinski\Tinymce4Rte\Controller\SelectImageController::class . '::buildImageMarkup',
     ],
+	'tinymce4_rte::ajaxPing' => [
+		'path' => '/tinymce4_rte/ajaxPing',
+		'target' => \SGalinski\Tinymce4Rte\Service\LicensingService::class . '::ajaxPing',
+	]
 ];
-- 
GitLab