diff --git a/CHANGELOG.md b/CHANGELOG.md index 2045fb5d5..4bcb448bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ This file documents all notable changes made to ITFlow. ### Upgrading to 26.12.3 -Update from Maintenance > Update as usual - this release has fourteen database changes: 4.2.2 adds a table for passkeys and a setting to turn them on or off, 4.2.3 lets a document keep the Markdown it was written in, 4.2.4 lets a ticket keep its pictures as files, 4.2.5 adds identity providers, 4.2.6 keeps each ticket's email conversation together, 4.2.7 lets a reply from a CC say who sent it, 4.2.8 renames the watchers table for CCs, 4.2.9 lets the mail queue go at a pace your mail host takes, 4.3.0 lets contacts unsubscribe from bulk email, 4.3.1 encrypts the mail passwords and tokens, 4.3.2 keeps the email the ticket mailbox took but couldn't make a ticket from, for your team to sort, 4.3.3 adds Undo send and Reply-To for ticket emails, 4.3.4 lets the links in emails stop working once they're done with and keeps your own wording for the emails about a ticket, and 4.3.5 adds ignored senders and ties a bounce to the email that bounced. The client portal's Microsoft 365 single sign-on becomes the first identity provider, for client contacts, and keeps its redirect address, so nothing changes in Microsoft Entra. Nothing else is required: passkeys are on, and no one has one until they add it. Pictures already inside tickets move to files in the background afterwards - the new Ticket Pictures job in Maintenance > Cron does a batch a minute, so an install with years of emailed screenshots may take a while, and tickets work as usual meanwhile. A passkey is made for your ITFlow's address (the one in config.php) and only works there, over HTTPS - if you move ITFlow to a new address, everyone adds theirs again. Watchers are called CCs now. Emails about a ticket go to its contact with the CCs copied in, rather than one each - Admin > Settings > Tickets has the switch, under Emails about a ticket. On nginx, where ITFlow's .htaccess files aren't read, add `location ^~ /uploads/mail_unprocessed/ { deny all; }` beside the other private folders - it holds copies of the email kept for Unprocessed Email. Emailed and texted replies now wait 10 seconds on the ticket page, with Undo, before they go - Admin > Settings > Tickets sets how long, or turns it off. Ticket emails sent from another address than the mailbox ITFlow reads now ask for replies at that mailbox (Reply-To) - turn that off in the same place if the From address is an alias of that mailbox and you'd rather clients only saw it. The links in emails that open a ticket, an invoice or a quote without signing in now stop working 90 days after it is done - a ticket closed, an invoice paid, a quote answered or run out - so after the update, links to ones done longer ago than that no longer open them. The client portal still shows them, and Admin > Settings > Security sets how long, or keeps them working as before. +Update from Maintenance > Update as usual - this release has fifteen database changes: 4.2.2 adds a table for passkeys and a setting to turn them on or off, 4.2.3 lets a document keep the Markdown it was written in, 4.2.4 lets a ticket keep its pictures as files, 4.2.5 adds identity providers, 4.2.6 keeps each ticket's email conversation together, 4.2.7 lets a reply from a CC say who sent it, 4.2.8 renames the watchers table for CCs, 4.2.9 lets the mail queue go at a pace your mail host takes, 4.3.0 lets contacts unsubscribe from bulk email, 4.3.1 encrypts the mail passwords and tokens, 4.3.2 keeps the email the ticket mailbox took but couldn't make a ticket from, for your team to sort, 4.3.3 adds Undo send and Reply-To for ticket emails, 4.3.4 lets the links in emails stop working once they're done with and keeps your own wording for the emails about a ticket, and 4.3.5 adds ignored senders and ties a bounce to the email that bounced, and 4.3.6 lets text messaging be receive only. The client portal's Microsoft 365 single sign-on becomes the first identity provider, for client contacts, and keeps its redirect address, so nothing changes in Microsoft Entra. Nothing else is required: passkeys are on, and no one has one until they add it. Pictures already inside tickets move to files in the background afterwards - the new Ticket Pictures job in Maintenance > Cron does a batch a minute, so an install with years of emailed screenshots may take a while, and tickets work as usual meanwhile. A passkey is made for your ITFlow's address (the one in config.php) and only works there, over HTTPS - if you move ITFlow to a new address, everyone adds theirs again. Watchers are called CCs now. Emails about a ticket go to its contact with the CCs copied in, rather than one each - Admin > Settings > Tickets has the switch, under Emails about a ticket. On nginx, where ITFlow's .htaccess files aren't read, add `location ^~ /uploads/mail_unprocessed/ { deny all; }` beside the other private folders - it holds copies of the email kept for Unprocessed Email. Emailed and texted replies now wait 10 seconds on the ticket page, with Undo, before they go - Admin > Settings > Tickets sets how long, or turns it off. Ticket emails sent from another address than the mailbox ITFlow reads now ask for replies at that mailbox (Reply-To) - turn that off in the same place if the From address is an alias of that mailbox and you'd rather clients only saw it. The links in emails that open a ticket, an invoice or a quote without signing in now stop working 90 days after it is done - a ticket closed, an invoice paid, a quote answered or run out - so after the update, links to ones done longer ago than that no longer open them. The client portal still shows them, and Admin > Settings > Security sets how long, or keeps them working as before. ### Security @@ -88,6 +88,7 @@ Update from Maintenance > Update as usual - this release has fourteen database c - Ignored senders: Admin > Ignored Senders lists the addresses and domains whose email the ticket mailbox sets aside - no ticket, no reply on one, no answer and nothing in Unprocessed Email. Newsletters, spam that gets through and noisy systems go straight to the mailbox's ITFlow folder. A domain takes its subdomains too, each shows how many emails it set aside and when the last came, and saving one that would set aside a contact's or a client's email says so. A mail server's bounce is still read, and Try an Email shows the step. - A support mailbox shared with other email: with From unknown senders too off, ITFlow takes out of the inbox only what is its own - email from contacts and clients' domains, replies to tickets, and bounces, delay notices and out-of-office replies that are about a ticket, an email ITFlow sent or a contact. The rest stays in the inbox, flagged so it isn't looked at again: a stranger's email, their out-of-office to an email someone sent from the mailbox by hand, a bounce for such an email (nobody gets a notice about it), and email from the mailbox's own address that ITFlow didn't send - a scanner emailing itself, a note to self - which is still never a ticket. With unknown senders on the mailbox is all ITFlow's, and everything is taken as before. - Bounces come back to the email they were about: a reply on a ticket says "Email to ... bounced" with what the mail server said, where it said it was emailed, and an invoice's or quote's history says its email bounced. The Mail Queue has a Bounced tab, and the email's window shows the mail server's reason. Each email ITFlow sends now has its own Message-ID, so the bounce finds it - one sent before the update is found by its address and subject. A bounce from an address the email wasn't sent to - someone's mail sent on to another mailbox - says where it came back from, and on an email to several people nobody is singled out for it. +- Text messaging can be receive only: in Admin > Text Messaging, set the number to Receive only and texts to it still become tickets, replies on tickets and verification codes, but nothing is texted out - no replies by text, no ticket number texted back, no answers to STOP or HELP and no texts to clients. It needs no SID or secret, and the number doesn't have to be registered for sending, for a service where sending is a hassle to set up. Replies on a ticket that came in by text go by email or the portal, and the reply box says so, with the number to call. Texts waiting to go are cancelled when a number is set to Receive only. ### Bug Fixes @@ -173,6 +174,7 @@ Update from Maintenance > Update as usual - this release has fourteen database c - Mail review stage 7c: database 4.3.3 adds settings.config_ticket_reply_undo_seconds (0, 5, 10, 20 or 30 - TICKET_REPLY_UNDO_CHOICES) and config_ticket_reply_to_read, read by ticketReplySendSettings() (functions/ticket_reply_send.php). Undo send is the ticket page alone: #ticketReplyForm's data-undo-seconds, and agent/js/ticket_reply_extras.js holds an emailed or texted reply's submit in a window listener - after the form's own checks and the canned response check on the document - with the reply card inert and the [data-reply-hold] bar, then requestSubmit()s it with holdOk, cannedSendAnyway and noTimeOk set; agent/post/ticket.php is unchanged. emailQueue() puts Reply-To (ticketMailReplyToRead(): email to ticket on, the setting on, the IMAP username an address and not From) in a thread email's headers; MAIL_QUEUE_HEADERS lets one bare address through and sendQueueEmail() gives it to addReplyTo(), so Graph's large-email draft carries it too. - Mail review stage 7d: database 4.3.4 adds settings.config_guest_link_days (GUEST_LINK_DAYS_CHOICES, 90) and email_templates. functions/guest_link.php: guestLinkDoneAt() - ticket_closed_at; a Paid invoice's last unarchived payment (else its Paid history, else invoice_updated_at); an Accepted, Declined or Invoiced quote's history for it, or the end of quote_expire for one unanswered - later of that and the last history line with 'emailed' in it; guestLinkExpired() adds the days. ticketByGuestKey() returns null for an expired link, setting its new &$expired, so the guest ticket page, its attachments, its posts and its calendar file all refuse it; guest_view_invoice.php, guest_view_quote.php, the guest PDF exports (410) and the quote upload check it too, and guestLinkExpiredHtml() is the page; agent/invoice.php and agent/quote.php mark Copy Client Link Expired. functions/email_template.php: EMAIL_TEMPLATES (the 13 Tickets - to clients preview keys, with ITFlow's wording in placeholders), emailTemplateApply() replaces title, intro (paragraphs) and after where saved, filling [Placeholders] from emailTemplateValues() (ticketPlaceholderValues() and the email's own; empty when there is nothing), and puts the subject in template_subject for emailTemplateSubject(), which prefixes [REF] when it isn't in it. Applied in ticketEmailContent() (not the stranger's subject, nor Resolve - no response's intro), ticketMailSend(), ticketMailCcsAdded(), ticketMailColleaguesNew(), ticketMailTaskApproval() (the client's only) and ticketMailClosedReply(). admin/modals/email_template/email_template_edit.php and admin/post/email_template.php (save_email_template, reset_email_template - a part sent back as the default is NULL); mail_preview.php answers template and edited. - Mail review stage 8: database 4.3.5 adds email_ignored_senders and email_queue.email_message_id (indexed, filled in from email_headers), email_about, email_bounced_at and email_bounce. functions/mail_ignore.php: mailIgnoreClean() takes an address or a domain (@ or *@ in front allowed), mailIgnoreFind() matches the From address - an address rule before a domain one, a domain matching its subdomains - and cron/ticket_email_parser.php sets the email aside (seen, moved, mailIgnoreHit()) after the bounce check, unless it is a MailUnprocessedMessage asked to make a ticket or reply; ticketMailTry() has the step. Admin > Ignored Senders (admin/email_ignored_senders.php, modals/email_ignored_sender/, admin/post/email_ignored_sender.php). The parser leaves email from someone who isn't a contact at a domain no client has (unknown senders off), or whose sender checks failed, in the inbox flagged as before 7b - mailUnprocessedLeave() keeps only an email that was taken and failed, or an agent's ask; ticketMailTry() says Left in the inbox. emailQueue() gives every email a Message-ID (emailMessageId()) where the conversation gave none and takes about (invoice:N, quote:N - billingEmailQueue()'s about, passed by every invoice and quote send); addToMailQueue() keeps both. addBounce() calls mailQueueBounced(): the sent row by email_message_id, else the last week's sent to a failed address with the bounce's subject, marked with {address: why}, and a billing email's history written. mailQueueStateOf() has bounced (status 3 with email_bounced_at), mailQueueTabs() a Bounced tab, ticketReplyEmailStates() marks each bounced recipient and the key bounced, and ticketReplyEmailHtml() says so. A report naming an address that isn't on the email, or nobody: on an email to one person it is theirs ("address: why"); on one to several the named address is kept in email_bounce as it is, nobody on it is marked, and the reply has elsewhere => [address or '' => why]. A billing email's history is written the first time and for each address more. With config_ticket_email_parse_unknown_senders off the parser moves only what is ITFlow's: addBounce() takes $take_all and returns whether it was (a ticket by Message-ID or subject, emailMessageIdOurs() - itflow..@ or itflow.q.@, or in email_queue - mailQueueBounceRow(), the find split from mailQueueBounced(), or a contact's address), the own-address step moves only an email with ITFlow's Message-ID, a ticket's number in its subject or a ticket's email that it answers, and an auto-reply answering no ticket is set aside only from a contact, a client's domain or in answer to an ITFlow Message-ID (In-Reply-To, References); the rest is flagged and left, as a stranger's email is. .gitignore leaves out uploads/mail_unprocessed and uploads/kb, as the other upload folders. +- Text messaging receive only: database 4.3.6 adds sms_providers.sms_provider_send (1, or 0 for Receive only). smsSends() reads it; smsReady() is false when a provider doesn't send, so smsQueue() and everything that texts stops there, and smsNotSendingWhy() says why. smsReceiveOnly() and smsTicketTextedInNumber() drive the notes on Text Messaging, Text Messages, Settings > Notifications and Tickets and the ticket reply box; smsProviderCheck() needs the SID and secret only when sending; setting a provider to Receive only cancels the texts waiting to go (smsCancelWaiting()). Inbound (api/sms/skyetel.php, smsInbound()) is unchanged. ## [26.12.2] diff --git a/admin/database_updates/4.3.6.php b/admin/database_updates/4.3.6.php new file mode 100644 index 000000000..76e3cbde9 --- /dev/null +++ b/admin/database_updates/4.3.6.php @@ -0,0 +1,23 @@ + 0) { + $itflow_send_exists = mysqli_query($mysqli, "SELECT 1 FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'sms_providers' AND COLUMN_NAME = 'sms_provider_send' LIMIT 1"); + if (!$itflow_send_exists || mysqli_num_rows($itflow_send_exists) === 0) { + mysqli_query($mysqli, "ALTER TABLE `sms_providers` ADD `sms_provider_send` tinyint(1) NOT NULL DEFAULT 1 AFTER `sms_provider_active`"); + } + } diff --git a/admin/modals/sms_provider/sms_provider_edit.php b/admin/modals/sms_provider/sms_provider_edit.php index b824866eb..0115c0528 100644 --- a/admin/modals/sms_provider/sms_provider_edit.php +++ b/admin/modals/sms_provider/sms_provider_edit.php @@ -2,8 +2,9 @@ /* * Admin > Text Messaging - one provider's window: Add (no id, or from sms_provider_add.php) - * or Edit. Its SID and secret - the secret never put back into the page, so left empty it - * stays - the number texts go from, and whether it is on. Save asks first (check_sms_provider, + * or Edit. Whether it sends and receives or is Receive only (database 4.3.6), its SID and + * secret - needed only to send; the secret never put back into the page, so left empty it + * stays - your number, and whether it is on. Save asks first (check_sms_provider, * admin/post/sms_provider.php): what is wrong shows beside each field and the window stays * open; once all is right, it saves. */ @@ -28,6 +29,9 @@ $free_kinds = array_diff_key($kinds, $taken); $name = $row ? (string) $row['sms_provider_name'] : (string) (array_key_first($free_kinds) ?? array_key_first($kinds)); $kind = $kinds[$name] ?? reset($kinds); $number = $row ? smsNumberText($row['sms_provider_number']) : ''; +// Send and receive, or Receive only - after the 4.3.6 database update +$can_receive_only = smsColumnExists('sms_providers', 'sms_provider_send'); +$sends = !$row || smsSends($row); // Where what is wrong with a field shows $error_for = function ($field) { @@ -69,12 +73,26 @@ ob_start();
> - +
Off, nothing is texted and texts to the number aren't taken in. Texts waiting to go wait.
+ +
+ +
+ > + + > + +
+
>Texts come in, and ITFlow texts back: replies on tickets, the ticket number for a new one, and the texts to clients in Settings > Notifications.
+
>Texts to your number still become tickets, replies on tickets and verification codes - nothing is texted out. Replies go by email or the portal. No keys are needed, and the number doesn't have to be registered for sending.
+
+ +
@@ -85,20 +103,21 @@ ob_start();
- +
-
SMS-enabled in 's portal - and, for a US number, registered for texting there, or carriers block what it sends.
+
SMS-enabled in 's portal - and, to send from a US number, registered for texting there, or carriers block what it sends.
Keys
-

