[TASK] FormEngine JS refactoring: copy JS files
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / FormProtection / FormProtectionFactory.php
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
17 /**
18 * This class creates and manages instances of the various form protection
19 * classes.
20 *
21 * This class provides only static methods. It can not be instantiated.
22 *
23 * Usage for the back-end form protection:
24 *
25 * <pre>
26 * $formProtection = \TYPO3\CMS\Core\FormProtection\FormProtectionFactory::get();
27 * </pre>
28 *
29 * Usage for the install tool form protection:
30 *
31 * <pre>
32 * $formProtection = \TYPO3\CMS\Core\FormProtection\FormProtectionFactory::get();
33 * </pre>
34 *
35 * @author Oliver Klee <typo3-coding@oliverklee.de>
36 * @author Ernesto Baschny <ernst@cron-it.de>
37 * @author Helmut Hummel <helmut.hummel@typo3.org>
38 */
39 class FormProtectionFactory {
40
41 /**
42 * created instances of form protections using the type as array key
43 *
44 * @var array<AbstracFormtProtection>
45 */
46 static protected $instances = array();
47
48 /**
49 * Private constructor to prevent instantiation.
50 */
51 private function __construct() {
52
53 }
54
55 /**
56 * Gets a form protection instance for the requested class $className.
57 *
58 * If there already is an existing instance of the requested $className, the
59 * existing instance will be returned. If no $className is provided, the factory
60 * detects the scope and returns the appropriate form protection object.
61 *
62 * @param string $className
63 * @return \TYPO3\CMS\Core\FormProtection\AbstractFormProtection the requested instance
64 */
65 static public function get($className = NULL) {
66 if ($className === NULL) {
67 $className = self::getClassNameByState();
68 }
69 if (!isset(self::$instances[$className])) {
70 self::createAndStoreInstance($className);
71 }
72 return self::$instances[$className];
73 }
74
75 /**
76 * Returns the class name depending on TYPO3_MODE and
77 * active backend session.
78 *
79 * @return string
80 */
81 static protected function getClassNameByState() {
82 switch (TRUE) {
83 case self::isInstallToolSession():
84 $className = \TYPO3\CMS\Core\FormProtection\InstallToolFormProtection::class;
85 break;
86 case self::isBackendSession():
87 $className = \TYPO3\CMS\Core\FormProtection\BackendFormProtection::class;
88 break;
89 case self::isFrontendSession():
90 default:
91 $className = \TYPO3\CMS\Core\FormProtection\DisabledFormProtection::class;
92 }
93 return $className;
94 }
95
96 /**
97 * Check if we are in the install tool
98 *
99 * @return bool
100 */
101 static protected function isInstallToolSession() {
102 return defined('TYPO3_enterInstallScript') && TYPO3_enterInstallScript;
103 }
104
105 /**
106 * Checks if a user is logged in and the session is active.
107 *
108 * @return bool
109 */
110 static protected function isBackendSession() {
111 return isset($GLOBALS['BE_USER']) && $GLOBALS['BE_USER'] instanceof \TYPO3\CMS\Core\Authentication\BackendUserAuthentication && isset($GLOBALS['BE_USER']->user['uid']);
112 }
113
114 /**
115 * Checks if a frontend user is logged in and the session is active.
116 *
117 * @return bool
118 */
119 static protected function isFrontendSession() {
120 return is_object($GLOBALS['TSFE']) && $GLOBALS['TSFE']->fe_user instanceof \TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication && isset($GLOBALS['TSFE']->fe_user->user['uid']) && TYPO3_MODE === 'FE';
121 }
122
123 /**
124 * Creates an instance for the requested class $className
125 * and stores it internally.
126 *
127 * @param string $className
128 * @throws \InvalidArgumentException
129 */
130 static protected function createAndStoreInstance($className) {
131 if (!class_exists($className, TRUE)) {
132 throw new \InvalidArgumentException('$className must be the name of an existing class, but ' . 'actually was "' . $className . '".', 1285352962);
133 }
134 $instance = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($className);
135 if (!$instance instanceof AbstractFormProtection) {
136 throw new \InvalidArgumentException('$className must be a subclass of ' . \TYPO3\CMS\Core\FormProtection\AbstractFormProtection::class . ', but actually was "' . $className . '".', 1285353026);
137 }
138 self::$instances[$className] = $instance;
139 }
140
141 /**
142 * Sets the instance that will be returned by get() for a specific class
143 * name.
144 *
145 * Note: This function is intended for testing purposes only.
146 *
147 * @access private
148 * @param string $className
149 * @param AbstractFormProtection $instance
150 * @return void
151 */
152 static public function set($className, AbstractFormProtection $instance) {
153 self::$instances[$className] = $instance;
154 }
155
156 /**
157 * Purges all existing instances.
158 *
159 * This function is particularly useful when cleaning up in unit testing.
160 *
161 * @return void
162 */
163 static public function purgeInstances() {
164 foreach (self::$instances as $key => $instance) {
165 $instance->__destruct();
166 unset(self::$instances[$key]);
167 }
168 }
169
170 }