Added feature #16437: Introduce a form protection API (Thanks to the Security Team...
[Packages/TYPO3.CMS.git] / t3lib / formprotection / class.t3lib_formprotection_factory.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 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 static protected $instances = array();
65
66 /**
67 * Private constructor to prevent instantiation.
68 */
69 private function __construct() {}
70
71 /**
72 * Gets a form protection instance for the requested class $className.
73 *
74 * If there already is an existing instance of the requested $className, the
75 * existing instance will be returned.
76 *
77 * @param string $className
78 * the name of the class for which to return an instance, must be
79 * "t3lib_formProtection_BackEnd" or "t3lib_formprotection_InstallToolFormProtection"
80 *
81 * @return t3lib_formprotection_Abstract the requested instance
82 */
83 static public function get($className) {
84 if (!isset(self::$instances[$className])) {
85 if (!class_exists($className, TRUE)) {
86 throw new InvalidArgumentException(
87 '$className must be the name of an existing class, but ' .
88 'actually was "' . $className . '".',
89 1285352962
90 );
91 }
92
93 $instance = t3lib_div::makeInstance($className);
94 if (!$instance instanceof t3lib_formprotection_Abstract) {
95 throw new InvalidArgumentException(
96 '$className must be a subclass of ' .
97 't3lib_formprotection_Abstract, but actually was "' .
98 $className . '".',
99 1285353026
100 );
101 }
102 self::$instances[$className] = $instance;
103 }
104 return self::$instances[$className];
105 }
106
107 /**
108 * Sets the instance that will be returned by get() for a specific class
109 * name.
110 *
111 * Note: This function is intended for testing purposes only.
112 *
113 * @param string $className
114 * the name of the class for which to set an instance, must be
115 * "t3lib_formProtection_BackEnd" or "t3lib_formprotection_InstallToolFormProtection"
116 * @param t3lib_formprotection_Abstract $instance
117 * the instance to set
118 *
119 * @return void
120 */
121 static public function set($className, t3lib_formprotection_Abstract $instance) {
122 self::$instances[$className] = $instance;
123 }
124
125 /**
126 * Purges all existing instances.
127 *
128 * This function is particularly useful when cleaning up in unit testing.
129 *
130 * @return void
131 */
132 static public function purgeInstances() {
133 foreach (self::$instances as $key => $instance) {
134 $instance->__destruct();
135 unset(self::$instances[$key]);
136 }
137 }
138 }
139
140 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/formprotection/class.t3lib_formprotection_factory.php']) {
141 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/formprotection/class.t3lib_formprotection_factory.php']);
142 }
143 ?>