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