<?php

    // Время истечения cookie входа
    $expire_days = 365;
    $expire = time() + $expire_days * 24 * 60 * 60;
    // Продлевать если истекают через
    $expire_less = 180;

    // Продление куки входа
    if ( is_admin() ) {
        $sess = $_COOKIE["sess"];
        $expire = (int) $cms["config"]["logged"][$sess]["expire"];
        $pre_expire = $expire - $expire_less * 24 * 60 * 60;
        $now = time();
        if ( $now > $pre_expire ) {
            $new_expire = time() + $expire_days * 24 * 60 * 60;
            if ( PHP_VERSION_ID < 70300 ) {
                setcookie( "sess", $sess, $new_expire );
            } else {
                setcookie( "sess", $sess, array( "SameSite" => "Lax", "expires" => $new_expire ) );
            }
            // Так же запомнить в конфиге
            $cms["config"]["logged"][$sess]["expire"] = $new_expire;
            cms_save_config();
        }
    }

    // Для работы выпадающего списка
    if ( empty( $cms["config"]["locale"] ) ) {
        $cms["config"]["locale"] = "en_US.UTF-8";
        $cms["config"]["lang"]   = "en";
    }
    if ( ! empty( $_GET["locale"] ) ) {
        $cms["config"]["locale"] = $_GET["locale"];
        $cms["config"]["lang"]   = substr( $_GET["locale"], 0, 2 );
    }
    
    // Login
    if ( isset( $_POST["login"] ) && isset( $_POST["password"] ) ) {

        // Install process
        if ( empty( $cms["config"]["admin.mod.php"]["admin_login"] )
         && empty( $cms["config"]["admin.mod.php"]["admin_password"] )
         && ( ! empty( $_POST["login"] ) || ! empty( $_POST["password"] ) ) ) {
            $cms["config"]["admin.mod.php"]["admin_login"]    = $_POST["login"];
            $cms["config"]["admin.mod.php"]["admin_password"] = $_POST["password"];
            $cms["config"]["admin.mod.php"]["admin_salt"]     = cms_uid();
            $cms["config"]["admin.mod.php"]["admin_url"]      = "-admin";
            $link = "{$cms['url']['scheme']}://{$cms['url']['host']}{$cms['base_path']}{$cms['config']['admin.mod.php']['admin_url']}";
            
            // Set Locale and Timezone
            $cms["config"]["locale"] = $_POST["locale"];
            $cms["config"]["lang"]   = substr( $cms["config"]["locale"], 0, 2 );
            cms_save_config();

            if ( is_email( $cms["config"]["admin.mod.php"]["admin_login"] ) ) {
                $subject = __( "install_finished" );
                $body  = __( "congrat" ) . "\n\n";
                $body .= __( "install_finished" ) . "\n\n";
                $body .= __( "login_info" ) . "\n\n";
                $body .= __( "login_url" ) . ": {$link}\n\n";
                $body .= __( "login" ) . ": {$_POST['login']}\n\n";
                $body .= __( "password" ) . ": {$_POST['password']}";
                cms_email( array(
                    "type" => "text/plain",
                    "from_email" => "noreply@" . $cms["url"]["host"],
                    "from_name"  => $cms["url"]["host"],
                    "to_email"   => $_POST["login"],
                    "subject"    => $subject,
                    "email_body" => $body,
                ) );
            }
        }
        // Check login and password
        if ( $_POST["login"]    === $cms["config"]["admin.mod.php"]["admin_login"] && 
             $_POST["password"] === $cms["config"]["admin.mod.php"]["admin_password"] )
        {

            $d        = date( "Y-m-d H:i:s" );
            $sess    = sha1( $cms["config"]["admin.mod.php"]["admin_login"] . $cms["config"]["admin.mod.php"]["admin_salt"] . $d );
            
            // Prepend New Session
            if ( ! isset( $cms["config"]["logged"] ) ) {
                $cms["config"]["logged"] = array();
            }
            $cms["config"]["logged"] = array(
                $sess => array(
                    "ip"         => $_SERVER["REMOTE_ADDR"],
                    "date"       => $d,
                    "user_agent" => $_SERVER["HTTP_USER_AGENT"],
                    "expire"     => $expire,
                )
            ) + $cms["config"]["logged"];

            // Set Locale and Timezone
            $cms["config"]["locale"] = $_POST["locale"];
            $cms["config"]["lang"]   = substr( $cms["config"]["locale"], 0, 2 );

            if ( cms_save_config() ) {
                
                if ( PHP_VERSION_ID < 70300 ) {
                    setcookie( "sess", $sess, $expire );
                } else {
                    setcookie( "sess", $sess, array( "SameSite" => "Lax", "expires" => $expire ) );
                }
                
                $cms["status"] = "302";
                $cms["status_302_location"] = $cms["base_path"] . $cms["config"]["admin.mod.php"]["admin_url"];
                return;

            } else {
                
                $error_message = __( "cant_write_config" ) . " .cms/config.php";

            }

        } else {
            
            $error_message = __( "access_denied" );

        }
    }

    // Тема
    if ( isset( $_COOKIE["theme"] ) && in_array( $_COOKIE["theme"], array( 1 ) ) ) {
        $pref_bg = "#e9e0dd";
    } else {
        $pref_bg = "#1e1d1d";
    }
    $styles = array( "dark", "light", "dark classic" );
    if ( isset( $_COOKIE["theme"] ) ) {
        $html_classes = $styles[ $_COOKIE["theme"] ];
    } else {
        $html_classes = "dark";
    }    

