@stagepass/cli 2.3.1 → 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 +24 -13
- 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 +84 -32
- 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 +30 -29
- 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 +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,100 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Single public entry for Stagepass PHP actions.
|
|
5
|
+
* Core file: leave this alone unless you are changing how routing works.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* GET /api/action.php?action=ping
|
|
9
|
+
* POST /api/action.php with JSON { "action": "contact", "name": "...", ... }
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
declare(strict_types=1);
|
|
13
|
+
|
|
14
|
+
define('STAGEPASS_PHP_ENTRY', true);
|
|
15
|
+
|
|
16
|
+
// Core library, then the allowlist you edit in actions.php.
|
|
17
|
+
require __DIR__ . '/lib/bootstrap.php';
|
|
18
|
+
require __DIR__ . '/actions.php';
|
|
19
|
+
|
|
20
|
+
install_error_handlers();
|
|
21
|
+
|
|
22
|
+
ini_set('display_errors', '0');
|
|
23
|
+
ini_set('display_startup_errors', '0');
|
|
24
|
+
|
|
25
|
+
$config = load_config();
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
date_default_timezone_set((string) $config['timezone']);
|
|
29
|
+
} catch (Throwable $e) {
|
|
30
|
+
date_default_timezone_set('UTC');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
$origin = request_origin();
|
|
34
|
+
$allowedOrigins = $config['allowed_origins'];
|
|
35
|
+
$method = request_method();
|
|
36
|
+
|
|
37
|
+
apply_cors_headers($origin, $allowedOrigins);
|
|
38
|
+
|
|
39
|
+
if ($method === 'OPTIONS') {
|
|
40
|
+
if ($origin === '' || !origin_is_allowed($origin, $allowedOrigins)) {
|
|
41
|
+
respond_fail(['origin' => 'Origin is not allowed.'], 'Origin is not allowed.', 403);
|
|
42
|
+
}
|
|
43
|
+
http_response_code(204);
|
|
44
|
+
header('Content-Length: 0');
|
|
45
|
+
exit;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
$input = parse_input((int) $config['max_body_bytes']);
|
|
49
|
+
|
|
50
|
+
$action = input_string($input, 'action');
|
|
51
|
+
$catalog = action_catalog($config);
|
|
52
|
+
|
|
53
|
+
if ($action === '' || !isset($catalog[$action])) {
|
|
54
|
+
respond_fail(['action' => 'Unknown action.'], 'Unknown action.', 404);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
$spec = $catalog[$action];
|
|
58
|
+
$allowedMethods = $spec['methods'];
|
|
59
|
+
|
|
60
|
+
if (!in_array($method, $allowedMethods, true)) {
|
|
61
|
+
if (!headers_sent()) {
|
|
62
|
+
header('Allow: ' . implode(', ', $allowedMethods));
|
|
63
|
+
}
|
|
64
|
+
respond_fail(
|
|
65
|
+
['method' => 'Use ' . implode(' or ', $allowedMethods) . '.'],
|
|
66
|
+
'Method not allowed.',
|
|
67
|
+
405
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
$originRequired = $spec['origin'] === 'required';
|
|
72
|
+
if ($originRequired && $origin === '') {
|
|
73
|
+
respond_fail(['origin' => 'Origin header is required.'], 'Origin is not allowed.', 403);
|
|
74
|
+
}
|
|
75
|
+
if ($origin !== '' && !origin_is_allowed($origin, $allowedOrigins)) {
|
|
76
|
+
respond_fail(['origin' => 'Origin is not allowed.'], 'Origin is not allowed.', 403);
|
|
77
|
+
}
|
|
78
|
+
if ($allowedOrigins === [] && $originRequired) {
|
|
79
|
+
respond_fail(['origin' => 'No origins are configured.'], 'Origin is not allowed.', 403);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if ($spec['rate_limit'] && !rate_limit_allows(client_ip(), $action, (int) $config['rate_limit_max'], (int) $config['rate_limit_window'])) {
|
|
83
|
+
if (!headers_sent()) {
|
|
84
|
+
header('Retry-After: ' . (string) $config['rate_limit_window']);
|
|
85
|
+
}
|
|
86
|
+
respond_fail(['rate_limit' => 'Too many requests. Try again later.'], 'Too many requests.', 429);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if ($spec['honeypot']) {
|
|
90
|
+
$honeypot = (string) $config['honeypot_field'];
|
|
91
|
+
if (honeypot_is_filled($input, $honeypot)) {
|
|
92
|
+
// Look like a normal success so bots do not learn the field name.
|
|
93
|
+
respond_success(['received' => true]);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
$handler = load_handler($spec['file']);
|
|
98
|
+
$data = $handler($input, $config);
|
|
99
|
+
|
|
100
|
+
respond_success(is_array($data) ? $data : ['ok' => true]);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Local-only hook to confirm unexpected exceptions become a generic HTTP 500.
|
|
5
|
+
* It is not registered unless APP_ENV is "local". Leave this as a test hook.
|
|
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
|
+
throw new RuntimeException('SECRET_INTERNAL_TRACE should never reach the client.');
|
|
21
|
+
};
|
|
@@ -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
|
+
}
|