<?php
	session_start();

	/**
	 * @author Jan Wehrs (jan.wehrs@billpay.de)
	 * @copyright Copyright 2010 Billpay GmbH
	 * @license commercial 
	 */
	
	$validateError = (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') && (
		empty($_POST['api-url-base']) || empty($_POST['mid']) || 
		empty($_POST['pid']) || empty($_POST['security-key']));
		
	if ($validateError == false) {
		$_SESSION['api-url-base'] = $_POST['api-url-base'];
		$_SESSION['mid'] = $_POST['mid'];
		$_SESSION['pid'] = $_POST['pid'];
		$_SESSION['security-key'] = $_POST['md5'] ? $_POST['security-key'] : md5($_POST['security-key']);
	}
	
	if (empty($_SESSION['api-url-base']) || empty($_SESSION['mid']) || empty($_SESSION['pid']) || empty($_SESSION)) {
		echo "Please insert your credentials that you received from Billpay:<br/>";
		echo "<form method='POST'>";
		echo "<table><tr>";
		echo "<td>API Url Base</td><td><input type='text' name='api-url-base' style='width:250px' value='https://test-api.billpay.de/xml/offline' /></td>";
		echo "</tr><tr>";
		echo "<td>Merchant ID</td><td><input type='text' name='mid' style='width:50px' /></td>";
		echo "</tr><tr>";
		echo "<td>Portal ID</td><td><input type='text' name='pid' style='width:50px' /></td>";
		echo "</tr><tr>";
		echo "<td>Security Key</td><td><input type='text' name='security-key' />";
		echo "<div><input type='radio' name='md5' value='0' checked /> Plain   <input type='radio' name='md5' value='1' /> MD5 </div> </td>";
		echo "</tr></table>";
		if ($validateError) {
			echo "<span style='color:red; font-weight:bold'>Please provide values for all fields</span><br />";
		}
		echo "<input type='submit' />";
		echo "</form>";
	}
	else {
		$startPage = 'preauth.php';
		$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		
		if (!strstr($url, 'index.php')) {
			$rev = strrev($url);
			if ($rev[0] != '/') {
				$url .= '/';
			}
			$url .= $startPage;
		}
		else {
			$url = str_replace('index.php', $startPage, $url);	
		}
		
		header ("HTTP/1.1 303 See other");
		header ("Location: $url");
		exit();
	}
 
?>

