<?php

include "../core/dbInfo.php";
include_once "../core/dbFunctions.php";


$mobile=$_REQUEST['mobile'];
if ($mobile!='') {

$otp=rand(1000,9999);

$sql_check = "Select * from users WHERE mobile='$mobile' AND is_active = 1";
$res_check = getXbyY($sql_check);
$DB_MOBILE=$res_check[0]['mobile'];
if ($DB_MOBILE==$mobile) {
        
$sql="UPDATE users set otp='$otp' WHERE mobile='$mobile' AND is_active = 1";
$SQL=setXbyY($sql);    

        
        $msg='Dear User Your UkReCharge App Login OTP verification code for is '.$otp.' Never Share Your OTP to Any Body.';
        
        
function connect($no, $msg){

// API URL
$url = 'http://wa.mpedia.website/send-message';

// Data to be sent in the POST request
$data = array(
    'api_key' => 'RHMbvTIy6YRXAn1BMMZlJue7FlMgwuIx',
    'sender' => '919306244578',
    'number' => '91'.$no.'',
    'message' => ''.$msg.''
);

// Initialize cURL session
$ch = curl_init($url);

// Set cURL options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute cURL session and store the response
$response = curl_exec($ch);

return  $response;   
}

connect($mobile,$msg);
        
        

        echo '{"error":"0",error_msg":"OTP Sended Successfully '.$otp.' "}';
    }else{

 $mobile=$_REQUEST['mobile'];        
    $msg='Dear User Your UkReCharge App Register OTP verification code for is '.$otp.' Never Share Your OTP to Any Body.';
        $user_msg=urlencode("$msg");
   connect($mobile,$msg);   
    
            
    
$sql="INSERT INTO user_otp (mobile, otp) VALUES ('$mobile','$otp')";   
$SQL=setXbyY($sql); 
echo '{"error":"2",error_msg":"New User "}';   

}
}else{
 echo '{"error":"3",error_msg":"Enter Mobile"}';       
}