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 1374 2011-01-10 12:21:47Z Luisehahne $
15
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/SecureForm.php $
16
 * @lastmodified    $Date: 2011-01-10 13:21:47 +0100 (Mon, 10 Jan 2011) $
17
 * @description     
18
 */
19

    
20
/**
21
 * Description of class
22
 *
23
 * @author wkl
24
 */
25
class SecureForm {
26

    
27
	private $_FTAN        = '';
28
	private $_IDKEYs      = array('0'=>'0');
29
	private $_ftan_name   = '';
30
	private $_idkey_name  = '';
31
	private $_salt        = '';
32
	private $_fingerprint = '';
33

    
34
/* Construtor */
35
	protected function __construct()
36
	{
37
		$this->_FTAN  = '';
38
		$this->_salt = $this->_generate_salt();
39
		$this->_fingerprint = $this->_generate_fingerprint();
40
	// generate names for session variables
41
		$this->_ftan_name = substr($this->_fingerprint, -(16 + hexdec($this->_fingerprint[0])), 16);
42
	// make sure there is a alpha-letter at first position
43
		$this->_ftan_name[0] = dechex(10 + (hexdec($this->_ftan_name[0]) % 5));
44
		$this->_idkey_name = substr($this->_fingerprint, 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_ACCEPT']) ) ? $_SERVER['HTTP_ACCEPT'] : '17';
82
		$fingerprint .= ( isset($_SERVER['HTTP_ACCEPT_CHARSET']) ) ? $_SERVER['HTTP_ACCEPT_CHARSET'] : '19';
83
		$fingerprint .= ( isset($_SERVER['HTTP_ACCEPT_ENCODING']) ) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : '23';
84
		$fingerprint .= ( isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '29';
85
		$fingerprint .= ( isset($_SERVER['HTTP_CONNECTION']) ) ? $_SERVER['HTTP_CONNECTION'] : '31';
86
		$fingerprint .= ( isset($_SERVER['HTTP_USER_AGENT']) ) ? $_SERVER['HTTP_USER_AGENT'] : '37';
87
		$fingerprint .= ( isset($_SERVER['REMOTE_ADDR']) ) ? $_SERVER['REMOTE_ADDR'] : '41';
88
		return md5($fingerprint);
89
	}
90

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

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

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

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

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