<?php
if(is_file('includes/configure.php'))
	die('IS CONFIGURED');
session_start();
switch($_GET['step']){
	case '3':
		if(!is_file($_POST['path'].'install_yes4c.php')){
			header('Location: install_yes4c.php?step=2&msg='.urldecode("Die absoluten Pfadangaben waren nicht korrekt"));
		}else{
			// DATA OK, WRITE CONF
			$filename = $_POST['path'].'includes/configure.php';
			$handle = fopen ($filename, "w");

			$configs = "<?php
/* YES4CARRIER AUTOCONF */
define('DB_SERVER', '".$_SESSION['yes4c']['db']['host']."');
define('DB_SERVER_USERNAME', '".$_SESSION['yes4c']['db']['user']."');
define('DB_SERVER_PASSWORD', '".$_SESSION['yes4c']['db']['password']."');
define('DB_DATABASE', '".$_SESSION['yes4c']['db']['database']."');
define('USE_PCONNECT', 'false');
define('STORE_SESSIONS', '');

define('DIR_FS_CATALOG','".$_POST['path']."');
define('HTTP_SERVER','".$_POST['url']."/');

?>";

			if (!fwrite($handle, $configs)) {
			        print "Kann in die Datei $filename nicht schreiben";
			        exit;
			}
			unset($_SESSION['yes4c']);
			fclose($handle);
			chmod($filename,0444);
			header('Location: index.php');
		}
		break;
	case '2':
		if(!isset($_SESSION['yes4c'])){
			$link = @mysql_connect($_POST['host'], $_POST['user'], $_POST['password']);
			if ($link){
				if (!@mysql_select_db($_POST['database'], $link)){
					$error = true;
				}else{
					$_SESSION['yes4c']['db'] = array(
						'host'=>$_POST['host'],
						'database'=>$_POST['database'],
						'user'=>$_POST['user'],
						'password'=>$_POST['password']
					);
				}
			}else{
				$error = true;
			}
		}
		if($error){
			header('Location: install_yes4c.php?msg='.urldecode("Zugangsdaten der Datenbank nicht korrekt"));
		}else{
			if($_GET['msg'])
				echo '<p><font color="#FF0000"><b>Fehler:</b> '.$_GET['msg'].'</font></p>';
?>
<h1>yes4carrier Installation</h1>
<h2>Schritt 2 - Pfade und URL</h2>
<form action="install_yes4c.php?step=3" method="post">
Absoluter Pfad zur Installation: <input type="text" name="path" size="60" value="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>" /><br />
URL:  <input type="text" name="url" size="60" value="http://<?php echo $_SERVER['HTTP_HOST']; ?>" /><br />
<i>Die URL unter der die Software erreichbar ein soll. Am Ende darf kein / stehen</i><br />
<input type="submit" value="Weiter zu Schritt 3">
</form>
<?php
		}
		break;
	default:
		unset($_SESSION['yes4c']);
		if($_GET['msg'])
			echo '<p><font color="#FF0000"><b>Fehler:</b> '.$_GET['msg'].'</font></p>';
?>
<h1>yes4carrier Installation</h1>
<h2>Schritt 1 - Datenbank</h2>
<form action="install_yes4c.php?step=2" method="post">
DB Host: <input type="text" name="host" value="localhost" /><br />
Datenbankname: <input type="text" name="database" value="yes4carrier" /><br />
DB User: <input type="text" name="user" value="" /><br />
DB Passwort: <input type="password" name="password" value="" /><br />
<input type="submit" value="Weiter zu Schritt 2">

</form>
<?php
		break;
}
exit;

