[TASK] Remove function index
[Packages/TYPO3.CMS.git] / typo3 / sysext / rsaauth / sv1 / storage / class.tx_rsaauth_split_storage.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 Dmitry Dulepov <dmitry@typo3.org>
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 require_once(t3lib_extMgm::extPath('rsaauth', 'sv1/storage/class.tx_rsaauth_abstract_storage.php'));
26
27 /**
28 * This class contains a "split" storage for the data. It keeps part of the data
29 * in the database, part in the database.
30 *
31 * @author Dmitry Dulepov <dmitry@typo3.org>
32 * @package TYPO3
33 * @subpackage tx_rsaauth
34 */
35 class tx_rsaauth_split_storage extends tx_rsaauth_abstract_storage {
36
37 /**
38 * Creates an instance of this class. It checks and initializes PHP
39 * sessions if necessary.
40 *
41 * @return void
42 */
43 public function __construct() {
44 if (!isset($_SESSION) || !is_array($_SESSION)) {
45 session_start();
46 }
47 }
48
49 /**
50 * Obtains a key from the database
51 *
52 * @return string The key or NULL
53 * @see tx_rsaauth_abstract_storage::get()
54 */
55 public function get() {
56 $result = NULL;
57
58 list($keyId, $keyPart1) = $_SESSION['tx_rsaauth_key'];
59 if (t3lib_div::testInt($keyId)) {
60
61 // Remove expired keys (more than 30 minutes old)
62 $GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_rsaauth_keys',
63 'crdate<' . ($GLOBALS['EXEC_TIME'] - 30 * 60));
64
65 // Get our value
66 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('key_value',
67 'tx_rsaauth_keys', 'uid=' . $keyId);
68 if (is_array($row)) {
69 $result = $keyPart1 . $row['key_value'];
70 }
71 }
72 return $result;
73 }
74
75 /**
76 * Adds a key to the storage or removes existing key
77 *
78 * @param string $key The key
79 * @return void
80 * @see tx_rsaauth_abstract_storage::put()
81 */
82 public function put($key) {
83 if ($key == NULL) {
84 // Remove existing key
85 list($keyId) = $_SESSION['tx_rsaauth_key'];
86
87 if (t3lib_div::testInt($keyId)) {
88 $GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_rsaauth_keys',
89 'uid=' . $keyId);
90 unset($_SESSION['tx_rsaauth_key']);
91 }
92 }
93 else {
94 // Add key
95
96 // Get split point. First part is always smaller than the second
97 // because it goes to the file system
98 $keyLength = strlen($key);
99 $splitPoint = rand(intval($keyLength/10), intval($keyLength/2));
100
101 // Get key parts
102 $keyPart1 = substr($key, 0, $splitPoint);
103 $keyPart2 = substr($key, $splitPoint);
104
105 // Store part of the key in the database
106 //
107 // Notice: we may not use TCEmain below to insert key part into the
108 // table because TCEmain requires a valid BE user!
109 $time = $GLOBALS['EXEC_TIME'];
110 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_rsaauth_keys', array(
111 'pid' => 0,
112 'crdate' => $time,
113 'key_value' => $keyPart2
114 ));
115 $keyId = $GLOBALS['TYPO3_DB']->sql_insert_id();
116
117 // Store another part in session
118 $_SESSION['tx_rsaauth_key'] = array($keyId, $keyPart1);
119 }
120
121 // Remove expired keys (more than 30 minutes old)
122 $GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_rsaauth_keys',
123 'crdate<' . ($GLOBALS['EXEC_TIME'] - 30 * 60));
124 }
125 }
126
127 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/rsaauth/sv1/storage/class.tx_rsaauth_split_storage.php'])) {
128 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/rsaauth/sv1/storage/class.tx_rsaauth_split_storage.php']);
129 }
130
131 ?>