<?php

$cms["modules"]["base.mod.php"] = array(
    "name"        => __( "module_name" ),
    "description" => __( "module_description" ),
    "version"     => "",
    "files" => array(
        ".cms/css/base.css",
        ".cms/mod/base.mod.php",
        ".cms/lang/ru_RU.UTF-8/base.mod.php",
        ".cms/lang/en_US.UTF-8/base.mod.php",
        ".cms/lang/uk_UA.UTF-8/base.mod.php",
    ),
);

// Return if module disabled
if ( ! empty( $cms["config"]["base.mod.php"]["disabled"] ) ) {

    return;

} else {

    // defautl settings
    if ( ! isset( $cms["config"]["base.mod.php"]["host"] ) )
        $cms["config"]["base.mod.php"]["host"] = "localhost";
    if ( ! isset( $cms["config"]["base.mod.php"]["port"] ) )
        $cms["config"]["base.mod.php"]["port"] = "3306";
    if ( ! isset( $cms["config"]["base.mod.php"]["user"] ) )
        $cms["config"]["base.mod.php"]["user"] = "";
    if ( ! isset( $cms["config"]["base.mod.php"]["password"] ) )
        $cms["config"]["base.mod.php"]["password"] = "";
    if ( ! isset( $cms["config"]["base.mod.php"]["base"] ) )
        $cms["config"]["base.mod.php"]["base"] = "";
    

    if ( is_admin() ) {
        hook_add_fn( "admin", "cms_base_admin" );
        hook_add_fn( "admin_header",  "cms_base_admin_header" );
        hook_add_fn( "api", "cms_base_connect", 5 );
    }
    hook_add_fn( "query", "cms_base_connect", 5 );

}

function cms_base_connect() {
    global $cms;
    // Return if module disabled
    if ( ! empty( $cms["config"]["base.mod.php"]["disabled"] ) ) {
        return;
    }

    if ( ! empty( $cms["config"]["base.mod.php"]["user"] ) ) {
        // return false on failure
        mysqli_report( MYSQLI_REPORT_OFF ); // PHP >= 8.1 FIX https://php.watch/versions/8.1/mysqli-error-mode
        $cms["base"] = mysqli_connect(
            $cms["config"]["base.mod.php"]["host"],
            $cms["config"]["base.mod.php"]["user"],
            $cms["config"]["base.mod.php"]["password"],
            $cms["config"]["base.mod.php"]["base"],
            $cms["config"]["base.mod.php"]["port"]
        );
        if ( $cms["base"] === false ) {
            return false;
        } else {
            mysqli_query( $cms["base"], "SET NAMES utf8" ); // PHP-5.2
            mysqli_set_charset( $cms["base"], "utf8mb4" );
            return true;
        }
    } else {
        return false;
    }

}

function cms_base_admin_header() {
    global $cms;
    echo "<link rel=stylesheet href='{$cms['base_path']}css/base.css'>";
}

function cms_base_admin() {
    global $cms;

    // Save settings
    if ( ! empty( $_POST["save_base_config"] ) ) {
        if ( $_POST["save_base_config"] === "create" ) {
            mysqli_report( MYSQLI_REPORT_OFF ); // PHP >= 8.1 FIX https://php.watch/versions/8.1/mysqli-error-mode
            $base = mysqli_connect( $_POST["host"], $_POST["admin_login"], $_POST["admin_password"], "mysql", $_POST["port"] );
            if ( $base ) {
                mysqli_query( $base, "CREATE DATABASE IF NOT EXISTS {$_POST['base']};" );
                mysqli_query( $base, "CREATE USER IF NOT EXISTS '{$_POST['user']}'@'{$_POST['host']}' IDENTIFIED BY '{$_POST['password']}';" );
                mysqli_query( $base, "GRANT ALL PRIVILEGES ON {$_POST['base']}.* TO '{$_POST['user']}'@'localhost';" );
                mysqli_query( $base, "FLUSH PRIVILEGES;" );
            }
        }

        $cms["config"]["base.mod.php"]["host"] = $_POST["host"];
        $cms["config"]["base.mod.php"]["port"] = $_POST["port"];
        $cms["config"]["base.mod.php"]["base"] = $_POST["base"];
        $cms["config"]["base.mod.php"]["user"] = $_POST["user"];
        $cms["config"]["base.mod.php"]["password"] = $_POST["password"];
        unset( $cms["config"]["base.mod.php"]["menu"]["base"]["class"] );
        cms_save_config();
        if ( cms_base_connect() ) {
            do_hook( "create_tables" );

            // Выполнить запросы в БД, находящиеся в файле update.sql
            $update_sql = "{$cms['cms_dir']}/update.sql";
            $q = "";
            if ( file_exists( $update_sql ) ) {
                $q = file_get_contents( $update_sql );
            }
            if ( $q ) {
                mysqli_multi_query( $cms["base"], $q );
            }
        }
        header( "Location: {$cms['base_path']}{$cms['config']['admin.mod.php']['admin_url']}" );
        exit;
    }

    $page = "
<div class=db-settings>
    <form method=post>
        <div class=basic>
            <div>
                <div>" . __( "server_address" ) . "</div>
                <input name=host type=text value='{$cms['config']['base.mod.php']['host']}' placeholder=localhost>
            </div>
            <div>
                <div>" . __( "server_port" ) . "</div>
                <input name=port type=text value='{$cms['config']['base.mod.php']['port']}' placeholder=3306>
            </div>
            <div>
                <div>" . __( "db_name" ) . "</div>
                <input name=base type=text value='{$cms['config']['base.mod.php']['base']}'>
            </div>
            <div>
                <div>" . __( "db_user" ) . "</div>
                <input name=user type=text value='{$cms['config']['base.mod.php']['user']}'>
            </div>
            <div>
                <div>" . __( "db_password" ) . "</div>
                <input name=password type=password value='{$cms['config']['base.mod.php']['password']}'>
                <div class=password-eye></div>
            </div>
            <button name=save_base_config value=save>" . __( "save" ) . "</button>
            <div class=pro-btn>PRO</div>
        </div>

        <div class='pro hidden'>
            <div class=create>" . __( "create_db" ) . "</div>
            <div>
                <div>" . __( "admin_login" ) . "</div>
                <input name=admin_login type=text value>
            </div>
            <div>
                <div>" . __( "admin_passwd" ) . "</div>
                <input name=admin_password type=password value>
                <div class=password-eye></div>
            </div>
            <button name=save_base_config value=create>" . __( "create_btn" ) . "</button>
        </div>
    </form>
</div>
    ";

    // Create menu item if not exists
    if ( empty( $cms["config"]["base.mod.php"]["menu"]["base"] ) ) {
        $cms["config"]["base.mod.php"]["menu"]["base"] = array(
            "title"    => "module_name",
            "sort"     => 20,
            "section"  => "settings",
        );
        cms_save_config();
    }

    if ( cms_base_connect() === false ) {
        $cms["config"]["base.mod.php"]["menu"]["base"]["class"] = "red";
    }

    $cms["admin_pages"]["base"] = $page;
}
