@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
|
@@ -1,7 +1,208 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Stagepass PHP boilerplate
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Plain PHP 8.2 or newer. No framework. No Composer. No mail.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
`api/action.php` is the only public entry. Every request names an `action`. That name is looked up in `api/actions.php` and then handed to a file in `api/actions/`. User input is never used as a file path or a function name.
|
|
6
|
+
|
|
7
|
+
A longer guide is at [https://stagepass.one/docs/php/](https://stagepass.one/docs/php/). Copy `api/.env.example` to `.env` in the project root, or use `.php/.env`. `stagepass build` never copies `.env` into `dist`.
|
|
8
|
+
|
|
9
|
+
## Where to edit
|
|
10
|
+
|
|
11
|
+
Edit these:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
api/actions.php Allowlist: register a new action here
|
|
15
|
+
api/actions/*.php Your action logic (see contact.php)
|
|
16
|
+
.env Origins, limits, APP_ENV (copy from api/.env.example)
|
|
17
|
+
.php/config.php Optional PHP overrides of the .env defaults
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Leave these alone unless you are changing the core:
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
api/action.php Front controller
|
|
24
|
+
api/lib/* Responses, request parsing, security, validation, config
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Layout
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
api/action.php Public front controller (core)
|
|
31
|
+
api/actions.php Action allowlist (you edit this)
|
|
32
|
+
api/actions/ping.php GET health check
|
|
33
|
+
api/actions/contact.php POST form handler (validates only, does not send mail)
|
|
34
|
+
api/lib/ Shared helpers (core)
|
|
35
|
+
api/.env.example Copy to the project root as .env
|
|
36
|
+
.php/config.php Optional overrides, inside the document root (the folder that contains api/)
|
|
37
|
+
.php/rate-limit/ Runtime files, not copied to dist. Deny hidden paths on the host
|
|
38
|
+
.htaccess Apache deny for hidden paths (shipped)
|
|
39
|
+
.env Local environment (do not commit)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Configure
|
|
43
|
+
|
|
44
|
+
1. Copy `api/.env.example` to `.env` in the project root (the folder that contains `api/`).
|
|
45
|
+
2. Set `ALLOWED_ORIGINS` to the exact site origins that may call this API, including the scheme and without a trailing slash. Typical values are `https://your-project.sp`, `https://your-site.webflow.io`, and `https://www.example.com`.
|
|
46
|
+
3. Keep `.env` out of git. The document root is the folder that contains `api/` (the project root locally, `dist/` after `stagepass build`). Caddy returns 404 for any path segment that starts with `.`. Since 2.4.1 this starter ships a web-root `.htaccess` (Apache needs `AllowOverride All` and `mod_rewrite`). `stagepass upgrade` writes that file when `stagepass.json` has `"php": true` and the project root has none. An existing `.htaccess` is left unchanged. If it has no `RewriteRule "(^|/)\." - [F]` line, add that rule yourself. Published CLI 2.4.0 does not write the file. `stagepass build` copies the project `.htaccess` into `dist/` and does not replace an existing project `.htaccess`. It does not copy `.env`, `.php/.env`, or `.php/rate-limit/`. nginx does not read `.htaccess`. Put this in the server block before `location ~ \.php$`:
|
|
47
|
+
|
|
48
|
+
```nginx
|
|
49
|
+
location ~* /\.(?!well-known\/) {
|
|
50
|
+
deny all;
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Prefer real environment variables on the host so a `.env` file is not required in the web root.
|
|
55
|
+
4. `stagepass build` copies `api/` to `dist/api/`, `.php/config.php` to `dist/.php/config.php`, and the project `.htaccess` to `dist/.htaccess`. Upload `dist/`. The public URL stays `/api/action.php`.
|
|
56
|
+
|
|
57
|
+
`APP_ENV=local` turns on a hidden `boom` action used only to test error handling. Leave `APP_ENV=production` on a live host.
|
|
58
|
+
|
|
59
|
+
## Add a new action
|
|
60
|
+
|
|
61
|
+
1. Create `api/actions/newsletter.php`. The file must return a callable. Read fields with `input_string()`. Throw `ValidationException` when input is invalid (that is a 422 fail). Any other uncaught exception becomes a generic 500 error.
|
|
62
|
+
|
|
63
|
+
```php
|
|
64
|
+
<?php
|
|
65
|
+
declare(strict_types=1);
|
|
66
|
+
|
|
67
|
+
if (!defined('STAGEPASS_PHP_ENTRY')) {
|
|
68
|
+
http_response_code(404);
|
|
69
|
+
exit;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return static function (array $input, array $config): array {
|
|
73
|
+
$errors = [];
|
|
74
|
+
validate_required($input, 'email', $errors);
|
|
75
|
+
validate_email($input, 'email', $errors);
|
|
76
|
+
if ($errors !== []) {
|
|
77
|
+
throw new ValidationException($errors);
|
|
78
|
+
}
|
|
79
|
+
// Your logic here.
|
|
80
|
+
return ['subscribed' => true];
|
|
81
|
+
};
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
2. Register the name in `api/actions.php`:
|
|
85
|
+
|
|
86
|
+
```php
|
|
87
|
+
'newsletter' => [
|
|
88
|
+
'file' => 'newsletter.php',
|
|
89
|
+
'methods' => ['POST'],
|
|
90
|
+
'honeypot' => true,
|
|
91
|
+
'rate_limit' => true,
|
|
92
|
+
'origin' => 'required',
|
|
93
|
+
],
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
3. Call it from JavaScript with `action: 'newsletter'`. Do not add a second public PHP file unless you have a strong reason.
|
|
97
|
+
|
|
98
|
+
## Call it from JavaScript
|
|
99
|
+
|
|
100
|
+
`sp.resolveLocalUrl` returns a local HTTPS URL only during an active `.sp` session, otherwise `''`. On a Webflow page `window.location.origin` is the Webflow host, and Webflow does not run PHP. There is no helper for the public PHP base URL (`origin` and `production` in `stagepass.json` are Webflow hosts). Use the absolute URL of the PHP host, for example `https://php.example.com/api/action.php`.
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
const path = '/api/action.php';
|
|
104
|
+
const phpHost = 'https://php.example.com';
|
|
105
|
+
const local = sp.vars.isLocal && sp.resolveLocalUrl(path);
|
|
106
|
+
const url = local || phpHost + path;
|
|
107
|
+
|
|
108
|
+
const response = await fetch(url, {
|
|
109
|
+
method: 'POST',
|
|
110
|
+
headers: {
|
|
111
|
+
Accept: 'application/json',
|
|
112
|
+
'Content-Type': 'application/json',
|
|
113
|
+
},
|
|
114
|
+
body: JSON.stringify({
|
|
115
|
+
action: 'contact',
|
|
116
|
+
name: 'Ada Lovelace',
|
|
117
|
+
email: 'ada@example.com',
|
|
118
|
+
message: 'Hello',
|
|
119
|
+
website: '', // honeypot; leave empty, hide this field in CSS
|
|
120
|
+
}),
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const result = await response.json();
|
|
124
|
+
// result.status is "success", "fail", or "error"
|
|
125
|
+
if (result.status === 'success') {
|
|
126
|
+
console.log('Saved', result.data);
|
|
127
|
+
} else {
|
|
128
|
+
console.warn(result.message, result.data);
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
A simple GET check:
|
|
133
|
+
|
|
134
|
+
```js
|
|
135
|
+
const url =
|
|
136
|
+
(sp.vars.isLocal && sp.resolveLocalUrl('/api/action.php')) ||
|
|
137
|
+
'https://php.example.com/api/action.php';
|
|
138
|
+
|
|
139
|
+
const result = await fetch(url + '?action=ping', {
|
|
140
|
+
headers: { Accept: 'application/json' },
|
|
141
|
+
}).then((r) => r.json());
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
`contact.php` validates a JSON or form POST and does not send mail. A native Webflow form needs its own submit listener. There is no shipped helper for that.
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
form.addEventListener('submit', async (event) => {
|
|
148
|
+
event.preventDefault();
|
|
149
|
+
const url =
|
|
150
|
+
(sp.vars.isLocal && sp.resolveLocalUrl('/api/action.php')) ||
|
|
151
|
+
'https://php.example.com/api/action.php';
|
|
152
|
+
await fetch(url, {
|
|
153
|
+
method: 'POST',
|
|
154
|
+
headers: { Accept: 'application/json', 'Content-Type': 'application/json' },
|
|
155
|
+
body: JSON.stringify({
|
|
156
|
+
action: 'contact',
|
|
157
|
+
name: form.elements.name.value,
|
|
158
|
+
email: form.elements.email.value,
|
|
159
|
+
message: form.elements.message.value,
|
|
160
|
+
website: '',
|
|
161
|
+
}),
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Response shape
|
|
167
|
+
|
|
168
|
+
Every response is JSON with `status`, `data`, and `message`, plus a matching HTTP status code.
|
|
169
|
+
|
|
170
|
+
```json
|
|
171
|
+
{ "status": "success", "data": { "pong": true }, "message": null }
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
```json
|
|
175
|
+
{
|
|
176
|
+
"status": "fail",
|
|
177
|
+
"data": { "email": "A valid email is required." },
|
|
178
|
+
"message": "Validation failed."
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
```json
|
|
183
|
+
{
|
|
184
|
+
"status": "error",
|
|
185
|
+
"data": null,
|
|
186
|
+
"message": "An unexpected error occurred. Try again later."
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
`fail` is a rejected request (validation 422, unknown action 404, wrong method 405, disallowed origin 403, too many requests 429). `error` is an unexpected server failure (500) and never includes internal details.
|
|
191
|
+
|
|
192
|
+
## Security defaults
|
|
193
|
+
|
|
194
|
+
POST actions require an `Origin` header that is on `ALLOWED_ORIGINS`. GET `ping` may omit `Origin`, but a present `Origin` still has to match the list. CORS headers are only sent for allowed origins. A filled honeypot field (`website` by default) returns a generic success and does not run the handler. Rate limiting is per IP and action, stored as files under `.php/rate-limit/`. `rate_limit_allows()` is fail-open: if the directory cannot be created, the file cannot be opened, `flock` fails, or the limiter throws, the request is allowed. Directory and file failures, and thrown errors, are logged. A failed `flock` allows the request and does not write its own log line. `client_ip()` uses only `REMOTE_ADDR`, never `X-Forwarded-For`. Do not trust that header without a proxy allowlist. Behind a reverse proxy or CDN every visitor can share one address, so the limit applies to everyone together. This stays fail-open.
|
|
195
|
+
|
|
196
|
+
## Local PHP server
|
|
197
|
+
|
|
198
|
+
From the project root (the folder that contains `api/`):
|
|
199
|
+
|
|
200
|
+
```bash
|
|
201
|
+
php -S 127.0.0.1:8080 -t .
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Then: `curl 'http://127.0.0.1:8080/api/action.php?action=ping'`
|
|
205
|
+
|
|
206
|
+
`php -S 127.0.0.1:8080 -t api` serves `/action.php` and does not match this layout. PHP's built-in server does not return 404 for hidden paths the way Caddy does (`/.env`, `/.php/`). Do not point `php -S -t .` at a folder that already contains a real `.env`. Prefer `stagepass dev` or the linked `.sp` domain.
|
|
207
|
+
|
|
208
|
+
On a linked Stagepass domain the same file is reached at `https://your-project.sp/api/action.php`.
|
|
@@ -0,0 +1,103 @@
|
|
|
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
|
+
if (!headers_sent()) {
|
|
44
|
+
header_remove('X-Powered-By');
|
|
45
|
+
}
|
|
46
|
+
http_response_code(204);
|
|
47
|
+
header('Content-Length: 0');
|
|
48
|
+
exit;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
$input = parse_input((int) $config['max_body_bytes']);
|
|
52
|
+
|
|
53
|
+
$action = input_string($input, 'action');
|
|
54
|
+
$catalog = action_catalog($config);
|
|
55
|
+
|
|
56
|
+
if ($action === '' || !isset($catalog[$action])) {
|
|
57
|
+
respond_fail(['action' => 'Unknown action.'], 'Unknown action.', 404);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
$spec = $catalog[$action];
|
|
61
|
+
$allowedMethods = $spec['methods'];
|
|
62
|
+
|
|
63
|
+
if (!in_array($method, $allowedMethods, true)) {
|
|
64
|
+
if (!headers_sent()) {
|
|
65
|
+
header('Allow: ' . implode(', ', $allowedMethods));
|
|
66
|
+
}
|
|
67
|
+
respond_fail(
|
|
68
|
+
['method' => 'Use ' . implode(' or ', $allowedMethods) . '.'],
|
|
69
|
+
'Method not allowed.',
|
|
70
|
+
405
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
$originRequired = $spec['origin'] === 'required';
|
|
75
|
+
if ($originRequired && $origin === '') {
|
|
76
|
+
respond_fail(['origin' => 'Origin header is required.'], 'Origin is not allowed.', 403);
|
|
77
|
+
}
|
|
78
|
+
if ($origin !== '' && !origin_is_allowed($origin, $allowedOrigins)) {
|
|
79
|
+
respond_fail(['origin' => 'Origin is not allowed.'], 'Origin is not allowed.', 403);
|
|
80
|
+
}
|
|
81
|
+
if ($allowedOrigins === [] && $originRequired) {
|
|
82
|
+
respond_fail(['origin' => 'No origins are configured.'], 'Origin is not allowed.', 403);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if ($spec['rate_limit'] && !rate_limit_allows(client_ip(), $action, (int) $config['rate_limit_max'], (int) $config['rate_limit_window'])) {
|
|
86
|
+
if (!headers_sent()) {
|
|
87
|
+
header('Retry-After: ' . (string) $config['rate_limit_window']);
|
|
88
|
+
}
|
|
89
|
+
respond_fail(['rate_limit' => 'Too many requests. Try again later.'], 'Too many requests.', 429);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if ($spec['honeypot']) {
|
|
93
|
+
$honeypot = (string) $config['honeypot_field'];
|
|
94
|
+
if (honeypot_is_filled($input, $honeypot)) {
|
|
95
|
+
// Look like a normal success so bots do not learn the field name.
|
|
96
|
+
respond_success(['received' => true]);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
$handler = load_handler($spec['file']);
|
|
101
|
+
$data = $handler($input, $config);
|
|
102
|
+
|
|
103
|
+
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
|
+
}
|