Files
fitme3070.pt/config/functions_notify_client.php
T
andreandClaude Opus 4.8 e68d357177 Initial import of fitme3070.pt platform
Plain-PHP fleet platform with id.andmore.pt SSO. Wires shared/platform-tools
as a submodule (platform_update, backups, git/migration tools) like the other
platforms.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-12 10:44:58 +01:00

51 lines
2.1 KiB
PHP

<?php
// Client for the centralized notification service at id.andmore.pt (/notify).
//
// fotografia no longer sends mail/SMS itself — delivery is centralized. This is
// the single egress point: it POSTs a message to {ANDMORE_ID_URL}notify/send.php
// authenticated with the per-app key (NOTIFY_API_KEY, minted on id.andmore.pt →
// Notificações → Chaves de app). The legacy enqueue_email()/flush_message() pair
// in config/functions_queue.php is now a thin façade over this.
//
// The app key is sent in the JSON body (not a header) to dodge Apache stripping
// the Authorization header on some setups.
function notify_remote_send(array $payload): array {
$base = defined('ANDMORE_ID_URL') ? ANDMORE_ID_URL : rtrim((string) env('ANDMORE_ID_URL', ''), '/') . '/';
$key = (string) env('NOTIFY_API_KEY', '');
if ($base === '' || $key === '') {
error_log('notify_remote_send: ANDMORE_ID_URL or NOTIFY_API_KEY not configured');
return ['ok' => false, 'error' => 'notify_not_configured'];
}
$payload['app_key'] = $key;
$url = rtrim($base, '/') . '/notify/send.php';
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 20,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
]);
$resp = curl_exec($ch);
$code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
$err = curl_error($ch);
curl_close($ch);
if ($resp === false) {
error_log("notify_remote_send: transport error ($err) calling $url");
return ['ok' => false, 'error' => 'transport_error'];
}
$data = json_decode($resp, true);
if (!is_array($data)) {
error_log("notify_remote_send: bad response (HTTP $code) from $url: " . substr((string) $resp, 0, 300));
return ['ok' => false, 'error' => 'bad_response', 'http' => $code];
}
if (empty($data['ok'])) {
error_log("notify_remote_send: notify error (HTTP $code): " . ($data['error'] ?? '?'));
}
return $data;
}