<?php
/**
 * -------------------------------------------------------------------------
 *
 * Template to display the Delete Fragment Block page.
 *
 * -------------------------------------------------------------------------
 *
 * Note that this template is very similar to the Log In page template in
 * its HTML markup and business logic. The only difference is that when this
 * template is rendered, an authorized administrator already exists, and his
 * re-entered login/password is perceived only as confirmation that he
 * agrees to delete this Fragment file.
 *
 * -------------------------------------------------------------------------
 *
 * Input parameters are:
 *
 *     $params = A contents of this Fragment file.
 *
 * -------------------------------------------------------------------------
 *
 * Inspired by code pen "Snake highlight" of Mikael Ainalem
 * https://codepen.io/ainalem/pen/EQXjOR
 *
 * -------------------------------------------------------------------------
 *
 * @package    MimimiFramework
 * @subpackage Examples / Static Pages only
 * @license    GPL-2.0
 *             https://opensource.org/license/gpl-2-0/
 * @copyright  2022 MiMiMi Community
 *             https://mimimi.software/
 *
 * -------------------------------------------------------------------------
 */

    /**
     * ---------------------------------------------------------------------
     *
     * Execute server logic according to a form on this page.
     *
     * ---------------------------------------------------------------------
     */

    $success = '';
    $error   = '';
    $login   = '';
    $url     = helperGetFragmentUrl ( );

    /**
     * ---------------------------------------------------------------------
     *
     * If the admin has submitted a form with the entered login/password.
     *
     * ---------------------------------------------------------------------
     */

    if ( getFormInput ( 'submit' ) ) {

        $login = getFormInput ( 'login' );

        /**
         * -----------------------------------------------------------------
         *
         * Now we check if the security timeout has expired. Note that we
         * have also set a new timeout period to ensure the safety of the
         * next action.
         *
         * -----------------------------------------------------------------
         */

        $expired = helperCheckForTimeout ( 'delete'                                     );
                   helperSetTimeout      ( 'delete', STATICPAGESONLY_TIMEOUT_FOR_DELETE );
        if ( $expired ) {

            /**
             * -------------------------------------------------------------
             *
             * After that, we must get the entered login and check it.
             *
             * -------------------------------------------------------------
             */

            if ( helperValidateLogin ( $login ) ) {

                /**
                 * ---------------------------------------------------------
                 *
                 * Then we must get the password entered by the admin and
                 * check it in a similar manner.
                 *
                 * ---------------------------------------------------------
                 */

                $pass = mimimiPost ( 'pass' );
                if ( helperValidatePassword ( $login, $pass ) ) {

                    /**
                     * -----------------------------------------------------
                     *
                     * Check if the site works in production mode now.
                     *
                     * -----------------------------------------------------
                     */

                    if ( ! helperIsDemo ( ) ) {

                        /**
                         * -------------------------------------------------
                         *
                         * Try to delete this file and redirect the admin
                         * to the Fragment Blocks page.
                         *
                         * -------------------------------------------------
                         */

                        helperDeleteFragment ( $url );
                        helperGotoPage ( 'fragments' );

                    /**
                     * -----------------------------------------------------
                     *
                     * Otherwise, prepare an appropriate error message.
                     *
                     * -----------------------------------------------------
                     */

                    } else {
                        $success = 'Everything is fine! However, you are currently in demo mode. Any deletion is only simulated in this mode. To see how it really works, download this package and install it on your site or localhost.';
                    }
                } else {
                    $error = 'Sorry, this login or password is not valid!';
                }
            } else {
                $error = 'Please enter a valid login!';
            }
        } else {
            $error = 'Your previous attempts were too frequent! To avoid attacks on Fragment blocks storage, the site uses a pause between critical actions (such as deleting a Fragment file). Try again in ' . STATICPAGESONLY_TIMEOUT_FOR_DELETE . ' seconds.';
        }
    }

    /**
     * ---------------------------------------------------------------------
     *
     * Send headers matching this page.
     *
     * ---------------------------------------------------------------------
     */

    sendHeaderHTML         (   );
    sendStatus200          (   );
    sendHeaderExpires      ( 0 );
    sendHeaderLastModified (   );

    /**
     * ---------------------------------------------------------------------
     *
     * Now we can generate the page content.
     *
     * ---------------------------------------------------------------------
     *
     * Please note that the input form below is protected by a <div> tag
     * instead of the classic <form> tag. Additionally, its submit button is
     * protected by removing the "value=1" attribute. Automatic removal of
     * protection is performed by calling the secureForm() function
     * implemented in the "static.pages.only/Themes/default/js/login.js"
     * file.
     *
     * This trivial protection allows you to protect the site from many spam
     * bots that try to send any form found on your site.
     *
     * ---------------------------------------------------------------------
     */

