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