195 lines
6.1 KiB
PHP
195 lines
6.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
if (!function_exists('page_entrypoint_map')) {
|
|
function page_entrypoint_map(): array {
|
|
static $map = [
|
|
'activate' => 'activate.php',
|
|
'dashboard' => 'dashboard.php',
|
|
'pos' => 'pos.php',
|
|
'sales' => 'sales.php',
|
|
'sales_returns' => 'sales_returns.php',
|
|
'purchases' => 'purchases.php',
|
|
'purchase_returns' => 'purchase_returns.php',
|
|
'quotations' => 'quotations.php',
|
|
'lpos' => 'lpos.php',
|
|
'accounting' => 'accounting.php',
|
|
'expense_categories' => 'expense_categories.php',
|
|
'expenses' => 'expenses.php',
|
|
'expense_report' => 'expense_report.php',
|
|
'items' => 'items.php',
|
|
'categories' => 'categories.php',
|
|
'units' => 'units.php',
|
|
'customers' => 'customers.php',
|
|
'suppliers' => 'suppliers.php',
|
|
'customer_statement' => 'customer_statement.php',
|
|
'supplier_statement' => 'supplier_statement.php',
|
|
'cashflow_report' => 'cashflow_report.php',
|
|
'expiry_report' => 'expiry_report.php',
|
|
'low_stock_report' => 'low_stock_report.php',
|
|
'loyalty_history' => 'loyalty_history.php',
|
|
'payment_methods' => 'payment_methods.php',
|
|
'settings' => 'settings.php',
|
|
'devices' => 'devices.php',
|
|
'hr_departments' => 'hr_departments.php',
|
|
'hr_employees' => 'hr_employees.php',
|
|
'hr_attendance' => 'hr_attendance.php',
|
|
'hr_payroll' => 'hr_payroll.php',
|
|
'role_groups' => 'role_groups.php',
|
|
'users' => 'users.php',
|
|
'scale_devices' => 'scale_devices.php',
|
|
'customer_display_settings' => 'customer_display_settings.php',
|
|
'backups' => 'backups.php',
|
|
'logs' => 'logs.php',
|
|
'cash_registers' => 'cash_registers.php',
|
|
'register_sessions' => 'register_sessions.php',
|
|
'outlets' => 'outlets.php',
|
|
'my_profile' => 'my_profile.php',
|
|
'copy_outlet_data' => 'copy_outlet_data.php',
|
|
];
|
|
|
|
return $map;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('page_entry_script')) {
|
|
function page_entry_script(string $page): ?string {
|
|
$page = strtolower(trim($page));
|
|
if ($page === '') {
|
|
return null;
|
|
}
|
|
|
|
$map = page_entrypoint_map();
|
|
return $map[$page] ?? null;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('page_from_entry_script')) {
|
|
function page_from_entry_script(?string $scriptName = null): ?string {
|
|
static $reverse = null;
|
|
if ($reverse === null) {
|
|
$reverse = array_flip(page_entrypoint_map());
|
|
}
|
|
|
|
$scriptName = strtolower(trim((string)($scriptName ?? basename((string)($_SERVER['SCRIPT_NAME'] ?? '')))));
|
|
if ($scriptName === '') {
|
|
return null;
|
|
}
|
|
|
|
return $reverse[$scriptName] ?? null;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('page_url')) {
|
|
function page_url(string $page = 'dashboard', array $params = []): string {
|
|
$targetScript = page_entry_script($page);
|
|
$query = $params;
|
|
|
|
if ($targetScript === null) {
|
|
$targetScript = 'index.php';
|
|
if ($page !== '' && strtolower($page) !== 'dashboard') {
|
|
$query = ['page' => $page] + $query;
|
|
}
|
|
}
|
|
|
|
$queryString = http_build_query($query);
|
|
return $targetScript . ($queryString !== '' ? '?' . $queryString : '');
|
|
}
|
|
}
|
|
|
|
if (!function_exists('page_normalize_url')) {
|
|
function page_normalize_url(string $url): string {
|
|
$url = trim($url);
|
|
if ($url === '') {
|
|
return $url;
|
|
}
|
|
|
|
$parts = parse_url($url);
|
|
if ($parts === false) {
|
|
return $url;
|
|
}
|
|
|
|
$query = [];
|
|
if (!empty($parts['query'])) {
|
|
parse_str($parts['query'], $query);
|
|
}
|
|
|
|
$page = isset($query['page']) ? strtolower(trim((string)$query['page'])) : '';
|
|
if ($page === '') {
|
|
return $url;
|
|
}
|
|
|
|
$targetScript = page_entry_script($page);
|
|
if ($targetScript === null) {
|
|
return $url;
|
|
}
|
|
|
|
$path = (string)($parts['path'] ?? '');
|
|
$basename = strtolower(trim($path !== '' ? basename($path) : ''));
|
|
$knownRoutePage = $basename !== '' ? page_from_entry_script($basename) : null;
|
|
|
|
if ($path !== '' && str_starts_with($path, '/')) {
|
|
$targetPath = '/' . $targetScript;
|
|
} else {
|
|
$targetPath = $targetScript;
|
|
}
|
|
|
|
if ($basename === '' || $basename === 'index.php' || $knownRoutePage !== null) {
|
|
$parts['path'] = $targetPath;
|
|
unset($query['page']);
|
|
} else {
|
|
return $url;
|
|
}
|
|
|
|
$normalized = (string)($parts['path'] ?? $targetPath);
|
|
if (!empty($query)) {
|
|
$normalized .= '?' . http_build_query($query);
|
|
}
|
|
if (isset($parts['fragment']) && $parts['fragment'] !== '') {
|
|
$normalized .= '#' . $parts['fragment'];
|
|
}
|
|
|
|
return $normalized;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('page_request_is_ajax')) {
|
|
function page_request_is_ajax(): bool {
|
|
return strtolower((string)($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '')) === 'xmlhttprequest';
|
|
}
|
|
}
|
|
|
|
if (!function_exists('page_redirect_legacy_url')) {
|
|
function page_redirect_legacy_url(): void {
|
|
if (PHP_SAPI === 'cli') {
|
|
return;
|
|
}
|
|
|
|
if (strtoupper((string)($_SERVER['REQUEST_METHOD'] ?? 'GET')) !== 'GET') {
|
|
return;
|
|
}
|
|
|
|
if (!isset($_GET['page']) || page_request_is_ajax()) {
|
|
return;
|
|
}
|
|
|
|
$requestUri = (string)($_SERVER['REQUEST_URI'] ?? '');
|
|
if ($requestUri === '') {
|
|
return;
|
|
}
|
|
|
|
$normalized = page_normalize_url($requestUri);
|
|
if ($normalized !== '' && $normalized !== $requestUri) {
|
|
header('Location: ' . $normalized, true, 302);
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!function_exists('set_route_page_context')) {
|
|
function set_route_page_context(string $page): void {
|
|
$_GET['page'] = $page;
|
|
$_REQUEST['page'] = $page;
|
|
}
|
|
}
|