1 |
1368
|
Luisehahne
|
<?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 |
1373
|
Luisehahne
|
* @copyright 2009-2011, Website Baker Org. e.V.
|
10 |
1368
|
Luisehahne
|
* @link http://www.websitebaker2.org/
|
11 |
|
|
* @license http://www.gnu.org/licenses/gpl.html
|
12 |
|
|
* @platform WebsiteBaker 2.8.x
|
13 |
1374
|
Luisehahne
|
* @requirements PHP 5.2.2 and higher
|
14 |
1368
|
Luisehahne
|
* @version $Id$
|
15 |
|
|
* @filesource $HeadURL$
|
16 |
|
|
* @lastmodified $Date$
|
17 |
1498
|
DarkViper
|
* @description
|
18 |
1368
|
Luisehahne
|
*/
|
19 |
1499
|
DarkViper
|
/* -------------------------------------------------------- */
|
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 |
1368
|
Luisehahne
|
|
27 |
|
|
class SecureForm {
|
28 |
|
|
|
29 |
1378
|
Luisehahne
|
const FRONTEND = 0;
|
30 |
|
|
const BACKEND = 1;
|
31 |
|
|
|
32 |
1422
|
Luisehahne
|
|
33 |
1368
|
Luisehahne
|
private $_FTAN = '';
|
34 |
|
|
private $_IDKEYs = array('0'=>'0');
|
35 |
1498
|
DarkViper
|
private $_ftan_name = '';
|
36 |
1368
|
Luisehahne
|
private $_idkey_name = '';
|
37 |
|
|
private $_salt = '';
|
38 |
|
|
private $_fingerprint = '';
|
39 |
1498
|
DarkViper
|
|
40 |
1368
|
Luisehahne
|
/* Construtor */
|
41 |
1378
|
Luisehahne
|
protected function __construct($mode = self::FRONTEND)
|
42 |
1368
|
Luisehahne
|
{
|
43 |
|
|
$this->_FTAN = '';
|
44 |
|
|
$this->_salt = $this->_generate_salt();
|
45 |
|
|
$this->_fingerprint = $this->_generate_fingerprint();
|
46 |
|
|
// generate names for session variables
|
47 |
1457
|
Luisehahne
|
$this->_ftan_name =
|
48 |
1498
|
DarkViper
|
substr($this->_fingerprint, -(16 + hexdec($this->_fingerprint[0])), 16);
|
49 |
1368
|
Luisehahne
|
// make sure there is a alpha-letter at first position
|
50 |
1457
|
Luisehahne
|
$this->_ftan_name = $this->_makeFirst2Letter($this->_ftan_name);
|
51 |
1498
|
DarkViper
|
$this->_idkey_name =
|
52 |
1457
|
Luisehahne
|
substr($this->_fingerprint, hexdec($this->_fingerprint[strlen($this->_fingerprint)-1]), 16);
|
53 |
1368
|
Luisehahne
|
// make sure there is a alpha-letter at first position
|
54 |
1457
|
Luisehahne
|
$this->_idkey_name = $this->_makeFirst2Letter($this->_idkey_name);
|
55 |
1368
|
Luisehahne
|
// 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 |
1498
|
DarkViper
|
$this->_IDKEYs = array('0'=>'0');
|
61 |
1368
|
Luisehahne
|
$_SESSION[$this->_idkey_name] = $this->_IDKEYs;
|
62 |
|
|
}
|
63 |
|
|
}
|
64 |
1498
|
DarkViper
|
|
65 |
1457
|
Luisehahne
|
private function _makeFirst2Letter($string)
|
66 |
|
|
{
|
67 |
1498
|
DarkViper
|
$string[0] = dechex(10 + (hexdec($string[0]) % 5));
|
68 |
1457
|
Luisehahne
|
return $string;
|
69 |
|
|
}
|
70 |
|
|
|
71 |
1368
|
Luisehahne
|
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 |
1561
|
Luisehahne
|
$usedOctets = ( defined('FINGERPRINT_WITH_IP_OCTETS') ) ? (intval(FINGERPRINT_WITH_IP_OCTETS) % 5) : 2;
|
87 |
1524
|
Luisehahne
|
// server depending values
|
88 |
1561
|
Luisehahne
|
$fingerprint = '';
|
89 |
|
|
$fingerprint .= ( isset($_SERVER['SERVER_SIGNATURE']) ) ? $_SERVER['SERVER_SIGNATURE'] : '2';
|
90 |
1368
|
Luisehahne
|
$fingerprint .= ( isset($_SERVER['SERVER_SOFTWARE']) ) ? $_SERVER['SERVER_SOFTWARE'] : '3';
|
91 |
|
|
$fingerprint .= ( isset($_SERVER['SERVER_NAME']) ) ? $_SERVER['SERVER_NAME'] : '5';
|
92 |
1561
|
Luisehahne
|
$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 |
1368
|
Luisehahne
|
$fingerprint .= ( isset($_SERVER['SERVER_PORT']) ) ? $_SERVER['SERVER_PORT'] : '11';
|
101 |
|
|
$fingerprint .= ( isset($_SERVER['SERVER_ADMIN']) ) ? $_SERVER['SERVER_ADMIN'] : '13';
|
102 |
1524
|
Luisehahne
|
$fingerprint .= __FILE__;
|
103 |
1368
|
Luisehahne
|
$fingerprint .= PHP_VERSION;
|
104 |
1524
|
Luisehahne
|
// client depending values
|
105 |
|
|
$fingerprint .= ( isset($_SERVER['HTTP_USER_AGENT']) ) ? $_SERVER['HTTP_USER_AGENT'] : '19';
|
106 |
1561
|
Luisehahne
|
// $usedOctets = ( defined('FINGERPRINT_WITH_IP_OCTETS') ) ? (intval(FINGERPRINT_WITH_IP_OCTETS) % 5) : 2;
|
107 |
1422
|
Luisehahne
|
$clientIp = ( isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '' );
|
108 |
|
|
if(($clientIp != '') && ($usedOctets > 0)){
|
109 |
|
|
$ip = explode('.', $clientIp);
|
110 |
1424
|
DarkViper
|
while(sizeof($ip) > $usedOctets) { array_pop($ip); }
|
111 |
1422
|
Luisehahne
|
$clientIp = implode('.', $ip);
|
112 |
|
|
}else {
|
113 |
1524
|
Luisehahne
|
$clientIp = '23';
|
114 |
1422
|
Luisehahne
|
}
|
115 |
1524
|
Luisehahne
|
$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 |
1368
|
Luisehahne
|
}
|
122 |
|
|
|
123 |
|
|
private function _calcFtan($tanPart)
|
124 |
|
|
{
|
125 |
|
|
$ftan = md5($tanPart . $this->_fingerprint);
|
126 |
|
|
$name = substr($ftan, -(16 + hexdec($ftan[0])), 16);
|
127 |
1457
|
Luisehahne
|
$name = $this->_makeFirst2Letter($name);
|
128 |
1368
|
Luisehahne
|
$value = substr($ftan, hexdec($ftan[strlen($ftan)-1]), 16);
|
129 |
|
|
return array( $name, $value);
|
130 |
|
|
}
|
131 |
1457
|
Luisehahne
|
/**
|
132 |
1368
|
Luisehahne
|
* creates Formular transactionnumbers for unique use
|
133 |
|
|
*
|
134 |
1457
|
Luisehahne
|
* @return void
|
135 |
1368
|
Luisehahne
|
* requirements: an active session must be available
|
136 |
|
|
*/
|
137 |
1457
|
Luisehahne
|
final protected function createFTAN()
|
138 |
1368
|
Luisehahne
|
{
|
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 |
1457
|
Luisehahne
|
}
|
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 |
1368
|
Luisehahne
|
$ftan = $this->_calcFtan($this->_FTAN);
|
155 |
1457
|
Luisehahne
|
if((is_string($mode) && strtolower($mode) == 'post') || ($mode === true))
|
156 |
1368
|
Luisehahne
|
{ // by default return a complete, hidden <input>-tag
|
157 |
|
|
return '<input type="hidden" name="'.$ftan[0].'" value="'.$ftan[1].'" title="" alt="" />';
|
158 |
1457
|
Luisehahne
|
}else{ // return an string with GET params (FTAN0=FTAN1)
|
159 |
1422
|
Luisehahne
|
return $ftan[0].'='.$ftan[1];
|
160 |
1368
|
Luisehahne
|
}
|
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 |
1457
|
Luisehahne
|
if(isset($_SESSION[$this->_ftan_name]))
|
176 |
1368
|
Luisehahne
|
{
|
177 |
1498
|
DarkViper
|
if( $_SESSION[$this->_ftan_name] && (strlen($_SESSION[$this->_ftan_name]) == strlen(md5('dummy'))))
|
178 |
1368
|
Luisehahne
|
{
|
179 |
1457
|
Luisehahne
|
$ftan = $this->_calcFtan($_SESSION[$this->_ftan_name]);
|
180 |
|
|
unset($_SESSION[$this->_ftan_name]);
|
181 |
|
|
$mode = (strtoupper($mode) != 'POST' ? '_GET' : '_POST');
|
182 |
1498
|
DarkViper
|
if( isset($GLOBALS[$mode][$ftan[0]]))
|
183 |
1457
|
Luisehahne
|
{
|
184 |
1498
|
DarkViper
|
$retval = ($GLOBALS[$mode][$ftan[0]] == $ftan[1]);
|
185 |
|
|
unset($GLOBALS[$mode][$ftan[0]]);
|
186 |
1457
|
Luisehahne
|
}
|
187 |
1368
|
Luisehahne
|
}
|
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 |
1457
|
Luisehahne
|
|
252 |
1368
|
Luisehahne
|
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 |
1498
|
DarkViper
|
$this->_IDKEYs = array('0'=>'0');
|
278 |
1368
|
Luisehahne
|
}
|
279 |
1498
|
DarkViper
|
}
|