$message])); } if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') { fail(405, 'Method not allowed.'); } if (!$api_key || !$upstream || !$form_id || !$origins) { fail(503, 'The form proxy is not configured.'); } if (!in_array($_SERVER['HTTP_ORIGIN'] ?? '', $origins, true)) { fail(403, 'This origin is not allowed.'); } $user_agent = str_replace(["\r", "\n"], '', $_SERVER['HTTP_USER_AGENT'] ?? ''); if (!preg_match('/mozilla|safari|chrome|firefox/i', $user_agent)) { fail(403, 'Submission blocked.'); } if ((int) ($_SERVER['CONTENT_LENGTH'] ?? 0) > MAX_BYTES) { fail(413, 'Submission too large.'); } if (str_contains($_SERVER['CONTENT_TYPE'] ?? '', 'application/json')) { $raw = file_get_contents('php://input', false, null, 0, MAX_BYTES + 1); if (strlen($raw) > MAX_BYTES) { fail(413, 'Submission too large.'); } $payload = json_decode($raw); } else { $payload = (object) $_POST; // a normal HTML form post } if (!is_object($payload)) { fail(400, 'Send the fields as a JSON object.'); } foreach (get_object_vars($payload) as $value) { $is_list = is_array($value) && array_values($value) === $value; foreach ($is_list ? $value : [$value] as $item) { if ($item !== null && !is_scalar($item)) { fail(400, 'Send text, numbers or lists only.'); } } } // Always submit to the configured form, whatever the browser sent. $payload->id = $form_id; $body = json_encode($payload); if ($body === false || strlen($body) > MAX_BYTES) { fail(413, 'Submission too large.'); } $request = curl_init(rtrim($upstream, '/') . '/forms/' . rawurlencode($form_id) . '/submit'); curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $body, CURLOPT_TIMEOUT => 15, CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'Accept: application/json', 'X-Core-Forms-Key: ' . $api_key, 'User-Agent: ' . $user_agent, ], ]); $result = curl_exec($request); $status = curl_getinfo($request, CURLINFO_HTTP_CODE); $type = (string) curl_getinfo($request, CURLINFO_CONTENT_TYPE); if ($result === false || !$status) { fail(502, 'The form server could not be reached.'); } if (!str_contains($type, 'application/json')) { fail(502, 'The form server returned an unexpected response.'); } reply($status, $result);