<?php

//This is a sample scripts/custom/user_create_post.sh script, to create a database upon User creation.
//Must use DA 1.43.1 or later because of: http://www.directadmin.com/features.php?id=1460
class api{
    function create_database($username, $password){
        
        require_once('httpsocket.php');
        
        $dbuser = "radio";
        $dbname = "radio";
        $dbpass = randomPassword();
        
        $sock = new HTTPSocket;
        
        // $sock->connect('https://localhost',2222);
        $sock->connect('https://localhost',2222);
        $sock->set_login($username, $password);

        $sock->set_method('POST');

        $sock->query('/CMD_API_DATABASES',
                array(
                        'action' => 'create',
                        'name' => $dbname,
                        'user' => $dbuser,
                        'passwd' => $dbpass,
                        'passwd2' => $dbpass,
            ));

        $result = $sock->fetch_body();
        
        parse_str($result, $outputArray);

        $outputArray = array_merge($outputArray, array('dbuser' => $username.'_'.$dbuser));
        $outputArray = array_merge($outputArray, array('dbname' => $username.'_'.$dbname));
        $outputArray = array_merge($outputArray, array('dbpass' => $dbpass));

        return $outputArray;

    }

    function delete_database($username, $password){

        require_once('httpsocket.php');

        $dbname = "radio";

        $sock = new HTTPSocket;

        // $sock->connect('https://localhost',2222);
        $sock->connect('https://localhost',2222);
        $sock->set_login($username, $password);

        $sock->set_method('POST');

        $sock->query('/CMD_API_DATABASES',
                array(
                        'action' => 'delete',
                        'select0' => $username.'_'.$dbname
            ));

        $result = $sock->fetch_body();

        parse_str($result, $outputArray);

        return $outputArray;

    }

    function check_user($username, $password){
        require_once('httpsocket.php');

        $sock = new HTTPSocket;

        $sock->connect('https://localhost',2222);
        $sock->set_login($username, $password);

        $sock->set_method('POST');

        $sock->query('/CMD_API_DATABASES',
                array(
                        'action' => 'list'
            ));


        $result = $sock->fetch_body();

        parse_str($result, $outputArray);

        if($outputArray['error'] == true){
            return array(
                'success' => true,
                'message' => 'Autenticado com sucesso'
            );
        }else{
            return array(
                'success' => false,
                'message' => 'Falha ao autenticar'
            );
        }
    }
    function randomPassword()
    {
        $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
        $pass = array(); //remember to declare $pass as an array
        $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
        for ($i = 0; $i < 8; $i++) {
            $n = rand(0, $alphaLength);
            $pass[] = $alphabet[$n];
        }
        return implode($pass); //turn the array into a string
    }
}

?>