Files
itflow/functions/guest_link.php

114 lines
6.4 KiB
PHP

<?php
/*
* Guest links - the links in emails that open a ticket, an invoice or a quote without signing in - stop working
* config_guest_link_days (Admin > Settings > Security, 4.3.4) after the thing is done:
* a ticket after it was closed (ticket_closed_at) - reopened, it works again
* an invoice after it was paid in full - its last payment
* a quote after it was accepted, declined or invoiced, or ran out (quote_expire) without an answer
* Emailed again since, it counts from then - a paid invoice sent again works for as long again. An open ticket, an
* unpaid invoice and a quote still waiting for an answer always work. 0 days keeps them all working, as before.
*
* Checked by ticketByGuestKey() (functions/ticket.php) for every guest ticket page and action, and by the guest
* invoice and quote pages and their PDFs (guest/). An expired link says so (guestLinkExpiredHtml()), and where the
* client portal is on, points there - signed in, they see it as ever.
*/
const GUEST_LINK_DAYS_CHOICES = [0 => 'Never - they keep working', 30 => '30 days', 60 => '60 days', 90 => '90 days', 180 => '180 days', 365 => 'A year'];
const GUEST_LINK_DAYS_DEFAULT = 90;
/** Whether the 4.3.4 update has run - the setting is there. */
function guestLinkReady() {
global $mysqli;
static $ready = null;
if ($ready === null) {
$version = defined('CURRENT_DATABASE_VERSION') ? CURRENT_DATABASE_VERSION
: (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT config_current_database_version FROM settings WHERE company_id = 1"))[0] ?? '0');
$ready = version_compare($version, '4.3.4', '>=');
}
return $ready;
}
/** How many days a done ticket's, invoice's or quote's link keeps working - 0 for always. */
function guestLinkDays() {
global $mysqli;
static $days = null;
if ($days === null) {
$days = guestLinkReady() ? intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT config_guest_link_days FROM settings WHERE company_id = 1"))[0] ?? 0) : 0;
$days = isset(GUEST_LINK_DAYS_CHOICES[$days]) ? $days : GUEST_LINK_DAYS_DEFAULT;
}
return $days;
}
/*
* When a ticket ('ticket'), invoice or quote ($id) was done, for its link - 'Y-m-d H:i:s', or '' while it isn't
* (an open ticket, an unpaid invoice, a quote waiting for an answer). Emailed since, then.
*/
function guestLinkDoneAt($type, $id) {
global $mysqli;
$id = intval($id);
$done = '';
if ($type === 'ticket') {
$done = (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT ticket_closed_at FROM tickets WHERE ticket_id = $id"))[0] ?? '');
return $done;
}
if ($type === 'invoice') {
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT invoice_status, invoice_updated_at, invoice_created_at FROM invoices WHERE invoice_id = $id"));
if (!$row || $row['invoice_status'] !== 'Paid') {
return '';
}
// Its last payment - else the history saying it was paid, else when it last changed
$done = (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT MAX(payment_created_at) FROM payments WHERE payment_invoice_id = $id AND payment_archived_at IS NULL"))[0] ?? '');
if ($done === '') {
$done = (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT MAX(history_created_at) FROM history WHERE history_invoice_id = $id AND history_status = 'Paid'"))[0] ?? '');
}
$done = $done !== '' ? $done : (string) ($row['invoice_updated_at'] ?: $row['invoice_created_at']);
$history = "history_invoice_id = $id";
} elseif ($type === 'quote') {
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT quote_status, quote_expire, quote_updated_at, quote_created_at FROM quotes WHERE quote_id = $id"));
if (!$row) {
return '';
}
if (in_array($row['quote_status'], ['Accepted', 'Declined', 'Invoiced'], true)) {
$done = (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT MAX(history_created_at) FROM history WHERE history_quote_id = $id
AND history_recurring_invoice_id = 0 AND history_status IN ('Accepted', 'Declined', 'Cancelled', 'Invoiced')"))[0] ?? '');
$done = $done !== '' ? $done : (string) ($row['quote_updated_at'] ?: $row['quote_created_at']);
} elseif (!empty($row['quote_expire']) && strtotime($row['quote_expire']) < strtotime('today')) {
// Ran out without an answer - at the end of its last day
$done = date('Y-m-d 23:59:59', strtotime($row['quote_expire']));
} else {
return '';
}
$history = "history_quote_id = $id AND history_recurring_invoice_id = 0";
} else {
return '';
}
// Emailed again since - it counts from then
$emailed = (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT MAX(history_created_at) FROM history WHERE $history AND history_description LIKE '%emailed%'"))[0] ?? '');
return $emailed !== '' && strtotime($emailed) > strtotime($done) ? $emailed : $done;
}
/** Whether a ticket's, invoice's or quote's link has stopped working - done, and that long ago. */
function guestLinkExpired($type, $id) {
$days = guestLinkDays();
if ($days <= 0) {
return false;
}
$done = guestLinkDoneAt($type, $id);
return $done !== '' && strtotime($done) + $days * 86400 < time();
}
/* The page an expired link opens - what it was for, and where to see it now: the client portal, when it is on. */
function guestLinkExpiredHtml($type) {
global $config_client_portal_enable, $config_base_url;
$what = ['ticket' => 'ticket', 'invoice' => 'invoice', 'quote' => 'quote'][$type] ?? 'link';
$icon = ['ticket' => 'fa-life-ring', 'invoice' => 'fa-file-invoice-dollar', 'quote' => 'fa-comment-dollar'][$type] ?? 'fa-link';
$portal = !empty($config_client_portal_enable);
$text = "Links to a $what stop working a while after it is done, so an old email can't open it. "
. ($portal ? 'Sign in to the client portal to see it, or ask us for a new link.' : 'Ask us if you need it again.');
return '<div class="itflow-asset-empty mt-5" data-guest-link-expired><i class="fas ' . $icon . ' fa-3x mb-3 opacity-50"></i>'
. '<h1 class="h4">This ' . $what . ' link has expired</h1><p>' . escapeHtml($text) . '</p>'
. ($portal ? '<a class="btn btn-primary" href="https://' . escapeHtml((string) $config_base_url) . '/client/"><i class="fas fa-sign-in-alt me-2"></i>Client portal</a>' : '')
. '</div>';
}