2 /***************************************************************
5 * (c) 2010 Oliver Klee <typo3-coding@oliverklee.de>
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.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
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.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
26 * Class t3lib_formprotection_Abstract.
28 * This class provides protection against cross-site request forgery (XSRF/CSRF)
31 * For documentation on how to use this class, please see the documentation of
32 * the corresponding subclasses, e.g. t3lib_formprotection_BackendFormProtection.
39 * @author Oliver Klee <typo3-coding@oliverklee.de>
41 abstract class t3lib_formprotection_Abstract
{
43 * the maximum number of tokens that can exist at the same time
47 protected $maximumNumberOfTokens = 0;
50 * Valid tokens sorted from oldest to newest.
52 * [tokenId] => array(formName, formInstanceName)
56 protected $tokens = array();
59 * Constructor. Makes sure existing tokens are read and available for
62 public function __construct() {
63 $this->retrieveTokens();
67 * Frees as much memory as possible.
69 public function __destruct() {
70 $this->tokens
= array();
74 * Deletes all existing tokens and persists the (empty) token table.
76 * This function is intended to be called when a user logs on or off.
80 public function clean() {
81 $this->tokens
= array();
82 $this->persistTokens();
86 * Generates and stores a token for a form.
88 * Calling this function two times with the same parameters will create
89 * two valid, different tokens.
91 * Generating more tokens than $maximumNumberOfEntries will cause the oldest
92 * tokens to get dropped.
94 * Note: This function does not persist the tokens.
96 * @param string $formName
97 * the name of the form, for example a table name like "tt_content",
98 * or some other identifier like "install_tool_password", must not be
100 * @param string $action
101 * the name of the action of the form, for example "new", "delete" or
102 * "edit", may also be empty
103 * @param string $formInstanceName
104 * a string used to differentiate two instances of the same form,
105 * form example a record UID or a comma-separated list of UIDs,
108 * @return string the 32-character hex ID of the generated token
110 public function generateToken(
111 $formName, $action = '', $formInstanceName = ''
113 if ($formName == '') {
114 throw new InvalidArgumentException('$formName must not be empty.');
118 $tokenId = bin2hex(t3lib_div
::generateRandomBytes(16));
119 } while (isset($this->tokens
[$tokenId]));
121 $this->tokens
[$tokenId] = array(
122 'formName' => $formName,
124 'formInstanceName' => $formInstanceName,
126 $this->preventOverflow();
132 * Checks whether the token $tokenId is valid in the form $formName with
135 * A token is valid if $tokenId, $formName and $formInstanceName match and
136 * the token has not been used yet.
138 * Calling this function will mark the token $tokenId as invalud (if it
141 * So calling this function with the same parameters two times will return
142 * FALSE the second time.
144 * @param string $tokenId
145 * a form token to check, may also be empty or utterly misformed
146 * @param string $formName
147 * the name of the form to check, for example "tt_content",
148 * may also be empty or utterly misformed
149 * @param string $action
150 * the action of the form to check, for example "edit",
151 * may also be empty or utterly misformed
152 * @param string $formInstanceName
153 * the instance name of the form to check, for example "42" or "foo"
154 * or "31,42", may also be empty or utterly misformed
157 * TRUE if $tokenId, $formName, $action and $formInstanceName match
158 * and the token has not been used yet, FALSE otherwise
160 public function validateToken(
161 $tokenId, $formName, $action = '', $formInstanceName = ''
163 if (isset($this->tokens
[$tokenId])) {
164 $token = $this->tokens
[$tokenId];
165 $isValid = ($token['formName'] == $formName)
166 && ($token['action'] == $action)
167 && ($token['formInstanceName'] == $formInstanceName);
168 $this->dropToken($tokenId);
174 $this->createValidationErrorMessage();
181 * Creates or displayes an error message telling the user that the submitted
182 * form token is invalid.
184 * This function may also be empty if the validation error should be handled
189 abstract protected function createValidationErrorMessage();
192 * Retrieves all saved tokens.
194 * @return array<arrray>
195 * the saved tokens, will be empty if no tokens have been saved
197 abstract protected function retrieveTokens();
200 * Saves the tokens so that they can be used by a later incarnation of this
205 abstract public function persistTokens();
208 * Drops the token with the ID $tokenId.
210 * If there is no token with that ID, this function is a no-op.
212 * Note: This function does not persist the tokens.
214 * @param string $tokenId
215 * the 32-character ID of an existing token, must not be empty
219 protected function dropToken($tokenId) {
220 if (isset($this->tokens
[$tokenId])) {
221 unset($this->tokens
[$tokenId]);
226 * Checks whether the number of current tokens still is at most
227 * $this->maximumNumberOfTokens.
229 * If there are more tokens, the oldest tokens are removed until the number
230 * of tokens is low enough.
232 * Note: This function does not persist the tokens.
236 protected function preventOverflow() {
237 if (empty($this->tokens
)) {
241 while (count($this->tokens
) > $this->maximumNumberOfTokens
) {
242 reset($this->tokens
);
243 $this->dropToken(key($this->tokens
));