Viewing File: /usr/local/cpanel/3rdparty/wpinstaller/src/WpInstaller/Database.php

<?php
namespace WpInstaller;

class Database {
    const DATABASE_NAME_LENGTH     = 3;
    const DATABASE_PREFIX_LENGTH   = 3;

    private $provisioned;
    private $api;

    public $database_name;
    public $user_name;
    public $password;
    public $host;
    public $table_prefix;

    /*
        Database object constructor

        Arguments:
            @username - Username for the hosting account to create a database for. This is used as
                        a prefix to the database username.
            @api      - An object that implements WpInstaller\Database\ApiInterface.
            @host     - Database server host or ip. Default is 'localhost'.
    */
    public function __construct(string $username, \WpInstaller\Database\ApiInterface $api) {
        $this->api      = $api;
        $generated_name = $this->gen_database_name($username);

        $this->database_name = $generated_name;
        $this->user_name     = $generated_name;
        $this->host          = $this->api->get_server_host();
        $this->password      = \WpInstaller\StringGenerator::gen_password();
        $this->table_prefix  = \WpInstaller\StringGenerator::random_string(3). '_';

        $this->provisioned = false;
    }

    public function provision() {
        if ($this->provisioned) {
            // Nothing to do
            return;
        }

        $this->api->create_database($this->database_name);
        $this->api->create_user($this->user_name, $this->password);
        $this->api->set_user_privs(
            $this->user_name,
            $this->database_name,
            'ALL'
        );

        return $this->provisioned = true;
    }

    public function info() {
        if (!$this->provisioned) {
            return [];
        }

        return [
            'database_name' => $this->database_name,
            'user_name'     => $this->user_name,
            'host'          => $this->host,
            'password'      => $this->password,
            'table_prefix'  => $this->table_prefix,
        ];
    }

    public function rollback() {
        if (!$this->provisioned) {
            // Nothing to do
            return;
        }

        $this->api->delete_user($this->user_name);
        $this->api->delete_database($this->database_name);

        $this->provisioned = false;
    }

    private function gen_database_name($cpanel_user) {
        $short_user = substr($cpanel_user, 0, 8);
        $suffix = \WpInstaller\StringGenerator::random_string(self::DATABASE_NAME_LENGTH);
        $suffix = strtoupper($suffix);

        return "{$short_user}_WP{$suffix}";
    }
}

?>
Back to Directory File Manager