From 's portal - the gear at the top right, then API Keys.

+

From 's portal - the gear at the top right, then API Keys. + >Only needed to send - leave them empty for Receive only, or fill them in now for later.

- +
@@ -107,7 +126,7 @@ ob_start();
- +
' . escapeHtml($name) . ' created, texting from ' . escapeHtml($number) . ' - paste the address texts come back to into its portal, then send a test text'); + logAudit("Text Message", "Create", "$session_name added texting provider " . escapeSql("$name, " . ($receive_only ? "receive only, at $number" : "texting from $number")), 0, $provider_id); + flashAlert($receive_only + ? '' . escapeHtml($name) . ' created, receive only, at ' . escapeHtml($number) . ' - paste the address texts come back to into its portal, then text the number to try it' + : '' . escapeHtml($name) . ' created, texting from ' . escapeHtml($number) . ' - paste the address texts come back to into its portal, then send a test text'); redirect('text_messaging.php'); } @@ -58,8 +62,13 @@ if (isset($_POST['check_sms_provider']) || isset($_POST['add_sms_provider']) || redirect(); } smsProviderWrite($after, $provider_id); + // Set to Receive only - texts waiting to go would go late, or never + $cancelled = $receive_only && smsSends($before) ? smsCancelWaiting("Texts don't go out - Text Messaging was set to Receive only") : 0; + if ($cancelled) { + $lines[] = "$cancelled text" . ($cancelled === 1 ? '' : 's') . " waiting to go cancelled"; + } logAudit("Text Message", "Edit", "$session_name changed texting provider " . escapeSql("$name: " . implode('; ', $lines)), 0, $provider_id); - flashAlert('' . escapeHtml($name) . ' saved - ' . escapeHtml(count($lines) === 1 ? $lines[0] : count($lines) . ' changes')); + flashAlert('' . escapeHtml($name) . ' saved - ' . escapeHtml(count($lines) === 1 ? $lines[0] : implode('; ', $lines))); redirect(); } @@ -136,6 +145,10 @@ if (isset($_POST['send_sms_test'])) { flashAlert('Turn the texting provider on first.', 'error'); redirect(); } + if (!smsSends($row)) { + flashAlert('It is set to Receive only, so nothing is texted - text your number to try it instead.', 'error'); + redirect(); + } $number = smsNumber((string) ($_POST['number'] ?? '')); $text = trim((string) ($_POST['text'] ?? '')); if ($number === '') { diff --git a/admin/settings_notification.php b/admin/settings_notification.php index cb19340f0..846833da0 100644 --- a/admin/settings_notification.php +++ b/admin/settings_notification.php @@ -75,7 +75,9 @@ echo settingsPageStart('notification'); - + +
Texting is set to Receive only, so none of these go. Text Messaging
+
Texting isn't set up, so none of these go. Set up texting
diff --git a/admin/settings_ticket.php b/admin/settings_ticket.php index 6a242cec9..9edd472bc 100644 --- a/admin/settings_ticket.php +++ b/admin/settings_ticket.php @@ -77,6 +77,8 @@ echo settingsPageStart('ticket'); Text Messaging.') ?>
Texting isn't set up yet, so no texts come in.
+ +
Texting is set to Receive only - texts come in as below, but nothing is texted back: not the ticket number, and no answer to STOP, START or HELP.
'Off, every text waits in Texts for an agent - STOP, START and HELP are still answered.']) ?>
diff --git a/admin/text_messages.php b/admin/text_messages.php index 2b09942fa..5482c738c 100644 --- a/admin/text_messages.php +++ b/admin/text_messages.php @@ -81,7 +81,13 @@ $ready = smsReady();
- + +
+ +
Receive only - texts to your number come in, and nothing is texted out.
+ Text Messaging +
+
Texting isn't on - nothing goes out, and texts to your number aren't taken in.
diff --git a/admin/text_messaging.php b/admin/text_messaging.php index a7a9d3667..36781488d 100644 --- a/admin/text_messaging.php +++ b/admin/text_messaging.php @@ -2,7 +2,7 @@ /* * Admin > Text Messaging - the texting service ITFlow texts through (functions/sms.php), one - * number for every client. Each row says whether it is on, the number, the address its texts + * number for every client. Each row says whether it is on, whether it is Receive only, the number, the address its texts * come back to (pasted into the service's portal), and its texts in the last 30 days. Send a * test text tries it; Opted out and blocked lists the numbers that replied STOP or were * marked spam. Add and Edit are one window (modals/sms_provider/sms_provider_edit.php); the @@ -114,6 +114,9 @@ $csrf = $_SESSION['csrf_token']; Off + + Receive only +
@@ -144,7 +147,7 @@ $csrf = $_SESSION['csrf_token']; Edit - + Send a Test Text @@ -182,7 +185,7 @@ $csrf = $_SESSION['csrf_token']; $empty_state = [ 'thing' => 'texting providers', 'icon' => 'fa-sms', - 'text' => 'Set up Skyetel and your agents can reply to tickets by text, texts from your clients open tickets, and verification codes texted to your number land in Texts.', + 'text' => 'Set up Skyetel and texts from your clients open tickets, verification codes texted to your number land in Texts, and your agents can reply to tickets by text - or set it to Receive only, and nothing is texted out.', 'actions' => $can_add ? [['label' => 'New Provider', 'modal' => $add_url]] : [], ]; ?> diff --git a/agent/ticket.php b/agent/ticket.php index 446e504a0..58f2030ff 100644 --- a/agent/ticket.php +++ b/agent/ticket.php @@ -474,6 +474,8 @@ if (isset($_GET['ticket_id'])) { $reply_to = $can_email_reply && $can_edit_ticket && $ticket_is_open && ticketReplyRecipientsReady() ? ticketReplyRecipients($ticket_id) : []; // A text reply - to its contact's mobile, or the number texting on it (functions/sms.php) $sms_recipient = smsTicketRecipient($ticket); + // Texted in while texting is Receive only - replies can't be texted, so the box says how to reach them + $sms_texted_in = $sms_recipient ? '' : smsTicketTextedInNumber($ticket); // The Resolve confirmation says what resolving does - resolve_ticket emails the // contact when client notifications are on, and the nightly job closes it later @@ -893,6 +895,9 @@ if (isset($_GET['ticket_id'])) {
replied STOP to your texts - they can't be texted until they reply START.
+ +
This came in by text from . Texting is Receive only, so replies aren't texted -
+
diff --git a/db.sql b/db.sql index 75f23c9a4..778be0257 100644 --- a/db.sql +++ b/db.sql @@ -4482,6 +4482,7 @@ CREATE TABLE `sms_providers` ( `sms_provider_number` varchar(20) NOT NULL DEFAULT '', `sms_provider_inbound_key` varchar(64) NOT NULL DEFAULT '', `sms_provider_active` tinyint(1) NOT NULL DEFAULT 1, + `sms_provider_send` tinyint(1) NOT NULL DEFAULT 1, `sms_provider_last_inbound_at` datetime DEFAULT NULL, `sms_provider_created_at` datetime NOT NULL DEFAULT current_timestamp(), `sms_provider_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(), diff --git a/functions/sms.php b/functions/sms.php index ac2189edc..cde8cabb4 100644 --- a/functions/sms.php +++ b/functions/sms.php @@ -7,6 +7,8 @@ * Going out: smsQueue() puts a text in sms_messages, smsSendQueued() sends one and the * Text Messages job (cron/sms_queue.php, smsQueueRun()) sends the rest, a second apart, as * the service allows. A number that replied STOP gets nothing more until it replies START. + * A provider set to Receive only (database 4.3.6, sms_provider_send = 0) texts nothing: + * smsReady() is false, so nothing is queued, and every text below still comes in. * * Coming in: the service posts each text to api/sms/.php, which hands it to * smsInbound(). What happens to it, in order: @@ -56,7 +58,7 @@ function smsProviderKinds(): array 'Skyetel' => [ 'icon' => 'fas fa-sms', 'portal_url' => 'https://portal.skyetel.com', - 'about' => 'Your Skyetel number texts clients, and texts to it come back into ITFlow.', + 'about' => 'Texts to your Skyetel number come into ITFlow - as tickets, replies and codes - and, unless it is Receive only, ITFlow texts clients from it.', 'username_label' => 'SID', 'secret_label' => 'Secret', 'endpoint' => 'skyetel', @@ -100,15 +102,34 @@ function smsProvider(bool $fresh = false): ?array return $provider; } -/* Whether texts can go out - a provider that is on, with its keys and number */ +/* Whether a provider sends texts - not when it is Receive only (database 4.3.6). Before the update, every one sends */ +function smsSends(?array $provider): bool +{ + return $provider !== null && (!array_key_exists('sms_provider_send', $provider) || intval($provider['sms_provider_send']) === 1); +} + +/* Whether texts can go out - a provider that is on and sends, with its keys and number */ function smsReady(): bool { $provider = smsProvider(); - return $provider && isset(smsProviderKinds()[$provider['sms_provider_name']]) + return $provider && smsSends($provider) && isset(smsProviderKinds()[$provider['sms_provider_name']]) && trim((string) $provider['sms_provider_username']) !== '' && trim((string) $provider['sms_provider_secret']) !== '' && smsNumberIsPhone((string) $provider['sms_provider_number']); } +/* Whether texting is on but set to Receive only - texts come in, nothing goes out */ +function smsReceiveOnly(): bool +{ + $provider = smsProvider(); + return $provider !== null && !smsSends($provider); +} + +/* Why no text can go - for what an agent or admin is told */ +function smsNotSendingWhy(): string +{ + return smsReceiveOnly() ? "Texts don't go out - Text Messaging is set to Receive only" : "Texting isn't set up - Admin > Text Messaging"; +} + /* The address the provider posts texts to - what to paste into its portal */ function smsInboundUrl(array $provider): string { @@ -150,19 +171,30 @@ function smsProviderCheck(array $in, ?array $before): array if ($after['sms_provider_secret'] === '' && $before) { $after['sms_provider_secret'] = (string) $before['sms_provider_secret']; } + // Send and receive (1), or Receive only (0) - after the 4.3.6 database update + $sends = true; + if (smsColumnExists('sms_providers', 'sms_provider_send')) { + $sends = (string) ($in['send'] ?? '1') !== '0'; + $after['sms_provider_send'] = $sends ? 1 : 0; + } + // The keys are for sending - Receive only keeps any typed in, for when it sends if ($after['sms_provider_username'] === '') { - $errors['username'] = "Its {$kind['username_label']}, from $name's portal."; + if ($sends) { + $errors['username'] = "Its {$kind['username_label']}, from $name's portal - or pick Receive only, which needs none."; + } } elseif (mb_strlen($after['sms_provider_username']) > 250 || preg_match('/\s/', $after['sms_provider_username'])) { $errors['username'] = "That isn't a {$kind['username_label']} - it has no spaces."; } if ($after['sms_provider_secret'] === '') { - $errors['secret'] = "Its {$kind['secret_label']}, shown when the {$kind['username_label']} is made."; + if ($sends) { + $errors['secret'] = "Its {$kind['secret_label']}, shown when the {$kind['username_label']} is made."; + } } elseif (mb_strlen($after['sms_provider_secret']) > 500) { $errors['secret'] = "That is too long to be a {$kind['secret_label']}."; } if (trim((string) ($in['number'] ?? '')) === '') { - $errors['number'] = "The $name number texts go from - SMS-enabled in $name's portal."; + $errors['number'] = "Your $name number - SMS-enabled in $name's portal."; } elseif ($after['sms_provider_number'] === '') { $errors['number'] = "That isn't a phone number - with its dial code, like +1 412 555 0100."; } @@ -204,6 +236,9 @@ function smsProviderChangeLines(array $before, array $after): array if (intval($before['sms_provider_active']) !== intval($after['sms_provider_active'])) { $lines[] = intval($after['sms_provider_active']) ? 'turned on' : 'turned off'; } + if (isset($after['sms_provider_send']) && intval($before['sms_provider_send'] ?? 1) !== intval($after['sms_provider_send'])) { + $lines[] = intval($after['sms_provider_send']) ? 'set to send and receive' : 'set to receive only'; + } return $lines; } @@ -539,7 +574,7 @@ function smsQueue($to, string $text, array $meta = []): array $provider = smsProvider(); if (!smsReady()) { - return ['ids' => [], 'error' => "Texting isn't set up - Admin > Text Messaging"]; + return ['ids' => [], 'error' => smsNotSendingWhy()]; } $number = preg_replace('/\D/', '', (string) $to); if (!smsNumberIsPhone($number)) { @@ -597,7 +632,7 @@ function smsSendQueued(int $id): array $provider = smsProvider(); $kind = $provider ? (smsProviderKinds()[$provider['sms_provider_name']] ?? null) : null; if (!$kind || !smsReady()) { - return ['ok' => false, 'error' => "Texting isn't set up"]; + return ['ok' => false, 'error' => smsNotSendingWhy()]; } // Development mode sends no texts - they wait in the queue (functions/dev_mode.php) @@ -1341,6 +1376,32 @@ function smsTicketRecipient(array $ticket): ?array return ['number' => $number, 'name' => $name, 'opted_out' => smsOptedOut($number)]; } +/* + * The number that texted in on a ticket, while Text Messaging is Receive only - the reply box says replies aren't + * texted, and the number to call. '' otherwise + */ +function smsTicketTextedInNumber(array $ticket): string +{ + global $mysqli; + if (!smsReceiveOnly()) { + return ''; + } + $last = mysqli_fetch_row(mysqli_query($mysqli, "SELECT sms_message_number FROM sms_messages WHERE sms_message_ticket_id = " . intval($ticket['ticket_id']) . " AND sms_message_direction = 'in' ORDER BY sms_message_id DESC LIMIT 1")); + return smsNumberIsPhone((string) ($last[0] ?? '')) ? (string) $last[0] : ''; +} + +/* + * Texts waiting to go, cancelled - when the provider is set to Receive only, or deleted, they would otherwise go late or + * never. How many + */ +function smsCancelWaiting(string $why): int +{ + global $mysqli; + mysqli_query($mysqli, "UPDATE sms_messages SET sms_message_status = 'cancelled', sms_message_error = '" . escapeSql($why) . "' + WHERE sms_message_direction = 'out' AND sms_message_status = 'queued'"); + return mysqli_affected_rows($mysqli); +} + /* * Text a reply on a ticket to who it goes to (smsTicketRecipient()) - now, the rest by the job * when it is long. ['ok' => went, 'queued' => waiting to go, 'to' => who, for reading, 'error']. @@ -1349,7 +1410,7 @@ function smsTicketReplySend(array $ticket, int $reply_id, string $html, int $use { $recipient = smsTicketRecipient($ticket); if (!$recipient) { - return ['ok' => false, 'queued' => false, 'to' => '', 'error' => smsReady() ? "there's no mobile number on its contact to text" : "texting isn't set up"]; + return ['ok' => false, 'queued' => false, 'to' => '', 'error' => smsReady() ? "there's no mobile number on its contact to text" : lcfirst(smsNotSendingWhy())]; } $to = ($recipient['name'] !== '' ? $recipient['name'] . ' at ' : '') . smsNumberText($recipient['number']); $text = smsTextFromHtml($html); @@ -1676,7 +1737,7 @@ function smsOptInAsk(int $contact_id, int $user_id): array { global $mysqli; if (!smsReady()) { - return ['ok' => false, 'error' => "texting isn't set up"]; + return ['ok' => false, 'error' => lcfirst(smsNotSendingWhy())]; } $contact = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT contact_id, contact_name, contact_client_id, contact_mobile, contact_mobile_country_code FROM contacts WHERE contact_id = $contact_id AND contact_archived_at IS NULL")); $number = $contact ? smsContactNumber($contact) : '';