<?php
include 'Connections/MyConnect.php'; 
date_default_timezone_set('Asia/Bangkok');

$today = date('d-m-Y', time());
$msgtype = "T";
$encoding = "8";
$i=0;
$query_sm = mysqli_query($MyConnect, "SELECT * FROM tbl_package_resend WHERE shortcode like '4877%' AND action = 'resend' " ) or die(mysql_error());
while($row_sm = mysqli_fetch_assoc($query_sm)){ 

    $shortcode = $row_sm['shortcode'];
    $keyword = $row_sm['keyword'];
    $serviceid = $row_sm['dtac_serviceid'];
    $service = $row_sm['service_name'];

    $telco = "Dtac_Mcom";

    $query_sv = mysqli_query($MyConnect, "SELECT * FROM tbl_service WHERE service_name='$service' " ) or die(mysql_error());
    $row_sv = mysqli_fetch_assoc($query_sv);
    $cate = $row_sv['content_category'];
    $type = $row_sv['content_type'];

    if($cate!="") {

        $query_url = mysqli_query($MyConnect, "SELECT * FROM tbl_service_url WHERE cms_type='$type' AND cms_category='$cate' AND company='mcom' " ) or die(mysql_error());
        $row_url = mysqli_fetch_assoc($query_url); 
        $link = $row_url['shorturl'];
        $dt = date("Y-m-d");
        
        $query_cms = mysqli_query($MyConnect, "SELECT * FROM tbl_cms_$type WHERE category='$cate' AND broadcast='$dt'" ) or die(mysql_error());
        $row_cms = mysqli_fetch_assoc($query_cms);  
        $id = $row_cms['id'];

        if($type=="Text") {      
            $msg = $row_cms['text'];
        }
        else {
            $msg = $id." ".$link;
        }
        $query_pm = mysqli_query($MyConnect, "SELECT * FROM tbl_mt_parameter WHERE telco='$telco'" ) or die(mysql_error());
        $row_pm = mysqli_fetch_assoc($query_pm); 
        $user = $row_pm['username'];
        $password = $row_pm['password'];
        $url = $row_pm['url'];
        $cpid = $row_pm['cpid'];

        $dt = date("d"); 
        $mt = date("m");
        $yr = date("y");
        $hr = date("H");
        $mn = date("i");
        $sc = date("s");
        $yrb = date("Y");

        $i1 = str_pad($i, 3, '0', STR_PAD_LEFT);
        $i2 = substr($i1, -3);

        $txid = "0".$cpid.$yr.$mt.$dt.$hr.$i2;
        $ts = $yrb.$mt.$dt.$hr.$mn.$sc;

        $mt2402 = '<?xml version="1.0" encoding="UTF-8"?>';
        $mt2402 .= '<cpa-request>';
        $mt2402 .= '<txid>'.$txid.'</txid>';
        $mt2402 .= '<authentication>';
        $mt2402 .= '<user>'.$user.'</user>';
        $mt2402 .= '<password>'.$password.'</password>';
        $mt2402 .= '</authentication>';
        $mt2402 .= '<originator>';
        $mt2402 .= '<sender>'.$shortcode.'</sender>';
        $mt2402 .= '</originator>';
        $mt2402 .= '<destination>';
        $mt2402 .= '<serviceid>'.$serviceid.'</serviceid>';
        $mt2402 .= '</destination>';
        $mt2402 .= '<message>';
        $mt2402 .= '<header>';
        $mt2402 .= '<timestamp>'.$ts.'</timestamp>';
        $mt2402 .= '</header>';
        $mt2402 .= '<sms>';
        $mt2402 .= '<msg>'.$msg.'</msg>';
        $mt2402 .= '<msgtype>'.$msgtype.'</msgtype>';
        $mt2402 .= '<encoding>'.$encoding.'</encoding>';
        $mt2402 .= '<userheader></userheader>';
        $mt2402 .= '</sms>';
        $mt2402 .= '</message></cpa-request>';

        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_POST, true );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $mt2402 );
        $result = curl_exec($ch);
        curl_close($ch);
    
        $date = date('d-m-Y H:i:s', time());

        mysqli_query($MyConnect, "INSERT INTO tbl_mt_resend (url,content,logtime) VALUES ('$url','$mt2402','$date')");  

        echo $i++;

    }
}

?>