$url, 'token' => $token, 'priority' => max(0, min(10, (int) ($row['priority'] ?? admin_cfg('gotify_priority', 5)))), 'updated_at' => (string) ($row['updated_at'] ?? ''), ]; } function admin_gotify_user_save(string $screenName, string $url, string $token, ?int $priority = null): array { if (!function_exists('admin_normalize_screen_name')) { return ['ok' => false, 'error' => 'Missing admin helpers.']; } $key = admin_normalize_screen_name($screenName); if ($key === '') return ['ok' => false, 'error' => 'Invalid screen name.']; $url = rtrim(trim($url), '/'); $token = trim($token); $priority = $priority ?? (int) admin_cfg('gotify_priority', 5); $priority = max(0, min(10, $priority)); $all = admin_gotify_users_load(); if ($url === '' || $token === '') { unset($all[$key]); $saved = admin_gotify_users_save_all($all); return $saved ? ['ok' => true, 'cleared' => true] : ['ok' => false, 'error' => 'Failed to save Gotify settings.']; } // Basic validation if (!preg_match('#^https?://#i', $url)) { return ['ok' => false, 'error' => 'Gotify URL must start with http:// or https://']; } $all[$key] = [ 'screen_name' => $screenName, 'url' => $url, 'token' => $token, 'priority' => $priority, 'updated_at' => date('c'), ]; $saved = admin_gotify_users_save_all($all); return $saved ? ['ok' => true, 'cleared' => false] : ['ok' => false, 'error' => 'Failed to save Gotify settings.']; } function admin_gotify_resolve(string $screenName): ?array { $user = admin_gotify_user_get($screenName); if ($user) return $user; // Optional global fallback $url = rtrim(trim((string) admin_cfg('gotify_url', '')), '/'); $token = trim((string) admin_cfg('gotify_token', '')); if ($url !== '' && $token !== '') { return [ 'url' => $url, 'token' => $token, 'priority' => max(0, min(10, (int) admin_cfg('gotify_priority', 5))), 'updated_at' => '', 'fallback' => true, ]; } return null; } function admin_gotify_enabled(): bool { // Back-compat alias: feature toggle (not "is configured for a specific user"). return admin_gotify_feature_enabled(); } function admin_gotify_send(string $title, string $message, ?int $priority = null, ?string $url = null, ?string $token = null): array { if (!admin_gotify_feature_enabled()) { return ['ok' => false, 'status' => 0, 'error' => 'Gotify is not enabled', 'data' => null, 'raw' => '']; } $url = rtrim(trim((string) ($url ?? admin_cfg('gotify_url', ''))), '/'); $token = trim((string) ($token ?? admin_cfg('gotify_token', ''))); if ($url === '' || $token === '') { return ['ok' => false, 'status' => 0, 'error' => 'Gotify is not configured', 'data' => null, 'raw' => '']; } $priority = $priority ?? (int) admin_cfg('gotify_priority', 5); $priority = max(0, min(10, $priority)); $endpoint = $url . '/message?token=' . rawurlencode($token); $payload = json_encode([ 'title' => $title, 'message' => $message, 'priority' => $priority, ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $ch = curl_init($endpoint); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_HTTPHEADER => ['Content-Type: application/json; charset=utf-8', 'Accept: application/json'], CURLOPT_POSTFIELDS => $payload, CURLOPT_TIMEOUT => 10, CURLOPT_CONNECTTIMEOUT => 5, ]); $raw = curl_exec($ch); $status = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE); $err = curl_error($ch); curl_close($ch); if ($raw === false) { return ['ok' => false, 'status' => 0, 'error' => $err ?: 'Gotify request failed', 'data' => null, 'raw' => '']; } $decoded = json_decode($raw, true); $ok = $status >= 200 && $status < 300; return [ 'ok' => $ok, 'status' => $status, 'error' => $ok ? null : (is_array($decoded) && isset($decoded['errorDescription']) ? (string) $decoded['errorDescription'] : ($raw !== '' ? $raw : 'HTTP ' . $status)), 'data' => $decoded, 'raw' => $raw, ]; } function admin_gotify_send_for_user(string $screenName, string $title, string $message, ?int $priority = null): array { $cfg = admin_gotify_resolve($screenName); if (!$cfg) { return ['ok' => false, 'status' => 0, 'error' => 'No Gotify configuration for this user', 'data' => null, 'raw' => '']; } return admin_gotify_send($title, $message, $priority ?? $cfg['priority'], $cfg['url'], $cfg['token']); }