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