-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenero-status.php
More file actions
102 lines (89 loc) · 2.93 KB
/
genero-status.php
File metadata and controls
102 lines (89 loc) · 2.93 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
<?php
/**
* genero-status.php
*/
header('Cache-Control: max-age=0, no-cache, must-revalidate');
header('Pragma: no-cache');
header('Expires: Wed, 11 Jan 1984 05:00:00 GMT');
header('Content-Type: text/plain; charset=utf-8');
function exitWithError(string $message): void
{
http_response_code(503);
echo $message . PHP_EOL;
exit;
}
if (! is_readable($bootstrap = __DIR__ . '/wp-config.php')) {
exitWithError('Filesystem not found.');
}
// Minimally bootstrap WordPress
define('WP_INSTALLING', TRUE);
define('WP_SETUP_CONFIG', TRUE);
define('SHORTINIT', TRUE);
include_once $bootstrap;
// Optionally authenticate the HTTP request
if (defined('STATUS_API_KEY') && STATUS_API_KEY) {
$user = $_SERVER['PHP_AUTH_USER'] ?? null;
$pass = $_SERVER['PHP_AUTH_PW'] ?? null;
if (STATUS_API_KEY !== "$user:$pass") {
header('WWW-Authenticate: Basic realm="genero"');
http_response_code(401);
echo 'Not allowed' . PHP_EOL;
exit;
}
}
// Validate WordPress environment variable
if (defined('WP_ENV') && WP_ENV !== 'production') {
exitWithError(sprintf('In "%s" mode', WP_ENV));
}
// Validate database connection
if (! $wpdb->db_connect(false)) {
exitWithError('Database is down');
}
/** @var WP_Object_Cache $wp_object_cache */
global $wp_object_cache;
// Validate redis-cache plugin can connect to redis
if (method_exists($wp_object_cache, 'redis_status') && ! $wp_object_cache->redis_status()) {
exitWithError('Redis cant connect');
}
// Validate wp-redis plugin can connect to redis
if (isset($wp_object_cache->is_redis_connected) && ! $wp_object_cache->is_redis_connected) {
exitWithError('Redis cant connect');
}
// Hit the local origin directly (bypass any CDN/edge in the path) while
// preserving the public Host header so nginx vhost routing still resolves.
$host = parse_url(WP_HOME, PHP_URL_HOST);
$scheme = parse_url(WP_HOME, PHP_URL_SCHEME) ?: 'https';
$loopback = "{$scheme}://127.0.0.1";
$context = stream_context_create([
'http' => [
'timeout' => 5,
'header' => "Host: {$host}\r\nUser-Agent: genero-healthcheck\r\n",
'ignore_errors' => true,
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'peer_name' => $host,
'SNI_enabled' => true,
],
]);
// Validate there's no "noindex" tag on the frontpage
$content = @file_get_contents("{$loopback}/", false, $context);
if ($content === false) {
exitWithError('Frontpage cannot be reached');
}
if (preg_match('~<meta[^>]+robots[^>]+noindex~', $content) !== 0) {
exitWithError('Meta robots has noindex');
}
if (preg_match('~<body~', $content) === false) {
exitWithError('Corrupt HTML');
}
// Validate there's no Disallow all in the robots.txt
$content = @file_get_contents("{$loopback}/robots.txt", false, $context);
if (preg_match('~Disallow:\h*/(?:\R|$)~i', $content) !== 0) {
exitWithError('robots.txt disallow /');
}
// Everything passed.
http_response_code(200);
header('Content-Type: text/plain; charset=utf-8');
echo 'OK' . PHP_EOL;