<?php

/**
 *	Module Class
 *  --------------
 *	Description : Move functions to objects
 *	Written by  : Kyriazis
 *	Version     : 1.0.0
 *  Created     : 05/08/2015
 *  Updated	    : 
 *	
 **/

class Clickatell {
	
    
	public function __construct()
	{
        
	}
	
	//==========================================================================
    // Class Destructor
	//==========================================================================
    public function __destruct()
	{
		//echo 'Object has been destroyed';
    }
    
        public static function GetClickatellSettings()
    {
        ## Initialize Connection
        $mysqli = Database::GetConnection();
        $output = '';
        
        $sql= "SELECT 
                primary_acc_user, 
                primary_acc_pass, 
                url_balance, 
                url_http_send, 
                sender_id, 
                sender_mobile, 
                max_chars,
                enable_concat,
                phones_rule_len, 
                phones_rule_pre, 
                phones_country_pre,
                primary_http_api_id
                FROM ".TABLE_CORE_SMS." WHERE primary_gateway_provider = 'Clickatell'";
        $result = database_query($mysqli, $sql, DATA_ONLY, FIRST_ROW_ONLY, FETCH_ASSOC, false);
        $output =  array(
                        'primary_acc_user'=>$result['primary_acc_user'], 
                        'primary_acc_pass'=>$result['primary_acc_pass'],
                        'url_balance'=>$result['url_balance'],
                        'url_http_send'=>$result['url_http_send'],
                        'sender_id'=>$result['sender_id'],
                        'sender_mobile'=>$result['sender_mobile'],
                        'max_chars'=>$result['max_chars'],
                        'enable_concat'=>$result['enable_concat'],
                        'phones_rule_len'=>$result['phones_rule_len'],
                        'phones_rule_pre'=>$result['phones_rule_pre'],
                        'phones_country_pre'=>$result['phones_country_pre'],
                        'primary_http_api_id'=>$result['primary_http_api_id']
                        );
        
        return $output;
        ## Close Connection
        mysqli_close($mysqli);
        
    }
    
    public static function GetClickatellSmsStatus($msgid){
               
		$mysqli = Database::GetConnection();
        $user_id = Core::GetUserId();
        
        $clickatell_settings =  self::GetClickatellSettings();
        $primary_acc_user = $clickatell_settings['primary_acc_user'];
        $primary_acc_pass = $clickatell_settings['primary_acc_pass'];
        $primary_http_api_id = $clickatell_settings['primary_http_api_id'];
        
		$charge_url = "http://api.clickatell.com/http/getmsgcharge?user=$primary_acc_user&password=$primary_acc_pass&api_id=$primary_http_api_id";
		$charge_url = $charge_url."&cliMsgId=".$msgid;
		$charge_return = file($charge_url);
        
        echo $charge_url; 
        
		foreach($charge_return as $item){ //1
            $findmsg = strpos($item, "cliMsgId:");
            $findcharge = strpos($item, "charge:");
            $findstatus = strpos($item, "status:");
            $length = strlen($item);
            //Find the climsgid
            $part1 = substr($item, $findmsg, $findstatus);
            $part2no = strpos($item, $part1);
            $apo = trim(substr($item, $findmsg, $findcharge ));
            $status= trim(substr($item, $findstatus, $length ));
            $charge= trim(substr($item, $findcharge, $findstatus));
            //find charge
            $ret = $charge;
            $sess = explode(":",$ret);
            $charge = trim($sess[1]);
            //find status
            $ret = $status;
            $sess = explode(":",$ret);
            $status = trim($sess[1]);
            //prepare input
            $status = mysqli_real_escape_string($mysqli, $status);
            $charge = mysqli_real_escape_string($mysqli, $charge);
            
            $sql = "UPDATE sms_outbox SET status = '$status', charge = '$charge', updated_on = NOW(), updated_by = '$user_id' WHERE message_id ='$msgid'";
            $result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
            mysqli_close($mysqli);
        } //end 1
  }
}
?>