<?php
session_start();
include "../../inc/config.php";


 function is_connected($url,$port)
      {
       
        if(!$sock = @fsockopen($url, $port))
        {
           return false;
        }
        else
        {
            return true;
        }

      }

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";
  $array_data = array($data_array);
  $dt = json_encode($data_array);
  $encode_json = base64_encode($dt);
  $json_after_salt = stringInsert($encode_json,$salt,3);

  return $json_after_salt;
}
session_check();
switch ($_GET["act"]) {
  case "up":
      $port = $_POST['port'];
      $protokol = $_POST['protokol'];
      $file_headers = @get_headers($protokol.'://'.$_POST["url"].':'.$port.'/ws/live2.php');


      if (!is_connected($_POST["url"],$port)) {
              $status = 'Server NEO FEEDER tidak aktif';
      } else {
           if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
              $status = 'Tidak menemukan NEO FEEDER Server/Url Salah';
            } else {

      $datas = array(
        'act' => 'GetToken',
        'username' => $_POST["username"],
        'password' => $_POST["password"]
      );
      $result = service_request($datas,$protokol.'://'.$_POST["url"].':'.$port.'/ws/live2.php'); // gunakan sandbox);
      if ($result->data) {
         $token = $result->data->token;
              //dump($hasil->data);
              $data = array(
                "username"=> insert_data($_POST["username"]),
                "password"=> insert_data($_POST["password"]),
                "url"=>$_POST["url"],
                "protokol"=>$protokol,
                "port"=>$port,
                "live" => 'Y'
              );


           $up = $db->update("config_user",$data,"id",$_POST["id"]);

            $data_req = [
                'act' => 'GetProfilPT',
                'token' => $token
            ];

            $url_rest = get_service_url('rest');
            $get_pt = json_decode($db->run($data_req,$url_rest));
          $data['id_user'] = "";
          $data['kode_pt'] = $db->trimmer($get_pt->data[0]->kode_perguruan_tinggi);
          $data['id_sp'] = $get_pt->data[0]->id_perguruan_tinggi;
          $data['nama_pt'] = $db->trimmer($get_pt->data[0]->nama_perguruan_tinggi);
         $status = 'good';

         $up = $db->update("config_user",$data,"id",$_POST["id"]);

      } else {
            $status = "ERROR: ".$result->error_desc;
         
         //exit();
      }

    }
      
}

      echo $status;
    break;
  default:
    # code...
    break;
}

?>