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