|
TYPO3 API
SVNRelease
|
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 2010-2011 Oliver Klee <typo3-coding@oliverklee.de> 00006 * All rights reserved 00007 * 00008 * This script is part of the TYPO3 project. The TYPO3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * 00017 * This script is distributed in the hope that it will be useful, 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 * GNU General Public License for more details. 00021 * 00022 * This copyright notice MUST APPEAR in all copies of the script! 00023 ***************************************************************/ 00024 00025 /** 00026 * Class t3lib_formprotection_Factory. 00027 * 00028 * This class creates and manages instances of the various form protection 00029 * classes. 00030 * 00031 * This class provides only static methods. It can not be instantiated. 00032 * 00033 * Usage for the back-end form protection: 00034 * 00035 * <pre> 00036 * $formProtection = t3lib_formprotection_Factory::get( 00037 * 't3lib_formProtection_BackEnd' 00038 * ); 00039 * </pre> 00040 * 00041 * Usage for the install tool form protection: 00042 * 00043 * <pre> 00044 * $formProtection = t3lib_formprotection_Factory::get( 00045 * 'tx_install_formprotection' 00046 * ); 00047 * $formProtection->injectInstallTool($this); 00048 * </pre> 00049 * 00050 * $Id$ 00051 * 00052 * @package TYPO3 00053 * @subpackage t3lib 00054 * 00055 * @author Oliver Klee <typo3-coding@oliverklee.de> 00056 * @author Ernesto Baschny <ernst@cron-it.de> 00057 */ 00058 final class t3lib_formprotection_Factory { 00059 /** 00060 * created instances of form protections using the type as array key 00061 * 00062 * @var array<t3lib_formProtectionAbstract> 00063 */ 00064 protected static $instances = array(); 00065 00066 /** 00067 * Private constructor to prevent instantiation. 00068 */ 00069 private function __construct() { 00070 } 00071 00072 /** 00073 * Gets a form protection instance for the requested class $className. 00074 * 00075 * If there already is an existing instance of the requested $className, the 00076 * existing instance will be returned. 00077 * 00078 * @param string $className 00079 * the name of the class for which to return an instance, must be 00080 * "t3lib_formProtection_BackEnd" or "t3lib_formprotection_InstallToolFormProtection" 00081 * 00082 * @return t3lib_formprotection_Abstract the requested instance 00083 */ 00084 public static function get($className = NULL) { 00085 if ($className === NULL) { 00086 $className = self::getClassNameByState(); 00087 } 00088 if (!isset(self::$instances[$className])) { 00089 self::createAndStoreInstance($className); 00090 } 00091 return self::$instances[$className]; 00092 } 00093 00094 /** 00095 * Returns the classname depending on TYPO3_MODE and 00096 * active backend session. 00097 * 00098 * @return string 00099 */ 00100 protected static function getClassNameByState() { 00101 switch (true) { 00102 case self::isInstallToolSession(): 00103 $className = 't3lib_formprotection_InstallToolFormProtection'; 00104 break; 00105 case self::isBackendSession(): 00106 $className = 't3lib_formprotection_BackendFormProtection'; 00107 break; 00108 case self::isFrontendSession(): 00109 default: 00110 $className = 't3lib_formprotection_DisabledFormProtection'; 00111 } 00112 return $className; 00113 } 00114 00115 /** 00116 * Check if we are in the install tool 00117 * 00118 * @return boolean 00119 */ 00120 protected static function isInstallToolSession() { 00121 return (defined(TYPO3_enterInstallScript) && TYPO3_enterInstallScript); 00122 } 00123 00124 /** 00125 * Checks if a user is logged in and the session is active. 00126 * 00127 * @return boolean 00128 */ 00129 protected static function isBackendSession() { 00130 return (isset($GLOBALS['BE_USER']) && 00131 $GLOBALS['BE_USER'] instanceof t3lib_beUserAuth && 00132 isset($GLOBALS['BE_USER']->user['uid']) && 00133 !(TYPO3_MODE == 'FE') 00134 ); 00135 } 00136 00137 /** 00138 * Checks if a frontend user is logged in and the session is active. 00139 * 00140 * @return boolean 00141 */ 00142 protected static function isFrontendSession() { 00143 return (is_object($GLOBALS['TSFE']) && 00144 $GLOBALS['TSFE']->fe_user instanceof tslib_feUserAuth && 00145 isset($GLOBALS['TSFE']->fe_user->user['uid']) && 00146 (TYPO3_MODE == 'FE') 00147 ); 00148 } 00149 00150 /** 00151 * Creates an instace for the requested class $className 00152 * and stores it internally. 00153 * 00154 * @param string $className 00155 * the name of the class for which to return an instance, must be 00156 * "t3lib_formProtection_BackEnd" or "t3lib_formprotection_InstallToolFormProtection" 00157 * 00158 * @throws InvalidArgumentException 00159 */ 00160 protected static function createAndStoreInstance($className) { 00161 if (!class_exists($className, TRUE)) { 00162 throw new InvalidArgumentException( 00163 '$className must be the name of an existing class, but ' . 00164 'actually was "' . $className . '".', 00165 1285352962 00166 ); 00167 } 00168 00169 $instance = t3lib_div::makeInstance($className); 00170 if (!$instance instanceof t3lib_formprotection_Abstract) { 00171 throw new InvalidArgumentException( 00172 '$className must be a subclass of ' . 00173 't3lib_formprotection_Abstract, but actually was "' . 00174 $className . '".', 00175 1285353026 00176 ); 00177 } 00178 self::$instances[$className] = $instance; 00179 } 00180 00181 /** 00182 * Sets the instance that will be returned by get() for a specific class 00183 * name. 00184 * 00185 * Note: This function is intended for testing purposes only. 00186 * 00187 * @param string $className 00188 * the name of the class for which to set an instance, must be 00189 * "t3lib_formProtection_BackEnd" or "t3lib_formprotection_InstallToolFormProtection" 00190 * @param t3lib_formprotection_Abstract $instance 00191 * the instance to set 00192 * 00193 * @return void 00194 */ 00195 public static function set($className, t3lib_formprotection_Abstract $instance) { 00196 self::$instances[$className] = $instance; 00197 } 00198 00199 /** 00200 * Purges all existing instances. 00201 * 00202 * This function is particularly useful when cleaning up in unit testing. 00203 * 00204 * @return void 00205 */ 00206 public static function purgeInstances() { 00207 foreach (self::$instances as $key => $instance) { 00208 $instance->__destruct(); 00209 unset(self::$instances[$key]); 00210 } 00211 } 00212 } 00213 00214 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/formprotection/class.t3lib_formprotection_factory.php'])) { 00215 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/formprotection/class.t3lib_formprotection_factory.php']); 00216 } 00217 ?>
1.8.0