?>
<!doctype html>
<html lang="<?php echo $cms["config"]["lang"]; ?>" class="<?php echo $html_classes; ?>">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, interactive-widget=resizes-content">
        <title><?php echo $cms['url']['host']; ?></title>
        <link rel="icon" href="<?php echo $cms["base_path"]; ?>img/favicon.svg">
        <style>
            html {
                background-color: <?php echo $pref_bg; ?>;
            }
        </style>


        <?php
        echo "<link rel=stylesheet href='{$cms['base_path']}css/admin.css'>";

        echo "<script>\nadmin_styles = " . json_encode( $styles ) . ";\n";
        if ( is_admin() && ! empty( $cms["config"]["locale"] ) ) {
            $lang = json_encode( $cms["lang"] ); // Переводы
            $tr   = json_encode( $cms["tr"] ); // Транслитерация
            $modules = json_encode( $cms["modules"] );
            if ( is_admin() ) {
                echo "cms = {};
                cms.base_path = '{$cms['base_path']}';
                cms.async_api = true;
                cms.api = '{$cms['base_path']}{$cms['config']['admin.mod.php']['api_url']}';
                cms.locale = '{$cms['config']['locale']}';
                cms.modules = {$modules};
                cms.lang = {$lang};
                cms.tr = {$tr};";
            }
        }
        echo "</script>\n";
        ?>

        <?php
        // Сюда модули могут подключать свои стили и скрипты
        do_hook( "admin_header" );
        ?>

    </head>


<?php if ( is_admin() ) : ?>

<body class=logged>
    <header>
        <div class=burger>
            <div class=menu-icon>
                <span class=line-1></span>
                <span class=line-2></span>
            </div>
        </div>

        <div class=page-tabs></div>

        <div class=menu>
            <a href="<?php echo $cms["base_path"]; ?>" data-front target=_blank>
                <?php echo __( "home_link" ); ?>
            </a>
            <div class=clear-cache>
                <?php echo __( "cache_btn" ); ?>
            </div>
            <div class=theme-switcher>
                <?php echo __( "theme_btn" ); ?>
            </div>
            <div data-logout>
                <?php echo __( "logout_btn" ); ?>
            </div>
        </div>
    </header>

    <aside>

