<?php
$apiKey = '10756$0b142a4cbabe3e3a66befb6fcb339b38229547dd4814630faeb8b85ea1adffa58d08ef16c2877b2d08767cd16970820cc09dee52baf4033333e5407947b2c5af';
function creates($name,$os,$password,$region,$plan){
    global $apiKey;
    $apiUrl = "https://api.gcore.com/cloud/v2/instances/460993/$region";
    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Authorization: APIKey $apiKey"
    ));
    $userData = "#cloud-config
password: $password
chpasswd: { expire:False }
ssh_pwauth: True
power_state:
  mode: reboot
  timeout: 30
  condition: True";

    $encodedUserData = base64_encode($userData);

    $data = array(
        'flavor' => $plan,
        'names' => array("$name"),
        'interfaces' => array(
            array('type' => 'external'),
        ),
        'user_data' => $encodedUserData,
        'volumes' => array(
            array(
                'image_id' => $os,
                'source' => 'image',
                'metadata' => array(),
            )
        )
    );

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    $response = curl_exec($ch);
    curl_close($ch);
    
    if ($response) {
        echo $response;
    } else {
        echo 'خطا در ارتباط با سرویس دهنده';
    }
}
function list_server($region){
    global $apiKey;
    $apiUrl = "https://api.gcore.com/cloud/v1/instances/460993/$region";
    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Authorization: APIKey $apiKey"
    ));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    $response = curl_exec($ch);
    curl_close($ch);
    
    if ($response) {
        echo $response;
    } else {
        echo 'خطا در ارتباط با سرویس دهنده';
    }
}
function list_image($region){
    global $apiKey;
    $apiUrl = "https://api.gcore.com/cloud/v1/images/460993/$region";
    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Authorization: APIKey $apiKey"
    ));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    $response = curl_exec($ch);
    curl_close($ch);
    if ($response) {
        $response = json_decode($response,true);
        foreach ($response['results'] as $match) {
            if($match['name'] == 'ubuntu-20.04-x64'){
                $ubuntu20 = $match['id'];
            }if($match['name'] == 'ubuntu-22.04-x64'){
                $ubuntu22 = $match['id'];
            }
            
        }echo "$ubuntu20|$ubuntu22";
    } else {
        echo 'خطا در ارتباط با سرویس دهنده';
    }
}
function req($region,$id,$post,$url){
    global $apiKey;
    if($url == '/no'){
        $url = null;
    }
    $apiUrl = "https://api.gcore.com/cloud/v1/instances/460993/$region/$id$url";
    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Authorization: APIKey $apiKey"
    ));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $post);
    $response = curl_exec($ch);
    curl_close($ch);
    if ($response) {
        echo $response;
    } else {
        echo 'خطا در ارتباط با سرویس دهنده';
    }
}
if(isset($_GET['create'])){
    creates($_GET['name'],$_GET['os'],$_GET['pass'],$_GET['reg'],$_GET['plan']);
}if(isset($_GET['list'])){
    list_server($_GET['reg']);
}if(isset($_GET['image'])){
    list_image($_GET['reg']);
}if(isset($_GET['req'])){
    $url = $_GET['url'] ?: 'no';
    req($_GET['loc'],$_GET['id'],$_GET['post'],"/$url");
}
?>
