Динамическое создание поддоменов в php (cpanel & hosting24) - PullRequest
0 голосов
/ 14 апреля 2019

Я пытаюсь создать поддомен динамически с помощью php, чтобы каждый раз, когда кто-то создает пользователя, он также создавал поддомен.

Мне кажется, все ответы, которые я могу найти, одинаковы, и они просто не работают.

Т.е. этот код наиболее подсказывает:

function createDomain($domain) {
        // your cPanel username
        $cpanel_user = 'username';
        $cpanel_pass = 'pass';
        $cpanel_skin = 'paper_lantern';
        $cpanel_host = 'mydomainname.com';
        $subdomain = $domain;
    // directory - defaults to public_html/subdomain_name
    $dir = 'public_html/user_site';

    // create the subdomain
    $sock = fsockopen($cpanel_host,2082);
    if(!$sock) {
        print('Socket error');
        exit();
    }

    $pass = base64_encode("$cpanel_user:$cpanel_pass");
    $in = "GET /frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$cpanel_host&domain=$subdomain&dir=$dir\r\n";
    $in .= "HTTP/1.0\r\n";
    $in .= "Host:$cpanel_host\r\n";
    $in .= "Authorization: Basic $pass\r\n";
    $in .= "\r\n";

    fputs($sock, $in);
    while (!feof($sock)) {
        $result = fgets($sock, 128);
    }
    fclose($sock);

    return $result;
}
createDomain('testing');

Когда япопробуйте использовать ссылку непосредственно в браузере, чтобы увидеть, что происходит, cpanel сообщает, что с токеном безопасности что-то не так, и я получаю форму входа.

Поэтому я попытался позвонить, чтобы сгенерировать токен безопасности:

function createSession() { // Example details
        $ip = "example.com";
        $cp_user = "username";
        $cp_pwd = "password";
        $url = "https://example.com:2083/login";
        $cookies = "/path/to/storage/for/cookies.txt";

    // Create new curl handle
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); // Save cookies to
    curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$cp_user&pass=$cp_pwd");
    curl_setopt($ch, CURLOPT_TIMEOUT, 100020);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // Execute the curl handle and fetch info then close streams.
    $f = curl_exec($ch);
    $h = curl_getinfo($ch);
    curl_close($ch);

    // If we had no issues then try to fetch the cpsess
    if ($f == true and strpos($h['url'],"cpsess")){
        // Get the cpsess part of the url
        $pattern="/.*?(\/cpsess.*?)\/.*?/is";
        $preg_res=preg_match($pattern,$h['url'],$cpsess);
    }

    // If we have a session then return it otherwise return empty string
    $token =  (isset($cpsess[1])) ? $cpsess[1] : "";




}
$test = createSession();

Он успешно создает токен.Затем я попытался отправить токен безопасности на другой вызов, так что это было бы примерно так:

$token . "/frontend/paper_lantern/subdomain/doadddomain.html?rootdomain=" . $main_domain . "&domain=" . $sub_domain_name . "&dir=public_html/subdomains/" . $sub_domain_name

, но ничего не происходит, ошибок нет, субдомен не создается.Как я могу сделать эту работу?

1 Ответ

0 голосов
/ 15 апреля 2019

После долгих попыток и поисков я наконец понял это.Я превратил это в класс, чтобы им было легко пользоваться и всем, кто ищет.

class Subdomain {
    const CP_user = env('CPANEL_USER', 'forge');
    const CP_password = env('CPANEL_PASSWORD', 'forge');
    const DOMAIN = env('CPANEL_DOMAIN', 'forge');
    const URL = env('CPANEL_URL', 'forge');
    public $token = "";

    public function __construct() {
        $token = self::createSession();
        $this->token = $token;
    }


    private function createSession() {
        $url = self::URL . "login";
        $cookies = "/path/to/storage/for/cookies.txt";

        // Create new curl handle
        $ch=curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); // Save cookies to
        curl_setopt($ch, CURLOPT_POSTFIELDS, "user=" . self::CP_user . "&pass=". self::CP_password);
        curl_setopt($ch, CURLOPT_TIMEOUT, 100020);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // Execute the curl handle and fetch info then close streams.
        $f = curl_exec($ch);
        $h = curl_getinfo($ch);
        curl_close($ch);

        // If we had no issues then try to fetch the cpsess
        if ($f == true and strpos($h['url'],"cpsess")){
            // Get the cpsess part of the url
            $pattern="/.*?(\/cpsess.*?)\/.*?/is";
            $preg_res=preg_match($pattern,$h['url'],$cpsess);
        }

        // If we have a session then return it otherwise return empty string
        $token =  (isset($cpsess[1])) ? $cpsess[1] : "";
        return $token;

    }

    private function get_query( $action, $subdomain ){
        $directory = $subdomain . "." . self::DOMAIN;

        switch ($action) {
            case 'create':
                $query = self::URL . $this->token . "/json-api/cpanel?cpanel_jsonapi_func=addsubdomain&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_version=2&domain=$subdomain&rootdomain=" . self::DOMAIN . "&dir=$directory";
            break;

            case 'delete':
                $query = self::URL . $this->token . "/json-api/cpanel?cpanel_jsonapi_func=delsubdomain&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_version=2&domain=".$subdomain.'.'.self::DOMAIN."&dir=$directory";
            break;

            case 'delete_dir':
                $query =  self::URL . $this->token . "/json-api/cpanel?cpanel_jsonapi_module=Fileman&cpanel_jsonapi_func=fileop&op=unlink&sourcefiles=$directory";
            break;
        }

        return $query;

    }


    public function create($subdomain){
        $query = self::get_query( 'create', $subdomain );
        self::request($query);
    }

    public function delete($subdomain){
        $query = self::get_query( 'delete', $subdomain );
        $query_dir = self::get_query( 'delete_dir', $subdomain );

        self::request($query, $query_dir);
    }

    public function request($query, $dir = false){

        $curl = curl_init();                                // Create Curl Object
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);       // Allow self-signed certs
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);       // Allow certs that do not match the hostname
        curl_setopt($curl, CURLOPT_HEADER,0);               // Do not include header in output
        curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);       // Return contents of transfer on curl_exec
        $header[0] = "Authorization: Basic " . base64_encode(self::CP_user.":".self::CP_password) . "\n\r";
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);    // set the username and password
        curl_setopt($curl, CURLOPT_URL, $query);            // execute the query
        $result = curl_exec($curl);
        if ($result == false) {
            error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
        }
        curl_close($curl);

        if ($dir) {
            self::request($dir);
        }

        print $result;

    }
}

// how to use
$new_url = new Subdomain();
$new_url->create('mypage');
$new_url->delete('mypage'); 

Примечания: - Я поместил приватные переменные в файл .env.- Удаление удалит как субдомен, так и каталог.- Удаление не удаляет созданный DNS, но еще не изучил его.

...