<?php namespace SGalinski\SgMail\Controller; /*************************************************************** * 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 SGalinski\SgMail\Service\BackendService; use SGalinski\SgMail\Service\MailTemplateService; use SGalinski\SgMail\Service\TypoScriptSettingsService; use SGalinski\SgMail\Session\PhpSession; use TYPO3\CMS\Backend\Template\Components\DocHeaderComponent; use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Core\Cache\CacheManager; use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; use TYPO3\CMS\Core\Log\Logger; use TYPO3\CMS\Core\Messaging\FlashMessage; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\VersionNumberUtility; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException; use TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException; use TYPO3\CMS\Extbase\Object\ObjectManager; use TYPO3\CMS\Extbase\Utility\LocalizationUtility; /** * Controller for the configuration mode of the backend module */ class ConfigurationController extends ActionController { const DEFAULT_EXTENSION_KEY = 'sg_mail'; /** * DocHeaderComponent * * @var DocHeaderComponent */ protected $docHeaderComponent; /** * @var \SGalinski\SgMail\Session\PhpSession */ protected $session; /** * * @param string $selectedTemplate * @param string $selectedExtension * @param array $filters * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException */ public function indexAction($selectedTemplate = NULL, $selectedExtension = NULL, array $filters = []) { if ($this->request->hasArgument('message')) { $this->addFlashMessage($this->request->getArgument('message'), '', FlashMessage::ERROR); } $pageUid = (int) GeneralUtility::_GP('id'); $registerArray = BackendService::getNonBlacklistedTemplates($pageUid); if ($selectedTemplate === NULL || $selectedTemplate === '') { if (!empty($registerArray)) { $selectedExtension = \key($registerArray); } if (!empty($registerArray)) { $selectedTemplate = \key($registerArray[$selectedExtension]); } } // make docheader $this->docHeaderComponent = GeneralUtility::makeInstance(DocHeaderComponent::class); $pageInfo = BackendUtility::readPageAccess($pageUid, $GLOBALS['BE_USER']->getPagePermsClause(1)); if ($pageInfo === FALSE) { $pageInfo = ['uid' => $pageUid]; } $this->docHeaderComponent->setMetaInformation($pageInfo); BackendService::makeButtons($this->docHeaderComponent, $this->request); $this->view->assign('docHeader', $this->docHeaderComponent->docHeaderContent()); $this->view->assign('selectedTemplateFilter', $filters['filterTemplate']); $this->view->assign('selectedExtensionFilter', $filters['filterExtension']); $this->view->assign('typo3Version', VersionNumberUtility::convertVersionNumberToInteger(TYPO3_version)); $this->view->assign('selectedTemplateKey', $selectedTemplate); $this->view->assign('selectedExtensionKey', $selectedExtension); $this->view->assign('mode', 'editor'); } /** * Create the template or display errors that occured * * @throws StopActionException * @throws UnsupportedRequestTypeException * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException */ public function createAction() { if (!$this->$this->request->hasArgument('configuration')) { $this->redirect( 'index', 'Configuration', NULL, ['message' => LocalizationUtility::translate('backend.create_error', 'sg_mail')] ); } /** @var array $configuration */ $configuration = $this->request->getArgument('configuration'); $templateName = $configuration['templateName']; $csv = $configuration['csv']; $subject = $configuration['subject']; $description = $configuration['description']; // parse csv (,,,;,,,;) $markersCsv = str_getcsv($csv, ';'); $markers = []; foreach ($markersCsv as $markerCsv) { $rowArray = GeneralUtility::trimExplode(',', $markerCsv); if (!$rowArray[0]) { continue; } $markers[] = [ 'identifier' => $rowArray[0], 'value' => $rowArray[1], 'description' => $rowArray[2] ]; } // write the new Register.php file $this->writeRegisterFile($templateName, self::DEFAULT_EXTENSION_KEY, $markers, $subject, $description); // call register function in mail template service class MailTemplateService::registerExtensions(); // clear caches $this->clearCaches(); // store selected template & extension key in the session if (!($this->session instanceof PhpSession)) { $this->session = $this->objectManager->get(PhpSession::class); $this->session->setSessionKey('sg_mail_controller_session'); } else { $this->session->setSessionKey('sg_mail_controller_session'); } $this->session->setDataByKey('selectedTemplate', $templateName); $this->session->setDataByKey('selectedExtension', self::DEFAULT_EXTENSION_KEY); $this->redirect( 'index', 'Mail', NULL, ['message' => LocalizationUtility::translate('backend.create_message', 'sg_mail')] ); } /** * Write the mail registration file * * @TODO move this to the register service class when security update is merged. refactor in FormEditorController xclass as well * * @param string $templateKey * @param string $extensionKey * @param array $markers * @param string $subject * @param string $description */ private function writeRegisterFile($templateKey, $extensionKey, $markers, $subject, $description) { // get the location where registrations should be stored $configurationLocation = $this->getRegistrationPath(); $registerFolder = GeneralUtility::getFileAbsFileName( $configurationLocation ); // create folder GeneralUtility::mkdir($registerFolder); $registerFile = GeneralUtility::getFileAbsFileName( $registerFolder . '/' . $templateKey . '.php' ); // build the register array $newRegisterArray = [ 'extension_key' => $extensionKey, 'template_key' => $templateKey, 'description' => $description, 'subject' => $subject, 'markers' => [] ]; // add the markers for this template foreach ($markers as $marker) { $markerName = $marker['identifier']; $newRegisterArray['markers'][] = [ 'marker' => $markerName, 'type' => MailTemplateService::MARKER_TYPE_STRING, 'value' => $marker['value'], 'description' => $marker['description'] ]; } file_put_contents($registerFile, '<?php return ' . var_export($newRegisterArray, TRUE) . ';'); } /** * Returns the path to the configured location where automatic mail template registrations should be * * @TODO move this to the register service class when security update is merged. refactor in FormEditorController xclass as well * * @return string */ private function getRegistrationPath(): string { // get typoscript settings from sg mail /** @var TypoScriptSettingsService $typoScriptSettingsService */ $typoScriptSettingsService = $this->objectManager->get(TypoScriptSettingsService::class); $tsSettings = $typoScriptSettingsService->getSettings(0, 'tx_sgmail'); // get the location where automatic registrations should be stored return 'EXT:' . $tsSettings['mail']['configurationLocation'] . '/' . MailTemplateService::CONFIG_PATH; } /** * Clear the sgmail register cache * * @TODO move this to the register service class when security update is merged. refactor in FormEditorController xclass as well * * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException */ private function clearCaches() { $objectManager = GeneralUtility::makeInstance(ObjectManager::class); $cacheManager = $objectManager->get(CacheManager::class); /** @var FrontendInterface $cache */ $cache = $cacheManager->getCache(MailTemplateService::CACHE_NAME); /** @var FrontendInterface $cache */ $cache->flush(); } }