Repository navigation
Expand file tree
/
Copy pathentrypoint.php
More file actions
119 lines (95 loc) · 3.38 KB
/
Copy pathentrypoint.php
File metadata and controls
119 lines (95 loc) · 3.38 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
113
114
115
116
117
118
119
<?php
use NginxUnit\HttpServer;
use NginxUnit\Request;
use NginxUnit\Response;
set_time_limit(0);
// Logging function
function log_debug($message) {
file_put_contents(__DIR__.'/debug.log', date('[Y-m-d H:i:s] ') . $message . "\n", FILE_APPEND);
}
log_debug("=== Entrypoint started ===");
// Register shutdown function to catch fatal errors
register_shutdown_function(function() {
$error = error_get_last();
if ($error !== null) {
log_debug("SHUTDOWN: " . print_r($error, true));
}
});
// Set error handler
set_error_handler(function($errno, $errstr, $errfile, $errline) {
log_debug("ERROR[$errno]: $errstr in $errfile:$errline");
return false; // Let PHP handle it normally
});
// Load WordPress core
include_once 'wp-loader.php';
//
// The function, the entry point, is called from NGINX UNIT.
//
HttpServer::onRequest(static function (Request $request, Response $response) {
$method = $request->getMethod();
$uri = $request->getUri();
$path = parse_url($uri, PHP_URL_PATH);
// Remove leading slash
$path = ltrim($path, '/');
// Default to index.php if path is empty
if (empty($path) || $path === '/') {
$path = 'index.php';
}
// Full file path
$file = WP_ROOT . '/' . $path;
// Check if it's a static file (not PHP)
if (is_file($file) && !str_ends_with($path, '.php')) {
// Serve static file
$mimeTypes = [
'css' => 'text/css',
'js' => 'application/javascript',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'svg' => 'image/svg+xml',
'ico' => 'image/x-icon',
'woff' => 'font/woff',
'woff2' => 'font/woff2',
'ttf' => 'font/ttf',
'json' => 'application/json',
'xml' => 'application/xml',
];
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
$contentType = $mimeTypes[$ext] ?? 'application/octet-stream';
$response->setHeader('Content-Type', $contentType);
$response->setStatus(200);
$response->write(file_get_contents($file));
$response->end();
return;
}
// Set up WordPress environment variables
$_SERVER['SCRIPT_FILENAME'] = $file;
$_SERVER['SCRIPT_NAME'] = '/' . $path;
$_SERVER['PHP_SELF'] = '/' . $path;
$_SERVER['DOCUMENT_ROOT'] = WP_ROOT;
ob_start();
try {
WPShared::cloneGlobals();
wp();
$template_loader = ABSPATH . WPINC . '/template-loader.php';
include $template_loader;
// Get the output
$output = ob_get_clean();
if (!headers_sent()) {
$response->setStatus(200);
$response->setHeader('Content-Type', 'text/html; charset=UTF-8');
}
$response->write($output);
$response->end();
} catch (Throwable $e) {
log_debug("ERROR processing PHP file: " . $e->getMessage());
ob_end_clean();
$response->setStatus(500);
$response->setHeader('Content-Type', 'text/html; charset=UTF-8');
$response->write('<h1>Error</h1>');
$response->write('<pre>' . htmlspecialchars($e->getMessage()) . '</pre>');
$response->write('<pre>' . htmlspecialchars($e->getTraceAsString()) . '</pre>');
$response->end();
}
});