<?php
include 'check_ip.php';
$apiKey = $_GET['api'] ?: 'QYRW4MORVCVV2WCS2NXACBRJIMWFXTWCABCQ';
function server(){
    global $apiKey;
    $apiUrl = 'https://api.vultr.com/v2/instances';
    
    
    $headers = array(
        'Authorization: Bearer ' . $apiKey,
    );
    
    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    if ($httpCode == 200) {
        echo $response;
    }
    curl_close($ch);
}
function create($region,$plan,$host,$os,$port){
    global $apiKey;
    $apiUrl = 'https://api.vultr.com/v2/instances';
    
    $headers = array(
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json',
    );
    
    $data = array(
        'region' => $region,
        'plan' => $plan,
        'label' => $host,
        'os_id' => $os,
        'backups' => 'disabled',
        'enable_ipv6' => true,
        'hostname' => $host,
        'user_data' => base64_encode("#cloud-config\nruncmd:\n  - sed -i \"s/^#\\{,1\\}Port [0-9]\\{1,6\\}/Port $port/\" /etc/ssh/sshd_config; service sshd restart; \n  - ufw disable"),
        'tags' => array(
            'a tag',
            'another',
        ),
    );
    
    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    echo $response;
    
    curl_close($ch);
    
}
function start($id){
    global $apiKey;
    $apiUrl = "https://api.vultr.com/v2/instances/$id/start";
    
    $headers = array(
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json',
    );
    
    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $response = curl_exec($ch);
    echo 'ok';
    
    curl_close($ch);

}
function stop($id){
    global $apiKey;
    $apiUrl = "https://api.vultr.com/v2/instances/$id/halt";
    
    $headers = array(
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json',
    );
    
    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $response = curl_exec($ch);
    echo 'ok';
    
    curl_close($ch);

}
function reboot($id){
    global $apiKey;
    $apiUrl = 'https://api.vultr.com/v2/instances/reboot';
    
    $headers = array(
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json',
    );
    
    $data = array(
        'instance_ids' => array(
            "$id"
        ),
    );
    
    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    
    $response = curl_exec($ch);
    echo 'ok';
    
    curl_close($ch);

}
function reb($id){
    global $apiKey;
    $apiUrl = "https://api.vultr.com/v2/instances/$id/reinstall";
    
    $headers = array(
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json',
    );
    
    $data = array(
        'hostname' => 'root'
    );
    
    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    
    $response = curl_exec($ch);
    echo $response;
    
    curl_close($ch);

}function update($id , $lable){
    global $apiKey;
    $apiUrl = "https://api.vultr.com/v2/instances/$id";
    
    $headers = array(
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json',
    );
    
    $data = array(
        'label' => $lable
    );
    
    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    
    $response = curl_exec($ch);
    echo $response;
    
    curl_close($ch);

}
function del($id){
    $apiUrl = "https://api.vultr.com/v2/instances/$id";
    global $apiKey;
    
    $headers = array(
        'Authorization: Bearer ' . $apiKey,
    );
    
    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    if ($httpCode == 204) {
        echo "ok";
    }
    
    curl_close($ch);
}function band($id){
    $apiUrl = "https://api.vultr.com/v2/instances/$id/bandwidth";
    global $apiKey;
    
    $headers = array(
        'Authorization: Bearer ' . $apiKey,
    );
    
    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    return $response;
    
    curl_close($ch);
}
if(isset($_GET['list'])){
    server();
}elseif(isset($_GET['create'])){
    create($_GET['reg'],$_GET['plan'],$_GET['host'],$_GET['os'],$_GET['port']);
}elseif(isset($_GET['stop'])){
    file_put_contents("test/{$_GET['stop']}","{$_GET['n']} ok");
    stop($_GET['stop']);
}elseif(isset($_GET['start'])){
    start($_GET['start']);
}elseif(isset($_GET['reb'])){
    reb($_GET['reb']);
}elseif(isset($_GET['del'])){
    del($_GET['del']);
}elseif(isset($_GET['update'])){
    update($_GET['update'],$_GET['lable']);
}elseif(isset($_GET['band'])){
    $bands = band($_GET['band']);
    $current_month = date('Y-m');
    $json_decode = json_decode($bands,true);
    foreach ($json_decode['bandwidth'] as $key => $droplet) {
        $time = substr($key, 0, 7);
        if($time == $current_month){
            $byt += $droplet['outgoing_bytes'];
        }
    }$byt = $byt / (1024 * 1024 * 1024 * 1024);
    echo number_format($byt, 3);
}elseif(isset($_GET['reboot'])){
    reboot($_GET['reb']);
}
?>