TYPO3  7.6
FormProtectionFactory.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\FormProtection;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
22 
42 {
48  protected static $instances = array();
49 
53  private function __construct()
54  {
55  }
56 
67  public static function get($className = 'default')
68  {
69  if (isset(self::$instances[$className])) {
70  return self::$instances[$className];
71  }
72  if ($className === 'default') {
73  $classNameAndConstructorArguments = self::getClassNameAndConstructorArgumentsByState();
74  } else {
75  $classNameAndConstructorArguments = func_get_args();
76  }
77  self::$instances[$className] = self::createInstance($classNameAndConstructorArguments);
78  return self::$instances[$className];
79  }
80 
87  protected static function getClassNameAndConstructorArgumentsByState()
88  {
89  switch (true) {
90  case self::isInstallToolSession():
91  $classNameAndConstructorArguments = [
92  InstallToolFormProtection::class
93  ];
94  break;
95  case self::isFrontendSession():
96  $classNameAndConstructorArguments = [
97  FrontendFormProtection::class,
98  $GLOBALS['TSFE']->fe_user
99  ];
100  break;
101  case self::isBackendSession():
102  $classNameAndConstructorArguments = [
103  BackendFormProtection::class,
104  $GLOBALS['BE_USER'],
105  GeneralUtility::makeInstance(Registry::class),
106  self::getMessageClosure(
107  $GLOBALS['LANG'],
108  GeneralUtility::makeInstance(FlashMessageService::class)->getMessageQueueByIdentifier(),
109  (bool)(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX)
110  )
111  ];
112  break;
113  default:
114  $classNameAndConstructorArguments = [
115  DisabledFormProtection::class
116  ];
117  }
118  return $classNameAndConstructorArguments;
119  }
120 
126  protected static function isInstallToolSession()
127  {
128  return defined('TYPO3_enterInstallScript') && TYPO3_enterInstallScript;
129  }
130 
136  protected static function isBackendSession()
137  {
138  return isset($GLOBALS['BE_USER']) && $GLOBALS['BE_USER'] instanceof \TYPO3\CMS\Core\Authentication\BackendUserAuthentication && isset($GLOBALS['BE_USER']->user['uid']);
139  }
140 
146  protected static function isFrontendSession()
147  {
148  return TYPO3_MODE === 'FE' && is_object($GLOBALS['TSFE']) && $GLOBALS['TSFE']->fe_user instanceof \TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication && isset($GLOBALS['TSFE']->fe_user->user['uid']);
149  }
150 
158  public static function getMessageClosure(LanguageService $languageService, FlashMessageQueue $messageQueue, $isAjaxCall)
159  {
160  return function () use ($languageService, $messageQueue, $isAjaxCall) {
162  $flashMessage = GeneralUtility::makeInstance(
163  \TYPO3\CMS\Core\Messaging\FlashMessage::class,
164  $languageService->sL('LLL:EXT:lang/locallang_core.xlf:error.formProtection.tokenInvalid'),
165  '',
166  \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR,
167  !$isAjaxCall
168  );
169  $messageQueue->enqueue($flashMessage);
170  };
171  }
172 
181  protected static function createInstance(array $classNameAndConstructorArguments)
182  {
183  $className = $classNameAndConstructorArguments[0];
184  if (!class_exists($className)) {
185  throw new \InvalidArgumentException('$className must be the name of an existing class, but ' . 'actually was "' . $className . '".', 1285352962);
186  }
187  $instance = call_user_func_array([\TYPO3\CMS\Core\Utility\GeneralUtility::class, 'makeInstance'], $classNameAndConstructorArguments);
188  if (!$instance instanceof AbstractFormProtection) {
189  throw new \InvalidArgumentException('$className must be a subclass of ' . AbstractFormProtection::class . ', but actually was "' . $className . '".', 1285353026);
190  }
191  return $instance;
192  }
193 
205  public static function set($className, AbstractFormProtection $instance)
206  {
207  self::$instances[$className] = $instance;
208  }
209 
217  public static function purgeInstances()
218  {
219  foreach (self::$instances as $key => $instance) {
220  unset(self::$instances[$key]);
221  }
222  }
223 }