?><!DOCTYPE html>
<html>
    <head>
        <base href="<?php printSiteUrl ( ) ?>">
        <meta charset="UTF-8">

        <title>
            Delete Fragment File
        </title>

        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots"   content="noindex, nofollow">

        <link rel="canonical"     href="<?php printPageUrl ( ) ?>">
        <link rel="shortcut icon" href="<?php printThemeUrl ( ) ?>images/favicon.ico" type="image/ico">
        <link rel="stylesheet"    href="<?php printThemeUrl ( ) ?>css/delete.css">
    </head>

    <body>
        <div class="wrapper">
            <header class="success"><?php
                printValue ( $success )
            ?></header>

            <aside class="error"><?php
                printValue ( $error )
            ?></aside>

            <main class="content">
                <div class="left">
                    <h1 class="title">
                        Delete Fragment
                    </h1>
                    <p>Are you sure you want to delete the <a href="<?php printSiteUrl (                          );
                                                                          printValue   ( 'fragments/edit/' . $url ) ?>" rel="nofollow"><?php
                                                               printValue ( $url )
                                                           ?></a> file?</p>

                    <a class="btn" href="<?php printSiteUrl ( ) ?>fragments" title="Return to the list of existing Fragment files">
                        Cancel
                    </a>
                </div>

                <div class="right">
                    <svg viewBox="0 0 320 300">
                        <defs>
                            <linearGradient inkscape:collect="always"
                                            id="linearGradient"
                                            x1="13"
                                            y1="193.49992"
                                            x2="307"
                                            y2="193.49992"
                                            gradientUnits="userSpaceOnUse">
                                <stop style="stop-color:#ff00ff;"
                                      offset="0"
                                      id="stop876" />
                                <stop style="stop-color:#ff0000;"
                                      offset="1"
                                      id="stop878" />
                            </linearGradient>
                        </defs>
                        <path d="m 40,120.00016 239.99984,-3.2e-4 c 0,0 24.99263,0.79932 25.00016,35.00016 0.008,34.20084 -25.00016,35 -25.00016,35 h -239.99984 c 0,-0.0205 -25,4.01348 -25,38.5 0,34.48652 25,38.5 25,38.5 h 215 c 0,0 20,-0.99604 20,-25 0,-24.00396 -20,-25 -20,-25 h -190 c 0,0 -20,1.71033 -20,25 0,24.00396 20,25 20,25 h 168.57143" />
                    </svg>

                    <div class="form">
                        <label>
                            Your Login:
                            <input name="login"
                                   type="text"
                                   maxlength="50"
                                   value="<?php printValue ( $login ) ?>"
                                   required
                                   tabindex="1">
                        </label>

                        <label>
                            Password:
                            <input name="pass"
                                   type="password"
                                   maxlength="100"
                                   value=""
                                   tabindex="2">
                        </label>

                        <button class="btn"
                                name="submit"
                                type="submit"
                                tabindex="3">
                            Yes, I'm sure
                        </button>
                    </div>
                </div>
            </main>

            <footer class="footnote">
                <a href="<?php printSiteUrl ( ) ?>" title="Go to the home page">
                    Home
                </a>

                <a href="<?php printSiteUrl ( ) ?>credits" title="Gratitude to third party developers">
                    Credits
                </a>
            </footer>
        </div>

        <script src="<?php printThemeUrl ( ) ?>js/animejs-2.2.0.min.js"></script>
        <script src="<?php printThemeUrl ( ) ?>js/delete.js"></script>
    </body>
</html>
