Project

General

Profile

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