38471-vm/patch_users_1.php
2026-02-26 03:44:24 +00:00

100 lines
4.9 KiB
PHP

<?php
$file = 'index.php';
$content = file_get_contents($file);
// Replace login script to store assigned_outlets
$content = str_replace(
" \$_SESSION['outlet_id'] = \$u['outlet_id'];",
" \$_SESSION['outlet_id'] = \$u['outlet_id'];\n \$_SESSION['assigned_outlets'] = \$u['assigned_outlets'];",
$content
);
// Replace switch_outlet logic
$old_switch = <<<EOD
if (isset(\$_GET['action']) && \$_GET['action'] === 'switch_outlet') {
\$target = (int)\$_GET['id'];
if ((\$_SESSION['user_role_name'] ?? '') === 'Administrator') {
\$_SESSION['outlet_id'] = \$target === 0 ? null : \$target;
}
header("Location: " . (\$_SERVER['HTTP_REFERER'] ?? 'index.php'));
exit;
}
EOD;
$new_switch = <<<EOD
if (isset(\$_GET['action']) && \$_GET['action'] === 'switch_outlet') {
\$target = (int)\$_GET['id'];
\$is_admin = (\$_SESSION['user_role_name'] ?? '') === 'Administrator';
\$assigned_outlets = isset(\$_SESSION['assigned_outlets']) ? explode(',', \$_SESSION['assigned_outlets']) : [];
if (\$is_admin || in_array(\$target, \$assigned_outlets)) {
\$_SESSION['outlet_id'] = (\$target === 0 && \$is_admin) ? null : \$target;
}
header("Location: " . (\$_SERVER['HTTP_REFERER'] ?? 'index.php'));
exit;
}
EOD;
$content = str_replace($old_switch, $new_switch, $content);
// Update nav dropdown logic
$old_nav = <<<EOD
<?php
if ((\$_SESSION['user_role_name'] ?? '') === 'Administrator'):
\$outlets = db()->query("SELECT * FROM outlets WHERE status = 'active'")->fetchAll(PDO::FETCH_ASSOC);
\$cur_out = \$_SESSION['outlet_id'] ?? 0;
\$cur_name = 'All Outlets';
foreach (\$outlets as \$o) { if (\$o['id'] == \$cur_out) \$cur_name = \$o['name']; }
?>
<div class="dropdown me-3">
<button class="btn btn-outline-primary btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown">
<i class="bi bi-shop"></i> <span class="d-none d-md-inline"><?= htmlspecialchars(\$cur_name) ?></span>
</button>
<ul class="dropdown-menu shadow-sm border-0">
<li><a class="dropdown-item <?= \$cur_out == 0 ? 'active' : '' ?>" href="index.php?action=switch_outlet&id=0">All Outlets</a></li>
<li><hr class="dropdown-divider"></li>
<?php foreach (\$outlets as \$o): ?>
<li><a class="dropdown-item <?= \$cur_out == \$o['id'] ? 'active' : '' ?>" href="index.php?action=switch_outlet&id=<?= \$o['id'] ?>"><?= htmlspecialchars(\$o['name']) ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
EOD;
$new_nav = <<<EOD
<?php
\$is_admin = (\$_SESSION['user_role_name'] ?? '') === 'Administrator';
\$assigned_outlets_str = \$_SESSION['assigned_outlets'] ?? '';
\$assigned_outlets_arr = array_filter(explode(',', \$assigned_outlets_str));
if (\$is_admin || count(\$assigned_outlets_arr) > 1):
if (\$is_admin) {
\$outlets = db()->query("SELECT * FROM outlets WHERE status = 'active'")->fetchAll(PDO::FETCH_ASSOC);
} else {
\$in_clause = implode(',', array_map('intval', \$assigned_outlets_arr));
\$outlets = db()->query("SELECT * FROM outlets WHERE status = 'active' AND id IN (\$in_clause)")->fetchAll(PDO::FETCH_ASSOC);
}
\$cur_out = \$_SESSION['outlet_id'] ?? 0;
\$cur_name = \$is_admin ? 'All Outlets' : (count(\$outlets) > 0 ? \$outlets[0]['name'] : 'Select Outlet');
foreach (\$outlets as \$o) { if (\$o['id'] == \$cur_out) \$cur_name = \$o['name']; }
?>
<div class="dropdown me-3">
<button class="btn btn-outline-primary btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown">
<i class="bi bi-shop"></i> <span class="d-none d-md-inline"><?= htmlspecialchars(\$cur_name) ?></span>
</button>
<ul class="dropdown-menu shadow-sm border-0">
<?php if (\$is_admin): ?>
<li><a class="dropdown-item <?= \$cur_out == 0 ? 'active' : '' ?>" href="index.php?action=switch_outlet&id=0">All Outlets</a></li>
<li><hr class="dropdown-divider"></li>
<?php endif; ?>
<?php foreach (\$outlets as \$o): ?>
<li><a class="dropdown-item <?= \$cur_out == \$o['id'] ? 'active' : '' ?>" href="index.php?action=switch_outlet&id=<?= \$o['id'] ?>"><?= htmlspecialchars(\$o['name']) ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
EOD;
$content = str_replace($old_nav, $new_nav, $content);
file_put_contents($file, $content);
echo "Patch 1 applied.\n";