From 61488305cb944f62f5076f2af20b314f2b450234 Mon Sep 17 00:00:00 2001 From: johnnyq Date: Thu, 19 Nov 2026 21:05:51 -0500 Subject: [PATCH] Admin: invite several agents at once with one role, try an email against the support mailbox to see what it would become without sending anything, and a weekly summary email to the owners with the week beside the week before and what needs a look (db 4.0.7) --- CHANGELOG.md | 8 +- admin/database_updates/4.0.7.php | 37 +++ admin/modals/ticket_mail/ticket_mail_try.php | 97 +++++++ admin/modals/user/user_invite_bulk.php | 79 ++++++ admin/post/settings_notification.php | 26 +- admin/post/users.php | 66 +++++ admin/settings_notification.php | 27 ++ admin/settings_ticket.php | 5 + admin/users.php | 6 + cron/nightly_tasks.php | 6 + db.sql | 4 + functions.php | 2 + functions/email_preview.php | 4 + functions/settings.php | 3 + functions/ticket_mail_try.php | 187 ++++++++++++++ functions/user_invite.php | 48 ++++ functions/weekly_summary.php | 255 +++++++++++++++++++ 17 files changed, 858 insertions(+), 2 deletions(-) create mode 100644 admin/database_updates/4.0.7.php create mode 100644 admin/modals/ticket_mail/ticket_mail_try.php create mode 100644 admin/modals/user/user_invite_bulk.php create mode 100644 functions/ticket_mail_try.php create mode 100644 functions/weekly_summary.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 099d1d655..a74a37de0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ This file documents all notable changes made to ITFlow. ### Upgrading to 26.12 -Update from Maintenance > Update — Queue Update hands the job to cron, which updates the files and applies this release's thirty-nine database changes (3.6.6 to 4.0.6) in the same pass. They add four tables for RMM providers and their alerts, a setting for API keys in the address, a column marking the emails the mail queue clears once sent, a column and a table for agent banners, two columns for ticket statuses with a setting for the "ticket assigned" message, a column on tickets with a setting for the "no response" message, a table for who a ticket's email replies go to, the settings for Quick tickets, columns on recurring tickets and recurring invoices for booking and making them ahead, two columns on ticket replies for when their time was worked, the settings for cron alerts, a table and a setting for labor costs, a column on users for API / service accounts, columns on tickets and recurring tickets for how long booked work takes, a column each on recurring tickets and calendar events for repeating on a weekday, three columns on agents' settings for their calendar color, what their calendar shows and their own time zone, a table for the agents helping on a ticket, three columns on quote lines with three settings for what a quote line costs and how it is priced, a column each on quote lines and products with a setting for their pictures, two settings and a column on tickets for keeping project tickets apart, a table for your other offices with a column each on agents and invites for which one they work from, three settings for the emails and notices your agents get about their tickets, three settings for sending and reading Microsoft 365 mail through Microsoft Graph, five tables, four settings and a role permission for purchase orders, five settings for switching Dispatch, Bookkeeping, Purchasing, Trips and the calendar on and off, and eight tables, three columns and two settings for the Knowledge Base, who can read its articles, what its readers think of them and their files, a column on documents for how often each is reviewed, a column on agents' settings for compact rows, a table for reply drafts, a table for which tickets each agent has opened, two columns on contacts for an email that bounces, a table for credentials' earlier passwords, a column on recurring invoices for a pause with an end, a column on calendar events, two columns on agents' settings and a table for reminders, and change nothing that is there. Nothing else is required, but it is a big release, so take a backup before you update. Purchasing starts off - switch it on in Admin > Settings > Modules - and so does the Knowledge Base's public help center, in Admin > Settings > Knowledge Base. This release has thirteen security fixes - see Security. On Apache, ITFlow's own .htaccess now sends security headers: if you show ITFlow inside a frame on another website, it will now refuse to load there. On nginx, add the same headers to your server block if you want them - see Developer Updates. +Update from Maintenance > Update — Queue Update hands the job to cron, which updates the files and applies this release's forty database changes (3.6.6 to 4.0.7) in the same pass. They add four tables for RMM providers and their alerts, a setting for API keys in the address, a column marking the emails the mail queue clears once sent, a column and a table for agent banners, two columns for ticket statuses with a setting for the "ticket assigned" message, a column on tickets with a setting for the "no response" message, a table for who a ticket's email replies go to, the settings for Quick tickets, columns on recurring tickets and recurring invoices for booking and making them ahead, two columns on ticket replies for when their time was worked, the settings for cron alerts, a table and a setting for labor costs, a column on users for API / service accounts, columns on tickets and recurring tickets for how long booked work takes, a column each on recurring tickets and calendar events for repeating on a weekday, three columns on agents' settings for their calendar color, what their calendar shows and their own time zone, a table for the agents helping on a ticket, three columns on quote lines with three settings for what a quote line costs and how it is priced, a column each on quote lines and products with a setting for their pictures, two settings and a column on tickets for keeping project tickets apart, a table for your other offices with a column each on agents and invites for which one they work from, three settings for the emails and notices your agents get about their tickets, three settings for sending and reading Microsoft 365 mail through Microsoft Graph, five tables, four settings and a role permission for purchase orders, five settings for switching Dispatch, Bookkeeping, Purchasing, Trips and the calendar on and off, and eight tables, three columns and two settings for the Knowledge Base, who can read its articles, what its readers think of them and their files, a column on documents for how often each is reviewed, a column on agents' settings for compact rows, a table for reply drafts, a table for which tickets each agent has opened, two columns on contacts for an email that bounces, a table for credentials' earlier passwords, a column on recurring invoices for a pause with an end, a column on calendar events, two columns on agents' settings and a table for reminders, four settings for the weekly summary, and change nothing that is there. Nothing else is required, but it is a big release, so take a backup before you update. Purchasing starts off - switch it on in Admin > Settings > Modules - and so does the Knowledge Base's public help center, in Admin > Settings > Knowledge Base. This release has thirteen security fixes - see Security. On Apache, ITFlow's own .htaccess now sends security headers: if you show ITFlow inside a frame on another website, it will now refuse to load there. On nginx, add the same headers to your server block if you want them - see Developer Updates. ### Security @@ -190,6 +190,9 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u - Archived: Tools > Archived lists what was archived lately - clients, contacts, locations, vendors, assets, credentials, documents, files, domains, certificates, networks, racks, services, licenses, projects and products - newest first, with who archived each one, to restore one or several at once. The last 30 days to start with, or pick another time, a kind or a client. - Client portal: one Search box in the top bar looks through the client's tickets - their subject, number and what was said on them - the documents you share with them and your Help articles at once, the first eight of each with See all. Nothing found offers Ask us, and a search with no Help article goes on the Knowledge Base's Not found list. - Client portal: a ticket says when to expect your first reply - We'll reply by and the time, from its SLA - until you reply, while it isn't on hold and the time is still ahead. New ticket says how soon you reply to each priority, beside the priority and under What happens next. Without SLAs nothing is promised. +- Users: Invite several, beside New User, invites a list of people at once with one role (and office, and whether two-step sign-in is required) - one per line, as a name and email or just the email. Each gets their own invite email; anyone who already has an account is skipped and named. It needs email set up. +- Tickets: Try an email, in Admin > Settings > Tickets under Email to ticket, says what the support mailbox would do with an email from someone with some subject - a reply on which ticket and as whom, a new ticket for which client, or left in the mailbox - step by step, without making or sending anything. +- Weekly summary: Admin > Settings > Notifications can email the owners - every admin, or the addresses you give - early on the day you pick, how the last seven days went beside the seven before: tickets opened and resolved, SLAs missed, ratings, time logged and billable, new clients, invoiced and paid, the busiest clients, and what needs a look - open tickets nobody has, overdue invoices, and domains and certificates running out. Send me one now shows what it looks like, and it is among the email previews. Off to start with. - Tickets: a ticket shows what its client owes on invoices past their due date, beside the client's name, to agents who can see invoices - with how many and how late the oldest is. It opens their overdue invoices. - Invoices: a draft left more than a week says so on the invoices list, under its status, and Drafts left a week picks them out - In drafts counts them too. The nightly notice about drafts says how many have been left that long, and opens them. - Contacts and assets: an email already on another contact - now when changing a contact too - and a serial number or MAC address already on another asset are named as they're typed, with who or which, and where. @@ -392,6 +395,9 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u - Pickers: `js/itflow_select.js` keeps the last five values picked (`RECENT_KEEP`) per kind in `localStorage` (`itflow.recent.`) - the kind from the select's name (`RECENT_NAMES`) or `data-recent`, `data-recent="false"` to leave one out, single selects only - and draws them first under Recent, the rest under All, while nothing is typed in a list of `SEARCH_FROM` (9) options or more. - Archived: `agent/archived.php` and `functions/archived.php` - `archivedKinds()` (table, columns, read module, audit log type), `archivedKindsFor($level)`, `archivedUnionSql()` (one `UNION ALL`, client-scoped, to sort and page), `archivedBy()` (the latest Archive in the audit log), `archivedUrl()`, `archivedCanRestore()` and `archivedRestore()`. Restore on a row uses each kind's own `restore_*` handler; `bulk_restore_archived` (`agent/post/archived.php`) takes `archived_items[]` as `kind:id`. - Client portal: `client/search.php` (tickets by `portalTicketScopeSql()` with their non-internal replies, documents behind the same check as `portalItGate()`, articles by `kbClientSql()`, `kbSearchMissSave()` when Help finds nothing), from `.itflow-portal-search` in `client/includes/header.php`. `portalReplyTimes()` (each priority's response target from `getTicketSlaId()`) and `portalTicketReplyBy($ticket_id)` (`ticket_response_due_at` while there is no `ticket_first_response_at`, the status doesn't pause the SLA and the time is ahead) in `client/functions.php`. +- Users: `invite_users_bulk` (`admin/post/users.php`, `admin/modals/user/user_invite_bulk.php`) - `userInviteBulkParse()` (up to `USER_INVITE_BULK_MAX`, 50) in `functions/user_invite.php`, then `userCheckIdentity()`, the seat limit, `userInviteSave()` and `userInviteSend()` for each, as `add_user` does for one. +- Tickets: `ticketMailTry($from, $subject, $auto_reply)` in `functions/ticket_mail_try.php` asks what `cron/ticket_email_parser.php` asks, in its order, with no writes - `ticketMailTryReply()` mirrors `addReply()`. Kept in step with the parser by hand. `admin/modals/ticket_mail/ticket_mail_try.php` (`?try=1` answers JSON with the result). +- Weekly summary: database 4.0.7 adds `settings.config_weekly_summary`, `_day` (1 Monday to 7 Sunday), `_to` (NULL for every admin) and `_sent_at`. `functions/weekly_summary.php`: `weeklySummaryReady()`, `weeklySummarySettings()`, `weeklySummaryRecipients()`, `weeklySummaryNumbers($from, $to)`, `weeklySummaryNeeds()`, `weeklySummaryEmail($until)`, `weeklySummarySend()` and `weeklySummaryRun()`, called by `cron/nightly_tasks.php`, which sends once on the day (`config_weekly_summary_sent_at`). `send_weekly_summary_now` in `admin/post/settings_notification.php`; the `weekly_summary` email preview. ## [26.11.8] diff --git a/admin/database_updates/4.0.7.php b/admin/database_updates/4.0.7.php new file mode 100644 index 000000000..513b879dd --- /dev/null +++ b/admin/database_updates/4.0.7.php @@ -0,0 +1,37 @@ + Settings > Notifications: + * settings.config_weekly_summary - on or off (off to start with) + * settings.config_weekly_summary_day - the day it goes, 1 Monday to 7 Sunday + * settings.config_weekly_summary_to - who it goes to, NULL for every admin + * settings.config_weekly_summary_sent_at - the day it last went, so it goes once + * + * Re-runnable: each column is only added where it isn't there yet. + */ + +defined('FROM_DB_UPDATER') || die("Direct file access is not allowed"); + + $itflow_column = function ($table, $column) use ($mysqli) { + $table = mysqli_real_escape_string($mysqli, $table); + $column = mysqli_real_escape_string($mysqli, $column); + return mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$table' AND COLUMN_NAME = '$column'")) > 0; + }; + + if (!$itflow_column('settings', 'config_weekly_summary')) { + mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_weekly_summary` tinyint(1) NOT NULL DEFAULT 0 AFTER `config_notify_helper_replies`"); + } + if (!$itflow_column('settings', 'config_weekly_summary_day')) { + mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_weekly_summary_day` tinyint(1) NOT NULL DEFAULT 1 AFTER `config_weekly_summary`"); + } + if (!$itflow_column('settings', 'config_weekly_summary_to')) { + mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_weekly_summary_to` varchar(500) DEFAULT NULL AFTER `config_weekly_summary_day`"); + } + if (!$itflow_column('settings', 'config_weekly_summary_sent_at')) { + mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_weekly_summary_sent_at` date DEFAULT NULL AFTER `config_weekly_summary_to`"); + } diff --git a/admin/modals/ticket_mail/ticket_mail_try.php b/admin/modals/ticket_mail/ticket_mail_try.php new file mode 100644 index 000000000..e115e46e7 --- /dev/null +++ b/admin/modals/ticket_mail/ticket_mail_try.php @@ -0,0 +1,97 @@ + Settings > Tickets, Email to ticket. + * Asked again with ?try=1, it answers with just the result, which the window + * puts under the form. + */ + +require_once '../../includes/modal_header.php'; + +/** The result - the steps, and where the email ends up. */ +function ticketMailTryHtml(array $result) { + [$icon, $tone, $title, $text] = $result['outcome']; + $html = '
    '; + foreach ($result['steps'] as [$question, $answer]) { + $html .= '
  1. ' . escapeHtml($question) . '' . escapeHtml($answer) . '
  2. '; + } + $html .= '
