<?php
include '../../conn.php';

$name=$_POST['name'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$password=$_POST['password'];
$refcoupon=$_POST['refercoupon'];

          $sql ="select email from `users` where email='$email' ";
            $result = mysqli_query($con,$sql);
            $noofuser=0;
            while($row = mysqli_fetch_array($result)){
             $noofuser=$noofuser + 1;
			}
			$refcouponmobile=null;
			$sqlrefcoupon ="select * from `users` where myCoupon='$refcoupon' ";
            $resultrefcoupon = mysqli_query($con,$sqlrefcoupon);
          
            while($rowrefcoupon = mysqli_fetch_array($resultrefcoupon)){
                  $refcouponmobile=$rowrefcoupon['phone'];
			}
// 			echo $refcouponmobile;
if($noofuser == 0){

$valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp' , 'pdf' , 'doc' , 'ppt'); // valid extensions
$path = 'images/'; // upload directory

$img = $_FILES['imagefile']['name'];
$tmp = $_FILES['imagefile']['tmp_name'];
// get uploaded file's extension
$ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));
// can upload same image using rand function
$final_image = rand(1000,1000000).$img;
// check's valid format
		if(in_array($ext, $valid_extensions)) 
		{ 
		$path = $path.strtolower($final_image); 
		move_uploaded_file($tmp,'../../'.$path);
	    }
$path=$baseimage.$path;
// $mycoupon=uniqid();
// $mycoupon=base64_encode($mycoupon);
$mycoupon = substr(md5(mt_rand()), 0, 6);
$insert = $con->query("INSERT INTO `users`(`email`, `password`,`name`, `phone`, `image`,  `myCoupon`)values ('$email','$password','$name','$phone','$path','$mycoupon')");


///////////////////////////////otp//////////////////////
// 	Authorisation details.
// Account details
$username = "petnvets@gmail.com";
	$hash = "044e4618ff9f90426920ea7f4e57bb3bb7c6f153cee7bedee3db9596f1ff7681";

	// Config variables. Consult http://api.textlocal.in/docs for more info.
	$test = "0";

	// Data for text message. This is the text message data.
	$sender = "TXTLCL"; // This is who the message appears to be from.
	$numbers = $refcouponmobile; // A single number or a comma-seperated list of numbers
	$message = "Your refer code are use other man";
	// 612 chars or less
	// A single number or a comma-seperated list of numbers
	$message = urlencode($message);
	$data = "username=".$username."&hash=".$hash."&message=".$message."&sender=".$sender."&numbers=".$numbers."&test=".$test;
	$ch = curl_init('http://api.textlocal.in/send/?');
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$result = curl_exec($ch); // This is the result from the API
	curl_close($ch);
// 	echo $result;
////////////////////////////////otp////////////////////////

if($insert > 0)
{ 
 
 echo json_encode('101');
}
}
else if($noofuser != 0){
  
 echo json_encode('102');
}
else{
 echo json_encode('Some thing wrong!!!');
}

?>
