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