<?php

$cms['modules']['base.mod.php'] = array(
    'name' => __('Data Base', 'base.mod.php'),
    'description' => __('Data Base Module', 'base.mod.php'),
    'url_title' => __('Module page'),
    'url_link' => __('https://coffee-cms.com/en/modules/base/', 'base.mod.php'),
    'author' => __('Coffee CMS team'),
    'version' => '21.05',
    'files' => array(
        '.cms/mod/base.mod.php',
        '.cms/lang/ru_RU.UTF-8/base.mod.php',
    ),
    'sort'    => 10,
);

// Return if module disabled
if ( isset($cms['config']['base.mod.php']['disabled'])
&& $cms['config']['base.mod.php']['disabled'] === true ) return;


// Install
if (empty($cms['config']['base.mod.php']['user'])) {
    $cms['config']['base.mod.php']['host']     = 'localhost';
    $cms['config']['base.mod.php']['port']     = '3306';
    $cms['config']['base.mod.php']['disabled'] = false;
}

cms_add_function('admin', 'cms_base_admin');
cms_add_function('query', 'cms_base_connect', 5);

function cms_base_connect() {
    global $cms;
    if (!empty($cms['config']['base.mod.php']['user'])) {
        // return false on failure
        $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']
        );
    } else {
        return false;
    }

    if ( $cms['base'] === false ) {
        return false;
    } else {
        mysqli_query( $cms['base'], "SET NAMES UTF8;" );
        return true;
    }
}

function cms_base_admin() {
    global $cms;
    if (!is_admin()) return;
    // Save settings
    if ( !empty($_POST['save_base_config']) ) {
        if ( $_POST['save_base_config'] === __('Create','base.mod.php') ) {
            $base = mysqli_connect( $_POST['host'], $_POST['admin'], $_POST['admin_password'], 'mysql', $_POST['port'] );
            if ( $base ) {
                mysqli_query( $base, "CREATE DATABASE {$_POST['base']};" );
                mysqli_query( $base, "CREATE USER '{$_POST['user']}'@'localhost' 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'] = array(
            'host' => $_POST['host'],
            'port' => $_POST['port'],
            'base' => $_POST['base'],
            'user' => $_POST['user'],
            'password' => $_POST['password'],
        );
        cms_save_config();
        if (cms_base_connect()) {
            cms_do_stage( 'create_tables' );
        }
        header('Location: ' . $cms["config"]["admin.mod.php"]["admin_url"]);
        exit;
    }
    @$page = "
    <div class='main-header'></div>
    <div class='main-main'>
        <div class='db-grid'>
            <div class='dbbox'>
            
                <form class='bdf' method='post'>
                    <div class='field'>
                        <div class='field-name-bd'>".__('Host', 'base.mod.php')."</div>
                        <input class='settingx sto' name='host' type='text' value='{$cms['config']['base.mod.php']['host']}' placeholder='localhost'>
                    </div>
                    <div class='field'>
                        <div class='field-name-bd'>".__('Port', 'base.mod.php')."</div>
                        <input class='settingx sto' name='port' type='text' value='{$cms['config']['base.mod.php']['port']}' placeholder='3306'>
                    </div>
                    <div class='field'>
                        <div class='field-name-bd'>".__('Base', 'base.mod.php')."</div>
                        <input class='settingx sto' name='base' type='text' value='{$cms['config']['base.mod.php']['base']}'>
                    </div>
                    <div class='field'>
                        <div class='field-name-bd'>".__('User', 'base.mod.php')."</div>
                        <input class='settingx sto' name='user' type='text' value='{$cms['config']['base.mod.php']['user']}'>
                    </div>
                    <div class='field'>
                        <div class='field-name-bd'>".__('Password', 'base.mod.php')."</div>
                        <input class='settingx sto' name='password' type='password' value='{$cms['config']['base.mod.php']['password']}'>
                        <div class='password-eye'></div>
                    </div>
                    <div class='field'>
                        <input type=submit name=save_base_config value='".__('Save')."' class='button-save'>
                    </div>

                    <div class='field'>
                        <div class='field-name-bd'>".__('DBMS Admin', 'base.mod.php')."</div>
                        <input class='settingx sto' name='admin' type='text' value=''>
                    </div>
                    <div class='field'>
                        <div class='field-name-bd'>".__('DBMS Admin Password', 'base.mod.php')."</div>
                        <input class='settingx sto' name='admin_password' type='password' value=''>
                        <div class='password-eye'></div>
                    </div>
                    <input type=submit name=save_base_config value='".__('Create','base.mod.php')."' class='button-save'>
                </form>
            
            </div>
            
            <div class='dbmanual'>
                <div class=info>
                    <h1>".__('Setting up a database connection', 'base.mod.php')."</h1>
                    <p>".__('Enter the credentials obtained from the hosting provider.', 'base.mod.php')."</p>
                    <p>".__('You can create the specified database if the server is yours. To do this, enter the login and password of the DBMS administrator and click the Create button.', 'base.mod.php')."</p>
                </div>
            </div>

        </div>
    </div>
    <div class='main-footer'></div>
    ";

    // Создаем пункт меню, если еще не создан
    if ( empty( $cms['config']['base.mod.php']['menu']['base'] ) ) {
        $cms['config']['base.mod.php']['menu']['base'] = array(
            'title'    => 'Data Base',
            'sort'     => 20,
            'class'    => '',
            'section'  => 'Settings',
        );
        cms_save_config();
    }

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

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