<?php
require_once("$CFG->dirroot/local/badiunet/lib/httpquerystring.php");
class local_badiunet_util {
    private $lastipconnection;
    function __construct() {
        $this->lastipconnection=null;
    }

    public function getJson($input, $outArray=true) {
        $result = null;
        $max_int_length = strlen((string) PHP_INT_MAX) - 1;
        $json_without_bigints = preg_replace('/:\s*(-?\d{' . $max_int_length . ',})/', ': "$1"', $input);
        $result = json_decode($json_without_bigints, $outArray);
        return $result;
    }
/*
 * $out 0 - ojbect | 1- array | 2 - original text
 */
    public function request($url,$pdata=null,$out = 1) {
       $data = $this->requestService($url,$pdata);
      // print_r($data);exit;    
       if($this->isResponseError($data)){return $data;}
       if($out==2){return $data;}
       $outarray=false;
       if($out){$outarray=true;}
        $result = $this->getJson($data, $outarray);
        return $result;
    }

    public function requestService($url,$pdata=null) {
        $host=null;
        $param=null;
          
        if(empty($pdata)){
            $h=$this->splitHostParam($url);
            $host=$h->host;
            $param=$paramjson = json_encode($h->param);
        }else if(is_array($pdata)){
             $host=$url;
             $param=json_encode($pdata);
        }else{
           
           $param= $pdata;
        }
   
        $ch = curl_init();
       
       // echo $host;
        //print_r($param);
        curl_setopt($ch, CURLOPT_URL, $host);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($ch);
        $this->lastipconnection=curl_getinfo($ch,CURLINFO_PRIMARY_IP);
        curl_close($ch);
        return $result;
       
    }

    function splitHostParam($url) {
        $address=new stdClass(); 
        $address->host="";
        $address->param="";
         $pos=strpos($url,'?');
         if ($pos === false) {
             $address->host=$url;
             return $address;
         }
      
        $p = explode("?", $url);
        if(isset($p[0])){$address->host=$p[0];}
        if(isset($p[1])){$address->param=$p[1];}
        if(!empty($address->param)){
            $querystr=new local_badiunet_httpquerystring($address->param);
            $address->param=$querystr->getParam();
        }
         
        return $address;
    }

    function getVlueOfArray($array, $key){
        $value=null;
        if(!is_array($array)){return null;}
         if (array_key_exists($key,$array)){
            $value= $array[$key];
         }
        return $value;
    }
    
    function isResponseError($response) {
        if(!is_array($response)){
             $response=$this->getJson($response, true);
        }
         if(is_array($response)){
               if ( array_key_exists('status',$response) 
                       && array_key_exists('info',$response) 
                       && array_key_exists('message',$response) ){return false;}
                  
           }
         return true;
       } 
       
    function plugininfo() {
        global $CFG;
        $fpath="$CFG->dirroot/local/badiunet/version.php";
       if(!file_exists($fpath)){return null;}
       require_once($fpath);
       if(isset($plugin)){
            $info=array('name'=>'badiunet','version'=>$plugin->version,'release'=>$plugin->release);
            return $info;
       }
       
        return null;

    }
       function getLastipconnection() {
           return $this->lastipconnection;
       }

       function setLastipconnection($lastipconnection) {
           $this->lastipconnection = $lastipconnection;
       }


}
