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

    
20
class SecureForm {
21

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

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

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

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

    
68
	private function _generate_fingerprint()
69
	{
70
	// server depending values
71
 		$fingerprint  = ( isset($_SERVER['SERVER_SIGNATURE']) ) ? $_SERVER['SERVER_SIGNATURE'] : '2';
72
		$fingerprint .= ( isset($_SERVER['SERVER_SOFTWARE']) ) ? $_SERVER['SERVER_SOFTWARE'] : '3';
73
		$fingerprint .= ( isset($_SERVER['SERVER_NAME']) ) ? $_SERVER['SERVER_NAME'] : '5';
74
		$fingerprint .= ( isset($_SERVER['SERVER_ADDR']) ) ? $_SERVER['SERVER_ADDR'] : '7';
75
		$fingerprint .= ( isset($_SERVER['SERVER_PORT']) ) ? $_SERVER['SERVER_PORT'] : '11';
76
		$fingerprint .= ( isset($_SERVER['SERVER_ADMIN']) ) ? $_SERVER['SERVER_ADMIN'] : '13';
77
		$fingerprint .= PHP_VERSION;
78
	// client depending values
79
		$fingerprint .= ( isset($_SERVER['HTTP_ACCEPT']) ) ? $_SERVER['HTTP_ACCEPT'] : '17';
80
		$fingerprint .= ( isset($_SERVER['HTTP_ACCEPT_CHARSET']) ) ? $_SERVER['HTTP_ACCEPT_CHARSET'] : '19';
81
		$fingerprint .= ( isset($_SERVER['HTTP_ACCEPT_ENCODING']) ) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : '23';
82
		$fingerprint .= ( isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '29';
83
		$fingerprint .= ( isset($_SERVER['HTTP_CONNECTION']) ) ? $_SERVER['HTTP_CONNECTION'] : '31';
84
		$fingerprint .= ( isset($_SERVER['HTTP_USER_AGENT']) ) ? $_SERVER['HTTP_USER_AGENT'] : '37';
85
		$fingerprint .= ( isset($_SERVER['REMOTE_ADDR']) ) ? $_SERVER['REMOTE_ADDR'] : '41';
86
		return md5($fingerprint);
87
	}
88

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

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

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

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

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