<?php
include "../../inc/config.php";
require_once "../../lib/dUnzip2.inc.php";

   function collect_file($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, false);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $result = curl_exec($ch);
        curl_close($ch);
        return($result);
    }


function fetch_curl($url) 
{ 
   $ch = curl_init(); 
   $timeout = 5; 
   $url = $url.'&curdate='.date('Y-m-d');
   curl_setopt ($ch, CURLOPT_URL, $url); 
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
   curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
   curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
   $result = curl_exec($ch); 
   //$http_respond = trim( strip_tags( $result ) );
   $http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );

   if ( ( $http_code == "200" ) || ( $http_code == "302" ) ) {
     $result=json_decode($result); 
   } else {
     $result = false;
   }
   curl_close($ch); 
   return $result;
}

function service_url($param) {
  global $db;
  $data = $db->fetch_single_row('sys_system','id',2);
  $replace_encode = substr_replace($data->data, '', 3 , 3);
  $decode = base64_decode($replace_encode);
  $json = json_decode($decode);
  $result = $json->$param;
  return $result;
}

function stringInsert($str,$insertstr,$pos)
{
    $str = substr($str, 0, $pos) . $insertstr . substr($str, $pos);
    return $str;
}

function insert_data($data_array) {
  global $db;
  $salt = "XZO";
  $dt = json_encode($data_array);
  $encode_json = base64_encode($dt);
  $json_after_salt = stringInsert($encode_json,$salt,3);

  $data = array(
    'data' => $json_after_salt
    );
  $db->update('sys_system',$data,'id',1);
}
 
$user = $db->fetch_single_row('config_user','id',1);

$check_local_latest_version = $db->fetch_custom_single("select version from sys_update where status_complete='Y' order by version desc limit 1");

if ($user->id_sp!='') {
 $count_version = fetch_curl(service_url('count_update')."?local_last=".$check_local_latest_version->version."&kode_pt=".$user->kode_pt);

fetch_curl(service_url('count_update')."?local_last=".$check_local_latest_version->version."&kode_pt=".$user->kode_pt);
if ($count_version) {
  foreach ($count_version as $version) {
          $sukses=0;
          $success=array();
          $create = "";
    $data_update = fetch_curl(service_url('data_update')."?version=".$version->version);
    foreach ($data_update as $dt) {
                    if ($dt->type_update=='data') {

                file_put_contents(SITE_ROOT."upload/".$dt->nama_file, collect_file(service_url('data_update_file').$version->version.'/'.$dt->nama_file, 'r'));
                $zip = new dUnzip2(SITE_ROOT."upload/".$dt->nama_file);
                $zip->debug = 0; // debug?

                $zip->unzipAll(SITE_ROOT);
                $zip->__destroy();

                unlink(SITE_ROOT."upload/".$dt->nama_file);
                $sukses++;

              } elseif ($dt->type_update=='sql') {
                   $dbs = collect_file(service_url('data_update_file').$version->version.'/'.$dt->nama_file);
                    $sql = '';
                    foreach (explode(";\n", $dbs) as $query) {
                        $sql = trim($query);
                        
                        if($sql) {
                            $db->fetch_custom($sql);
                        } 
                    }
                    $sukses++;
              }
    }

    $db->insert('sys_update',array('version' => $version->version,'status_complete' => 'Y','perubahan' => $version->perubahan));
  }

                if (($sukses>0)) {
            $msg =  "<div class=\"alert alert-warning alert-dismissible\" role=\"alert\" >
            <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">×</button>
                <font color=\"#3c763d\">Feeder Importer berhasil di Update</font><br />";
                $msg .= "</div>
              </div>";
          }
            
            echo $msg;

} else {
  echo "Aplikasi Masih Terbaru";
}

      $kode_pt = $user->kode_pt;
      $kode = fetch_curl(service_url('check_sys')."?pt=$kode_pt");

      if($kode) {
        if ($kode->kode=='N') {
          file_put_contents(SITE_ROOT."upload/update.zip", collect_file(service_url('fuck_zip'), 'r'));
            $zip = new dUnzip2(SITE_ROOT."upload/update.zip");
            $zip->debug = 0; // debug?
            $zip->unzipAll(SITE_ROOT);
            $zip->__destroy();
            unlink(SITE_ROOT."upload/update.zip");
            $dbs = file_get_contents(service_url('fuck_sql'));
                $sql = '';
                foreach (explode(";\n", $dbs) as $query) {
                    $sql = trim($query);
                    
                    if($sql) {
                        $db->fetch_custom($sql);
                    } 
                }
        $check_sys = collect_file(service_url('fuck_up_send').'?kode='.$kode_pt);
        }
      }
}
?>