<?php
$escape = '';
if($_POST['ROOT_DIR'] == '')
	$escape .= "ROOT_DIR?! <br>";
if($_POST['HOST_NAME'] == '')
	$escape .= "Hostname?! <br>";
if(! IsSet($_POST['FTP_USED']))
	$escape .= $lang['no_ftp_used'];

if($_POST['FTP_USED'] == 1)
{
	$_POST['ftp_server'] = trim($_POST['ftp_server']);
	$ftp_conn_id = ftp_connect($_POST['ftp_server']);
	if(!$ftp_conn_id) //wenn keine Verbindung zustande kommt
		$escape = "Can't connect to FTP-Server";

	$i = 0;
	if(! ftp_login($ftp_conn_id, $_POST['ftp_user'], $_POST['ftp_passw']))
				$escape = "The FTP-access-data probably wrong are wrong";

}

//USER
if($_POST['admin_username'] == '')
	$escape .= $lang['no_admin_user']."<br>";
if($_POST['admin_passw'] == '')
	$escape .= $lang['no_admin_pw']."<br>";
if($_POST['admin_passw'] != $_POST['admin_repeat'])
	$escape .= $lang['wrong_repeat']."<br>";

if($escape != '')
{
	if(IsSet($ftp_conn_id))
		@ftp_quit($ftp_conn_id);
}
else
{
	
	if(substr($_POST['HOST_NAME'], strlen($_POST['HOST_NAME'])-1) == "/") 
  	$_POST['HOST_NAME'] = str_cut($_POST['HOST_NAME'], strlen($_POST['HOST_NAME']) - 2);

	if(substr($_POST['FTP_ROOT'], strlen($_POST['FTP_ROOT'])-1) == "/") 
  	$_POST['FTP_ROOT'] = str_cut($_POST['FTP_ROOT'], strlen($_POST['FTP_ROOT']) - 2);
	
	if(substr($_POST['ROOT_DIR'], strlen($_POST['ROOT_DIR'])-1) == "/") 
  	$_POST['ROOT_DIR'] = str_cut($_POST['ROOT_DIR'], strlen($_POST['ROOT_DIR']) - 2);
	if(substr($_POST['SCRIPT_PATH'], strlen($_POST['SCRIPT_PATH'])-1) == "/") 
  	$_POST['SCRIPT_PATH'] = str_cut($_POST['SCRIPT_PATH'], strlen($_POST['SCRIPT_PATH']) - 2);
	
	//WENN ERSTER BUCHSTABER
	if(substr($_POST['SCRIPT_PATH'], 0, 1) == "/") 
		$_POST['SCRIPT_PATH']= substr($_POST['SCRIPT_PATH'], 1);
 	
 	if(!file_exists($_POST['ROOT_DIR']."/".$_POST['SCRIPT_PATH']))
 		$escape = "Invalid root-dir";
 	
	//constants.php wird geschrieben
	$tmpfile = create_tmpfile();	
	$fp = fopen($tmpfile, 'w');
	fwrite($fp, "");
	fputs($fp, "<?php\n");
	fputs($fp, "define(\"HOST_NAME\", \"".$_POST['HOST_NAME']."\");\n");
	fputs($fp, "define(\"ROOT_DIR\", \"".$_POST['ROOT_DIR']."\");\n");				 
	fputs($fp, "define(\"FTP_USED\", ".$_POST['FTP_USED'].");\n");
	fputs($fp, "define(\"FTP_ROOT\", \"".$_POST['FTP_ROOT']."\");\n");
	fputs($fp, "define(\"SCRIPT_PATH\", \"".$_POST['SCRIPT_PATH']."\");\n");
	fputs($fp, "define(\"LOG_USED\", 1);\n");
	fputs($fp, "define(\"SHOW_CODEPRESS\", 0);\n");
	fputs($fp, "define(\"READ_ONLY\", \"jpg,gif,png,bmp,pdf,zip,doc\");\n");
	fputs($fp, "?"); fputs($fp, ">");
	fclose($fp);

	if($_POST['FTP_USED'] == 1)
  {

		if(! ftp_put($ftp_conn_id , $_POST['FTP_ROOT']."/".$_POST['SCRIPT_PATH']."/work/data/constants.php", $_POST['ROOT_DIR']."/".$_POST['SCRIPT_PATH']."/".$tmpfile , FTP_BINARY))
  		$escape = $lang['gen_confg_wrong'];
	}
	else
  {
		if(! copy( $_POST['ROOT_DIR']."/".$_POST['SCRIPT_PATH']."/".$tmpfile ,$_POST['ROOT_DIR']."/".$_POST['SCRIPT_PATH']."/work/data/constants.php"))
			$escape = $lang['gen_confg_wrong'];
	}
	unlink($tmpfile);
	
	//schreibe config.php
	$tmpfile = create_tmpfile();
	$fp = fopen($tmpfile, 'w');
	if((IsSet($fp)) && ($fp != ''))
	{			
		@fputs($fp, "<?php\n");
		@fputs($fp,"\$ftp_server = '".$_POST['ftp_server']."';\n");
		@fputs($fp,"\$ftp_user = '".$_POST['ftp_user']."';\n");
		@fputs($fp,"\$ftp_password = '".$_POST['ftp_passw']."';\n");
		@fputs($fp, "?"); fputs($fp,">");
	
		fclose($fp);
	}
	else
		echo("<p class='error'>Die Datei config.php konnte nicht ausgefüllt werden! Bitte füllen sie diese manuell aus!</p>");
	
	if($_POST['FTP_USED'] == 1)
  {
		if(! ftp_put($ftp_conn_id , $_POST['FTP_ROOT']."/".$_POST['SCRIPT_PATH']."/config.php", $_POST['ROOT_DIR']."/".$_POST['SCRIPT_PATH']."/".$tmpfile , FTP_BINARY))
  		$escape = $lang['gen_confg_wrong'];
	}
	else
  {
		if(! copy( $_POST['ROOT_DIR']."/".$_POST['SCRIPT_PATH']."/".$tmpfile ,$_POST['ROOT_DIR']."/".$_POST['SCRIPT_PATH']."/config.php"))
			$escape = $lang['gen_confg_wrong'];
	}
	unlink($tmpfile);
	
	//Der Erste User wird eingetraagen
	$tmpfile = create_tmpfile();
	$fp = fopen($tmpfile, 'w');
	fwrite($fp, "");
	fputs($fp, "<?php/*;");
	fputs($fp, "1,".$_POST['admin_username'].",".crypt($_POST['admin_passw']).",1,11111,english,0,default,,codepress;");
	fputs($fp, "*/?");
	fputs($fp, ">");
	fclose($fp);
	
	if($_POST['FTP_USED'] == 1)
  {
		if(! ftp_put($ftp_conn_id , $_POST['FTP_ROOT']."/".$_POST['SCRIPT_PATH']."/work/data/user_".strtolower($_POST['admin_username']).".php", $_POST['ROOT_DIR']."/".$_POST['SCRIPT_PATH']."/".$tmpfile , FTP_BINARY))
  		$escape = $lang['gen_confg_wrong'];
	}
	else
  {
		if(! copy( $_POST['ROOT_DIR']."/".$_POST['SCRIPT_PATH']."/".$tmpfile ,$_POST['ROOT_DIR']."/".$_POST['SCRIPT_PATH']."/work/data/user_".strtolower($_POST['admin_username']).".php"))
			$escape = $lang['gen_confg_wrong'];
	}
	unlink($tmpfile);
	
	$tmpfile = create_tmpfile();
	$fp = fopen($tmpfile, 'w');
	fputs($fp, "<Files config.php>");
	fputs($fp, "Deny from all");
	fputs($fp, "</Files>");
	fclose($fp);
	
	if($_POST['FTP_USED'] == 1)
  {
		if(! ftp_put($ftp_conn_id , $_POST['FTP_ROOT']."/".$_POST['SCRIPT_PATH']."/.htaccess", $_POST['ROOT_DIR']."/".$_POST['SCRIPT_PATH']."/".$tmpfile , FTP_BINARY))
  		$escape = $lang['gen_confg_wrong'];
	}
	else
  {
		if(! copy( $_POST['ROOT_DIR']."/".$_POST['SCRIPT_PATH']."/".$tmpfile ,$_POST['ROOT_DIR']."/".$_POST['SCRIPT_PATH']."/.htaccess"))
			$escape = $lang['gen_confg_wrong'];
	}
	unlink($tmpfile);
	
	
	
	if(IsSet($ftp_conn_id))
		ftp_quit($ftp_conn_id);
}
?>
