<?php
include 'check_ip.php';
$applicationKey = 'e3340495c45eff76';
$applicationSecret = '5830e27cd22e6d2794ae5aaff9fec105';
$consumerKey = '45744f69c34f1d702cca9b1dfed1caae';
$endpoint = 'ovh-ca';

require __DIR__ . '/vendor/autoload.php';
use \Ovh\Api;

$ovh = new Api($applicationKey,
                $applicationSecret,
                $endpoint,
                $consumerKey);
if(isset($_GET['list_server'])){
    try {
        $response = $ovh->get('/cloud/project/7df755287d69436d88daa94d5d3b6e7c/instance');
        echo json_encode($response);
    }catch (GuzzleHttp\Exception\ClientException $e) {
        $response = $e->getResponse();
        $responseBodyAsString = $response->getBody()->getContents();
        echo $responseBodyAsString;
    }
}if(isset($_GET['create'])){
    try {
        $password = $_GET['password'];
        $response = $ovh->post('/cloud/project/7df755287d69436d88daa94d5d3b6e7c/instance', array(
            'flavorId' => $_GET['plan'],
            'name' => $_GET['name'],
            'region' => $_GET['loc'],
            'monthlyBilling' => false,
            'imageId' => $_GET['os'],
            'userData' => "#cloud-config

runcmd:
  -  sed -i 's/^#*\s*PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
  -  sed -i 's/^#*\s*PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config
  -  sed -i 's/^#*\s*KbdInteractiveAuthentication.*/KbdInteractiveAuthentication yes/' /etc/ssh/sshd_config
  -  systemctl restart sshd
  -  echo 'root:$password' | sudo chpasswd"
        ));
        echo json_encode($response);
    }catch (GuzzleHttp\Exception\ClientException $e) {
        $response = $e->getResponse();
        $responseBodyAsString = $response->getBody()->getContents();
        echo $responseBodyAsString;
    }
}if(isset($_GET['poweroff'])){
    try {
        $result = $ovh->post("/cloud/project/7df755287d69436d88daa94d5d3b6e7c/instance/{$_GET['id']}/stop");
        echo json_encode($response);
    }catch (GuzzleHttp\Exception\ClientException $e) {
        $response = $e->getResponse();
        $responseBodyAsString = $response->getBody()->getContents();
        echo $responseBodyAsString;
    }
}if(isset($_GET['poweron'])){
    try {
        $result = $ovh->post("/cloud/project/7df755287d69436d88daa94d5d3b6e7c/instance/{$_GET['id']}/start");
        echo json_encode($response);
    }catch (GuzzleHttp\Exception\ClientException $e) {
        $response = $e->getResponse();
        $responseBodyAsString = $response->getBody()->getContents();
        echo $responseBodyAsString;
    }
}if(isset($_GET['delete'])){
    try {
        $ovh->delete("/cloud/project/7df755287d69436d88daa94d5d3b6e7c/instance/{$_GET['id']}");
    }catch (GuzzleHttp\Exception\ClientException $e) {
        $response = $e->getResponse();
        $responseBodyAsString = $response->getBody()->getContents();
        echo $responseBodyAsString;
    }
}if(isset($_GET['osm'])){
    try {
    $result = $ovh->get('/cloud/project/7df755287d69436d88daa94d5d3b6e7c/image', array(
        'flavorType' => $_GET['plan'],
        'region' => $_GET['loc'],
    ));
    
    function findByName($array, $name) {
        foreach ($array as $item) {
            if ($item['name'] == $name) {
                return $item;
            }
        }
        return null;
    }
    $os_id = findByName($result, $_GET['os_name']);
    if ($os_id !== null) {
        echo  $os_id['id'];
    }

}catch (GuzzleHttp\Exception\ClientException $e) {
        $response = $e->getResponse();
        $responseBodyAsString = $response->getBody()->getContents();
        echo $responseBodyAsString;
    }
}if(isset($_GET['reinstall'])){
    try {
        $ovh->post("/cloud/project/7df755287d69436d88daa94d5d3b6e7c/instance/{$_GET['id']}/reinstall");

    }catch (GuzzleHttp\Exception\ClientException $e) {
        $response = $e->getResponse();
        $responseBodyAsString = $response->getBody()->getContents();
        echo $responseBodyAsString;
    }
}
?>