<?php
// Боковое меню, секции и пункты
// Сортировка секций происходит в admin.mod.php
foreach( $cms["admin_sections"] as $section_name => $section ) {
    if ( empty( $section["hide"] ) ) {
        // Не выводим пустые секции
        if ( ! empty( $cms["admin_sections"][$section_name]["items"] ) ) {
            
            echo "<section sort={$section["sort"]}>";
            echo "<div>{$cms['admin_sections'][$section_name]['title']}</div>";

            foreach( $cms["admin_sections"][$section_name]["items"] as $item_name => $item ) {
                if ( empty( $item["hide"] ) ) {
                    $title = __( $item["title"], $item["module"] );
                    // for highlite
                    if ( ! empty( $item["class"] ) ) {
                        $class = "class='{$item['class']}'";
                    } else {
                        $class = "";
                    }
                    if ( empty( $item["url"] ) ) {
                        echo "<a href=#{$item_name} {$class} sort={$item['sort']}>{$title}</a>";
                    } else {
                        echo "<a href='{$item['url']}' target=_blank {$class} sort={$item['sort']}>{$title}</a>";
                    }
                }
            }
            echo "</section>";
        }
    }
}
?>

    </aside>


    <main>

<?php
    $hello = __( "hello" );
    if ( cms_base_connect() === false ) {
        $base_ok = "<p>" . __( "hello_set_base" ) . "</p>";
    } else {
        $base_ok = "";
    }
    
    echo "
<section id=start>
    <div>
        <div>{$hello}</div>
        {$base_ok}
    </div>
</section>";

    foreach( $cms["admin_pages"] as $name => $page ) {
        echo "<section id={$name}>{$page}</section>";
    }
    
?>

    </main>

    <div class=milk></div>

    <div class=log-info-box>
        <!-- div for messages -->
    </div>

<?php else : ?>

