mirror of
https://github.com/itflow-org/itflow
synced 2026-12-03 09:39:02 +00:00
225 lines
17 KiB
PHP
225 lines
17 KiB
PHP
<?php
|
|
|
|
/*
|
|
* ITFlow - where an email to the ticket mailbox goes: the one place it is decided, for the Ticket Email Parser
|
|
* (cron/ticket_email_parser.php), which then does it, and for Try an Email (functions/ticket_mail_try.php), which shows
|
|
* it. Asked in this order, each answer kept as a step:
|
|
*
|
|
* 1. a reply in a ticket's conversation, by its In-Reply-To or References (ticketMailHeaderTicket()) - a reply on it.
|
|
* Try an Email has no headers to read, so it says so and goes on;
|
|
* 2. a ticket number in [brackets] in the subject, with the prefix that ticket was given (ticketMailSubjectTicket()) -
|
|
* a reply on it;
|
|
* - from one of your agents, and not forged, automatic or a website form's - the parser reads a forward in it
|
|
* (functions/ticket_mail_forward.php) and asks again as from whoever it forwards;
|
|
* - an email routing rule (mailRouteFor(), functions/mail_route.php) - how the ticket is set up, and its client over
|
|
* the sender's contact or domain. Never an out-of-office;
|
|
* - the sender's contact - at the client whose domain it is, when they are one at more than one
|
|
* (ticketMailSenderContact()) - or the client whose domain it is;
|
|
* - an out-of-office that answers no ticket - set aside, when it is ITFlow's to take; else left in the inbox;
|
|
* 3. a subject almost the same as an open ticket of the client's from the last week - a reply on it from its contact or
|
|
* a CC; from anyone else, and from a forward or a system a rule routed, a ticket of its own, linked as related;
|
|
* 4. a contact - their client's ticket - or routed to a client, the rule's client's;
|
|
* 5. an address at a client's domain - a new contact and their ticket. Not a forged one;
|
|
* 6. anyone else - a ticket with no client when unknown senders are let in, an agent forwarded it or a rule took it;
|
|
* else left in the inbox, flagged.
|
|
*
|
|
* Before any of this the parser has dealt with ITFlow's own email come back, a mail server's bounce, an ignored sender,
|
|
* a second copy of an email and what an agent asked for in Unprocessed Email - Try an Email asks those itself, as they
|
|
* hang on what is in the email.
|
|
*/
|
|
|
|
/*
|
|
* Where an email goes. $in, raw - none of it SQL-escaped:
|
|
* from the sender's address ('' for none) - its Reply-To's, when that is someone at the same domain
|
|
* from_name their name
|
|
* subject its subject
|
|
* visitor ['email', 'name'] - a website form's visitor, its Reply-To at another domain - or null
|
|
* thread_ticket_id the ticket whose emails it answers, 0 for none - null when that can't be known (Try an Email)
|
|
* automatic '', 'auto-reply' (an out-of-office) or 'automated' (a system or a list) - ticketMailAutomaticKind()
|
|
* forged why the mail server found it forged, '' when it didn't - ticketMailSenderCheck()
|
|
* answers_ours an out-of-office answering an email ITFlow sent, by its headers - null when that can't be known
|
|
* unprocessed tried again from Unprocessed Email, so ITFlow's to take
|
|
* forward the agent who forwarded it, ['user_id', 'user_name'] - the sender is whoever it forwards
|
|
*
|
|
* What it found - ['steps' => [[question, answer], ...], 'action' => ...] and for each action:
|
|
* reply 'ticket_id', 'by' - thread, number or similar
|
|
* ticket 'client_id', 'client_name', 'contact' (the ticket's contact - contact_id, contact_name, contact_email,
|
|
* contact_client_id - or null), 'new_contact' (the sender made a contact at the client first), 'picked'
|
|
* (ticketMailSenderContact(), at more than one client - for the note saying so), 'sender' (['name',
|
|
* 'email'] when the contact isn't who sent it), 'similar' (the ticket it is linked to as related, or null)
|
|
* and 'link_why' ('', forward or route), 'taken' (for one with no client: unknown, forward or route)
|
|
* set_aside an out-of-office that answers no ticket
|
|
* leave 'why', and 'not_ours' - left in the inbox, flagged, for whoever reads it
|
|
* With every action: 'agent' (the sender is an agent, and it may be a forward - the parser reads it, Try an Email says so),
|
|
* 'route' (the rule, mailRouteFor(), or null) and 'quiet' (routed, and the sender isn't the ticket's contact: not
|
|
* answered or copied in).
|
|
*/
|
|
function ticketMailDecide(array $in) {
|
|
global $mysqli, $config_ticket_email_parse_unknown_senders, $config_ticket_prefix;
|
|
|
|
$from = strtolower(trim((string) ($in['from'] ?? '')));
|
|
$from = preg_match('/^[^@\s]+@[^@\s]+$/', $from) ? $from : '';
|
|
$domain = $from !== '' ? substr($from, strrpos($from, '@') + 1) : '';
|
|
$subject = (string) ($in['subject'] ?? '');
|
|
$visitor = !empty($in['visitor']) ? (array) $in['visitor'] : null;
|
|
$automatic = (string) ($in['automatic'] ?? '');
|
|
$forged = (string) ($in['forged'] ?? '');
|
|
$forward = !empty($in['forward']) ? (array) $in['forward'] : null;
|
|
$thread_ticket_id = array_key_exists('thread_ticket_id', $in) && $in['thread_ticket_id'] === null ? null : intval($in['thread_ticket_id'] ?? 0);
|
|
$from_sql = escapeSql($from);
|
|
|
|
$out = ['steps' => [], 'action' => 'leave', 'agent' => null, 'route' => null, 'quiet' => false, 'ticket_id' => 0, 'by' => '',
|
|
'client_id' => 0, 'client_name' => null, 'contact' => null, 'new_contact' => false, 'picked' => [], 'sender' => null,
|
|
'similar' => null, 'link_why' => '', 'taken' => '', 'why' => '', 'not_ours' => false, 'answers_unknown' => false];
|
|
$step = function ($question, $answer) use (&$out) {
|
|
$out['steps'][] = [$question, $answer];
|
|
};
|
|
|
|
if ($forward) {
|
|
$step('Forwarded by one of your agents?', 'Yes - by ' . $forward['user_name'] . ', so it is read as from ' . ($from !== '' ? $from : 'whoever it forwards') . ', a ticket of its own');
|
|
} else {
|
|
// 1. A reply in a ticket's conversation
|
|
if ($thread_ticket_id === null) {
|
|
$step('A reply to one of a ticket\'s emails?', 'Not known here - an email answering one is matched by its headers first, whatever its subject says');
|
|
} elseif ($thread_ticket_id) {
|
|
$step('A reply to one of a ticket\'s emails?', 'Yes - ' . ticketReference(ticketRow($thread_ticket_id) ?: ['ticket_prefix' => '', 'ticket_number' => $thread_ticket_id]));
|
|
return ['action' => 'reply', 'ticket_id' => $thread_ticket_id, 'by' => 'thread'] + $out;
|
|
} else {
|
|
$step('A reply to one of a ticket\'s emails?', 'No');
|
|
}
|
|
|
|
// 2. A ticket number in the subject, with the prefix that ticket was given
|
|
$numbered = ticketMailSubjectTicket($subject);
|
|
if ($numbered) {
|
|
$step('A ticket number in the subject?', 'Yes - ' . $numbered['reference']);
|
|
return ['action' => 'reply', 'ticket_id' => intval($numbered['ticket_id']), 'by' => 'number'] + $out;
|
|
}
|
|
$step('A ticket number in the subject?', preg_match('/\[[^\[\]\s]{0,40}\d\]/', $subject)
|
|
? 'No - there is no ticket with the number in brackets, so it carries on'
|
|
: 'No - it would be [' . $config_ticket_prefix . '123] somewhere in it');
|
|
|
|
// An agent's - a forward of a client's email is read as from the client (the parser asks again, as from them)
|
|
if ($forged === '' && $automatic === '' && !$visitor && ($agent = ticketMailForwardAgent($from))) {
|
|
$out['agent'] = $agent;
|
|
$step('From one of your agents?', 'Yes - ' . $agent['user_name'] . '. A client\'s email they forward to the mailbox is read as from the client: its sender, subject and text go the steps below,'
|
|
. ' and what ' . $agent['user_name'] . ' wrote above it is their internal note on the ticket. One from someone who isn\'t a client\'s is a ticket with no client even when tickets from unknown senders are off, and they aren\'t answered or copied in. Anything else from them goes on as from them');
|
|
}
|
|
}
|
|
|
|
// An email routing rule - never for an out-of-office
|
|
$route = $automatic !== 'auto-reply' ? mailRouteFor($from, $subject) : null;
|
|
if (mailRouteReady() && $automatic !== 'auto-reply') {
|
|
$step('An email routing rule?', $route ? 'Yes - ' . $route['name'] . ': ' . mailRouteSummary($route) . ($route['gone'] ? ' - but ' . implode(', ', $route['gone']) : '') : 'No');
|
|
}
|
|
$out['route'] = $route;
|
|
|
|
// The sender: a contact - at the client whose domain it is, when they are one at more than one - or the client whose domain it is
|
|
$picked = ticketMailSenderContact($from, $domain);
|
|
$contact = $picked['contact'];
|
|
$domain_row = null;
|
|
if (!$contact && $domain !== '') {
|
|
$domain_row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT domain_client_id, domain_name, client_name FROM domains LEFT JOIN clients ON client_id = domain_client_id
|
|
WHERE domain_name = '" . escapeSql($domain) . "' AND domain_archived_at IS NULL LIMIT 1")) ?: null;
|
|
$domain_row = $domain_row && strcasecmp($domain, (string) $domain_row['domain_name']) === 0 ? $domain_row : null;
|
|
}
|
|
|
|
// An out-of-office that answers no ticket is set aside: it isn't a ticket, and isn't answered. With unknown senders not let
|
|
// in, a stranger's is ITFlow's only when it answers an email ITFlow sent; else it is left in the inbox as a stranger's is
|
|
if ($automatic === 'auto-reply') {
|
|
$ours = $config_ticket_email_parse_unknown_senders || $contact || ($domain_row && $forged === '') || !empty($in['answers_ours']) || !empty($in['unprocessed']);
|
|
if ($ours) {
|
|
$step('An out-of-office or other automatic reply?', 'Yes - it answers no ticket, so it is set aside');
|
|
return ['action' => 'set_aside'] + $out;
|
|
}
|
|
$step('An out-of-office or other automatic reply?', array_key_exists('answers_ours', $in) && $in['answers_ours'] === null
|
|
? 'Yes - from someone who isn\'t a contact, at a domain no client has: set aside if it answers an email ITFlow sent, by its headers, else left in the inbox'
|
|
: 'Yes - from someone who isn\'t a contact, at a domain no client has, and it answers nothing ITFlow sent');
|
|
return ['action' => 'leave', 'not_ours' => true, 'answers_unknown' => array_key_exists('answers_ours', $in) && $in['answers_ours'] === null,
|
|
'why' => $domain_row && $forged !== '' ? "Its sender checks failed - $forged - so it isn't taken as from the client whose domain it is from" : 'From someone who isn\'t a contact, at a domain no client has'] + $out;
|
|
}
|
|
$step('An out-of-office or other automatic reply?', $automatic === 'automated' ? 'No - sent by a system or a list: taken, but never answered' : 'No');
|
|
|
|
// Routed to a client: the rule's, over the sender's own contact or domain. Its contact is the rule's, else the sender when
|
|
// they are one of that client's contacts, else nobody. A website form's is from its visitor, as ever
|
|
$route_client = $route && $route['client'] ? $route['client'] : null;
|
|
if ($route_client) {
|
|
$picked = ['contact' => null, 'others' => [], 'why' => ''];
|
|
$domain_row = null;
|
|
$contact = null;
|
|
if (!$visitor) {
|
|
$contact = $route['contact'] ? $route['contact'] + ['client_name' => $route_client['client_name']] : (mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT contact_id, contact_name, contact_email, contact_client_id, client_name
|
|
FROM contacts LEFT JOIN clients ON client_id = contact_client_id WHERE contact_client_id = " . intval($route_client['client_id']) . " AND contact_email = '$from_sql' AND contact_archived_at IS NULL LIMIT 1")) ?: null);
|
|
}
|
|
}
|
|
// A routed email's sender is answered and copied in only as the ticket's contact - who they are, or who they will be as a
|
|
// new contact at a client's domain: a system's address isn't sent "we have your email" and every reply after it
|
|
$sender_is_contact = $contact && strtolower((string) $contact['contact_email']) === $from;
|
|
$out['quiet'] = $route && !($visitor || $sender_is_contact || ($domain_row && $forged === ''));
|
|
if ($contact && !$sender_is_contact && !$visitor) {
|
|
$out['sender'] = ['name' => (string) ($in['from_name'] ?? ''), 'email' => $from];
|
|
}
|
|
|
|
// 3. Almost the same subject as an open ticket of the client's from the last week - a reply on it from its contact or a CC.
|
|
// From anyone else at the client it is a ticket of its own, linked to that one as related - and from an agent's forward, a
|
|
// website form or a system a rule routed, always
|
|
$client_id = $route_client ? intval($route_client['client_id']) : ($contact ? intval($contact['contact_client_id']) : intval($domain_row['domain_client_id'] ?? 0));
|
|
if ($client_id && mb_strlen(trim($subject)) > 10) {
|
|
$sql = mysqli_query($mysqli, "SELECT ticket_id, ticket_prefix, ticket_number, ticket_subject, ticket_contact_id FROM tickets
|
|
WHERE ticket_client_id = $client_id AND ticket_resolved_at IS NULL AND ticket_created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)");
|
|
while ($row = mysqli_fetch_assoc($sql)) {
|
|
similar_text(mb_strtolower($subject), mb_strtolower((string) $row['ticket_subject']), $percent);
|
|
if ($percent >= 95) {
|
|
$out['similar'] = $row;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if ($out['similar']) {
|
|
$similar = $out['similar'];
|
|
$theirs = !$visitor && !$forward && !$out['quiet'] && (($contact && !$out['sender'] && intval($contact['contact_id']) === intval($similar['ticket_contact_id']))
|
|
|| ($from !== '' && mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM ticket_ccs WHERE ticket_cc_ticket_id = " . intval($similar['ticket_id']) . " AND ticket_cc_email = '$from_sql' LIMIT 1"))));
|
|
if ($theirs) {
|
|
$step('The same subject as an open ticket of theirs from the last week?', 'Yes - ' . ticketReference($similar));
|
|
return ['action' => 'reply', 'ticket_id' => intval($similar['ticket_id']), 'by' => 'similar', 'similar' => null] + $out;
|
|
}
|
|
$step('The same subject as an open ticket of theirs from the last week?', 'Yes - ' . ticketReference($similar) . ', but '
|
|
. ($forward ? 'a forward is a ticket of its own' : ($out['quiet'] ? 'a routed email from someone who isn\'t its contact is a ticket of its own' : 'someone else\'s')) . ', so it carries on');
|
|
$out['link_why'] = $forward ? 'forward' : ($out['quiet'] ? 'route' : '');
|
|
} else {
|
|
$step('The same subject as an open ticket of theirs from the last week?', $client_id ? 'No' : 'No - they aren\'t a client\'s');
|
|
}
|
|
|
|
// Routed to a client: its ticket, whoever the sender is
|
|
if ($route_client) {
|
|
return ['action' => 'ticket', 'client_id' => intval($route_client['client_id']), 'client_name' => (string) $route_client['client_name'], 'contact' => $contact] + $out;
|
|
}
|
|
|
|
// 4. A contact - a website form sent as one (an address made a contact before) is that contact's client's, from its visitor
|
|
if ($contact) {
|
|
$others = $picked['others'] ? ' - and at ' . implode(', ', $picked['others']) . ' too, so it goes to ' . ($contact['client_name'] ?? 'no client') . ', ' . $picked['why'] : '';
|
|
$step('A contact\'s address?', 'Yes - ' . $contact['contact_name'] . ($contact['client_name'] !== null ? ', ' . $contact['client_name'] : '') . $others);
|
|
return ['action' => 'ticket', 'client_id' => intval($contact['contact_client_id']), 'client_name' => $contact['client_name'] ?? null,
|
|
'contact' => $visitor ? null : $contact, 'picked' => $visitor ? [] : $picked] + $out;
|
|
}
|
|
$step('A contact\'s address?', 'No');
|
|
|
|
// 5. A client's domain - not a forged one: anyone could make themselves a client's contact by saying they are from its domain
|
|
if ($domain_row && $forged === '' && mb_strlen($from) <= 200) {
|
|
$step('At a client\'s domain?', 'Yes - ' . $domain . ', ' . $domain_row['client_name']);
|
|
return ['action' => 'ticket', 'client_id' => intval($domain_row['domain_client_id']), 'client_name' => $domain_row['client_name'], 'new_contact' => !$visitor] + $out;
|
|
}
|
|
$step('At a client\'s domain?', $domain_row ? "Yes - but its sender checks failed ($forged), so it isn't taken as the client's" : ($domain !== '' ? 'No - no client has ' . $domain : 'No'));
|
|
|
|
// 6. Anyone else - taken when unknown senders are let in, an agent forwarded it, or a rule took it
|
|
if ($route || $forward || $config_ticket_email_parse_unknown_senders) {
|
|
if (!$route && !$forward) {
|
|
$step('Tickets from unknown senders?', 'On');
|
|
}
|
|
return ['action' => 'ticket', 'taken' => $route ? 'route' : ($forward ? 'forward' : 'unknown')] + $out;
|
|
}
|
|
$step('Tickets from unknown senders?', 'Off');
|
|
return ['action' => 'leave', 'not_ours' => true, 'why' => $domain_row && $forged !== ''
|
|
? "Its sender checks failed - $forged - so it isn't taken as from the client whose domain it is from"
|
|
: 'From someone who isn\'t a contact, at a domain no client has'] + $out;
|
|
}
|