@stagepass/cli 2.3.1 → 2.4.1
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 +55 -15
- package/package.json +1 -1
- package/src/cli.js +48 -5
- 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 +103 -32
- package/src/lib/build-script-upgrade.js +249 -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 +30 -29
- package/src/lib/htaccess-upgrade.js +79 -0
- package/src/lib/migrate.js +29 -2
- package/src/lib/package-versions.js +1 -1
- package/src/lib/php-runtime.js +249 -0
- package/src/lib/scaffold.js +24 -16
- package/templates/project/README.md.tmpl +8 -6
- package/templates/project/api/README.md.tmpl +206 -5
- package/templates/project/api/action.php +103 -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 +179 -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/htaccess +7 -0
- package/templates/project/php-private/config.php +24 -0
- package/templates/project/scripts/build.mjs +128 -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,179 @@
|
|
|
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
|
+
* Config and rate-limit directory, inside the document root (the folder that contains api/).
|
|
26
|
+
* Deny hidden paths with the shipped .htaccess, the nginx snippet in api/README.md, or Caddy locally.
|
|
27
|
+
*/
|
|
28
|
+
function private_dir(): string
|
|
29
|
+
{
|
|
30
|
+
return project_root() . '/.php';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* True when this file is the script PHP was asked to run (direct HTTP hit).
|
|
35
|
+
*/
|
|
36
|
+
function is_direct_script_access(string $file): bool
|
|
37
|
+
{
|
|
38
|
+
$script = (string) ($_SERVER['SCRIPT_FILENAME'] ?? '');
|
|
39
|
+
if ($script === '') {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
$scriptReal = realpath($script);
|
|
44
|
+
$fileReal = realpath($file);
|
|
45
|
+
|
|
46
|
+
return $scriptReal !== false && $fileReal !== false && $scriptReal === $fileReal;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Load KEY=value pairs from a .env file without overwriting real environment variables.
|
|
51
|
+
*/
|
|
52
|
+
function load_dotenv(string $path): void
|
|
53
|
+
{
|
|
54
|
+
if (!is_readable($path)) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
$lines = file($path, FILE_IGNORE_NEW_LINES);
|
|
59
|
+
if ($lines === false) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
foreach ($lines as $line) {
|
|
64
|
+
$line = trim($line);
|
|
65
|
+
if ($line === '' || str_starts_with($line, '#')) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (str_starts_with($line, 'export ')) {
|
|
69
|
+
$line = trim(substr($line, 7));
|
|
70
|
+
}
|
|
71
|
+
$pos = strpos($line, '=');
|
|
72
|
+
if ($pos === false) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
$key = trim(substr($line, 0, $pos));
|
|
76
|
+
$value = trim(substr($line, $pos + 1));
|
|
77
|
+
if ($key === '' || !preg_match('/^[A-Z_][A-Z0-9_]*$/', $key)) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (getenv($key) !== false) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (
|
|
84
|
+
(str_starts_with($value, '"') && str_ends_with($value, '"'))
|
|
85
|
+
|| (str_starts_with($value, "'") && str_ends_with($value, "'"))
|
|
86
|
+
) {
|
|
87
|
+
$value = substr($value, 1, -1);
|
|
88
|
+
}
|
|
89
|
+
putenv($key . '=' . $value);
|
|
90
|
+
$_ENV[$key] = $value;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function env_string(string $key, string $default = ''): string
|
|
95
|
+
{
|
|
96
|
+
$value = getenv($key);
|
|
97
|
+
return $value === false ? $default : $value;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function env_int(string $key, int $default): int
|
|
101
|
+
{
|
|
102
|
+
$value = getenv($key);
|
|
103
|
+
if ($value === false || $value === '') {
|
|
104
|
+
return $default;
|
|
105
|
+
}
|
|
106
|
+
if (!preg_match('/^-?\d+$/', $value)) {
|
|
107
|
+
return $default;
|
|
108
|
+
}
|
|
109
|
+
return (int) $value;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @return list<string>
|
|
114
|
+
*/
|
|
115
|
+
function env_list(string $key): array
|
|
116
|
+
{
|
|
117
|
+
$raw = env_string($key, '');
|
|
118
|
+
if ($raw === '') {
|
|
119
|
+
return [];
|
|
120
|
+
}
|
|
121
|
+
$items = [];
|
|
122
|
+
foreach (explode(',', $raw) as $part) {
|
|
123
|
+
$item = trim($part);
|
|
124
|
+
if ($item !== '') {
|
|
125
|
+
$items[] = $item;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return array_values(array_unique($items));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Load configuration from the project-root .env, .php/.env, and optional .php/config.php.
|
|
133
|
+
*
|
|
134
|
+
* @return array{
|
|
135
|
+
* env: string,
|
|
136
|
+
* allowed_origins: list<string>,
|
|
137
|
+
* honeypot_field: string,
|
|
138
|
+
* rate_limit_max: int,
|
|
139
|
+
* rate_limit_window: int,
|
|
140
|
+
* max_body_bytes: int,
|
|
141
|
+
* timezone: string
|
|
142
|
+
* }
|
|
143
|
+
*/
|
|
144
|
+
function load_config(): array
|
|
145
|
+
{
|
|
146
|
+
load_dotenv(project_root() . '/.env');
|
|
147
|
+
load_dotenv(private_dir() . '/.env');
|
|
148
|
+
|
|
149
|
+
$defaults = [
|
|
150
|
+
'env' => env_string('APP_ENV', 'production'),
|
|
151
|
+
'allowed_origins' => env_list('ALLOWED_ORIGINS'),
|
|
152
|
+
'honeypot_field' => env_string('HONEYPOT_FIELD', 'website'),
|
|
153
|
+
'rate_limit_max' => env_int('RATE_LIMIT_MAX', 20),
|
|
154
|
+
'rate_limit_window' => env_int('RATE_LIMIT_WINDOW', 60),
|
|
155
|
+
'max_body_bytes' => env_int('MAX_BODY_BYTES', 65536),
|
|
156
|
+
'timezone' => env_string('APP_TIMEZONE', 'Europe/Berlin'),
|
|
157
|
+
];
|
|
158
|
+
|
|
159
|
+
$file = null;
|
|
160
|
+
if (is_readable(private_dir() . '/config.php')) {
|
|
161
|
+
$file = private_dir() . '/config.php';
|
|
162
|
+
} elseif (is_readable(project_root() . '/config.php')) {
|
|
163
|
+
$file = project_root() . '/config.php';
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if ($file !== null) {
|
|
167
|
+
$fromFile = require $file;
|
|
168
|
+
if (is_array($fromFile)) {
|
|
169
|
+
return array_merge($defaults, $fromFile);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return $defaults;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function is_local_env(array $config): bool
|
|
177
|
+
{
|
|
178
|
+
return ($config['env'] ?? '') === 'local';
|
|
179
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Field checks used by your actions.
|
|
5
|
+
* Throw ValidationException when input is invalid. That becomes a 422 fail.
|
|
6
|
+
* Add a helper here only when several actions share the same check.
|
|
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
|
+
* Validation failure with per-field messages (JSend "fail", HTTP 422).
|
|
20
|
+
*/
|
|
21
|
+
final class ValidationException extends RuntimeException
|
|
22
|
+
{
|
|
23
|
+
/** @param array<string, string> $errors */
|
|
24
|
+
public function __construct(private array $errors)
|
|
25
|
+
{
|
|
26
|
+
parent::__construct('Validation failed.');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** @return array<string, string> */
|
|
30
|
+
public function errors(): array
|
|
31
|
+
{
|
|
32
|
+
return $this->errors;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function validate_required(array $input, string $key, array &$errors): void
|
|
37
|
+
{
|
|
38
|
+
if (input_string($input, $key) === '') {
|
|
39
|
+
$errors[$key] = 'This field is required.';
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function validate_email(array $input, string $key, array &$errors): void
|
|
44
|
+
{
|
|
45
|
+
if (isset($errors[$key])) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
$value = input_string($input, $key);
|
|
49
|
+
if ($value === '') {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (filter_var($value, FILTER_VALIDATE_EMAIL) === false) {
|
|
53
|
+
$errors[$key] = 'A valid email is required.';
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function validate_max_length(array $input, string $key, int $max, array &$errors): void
|
|
58
|
+
{
|
|
59
|
+
if (isset($errors[$key])) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
$value = input_string($input, $key);
|
|
63
|
+
if ($value === '') {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (strlen($value) > $max) {
|
|
67
|
+
$errors[$key] = 'This field is too long (maximum ' . $max . ' characters).';
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Optional PHP config. The document root is the folder that contains api/, so this file is inside it.
|
|
5
|
+
* load_config() reads .env in the project root and .php/.env. Values here override those defaults.
|
|
6
|
+
* Hidden paths: shipped .htaccess on Apache, the nginx snippet in api/README.md, Caddy 404 locally.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
declare(strict_types=1);
|
|
10
|
+
|
|
11
|
+
if (!defined('STAGEPASS_PHP_ENTRY')) {
|
|
12
|
+
http_response_code(404);
|
|
13
|
+
exit;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return [
|
|
17
|
+
'env' => env_string('APP_ENV', 'production'),
|
|
18
|
+
'allowed_origins' => env_list('ALLOWED_ORIGINS'),
|
|
19
|
+
'honeypot_field' => env_string('HONEYPOT_FIELD', 'website'),
|
|
20
|
+
'rate_limit_max' => env_int('RATE_LIMIT_MAX', 20),
|
|
21
|
+
'rate_limit_window' => env_int('RATE_LIMIT_WINDOW', 60),
|
|
22
|
+
'max_body_bytes' => env_int('MAX_BODY_BYTES', 65536),
|
|
23
|
+
'timezone' => env_string('APP_TIMEZONE', 'Europe/Berlin'),
|
|
24
|
+
];
|