Project

General

Profile

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 1424 2011-01-31 11:56:14Z DarkViper $
15
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/SecureForm.php $
16
 * @lastmodified    $Date: 2011-01-31 12:56:14 +0100 (Mon, 31 Jan 2011) $
17
 * @description     
18
 */
19

    
20
class SecureForm {
21

    
22
	const FRONTEND = 0;
23
	const BACKEND  = 1;
24

    
25

    
26
	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
	protected function __construct($mode = self::FRONTEND)
35
	{
36
		$this->_FTAN  = '';
37
		$this->_salt = $this->_generate_salt();
38
		$this->_fingerprint = $this->_generate_fingerprint();
39
	// generate names for session variables
40
		$this->_ftan_name = substr($this->_fingerprint, -(16 + hexdec($this->_fingerprint[0])), 16);
41
	// make sure there is a alpha-letter at first position
42
		$this->_ftan_name[0] = dechex(10 + (hexdec($this->_ftan_name[0]) % 5));
43
		$this->_idkey_name = substr($this->_fingerprint,
44
				                    hexdec($this->_fingerprint[strlen($this->_fingerprint)-1]), 16);
45
	// make sure there is a alpha-letter at first position
46
		$this->_idkey_name[0] = dechex(10 + (hexdec($this->_idkey_name[0]) % 5));
47
	// takeover id_keys from session if available
48
		if(isset($_SESSION[$this->_idkey_name]) && is_array($_SESSION[$this->_idkey_name]))
49
		{
50
			$this->_IDKEYs = $_SESSION[$this->_idkey_name];
51
		}else{
52
			$this->_IDKEYs = array('0'=>'0');
53
			$_SESSION[$this->_idkey_name] = $this->_IDKEYs;
54
		}
55
	}
56

    
57
	private function _generate_salt()
58
	{
59
		if(function_exists('microtime'))
60
		{
61
			list($usec, $sec) = explode(" ", microtime());
62
			$salt = (string)((float)$usec + (float)$sec);
63
		}else{
64
			$salt = (string)time();
65
		}
66
		$salt = (string)rand(10000, 99999) . $salt . (string)rand(10000, 99999);
67
		return md5($salt);
68
	}
69

    
70
	private function _generate_fingerprint()
71
	{
72
	// server depending values
73
 		$fingerprint  = ( isset($_SERVER['SERVER_SIGNATURE']) ) ? $_SERVER['SERVER_SIGNATURE'] : '2';
74
		$fingerprint .= ( isset($_SERVER['SERVER_SOFTWARE']) ) ? $_SERVER['SERVER_SOFTWARE'] : '3';
75
		$fingerprint .= ( isset($_SERVER['SERVER_NAME']) ) ? $_SERVER['SERVER_NAME'] : '5';
76
		$fingerprint .= ( isset($_SERVER['SERVER_ADDR']) ) ? $_SERVER['SERVER_ADDR'] : '7';
77
		$fingerprint .= ( isset($_SERVER['SERVER_PORT']) ) ? $_SERVER['SERVER_PORT'] : '11';
78
		$fingerprint .= ( isset($_SERVER['SERVER_ADMIN']) ) ? $_SERVER['SERVER_ADMIN'] : '13';
79
		$fingerprint .= PHP_VERSION;
80
	// client depending values
81
		$fingerprint .= ( isset($_SERVER['HTTP_USER_AGENT']) ) ? $_SERVER['HTTP_USER_AGENT'] : '17';
82
		$usedOctets = ( defined('FINGERPRINT_WITH_IP_OCTETS') ) ? intval(defined('FINGERPRINT_WITH_IP_OCTETS')) : 0;
83
		$clientIp = ( isset($_SERVER['REMOTE_ADDR'])  ? $_SERVER['REMOTE_ADDR'] : '' );
84
		if(($clientIp != '') && ($usedOctets > 0)){
85
			$ip = explode('.', $clientIp);
86
			while(sizeof($ip) > $usedOctets) { array_pop($ip); }
87
			$clientIp = implode('.', $ip);
88
		}else {
89
			$clientIp = 19;
90
		}
91
		$fingerprint .= $clientIp;
92
		return md5($fingerprint);
93
	}
94

    
95
	private function _calcFtan($tanPart)
96
	{
97
		$ftan = md5($tanPart . $this->_fingerprint);
98
		$name = substr($ftan, -(16 + hexdec($ftan[0])), 16);
99
		$name[0] = dechex(10 + (hexdec($name[0]) % 5));
100
		$value = substr($ftan, hexdec($ftan[strlen($ftan)-1]), 16);
101
		return array( $name, $value);
102
	}
103
/*
104
 * creates Formular transactionnumbers for unique use
105
 * @access public
106
 * @param bool $asTAG: true returns a complete prepared, hidden HTML-Input-Tag (default)
107
 *                     false returns an GET argument 'key=value'
108
 * @return mixed:      array or string
109
 *
110
 * requirements: an active session must be available
111
 */
112
	final public function getFTAN( $as_tag = true)
113
	{
114
		if( $this->_FTAN == '')
115
		{ // if no FTAN exists, create new one from time and salt
116
			$this->_FTAN = md5($this->_fingerprint.$this->_salt);
117
			$_SESSION[$this->_ftan_name] = $this->_FTAN; // store FTAN into session
118
		}
119
		$ftan = $this->_calcFtan($this->_FTAN);
120
		if($as_tag == true)
121
		{ // by default return a complete, hidden <input>-tag
122
			return '<input type="hidden" name="'.$ftan[0].'" value="'.$ftan[1].'" title="" alt="" />';
123
		}else{ // return an array with raw FTAN0 and FTAN1
124
			return $ftan[0].'='.$ftan[1];
125
		}
126
	}
127

    
128
/*
129
 * checks received form-transactionnumbers against session-stored one
130
 * @access public
131
 * @param string $mode: requestmethode POST(default) or GET
132
 * @return bool:    true if numbers matches against stored ones
133
 *
134
 * requirements: an active session must be available
135
 * this check will prevent from multiple sending a form. history.back() also will never work
136
 */
137
	final public function checkFTAN( $mode = 'POST')
138
	{
139
		$retval = false;
140
		if(isset($_SESSION[$this->_ftan_name]) &&
141
		   (strlen($_SESSION[$this->_ftan_name]) == strlen(md5('dummy'))))
142
		{
143
			$ftan = $this->_calcFtan($_SESSION[$this->_ftan_name]);
144
			unset($_SESSION[$this->_ftan_name]);
145
			$mode = (strtoupper($mode) != 'POST' ? '_GET' : '_POST');
146
			if( isset($GLOBALS[$mode][$ftan[0]]))
147
			{
148
				$retval = ($GLOBALS[$mode][$ftan[0]] == $ftan[1]);
149
				unset($GLOBALS[$mode][$ftan[0]]);
150
			}
151
		}
152
		return $retval;
153
	}
154

    
155
/*
156
 * save values in session and returns a ID-key
157
 * @access public
158
 * @param mixed $value: the value for witch a key shall be generated and memorized
159
 * @return string:      a MD5-Key to use instead of the real value
160
 *
161
 * @requirements: an active session must be available
162
 * @description: IDKEY can handle string/numeric/array - vars. Each key is a
163
 */
164
	final public function getIDKEY($value)
165
	{
166
		if( is_array($value) == true )
167
		{ // serialize value, if it's an array
168
			$value = serialize($value);
169
		}
170
		// crypt value with salt into md5-hash
171
		// and return a 16-digit block from random start position
172
		$key = substr( md5($this->_salt.(string)$value), rand(0,15), 16);
173
		do{ // loop while key/value isn't added
174
			if( !array_key_exists($key, $this->_IDKEYs) )
175
			{ // the key is unique, so store it in list
176
				$this->_IDKEYs[$key] = $value;
177
				break;
178
			}else {
179
				// if key already exist, increment the last five digits until the key is unique
180
				$key = substr($key, 0, -5).dechex(('0x'.substr($key, -5)) + 1);
181
			}
182
		}while(0);
183
		// store key/value-pairs into session
184
		$_SESSION[$this->_idkey_name] = $this->_IDKEYs;
185
		return $key;
186
	}
187

    
188
/*
189
 * search for key in session and returns the original value
190
 * @access public
191
 * @param string $fieldname: name of the POST/GET-Field containing the key or hex-key itself
192
 * @param mixed $default: returnvalue if key not exist (default 0)
193
 * @param string $request: requestmethode can be POST or GET or '' (default POST)
194
 * @return mixed: the original value (string, numeric, array) or DEFAULT if request fails
195
 *
196
 * @requirements: an active session must be available
197
 * @description: each IDKEY can be checked only once. Unused Keys stay in list until the
198
 *               session is destroyed.
199
 */
200
 	final public function checkIDKEY( $fieldname, $default = 0, $request = 'POST' )
201
	{
202
		$return_value = $default; // set returnvalue to default
203
		switch( strtoupper($request) )
204
		{
205
			case 'POST':
206
				$key = isset($_POST[$fieldname]) ? $_POST[$fieldname] : $fieldname;
207
				break;
208
			case 'GET':
209
				$key = isset($_GET[$fieldname]) ? $_GET[$fieldname] : $fieldname;
210
				break;
211
			default:
212
				$key = $fieldname;
213
		}
214
		if( preg_match('/[0-9a-f]{16}$/', $key) )
215
		{ // key must be a 16-digit hexvalue
216
			if( array_key_exists($key, $this->_IDKEYs))
217
			{ // check if key is stored in IDKEYs-list
218
				$return_value = $this->_IDKEYs[$key]; // get stored value
219
				unset($this->_IDKEYs[$key]);   // remove from list to prevent multiuse
220
				$_SESSION[$this->_idkey_name] = $this->_IDKEYs; // save modified list into session again
221
				if( preg_match('/.*(?<!\{).*(\d:\{.*;\}).*(?!\}).*/', $return_value) )
222
				{ // if value is a serialized array, then deserialize it
223
					$return_value = unserialize($return_value);
224
				}
225
			}
226
		}
227
		return $return_value;
228
	}
229

    
230
/* @access public
231
 * @return void
232
 *
233
 * @requirements: an active session must be available
234
 * @description: remove all entries from IDKEY-Array
235
 *
236
 */
237
 	final public function clearIDKEY()
238
	{
239
		 $this->_IDKEYs = array('0'=>'0');
240
	}
241
}
(1-1/16)