38471-vm/fix_index_py.py
2026-03-18 09:45:33 +00:00

59 lines
1.7 KiB
Python

with open('index.php', 'r') as f:
lines = f.readlines()
# The corrupted block starts around line 50 (index 50 is line 51)
# And ends around line 65 where "// Timezone Setup" is.
# Let's find "require_once 'db/config.php';" which I added, and "// Timezone Setup"
start_idx = -1
end_idx = -1
for i, line in enumerate(lines):
if "require_once 'db/config.php';" in line:
start_idx = i
if "// Timezone Setup" in line:
end_idx = i
break
if start_idx != -1 and end_idx != -1:
print(f"Replacing lines {start_idx+1} to {end_idx}")
new_block = """require_once 'db/config.php';
require_once 'includes/stock_helper.php';
// Helper for current outlet
if (!function_exists('current_outlet_id')) {
function current_outlet_id() {
if (session_status() === PHP_SESSION_NONE) session_start();
return (int)($_SESSION['outlet_id'] ?? 1);
}
}
// Handle Outlet Switch
if (isset($_GET['action']) && $_GET['action'] === 'switch_outlet' && isset($_GET['id'])) {
$target_id = (int)$_GET['id'];
$allowed_outlets = $_SESSION['user_outlets'] ?? [1];
$is_admin = ($_SESSION['user_role_name'] ?? '') === 'Administrator';
if ($is_admin || in_array($target_id, $allowed_outlets)) {
$stmt = db()->prepare("SELECT id FROM outlets WHERE id = ? AND status = 'active'");
$stmt->execute([$target_id]);
if ($stmt->fetchColumn()) {
$_SESSION['outlet_id'] = $target_id;
}
}
header("Location: index.php");
exit;
}
"""
# Replace the slice
lines[start_idx:end_idx] = [new_block]
with open('index.php', 'w') as f:
f.writelines(lines)
print("Fixed index.php")
else:
print("Could not find block boundaries.")