
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php
session_start();

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $mobile = $_POST['mobile'] ?? '';

    if (!isset($_SESSION['otp_attempts'])) {
        $_SESSION['otp_attempts'] = 0;
    }

    if ($_SESSION['otp_attempts'] < 3) {
        $_SESSION['otp'] = rand(100000, 999999); // Generate new OTP
        $_SESSION['otp_attempts'] += 1;

        // Simulate sending OTP (Replace with actual SMS API)
        
        //$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);
             
    
}

$msg = "Your verfication code is: ".$_SESSION['otp'];

send_sms2($mobile,$msg);
        
        
        echo "تم إرسال رمز التحقق الجديد إلى " . $mobile;
    } else {
        echo "لقد تجاوزت الحد الأقصى لإعادة إرسال الرمز.";
    }
} else {
    echo "invalid"; // Invalid request method
}
?>