<?php
// ======================================================================== //
//                                                                          //
//   KooBI- Webportalsystem                                                 //
//   SCRiPTMAFiA 2005 - THE DiRTY HANDS ON YOUR SCRiPTS                     //
//                                                                          //
//   ====================================================================   //
//                                                                          //   
//   dream4 | webservices - Am Michelsgrund 7 - 69469 Weinheim - Germany    //  
//   tel. +49(0) 6201 - 980442 / fax. +49(0) 6201 - 64809                   //
//                                                                          //
// ======================================================================== //

ob_start();
define("SETUP",1);
error_reporting(E_ALL ^ E_NOTICE);
define("BASEDIR", dirname(__FILE__));

require_once(BASEDIR . "/inc/config.php");
require_once(BASEDIR . "/inc/functions.php");
require_once(BASEDIR . "/inc/functions_post.php");
require_once(BASEDIR . "/inc/version.ini.php");
require_once(BASEDIR . "/class/tpl/Koobi.class.php");
require_once(BASEDIR . "/data/lang/zh_install.php");

$tmpl = new Koobi("data/tpl/");
$tmpl->assign("kversion", KVERSION);
$tmpl->assign('pref', $pref);
$tmpl->assign('la',$lang_i);

$required_php = 423;
$dbprefix = ($dbprefix!="") ? $dbprefix : $_REQUEST['dbprefix'];                  																	
$required = array();
$required[] = "data/lang/zh_user.php";
$required[] = "data/lang/zh_admin.php";
$required[] = "data/struktur.sql";
$required[] = "data/data.sql";
$required[] = "data/eula/zh.tpl";
$required[] = "inc/config.php";

$writeable = array();
$writeable[] = "temp/";
$writeable[] = "temp/1/";
$writeable[] = "temp/2/";
$writeable[] = "temp/3/";
$writeable[] = "temp/4/";
$writeable[] = "temp/5/";
$writeable[] = "temp/6/";
$writeable[] = "temp/7/";
$writeable[] = "temp/8/";
$writeable[] = "temp/9/";
$writeable[] = "temp/10/";
$writeable[] = "uploads/";
$writeable[] = "uploads/affiliates/";
$writeable[] = "uploads/articlecat_icons/";
$writeable[] = "uploads/attachment/";
$writeable[] = "uploads/avatars/";
$writeable[] = "uploads/download_images/";
$writeable[] = "uploads/files/";
$writeable[] = "uploads/galerie/";
$writeable[] = "uploads/inlineshots/";
$writeable[] = "uploads/manufacturer/";
$writeable[] = "uploads/mediapool/";
$writeable[] = "uploads/newscat_icons/";
$writeable[] = "uploads/newsletter_attachments/";
$writeable[] = "uploads/products/";
$writeable[] = "uploads/topnews_icons/";


$con = true;
if(!@mysql_connect($dbhost, $dbuser, $dbpass)) { 
	$con = false;
	}

if(!@mysql_select_db($dbname)) {
	$con = false;
	}
	
if(!$con) {
	$db_error = 1;
	$error = 1;
}


// writeable
	$error_is_required = array();
	foreach ($writeable as $must_writeable){
		if(!is_writeable($must_writeable)){
			array_push($error_is_required, $lang_i['error_is_writeable'] . $must_writeable . $lang_i['error_is_writeable_2'] );
			$error = 1;
		}
	}
	
	// required
	foreach ($required as $is_required){
		if(@!is_file($is_required)){
			array_push($error_is_required, $lang_i['error_is_required'] . $is_required . $lang_i['error_is_required_2'] );
			$error = 1;
		}
	}
	
	// php- version
	$myphp = @PHP_VERSION;
	if($myphp){
		$myphp_v = str_replace('.', '', $myphp);
		if($myphp_v < $required_php){
			array_push($error_is_required, $lang_i['phpversion_toold'] . $required_php);
		}
	}
	

	if(count($error_is_required)>=1){
		$error = 1;
		$tmpl->assign('error_header', $lang_i['erroro_more']);
	} else {
		$tmpl->assign('error_header', $lang_i['erroro']);
	}
	
	if(count($error_is_required)<1){ $error = ""; }
	if( ($error == 1) && ($_REQUEST['force'] != 1) ){
		$tmpl->assign('error_is_required', $error_is_required);
		$tmpl->display('error.tpl');
		exit;
	}
	
	///////////////////////////////////////////////////////
if($_REQUEST['step']!='finish'){
	$query = @mysql_query("SELECT uid FROM ".$dbprefix."_user limit 1");
	$num = @mysql_num_rows($query);

	if($num>0){
		echo "<pre>".$lang_i['installed']."</pre>";
		exit;
	}
}