<body class=login>
  
    <header>
        <div class=menu>
            <div class=theme-switcher>
                <?php echo __( "theme_btn" ); ?>
            </div>
        </div>
    </header>

    

    <div class=aside-main>
        <div class=center-box>
            <div class=setup-error>
                <?php
                // Если файл существует, то не перезаписывать его,
                // иначе можно менять языки не заходя в админку
                // Но проверку оставить, могут быть сменены права позже
                if ( is_file( $cms["config_file"] ) ) {
                    if ( ! is_writable( $cms["config_file"] ) ) {
                        echo __( "cant_write_config" ) . " {$cms["config_file"]}";
                    }
                }
                // config.php not writeable
                elseif ( cms_save_config() === false ) {
                    echo __( "cant_write_config" ) . " {$cms["config_file"]}";
                }

                // Error login and password
                if ( isset( $error_message ) ) {
                    echo __( $error_message );
                }
                ?>
            </div>
            
            <?php
                // Scan all locales
                $options = "";
                foreach( glob( $cms["cms_dir"] . "/lang/*.UTF-8/admin.mod.php" ) as $lang_file ) {
                    include( $lang_file );
                    preg_match( "/\/lang\/(\w{5}\.UTF-\d{1,2})\//", $lang_file, $m );
                    $locale = $m[1];

                    // translate
                    $lang = $cms["lang"]["admin.mod.php"][$locale][$locale];

                    $options .= "<div class=option value='{$locale}'>{$lang}</div>";
                }
            ?>
            <div class=lang-selector>
                <div class=lang-select-grid>
                    <div class=field-select data-lang='<?php echo $cms["config"]["locale"]; ?>'>
                        <div class=value>
                            <?php echo $cms["lang"]["admin.mod.php"][$cms["config"]["locale"]][$cms["config"]["locale"]]; ?>
                        </div>
                        <div class=icon></div>
                    </div>
                    <div class=field-options>
                        <?php echo $options; ?>
                    </div>
                </div>
            </div>
            <div class=setup-auth>
                <?php
                if ( empty( $cms["config"]["admin.mod.php"]["admin_login"] ) && empty( $cms["config"]["admin.mod.php"]["admin_password"] ) ) {
                    echo __( "set_login_and_password" );
                }
                ?>
            </div>

            <script>
            <?php
            $form = "
            <form class=login-and-password method=post>
                <div class=login>
                    <input placeholder=\"" . __( "login_or_password" ) . "\" name=login type=text>
                </div>
                <div class=password>
                    <input placeholder=\"" . __( "password" ) . "\" name=password type=password>
                    <div class=password-eye></div>
                    <button title=\"" . __( "login_btn" ) . "\"></button>
                </div>
                <input type=hidden name=locale value=\"{$cms["config"]["locale"]}\">
            </form>";
            $form = base64_encode( $form );
            ?>
            let f = decodeURIComponent( escape( window.atob( "<?php echo $form; ?>" ) ) );
            document.write( f );

            document.querySelectorAll( ".login-and-password .login input" ).forEach( function( login ) {
                login.focus();
            } );

            function get_cookie( name ) {
                let cookies = document.cookie.split( ";" );
                for ( let line of cookies ) {
                    let cookie = line.split( "=" );
                    if ( name == cookie[ 0 ].trim() ) {
                        return decodeURIComponent( cookie[ 1 ] );
                    }
                }
                return "";
            }

            function set_cookie( name, value ) {
                document.cookie = encodeURIComponent( name ) + "=" + encodeURIComponent( value ) + ";SameSite=Lax";
            }

            // Theme switcher
            document.querySelectorAll( ".theme-switcher" ).forEach( function( el ) {
                el.addEventListener( "click", function( event ) {
                    event.preventDefault();
                    let n = get_cookie( "theme" ) || 0;
                    let styles2 = admin_styles[n];
                    let styles = styles2.split( " " );
                    styles.forEach( function( style ) {
                        document.documentElement.classList.remove( style );
                    } );
                    n = (+n+1) % admin_styles.length;
                    styles2 = admin_styles[n];
                    styles = styles2.split( " " );
                    styles.forEach( function( style ) {
                        document.documentElement.classList.add( style );
                    } );
                    set_cookie( "theme" , n );
                } );
            } );

            // Select language. Select
            document.querySelectorAll( ".login .field-select" ).forEach( function( select ) {
                select.addEventListener( "click", function( e ) {
                    e.stopPropagation();
                    select.nextElementSibling.classList.toggle( "open" );
                    select.closest( ".lang-select-grid" ).classList.toggle( "open" );
                } );
            } );

            // Select language. Option
            document.querySelectorAll( ".login .field-options .option" ).forEach( function( select ) {
                select.addEventListener( "click", function( e ) {
                    let input = this.closest( ".lang-select-grid" ).querySelector( ".field-select" );
                    input.setAttribute( "data-lang", this.getAttribute( "value" ) );
                    input.querySelector( ".value" ).innerText = this.innerText;
                    //e.stopPropagation(); убираем чтобы закрылось автоматически
                    let locale = this.getAttribute( "value" );
                    let search = window.location.search.replace( /&*locale=[^&]+/, "" );
                    if ( search == "" ) { 
                        search += "?locale=" + locale;
                    } else if ( search == "?" ) {
                        search += "locale=" + locale;
                    } else {
                        search += "&locale=" + locale;
                    }
                    window.location.search = search;
                } );
            } );

            // Клик по молоку
            document.body.addEventListener( "click", function( e ) {
                let select = document.querySelector( ".login .lang-select-grid" );
                select.classList.remove( "open" );
                select.querySelector( ".field-options" ).classList.remove( "open" );
            } );

            // Show/Hide password
            document.querySelectorAll( ".password-eye" ).forEach( function( eye ) {
                eye.addEventListener( "click", function( e ) {
                    this.classList.toggle( "showed" );
                    let inp = this.previousElementSibling;
                    let t   = inp.getAttribute( "type" );
                    if ( t == "password" ) {
                        inp.setAttribute( "type", "text" );
                    } else {
                        inp.setAttribute( "type", "password" );
                    }
                    inp.focus();
                } );
            } );
            </script>

            <div class=flatfree></div>
            <div class=support-box>
                <a target=_blank href='<?php echo __( "support_url" ); ?>'><?php echo __( "support" ); ?></a>
            </div>
        </div>
    </div>

    
<?php endif; ?>
    
</body>
</html>
