Unprocessed * Email) goes where they said. * * A rule is an address - alerts@vendor.example - or a domain - vendor.example, which takes its subdomains too * (mail.vendor.example). Kept in Admin > Ignored Senders (admin/email_ignored_senders.php). Checked by * cron/ticket_email_parser.php (mailIgnoreFind()) and by Try an Email (functions/ticket_mail_try.php). */ /** Whether the 4.3.5 update has run - email_ignored_senders is there. */ function mailIgnoreReady() { 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.5', '>='); } return $ready; } /* * What was typed, as a rule - ['match' => the address or domain, lower case, 'kind' => address or domain] - or null * when it is neither. "@vendor.example" and "*@vendor.example" are the domain. */ function mailIgnoreClean($input) { $input = strtolower(trim((string) $input)); $input = preg_replace('/^\*?@/', '', $input); if (filter_var($input, FILTER_VALIDATE_EMAIL) && mb_strlen($input) <= 255) { return ['match' => $input, 'kind' => 'address']; } // Its last part letters, or an international one as mail servers write it (xn--p1ai) if (preg_match('/^(?=.{3,253}$)([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+(?:[a-z]{2,63}|xn--[a-z0-9-]{1,59})$/D', $input)) { return ['match' => $input, 'kind' => 'domain']; } return null; } /** Every rule - id => row - read once a run. $reload reads them again. */ function mailIgnoreRules($reload = false) { global $mysqli; static $rules = null; if ($rules === null || $reload) { $rules = []; if (mailIgnoreReady()) { $sql = mysqli_query($mysqli, "SELECT * FROM email_ignored_senders ORDER BY ignored_sender_match"); while ($sql && ($row = mysqli_fetch_assoc($sql))) { $rules[intval($row['ignored_sender_id'])] = $row; } } } return $rules; } /** Whether a rule (its match) takes $email - the address itself, or an address at the domain or one of its subdomains. */ function mailIgnoreMatches($match, $email) { $match = strtolower((string) $match); $email = strtolower(trim((string) $email)); if ($email === '' || $match === '') { return false; } if (strpos($match, '@') !== false) { return $email === $match; } $domain = substr($email, strrpos($email, '@') + 1); return $domain === $match || str_ends_with($domain, ".$match"); } /** The rule that takes email from $email - its row, an address rule before a domain one - or null. */ function mailIgnoreFind($email) { $found = null; foreach (mailIgnoreRules() as $rule) { if (mailIgnoreMatches($rule['ignored_sender_match'], $email)) { if (strpos($rule['ignored_sender_match'], '@') !== false) { return $rule; } $found = $found ?? $rule; } } return $found; } /** A rule set an email aside - counted, with when. */ function mailIgnoreHit($id) { global $mysqli; mysqli_query($mysqli, "UPDATE email_ignored_senders SET ignored_sender_matches = ignored_sender_matches + 1, ignored_sender_matched_at = NOW() WHERE ignored_sender_id = " . intval($id)); } /* * Who a rule would also take that you may not mean it to - ['contacts' => how many contacts have an address it takes, * 'clients' => the names of the clients whose domain it is]. For the warning on saving one. */ function mailIgnoreReach($match) { global $mysqli; $match = strtolower((string) $match); $out = ['contacts' => 0, 'clients' => []]; if ($match === '') { return $out; } $match_sql = escapeSql($match); if (strpos($match, '@') !== false) { $out['contacts'] = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM contacts WHERE contact_archived_at IS NULL AND LOWER(contact_email) = '$match_sql'"))[0] ?? 0); return $out; } $like = escapeSql(str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], $match)); $out['contacts'] = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM contacts WHERE contact_archived_at IS NULL AND (LOWER(contact_email) LIKE '%@$like' OR LOWER(contact_email) LIKE '%.$like')"))[0] ?? 0); $sql = mysqli_query($mysqli, "SELECT DISTINCT client_name FROM domains JOIN clients ON client_id = domain_client_id WHERE domain_archived_at IS NULL AND client_archived_at IS NULL AND (LOWER(domain_name) = '$match_sql' OR LOWER(domain_name) LIKE '%.$like') ORDER BY client_name LIMIT 5"); while ($sql && ($row = mysqli_fetch_row($sql))) { $out['clients'][] = (string) $row[0]; } return $out; } /* The warning for a rule that takes contacts' or clients' email - '' when it takes none. HTML. */ function mailIgnoreReachText(array $reach) { $parts = []; if ($reach['contacts']) { $parts[] = $reach['contacts'] . ' contact' . ($reach['contacts'] === 1 ? '' : 's'); } if ($reach['clients']) { $parts[] = 'the domain of ' . implode(', ', array_map('escapeHtml', $reach['clients'])); } return $parts ? 'It takes the email of ' . implode(' and ', $parts) . ' - their tickets and replies by email are set aside too.' : ''; } /* * Add a rule - $rule from mailIgnoreClean(), $note why (raw) - by $user_id. Its id: the one there already when it * is, as the match is kept once. */ function mailIgnoreAdd(array $rule, $note, $user_id) { global $mysqli; $match_sql = escapeSql($rule['match']); $there = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT ignored_sender_id FROM email_ignored_senders WHERE ignored_sender_match = '$match_sql'"))[0] ?? 0); if ($there) { return $there; } mysqli_query($mysqli, "INSERT INTO email_ignored_senders SET ignored_sender_match = '$match_sql', ignored_sender_note = '" . escapeSql(mb_substr(trim((string) $note), 0, 255)) . "', ignored_sender_created_by = " . intval($user_id)); $id = intval(mysqli_insert_id($mysqli)); mailIgnoreRules(true); return $id; }