<?php

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

$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];

mysqli_select_db($conn,$zdb);
//browse($zdb);
error_reporting(E_ALL);
if (isset($_POST["submit"])) {



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

//    $cond = "";
//    if (!empty($_POST["groupmast"])) {
//        $district = trim($_POST["groupmast"]);
//        $cond = "AND `code` = '$district'";
//    }
    
    if (!empty($groupmast)) {
        $groupmastcond = " and accode in (select memcode from grouptran where code != '' and `code`='$groupmast')";
    }

    $catcond = "";
    if (!empty($_POST["member"])) {
        $catgcode = trim($_POST["member"]);
        $catcond = "AND `accode` = '$catgcode'";
    }
    $regncond = "";
    if (!empty($_POST["region"])) {
        $region = trim($_POST["region"]);
        $regioncond = "AND `region` = '$region'";
    }
    $pincond = "";
    if (!empty($_POST["pin"])) {
        $pin = trim($_POST["pin"]);
        $pincond = "AND `pin` = '$pin'";
    }
//    print $catcond;

    $msg = $_POST["sms"];

    $ctr = 0;
    $to = $_POST["mobile"];

    $query = mysqli_query($conn,"SELECT RIGHT(`mobile`,10) as `mobile` FROM `finallist` WHERE LENGTH(`mobile`) >= 10 $catcond $regioncond $groupmastcond $pincond") or die(mysqli_error($conn));
    while ($pax = mysqli_fetch_array($query)) {
        $ctr++;
        $to.= "$pax[mobile],";
    }
//    $to .= "8652372200,8108364186,9819999786";
//    BROWSE($to);
    /* $user = "tamboli2013";
      $password = "tamboli2013"; */
    $user = "ASIFDN";
    $password = "ASIFDN@2018";
    $mobilenumbers = "$to";
    $message = "$msg";
    $senderid = "ASIFDN";

//**************** old api ***************
//$url = "http://vas.mobilogi.com/API/WebSMS/Http/v1.0a/index.php?";
//$message = urlencode($message);
//$ch = curl_init();
//echo "$user";
//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_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
////curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$user&password=$password&to=$mobilenumbers&sender=$senderid&text=$message&response='Y'&unicode='Y'");
//curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$user&password=$password&sender=$senderid&to=$mobilenumbers&message=$message&response='Y'&unicode='Y'");
//$url = "https://vas.mobilogi.com/api.php?";
    $url = "144.76.39.175/api.php?";
    $message = urlencode($message);
    $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_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$user&password=$password&to=$mobilenumbers&sender=$senderid&text=$message&response='Y'&unicode='Y'");
//curl_setopt($ch, CURLOPT_POSTFIELDS, "username=1$user&password=$password&sender=$senderid&to=$mobilenumbers&message=$message&response='Y'&unicode='Y'");
//http://vas.mobilogi.com/api.php?username=tamboli2013&password=tamboli2013&route=1&sender=TMBOLI&mobile[]=8082372012&message[]=%22TEST%22
    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;
    }
    $file = 'smsaddress.php';
//    header("Location: http://www.tambolitours.com/main.php?action=smsaddress");
    header("refresh: 1; $file");
}
?>
