Files
itflow/admin/post/email_routing_rule.php

127 lines
6.4 KiB
PHP

<?php
/*
* Admin > Email Routing (admin/email_routing_rules.php, functions/mail_route.php): add_routing_rule and edit_routing_rule
* keep an address or a domain, with words its subject has, and where its new tickets by email go - a client and a
* contact, a category, a priority, an agent and a ticket template - and why. An address or domain that isn't one, a rule
* there already for it and those words, or one that sets nothing, comes back saying so. Each thing it names is checked
* against what exists: the client's own contact, an active agent, a template not archived. One that takes other
* clients' contacts' or domains' email, or that Ignored Senders sets aside first, is saved, saying so.
* delete_routing_rule deletes it.
*/
defined('FROM_POST_HANDLER') || die("Direct file access is not allowed");
if (isset($_POST['add_routing_rule']) || isset($_POST['edit_routing_rule'])) {
validateCSRFToken();
if (!mailRouteReady()) {
flashAlert("Email routing needs the database update", 'error');
redirect('email_routing_rules.php');
}
$editing = isset($_POST['edit_routing_rule']);
$id = $editing ? intval($_POST['routing_rule_id'] ?? 0) : 0;
if ($editing && !mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM email_routing_rules WHERE routing_rule_id = $id"))) {
flashAlert("That routing rule isn't there any more", 'error');
redirect('email_routing_rules.php');
}
$rule = mailIgnoreClean($_POST['routing_rule_match'] ?? '');
if (!$rule) {
flashAlert("<strong>" . escapeHtml(trim((string) ($_POST['routing_rule_match'] ?? ''))) . "</strong> isn't an email address or a domain", 'error');
redirect();
}
$subject = mailRouteSubjectClean($_POST['routing_rule_subject'] ?? '');
$note = mb_substr(trim((string) ($_POST['routing_rule_note'] ?? '')), 0, 255);
// What it sets - each one that exists, else left as it is
$client_id = intval($_POST['routing_rule_client_id'] ?? 0);
$client_name = '';
if ($client_id) {
$client = mysqli_fetch_row(mysqli_query($mysqli, "SELECT client_name FROM clients WHERE client_id = $client_id AND client_archived_at IS NULL LIMIT 1"));
$client_id = $client ? $client_id : 0;
$client_name = $client ? (string) $client[0] : '';
}
$contact_id = $client_id ? intval($_POST['routing_rule_contact_id'] ?? 0) : 0;
if ($contact_id && !mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM contacts WHERE contact_id = $contact_id AND contact_client_id = $client_id AND contact_archived_at IS NULL LIMIT 1"))) {
$contact_id = 0;
}
$category_id = ticketCategoryId($_POST['routing_rule_category_id'] ?? 0);
$priority = in_array($_POST['routing_rule_priority'] ?? '', TICKET_PRIORITIES, true) ? (string) $_POST['routing_rule_priority'] : '';
$assigned_to = ticketAgentId($_POST['routing_rule_assigned_to'] ?? 0);
$template_id = intval($_POST['routing_rule_ticket_template_id'] ?? 0);
if ($template_id) {
$template = ticketTemplateRow($template_id);
$template_id = $template && empty($template['ticket_template_archived_at']) ? $template_id : 0;
}
if (!$client_id && !$category_id && $priority === '' && !$assigned_to && !$template_id) {
flashAlert("Pick what the rule sets - a client, a category, a priority, an agent or a template", 'error');
redirect();
}
$match_sql = escapeSql($rule['match']);
$subject_sql = escapeSql($subject);
if (mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM email_routing_rules WHERE routing_rule_match = '$match_sql' AND routing_rule_subject = '$subject_sql' AND routing_rule_id <> $id"))) {
flashAlert("There's a rule for <strong>" . escapeHtml($rule['match']) . "</strong>" . ($subject !== '' ? " with &ldquo;" . escapeHtml($subject) . "&rdquo; in the subject" : '') . " already", 'error');
redirect();
}
$set = "routing_rule_match = '$match_sql', routing_rule_subject = '$subject_sql', routing_rule_client_id = $client_id, routing_rule_contact_id = $contact_id,
routing_rule_category_id = $category_id, routing_rule_priority = '" . escapeSql($priority) . "', routing_rule_assigned_to = $assigned_to,
routing_rule_ticket_template_id = $template_id, routing_rule_note = '" . escapeSql($note) . "'";
if ($editing) {
mysqli_query($mysqli, "UPDATE email_routing_rules SET $set WHERE routing_rule_id = $id");
} else {
mysqli_query($mysqli, "INSERT INTO email_routing_rules SET $set, routing_rule_created_by = " . intval($session_user_id));
$id = intval(mysqli_insert_id($mysqli));
}
mailRouteRules(true);
$saved = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM email_routing_rules WHERE routing_rule_id = $id"));
$name = mailRouteName($saved);
$summary = mailRouteSummary(mailRouteTargets($saved));
// Said on saving: other clients' email it takes, and an ignored sender that takes it first
$warnings = [];
if ($client_id) {
$reach = mailRouteReachText(mailRouteReach($rule['match'], $client_id), $client_name);
if ($reach !== '') {
$warnings[] = $reach;
}
}
$ignored = mailIgnoreFind($rule['kind'] === 'domain' ? "someone@" . $rule['match'] : $rule['match']);
if ($ignored) {
$warnings[] = 'Ignored Senders sets aside email from ' . escapeHtml($ignored['ignored_sender_match']) . ' first, so this rule never routes it.';
}
logAudit("Settings", $editing ? "Edit" : "Create", escapeSql("$session_name " . ($editing ? 'changed' : 'added') . " the email routing rule for $name: $summary"));
flashAlert("New tickets by email from <strong>" . escapeHtml($name) . "</strong>: " . escapeHtml($summary) . ($warnings ? ' - ' . implode(' ', $warnings) : ''), $warnings ? 'warning' : 'success');
redirect();
}
if (isset($_GET['delete_routing_rule'])) {
validateCSRFToken();
$id = intval($_GET['delete_routing_rule']);
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM email_routing_rules WHERE routing_rule_id = $id"));
if (!$row) {
flashAlert("That routing rule isn't there any more", 'error');
redirect();
}
mysqli_query($mysqli, "DELETE FROM email_routing_rules WHERE routing_rule_id = $id");
$name = mailRouteName($row);
logAudit("Settings", "Delete", escapeSql("$session_name deleted the email routing rule for $name"));
flashAlert("Email from <strong>" . escapeHtml($name) . "</strong> isn't routed any more", 'error');
redirect();
}