From 1b7a84de4089b60bee73d3dfa2bf8270cac0d0c3 Mon Sep 17 00:00:00 2001 From: johnnyq Date: Wed, 25 Nov 2026 01:00:50 -0500 Subject: [PATCH] Show which categories a managed instance's errors were logged under: its check-in sends the count per category, never the error text, which can hold client addresses and subjects; the manager names them in the health line and lists them under Errors today, where the count and each category sign in as support straight to the instance's App Logs filtered to those errors, a page an older instance ignores so its count stays plain; Managed By's list of what it tells its manager says so --- admin/managed_by.php | 2 +- admin/managed_instance.php | 22 +++++++++++- admin/post/managed_instances.php | 11 ++++-- functions/managed.php | 59 ++++++++++++++++++++++++++++---- login_support.php | 4 +++ 5 files changed, 88 insertions(+), 10 deletions(-) diff --git a/admin/managed_by.php b/admin/managed_by.php index 16062c999..4f5e1856b 100644 --- a/admin/managed_by.php +++ b/admin/managed_by.php @@ -195,7 +195,7 @@ $lock = 'No tickets, documents, credentials, invoices or contacts.

diff --git a/admin/managed_instance.php b/admin/managed_instance.php index 0376d4b1c..2dae68b13 100644 --- a/admin/managed_instance.php +++ b/admin/managed_instance.php @@ -95,6 +95,26 @@ $size = function ($mb) { return $mb >= 1024 ? round($mb / 1024, 1) . ' GB' : round($mb) . ' MB'; }; +// Errors today, and the categories they were logged under - each opening its error log on the +// instance, signed in as support. Only once it sends the categories: an older instance would +// land on its start page +$error_count = intval($report['errors_24h'] ?? 0); +$error_log_link = $can_act && array_key_exists('errors_by_category', $report); +$error_link = function (string $text_html, string $category = '') use ($instance_id, $csrf, $error_log_link) { + if (!$error_log_link) { + return $text_html; + } + $href = "post.php?managed_support_sign_in=$instance_id&to=errors" . ($category !== '' ? '&category=' . rawurlencode($category) : '') . "&csrf_token=$csrf"; + return '' . $text_html . ''; +}; +$errors_html = '0'; +if ($error_count > 0) { + $errors_html = '' . $error_link((string) $error_count) . ''; + foreach (managedInstanceErrorCategories($report) as $category => $logged) { + $errors_html .= '
' . $error_link(escapeHtml($category !== '' ? $category : 'No category'), $category) . ' · ' . intval($logged) . '
'; + } +} + ?> @@ -375,7 +395,7 @@ $size = function ($mb) { Switched off' : 'Last ran ' . $when($cron['last_dispatch_at'] ?? null)) ?> - 0 ? '' . intval($report['errors_24h']) . '' : '0') ?> + ' . $size($disk['db_mb'] ?? 0) . ' database') ?> diff --git a/admin/post/managed_instances.php b/admin/post/managed_instances.php index 3482e7172..aa89c2cb8 100644 --- a/admin/post/managed_instances.php +++ b/admin/post/managed_instances.php @@ -184,14 +184,21 @@ if (isset($_GET['managed_support_sign_in'])) { $instance_id = intval($row['managed_instance_id']); $me = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT user_name, user_email FROM users WHERE user_id = " . intval($session_user_id))) ?: []; + // Straight to its error log, or one category of it - from Errors today on its page + $page = ''; + if (($_GET['to'] ?? '') === 'errors') { + $category = is_string($_GET['category'] ?? null) ? mb_substr(trim($_GET['category']), 0, 100) : ''; + $page = 'admin/app_logs.php?' . http_build_query(array_filter(['view' => 'error', 'category' => $category], 'strlen')); + } + try { - $link = managedSupportLink($row, (string) ($me['user_name'] ?? ''), (string) ($me['user_email'] ?? '')); + $link = managedSupportLink($row, (string) ($me['user_name'] ?? ''), (string) ($me['user_email'] ?? ''), $page); } catch (Throwable $e) { flashAlert(escapeHtml($e->getMessage()), 'error'); redirect("managed_instance.php?id=$instance_id"); } - logAudit("Managed Instance", "Support", "$session_name signed in to " . escapeSql($row['managed_instance_name']) . " as support", intval($row['managed_instance_client_id']), $instance_id); + logAudit("Managed Instance", "Support", "$session_name signed in to " . escapeSql($row['managed_instance_name']) . " as support" . ($page !== '' ? ' to look at its errors' : ''), intval($row['managed_instance_client_id']), $instance_id); redirect($link); diff --git a/functions/managed.php b/functions/managed.php index 7f59e7821..83a2e5686 100644 --- a/functions/managed.php +++ b/functions/managed.php @@ -473,6 +473,16 @@ function managedReport(): array WHERE backup_status IN ('Complete', 'Failed') ORDER BY backup_created_at DESC LIMIT 1")) ?: []; $backup_ok = mysqli_fetch_row(mysqli_query($mysqli, "SELECT MAX(backup_completed_at) FROM backups WHERE backup_status = 'Complete'"))[0] ?? null; $errors = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM app_logs WHERE app_log_type = 'error' AND app_log_created_at > NOW() - INTERVAL 1 DAY"))[0] ?? 0); + // The categories they were logged under, the most first - names only, never what they say, + // which can hold client details (a mail error has the address and subject) + $error_categories = []; + if ($errors) { + $result = mysqli_query($mysqli, "SELECT COALESCE(app_log_category, '') AS category, COUNT(*) AS logged FROM app_logs + WHERE app_log_type = 'error' AND app_log_created_at > NOW() - INTERVAL 1 DAY GROUP BY category ORDER BY logged DESC, category ASC LIMIT 10"); + while ($category = mysqli_fetch_assoc($result)) { + $error_categories[mb_substr((string) $category['category'], 0, 100)] = intval($category['logged']); + } + } $clients = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM clients WHERE client_archived_at IS NULL AND client_lead = 0"))[0] ?? 0); $company = (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT company_name FROM companies WHERE company_id = 1"))[0] ?? ''); @@ -523,6 +533,8 @@ function managedReport(): array 'last_ok_at' => managedIsoTime($backup_ok), ], 'errors_24h' => $errors, + // An object even when empty - {} rather than [] + 'errors_by_category' => (object) $error_categories, 'agents' => [ 'active' => getActiveAgentUserCount(), 'invites' => function_exists('userInvitesHoldingSeats') ? userInvitesHoldingSeats() : 0, @@ -697,9 +709,13 @@ function managedVerifySupportToken(string $token, &$error = ''): ?array $config['nonces'] = $nonces; managedConfigSave($config); + // A page to land on instead of the start page - one of this ITFlow's own, as a path + $page = (string) ($payload['p'] ?? ''); + return [ 'name' => mb_substr(trim((string) ($payload['n'] ?? '')), 0, 120), 'email' => mb_substr(trim((string) ($payload['e'] ?? '')), 0, 200), + 'page' => preg_match('#^(admin|agent)/[a-z0-9_]+\.php(\?[a-z0-9_=&%.+-]*)?$#i', $page) ? $page : '', ]; } @@ -968,6 +984,22 @@ function managedInstanceReport(array $row): array return is_array($report) ? $report : []; } +/* + * The categories an instance's errors today were logged under, the most first - [category => + * how many], '' for none. Empty from an instance too old to send them; it sends the count alone. + */ +function managedInstanceErrorCategories(array $report): array +{ + $categories = []; + foreach ((array) ($report['errors_by_category'] ?? []) as $category => $logged) { + if (intval($logged) > 0) { + $categories[mb_substr((string) $category, 0, 100)] = intval($logged); + } + } + arsort($categories); + return $categories; +} + /* * How an instance is doing, from its last report - state (pending: never connected; * waiting: connected, but its cron has never run - a new install whose cron job isn't in @@ -1022,8 +1054,17 @@ function managedInstanceHealth(array $row): array } elseif (strtotime($backup['last_ok_at']) < strtotime('-3 days')) { $issues[] = ['warning', 'No backup for ' . str_replace(' ago', '', timeAgo($backup['last_ok_at']))]; } - if (intval($report['errors_24h'] ?? 0) > 0) { - $issues[] = ['warning', intval($report['errors_24h']) . ' error' . (intval($report['errors_24h']) === 1 ? '' : 's') . ' logged today']; + $error_count = intval($report['errors_24h'] ?? 0); + if ($error_count > 0) { + // Under which categories, the most first - three named, then how many more + $error_categories = managedInstanceErrorCategories($report); + $named = []; + foreach (array_slice($error_categories, 0, 3, true) as $category => $logged) { + $named[] = ($category !== '' ? $category : 'No category') . " ($logged)"; + } + $more = count($error_categories) - count($named); + $issues[] = ['warning', "$error_count error" . ($error_count === 1 ? '' : 's') . ' logged today' + . ($named ? ' - ' . implode(', ', $named) . ($more > 0 ? " and $more more" : '') : '')]; } $agents = (array) ($report['agents'] ?? []); if (intval($agents['limit'] ?? 0) > 0 && intval($agents['active'] ?? 0) + intval($agents['invites'] ?? 0) >= intval($agents['limit'])) { @@ -1414,17 +1455,23 @@ function managerCheckin(string $authorization, array $body, string $ip): array /* * A link that signs the person asking into an instance as its support user - signed with this - * ITFlow's key, for that instance only, good for two minutes and once. + * ITFlow's key, for that instance only, good for two minutes and once. $page lands it on one of + * the instance's pages (admin/app_logs.php?view=error) instead of its start page - an instance + * too old to know it lands on its start page. */ -function managedSupportLink(array $row, string $name, string $email): string +function managedSupportLink(array $row, string $name, string $email, string $page = ''): string { - $token = managerSign([ + $payload = [ 'i' => $row['managed_instance_uuid'], 'n' => $name, 'e' => $email, 'x' => time() + MANAGED_SUPPORT_LINK_SECONDS, 'j' => bin2hex(random_bytes(16)), - ]); + ]; + if ($page !== '') { + $payload['p'] = $page; + } + $token = managerSign($payload); return rtrim((string) $row['managed_instance_url'], '/') . '/login_support.php?token=' . rawurlencode($token); } diff --git a/login_support.php b/login_support.php index 5b7d8807d..7b8406ad0 100644 --- a/login_support.php +++ b/login_support.php @@ -75,5 +75,9 @@ $session_user_id = $support_user_id; logAudit("Login", "Support", escapeSql("Support sign-in from $support_manager" . ($support_label !== '' ? " - $support_label" : '')), 0, $support_user_id); appNotify("Management", escapeSql(($support_label !== '' ? $support_label : 'Support') . " from $support_manager signed in for support"), "/admin/audit_logs.php", 0, $support_user_id); +// The page the manager sent them to (its error log, say), else the start page +if ($support_who['page'] !== '') { + redirect('/' . $support_who['page']); +} $support_start_page = (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT config_start_page FROM settings WHERE company_id = 1"))[0] ?? ''); redirect("/agent/" . (preg_match('/^[a-z0-9_\/.?=&-]+$/i', $support_start_page) ? $support_start_page : 'clients.php'));