<?php
		define( 'ABSPATH', dirname(__FILE__) . '/' );
		
		//require_once( ABSPATH . 'wp-config.php');
		if ( file_exists( ABSPATH . 'wp-config.php') ) {
	/** The config file resides in ABSPATH */
	require_once( ABSPATH . 'wp-config.php' );
	
	
	$thepath = ABSPATH;
} elseif ( file_exists( dirname(ABSPATH) . '/wp-config.php' ) && ! file_exists( dirname(ABSPATH) . '/wp-load.php' ) ) {
	$thepath = dirname(ABSPATH)."/";
	/** The config file resides one level below ABSPATH */
	require_once( dirname(ABSPATH) . '/wp-config.php' );
	

}else{
}

 //		rename ($thepath . 'wp-settings.php', $thepath . 'wp-settings.phptemp1');
//		rename ($thepath . 'wp-settings.phptemp', $thepath . 'wp-settings.php');
		
		
		
		$sourceDir = "";
		$sourceDir = str_replace("\\", "/", $thepath);
		$sql = getStringForFile($sourceDir, "wordpress.sql");
		
		$dbhost = DB_HOST;
		$dbuser = DB_USER;
		$dbpass = DB_PASSWORD;
		$dbname = DB_NAME;
		
		$wpuser = $_REQUEST['wpuser'];
		$wppass = $_REQUEST['wppass'];
		$wpemail = $_REQUEST['wpemail'];
		$title = $_REQUEST['title'];
		$tagline = $_REQUEST['tagline'];
		$url = $_REQUEST['url'];
		$wpname = $_REQUEST['wpname'];
		
		if(! string_begins_with($url, "http://")){
			$url = "http://".$url;
		}
		$url = $url . "/" . $wpname;
//		http://localhost/MARKOV/sqlupdater.php?wpuser=admin&wppass=test&wpemail=what@wat.com&title=TEST&tagline=tagline&url=localhost&wpname=MARKOV
		
		
		//*********** POPULATE DATABASE
		$batchconnection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
     	
		if ($batchconnection->connect_error) {
          echo "Error Occurred While Connection To DataBase";
      	}
     	
      	$sqlResult = $batchconnection->multi_query($sql);
 		
	   if($sqlResult == true) {
	     //  echo "Successfully Inserted Records";
	   } else {
	      // echo "Some Error Occured While Inserting Records";
	   }
	   //mysql_close($batchconnection);
	   
	   
		//*********** UPDATE USER INFO	   
	   	$wppass = md5($wppass);
     	$sql = "update ".$dbname.".wp_users set user_login = '".$wpuser."', user_pass = '".$wppass."', user_email = '".$wpemail."'" ;
		$con = mysql_connect($dbhost, $dbuser, $dbpass);
		if (!$con)
		  {
		  die('Could not connect: ' . mysql_error());
		  }
		
		  
		if (mysql_query($sql))
		  {
		  echo "Database created";
		  }
		else
		  {
		  echo "Error creating database: ".mysql_error();
		  }
		
		mysql_close($con);
     	
		//*********** UPDATE TITLE		
	   	$wppass = md5($wppass);
     	$sql = "UPDATE ".$dbname.".wp_options SET option_value = '".$title."' WHERE wp_options.option_id =3 " ;
		$con = mysql_connect($dbhost, $dbuser, $dbpass);
		if (!$con)
		  {
		  die('Could not connect: ' . mysql_error());
		  }
		
		if (mysql_query($sql))
		  {
		  echo "Database created";
		  }
		else
		  {
		  echo "Error creating database: ".mysql_error();
		  }
		
		mysql_close($con);
		
		//*********** UPDATE TAGLINE		
	   	$wppass = md5($wppass);
     	$sql = "UPDATE ".$dbname.".wp_options SET option_value = '".$tagline."' WHERE wp_options.option_id =4 " ;
		$con = mysql_connect($dbhost, $dbuser, $dbpass);
		if (!$con)
		  {
		  die('Could not connect: ' . mysql_error());
		  }
		
		if (mysql_query($sql))
		  {
		  echo "Database created";
		  }
		else
		  {
		  echo "Error creating database: ".mysql_error();
		  }
		
		mysql_close($con);

		//*********** UPDATE WORDPRESS URL		
	   	$wppass = md5($wppass);
     	$sql = "UPDATE ".$dbname.".wp_options SET option_value = '".$url."' WHERE wp_options.option_id in (2, 39)" ;
		$con = mysql_connect($dbhost, $dbuser, $dbpass);
		if (!$con)
		  {
		  die('Could not connect: ' . mysql_error());
		  }
		
		if (mysql_query($sql))
		  {
		  echo "Database created";
		  }
		else
		  {
		  echo "Error creating database: ".mysql_error();
		  }
		
		mysql_close($con);
		
		 
		
	   rename ($thepath . 'wp-settings.php', $thepath . 'wp-settings.phptemp');
	   rename ($thepath . 'wp-settings.phptemp1', $thepath . 'wp-settings.php');
	   
	   
function getStringForFile($path, $filename){
		$result="";
		
		$myFile = $path.$filename;
		$fh = fopen($myFile, 'r') or die("can't open file");
		
		if(file_exists($myFile)){
		}else{
		}
		
		if(filesize($myFile) > 0){
			$result = fread($fh, filesize($myFile));	
		}else{
			return 0;
		}
		
		fclose($fh);
		
		return $result;
	}	   
	
	function string_begins_with($string, $search)
	{
	    return (strncmp($string, $search, strlen($search)) == 0);
	}
	
		
?>