Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions includes/require-admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
// Auth gate for internal admin-only scripts under /scripts/ that write to the
// database (data migration/processing tools with no public UI entry point).
//
// Requires an admin_env.php file at the repo root (NOT committed, like
// ecobricks_env.php / gobrikconn_env.php) defining a secret token, using
// PHP's define() function with the constant name ADMIN_TOKEN and a long
// random string value.
//
// Pass it once as ?admin_token=... (or POST field admin_token); the session
// remembers it so the multi-step migration scripts can keep redirecting to
// each other without re-authenticating every request.

if (session_status() === PHP_SESSION_NONE) {
session_start();
}

if (empty($_SESSION['gea_admin_authenticated'])) {
$adminEnvPath = __DIR__ . '/../admin_env.php';

if (!file_exists($adminEnvPath)) {
if (!headers_sent()) {
http_response_code(503);
}
die('Admin authentication is not configured on this server.');
}

require_once $adminEnvPath;

$providedToken = (string) ($_POST['admin_token'] ?? $_GET['admin_token'] ?? '');

if (!defined('ADMIN_TOKEN') || $providedToken === '' || !hash_equals(ADMIN_TOKEN, $providedToken)) {
if (!headers_sent()) {
http_response_code(403);
}
die('Forbidden: admin authentication required.');
}

$_SESSION['gea_admin_authenticated'] = true;
}
1 change: 1 addition & 0 deletions scripts/process_ecobrick-full.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?php require_once '../includes/require-admin.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down
2 changes: 1 addition & 1 deletion scripts/process_ecobrick-old.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

<?php require_once '../includes/require-admin.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down
1 change: 1 addition & 0 deletions scripts/process_ecobrick.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?php require_once '../includes/require-admin.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down
2 changes: 1 addition & 1 deletion scripts/process_ecobrick_for_maker_id.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

<?php require_once '../includes/require-admin.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down
1 change: 1 addition & 0 deletions scripts/process_training.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// PART 1 of the code
// process_training.php

require_once '../includes/require-admin.php';
include '../ecobricks_env.php';

// Knack API settings
Expand Down
2 changes: 1 addition & 1 deletion scripts/process_training2.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

<?php
// PART 1 of the code
// process_training2.php

require_once '../includes/require-admin.php';
include '../ecobricks_env.php';

// Knack API settings
Expand Down
1 change: 1 addition & 0 deletions scripts/process_training3.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// PART 1 of the code
// process_training2.php

require_once '../includes/require-admin.php';
include '../ecobricks_env.php';

// Knack API settings
Expand Down
1 change: 1 addition & 0 deletions scripts/training.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
require_once '../includes/require-admin.php';
include '../ecobricks_env.php';

$training_id = $_GET['training_id'];
Expand Down
1 change: 1 addition & 0 deletions scripts/update-training-database.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
// PART 4: Database Update
require_once '../includes/require-admin.php';
include '../ecobricks_env.php';

$training_id = $_GET['training_id'];
Expand Down