@stagepass/cli 2.3.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -24
- package/package.json +1 -1
- package/src/cli.js +48 -5
- package/src/commands/daemon.js +18 -2
- package/src/commands/dev.js +14 -0
- package/src/commands/link.js +11 -1
- package/src/commands/new.js +39 -16
- package/src/commands/npm-run.js +14 -0
- package/src/commands/reload.js +5 -1
- package/src/commands/start.js +1 -1
- package/src/commands/upgrade.js +84 -32
- package/src/lib/autostart.js +66 -8
- package/src/lib/build-script-upgrade.js +244 -0
- package/src/lib/caddy-version.js +31 -0
- package/src/lib/caddyfile.js +76 -36
- package/src/lib/doctor-site.js +15 -10
- package/src/lib/doctor.js +131 -30
- package/src/lib/migrate.js +29 -2
- package/src/lib/package-versions.js +1 -1
- package/src/lib/paths.js +5 -0
- package/src/lib/php-runtime.js +249 -0
- package/src/lib/scaffold.js +22 -16
- package/templates/project/README.md.tmpl +8 -6
- package/templates/project/api/README.md.tmpl +172 -5
- package/templates/project/api/action.php +100 -0
- package/templates/project/api/actions/boom.php +21 -0
- package/templates/project/api/actions/contact.php +46 -0
- package/templates/project/api/actions/ping.php +23 -0
- package/templates/project/api/actions.php +59 -0
- package/templates/project/api/env.example +25 -0
- package/templates/project/api/lib/bootstrap.php +44 -0
- package/templates/project/api/lib/config.php +180 -0
- package/templates/project/api/lib/request.php +94 -0
- package/templates/project/api/lib/response.php +97 -0
- package/templates/project/api/lib/security.php +114 -0
- package/templates/project/api/lib/validate.php +69 -0
- package/templates/project/gitignore +4 -0
- package/templates/project/php-private/config.php +24 -0
- package/templates/project/scripts/build.mjs +114 -8
- package/templates/project/examples/php/README.md.tmpl +0 -18
- package/templates/project/examples/php/form.php +0 -105
- package/templates/project/examples/php/proxy.php +0 -171
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Contact form action. POST only.
|
|
5
|
+
* Write your own logic where the comment says. This starter does not send mail.
|
|
6
|
+
*
|
|
7
|
+
* Invalid input: throw ValidationException (status "fail", HTTP 422).
|
|
8
|
+
* Anything else that is not caught becomes status "error" with a generic HTTP 500.
|
|
9
|
+
*
|
|
10
|
+
* @return callable(array<string, mixed>, array): array<string, mixed>
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
declare(strict_types=1);
|
|
14
|
+
|
|
15
|
+
if (!defined('STAGEPASS_PHP_ENTRY')) {
|
|
16
|
+
http_response_code(404);
|
|
17
|
+
header('Content-Type: application/json; charset=utf-8');
|
|
18
|
+
echo '{"status":"fail","data":null,"message":"Not found."}';
|
|
19
|
+
exit;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return static function (array $input, array $config): array {
|
|
23
|
+
$errors = [];
|
|
24
|
+
|
|
25
|
+
validate_required($input, 'name', $errors);
|
|
26
|
+
validate_max_length($input, 'name', 120, $errors);
|
|
27
|
+
|
|
28
|
+
validate_required($input, 'email', $errors);
|
|
29
|
+
validate_email($input, 'email', $errors);
|
|
30
|
+
validate_max_length($input, 'email', 254, $errors);
|
|
31
|
+
|
|
32
|
+
validate_required($input, 'message', $errors);
|
|
33
|
+
validate_max_length($input, 'message', 4000, $errors);
|
|
34
|
+
|
|
35
|
+
if ($errors !== []) {
|
|
36
|
+
throw new ValidationException($errors);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Your logic here (save a record, call an API, enqueue a job).
|
|
40
|
+
// Do not add mail sending in this starter.
|
|
41
|
+
|
|
42
|
+
return [
|
|
43
|
+
'received' => true,
|
|
44
|
+
'name' => input_string($input, 'name'),
|
|
45
|
+
];
|
|
46
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Health-check action. GET only.
|
|
5
|
+
* You can change the returned array if you want a richer status payload.
|
|
6
|
+
*
|
|
7
|
+
* @return callable(array<string, mixed>, array): array<string, mixed>
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
declare(strict_types=1);
|
|
11
|
+
|
|
12
|
+
if (!defined('STAGEPASS_PHP_ENTRY')) {
|
|
13
|
+
http_response_code(404);
|
|
14
|
+
header('Content-Type: application/json; charset=utf-8');
|
|
15
|
+
echo '{"status":"fail","data":null,"message":"Not found."}';
|
|
16
|
+
exit;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return static function (array $input, array $config): array {
|
|
20
|
+
return [
|
|
21
|
+
'pong' => true,
|
|
22
|
+
];
|
|
23
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Action allowlist. This is the one file you edit to register a new action.
|
|
5
|
+
* Point each name at a file in api/actions/. Unknown names are rejected.
|
|
6
|
+
* The "boom" action is registered only when APP_ENV=local.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
declare(strict_types=1);
|
|
10
|
+
|
|
11
|
+
if (!defined('STAGEPASS_PHP_ENTRY')) {
|
|
12
|
+
http_response_code(404);
|
|
13
|
+
header('Content-Type: application/json; charset=utf-8');
|
|
14
|
+
echo '{"status":"fail","data":null,"message":"Not found."}';
|
|
15
|
+
exit;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @return array<string, array{file: string, methods: list<string>, honeypot: bool, rate_limit: bool, origin: string}>
|
|
20
|
+
*/
|
|
21
|
+
function action_catalog(array $config): array
|
|
22
|
+
{
|
|
23
|
+
$actions = [
|
|
24
|
+
'ping' => [
|
|
25
|
+
'file' => 'ping.php',
|
|
26
|
+
'methods' => ['GET'],
|
|
27
|
+
'honeypot' => false,
|
|
28
|
+
'rate_limit' => true,
|
|
29
|
+
'origin' => 'optional',
|
|
30
|
+
],
|
|
31
|
+
'contact' => [
|
|
32
|
+
'file' => 'contact.php',
|
|
33
|
+
'methods' => ['POST'],
|
|
34
|
+
'honeypot' => true,
|
|
35
|
+
'rate_limit' => true,
|
|
36
|
+
'origin' => 'required',
|
|
37
|
+
],
|
|
38
|
+
// Add your action here, then create the matching file in api/actions/.
|
|
39
|
+
// 'newsletter' => [
|
|
40
|
+
// 'file' => 'newsletter.php',
|
|
41
|
+
// 'methods' => ['POST'],
|
|
42
|
+
// 'honeypot' => true,
|
|
43
|
+
// 'rate_limit' => true,
|
|
44
|
+
// 'origin' => 'required',
|
|
45
|
+
// ],
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
if (is_local_env($config)) {
|
|
49
|
+
$actions['boom'] = [
|
|
50
|
+
'file' => 'boom.php',
|
|
51
|
+
'methods' => ['GET'],
|
|
52
|
+
'honeypot' => false,
|
|
53
|
+
'rate_limit' => false,
|
|
54
|
+
'origin' => 'optional',
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return $actions;
|
|
59
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Copy this file to the project root as ".env" and edit the values.
|
|
2
|
+
# This is the main place to configure origins, limits, and the environment.
|
|
3
|
+
# Never commit real secrets. This boilerplate does not send mail.
|
|
4
|
+
|
|
5
|
+
# "local" or "production". The "boom" test action is only registered when local.
|
|
6
|
+
APP_ENV=production
|
|
7
|
+
|
|
8
|
+
# Exact origins allowed to call POST actions and to receive CORS headers.
|
|
9
|
+
# Comma-separated, no trailing slashes, include the scheme.
|
|
10
|
+
# Local Stagepass: https://your-project.sp
|
|
11
|
+
# Webflow preview or live site: https://your-site.webflow.io,https://www.example.com
|
|
12
|
+
ALLOWED_ORIGINS=https://your-project.sp,https://your-site.webflow.io
|
|
13
|
+
|
|
14
|
+
# Hidden form field. If it is filled in, the request is treated as a bot
|
|
15
|
+
# and a generic success is returned without running the handler.
|
|
16
|
+
HONEYPOT_FIELD=website
|
|
17
|
+
|
|
18
|
+
# Maximum accepted requests per IP and action within the window (seconds).
|
|
19
|
+
RATE_LIMIT_MAX=20
|
|
20
|
+
RATE_LIMIT_WINDOW=60
|
|
21
|
+
|
|
22
|
+
# JSON / form body size cap in bytes.
|
|
23
|
+
MAX_BODY_BYTES=65536
|
|
24
|
+
|
|
25
|
+
APP_TIMEZONE=Europe/Berlin
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Loads the shared library files. Not a public endpoint.
|
|
5
|
+
* Core file: leave this alone.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
declare(strict_types=1);
|
|
9
|
+
|
|
10
|
+
if (!defined('STAGEPASS_PHP_ENTRY')) {
|
|
11
|
+
http_response_code(404);
|
|
12
|
+
header('Content-Type: application/json; charset=utf-8');
|
|
13
|
+
echo '{"status":"fail","data":null,"message":"Not found."}';
|
|
14
|
+
exit;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
require __DIR__ . '/config.php';
|
|
18
|
+
require __DIR__ . '/validate.php';
|
|
19
|
+
require __DIR__ . '/response.php';
|
|
20
|
+
require __DIR__ . '/request.php';
|
|
21
|
+
require __DIR__ . '/security.php';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Include a handler from api/actions/. The file name must already come from the allowlist.
|
|
25
|
+
*/
|
|
26
|
+
function load_handler(string $file): callable
|
|
27
|
+
{
|
|
28
|
+
if (basename($file) !== $file || preg_match('/^[a-z0-9_-]+\.php$/', $file) !== 1) {
|
|
29
|
+
throw new RuntimeException('Invalid handler name.');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
$dir = realpath(__DIR__ . '/../actions');
|
|
33
|
+
$path = realpath(__DIR__ . '/../actions/' . $file);
|
|
34
|
+
if ($dir === false || $path === false || !str_starts_with($path, $dir . DIRECTORY_SEPARATOR)) {
|
|
35
|
+
throw new RuntimeException('Handler not found.');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
$fn = require $path;
|
|
39
|
+
if (!is_callable($fn)) {
|
|
40
|
+
throw new RuntimeException('Handler must return a callable.');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return $fn;
|
|
44
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Loads .env and the optional .php/config.php.
|
|
5
|
+
* Edit values in .env (or in .php/config.php), not in this file.
|
|
6
|
+
* Core file: leave this alone.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
declare(strict_types=1);
|
|
10
|
+
|
|
11
|
+
if (!defined('STAGEPASS_PHP_ENTRY')) {
|
|
12
|
+
http_response_code(404);
|
|
13
|
+
header('Content-Type: application/json; charset=utf-8');
|
|
14
|
+
echo '{"status":"fail","data":null,"message":"Not found."}';
|
|
15
|
+
exit;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Project root (parent of api/). */
|
|
19
|
+
function project_root(): string
|
|
20
|
+
{
|
|
21
|
+
return dirname(__DIR__, 2);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Hidden directory for config and rate-limit files.
|
|
26
|
+
*
|
|
27
|
+
* On Stagepass, Caddy already returns 404 for any path segment starting with ".".
|
|
28
|
+
*/
|
|
29
|
+
function private_dir(): string
|
|
30
|
+
{
|
|
31
|
+
return project_root() . '/.php';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* True when this file is the script PHP was asked to run (direct HTTP hit).
|
|
36
|
+
*/
|
|
37
|
+
function is_direct_script_access(string $file): bool
|
|
38
|
+
{
|
|
39
|
+
$script = (string) ($_SERVER['SCRIPT_FILENAME'] ?? '');
|
|
40
|
+
if ($script === '') {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
$scriptReal = realpath($script);
|
|
45
|
+
$fileReal = realpath($file);
|
|
46
|
+
|
|
47
|
+
return $scriptReal !== false && $fileReal !== false && $scriptReal === $fileReal;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Load KEY=value pairs from a .env file without overwriting real environment variables.
|
|
52
|
+
*/
|
|
53
|
+
function load_dotenv(string $path): void
|
|
54
|
+
{
|
|
55
|
+
if (!is_readable($path)) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
$lines = file($path, FILE_IGNORE_NEW_LINES);
|
|
60
|
+
if ($lines === false) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
foreach ($lines as $line) {
|
|
65
|
+
$line = trim($line);
|
|
66
|
+
if ($line === '' || str_starts_with($line, '#')) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (str_starts_with($line, 'export ')) {
|
|
70
|
+
$line = trim(substr($line, 7));
|
|
71
|
+
}
|
|
72
|
+
$pos = strpos($line, '=');
|
|
73
|
+
if ($pos === false) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
$key = trim(substr($line, 0, $pos));
|
|
77
|
+
$value = trim(substr($line, $pos + 1));
|
|
78
|
+
if ($key === '' || !preg_match('/^[A-Z_][A-Z0-9_]*$/', $key)) {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (getenv($key) !== false) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (
|
|
85
|
+
(str_starts_with($value, '"') && str_ends_with($value, '"'))
|
|
86
|
+
|| (str_starts_with($value, "'") && str_ends_with($value, "'"))
|
|
87
|
+
) {
|
|
88
|
+
$value = substr($value, 1, -1);
|
|
89
|
+
}
|
|
90
|
+
putenv($key . '=' . $value);
|
|
91
|
+
$_ENV[$key] = $value;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function env_string(string $key, string $default = ''): string
|
|
96
|
+
{
|
|
97
|
+
$value = getenv($key);
|
|
98
|
+
return $value === false ? $default : $value;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function env_int(string $key, int $default): int
|
|
102
|
+
{
|
|
103
|
+
$value = getenv($key);
|
|
104
|
+
if ($value === false || $value === '') {
|
|
105
|
+
return $default;
|
|
106
|
+
}
|
|
107
|
+
if (!preg_match('/^-?\d+$/', $value)) {
|
|
108
|
+
return $default;
|
|
109
|
+
}
|
|
110
|
+
return (int) $value;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @return list<string>
|
|
115
|
+
*/
|
|
116
|
+
function env_list(string $key): array
|
|
117
|
+
{
|
|
118
|
+
$raw = env_string($key, '');
|
|
119
|
+
if ($raw === '') {
|
|
120
|
+
return [];
|
|
121
|
+
}
|
|
122
|
+
$items = [];
|
|
123
|
+
foreach (explode(',', $raw) as $part) {
|
|
124
|
+
$item = trim($part);
|
|
125
|
+
if ($item !== '') {
|
|
126
|
+
$items[] = $item;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return array_values(array_unique($items));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Load configuration from .env and an optional PHP config file outside the public root.
|
|
134
|
+
*
|
|
135
|
+
* @return array{
|
|
136
|
+
* env: string,
|
|
137
|
+
* allowed_origins: list<string>,
|
|
138
|
+
* honeypot_field: string,
|
|
139
|
+
* rate_limit_max: int,
|
|
140
|
+
* rate_limit_window: int,
|
|
141
|
+
* max_body_bytes: int,
|
|
142
|
+
* timezone: string
|
|
143
|
+
* }
|
|
144
|
+
*/
|
|
145
|
+
function load_config(): array
|
|
146
|
+
{
|
|
147
|
+
load_dotenv(project_root() . '/.env');
|
|
148
|
+
load_dotenv(private_dir() . '/.env');
|
|
149
|
+
|
|
150
|
+
$defaults = [
|
|
151
|
+
'env' => env_string('APP_ENV', 'production'),
|
|
152
|
+
'allowed_origins' => env_list('ALLOWED_ORIGINS'),
|
|
153
|
+
'honeypot_field' => env_string('HONEYPOT_FIELD', 'website'),
|
|
154
|
+
'rate_limit_max' => env_int('RATE_LIMIT_MAX', 20),
|
|
155
|
+
'rate_limit_window' => env_int('RATE_LIMIT_WINDOW', 60),
|
|
156
|
+
'max_body_bytes' => env_int('MAX_BODY_BYTES', 65536),
|
|
157
|
+
'timezone' => env_string('APP_TIMEZONE', 'Europe/Berlin'),
|
|
158
|
+
];
|
|
159
|
+
|
|
160
|
+
$file = null;
|
|
161
|
+
if (is_readable(private_dir() . '/config.php')) {
|
|
162
|
+
$file = private_dir() . '/config.php';
|
|
163
|
+
} elseif (is_readable(project_root() . '/config.php')) {
|
|
164
|
+
$file = project_root() . '/config.php';
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if ($file !== null) {
|
|
168
|
+
$fromFile = require $file;
|
|
169
|
+
if (is_array($fromFile)) {
|
|
170
|
+
return array_merge($defaults, $fromFile);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return $defaults;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function is_local_env(array $config): bool
|
|
178
|
+
{
|
|
179
|
+
return ($config['env'] ?? '') === 'local';
|
|
180
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Reads the HTTP method, Origin, and JSON or form bodies.
|
|
5
|
+
* Core file: you rarely need to edit this.
|
|
6
|
+
* In your actions, use input_string() to read a field.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
declare(strict_types=1);
|
|
10
|
+
|
|
11
|
+
if (!defined('STAGEPASS_PHP_ENTRY')) {
|
|
12
|
+
http_response_code(404);
|
|
13
|
+
header('Content-Type: application/json; charset=utf-8');
|
|
14
|
+
echo '{"status":"fail","data":null,"message":"Not found."}';
|
|
15
|
+
exit;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function request_method(): string
|
|
19
|
+
{
|
|
20
|
+
return strtoupper((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET'));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function request_origin(): string
|
|
24
|
+
{
|
|
25
|
+
return trim((string) ($_SERVER['HTTP_ORIGIN'] ?? ''));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function client_ip(): string
|
|
29
|
+
{
|
|
30
|
+
$ip = (string) ($_SERVER['REMOTE_ADDR'] ?? '');
|
|
31
|
+
return $ip !== '' ? $ip : '0.0.0.0';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Parse JSON or form bodies. Query parameters are the base; body fields win.
|
|
36
|
+
*
|
|
37
|
+
* @return array<string, mixed>
|
|
38
|
+
*/
|
|
39
|
+
function parse_input(int $maxBodyBytes): array
|
|
40
|
+
{
|
|
41
|
+
$input = $_GET;
|
|
42
|
+
$method = request_method();
|
|
43
|
+
if (!in_array($method, ['POST', 'PUT', 'PATCH'], true)) {
|
|
44
|
+
return $input;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
$contentType = (string) ($_SERVER['CONTENT_TYPE'] ?? $_SERVER['HTTP_CONTENT_TYPE'] ?? '');
|
|
48
|
+
$mediaType = strtolower(trim(explode(';', $contentType, 2)[0]));
|
|
49
|
+
|
|
50
|
+
if ($mediaType === 'application/json') {
|
|
51
|
+
$raw = file_get_contents('php://input', false, null, 0, $maxBodyBytes + 1);
|
|
52
|
+
if ($raw === false) {
|
|
53
|
+
respond_fail(['body' => 'Could not read the request body.'], 'Invalid request body.', 400);
|
|
54
|
+
}
|
|
55
|
+
if (strlen($raw) > $maxBodyBytes) {
|
|
56
|
+
respond_fail(['body' => 'The request body is too large.'], 'The request body is too large.', 413);
|
|
57
|
+
}
|
|
58
|
+
$raw = trim($raw);
|
|
59
|
+
if ($raw === '') {
|
|
60
|
+
return $input;
|
|
61
|
+
}
|
|
62
|
+
$decoded = json_decode($raw, true);
|
|
63
|
+
if (!is_array($decoded)) {
|
|
64
|
+
respond_fail(['body' => 'Invalid JSON.'], 'Invalid JSON.', 400);
|
|
65
|
+
}
|
|
66
|
+
return array_merge($input, $decoded);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (
|
|
70
|
+
$mediaType === ''
|
|
71
|
+
|| $mediaType === 'application/x-www-form-urlencoded'
|
|
72
|
+
|| $mediaType === 'multipart/form-data'
|
|
73
|
+
) {
|
|
74
|
+
return array_merge($input, $_POST);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
respond_fail(
|
|
78
|
+
['content_type' => 'Use application/json or a form content type.'],
|
|
79
|
+
'Unsupported content type.',
|
|
80
|
+
415
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function input_string(array $input, string $key): string
|
|
85
|
+
{
|
|
86
|
+
$value = $input[$key] ?? '';
|
|
87
|
+
if (is_int($value) || is_float($value)) {
|
|
88
|
+
$value = (string) $value;
|
|
89
|
+
}
|
|
90
|
+
if (!is_string($value)) {
|
|
91
|
+
return '';
|
|
92
|
+
}
|
|
93
|
+
return trim($value);
|
|
94
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* JSend envelope and HTTP helpers (success, fail, error).
|
|
5
|
+
* Core file: you rarely need to edit this.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
declare(strict_types=1);
|
|
9
|
+
|
|
10
|
+
if (!defined('STAGEPASS_PHP_ENTRY')) {
|
|
11
|
+
http_response_code(404);
|
|
12
|
+
header('Content-Type: application/json; charset=utf-8');
|
|
13
|
+
echo '{"status":"fail","data":null,"message":"Not found."}';
|
|
14
|
+
exit;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function json_headers(int $http): void
|
|
18
|
+
{
|
|
19
|
+
if (headers_sent()) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
header_remove('X-Powered-By');
|
|
23
|
+
http_response_code($http);
|
|
24
|
+
header('Content-Type: application/json; charset=utf-8');
|
|
25
|
+
header('X-Content-Type-Options: nosniff');
|
|
26
|
+
header('Cache-Control: no-store');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @param mixed $data
|
|
31
|
+
*/
|
|
32
|
+
function respond(string $status, $data, ?string $message, int $http): never
|
|
33
|
+
{
|
|
34
|
+
json_headers($http);
|
|
35
|
+
$json = json_encode(
|
|
36
|
+
[
|
|
37
|
+
'status' => $status,
|
|
38
|
+
'data' => $data,
|
|
39
|
+
'message' => $message,
|
|
40
|
+
],
|
|
41
|
+
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE
|
|
42
|
+
);
|
|
43
|
+
echo $json === false ? '{"status":"error","data":null,"message":"Could not encode the response."}' : $json;
|
|
44
|
+
exit;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** @param mixed $data */
|
|
48
|
+
function respond_success($data = null, ?string $message = null, int $http = 200): never
|
|
49
|
+
{
|
|
50
|
+
respond('success', $data, $message, $http);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** @param mixed $data */
|
|
54
|
+
function respond_fail($data, ?string $message = null, int $http = 400): never
|
|
55
|
+
{
|
|
56
|
+
respond('fail', $data, $message ?? 'The request was rejected.', $http);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** @param mixed $data */
|
|
60
|
+
function respond_error(string $message, int $http = 500, $data = null): never
|
|
61
|
+
{
|
|
62
|
+
respond('error', $data, $message, $http);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function install_error_handlers(): void
|
|
66
|
+
{
|
|
67
|
+
set_exception_handler(static function (Throwable $e): void {
|
|
68
|
+
if ($e instanceof ValidationException) {
|
|
69
|
+
respond_fail($e->errors(), $e->getMessage(), 422);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
error_log('stagepass-php: ' . $e::class . ': ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine());
|
|
73
|
+
respond_error('An unexpected error occurred. Try again later.', 500);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
set_error_handler(static function (int $severity, string $message, string $file, int $line): bool {
|
|
77
|
+
if ((error_reporting() & $severity) === 0) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
throw new ErrorException($message, 0, $severity, $file, $line);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
register_shutdown_function(static function (): void {
|
|
84
|
+
$err = error_get_last();
|
|
85
|
+
if ($err === null) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
$fatal = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR];
|
|
89
|
+
if (!in_array($err['type'], $fatal, true)) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
error_log('stagepass-php fatal: ' . $err['message'] . ' in ' . $err['file'] . ':' . $err['line']);
|
|
93
|
+
if (!headers_sent()) {
|
|
94
|
+
respond_error('An unexpected error occurred. Try again later.', 500);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Origin allowlist, CORS, honeypot, and the file-based rate limiter.
|
|
5
|
+
* Core file: you rarely need to edit this.
|
|
6
|
+
* Change origins, limits, and the honeypot field name in .env.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
declare(strict_types=1);
|
|
10
|
+
|
|
11
|
+
if (!defined('STAGEPASS_PHP_ENTRY')) {
|
|
12
|
+
http_response_code(404);
|
|
13
|
+
header('Content-Type: application/json; charset=utf-8');
|
|
14
|
+
echo '{"status":"fail","data":null,"message":"Not found."}';
|
|
15
|
+
exit;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function origin_is_allowed(string $origin, array $allowed): bool
|
|
19
|
+
{
|
|
20
|
+
if ($origin === '' || $allowed === []) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
return in_array($origin, $allowed, true);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Send CORS headers when the Origin is on the allowlist. Never echo an unknown origin.
|
|
28
|
+
*/
|
|
29
|
+
function apply_cors_headers(string $origin, array $allowed): void
|
|
30
|
+
{
|
|
31
|
+
if (!origin_is_allowed($origin, $allowed)) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (headers_sent()) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
header('Access-Control-Allow-Origin: ' . $origin);
|
|
38
|
+
header('Vary: Origin');
|
|
39
|
+
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
|
40
|
+
header('Access-Control-Allow-Headers: Content-Type, Accept');
|
|
41
|
+
header('Access-Control-Max-Age: 86400');
|
|
42
|
+
header('Access-Control-Allow-Credentials: false');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function honeypot_is_filled(array $input, string $field): bool
|
|
46
|
+
{
|
|
47
|
+
return $field !== '' && input_string($input, $field) !== '';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* File-based fixed-window limiter, keyed by IP and action.
|
|
52
|
+
*
|
|
53
|
+
* @return bool True when the request may proceed.
|
|
54
|
+
*/
|
|
55
|
+
function rate_limit_allows(string $ip, string $action, int $max, int $window): bool
|
|
56
|
+
{
|
|
57
|
+
if ($max <= 0 || $window <= 0) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
$dir = private_dir() . '/rate-limit';
|
|
62
|
+
if (!is_dir($dir) && !mkdir($dir, 0700, true) && !is_dir($dir)) {
|
|
63
|
+
error_log('stagepass-php: could not create the rate-limit directory.');
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
$file = $dir . '/' . hash('sha256', $ip . '|' . $action) . '.json';
|
|
68
|
+
$handle = fopen($file, 'c+');
|
|
69
|
+
if ($handle === false) {
|
|
70
|
+
error_log('stagepass-php: could not open a rate-limit file.');
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
if (!flock($handle, LOCK_EX)) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
$raw = stream_get_contents($handle);
|
|
79
|
+
$decoded = [];
|
|
80
|
+
if (is_string($raw) && $raw !== '') {
|
|
81
|
+
$parsed = json_decode($raw, true);
|
|
82
|
+
if (is_array($parsed)) {
|
|
83
|
+
$decoded = $parsed;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
$now = time();
|
|
88
|
+
$cutoff = $now - $window;
|
|
89
|
+
$hits = [];
|
|
90
|
+
foreach ($decoded as $ts) {
|
|
91
|
+
if (is_numeric($ts) && (int) $ts > $cutoff) {
|
|
92
|
+
$hits[] = (int) $ts;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (count($hits) >= $max) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
$hits[] = $now;
|
|
101
|
+
rewind($handle);
|
|
102
|
+
ftruncate($handle, 0);
|
|
103
|
+
fwrite($handle, json_encode($hits, JSON_THROW_ON_ERROR));
|
|
104
|
+
fflush($handle);
|
|
105
|
+
|
|
106
|
+
return true;
|
|
107
|
+
} catch (Throwable $e) {
|
|
108
|
+
error_log('stagepass-php: rate-limit error: ' . $e->getMessage());
|
|
109
|
+
return true;
|
|
110
|
+
} finally {
|
|
111
|
+
flock($handle, LOCK_UN);
|
|
112
|
+
fclose($handle);
|
|
113
|
+
}
|
|
114
|
+
}
|