<?php

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

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

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



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

    $cond = "";
    if (!empty($_POST["district"])) {
        $district = trim($_POST["district"]);
        $cond .= "AND `district` = '$district'";
    }

    if (!empty($_POST["state"])) {
        $state = trim($_POST["state"]);
        $cond .= "AND `state` = '$state'";
    }

    if (!empty($_POST["for"])) {
        $for = trim($_POST["for"]);
        $cond .= "AND `visitingfor` = '$for'";
    }

    $ccond = "";
    if (!empty($_POST["airline"])) {
        $air = trim($_POST["airline"]);
        $ccond .= "AND `airline_code` = '$air'";
    }

    if (!empty($_POST["flightno"])) {
        $flightno = trim($_POST["flightno"]);
        $ccond .= "AND `flightno` = '$flightno'";
    }

    if (!empty($_POST["bjdate"])) {
        $date1 = $_POST["bjdate"];
        $ccond .= "AND `dept_date` = '$date1'";
    }

    if (!empty($_POST["jbdate"])) {
        $date2 = trim($_POST["jbdate"]);
        $ccond .= "AND `dept_date` = '$date2'";
    }

    if (!empty($_POST["siscono"])) {
        $siscono = trim($_POST["siscono"]);
        $cond .= "AND `company` = '$siscono'";
    }

    if (!empty($_POST["batch"])) {
        $batch = $_POST["batch"];
        $condbatch = " AND `batchno` = '$batch' ";
    }
//    browse($condbatch);

    $hajicond = "";
    if (!empty($_POST["fromhaji"]) && !empty($_POST["tohaji"])) {
        $fromhaji = $_POST["fromhaji"];
        $tohaji = $_POST["tohaji"];
        $hajicond = " AND `hajino` BETWEEN '$fromhaji' AND '$tohaji'";
    }

    $paxcond = "";
    if (!empty($_POST["frompax"]) && !empty($_POST["topax"])) {
        $frompax = $_POST["frompax"];
        $topax = $_POST["topax"];
        $paxcond = " AND `paxid` BETWEEN '$frompax' AND '$topax'";
    }

    $msg = $_POST["sms"];


    $ctr = 0;
    $query = mysqli_query($conn, "SELECT RIGHT(`mobile`,10) as `mobile`,paxid  FROM `pax` WHERE LENGTH(`mobile`) >= 10 AND `paxid` IN (SELECT `paxno` FROM `airline_detail` WHERE paxno IS NOT NULL $ccond) $cond $hajicond $paxcond ") or die(mysqli_error($conn));
    while ($pax = mysqli_fetch_array($query)) {
        $getdis = mysqli_query($conn, "SELECT RIGHT(`mobile`,10) as `mobile` FROM `nominee_detail` where `paxno`='$pax[paxid]' ") or die(mysqli_error($conn));
        $getdis2 = mysqli_fetch_array($getdis);
        $nom_mob = $getdis2['mobile'];
//        browse($nom_mob);
        $ctr++;
        $to .= "$pax[mobile],$nom_mob,";
//        browse($to);
    }
    

//    $to .= "8652372200";
////print $to;
    /* $user = "tamboli2013";
      $password = "tamboli2013"; */
    $user = "tamboli2013";
    $password = "tamboli2013";
    $mobilenumbers = $to;
    $message = $msg;
    $senderid = "TMBOLI";
    //$url = "http://vas.mobilogi.com/API/WebSMS/Http/v1.0a/index.php?";
    //$url = "http://vas.mobilogi.com/api.php?";
//    $url = "144.76.39.175/api.php?";
    $url = "http://vas.mobilogi.com/api.php?";


//$url = "http://tran.mobilogi.com/api/httpapi.php";
//http://vas.mobilogi.com/API/WebSMS/Http/v1.0a/index.php?username=raj&password=<your_password>&sender=<mysenderid>&to=<myrecipient>&message=Hello+Test+Message&reqid=1&format={json|text}&route_id=<route+id>&callback=<Any Callback URL>&unique=0&sendondate=15-06-2013T07:14:19
    $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'");
//curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$user&password=$password&route=1&sender=$senderid&to=$mobilenumbers&message=$message");
    curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$user&password=$password&route=1&sender=$senderid&mobile[]=$mobilenumbers&message[]=$message");


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

    $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 = 'smsall.php';
    header("refresh:1; http://www.tambolitours.com/main.php?action=smsall");
    header("refresh: 1; $file");
}
?>