'; + $html .= '
' + . '
' . escapeHtml($title) . '
' . ($text !== '' ? '
' . escapeHtml($text) . '
' : '') . '
'; + return $html; +} + +if (isset($_GET['try'])) { + $from = trim((string) ($_GET['from'] ?? '')); + if (!filter_var($from, FILTER_VALIDATE_EMAIL)) { + echo json_encode(['result' => '
' . ($from === '' ? 'Type who it is from.' : '' . escapeHtml($from) . ' is not an email address.') . '
']); + exit; + } + echo json_encode(['result' => ticketMailTryHtml(ticketMailTry($from, (string) ($_GET['subject'] ?? ''), !empty($_GET['auto'])))]); + exit; +} + +ob_start(); + +?> + +
+ + +
+ + + + + +
+ + + + +
+ +=') ? ['config_notify_assigned_app', 'config_notify_assigned_email', 'config_notify_helper_replies'] : []; - $before = settingsCurrent(array_merge(['config_enable_alert_domain_expire', 'config_ticket_client_general_notifications', 'config_recurring_auto_send_invoice'], $team_columns, $sms_columns)); + // The weekly summary - from database 4.0.7 (functions/weekly_summary.php) + $weekly_columns = weeklySummaryReady() ? ['config_weekly_summary', 'config_weekly_summary_day', 'config_weekly_summary_to'] : []; + $before = settingsCurrent(array_merge(['config_enable_alert_domain_expire', 'config_ticket_client_general_notifications', 'config_recurring_auto_send_invoice'], $team_columns, $sms_columns, $weekly_columns)); $after = ['config_enable_alert_domain_expire' => empty($_POST['config_enable_alert_domain_expire']) ? 0 : 1]; if ($config_module_enable_ticketing) { @@ -43,8 +45,30 @@ if (isset($_POST['edit_notification_settings'])) { } } + if ($weekly_columns) { + $after['config_weekly_summary'] = empty($_POST['config_weekly_summary']) ? 0 : 1; + $day = intval($_POST['config_weekly_summary_day'] ?? $before['config_weekly_summary_day']); + $after['config_weekly_summary_day'] = isset(WEEKLY_SUMMARY_DAYS[$day]) ? $day : intval($before['config_weekly_summary_day']); + $after['config_weekly_summary_to'] = implode(', ', weeklySummaryAddresses($_POST['config_weekly_summary_to'] ?? '')); + } + settingsSave('notification', $before, $after); redirect(); } + +if (isset($_POST['send_weekly_summary_now'])) { + + // The weekly summary for the seven days to yesterday, to the admin asking - to see what it looks like + validateCSRFToken(); + + if (!weeklySummaryReady() || !filter_var($session_email ?? '', FILTER_VALIDATE_EMAIL)) { + flashAlert('There is nowhere to send it - your account has no email address.', 'error'); + redirect(); + } + $sent = weeklySummarySend([['email' => $session_email, 'name' => $session_name]], date('Y-m-d')); + flashAlert($sent ? 'The weekly summary is on its way to ' . escapeHtml($session_email) . '.' : 'It could not be queued - check Admin > Mail.', $sent ? 'success' : 'error'); + redirect(); + +} diff --git a/admin/post/users.php b/admin/post/users.php index 469a06759..a5f13c5cc 100644 --- a/admin/post/users.php +++ b/admin/post/users.php @@ -131,6 +131,72 @@ if (isset($_POST['add_user'])) { } +if (isset($_POST['invite_users_bulk'])) { + + // Several agents invited at once, all with one role - each emailed their own link (functions/user_invite.php) + validateCSRFToken(); + + if (!userInvitesReady() || !userInviteMailReady()) { + flashAlert('Inviting several at once needs email set up - invite them one at a time from New User to get each link.', 'error'); + redirect(); + } + $error = ''; + $role = userCheckRole($_POST['role'] ?? 0, 0, $error); + if (!$role) { + flashAlert($error, 'error'); + redirect(); + } + $people = userInviteBulkParse($_POST['people'] ?? ''); + if (!$people) { + flashAlert('Put one person on each line - their name and email, or just the email.', 'error'); + redirect(); + } + $force_mfa = !empty($_POST['force_mfa']) ? 1 : 0; + + $invited = []; + $skipped = []; + foreach ($people as $i => $person) { + if (!filter_var($person['email'], FILTER_VALIDATE_EMAIL)) { + $skipped[] = mb_substr($person['line'], 0, 100) . ' - no email address in it'; + continue; + } + $identity = userCheckIdentity($person, 0, $error); + if (!$identity) { + $skipped[] = rtrim(strip_tags($error), '.'); + continue; + } + [$name, $email] = $identity; + $open = userInviteFor($email); + if (agentUserLimitReached($open && userInviteState($open) === 'pending' ? intval($open['invite_id']) : 0)) { + $skipped[] = 'every seat is taken, so ' . (count($people) - $i) . ' more weren\'t invited'; + break; + } + $saved = userInviteSave(['name' => $name, 'email' => $email, 'role_id' => intval($role['role_id']), 'force_mfa' => $force_mfa, + 'access' => [], 'office_id' => $_POST['office_id'] ?? 0]); + $send_error = ''; + if (!userInviteSend($saved['invite_id'], true, $send_error)) { + if (!$saved['again']) { + mysqli_query($mysqli, "DELETE FROM user_invites WHERE invite_id = " . intval($saved['invite_id'])); + } + $skipped[] = rtrim(strip_tags($send_error), '.'); + break; + } + logAudit("User", "Invite", "$session_name invited " . escapeSql("$name ($email) as {$role['role_name']}") . ($saved['again'] ? ' again' : '')); + $invited[] = escapeHtml($name); + } + + $count = count($invited); + $message = $count + ? "Invited $count as " . escapeHtml($role['role_name']) . " - an email with a link to join is on the way to " . ($count === 1 ? 'them' : 'each') . ". It works for " . USER_INVITE_LINK_DAYS . " days" + : 'Nobody was invited'; + if ($skipped) { + $message .= '. Skipped: ' . implode('; ', array_map('escapeHtml', $skipped)); + } + flashAlert($message, $skipped ? ($count ? 'warning' : 'error') : 'success'); + redirect($count ? 'users.php?show=invited' : null); + +} + if (isset($_GET['resend_user_invite']) || isset($_GET['link_user_invite']) || isset($_GET['cancel_user_invite'])) { // An invite waiting: email it again, get its link to pass on (either makes a new link, diff --git a/admin/settings_notification.php b/admin/settings_notification.php index 46caf4d88..7c664f3b2 100644 --- a/admin/settings_notification.php +++ b/admin/settings_notification.php @@ -34,6 +34,25 @@ echo settingsPageStart('notification'); + + + +
Mail sending is off, so it can't go. Set up sending
+ + 'Sent by the nightly job in Maintenance > Cron.']) ?> +
+ 'fa-calendar-day']) ?> + 'fa-envelope', 'maxlength' => 500, 'placeholder' => 'Every admin', + 'help' => 'Addresses, separated by commas. Empty, every admin gets it.']) ?> +
+
+ + The last seven days, to - to see what it looks like. +
+ + +
Mail sending is off, so none of these go. Set up sending
@@ -75,5 +94,13 @@ echo settingsPageStart('notification'); + + + + +
'From someone who isn\'t a contact, at a domain no client has - otherwise those are left in the mailbox, flagged and unread.']) ?> + +
+ + What an email from someone would become - a reply, a ticket, or left in the mailbox - and why. Uses the saved settings. +
diff --git a/admin/users.php b/admin/users.php index 4aba827de..5e2c413e2 100644 --- a/admin/users.php +++ b/admin/users.php @@ -251,6 +251,12 @@ $csrf = $_SESSION['csrf_token'];