
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php
session_start();
header("Content-Type: application/json");

if (!isset($_POST['mobile'])) {
    echo json_encode(["success" => false, "message" => "Mobile number required."]);
    exit;
}

function convert_persian_numbers ($string) {
    $newNumbers = range(0, 9);
    // 1. Persian HTML decimal
    $persianDecimal = array('&#1776;', '&#1777;', '&#1778;', '&#1779;', '&#1780;', '&#1781;', '&#1782;', '&#1783;', '&#1784;', '&#1785;');
    // 2. Arabic HTML decimal
    $arabicDecimal = array('&#1632;', '&#1633;', '&#1634;', '&#1635;', '&#1636;', '&#1637;', '&#1638;', '&#1639;', '&#1640;', '&#1641;');
    // 3. Arabic Numeric
    $arabic = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
    // 4. Persian Numeric
    $persian = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');

    $string =  str_replace($persianDecimal, $newNumbers, $string);
    $string =  str_replace($arabicDecimal, $newNumbers, $string);
    $string =  str_replace($arabic, $newNumbers, $string);
    return str_replace($persian, $newNumbers, $string);
}

$mobile = convert_persian_numbers($_POST['mobile']);
$otp = rand(100000, 999999);
$_SESSION['otp'] = $otp;
$_SESSION['otp_attempts'] = 3;

// TODO: Integrate with SMS API to send OTP
// Example: send_sms($mobile, "Your OTP is $otp");

// For testing without SMS integration
//error_log("Generated OTP for $mobile: $otp");

$msg = "Your verfication code is: ".$otp;

function generateRandomBasedOnTime() {
    // Get the current date and time
    $date = date('YmdHis'); // Format: YearMonthDayHourMinuteSecond

    // Get the current microtime with microseconds
    $microtime = microtime();
    
    // Split the microtime into seconds and microseconds
    list($microseconds, $seconds) = explode(' ', $microtime);
    
    // Remove the decimal point from microseconds for integer combination
    $microseconds = str_replace('.', '', $microseconds);
    
    // Combine date, seconds, and microseconds into a single string
    $combined = $date . $seconds . $microseconds;
    
    // Convert the combined string into an integer and hash it for randomness
    $random = abs(crc32($combined) + mt_rand(0, 9999));

    return $random;
}

function send_sms2($number,$msg){

$username = "balubaidy8r60j1czfbbh6td";
$password = "lpfl8lzwmgsntg9uddaaoe74lz2yjsmg";    

 $rand = generateRandomBasedOnTime();

$number = "966".ltrim($number,"0");

$to = $number;
//$msg = $_POST['message'];
//$_GET['fromInput'] = $_POST['sender'];


//print_r($_POST);

//if($key == 'TGFWYUxvb24tSGFyYW0tVGFuYXFvbA=='){

       //$to = "966".ltrim($_GET['toInput'],"0");
        if ($to <> '') {
            $from = $_GET['fromInput'];
            //$messageId = $_POST['messageIdInput'];
            $text = $msg;
            //$text = $_GET['textInput'];
            //$notifyUrl = $_POST['notifyUrlInput'];
           // $notifyContentType = $_POST['notifyContentTypeInput'];
            //$callbackData = $_POST['callbackDataInput'];
           // $username = $_GET['username'];
        //    $password = $_GET['password'];

            $postUrl = "https://api.goinfinito.me/unified/v2/send";

            // creating an object for sending SMS
            $destination = array("to" => array("phoneNumber" => $to));
                
           // $whatsappMessage = array("text" => $msg
        //    , "imageUrl" => $attachment
         //   );
            $smsMessage = array("text" => "Failover Message API Testing");
            
            $message = '{
  "apiver": "1.0",
    "sms": {
    "ver": "2.0",
      "dlr": {
      "url": ""
    },
    "messages": [{
      "udh": "0",
      "coding": 2,
      "text": "'.$msg.'",
      "property": 0,
      "id": "'.$rand.'",
      "addresses": [{
        "from": "AUTOZONE",
        "to": "'.$number.'",
        "seq": "'.$rand.'",
        "tag": "AUTOZONE"
      }]
    }]
  }
}';

            $postData = $message;
            //$postDataJson = json_encode($postData);
            
            $postDataJson = $postData;

            $ch = curl_init();
            $header = array("Content-Type:application/json", "Accept:application/json",);

            curl_setopt($ch, CURLOPT_URL, $postUrl);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
            curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataJson);

            // response of the POST request
            $response = curl_exec($ch);
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            $responseBody = json_decode($response);
            curl_close($ch);
//print_r($response);
//return $response;
//return $message;
//print_r($postDataJson);

        } else {
            
  //          echo '<div class="alert alert-danger" role="alert">
//                <b>An error occurred!</b> Reason: Phone number is missing
  //          </div>';
            
        }

//file_put_contents('smslog.txt', $response);

error_log($response);
             
    
}

send_sms2($mobile,$msg);

echo json_encode(["success" => true, "message" => "OTP sent successfully."]);
?>