1
|
<?php
|
2
|
/**
|
3
|
*
|
4
|
* @category framework
|
5
|
* @package SecureForm
|
6
|
* @author Independend-Software-Team
|
7
|
* @author WebsiteBaker Project
|
8
|
* @copyright 2004-2009, Ryan Djurovich
|
9
|
* @copyright 2009-2011, Website Baker Org. e.V.
|
10
|
* @link http://www.websitebaker2.org/
|
11
|
* @license http://www.gnu.org/licenses/gpl.html
|
12
|
* @platform WebsiteBaker 2.8.x
|
13
|
* @requirements PHP 5.2.2 and higher
|
14
|
* @version $Id: SecureForm.php 1561 2012-01-05 11:56:34Z Luisehahne $
|
15
|
* @filesource $HeadURL: svn://isteam.dynxs.de/wb-archiv/tags/2.8.3/wb/framework/SecureForm.php $
|
16
|
* @lastmodified $Date: 2012-01-05 12:56:34 +0100 (Thu, 05 Jan 2012) $
|
17
|
* @description
|
18
|
*/
|
19
|
/* -------------------------------------------------------- */
|
20
|
// Must include code to stop this file being accessed directly
|
21
|
if(!defined('WB_PATH')) {
|
22
|
require_once(dirname(__FILE__).'/globalExceptionHandler.php');
|
23
|
throw new IllegalFileException();
|
24
|
}
|
25
|
/* -------------------------------------------------------- */
|
26
|
|
27
|
class SecureForm {
|
28
|
|
29
|
const FRONTEND = 0;
|
30
|
const BACKEND = 1;
|
31
|
|
32
|
|
33
|
private $_FTAN = '';
|
34
|
private $_IDKEYs = array('0'=>'0');
|
35
|
private $_ftan_name = '';
|
36
|
private $_idkey_name = '';
|
37
|
private $_salt = '';
|
38
|
private $_fingerprint = '';
|
39
|
|
40
|
/* Construtor */
|
41
|
protected function __construct($mode = self::FRONTEND)
|
42
|
{
|
43
|
$this->_FTAN = '';
|
44
|
$this->_salt = $this->_generate_salt();
|
45
|
$this->_fingerprint = $this->_generate_fingerprint();
|
46
|
// generate names for session variables
|
47
|
$this->_ftan_name =
|
48
|
substr($this->_fingerprint, -(16 + hexdec($this->_fingerprint[0])), 16);
|
49
|
// make sure there is a alpha-letter at first position
|
50
|
$this->_ftan_name = $this->_makeFirst2Letter($this->_ftan_name);
|
51
|
$this->_idkey_name =
|
52
|
substr($this->_fingerprint, hexdec($this->_fingerprint[strlen($this->_fingerprint)-1]), 16);
|
53
|
// make sure there is a alpha-letter at first position
|
54
|
$this->_idkey_name = $this->_makeFirst2Letter($this->_idkey_name);
|
55
|
// takeover id_keys from session if available
|
56
|
if(isset($_SESSION[$this->_idkey_name]) && is_array($_SESSION[$this->_idkey_name]))
|
57
|
{
|
58
|
$this->_IDKEYs = $_SESSION[$this->_idkey_name];
|
59
|
}else{
|
60
|
$this->_IDKEYs = array('0'=>'0');
|
61
|
$_SESSION[$this->_idkey_name] = $this->_IDKEYs;
|
62
|
}
|
63
|
}
|
64
|
|
65
|
private function _makeFirst2Letter($string)
|
66
|
{
|
67
|
$string[0] = dechex(10 + (hexdec($string[0]) % 5));
|
68
|
return $string;
|
69
|
}
|
70
|
|
71
|
private function _generate_salt()
|
72
|
{
|
73
|
if(function_exists('microtime'))
|
74
|
{
|
75
|
list($usec, $sec) = explode(" ", microtime());
|
76
|
$salt = (string)((float)$usec + (float)$sec);
|
77
|
}else{
|
78
|
$salt = (string)time();
|
79
|
}
|
80
|
$salt = (string)rand(10000, 99999) . $salt . (string)rand(10000, 99999);
|
81
|
return md5($salt);
|
82
|
}
|
83
|
|
84
|
private function _generate_fingerprint()
|
85
|
{
|
86
|
$usedOctets = ( defined('FINGERPRINT_WITH_IP_OCTETS') ) ? (intval(FINGERPRINT_WITH_IP_OCTETS) % 5) : 2;
|
87
|
// server depending values
|
88
|
$fingerprint = '';
|
89
|
$fingerprint .= ( isset($_SERVER['SERVER_SIGNATURE']) ) ? $_SERVER['SERVER_SIGNATURE'] : '2';
|
90
|
$fingerprint .= ( isset($_SERVER['SERVER_SOFTWARE']) ) ? $_SERVER['SERVER_SOFTWARE'] : '3';
|
91
|
$fingerprint .= ( isset($_SERVER['SERVER_NAME']) ) ? $_SERVER['SERVER_NAME'] : '5';
|
92
|
$serverIp = ( isset($_SERVER['SERVER_ADDR']) ) ? $_SERVER['SERVER_ADDR'] : '';
|
93
|
if(($serverIp != '') && ($usedOctets > 0)){
|
94
|
$ip = explode('.', $serverIp);
|
95
|
while(sizeof($ip) > $usedOctets) { array_pop($ip); }
|
96
|
$fingerprint .= implode('.', $ip);
|
97
|
}else {
|
98
|
$fingerprint .= '7';
|
99
|
}
|
100
|
$fingerprint .= ( isset($_SERVER['SERVER_PORT']) ) ? $_SERVER['SERVER_PORT'] : '11';
|
101
|
$fingerprint .= ( isset($_SERVER['SERVER_ADMIN']) ) ? $_SERVER['SERVER_ADMIN'] : '13';
|
102
|
$fingerprint .= __FILE__;
|
103
|
$fingerprint .= PHP_VERSION;
|
104
|
// client depending values
|
105
|
$fingerprint .= ( isset($_SERVER['HTTP_USER_AGENT']) ) ? $_SERVER['HTTP_USER_AGENT'] : '19';
|
106
|
// $usedOctets = ( defined('FINGERPRINT_WITH_IP_OCTETS') ) ? (intval(FINGERPRINT_WITH_IP_OCTETS) % 5) : 2;
|
107
|
$clientIp = ( isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '' );
|
108
|
if(($clientIp != '') && ($usedOctets > 0)){
|
109
|
$ip = explode('.', $clientIp);
|
110
|
while(sizeof($ip) > $usedOctets) { array_pop($ip); }
|
111
|
$clientIp = implode('.', $ip);
|
112
|
}else {
|
113
|
$clientIp = '23';
|
114
|
}
|
115
|
$fingerprint = md5($fingerprint.$clientIp);
|
116
|
$lastDigit = hexdec($fingerprint[strlen($fingerprint)-1]);
|
117
|
if ((hexdec($fingerprint[0]) + $lastDigit) == 16) {
|
118
|
$fingerprint[strlen($fingerprint)-1] = dechex(($lastDigit + 6) % 16);
|
119
|
}
|
120
|
return $fingerprint;
|
121
|
}
|
122
|
|
123
|
private function _calcFtan($tanPart)
|
124
|
{
|
125
|
$ftan = md5($tanPart . $this->_fingerprint);
|
126
|
$name = substr($ftan, -(16 + hexdec($ftan[0])), 16);
|
127
|
$name = $this->_makeFirst2Letter($name);
|
128
|
$value = substr($ftan, hexdec($ftan[strlen($ftan)-1]), 16);
|
129
|
return array( $name, $value);
|
130
|
}
|
131
|
/**
|
132
|
* creates Formular transactionnumbers for unique use
|
133
|
*
|
134
|
* @return void
|
135
|
* requirements: an active session must be available
|
136
|
*/
|
137
|
final protected function createFTAN()
|
138
|
{
|
139
|
if( $this->_FTAN == '')
|
140
|
{ // if no FTAN exists, create new one from time and salt
|
141
|
$this->_FTAN = md5($this->_fingerprint.$this->_salt);
|
142
|
$_SESSION[$this->_ftan_name] = $this->_FTAN; // store FTAN into session
|
143
|
}
|
144
|
}
|
145
|
/*
|
146
|
* returns the current FTAN
|
147
|
* @access public
|
148
|
* @param bool $mode: true or POST returns a complete prepared, hidden HTML-Input-Tag (default)
|
149
|
* false or GET returns an GET argument 'key=value'
|
150
|
* @return mixed: array or string
|
151
|
*/
|
152
|
final public function getFTAN( $mode = 'POST')
|
153
|
{
|
154
|
$ftan = $this->_calcFtan($this->_FTAN);
|
155
|
if((is_string($mode) && strtolower($mode) == 'post') || ($mode === true))
|
156
|
{ // by default return a complete, hidden <input>-tag
|
157
|
return '<input type="hidden" name="'.$ftan[0].'" value="'.$ftan[1].'" title="" alt="" />';
|
158
|
}else{ // return an string with GET params (FTAN0=FTAN1)
|
159
|
return $ftan[0].'='.$ftan[1];
|
160
|
}
|
161
|
}
|
162
|
|
163
|
/*
|
164
|
* checks received form-transactionnumbers against session-stored one
|
165
|
* @access public
|
166
|
* @param string $mode: requestmethode POST(default) or GET
|
167
|
* @return bool: true if numbers matches against stored ones
|
168
|
*
|
169
|
* requirements: an active session must be available
|
170
|
* this check will prevent from multiple sending a form. history.back() also will never work
|
171
|
*/
|
172
|
final public function checkFTAN( $mode = 'POST')
|
173
|
{
|
174
|
$retval = false;
|
175
|
if(isset($_SESSION[$this->_ftan_name]))
|
176
|
{
|
177
|
if( $_SESSION[$this->_ftan_name] && (strlen($_SESSION[$this->_ftan_name]) == strlen(md5('dummy'))))
|
178
|
{
|
179
|
$ftan = $this->_calcFtan($_SESSION[$this->_ftan_name]);
|
180
|
unset($_SESSION[$this->_ftan_name]);
|
181
|
$mode = (strtoupper($mode) != 'POST' ? '_GET' : '_POST');
|
182
|
if( isset($GLOBALS[$mode][$ftan[0]]))
|
183
|
{
|
184
|
$retval = ($GLOBALS[$mode][$ftan[0]] == $ftan[1]);
|
185
|
unset($GLOBALS[$mode][$ftan[0]]);
|
186
|
}
|
187
|
}
|
188
|
}
|
189
|
return $retval;
|
190
|
}
|
191
|
|
192
|
/*
|
193
|
* save values in session and returns a ID-key
|
194
|
* @access public
|
195
|
* @param mixed $value: the value for witch a key shall be generated and memorized
|
196
|
* @return string: a MD5-Key to use instead of the real value
|
197
|
*
|
198
|
* @requirements: an active session must be available
|
199
|
* @description: IDKEY can handle string/numeric/array - vars. Each key is a
|
200
|
*/
|
201
|
final public function getIDKEY($value)
|
202
|
{
|
203
|
if( is_array($value) == true )
|
204
|
{ // serialize value, if it's an array
|
205
|
$value = serialize($value);
|
206
|
}
|
207
|
// crypt value with salt into md5-hash
|
208
|
// and return a 16-digit block from random start position
|
209
|
$key = substr( md5($this->_salt.(string)$value), rand(0,15), 16);
|
210
|
do{ // loop while key/value isn't added
|
211
|
if( !array_key_exists($key, $this->_IDKEYs) )
|
212
|
{ // the key is unique, so store it in list
|
213
|
$this->_IDKEYs[$key] = $value;
|
214
|
break;
|
215
|
}else {
|
216
|
// if key already exist, increment the last five digits until the key is unique
|
217
|
$key = substr($key, 0, -5).dechex(('0x'.substr($key, -5)) + 1);
|
218
|
}
|
219
|
}while(0);
|
220
|
// store key/value-pairs into session
|
221
|
$_SESSION[$this->_idkey_name] = $this->_IDKEYs;
|
222
|
return $key;
|
223
|
}
|
224
|
|
225
|
/*
|
226
|
* search for key in session and returns the original value
|
227
|
* @access public
|
228
|
* @param string $fieldname: name of the POST/GET-Field containing the key or hex-key itself
|
229
|
* @param mixed $default: returnvalue if key not exist (default 0)
|
230
|
* @param string $request: requestmethode can be POST or GET or '' (default POST)
|
231
|
* @return mixed: the original value (string, numeric, array) or DEFAULT if request fails
|
232
|
*
|
233
|
* @requirements: an active session must be available
|
234
|
* @description: each IDKEY can be checked only once. Unused Keys stay in list until the
|
235
|
* session is destroyed.
|
236
|
*/
|
237
|
final public function checkIDKEY( $fieldname, $default = 0, $request = 'POST' )
|
238
|
{
|
239
|
$return_value = $default; // set returnvalue to default
|
240
|
switch( strtoupper($request) )
|
241
|
{
|
242
|
case 'POST':
|
243
|
$key = isset($_POST[$fieldname]) ? $_POST[$fieldname] : $fieldname;
|
244
|
break;
|
245
|
case 'GET':
|
246
|
$key = isset($_GET[$fieldname]) ? $_GET[$fieldname] : $fieldname;
|
247
|
break;
|
248
|
default:
|
249
|
$key = $fieldname;
|
250
|
}
|
251
|
|
252
|
if( preg_match('/[0-9a-f]{16}$/', $key) )
|
253
|
{ // key must be a 16-digit hexvalue
|
254
|
if( array_key_exists($key, $this->_IDKEYs))
|
255
|
{ // check if key is stored in IDKEYs-list
|
256
|
$return_value = $this->_IDKEYs[$key]; // get stored value
|
257
|
unset($this->_IDKEYs[$key]); // remove from list to prevent multiuse
|
258
|
$_SESSION[$this->_idkey_name] = $this->_IDKEYs; // save modified list into session again
|
259
|
if( preg_match('/.*(?<!\{).*(\d:\{.*;\}).*(?!\}).*/', $return_value) )
|
260
|
{ // if value is a serialized array, then deserialize it
|
261
|
$return_value = unserialize($return_value);
|
262
|
}
|
263
|
}
|
264
|
}
|
265
|
return $return_value;
|
266
|
}
|
267
|
|
268
|
/* @access public
|
269
|
* @return void
|
270
|
*
|
271
|
* @requirements: an active session must be available
|
272
|
* @description: remove all entries from IDKEY-Array
|
273
|
*
|
274
|
*/
|
275
|
final public function clearIDKEY()
|
276
|
{
|
277
|
$this->_IDKEYs = array('0'=>'0');
|
278
|
}
|
279
|
}
|