<?php
error_reporting(1);
class Database {
	
	function create_database($data)
	{
		$mysqli = new mysqli($data['hostname'],$data['username'],$data['password'],'');
		if(mysqli_connect_errno())
			return false;
		$mysqli->query("CREATE DATABASE IF NOT EXISTS ".$data['database']);
		$mysqli->close();
		return true;
	}

	function create_tables($data)
	{
		$mysqli = new mysqli($data['hostname'],$data['username'],$data['password'],$data['database']);
		if(mysqli_connect_errno())
		return false;
		
		if (!$mysqli->select_db($data['database'])) {
		    $msg = "Sorry ".$data['database']." databse NOT EXISTS Please import The database first";
			return ['st'=>false,'msg'=>$msg];
		}else{
			
			if ($result = $mysqli->query("select 1 from setting")) {
				return ['st'=>true,'msg'=>''];
			}else {
				$msg = "sorry Setting table not exits";
				return ['st'=>false,'msg'=>$msg];
			}

			
		}
	}

	function registration($data){
			$conn = new mysqli($data['hostname'],$data['username'],$data['password'],$data['database']);
			if(mysqli_connect_errno())
				return false;

				$siteUrl =   str_replace(basename($_SERVER['SCRIPT_NAME']), '',$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']);
				$siteUrl = str_replace('system_config', '', $siteUrl);
				$siteUrl = preg_replace('#(^https?:\/\/(w{3}\.)?)|(\/$)#', '', $siteUrl);


			$conn->query("SET sql_mode = ''");
			$version = 4.3;

			$form_data = array(
				'site_name' => $data['base_url'],
				'username'  => $data['name'],
				'email'  => $data['email'],
				'script_name'  => "portfolio",
				'version'  => $version,
			);

			$form_data = json_encode($form_data);
			$ch = curl_init("https://thinkncode.net/site_api/install-script");  
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                     
			curl_setopt($ch, CURLOPT_POSTFIELDS, $form_data);                   
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
			curl_setopt($ch, CURLOPT_POST, 1);                                          
			$result = curl_exec($ch);
			curl_close($ch);
			$result = json_decode($result);
			$username = mysqli_real_escape_string($conn,$data['name']);
			$email = mysqli_real_escape_string($conn,$data['email']);
			$password = md5(mysqli_real_escape_string($conn,$data['pass']));
			$user_type =  1;
			$site_id =  $result->site_id;
			if(isset($site_id) && $site_id !=0):
				$dt = new DateTime('now',new DateTimezone('Asia/Dhaka'));
	       		$date = $dt->format('Y-m-d H:i:s');

				$get = "SELECT * FROM setting WHERE site_id ='$site_id'";
				$result = $conn->query($get); 
				
				if($result->num_rows > 0){
					$update = "UPDATE users SET username='$username',email='$email',password='$password',user_type=$user_type WHERE user_type=1";

					$update = "UPDATE setting SET site_id=$site_id,version=$version,created_at='$date',site_url='$siteUrl' WHERE id=1";

					$insert = $conn->query($update);

				}else{

					$sql = "INSERT INTO users (username,email,password,user_type) VALUES ('$username','$email','$password',$user_type)";

					$sql = "INSERT INTO setting (site_id,version,created_at,site_url) VALUES ($site_id,$version,'$date','$siteUrl')";

					$insert = $conn->query($sql);
				}
				if($insert){
					return true;
				}else{
					echo $conn->error;
				}
			endif;
	}

}