switch($_REQUEST['step']){
	
	case "" :
	if( ($con) && ($_REQUEST['subaction']!="newset") ){
		$tmpl->display('license.tpl');
		exit;
	}
	switch($_REQUEST['subaction']){
		case "newset" :
		
		// wenn config nicht beschreibbar isz
		if(@!is_writeable("inc/config.php")){
			$tmpl->assign('step', 'no');
			$tmpl->display('error.tpl');
			exit;
		}
		
		$connection = 1;
		if(!@mysql_connect($_POST['dbhost'], $_POST['dbuser'], $_POST['dbpass'])) $connection  = 0;
		if(!@mysql_select_db($_POST['dbname'])) $connection = 0;
		
		
		if( ($connection  == 1) && ($_POST['dbprefix'] != "") ){
			$fp=@fopen("inc/config.php","w+");
@fwrite($fp,"<?php
\$dbhost = \"".str_replace("\"","\\\\\"",stripslashes(trim($_POST['dbhost'])))."\";
\$dbuser = \"".str_replace("\"","\\\\\"",stripslashes(trim($_POST['dbuser'])))."\";
\$dbpass = \"".str_replace("\"","\\\\\"",stripslashes(trim($_POST['dbpass'])))."\";
\$dbname = \"".str_replace("\"","\\\\\"",stripslashes(trim($_POST['dbname'])))."\";
\$dbprefix = \"".str_replace("\"","\\\\\"",stripslashes(trim($_POST['dbprefix'])))."\";
\$securecode_by_register = \"0\"; // 1 = Sicherheitscode bei Registration (nur bei installierter GD)
define(\"MRWRITE\", 0); // Nur für Fortgeschrittene Benutzer
?>");
			
			@fclose($fp);
			header("Location:setup.php?step=");
			
		} else {
			$tmpl->assign('warnnodb', $lang_i['enoconn']);
			$tmpl->assign('dbhost' , stripslashes(trim($_POST['dbhost'])));
			$tmpl->assign('dbuser' , stripslashes(trim($_POST['dbuser'])));
			$tmpl->assign('dbpass' , stripslashes(trim($_POST['dbpass'])));
			$tmpl->assign('dbname' , stripslashes(trim($_POST['dbname'])));
			$tmpl->assign('dbprefix' , stripslashes(trim($_POST['dbprefix'])));
		}
		
		break;
	}
	
	
	$tmpl->display('start.tpl');
	break;
	
	case "check" :
	
	
	$tmpl->display('license.tpl');
	break;
	
	// struktur importieren
	case 1 :
	$first_sql = "data/struktur.sql";
	$handle = fopen($first_sql, "r");
	$db = fread($handle, filesize($first_sql));
	fclose($handle);
	
	$db = str_replace('koobi4', $dbprefix, $db);
	
	$m_ok = 0;
	$m_fail = 0;
	
	// tabellen anlegen
	$message_ok = array();
	$message_error = array();
	$ar = explode(";##\n", $db);
	while (list($key,$val) = each($ar)) {
		if (rtrim(ltrim($val)) != "") {
			$q = str_replace("\n","",$val);
			$q = $q . ";";
			if (mysql_query($q)) {
				$m_ok++;
				$e = preg_replace("/CREATE TABLE ([a-zA-Z0-9_]*)(.*)/", "\\1", $q);
				array_push($message_ok, $lang_i['c_table_1'] . " '<b> $e </b>'" . $lang_i['c_table_2'] );
			} else {
				$e = preg_replace("/CREATE TABLE ([a-zA-Z0-9_]*)(.*)/", "\\1", $q);
				switch(mysql_errno()){
					case 1050 :
					$mysql_e = $lang_i['table_error_exists_2'];
					break;
				}
				array_push($message_error, $lang_i['table_error_exists'] . " '<b> $e </b>' $mysql_e");
				$m_fail++;
			}
		}
	}
	$tmpl->assign('header_title', $lang_i['step1_table_status']);
	$tmpl->assign('message_ok', $message_ok);
	$tmpl->assign('message_error', $message_error);
	$tmpl->display('step1.tpl');
	break;
	
	// sprache einfuegen
	case 2 :
	//user
	$message_error = array();
	$message_ok = array();
	
	$klang = array();
	include("data/lang/zh_user.php");
	
	$code = array_keys($klang);
	$lang = $klang[$code[0]];
	
	foreach($lang as $key => $value){
		$sql = "INSERT INTO " . $dbprefix . "_languages (
		code,
		name,
		value,
		value_original
		) VALUES (
		'".$code[0]."',
		'$key',
		'$value',
		'$value'
		)
		";
		@mysql_query($sql);
		if(mysql_errno() == 1062){
			array_push($message_error, $lang_i['eimport'] . ' "<b>'.$key.'</b>" ' . $lang_i['eimport_2']);
		} else {
			array_push($message_ok, $lang_i['oimport'] . ' "<b>'.$key.'</b>" ' . $lang_i['oimport_2']);
		}
	}
	// admin
	$klang_admin = array();
	include("data/lang/zh_admin.php");
	
	$code = array_keys($klang_admin);
	$lang = $klang_admin[$code[0]];
	
	
	foreach($lang as $key => $value){
		$sql = "INSERT INTO " . $dbprefix . "_languages_admin (
		code,
		name,
		value,
		value_original
		) VALUES (
		'".$code[0]."',
		'$key',
		'$value',
		'$value'
		)
		";
		@mysql_query($sql);
		if(mysql_errno() == 1062){
			array_push($message_error, $lang_i['eimport'] . ' "<b>'.$key.'</b>" ' . $lang_i['eimport_2']);
		} else {
			array_push($message_ok, $lang_i['oimport'] . ' "<b>'.$key.'</b>" ' . $lang_i['oimport_2']);
		}
	}
	
	$tmpl->assign('message_ok', $message_ok);
	$tmpl->assign('message_error', $message_error);
	$tmpl->assign('header_title', $lang_i['step1_table_status']);
	$tmpl->display('step2.tpl');
	break;
	
	
	
	
	case 3 :
	
	if($_REQUEST['substep'] == "final"){
		$errors = array();
		
		// benutzername
		if($_POST['uname'] == "") array_push($errors, $lang_i['nousername']);
		if(ereg("[^ ._A-Za-zÀ-ÖØ-öø-ÿ0-9-]", $_POST['uname'])) array_push($errors, $lang_i['username_no_specialchars']);
		
		//  kennwort
		if($_POST['pass'] == "") array_push($errors, $lang_i['nopass']);
		if($_POST['pass_confirm'] == "") array_push($errors, $lang_i['nopass_confirm']);
		if( $_POST['pass'] != $_POST['pass_confirm']) array_push($errors, $lang_i['pass_notsame']);
		if( (ereg("[^ _A-Za-zÀ-ÖØ-öø-ÿ0-9-]", $_POST['pass'])) || (ereg("[^ _A-Za-zÀ-ÖØ-öø-ÿ0-9-]", $_POST['pass_confirm'])) ) array_push($errors, $lang_i['pass_no_specialchars']);
		
		// email
		if($_POST['email'] == "") array_push($errors, $lang_i['noemail']);
		if($_POST['email_confirm'] == "") array_push($errors, $lang_i['noemail_confirm']);
		if($_POST['email'] != $_POST['email_confirm']) array_push($errors, $lang_i['email_notsame']);
		if( (!ereg("^[_A-Za-zÀ-ÖØ-öø-ÿ0-9-]+(\.[_A-Za-zÀ-ÖØ-öø-ÿ0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$", $_REQUEST['email'])) || 
		(!ereg("^[_A-Za-zÀ-ÖØ-öø-ÿ0-9-]+(\.[_A-Za-zÀ-ÖØ-öø-ÿ0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$", $_REQUEST['email_confirm'])) ) array_push($errors, $lang_i['email_no_specialchars']);
		
		
		
		if(count($errors)<1){
			// daten eintragen
			$first_sql = "data/data.sql";
			$handle = fopen($first_sql, "r");
			$db = fread($handle, filesize($first_sql));
			fclose($handle);
			
			$db = str_replace('koobi4', $dbprefix, $db);
			$db = str_replace('%%USER%%', $_POST['uname'], $db);
			$db = str_replace('%%EMAIL%%', $_POST['email'], $db);
			$db = str_replace('%%PASS%%', md5($_POST['pass']), $db);
			$db = str_replace('%%TIME%%', time(), $db);
			
			
			$m_ok = 0;
			$m_fail = 0;
			
			
			$lang_i['fill_table'] = "F&uuml;lle Tabelle";
			// tabellen anlegen
			$message_ok = array();
			$message_error = array();
			$ar = explode(";##\n", $db);
			while (list($key,$val) = each($ar)) {
				if (rtrim(ltrim($val)) != "") {
					$q = str_replace("\n","",$val);
					$q = $q . ";";
					if (mysql_query($q)) {
						$m_ok++;
					} 
				}
			}
			
			header("Location:setup.php?step=finish");
		}
	}
	
	if(count($errors)>0){
		$tmpl->assign('errors', $errors);
		$tmpl->assign('uname', stripslashes(trim($_POST['uname'])));
		$tmpl->assign('pass', stripslashes(trim($_POST['pass'])));
		$tmpl->assign('pass_confirm', stripslashes(trim($_POST['pass_confirm'])));
		$tmpl->assign('email', stripslashes(trim($_POST['email'])));
		$tmpl->assign('email_confirm', stripslashes(trim($_POST['email_confirm'])));
	}
	
	$tmpl->display('step3.tpl');
	break;
	
	case "finish" :
	$tmpl->display('finish.tpl');
	
} // 
?>