$message]); exit; } if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') { header('Allow: POST'); reply(405, 'POST required'); } if (strtolower(trim(explode(';', $_SERVER['CONTENT_TYPE'] ?? '')[0])) !== 'application/json') { reply(415, 'application/json required'); } $device = getenv('PUSH2HOOK_DEVICE_ID') ?: ''; $secret = getenv('PUSH2HOOK_SECRET') ?: ''; $inbox = getenv('PUSH2HOOK_INBOX') ?: ''; if ($device === '' || strlen($secret) < 32 || $inbox === '' || $inbox[0] !== '/' || !is_dir($inbox)) { reply(503, 'Receiver not configured'); } $deviceHeader = $_SERVER['HTTP_X_DEVICE_ID'] ?? ''; $timestamp = $_SERVER['HTTP_X_TIMESTAMP'] ?? ''; $signature = $_SERVER['HTTP_X_SIGNATURE'] ?? ''; $now = time(); if (!hash_equals($device, $deviceHeader) || !preg_match('/\A[0-9]{1,12}\z/', $timestamp) || abs($now - (int)$timestamp) > 300 || strlen($signature) > 128) { reply(401, 'Authentication failed'); } const MAX_BODY = 1048576; if ((int)($_SERVER['CONTENT_LENGTH'] ?? 0) > MAX_BODY) reply(413, 'Body too large'); $raw = file_get_contents('php://input', false, null, 0, MAX_BODY + 1); if ($raw === false || strlen($raw) > MAX_BODY) reply(413, 'Body too large'); // Sign the ORIGINAL bytes: never decode/re-encode JSON before checking HMAC. // Base64(HMAC-SHA256(UTF8(secret), UTF8(timestamp + ".") + rawBody)). $supplied = base64_decode($signature, true); $expected = hash_hmac('sha256', $timestamp . '.' . $raw, $secret, true); if ($supplied === false || strlen($supplied) !== 32 || !hash_equals($expected, $supplied)) { reply(401, 'Authentication failed'); } try { $data = json_decode($raw, true, 64, JSON_THROW_ON_ERROR); } catch (JsonException $error) { reply(400, 'Invalid JSON'); } if (!is_array($data) || ($data['schemaVersion'] ?? null) !== 1 || ($data['deviceId'] ?? null) !== $device || ($data['demo'] ?? null) !== false || !is_string($data['eventId'] ?? null) || !preg_match('/\A[A-Za-z0-9._:-]{1,200}\z/', $data['eventId']) || !is_array($data['source'] ?? null) || !is_string($data['source']['packageName'] ?? null) || $data['source']['packageName'] === '' || strlen($data['source']['packageName']) > 255 || !is_array($data['notification'] ?? null) || array_is_list($data['notification'])) { reply(400, 'Invalid notification or device mismatch'); } // A persistent inbox, keyed by device + eventId. Retries are acknowledged without // storing a second copy. Process these files asynchronously in your automation. $path = $inbox . '/' . hash('sha256', $device . "\0" . $data['eventId']) . '.json'; $temporary = null; $lock = null; $output = null; $duplicate = false; $oldMask = umask(0077); try { $lock = @fopen($inbox . '/.lock', 'c'); if ($lock === false || !flock($lock, LOCK_EX)) throw new RuntimeException(); $duplicate = is_file($path); if (!$duplicate) { $temporary = tempnam($inbox, '.pending-'); if ($temporary === false) throw new RuntimeException(); $output = fopen($temporary, 'wb'); if ($output === false) throw new RuntimeException(); $offset = 0; while ($offset < strlen($raw)) { $written = fwrite($output, substr($raw, $offset)); if ($written === false || $written === 0) throw new RuntimeException(); $offset += $written; } if (!fflush($output) || !fsync($output)) throw new RuntimeException(); fclose($output); $output = null; if (!rename($temporary, $path)) throw new RuntimeException(); $temporary = null; } } catch (Throwable $error) { // A 5xx response asks push2hook to retry; never acknowledge failed storage. $failed = true; } finally { if (is_resource($output)) fclose($output); if (is_string($temporary) && is_file($temporary)) unlink($temporary); if (is_resource($lock)) { flock($lock, LOCK_UN); fclose($lock); } umask($oldMask); } if (isset($failed)) reply(503, 'Storage unavailable'); reply($duplicate ? 204 : 202);