[TASK] Re-work/simplify copyright header in PHP files - Part 9
[Packages/TYPO3.CMS.git] / typo3 / sysext / rsaauth / Classes / Storage / SplitStorage.php
1 <?php
2 namespace TYPO3\CMS\Rsaauth\Storage;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 /**
17 * This class contains a "split" storage for the data. It keeps part of the data
18 * in the database, part in the database.
19 *
20 * @author Dmitry Dulepov <dmitry@typo3.org>
21 */
22 class SplitStorage extends \TYPO3\CMS\Rsaauth\Storage\AbstractStorage {
23
24 /**
25 * Creates an instance of this class. It checks and initializes PHP
26 * sessions if necessary.
27 */
28 public function __construct() {
29 if (session_id() === '') {
30 session_start();
31 }
32 }
33
34 /**
35 * Obtains a key from the database
36 *
37 * @return string The key or NULL
38 * @see \TYPO3\CMS\Rsaauth\Storage\AbstractStorage::get()
39 */
40 public function get() {
41 $result = NULL;
42 list($keyId, $keyPart1) = $_SESSION['tx_rsaauth_key'];
43 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($keyId)) {
44 // Remove expired keys (more than 30 minutes old)
45 $GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_rsaauth_keys', 'crdate<' . ($GLOBALS['EXEC_TIME'] - 30 * 60));
46 // Get our value
47 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('key_value', 'tx_rsaauth_keys', 'uid=' . $keyId);
48 if (is_array($row)) {
49 $result = $keyPart1 . $row['key_value'];
50 }
51 }
52 return $result;
53 }
54
55 /**
56 * Adds a key to the storage or removes existing key
57 *
58 * @param string $key The key
59 * @return void
60 * @see \TYPO3\CMS\Rsaauth\Storage\AbstractStorage::put()
61 */
62 public function put($key) {
63 if ($key == NULL) {
64 // Remove existing key
65 list($keyId) = $_SESSION['tx_rsaauth_key'];
66 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($keyId)) {
67 $GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_rsaauth_keys', 'uid=' . $keyId);
68 unset($_SESSION['tx_rsaauth_key']);
69 }
70 } else {
71 // Add key
72 // Get split point. First part is always smaller than the second
73 // because it goes to the file system
74 $keyLength = strlen($key);
75 $splitPoint = rand((int)($keyLength / 10), (int)($keyLength / 2));
76 // Get key parts
77 $keyPart1 = substr($key, 0, $splitPoint);
78 $keyPart2 = substr($key, $splitPoint);
79 // Store part of the key in the database
80 //
81 // Notice: we may not use TCEmain below to insert key part into the
82 // table because TCEmain requires a valid BE user!
83 $time = $GLOBALS['EXEC_TIME'];
84 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_rsaauth_keys', array(
85 'pid' => 0,
86 'crdate' => $time,
87 'key_value' => $keyPart2
88 ));
89 $keyId = $GLOBALS['TYPO3_DB']->sql_insert_id();
90 // Store another part in session
91 $_SESSION['tx_rsaauth_key'] = array($keyId, $keyPart1);
92 }
93 // Remove expired keys (more than 30 minutes old)
94 $GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_rsaauth_keys', 'crdate<' . ($GLOBALS['EXEC_TIME'] - 30 * 60));
95 }
96
97 }