Repository navigation
Expand file tree
/
Copy pathserver.php
More file actions
112 lines (90 loc) · 3.25 KB
/
Copy pathserver.php
File metadata and controls
112 lines (90 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* Berkan - Apache-based PHP Development Environment
*
* This is the main request router script. Apache routes all requests
* to this file, which then determines the appropriate site and driver.
*/
// Define constants
define('BERKAN_HOME_PATH', $_SERVER['HOME'] . '/.config/berkan');
define('BERKAN_SERVER_PATH', __DIR__);
define('BERKAN_STATIC_PREFIX', '/41c270e4-5535-4daa-b23e-c269e73c6c01');
// Polyfills for PHP 7.x compatibility
if (! function_exists('str_starts_with')) {
function str_starts_with(string $haystack, string $needle): bool
{
return strncmp($haystack, $needle, strlen($needle)) === 0;
}
}
if (! function_exists('str_ends_with')) {
function str_ends_with(string $haystack, string $needle): bool
{
if ($needle === '') {
return true;
}
return substr($haystack, -strlen($needle)) === $needle;
}
}
if (! function_exists('str_contains')) {
function str_contains(string $haystack, string $needle): bool
{
return strpos($haystack, $needle) !== false;
}
}
// Load only the Berkan classes needed at runtime (no Composer autoloader)
// This ensures compatibility with older PHP versions (7.x) used by isolated sites
require_once __DIR__ . '/cli/Berkan/Server.php';
require_once __DIR__ . '/cli/Berkan/Drivers/BerkanDriver.php';
require_once __DIR__ . '/cli/Berkan/Drivers/BasicWithPublicBerkanDriver.php';
require_once __DIR__ . '/cli/Berkan/Drivers/BasicBerkanDriver.php';
use Berkan\Server;
use Berkan\Drivers\BerkanDriver;
// Get the request URI and host
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$serverName = $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? 'localhost';
// Check for Berkan static file prefix
if (str_starts_with($uri, BERKAN_STATIC_PREFIX)) {
$uri = substr($uri, strlen(BERKAN_STATIC_PREFIX));
}
// Resolve the site
[$sitePath, $siteName, $uri] = Server::resolve($uri, $serverName);
// If no site path was found, show 404
if (empty($sitePath) || ! is_dir($sitePath)) {
Server::show404();
return;
}
// Find the appropriate driver
$driver = BerkanDriver::assign($sitePath, $siteName, $uri);
if (! $driver) {
Server::show404();
return;
}
// Allow the driver to mutate the URI
$uri = $driver->mutateUri($uri);
// Check if the request is for a static file
$staticFilePath = $driver->isStaticFile($sitePath, $siteName, $uri);
if ($staticFilePath) {
$driver->serveStaticFile($staticFilePath, $sitePath, $siteName, $uri);
return;
}
// Get the front controller path
$frontControllerPath = $driver->frontControllerPath($sitePath, $siteName, $uri);
if (! file_exists($frontControllerPath)) {
Server::show404();
return;
}
// Set up the server environment
// Note: DOCUMENT_ROOT, SCRIPT_NAME, and SCRIPT_FILENAME are set by the
// driver's frontControllerPath() above — do not overwrite them here.
$_SERVER['SERVER_NAME'] = $serverName;
$_SERVER['PHP_SELF'] = $uri;
// Apply error display setting from config
$berkanConfig = Server::loadConfig();
if (! empty($berkanConfig['hide_errors'])) {
error_reporting(0);
ini_set('display_errors', '0');
}
// Change to the site directory for relative path resolution
chdir(dirname($frontControllerPath));
// Include the front controller
require $frontControllerPath;