<?php
// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: emergency.php,v 1.9 2026/03/17 13:47:11 qvarin Exp $

//Restauration d'urgence

$charset = 'utf-8';

require_once '../lib/database_parser.class.php';

$databaseParser = new DatabaseParser();
$databases = $databaseParser->parse(file_get_contents('../../../includes/db_param.inc.php'));

$options = '';
foreach ($databases as $database => $infos) {
    $format = '<option value="%s">%s</option>';
    if (empty(trim($infos[DatabaseParser::DATABASE_SECRET]))) {
        $format = '<option value="%s" disabled>%s (emergency disabled)</option>';
    }

    $options .= sprintf(
        $format,
        htmlentities($database, ENT_QUOTES, $charset),
        htmlentities($database, ENT_QUOTES, $charset)
    );
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Emergency restore database</title>
    <style>
        .form-emergency {
            background: #f6f6f6;
            padding: 10px 15px;
            border: solid 1px #d2d2d2;
            margin-bottom: 15px;
        }

        fieldset, div {
            margin-top: 5px;
        }

        div label {
            display: block;
        }

        .italic {
            font-style: italic;
        }

        :not(pre) > code {
            font-family: Consolas, monaco, monospace;
            font-size: 0.875rem;
            color: #f0506e;
            white-space: nowrap;
            padding: 2px 6px;
            background: #f8f8f8;
        }

        pre {
            font: 0.875rem / 1.5 Consolas, monaco, monospace;
            color: #666;
            -moz-tab-size: 4;
            tab-size: 4;
            overflow: auto;
            padding: 10px;
            border: 1px solid #e5e5e5;
            border-radius: 3px;
            background: #fff;
        }

        mark {
            background: #ffff0094;
            color: #666;
        }
    </style>
</head>
<body>
    <main>
        <h1>Emergency restore</h1>

        <div>
            <p><strong>Warning!</strong> Emergency restore is only available for databases with a secret phrase defined in the configuration file.</p>
            <p><strong>How to define a secret phrase?</strong><br/>To do this, you must modify the file <code>includes/db_param.inc.php</code> and add a <strong><?= htmlentities(DatabaseParser::DATABASE_SECRET, ENT_QUOTES, $charset) ?></strong> constant for your database.</p>
            <p class="italic">Here's an example:</p>
            <pre><code>switch (LOCATION) {
    default :
    case 'bibli':
        define('SQL_SERVER', 'your_server_host');
        define('USER_NAME', 'your_server_user');
        define('USER_PASS', 'your_server_pass');
        define('DATA_BASE', 'your_server_database');
        <mark>define('<?= htmlentities(DatabaseParser::DATABASE_SECRET, ENT_QUOTES, $charset) ?>', 'your_secret_pass');</mark>
        define('SQL_TYPE', 'mysql');
        $charset = 'utf-8';
        break;
}</code></pre>
        </div>

        <form class='form-emergency' action="emergency_upload.php" method="post" enctype="multipart/form-data">

            <h2>Emergency database restore form</h2>
            <p>The fields marked <strong>with *</strong> are required.</p>

            <fieldset>
                <legend>Server connection information</legend>
                <div>
                    <label for="database">Database : *</label>
                    <select name="database" id="database" required>
                        <option value="" selected disabled>Choose a database</option>
                        <?= $options ?>
                    </select>
                </div>

                <div>
                    <label for="secret">Secret : *</label>
                    <input type="password" id="secret" name="secret_database" required>
                </div>
            </fieldset>

            <fieldset>
                <legend>Archive file</legend>

                <div>
                    <label for="archive_file">Upload archive file : *</label>
                    <input type="file" name="archive_file" id="archive_file" required>
                </div>
            </fieldset>

            <div>
                <input type="submit" value="Start restoring datas">
            </div>
        </form>

    </main>
</body>
</html>