<?php
error_reporting(0); 

// physical path of your root
define( 'ABSPATH', str_replace( '\\', '/',  dirname(dirname( __FILE__ ) ))  );
// physical path of includes directory
define( 'CONFIG_PATH', ABSPATH.'/conf' );	
define( 'INSTALL_PATH', ABSPATH.'/setup' );	

//Config include
require_once(CONFIG_PATH."/my_config.php");

//Check if config exists
if(!is_readable(CONFIG_PATH.'/my_config.php')){
echo '<h1>Hold on! Configuration file ismissing! </h1><br />';
echo 'To continue: <strong>edit database and site details</strong> in file vibe_config.php <br /> and after the setup runs delete the file named hold in root ( '.ABSPATH.'/hold ) <br />';
die();
}	


$base_href_path = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME);

$base_href_protocol = ( array_key_exists('HTTPS', $_SERVER) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http' ).'://';
if( array_key_exists('HTTP_HOST', $_SERVER) && !empty($_SERVER['HTTP_HOST']) )
{
	$base_href_host = $_SERVER['HTTP_HOST'];
}
elseif( array_key_exists('SERVER_NAME', $_SERVER) && !empty($_SERVER['SERVER_NAME']) )
{
	$base_href_host = $_SERVER['SERVER_NAME'].( $_SERVER['SERVER_PORT'] !== 80 ? ':'.$_SERVER['SERVER_PORT'] : '' );
}
$base_href = rtrim( $base_href_protocol.$base_href_host.$base_href_path, "/" ).'/';
$site_link = parse_url($base_href);
$site_link = $site_link['host'];
$site_url = $base_href;
define( 'ADMINCP',"/");		
require_once( INSTALL_PATH.'/functions.php' );

// put header
print setup_header();

// steps
if(isset($_GET["steps"])){
$steps=$_GET["steps"];
}else{
$error = 0;

if (!is_writable(ABSPATH.'/custom/domain_1/tmp')) {
echo '<div class="msg-warning">tmp folder ('.ABSPATH.'/custom/domain_1/tmp) is not writeable</div>';
$error++;
}
if (!is_writable(ABSPATH.'/custom/domain_1/cache_filter')) {
echo '<div class="msg-warning">('.ABSPATH.'/custom/domain_1/cache_filter) is not writeable</div>';
$error++;
}
if (!is_writable(ABSPATH.'/custom/domain_1/cache_full')) {
echo '<div class="msg-warning">('.ABSPATH.'/custom/domain_1/cache_full) is not writeable</div>';
$error++;
}
if (!is_writable(ABSPATH.'/custom/domain_1/cache_partial')) {
echo '<div class="msg-warning">('.ABSPATH.'/custom/domain_1/cache_partial) is not writeable</div>';
$error++;
}
if (!is_writable(ABSPATH.'/custom/domain_1/cacheExpirationQueries')) {
echo '<div class="msg-warning">('.ABSPATH.'/custom/domain_1/cacheExpirationQueries) is not writeable</div>';
$error++;
}
if (!is_writable(ABSPATH.'/custom/domain_1/cacheUpdateToken')) {
echo '<div class="msg-warning">('.ABSPATH.'/custom/domain_1/cacheUpdateToken) is not writeable</div>';
$error++;
}
if (!is_writable(ABSPATH.'/custom/domain_1/cacheVerbose')) {
echo '<div class="msg-warning">('.ABSPATH.'/custom/domain_1/cacheVerbose) is not writeable</div>';
$error++;
}
if (!is_writable(ABSPATH.'/custom/domain_1/content_files')) {
echo '<div class="msg-warning">('.ABSPATH.'/custom/domain_1/content_files) is not writeable</div>';
$error++;
}
if (!is_writable(ABSPATH.'/custom/domain_1/editor_files')) {
echo '<div class="msg-warning">('.ABSPATH.'/custom/domain_1/editor_files) is not writeable</div>';
$error++;
}
if (!is_writable(ABSPATH.'/custom/domain_1/export_files')) {
echo '<div class="msg-warning">('.ABSPATH.'/custom/domain_1/export_files) is not writeable</div>';
$error++;
}
if (!is_writable(ABSPATH.'/custom/domain_1/image_files')) {
echo '<div class="msg-warning">('.ABSPATH.'/custom/domain_1/image_files) is not writeable</div>';
$error++;
}
if (!is_writable(ABSPATH.'/custom/domain_1/extra_files')) {
echo '<div class="msg-warning">('.ABSPATH.'/custom/domain_1/extra_files) is not writeable</div>';
$error++;
}
if (!is_writable(ABSPATH.'/custom/domain_1/import_files')) {
echo '<div class="msg-warning">('.ABSPATH.'/custom/domain_1/import_files) is not writeable</div>';
$error++;
}




if(!extension_loaded('mbstring')) { 
echo '<div class="msg-warning">Seems your host misses the mbstring extension </div>';
 $error++;
}if(!extension_loaded('Safemode')) { 
echo '<div class="msg-hint">Safemode enabled on this server please  turn it off </div>';
 }if(!extension_loaded('cURL')) { 
echo '<div class="msg-warning">cURL not enabled </div>';
$error++;
}if(!extension_loaded('JSON')) { 
echo '<div class="msg-warning">JSON not enabled </div>';
$error++;
} if(!extension_loaded('gd') && function_exists('gd_info')) { 
echo '<div class="msg-warning">GD 2+ Image libraries need it for this script </div>';
$error++;
}if(!extension_loaded('mcrypt')) { 
echo '<div class="msg-warning">mcrypt not enabled </div>';
$error++;
}if (phpversion() < 5) {
echo '<div class="msg-warning">Error: phpVibe needs php 5.0+ (your version is '.phpversion().' )</div>';
$error++;
} if (phpversion() < 5.3) {
echo '<div class="msg-hint">Recomended php version is 5.3 (and above)  while your version is '.phpversion().'! However, this is optional.</div>';
} if($error > 0) {
echo '<div class="msg-warning">Please correct the '.$error.' errors listed above to continue.</div>';
die();
}else{
?>





<h3> Main Database Connection </h3>

<form id="validate" class="form-horizontal styled" action="<?php echo $base_href;?>?steps=1" enctype="multipart/form-data" method="post">
<fieldset>
<div class="control-group">
<label class="control-label">Database host</label>
<div class="controls">
<input type="text" name="db_host" class="validate[required] span6" value="" /> 						
<span class="help-block" id="limit-text">please enter your database host (localhost)</span>
</div>	
</div>
<div class="control-group">
<label class="control-label">Database username</label>
<div class="controls">
<input type="text" name="db_username" class="validate[required] span6" value="" /> 						
<span class="help-block" id="limit-text">please enter your database username</span>
</div>	
</div>	
<div class="control-group">
<label class="control-label">Database password</label>
<div class="controls">
<input type="text" name="db_password" class=" span6" value="" /> 						
<span class="help-block" id="limit-text">please enter your database password</span>
</div>	
</div>
<div class="control-group">
<label class="control-label">Database name</label>
<div class="controls">
<input type="text" name="db_name" class="validate[required] span6" value="" /> 						
<span class="help-block" id="limit-text">please enter your database name</span>
</div>	
</div>			

<div class="control-group">
<button class="btn btn-large btn-primary pull-right" type="submit" onclick="">Next ></button>	
</div>	
</fieldset>					
</form>
<?php
die();
}
}
if($steps=="lic"){
	$post_details = $_POST;

	   $root_qaidi = base64_encode($_SERVER["DOCUMENT_ROOT"]);
   $root_qaidi = substr_replace($root_qaidi, "", -1);
   $check_qaidi=file_get_contents("http://project.qaidi.info/edir/check.php?serial=".QAIDI_LICENSE."&ip=".$_SERVER['SERVER_ADDR']."&dir=".$root_qaidi."&domain=".$_SERVER['HTTP_HOST']."");
        if($check_qaidi==0) 
		{
		die('<div class="msg-warning">MySQL Connection error</div><button class="btn btn-large btn-primary pull-right" type="submit" onclick="history.go(-1);">< Back</button>');

		}else{
	$create_lic = create_config($post_details);
	$page_content = '<p>' . $create_lic['display'] . '</p> ';
	if( $create_lic['result'] == true){
	$lic=true;
	}
	?>
<form id="validate" class="form-horizontal styled" action="<?php echo $base_href;?>?steps=2" enctype="multipart/form-data" method="POST">
<fieldset>
<?php echo $page_content; ?>
<div class="control-group">
<button class="btn btn-large btn-primary pull-right" type="submit" onclick="">Next ></button>
<button class="btn btn-large btn-primary pull-left" type="button" onclick="history.go(-1);">< Back</button>	
</div>	
</fieldset>					
</form>
	
	<?php
		}
die();

}elseif($steps=="1"){
	$lic=true;
	if( $lic == true){
	
	$post_details = $_POST;

// check  MySQL
$link = mysql_connect($post_details['db_host'], $post_details['db_username'], $post_details['db_password']);
if (!$link) {
die('<div class="msg-warning">MySQL Connection error</div><button class="btn btn-large btn-primary pull-right" type="submit" onclick="history.go(-1);">< Back</button>');
}
if (!mysql_select_db($post_details['db_name'], $link)) {
die('<div class="msg-warning">MySQL Database Connection error</div><button class="btn btn-large btn-primary pull-right" type="submit" onclick="history.go(-1);">< Back</button>');
}
	$create_config = create_config($post_details);
	$page_content = '<p>' . $create_config['display'] . '</p> ';
		if( $create_config['result'] == true){
	$step1=true;
	}
	?>
<h3> Creating database configuration  file </h3>
<form id="validate" class="form-horizontal styled" action="<?php echo $base_href;?>?steps=2" enctype="multipart/form-data" method="POST">
<fieldset>
<?php echo $page_content; ?>
<div class="control-group">
<button class="btn btn-large btn-primary pull-right" type="submit" onclick="">Next ></button>
<button class="btn btn-large btn-primary pull-left" type="button" onclick="history.go(-1);">< Back</button>	
</div>	
</fieldset>					
</form>
<?php
mysqli_close($link);

die();
}
}elseif ($steps=="2"){

$path      = "db/";
// SQL File
$SQLFile = $path."main.sql"; // Filename of dump, default: dump.sql
// Connect MySQL
$link = mysql_connect(_DIRECTORYDB_HOST, _DIRECTORYDB_USER, _DIRECTORYDB_PASS);
if (!$link) {
die('<div class="msg-warning">MySQL Connection error</div><button class="btn btn-large btn-primary pull-right" type="submit" onclick="history.go(-1);">< Back</button>');
}
if (!mysql_select_db(_DIRECTORYDB_NAME, $link)) {
die('<div class="msg-warning">MySQL Database Connection error</div><button class="btn btn-large btn-primary pull-right" type="submit" onclick="history.go(-1);">< Back</button>');
}
if(mysql_query("DESCRIBE `domain`")) {
die('<div class="msg-warning">The tables already exist!</div> <button class="btn btn-large btn-primary pull-right" type="submit" onclick="history.go(-1);">< Back</button>');
}

/* * * Use Function Like This: ** */
MultiQuery($SQLFile);
if(MultiQuery($SQLFile) == "1"){
echo '<form id="validate" class="form-horizontal styled" action="'.$base_href.'?steps=3" enctype="multipart/form-data" method="POST"><div class="msg-win">Database successfully installed!</div><button class="btn btn-large btn-primary pull-right" type="submit" onclick="" autofocus="autofocus">Next ></button></form>';
?>
<script>
$(function() {
$('body').scrollTo('#validate');
  });
</script>
<?php

}else{
die("unsespected error");
}
mysqli_close($link);

die();

}elseif ($steps=="3"){
?>
<h3> Domain Database Connection </h3>

<form id="validate" class="form-horizontal styled" action="<?php echo $base_href;?>?steps=4" enctype="multipart/form-data" method="post">
<fieldset>
<div class="control-group">
<label class="control-label">domain name</label>
<div class="controls">
<input type="text" name="domain_name" class="validate[required] span6" value="" /> 						
<span class="help-block" id="limit-text">please enter domain name (without www.)</span>
</div>	
</div>
<div class="control-group">
<label class="control-label">Database host</label>
<div class="controls">
<input type="text" name="db_host" class="validate[required] span6" value="<?php echo _DIRECTORYDB_HOST;?>" /> 						
<span class="help-block" id="limit-text">please enter your database host (localhost)</span>
</div>	
</div>
<div class="control-group">
<label class="control-label">Database username</label>
<div class="controls">
<input type="text" name="db_username" class="validate[required] span6" value="<?php echo _DIRECTORYDB_USER;?>" /> 						
<span class="help-block" id="limit-text">please enter your database username</span>
</div>	
</div>	
<div class="control-group">
<label class="control-label">Database password</label>
<div class="controls">
<input type="text" name="db_password" class=" span6" value="<?php echo _DIRECTORYDB_PASS;?>" /> 						
<span class="help-block" id="limit-text">please enter your database password</span>
</div>	
</div>
<div class="control-group">
<label class="control-label">Database name</label>
<div class="controls">
<input type="text" name="db_name" class="validate[required] span6" value="" /> 						
<span class="help-block" id="limit-text">please enter your  domain database name</span>
</div>	
</div>			

<div class="control-group">
<button class="btn btn-large btn-primary pull-right" type="submit" onclick="">Next ></button>	
</div>	
</fieldset>					
</form>
<?php

die("");
}elseif ($steps=="4"){
	$db_details = $_POST;
$path      = "db/";
// SQL File
$SQLFile = $path."domain.sql"; 
// Connect MySQL
$link = mysql_connect($db_details['db_host'], $db_details['db_username'], $db_details['db_password']);
if (!$link) {
die('<div class="msg-warning">MySQL Connection error</div><button class="btn btn-large btn-primary pull-right" type="submit" onclick="history.go(-1);">< Back</button>');
}
if (!mysql_select_db($db_details['db_name'], $link)) {
die('<div class="msg-warning">MySQL Database Connection error</div><button class="btn btn-large btn-primary pull-right" type="submit" onclick="history.go(-1);">< Back</button>');
}
if(mysql_query("DESCRIBE `accountprofilecontact`")) {
die('<div class="msg-warning">The tables already exist!</div> <button class="btn btn-large btn-primary pull-right" type="submit" onclick="history.go(-1);">< Back</button>');
}

/* * * Use Function Like This: ** */
MultiQuery($SQLFile);
if(MultiQuery($SQLFile) == "1"){
echo '<form id="validate" class="form-horizontal styled" action="'.$base_href.'?steps=5" enctype="multipart/form-data" method="POST"><div class="msg-win">Database successfully installed!</div><button class="btn btn-large btn-primary pull-right" type="submit" onclick="" autofocus="autofocus">Next ></button></form>';
?>
<script>
$(function() {
$('body').scrollTo('#validate');
  });
</script>
<?php

$date_time = db_formatString(date("Y-m-d H:i:s"));
mysqli_close($link);
mysql_connect(_DIRECTORYDB_HOST, _DIRECTORYDB_USER, _DIRECTORYDB_PASS);
mysql_select_db(_DIRECTORYDB_NAME);
$sql = "INSERT INTO Domain (smaccount_id,name,database_host,database_port,database_username,database_password,database_name,url,status,activation_status,created,deleted_date,article_feature,banner_feature,classified_feature,event_feature,subfolder)"
 . " VALUES ('1','". $db_details['domain_name'] ."','" . $db_details['db_host'] . "','3306','" . $db_details['db_username'] . "','".$db_details['db_password']."','".$db_details['db_name']."','".$site_link."','A','A',". $date_time .",'0000-00-00','on','on','on','on','/')";
mysql_query($sql);
	$create_domain_file = create_domain_file($site_link);
	$create_domain_file = '<p>' . $create_domain_file['display'] . '</p> ';
	echo $create_domain_file;
}else{
die("unsespected error");
}
mysqli_close();

die();

}elseif ($steps=="5"){

?>
<h3>Admin account</h3>

<form id="validate" class="form-horizontal styled" action="<?php echo $base_href;?>?steps=6" enctype="multipart/form-data" method="post">
<fieldset>
<div class="control-group">
<label class="control-label">Admin Email</label>
<div class="controls">
<input type="text" name="admin_email" class="validate[required] span6" value="" /> 						
<span class="help-block" id="limit-text">Please enter your Admin Email</span>
</div>	
</div>


<div class="control-group">
<label class="control-label">Admin password</label>
<div class="controls">
<input type="text" name="admin_pass" class="validate[required] span6" value="" /> 						
<span class="help-block" id="limit-text">Please enter your Admin password</span>
</div>	
</div>
	

<div class="control-group">
<button class="btn btn-large btn-primary pull-right" type="submit" onclick="">Next ></button>
<button class="btn btn-large btn-primary pull-left" type="button" onclick="history.go(-1);">< Back</button>	
</div>	
</fieldset>					
</form>
<?php
die();

}elseif ($steps=="6"){
	$posts = $_POST;
$date_time = db_formatString(date("Y-m-d H:i:s"));
mysqli_close($link);
mysql_connect(_DIRECTORYDB_HOST, _DIRECTORYDB_USER, _DIRECTORYDB_PASS);
mysql_select_db(_DIRECTORYDB_NAME);

 

$sql = "INSERT INTO Setting (name,value)"
. " VALUES ('sitemgr_password','" . md5($posts['admin_pass']) . "')";
 mysql_query($sql);

$sql2 = "INSERT INTO Setting (name,value)"
. " VALUES ('sitemgr_username','" . $posts['admin_email'] . "')";
 mysql_query($sql2);
 mysqli_close();

$new_url = str_replace("/setup/","",$site_url);


 echo '<div class="msg-win">All done. Remember to remove Setup folder in root.</div>';
 
  echo '<div class="msg-info"><b>Login information to your Site Manager Account </b> </br> 
E-mail Address; '.$posts['admin_email'].'</br>
Password; '.$posts['admin_pass'].'</br>

 </div>';

echo '<a class="btn btn-large btn-primary pull-right" href="'.$new_url.'/sitemgr/">Setup is complete. Login</a>';


die("");

}


echo '
</div>
</div>
</div>
</body>
</html>
';

ob_end_flush();
?>