<?php

require ("includes/check_auth_user.php");
require ("includes/dbconnect.php");
include ('includes/function.php');

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

if (isset($_POST["submit"])) {


    $message = substr($_POST["sms"], 0, 320);

    $msg = $_POST["sms"];
    
    $getcomp = mysqli_query($conn, "Select user, password from company ") or die(mysqli_error($conn));
    $fetcomp = mysqli_fetch_array($getcomp);
    $muser = mysqli_real_escape_string($conn, $fetcomp['user']);
    $mpassword = mysqli_real_escape_string($conn, $fetcomp['password']);
    $user = $muser;
    $password = $mpassword;
    $message = "$msg";
    $senderid = $muser;
    $alert = $_POST["alert"];
//    $url = "144.76.39.175/api.php?";
    $url = "http://vas.mobilogi.com/api.php?";


    $mobilenumbers = array();
//    browse($mobilenumbers);

//        $message = urlencode($message);

    foreach ($_POST['check_list'] as $to) {
//            $mobilenumbers .= $_POST['check_list'][$i]."|";
//    $ctr = 0;
//    for ($i = 0; $i<=$_POST['selchk']; $i++) {
//    
//browse($_POST['check_list']);
//        $to = $_POST['selchk_'.$i];
        $mobilenumbers = "$to,$alert";

//        browse($message);
//        browse($mobilenumbers);

        $ch = curl_init();
        if (!$ch) {
            die("Couldn't initialize a cURL handle");
        }


        $ret = curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$user&password=$password&route=1&sender=$senderid&mobile[]=$mobilenumbers&message[]=$message");

        $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $curlresponse = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'curl error : ' . curl_error($ch);
        }
        if (empty($ret)) {

            die(curl_error($ch));
            curl_close($ch);
        } else {
            $info = curl_getinfo($ch);
            curl_close($ch);

            echo $curlresponse;
        }
    }
//    header("refresh: 1;menu.php?action=smsaddress");
echo "<script>
            parent.window.location.reload();
            </script>";        
}
?>
    
