[BUGFIX] Misspelled class name FrontendUserAuthtenication
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / FormProtection / FormProtectionFactory.php
1 <?php
2 namespace TYPO3\CMS\Core\FormProtection;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2011 Oliver Klee <typo3-coding@oliverklee.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Class t3lib_formprotection_Factory.
28 *
29 * This class creates and manages instances of the various form protection
30 * classes.
31 *
32 * This class provides only static methods. It can not be instantiated.
33 *
34 * Usage for the back-end form protection:
35 *
36 * <pre>
37 * $formProtection = t3lib_formprotection_Factory::get();
38 * </pre>
39 *
40 * Usage for the install tool form protection:
41 *
42 * <pre>
43 * $formProtection = t3lib_formprotection_Factory::get();
44 * $formProtection->injectInstallTool($this);
45 * </pre>
46 *
47 * @author Oliver Klee <typo3-coding@oliverklee.de>
48 * @author Ernesto Baschny <ernst@cron-it.de>
49 * @author Helmut Hummel <helmut.hummel@typo3.org>
50 */
51 class FormProtectionFactory {
52
53 /**
54 * created instances of form protections using the type as array key
55 *
56 * @var array<t3lib_formProtectionAbstract>
57 */
58 static protected $instances = array();
59
60 /**
61 * Private constructor to prevent instantiation.
62 */
63 private function __construct() {
64
65 }
66
67 /**
68 * Gets a form protection instance for the requested class $className.
69 *
70 * If there already is an existing instance of the requested $className, the
71 * existing instance will be returned. If no $className is provided, the factory
72 * detects the scope and returns the appropriate form protection object.
73 *
74 * @param string $className
75 * @return \TYPO3\CMS\Core\FormProtection\AbstractFormProtection the requested instance
76 */
77 static public function get($className = NULL) {
78 if ($className === NULL) {
79 $className = self::getClassNameByState();
80 }
81 if (!isset(self::$instances[$className])) {
82 self::createAndStoreInstance($className);
83 }
84 return self::$instances[$className];
85 }
86
87 /**
88 * Returns the class name depending on TYPO3_MODE and
89 * active backend session.
90 *
91 * @return string
92 */
93 static protected function getClassNameByState() {
94 switch (TRUE) {
95 case self::isInstallToolSession():
96 $className = 'TYPO3\\CMS\\Core\\FormProtection\\InstallToolFormProtection';
97 break;
98 case self::isBackendSession():
99 $className = 'TYPO3\\CMS\\Core\\FormProtection\\BackendFormProtection';
100 break;
101 case self::isFrontendSession():
102
103 default:
104 $className = 'TYPO3\\CMS\\Core\\FormProtection\\DisabledFormProtection';
105 }
106 return $className;
107 }
108
109 /**
110 * Check if we are in the install tool
111 *
112 * @return boolean
113 */
114 static protected function isInstallToolSession() {
115 return defined(TYPO3_enterInstallScript) && TYPO3_enterInstallScript;
116 }
117
118 /**
119 * Checks if a user is logged in and the session is active.
120 *
121 * @return boolean
122 */
123 static protected function isBackendSession() {
124 return isset($GLOBALS['BE_USER']) && $GLOBALS['BE_USER'] instanceof \TYPO3\CMS\Core\Authentication\BackendUserAuthentication && isset($GLOBALS['BE_USER']->user['uid']) && !(TYPO3_MODE === 'FE');
125 }
126
127 /**
128 * Checks if a frontend user is logged in and the session is active.
129 *
130 * @return boolean
131 */
132 static protected function isFrontendSession() {
133 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';
134 }
135
136 /**
137 * Creates an instance for the requested class $className
138 * and stores it internally.
139 *
140 * @param string $className
141 * @throws \InvalidArgumentException
142 */
143 static protected function createAndStoreInstance($className) {
144 if (!class_exists($className, TRUE)) {
145 throw new \InvalidArgumentException('$className must be the name of an existing class, but ' . 'actually was "' . $className . '".', 1285352962);
146 }
147 $instance = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($className);
148 if (!$instance instanceof \TYPO3\CMS\Core\FormProtection\AbstractFormProtection) {
149 throw new \InvalidArgumentException('$className must be a subclass of ' . 'TYPO3\\CMS\\Core\\FormProtection\\AbstractFormProtection, but actually was "' . $className . '".', 1285353026);
150 }
151 self::$instances[$className] = $instance;
152 }
153
154 /**
155 * Sets the instance that will be returned by get() for a specific class
156 * name.
157 *
158 * Note: This function is intended for testing purposes only.
159 *
160 * @access private
161 * @param string $className
162 * @param \TYPO3\CMS\Core\FormProtection\AbstractFormProtection $instance
163 * @return void
164 */
165 static public function set($className, \TYPO3\CMS\Core\FormProtection\AbstractFormProtection $instance) {
166 self::$instances[$className] = $instance;
167 }
168
169 /**
170 * Purges all existing instances.
171 *
172 * This function is particularly useful when cleaning up in unit testing.
173 *
174 * @return void
175 */
176 static public function purgeInstances() {
177 foreach (self::$instances as $key => $instance) {
178 $instance->__destruct();
179 unset(self::$instances[$key]);
180 }
181 }
182
183 }
184
185
186 ?>