Vendor templates stage 1: Admin > Vendor Templates rebuilt on the standard list with Active and Archived tabs, support line, hours and SLA, how many clients have a vendor from each, last used and changed by, sortable and searchable including by support line; new template page with what a vendor starts with, the clients with it and their account numbers, the license templates that use it, made and changed by, and history; shared New/Edit window on the vendor window's tabs with per-field push to the vendors made from it, each logged on the vendor; copy, archive, restore and delete with vendors and license templates unlinked on delete; Make a template from a client's vendor, linking the vendor to it, and the vendor page linking its template for admins; Delete offered to every admin role, not only the built-in Administrator; the admin handler no longer includes the agent vendor handler; database 3.3.4 adds vendor_template_created_by and vendor_template_updated_by, backfilled from the audit log

This commit is contained in:
johnnyq
2026-10-19 12:34:53 -04:00
parent a5a43e37ac
commit dfc708145d
36 changed files with 3514 additions and 780 deletions

188
functions/app_log.php Normal file
View File

@@ -0,0 +1,188 @@
<?php
/*
* ITFlow - the app logs, read: what ITFlow itself does and runs into - cron
* runs, mail going out and coming in, payment providers, the API, the AI
* providers, requests no handler took - written with logApp()
* (functions/logging.php). Admin > App Logs lists them (admin/app_logs.php)
* by level, searched and filtered by category and date, the same way they
* are exported and cleared (appLogFiltersSql()), each with a quick look
* (admin/modals/app_log/app_log.php). Nightly keeps them for their own number
* of days (Admin > Settings > Logs, appLogRetention()).
*/
/** The levels, worst first - each with its label, the word for one, icon and tone. */
function appLogLevels() {
return [
'error' => ['label' => 'Errors', 'one' => 'Error', 'icon' => 'fa-times-circle', 'tone' => 'danger', 'title' => 'Something failed'],
'warning' => ['label' => 'Warnings', 'one' => 'Warning', 'icon' => 'fa-exclamation-triangle', 'tone' => 'warning', 'title' => 'Something to look at, that carried on'],
'info' => ['label' => 'Info', 'one' => 'Info', 'icon' => 'fa-info-circle', 'tone' => 'info', 'title' => 'What was done - a cron run, mail sent'],
'debug' => ['label' => 'Debug', 'one' => 'Debug', 'icon' => 'fa-bug', 'tone' => 'secondary', 'title' => 'Detail for tracking a problem down'],
];
}
/** A level, as one of appLogLevels() - 'info' for anything else. */
function appLogLevel($level) {
$level = strtolower((string) $level);
return isset(appLogLevels()[$level]) ? $level : 'info';
}
/** A level as a soft pill, in its tone. */
function appLogLevelPill($level) {
$level = appLogLevel($level);
$style = appLogLevels()[$level];
$tone = $style['tone'];
return '<span class="badge rounded-pill bg-' . $tone . '-subtle text-' . $tone . '-emphasis border border-' . $tone . '-subtle fw-normal text-nowrap" data-app-log-level="' . $level . '">'
. '<i class="fas ' . $style['icon'] . ' me-1"></i>' . $style['one'] . '</span>';
}
/*
* A category's icon, as the full class - by its name, or the part before a
* dash (Cron-Mail-Queue is mail, Cron-Domain-Refresher a domain).
*/
function appLogCategoryIcon($category) {
$icons = [
'Cron-Email-Parser' => 'fa-inbox', 'Cron-Mail-Queue' => 'fa-paper-plane', 'Cron-Ticket-SLA' => 'fa-stopwatch',
'Cron-Domain-Refresher' => 'fa-globe', 'Cron-Certificate-Refresher' => 'fa-lock', 'Cron' => 'fa-clock',
'Stripe' => 'fa-credit-card', 'Mail' => 'fa-envelope', 'API' => 'fa-plug', 'AI' => 'fa-robot', 'Request' => 'fa-exchange-alt',
'Quote' => 'fa-comment-dollar', 'Invoice' => 'fa-file-invoice', 'Guest' => 'fa-user', 'Backup' => 'fa-cloud-upload-alt',
'Update' => 'fa-download', LOG_FORWARD_CATEGORY => 'fa-share-square', 'Database' => 'fa-database',
];
$category = (string) $category;
$icon = $icons[$category] ?? $icons[strtok($category, '-')] ?? 'fa-clipboard-list';
return "fas $icon";
}
/*
* The list's views - one a level, and All - key => label, icon, title and
* the SQL on app_logs that picks them.
*/
function appLogViews() {
$views = ['' => ['label' => 'All', 'icon' => 'fa-list', 'title' => 'Everything', 'where' => '', 'tone' => '']];
foreach (appLogLevels() as $level => $style) {
$views[$level] = ['label' => $style['label'], 'icon' => $style['icon'], 'title' => $style['title'], 'where' => "app_log_type = '$level'", 'tone' => $style['tone']];
}
return $views;
}
/*
* The app logs' filters as SQL on app_logs - from the list page's GET, or
* the export and clear windows' POST: q (the category or details), category,
* view (a level, appLogViews()) and dtf / dtt. $summary gets them in words,
* for an export or the audit log entry for a clear.
*/
function appLogFiltersSql(array $src, &$summary = []) {
$summary = [];
$sql = '1 = 1';
// One value each - anything else (a list in the query string) is no filter
$src = array_map(function ($value) {
return is_scalar($value) ? trim((string) $value) : '';
}, $src);
$q = $src['q'] ?? '';
if ($q !== '') {
$like = escapeSql($q);
$sql .= " AND (app_log_category LIKE '%$like%' OR app_log_details LIKE '%$like%')";
$summary['Search'] = $q;
}
$category = $src['category'] ?? '';
if ($category !== '') {
$sql .= " AND app_log_category = '" . escapeSql($category) . "'";
$summary['Category'] = $category;
}
$views = appLogViews();
$view = $src['view'] ?? '';
if (isset($views[$view]) && $views[$view]['where'] !== '') {
$sql .= " AND {$views[$view]['where']}";
$summary['Showing'] = $views[$view]['label'];
}
// A half-open range on the column itself, so KEY app_log_created_at serves it
$dtf = $src['dtf'] ?? '';
$dtt = $src['dtt'] ?? '';
$dtf = appLogDate($dtf) ? $dtf : '1970-01-01';
$dtt = appLogDate($dtt) ? $dtt : '2099-12-31';
if ($dtf !== '1970-01-01' || $dtt !== '2099-12-31') {
$sql .= " AND app_log_created_at >= '$dtf 00:00:00' AND app_log_created_at < '$dtt' + INTERVAL 1 DAY";
$range = formatExportDateRange($dtf, $dtt);
if ($range) {
$summary['Dated'] = $range;
}
}
return $sql;
}
/** The filters' summary (appLogFiltersSql()) in a phrase - "Errors from Stripe matching "card", 2026-10-01 to 2026-10-19". */
function appLogFiltersText(array $summary) {
$text = isset($summary['Showing']) ? $summary['Showing'] : 'Entries';
if (isset($summary['Category'])) {
$text .= ' from ' . $summary['Category'];
}
if (isset($summary['Search'])) {
$text .= ' matching "' . $summary['Search'] . '"';
}
if (isset($summary['Dated'])) {
$text .= ', ' . $summary['Dated'];
}
return $text;
}
/** Is this a real Y-m-d date? */
function appLogDate($value) {
$date = DateTime::createFromFormat('!Y-m-d', (string) $value);
return $date && $date->format('Y-m-d') === (string) $value;
}
/** The categories there are, with how many entries each has: name => count. */
function appLogCategories() {
global $mysqli;
$categories = [];
$sql = mysqli_query($mysqli, "SELECT app_log_category, COUNT(*) FROM app_logs WHERE app_log_category IS NOT NULL AND app_log_category <> '' GROUP BY app_log_category ORDER BY app_log_category ASC");
while ($row = mysqli_fetch_row($sql)) {
$categories[$row[0]] = intval($row[1]);
}
return $categories;
}
/*
* How many days each log is kept - 'audit' (and API key use) and 'app' - 0
* for keep forever. Read apart from the settings every page loads, so a
* page still opens before the update that adds the app logs' own.
*/
function appLogRetention() {
global $mysqli;
$row = [];
try {
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT config_log_retention, config_app_log_retention FROM settings WHERE company_id = 1")) ?: [];
} catch (Throwable $e) {
$result = mysqli_query($mysqli, "SELECT config_log_retention FROM settings WHERE company_id = 1");
$row = $result ? (mysqli_fetch_assoc($result) ?: []) : [];
$row['config_app_log_retention'] = $row['config_log_retention'] ?? 90;
}
return [
'audit' => max(0, intval($row['config_log_retention'] ?? 90)),
'app' => max(0, intval($row['config_app_log_retention'] ?? 90)),
];
}
/** A retention in words - "90 days", "Forever". */
function appLogRetentionText($days) {
$days = intval($days);
if ($days <= 0) {
return 'Forever';
}
if ($days % 365 === 0) {
return ($days / 365) . ' year' . ($days === 365 ? '' : 's');
}
return number_format($days) . ' day' . ($days === 1 ? '' : 's');
}
/** How much a log holds - its entries and the oldest one: ['count', 'oldest']. */
function appLogSize($table) {
global $mysqli;
$columns = ['logs' => 'log_created_at', 'app_logs' => 'app_log_created_at'];
if (!isset($columns[$table])) {
return ['count' => 0, 'oldest' => null];
}
$row = mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*), MIN({$columns[$table]}) FROM $table"));
return ['count' => intval($row[0] ?? 0), 'oldest' => $row[1] ?? null];
}

View File

@@ -57,6 +57,7 @@ function auditLogKinds() {
'Project Template' => ['icon' => 'fa-project-diagram', 'table' => 'project_templates', 'id' => 'project_template_id', 'name' => 'project_template_name', 'url' => '/admin/project_template.php?project_template_id={id}'],
'Document Template' => ['icon' => 'fa-file-alt', 'table' => 'document_templates', 'id' => 'document_template_id', 'name' => 'document_template_name', 'url' => '/admin/document_template.php?document_template_id={id}'],
'Software Template' => ['icon' => 'fa-cube', 'table' => 'software_templates', 'id' => 'software_template_id', 'name' => 'software_template_name', 'url' => '/admin/software_template.php?software_template_id={id}'],
'Vendor Template' => ['icon' => 'fa-building', 'table' => 'vendor_templates', 'id' => 'vendor_template_id', 'name' => 'vendor_template_name', 'url' => '/admin/vendor_template.php?vendor_template_id={id}'],
// Not one record to open, or nothing to open it with - just an icon
'Login' => ['icon' => 'fa-sign-in-alt'],
@@ -92,7 +93,7 @@ function auditLogKinds() {
'Reconciliation' => ['icon' => 'fa-balance-scale', 'module' => 'module_financial'],
'Budget' => ['icon' => 'fa-chart-pie', 'module' => 'module_financial'],
'Tax' => ['icon' => 'fa-percent'],
'Stripe' => ['icon' => 'fa-stripe-s', 'module' => 'module_financial'],
'Stripe' => ['icon' => 'fa-credit-card', 'module' => 'module_financial'],
'Payment Provider' => ['icon' => 'fa-credit-card'],
'Payment Method' => ['icon' => 'fa-credit-card', 'module' => 'module_financial'],
'Email' => ['icon' => 'fa-envelope'],
@@ -105,7 +106,6 @@ function auditLogKinds() {
'SLA' => ['icon' => 'fa-stopwatch'],
'Canned Response' => ['icon' => 'fa-comment-dots'],
'Contract Template' => ['icon' => 'fa-file-contract'],
'Vendor Template' => ['icon' => 'fa-building'],
'Custom Field' => ['icon' => 'fa-th-list'],
'Custom Link' => ['icon' => 'fa-external-link-alt'],
'AI Provider' => ['icon' => 'fa-robot'],
@@ -115,6 +115,7 @@ function auditLogKinds() {
'Report' => ['icon' => 'fa-chart-bar', 'module' => 'module_reporting'],
'Sharing' => ['icon' => 'fa-share-alt'],
'Audit Log' => ['icon' => 'fa-history'],
'App Log' => ['icon' => 'fa-clipboard-list'],
];
}
@@ -165,9 +166,9 @@ function auditLogActionPill($action) {
* them. Sign-ins are agents and portal contacts signing in and out; security
* is the sign-ins that failed or were turned away and the admins told about
* them, API calls turned away, access denied, the portal viewed as a contact,
* and changes to users, roles, API keys, the master key, settings and
* backups; data out is passwords looked at, exports, downloads and shared
* links.
* app log entries cleared, and changes to users, roles, API keys, the master
* key, settings and backups; data out is passwords looked at, exports,
* downloads and shared links.
*/
function auditLogViews() {
$signins = "log_type IN ('Login', 'Login 2FA', 'Logout', 'Client Login')";
@@ -177,8 +178,8 @@ function auditLogViews() {
'changes' => ['label' => 'Changes', 'icon' => 'fa-pen', 'title' => 'Records made, changed, archived, restored and deleted', 'where' => "NOT $signins AND NOT $data_out"],
'deletions' => ['label' => 'Deletions', 'icon' => 'fa-trash-alt', 'title' => 'Records deleted for good', 'where' => "log_action IN ('Delete', 'Bulk Delete')"],
'signins' => ['label' => 'Sign-ins', 'icon' => 'fa-sign-in-alt', 'title' => 'Agents and portal contacts signing in and out', 'where' => $signins],
'security' => ['label' => 'Security', 'icon' => 'fa-shield-alt', 'title' => 'Failed and turned away sign-ins and the alerts about them, API calls turned away, access denied, the portal viewed as a contact, and changes to users, roles, API keys, the master key, settings and backups',
'where' => "(($signins AND log_action IN ('Failed', 'MFA Failed', 'Blocked', 'Alert')) OR (log_type = 'API' AND log_action = 'Failed') OR log_action IN ('Denied', 'View As') OR log_type IN ('User', 'User Account', 'User Role', 'API Key', 'Master Key', 'Settings', 'Backup', 'Identity Provider'))"],
'security' => ['label' => 'Security', 'icon' => 'fa-shield-alt', 'title' => 'Failed and turned away sign-ins and the alerts about them, API calls turned away, access denied, the portal viewed as a contact, app logs cleared, and changes to users, roles, API keys, the master key, settings and backups',
'where' => "(($signins AND log_action IN ('Failed', 'MFA Failed', 'Blocked', 'Alert')) OR (log_type = 'API' AND log_action = 'Failed') OR log_action IN ('Denied', 'View As') OR (log_type = 'App Log' AND log_action = 'Delete') OR log_type IN ('User', 'User Account', 'User Role', 'API Key', 'Master Key', 'Settings', 'Backup', 'Identity Provider'))"],
'data' => ['label' => 'Data out', 'icon' => 'fa-file-export', 'title' => 'Passwords looked at, exports, downloads and shared links', 'where' => $data_out],
];
}

View File

@@ -494,6 +494,14 @@ function getExportColumns($export_type) {
'log_id' => ['label' => 'Entry', 'default' => false],
],
'app_logs' => [
'app_log_created_at' => ['label' => 'When'],
'app_log_type' => ['label' => 'Level'],
'app_log_category' => ['label' => 'Category'],
'app_log_details' => ['label' => 'Details', 'weight' => 4],
'app_log_id' => ['label' => 'Entry', 'default' => false],
],
];
return $registry[$export_type] ?? [];

588
functions/log_forward.php Normal file
View File

@@ -0,0 +1,588 @@
<?php
/*
* ITFlow - sending log entries on to a syslog server or SIEM as they are
* written (Admin > Settings > Logs). Each audit log entry - from logAudit()
* and the few places that write to logs themselves - and each app log entry
* at or above the level picked (logApp()) goes out as an RFC 5424 syslog
* message, over UDP, TCP or TLS, with the entry as JSON, CEF or plain text.
*
* Entries are queued as they are written (logForwardQueue()) and sent when
* the request ends (logForwardFlush()), over one connection - so an entry
* written in a transaction that is then rolled back is never sent, as it is
* no longer there to read. Sending never gets in the way: a server that
* can't be reached is tried once, the failure goes in the app log under Log
* Forwarding, and nothing more is sent for a minute, so a dead server doesn't
* slow every page down. The entries in between are only in ITFlow's own logs.
*/
const LOG_FORWARD_PROTOCOLS = ['udp' => 'UDP', 'tcp' => 'TCP', 'tls' => 'TLS'];
const LOG_FORWARD_FORMATS = ['json' => 'JSON', 'cef' => 'CEF', 'plain' => 'Plain text'];
const LOG_FORWARD_TIMEOUT = 2; // seconds to connect, or to write
const LOG_FORWARD_BACKOFF = 60; // seconds nothing is sent after a failure
const LOG_FORWARD_BATCH = 50; // entries queued before they are sent anyway, for a long cron run
const LOG_FORWARD_FACILITY = 16; // local0
const LOG_FORWARD_CATEGORY = 'Log Forwarding';
/** Which app log entries go out - the level and the ones above it, '' for none. */
function logForwardAppLevels() {
return [
'' => 'None',
'error' => 'Errors',
'warning' => 'Warnings and errors',
'info' => 'Info, warnings and errors',
'debug' => 'Everything, debug too',
];
}
/** What each protocol is, in a line. */
function logForwardProtocolHelp() {
return [
'udp' => 'Fast, and nothing waits for the server - but nothing says whether it got there.',
'tcp' => 'Each entry arrives, in order, one a line.',
'tls' => 'TCP, encrypted - for a server outside your network.',
];
}
/** What each format is, in a line. */
function logForwardFormatHelp() {
return [
'json' => 'One JSON object an entry - Graylog, Splunk, Elastic, Wazuh and most others read it as it is.',
'cef' => 'Common Event Format - for Microsoft Sentinel, QRadar, ArcSight and other SIEMs that take it.',
'plain' => 'A line of text an entry, as a person would read it.',
];
}
/*
* The forwarding settings, tidied: enabled, host, port, protocol, format,
* audit, app_level and tls_verify. Read once a request, apart from the
* settings every page loads, so a page still opens before the update that
* adds them - all off until then. $fresh reads them again, after a save.
*/
function logForwardSettings($fresh = false) {
global $mysqli;
static $settings = null;
if ($settings !== null && !$fresh) {
return $settings;
}
$row = [];
try {
$result = mysqli_query($mysqli, "SELECT config_log_forward_enabled, config_log_forward_host, config_log_forward_port, config_log_forward_protocol,
config_log_forward_format, config_log_forward_audit, config_log_forward_app_level, config_log_forward_tls_verify
FROM settings WHERE company_id = 1");
$row = $result ? (mysqli_fetch_assoc($result) ?: []) : [];
} catch (Throwable $e) {
$row = [];
}
$settings = logForwardTidy([
'enabled' => $row['config_log_forward_enabled'] ?? 0,
'host' => $row['config_log_forward_host'] ?? '',
'port' => $row['config_log_forward_port'] ?? 514,
'protocol' => $row['config_log_forward_protocol'] ?? 'udp',
'format' => $row['config_log_forward_format'] ?? 'json',
'audit' => $row['config_log_forward_audit'] ?? 1,
'app_level' => $row['config_log_forward_app_level'] ?? 'warning',
'tls_verify' => $row['config_log_forward_tls_verify'] ?? 1,
]);
return $settings;
}
/** Settings from the database or the form, each made one of what it can be. */
function logForwardTidy(array $src) {
// One value each - a list from a form is nothing
$src = array_map(function ($value) {
return is_scalar($value) || $value === null ? $value : '';
}, $src);
$host = trim((string) ($src['host'] ?? ''));
$port = intval($src['port'] ?? 514);
// A name or IPv4 address typed with its port - logs.example.com:1514 - is the two
if (preg_match('/^([A-Za-z0-9._-]+):(\d{1,5})$/', $host, $match)) {
$host = $match[1];
$port = intval($match[2]);
}
// A name or an address, IPv6 in brackets or not - nothing that could break out of the address it is put in
if (!preg_match('/^\[?[A-Za-z0-9.:_-]{1,200}\]?$/', $host)) {
$host = '';
}
$protocol = (string) ($src['protocol'] ?? 'udp');
$format = (string) ($src['format'] ?? 'json');
$app_level = (string) ($src['app_level'] ?? '');
return [
'enabled' => intval($src['enabled'] ?? 0) ? 1 : 0,
'host' => trim($host, '[]'),
'port' => $port >= 1 && $port <= 65535 ? $port : 514,
'protocol' => isset(LOG_FORWARD_PROTOCOLS[$protocol]) ? $protocol : 'udp',
'format' => isset(LOG_FORWARD_FORMATS[$format]) ? $format : 'json',
'audit' => intval($src['audit'] ?? 0) ? 1 : 0,
'app_level' => isset(logForwardAppLevels()[$app_level]) ? $app_level : '',
'tls_verify' => intval($src['tls_verify'] ?? 0) ? 1 : 0,
];
}
/** Is anything being sent? */
function logForwardOn(?array $settings = null) {
$settings = $settings ?? logForwardSettings();
return $settings['enabled'] && $settings['host'] !== '' && ($settings['audit'] || $settings['app_level'] !== '');
}
/** Where they go, in words - "logs.example.com:514 over UDP". */
function logForwardTarget(array $settings) {
$host = strpos($settings['host'], ':') !== false ? '[' . $settings['host'] . ']' : $settings['host'];
return $host . ':' . $settings['port'] . ' over ' . LOG_FORWARD_PROTOCOLS[$settings['protocol']];
}
/** Does an app log entry at this level go out, by the setting? */
function logForwardAppLevelSent($level, $threshold) {
$order = ['error' => 1, 'warning' => 2, 'info' => 3, 'debug' => 4];
$level = strtolower((string) $level);
return $threshold !== '' && isset($order[$level], $order[$threshold]) && $order[$level] <= $order[$threshold];
}
/*
* The entries waiting to go, by log - 'audit' and 'app' ids. Called with
* ['audit', 12] it adds one; with false it hands them over and starts again.
*/
function logForwardPending($add = null) {
static $pending = ['audit' => [], 'app' => []];
if ($add === false) {
$taken = $pending;
$pending = ['audit' => [], 'app' => []];
return $taken;
}
if (is_array($add)) {
$pending[$add[0]][] = intval($add[1]);
}
return $pending;
}
/*
* An entry just written, to send - 'audit' with its log_id, or 'app' with its
* app_log_id, level and category. Nothing when forwarding is off, for that
* log or level, for the forwarding's own failures, or for an email preview,
* whose sample records are thrown away again (functions/email_preview.php).
*/
function logForwardQueue($log, $id, $level = '', $category = '') {
static $registered = false;
$id = intval($id);
if ($id <= 0 || (function_exists('emailCapturing') && emailCapturing())) {
return;
}
$settings = logForwardSettings();
if (!logForwardOn($settings)) {
return;
}
if ($log === 'audit' && !$settings['audit']) {
return;
}
if ($log === 'app' && ((string) $category === LOG_FORWARD_CATEGORY || !logForwardAppLevelSent($level, $settings['app_level']))) {
return;
}
if ($log !== 'audit' && $log !== 'app') {
return;
}
logForwardPending([$log, $id]);
if (!$registered) {
$registered = true;
register_shutdown_function('logForwardFlush');
}
$pending = logForwardPending();
if (count($pending['audit']) + count($pending['app']) >= LOG_FORWARD_BATCH) {
logForwardFlush();
}
}
/*
* Sends what is waiting - read back from the database, so it is what was
* kept - over one connection, kept open for the rest of the request. Runs
* when the request ends, and on the way for a long cron run. Never throws.
*/
function logForwardFlush() {
global $mysqli;
$pending = logForwardPending(false);
if (!$pending['audit'] && !$pending['app']) {
return;
}
try {
$settings = logForwardSettings();
if (!logForwardOn($settings) || logForwardBackingOff($settings)) {
return;
}
$messages = [];
if ($pending['audit']) {
$sql = mysqli_query($mysqli, "SELECT log_id, log_type, log_action, log_description, log_ip, log_user_agent, log_created_at, log_client_id, log_user_id, log_entity_id,
user_name, user_email, user_type, client_name
FROM logs LEFT JOIN users ON user_id = log_user_id AND log_user_id > 0 LEFT JOIN clients ON client_id = log_client_id AND log_client_id > 0
WHERE log_id IN (" . implode(',', array_unique($pending['audit'])) . ") ORDER BY log_id");
while ($row = mysqli_fetch_assoc($sql)) {
$messages[] = [strtotime($row['log_created_at']), logForwardAuditMessage($row, $settings['format'])];
}
}
if ($pending['app']) {
$sql = mysqli_query($mysqli, "SELECT app_log_id, app_log_category, app_log_type, app_log_details, app_log_created_at FROM app_logs
WHERE app_log_id IN (" . implode(',', array_unique($pending['app'])) . ") ORDER BY app_log_id");
while ($row = mysqli_fetch_assoc($sql)) {
$messages[] = [strtotime($row['app_log_created_at']), logForwardAppMessage($row, $settings['format'])];
}
}
if (!$messages) {
return;
}
// In the order they happened, the two logs together
usort($messages, function ($a, $b) {
return $a[0] <=> $b[0];
});
$error = '';
if (!logForwardSend($settings, array_column($messages, 1), $error)) {
logForwardFailed($settings, count($messages), $error);
}
} catch (Throwable $e) {
error_log('ITFlow log forwarding: ' . $e->getMessage());
}
}
/*
* Sends lines over the connection this request keeps open to the server,
* opening it the first time - true when every one was written. TCP and TLS
* end each with a newline (RFC 6587 non-transparent framing); UDP sends one
* datagram each. $keep false closes it after, for a one-off like the test.
*/
function logForwardSend(array $settings, array $lines, &$error = '', $keep = true) {
static $socket = null;
static $socket_target = '';
$error = '';
$target = logForwardTarget($settings) . ($settings['protocol'] === 'tls' ? ($settings['tls_verify'] ? ' verified' : ' unverified') : '');
if ($socket && ($socket_target !== $target || !$keep)) {
@fclose($socket);
$socket = null;
}
if (!$socket) {
$socket = logForwardConnect($settings, $error);
$socket_target = $target;
if (!$socket) {
return false;
}
}
foreach ($lines as $line) {
$data = $settings['protocol'] === 'udp' ? $line : $line . "\n";
if (!logForwardWrite($socket, $data, $error)) {
@fclose($socket);
$socket = null;
return false;
}
}
if (!$keep) {
// A UDP port nothing listens on answers with "port unreachable", which only shows on the next
// read - it fails at once, where a port something listens on just has nothing to read in time
if ($settings['protocol'] === 'udp') {
stream_set_timeout($socket, 0, 300000);
$read = @fread($socket, 1);
if ($read === false && empty(stream_get_meta_data($socket)['timed_out'])) {
$error = 'the server said nothing is listening on that port';
@fclose($socket);
$socket = null;
return false;
}
}
@fclose($socket);
$socket = null;
}
return true;
}
/** Opens the connection - null, with why in $error, when it can't. */
function logForwardConnect(array $settings, &$error) {
$host = strpos($settings['host'], ':') !== false ? '[' . $settings['host'] . ']' : $settings['host'];
$options = [];
if ($settings['protocol'] === 'tls') {
$verify = (bool) $settings['tls_verify'];
$options['ssl'] = [
'verify_peer' => $verify,
'verify_peer_name' => $verify,
'allow_self_signed' => !$verify,
'peer_name' => $settings['host'],
'SNI_enabled' => true,
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT | (defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT') ? STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT : 0),
];
}
// A TLS failure says why only in the warnings on the way, not in $errstr - so they are kept
$warnings = [];
set_error_handler(function ($severity, $message) use (&$warnings) {
$warnings[] = $message;
return true;
});
$errno = 0;
$errstr = '';
try {
$socket = stream_socket_client($settings['protocol'] . "://$host:" . $settings['port'], $errno, $errstr, LOG_FORWARD_TIMEOUT, STREAM_CLIENT_CONNECT, stream_context_create($options));
} finally {
restore_error_handler();
}
if (!$socket) {
$error = logForwardReason(array_merge($warnings, [$errstr]));
return null;
}
stream_set_timeout($socket, LOG_FORWARD_TIMEOUT);
return $socket;
}
/** Writes all of it - false, with why in $error, when it can't. */
function logForwardWrite($socket, $data, &$error) {
$length = strlen($data);
$written = 0;
while ($written < $length) {
error_clear_last();
$sent = @fwrite($socket, substr($data, $written));
if ($sent === false || $sent === 0) {
$meta = stream_get_meta_data($socket);
$error = !empty($meta['timed_out']) ? 'the server stopped taking them' : logForwardReason([error_get_last()['message'] ?? '', 'the connection was closed']);
return false;
}
$written += $sent;
}
return true;
}
/*
* Why a connection or write failed, in words - from the messages PHP gave on
* the way (one, or several), without its own wording around them.
*/
function logForwardReason($messages) {
$messages = array_values(array_filter(array_map(function ($message) {
$message = trim(preg_replace('/^.*?(stream_socket_client|fwrite|fread)\(\):\s*/', '', (string) $message));
return trim(preg_replace('/^Unable to connect to \S+ \((.*)\)$/', '$1', $message));
}, (array) $messages), function ($message) {
return $message !== '' && strcasecmp($message, 'Unknown error') !== 0;
}));
$all = implode(' ', $messages);
if (stripos($all, 'certificate verify failed') !== false) {
return "the server's certificate couldn't be checked - it isn't from an authority this server trusts, has run out, or is for another name";
}
if (stripos($all, 'did not match expected CN') !== false || stripos($all, 'peer certificate CN') !== false) {
return "the server's certificate is for another name";
}
if (stripos($all, 'getaddrinfo') !== false || stripos($all, 'php_network_getaddresses') !== false) {
return "that server's name couldn't be found";
}
if (stripos($all, 'Connection refused') !== false) {
return 'the server refused the connection - nothing is listening on that port';
}
if (stripos($all, 'timed out') !== false) {
return 'the server did not answer in ' . LOG_FORWARD_TIMEOUT . ' seconds';
}
if (stripos($all, 'crypto') !== false || stripos($all, 'SSL') !== false) {
return "it couldn't start TLS with the server - " . ($messages[0] ?? 'it may not take TLS on that port');
}
return $messages[0] ?? 'it could not be reached';
}
/** The file that says when sending last failed, so the minute after is skipped. */
function logForwardBackoffFile(array $settings) {
return rtrim(sys_get_temp_dir(), '/\\') . '/itflow-log-forward-' . substr(md5(__DIR__ . '|' . logForwardTarget($settings)), 0, 16);
}
/** Did sending to this server fail in the last minute? */
function logForwardBackingOff(array $settings) {
$file = logForwardBackoffFile($settings);
$at = @filemtime($file);
return $at !== false && $at > time() - LOG_FORWARD_BACKOFF;
}
/** Sending failed - wait a minute before trying again, and say so in the app log. */
function logForwardFailed(array $settings, $count, $error) {
@touch(logForwardBackoffFile($settings));
$what = $count === 1 ? '1 log entry' : "$count log entries";
logApp(LOG_FORWARD_CATEGORY, 'error', "Couldn't send $what to " . logForwardTarget($settings) . " - $error. Nothing more is sent for a minute; the entries are still here.");
}
/** Sending works again - forget the last failure. */
function logForwardRecovered(array $settings) {
@unlink(logForwardBackoffFile($settings));
}
/*
* ---------- the messages ----------
*/
/** How serious an audit log entry is, as a syslog severity: a failure or something turned away is a warning, a deletion a notice, the rest information. */
function logForwardAuditSeverity($action) {
if (in_array((string) $action, ['Failed', 'MFA Failed', 'Failure', 'Blocked', 'Denied', 'Alert', 'Error'], true)) {
return 4;
}
if (in_array((string) $action, ['Delete', 'Bulk Delete', 'Revoke', 'Discard'], true)) {
return 5;
}
return 6;
}
/** An app log level as a syslog severity. */
function logForwardAppSeverity($level) {
return ['error' => 3, 'warning' => 4, 'info' => 6, 'debug' => 7][strtolower((string) $level)] ?? 6;
}
/** The name ITFlow sends as - its address, else the server's name. */
function logForwardHostname() {
global $config_base_url;
$host = (string) parse_url('https://' . preg_replace('#^https?://#i', '', (string) ($config_base_url ?? '')), PHP_URL_HOST);
if ($host === '') {
$host = (string) gethostname();
}
$host = preg_replace('/[^\x21-\x7e]/', '', $host);
return $host !== '' ? substr($host, 0, 255) : '-';
}
/** A time from the database as RFC 3339, in ITFlow's timezone. */
function logForwardTime($datetime) {
try {
return (new DateTime((string) $datetime))->format('Y-m-d\TH:i:sP');
} catch (Throwable $e) {
return date('Y-m-d\TH:i:sP');
}
}
/** The RFC 5424 line around a message. */
function logForwardSyslog($severity, $msgid, $time, $message) {
$message = str_replace(["\r\n", "\r", "\n"], ' ', $message);
return '<' . (LOG_FORWARD_FACILITY * 8 + $severity) . '>1 ' . logForwardTime($time) . ' ' . logForwardHostname() . " itflow - $msgid - $message";
}
/** A CEF header field - | and \ escaped. */
function logForwardCefHeader($value) {
return str_replace(['\\', '|', "\r", "\n"], ['\\\\', '\\|', ' ', ' '], (string) $value);
}
/** CEF extensions, from key => value, the empty ones left out - \ and = escaped, line breaks as \n. */
function logForwardCefExtensions(array $fields) {
$out = [];
foreach ($fields as $key => $value) {
if ($value === null || $value === '') {
continue;
}
$out[] = $key . '=' . str_replace(['\\', '=', "\r\n", "\n", "\r"], ['\\\\', '\\=', '\\n', '\\n', '\\n'], (string) $value);
}
return implode(' ', $out);
}
/** CEF's severity, 0 to 10, from a syslog one. */
function logForwardCefSeverity($severity) {
return [0 => 10, 1 => 10, 2 => 9, 3 => 8, 4 => 6, 5 => 4, 6 => 3, 7 => 1][$severity] ?? 3;
}
/** The start of a CEF message. */
function logForwardCefStart($signature, $name, $severity) {
require_once __DIR__ . '/../includes/app_version.php';
return 'CEF:0|ITFlow|ITFlow|' . logForwardCefHeader(defined('APP_VERSION') ? APP_VERSION : '') . '|' . logForwardCefHeader($signature) . '|' . logForwardCefHeader($name) . '|' . logForwardCefSeverity($severity) . '|';
}
/** An audit log entry (with user_name, user_email, user_type and client_name) as a syslog line. */
function logForwardAuditMessage(array $row, $format) {
$severity = logForwardAuditSeverity($row['log_action']);
$user_id = intval($row['log_user_id']);
$who = $user_id ? (string) ($row['user_name'] ?? '') : 'ITFlow';
$who_kind = !$user_id ? 'system' : (intval($row['user_type'] ?? 1) === 2 ? 'portal' : 'agent');
$ip = trim((string) $row['log_ip']);
if ($format === 'cef') {
$message = logForwardCefStart($row['log_type'] . ':' . $row['log_action'], $row['log_type'] . ' ' . $row['log_action'], $severity) . logForwardCefExtensions([
'rt' => strtotime($row['log_created_at']) * 1000,
'externalId' => intval($row['log_id']),
'cat' => $row['log_type'],
'act' => $row['log_action'],
'msg' => trim((string) $row['log_description']),
'suid' => $user_id ?: null,
'suser' => $who,
'src' => filter_var($ip, FILTER_VALIDATE_IP) ? $ip : null,
'requestClientApplication' => $row['log_user_agent'],
'cs1Label' => $row['client_name'] !== null ? 'Client' : null,
'cs1' => $row['client_name'],
'cn1Label' => intval($row['log_client_id']) ? 'Client ID' : null,
'cn1' => intval($row['log_client_id']) ?: null,
'cn2Label' => intval($row['log_entity_id']) ? 'Record ID' : null,
'cn2' => intval($row['log_entity_id']) ?: null,
'cs2Label' => 'Who',
'cs2' => $who_kind,
'dvchost' => logForwardHostname(),
]);
} elseif ($format === 'plain') {
$details = array_filter([
$user_id ? "by $who" . ($who_kind === 'portal' ? ' (portal)' : '') : 'by ITFlow',
$row['client_name'] !== null ? 'for ' . $row['client_name'] : '',
$ip !== '' ? "from $ip" : '',
'entry ' . intval($row['log_id']),
]);
$message = $row['log_type'] . ' ' . $row['log_action'] . ': ' . trim((string) $row['log_description']) . ' (' . implode(', ', $details) . ')';
} else {
$message = json_encode([
'log' => 'audit',
'id' => intval($row['log_id']),
'time' => logForwardTime($row['log_created_at']),
'type' => (string) $row['log_type'],
'action' => (string) $row['log_action'],
'description' => trim((string) $row['log_description']),
'severity' => ['', '', '', 'error', 'warning', 'notice', 'info'][$severity],
'who' => $who_kind,
'user_id' => $user_id,
'user' => $who,
'user_email' => $user_id ? (string) ($row['user_email'] ?? '') : '',
'client_id' => intval($row['log_client_id']),
'client' => (string) ($row['client_name'] ?? ''),
'record_id' => intval($row['log_entity_id']),
'ip' => $ip,
'user_agent' => (string) $row['log_user_agent'],
'host' => logForwardHostname(),
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
}
return logForwardSyslog($severity, 'audit', $row['log_created_at'], $message);
}
/** An app log entry as a syslog line. */
function logForwardAppMessage(array $row, $format) {
$level = strtolower((string) $row['app_log_type']);
$severity = logForwardAppSeverity($level);
$category = (string) $row['app_log_category'];
if ($format === 'cef') {
$message = logForwardCefStart('app:' . $category, $category . ' ' . $level, $severity) . logForwardCefExtensions([
'rt' => strtotime($row['app_log_created_at']) * 1000,
'externalId' => intval($row['app_log_id']),
'cat' => $category,
'msg' => trim((string) $row['app_log_details']),
'cs1Label' => 'Level',
'cs1' => $level,
'dvchost' => logForwardHostname(),
]);
} elseif ($format === 'plain') {
$message = "$level $category: " . trim((string) $row['app_log_details']) . ' (app log entry ' . intval($row['app_log_id']) . ')';
} else {
$message = json_encode([
'log' => 'app',
'id' => intval($row['app_log_id']),
'time' => logForwardTime($row['app_log_created_at']),
'level' => $level,
'category' => $category,
'details' => trim((string) $row['app_log_details']),
'host' => logForwardHostname(),
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
}
return logForwardSyslog($severity, 'app', $row['app_log_created_at'], $message);
}
/*
* Sends one test message with these settings, now, and says how it went:
* ['ok', 'error']. Over UDP it can only say a closed port was turned away -
* the message going out is no sign it arrived.
*/
function logForwardTest(array $settings, $by = '') {
$now = date('Y-m-d H:i:s');
$text = 'Test message from ITFlow' . ($by !== '' ? " - sent by $by" : '') . ' from Admin > Settings > Logs';
if ($settings['format'] === 'cef') {
$message = logForwardCefStart('test', 'Test message', 6) . logForwardCefExtensions(['rt' => time() * 1000, 'msg' => $text, 'dvchost' => logForwardHostname()]);
} elseif ($settings['format'] === 'plain') {
$message = $text;
} else {
$message = json_encode(['log' => 'test', 'time' => logForwardTime($now), 'message' => $text, 'host' => logForwardHostname()], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
}
$error = '';
$ok = logForwardSend($settings, [logForwardSyslog(6, 'test', $now, $message)], $error, false);
if ($ok) {
logForwardRecovered($settings);
}
return ['ok' => $ok, 'error' => $error];
}

View File

@@ -86,6 +86,9 @@ function logAudit($type, $action, $description, $client_id = 0, $entity_id = 0)
}
mysqli_query($mysqli, "INSERT INTO logs SET log_type = '$type', log_action = '$action', log_description = '$description', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $entity_id");
// On to a syslog server or SIEM, when that is set up (functions/log_forward.php)
logForwardQueue('audit', mysqli_insert_id($mysqli));
}
/*
@@ -164,11 +167,29 @@ function logHistory($status, $description, $invoice_id = 0, $quote_id = 0, $recu
mysqli_query($mysqli, "INSERT INTO history SET history_status = '$status', history_description = '$description', history_invoice_id = $invoice_id, history_quote_id = $quote_id, history_recurring_invoice_id = $recurring_invoice_id");
}
/*
* Writes an app log entry - what ITFlow itself does: cron runs, mail, payment
* providers, the API, errors (Admin > App Logs, functions/app_log.php).
* Unlike logAudit(), it takes plain text and escapes it here. $type is the
* level - info, warning, error or debug; anything else is put to the nearest,
* as the column takes only those.
*/
function logApp($category, $type, $details) {
global $mysqli;
$category = mysqli_real_escape_string($mysqli, substr($category, 0, 200));
$details = mysqli_real_escape_string($mysqli, substr($details, 0, 1000));
$type = strtolower(trim((string) $type));
if (!in_array($type, ['info', 'warning', 'error', 'debug'], true)) {
$type = in_array($type, ['critical', 'fatal', 'alert', 'emergency'], true) ? 'error' : ($type === 'warn' ? 'warning' : 'info');
}
mysqli_query($mysqli, "INSERT INTO app_logs SET app_log_category = '$category', app_log_type = '$type', app_log_details = '$details'");
// Cut by character, so a name or message with accents isn't cut through the middle of one
$category = mb_substr((string) $category, 0, 200);
$details = mb_substr((string) $details, 0, 1000);
$category_sql = mysqli_real_escape_string($mysqli, $category);
$details_sql = mysqli_real_escape_string($mysqli, $details);
mysqli_query($mysqli, "INSERT INTO app_logs SET app_log_category = '$category_sql', app_log_type = '$type', app_log_details = '$details_sql'");
// On to a syslog server or SIEM, from the level picked (functions/log_forward.php)
logForwardQueue('app', mysqli_insert_id($mysqli), $type, $category);
}

View File

@@ -190,6 +190,15 @@ function reportCatalog() {
],
],
],
'activity' => [
'label' => 'Activity',
'reports' => [
'audit_activity' => [
'title' => 'Audit Activity', 'icon' => 'fa-history', 'module' => 'module_client', 'feature' => [],
'description' => 'What was done in ITFlow - changes, deletions, passwords looked at, exports and sign-ins - over time, out of hours, and by person, client and kind of record.',
],
],
],
];
}

View File

@@ -0,0 +1,321 @@
<?php
/*
* ITFlow - vendor templates (Admin > Vendor Templates): the vendors an MSP's
* clients use again and again - the ISPs, the hardware makers, the
* distributors, the line-of-business software - set up once with their
* support line, hours, SLA, support email and site, ready to add to any
* client. A vendor made from one gets its fields and remembers which template
* it came from (vendors.vendor_template_id), so each template's use is
* counted: how many clients have a vendor from it, and when one was last
* added. License templates take their vendor from one
* (software_templates.software_template_vendor_template_id).
*
* From database 3.3.4 on a template says who made it and last changed it.
*
* Shared by admin/vendor_templates.php, admin/vendor_template.php,
* admin/modals/vendor_template/* and admin/post/vendor_template.php. A
* vendor is made from one on the agent side by add_vendor,
* add_vendor_from_template and add_vendor_template_to_clients
* (agent/post/vendor.php, with vendorTemplateRow() and
* vendorTemplateFormFields() in functions/vendor.php).
*/
/*
* What a template carries, as it shows on its page and in the window - key
* => label and icon, in order. 'phone' is the support line with its country
* code and extension.
*/
function vendorTemplateFieldLabels() {
return [
'name' => ['label' => 'Name', 'icon' => 'fa-building'],
'description' => ['label' => 'What they are', 'icon' => 'fa-align-left'],
'phone' => ['label' => 'Support line', 'icon' => 'fa-phone'],
'hours' => ['label' => 'Support hours', 'icon' => 'fa-clock'],
'sla' => ['label' => 'SLA', 'icon' => 'fa-handshake'],
'email' => ['label' => 'Support email', 'icon' => 'fa-envelope'],
'website' => ['label' => 'Support site', 'icon' => 'fa-globe'],
'account_number' => ['label' => 'Account number', 'icon' => 'fa-fingerprint'],
'code' => ['label' => 'PIN', 'icon' => 'fa-key'],
'contact_name' => ['label' => 'Account manager', 'icon' => 'fa-user-tie'],
'notes' => ['label' => 'Notes', 'icon' => 'fa-sticky-note'],
];
}
/*
* Each field's columns - on the template, and on a vendor made from it. The
* support line is three: country code, number and extension.
*/
function vendorTemplateColumns() {
return [
'name' => [['vendor_template_name', 'vendor_name']],
'description' => [['vendor_template_description', 'vendor_description']],
'phone' => [['vendor_template_phone_country_code', 'vendor_phone_country_code'], ['vendor_template_phone', 'vendor_phone'], ['vendor_template_extension', 'vendor_extension']],
'hours' => [['vendor_template_hours', 'vendor_hours']],
'sla' => [['vendor_template_sla', 'vendor_sla']],
'email' => [['vendor_template_email', 'vendor_email']],
'website' => [['vendor_template_website', 'vendor_website']],
'account_number' => [['vendor_template_account_number', 'vendor_account_number']],
'code' => [['vendor_template_code', 'vendor_code']],
'contact_name' => [['vendor_template_contact_name', 'vendor_contact_name']],
'notes' => [['vendor_template_notes', 'vendor_notes']],
];
}
/** One template, archived or not, with who made it and last changed it - raw, or null. */
function vendorTemplateInfo($vendor_template_id) {
global $mysqli;
$vendor_template_id = intval($vendor_template_id);
if ($vendor_template_id <= 0) {
return null;
}
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT vendor_templates.*, creator.user_name AS created_by_name, updater.user_name AS updated_by_name
FROM vendor_templates
LEFT JOIN users creator ON creator.user_id = vendor_template_created_by AND vendor_template_created_by > 0
LEFT JOIN users updater ON updater.user_id = vendor_template_updated_by AND vendor_template_updated_by > 0
WHERE vendor_template_id = $vendor_template_id"));
return $row ?: null;
}
/*
* How the templates are used - template id => clients (current clients with a
* current vendor from it), vendors (those vendors), ours (our own vendors from
* it) and last (when the latest was added). Ones not used aren't in it.
*/
function vendorTemplateUsage(array $vendor_template_ids) {
global $mysqli;
$ids = array_values(array_filter(array_map('intval', $vendor_template_ids)));
$usage = [];
if (!$ids) {
return $usage;
}
$sql = mysqli_query($mysqli, "SELECT vendor_template_id, COUNT(DISTINCT NULLIF(vendor_client_id, 0)) AS clients, SUM(vendor_client_id > 0) AS vendors,
SUM(vendor_client_id = 0) AS ours, MAX(vendor_created_at) AS last
FROM vendors LEFT JOIN clients ON client_id = vendor_client_id AND vendor_client_id > 0
WHERE vendor_template_id IN (" . implode(',', $ids) . ") AND vendor_archived_at IS NULL AND (vendor_client_id = 0 OR client_archived_at IS NULL AND client_id IS NOT NULL)
GROUP BY vendor_template_id");
while ($row = mysqli_fetch_assoc($sql)) {
$usage[intval($row['vendor_template_id'])] = ['clients' => intval($row['clients']), 'vendors' => intval($row['vendors']), 'ours' => intval($row['ours']), 'last' => $row['last']];
}
return $usage;
}
/*
* The window's fields, checked - or null with $error saying why. Raw values:
* name, description, phone_country_code, phone and extension (digits),
* hours, sla, email, website (without its scheme), account_number, code,
* contact_name and notes - as vendorFormSet() reads a vendor's.
*/
function vendorTemplateFields(array $post, &$error = '') {
$error = '';
$line = function ($key, $length = 200) use ($post) {
$value = is_scalar($post[$key] ?? null) ? trim(preg_replace('/\s+/u', ' ', (string) $post[$key])) : '';
return mb_substr($value, 0, $length);
};
$digits = function ($key, $length = 200) use ($post) {
return substr(preg_replace('/[^0-9]/', '', is_scalar($post[$key] ?? null) ? (string) $post[$key] : ''), 0, $length);
};
$name = is_scalar($post['name'] ?? null) ? trim(preg_replace('/\s+/u', ' ', (string) $post['name'])) : '';
if ($name === '') {
$error = 'A vendor template needs a name';
return null;
}
if (mb_strlen($name) > 200) {
$error = 'Keep the name to 200 characters';
return null;
}
$email = $line('email');
if ($email !== '' && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = "The support email isn't an email address";
return null;
}
$phone = $digits('phone');
return [
'name' => $name,
'description' => $line('description'),
'phone_country_code' => $phone !== '' ? $digits('phone_country_code', 10) : '',
'phone' => $phone,
'extension' => $digits('extension'),
'hours' => $line('hours'),
'sla' => $line('sla'),
'email' => $email,
'website' => mb_substr(rtrim(preg_replace('#^https?://#i', '', $line('website')), '/'), 0, 200),
'account_number' => $line('account_number'),
'code' => $line('code'),
'contact_name' => $line('contact_name'),
'notes' => is_scalar($post['notes'] ?? null) ? trim((string) $post['notes']) : '',
];
}
/** A template's fields as vendorTemplateFields() gives them - from its row, for comparing and copying. */
function vendorTemplateRowFields(array $row) {
return [
'name' => (string) $row['vendor_template_name'],
'description' => (string) $row['vendor_template_description'],
'phone_country_code' => (string) $row['vendor_template_phone_country_code'],
'phone' => (string) $row['vendor_template_phone'],
'extension' => (string) $row['vendor_template_extension'],
'hours' => (string) $row['vendor_template_hours'],
'sla' => (string) $row['vendor_template_sla'],
'email' => (string) $row['vendor_template_email'],
'website' => (string) $row['vendor_template_website'],
'account_number' => (string) $row['vendor_template_account_number'],
'code' => (string) $row['vendor_template_code'],
'contact_name' => (string) $row['vendor_template_contact_name'],
'notes' => (string) $row['vendor_template_notes'],
];
}
/* Save a template's fields (vendorTemplateFields()) - a new one with $vendor_template_id 0. Its id. */
function vendorTemplateSave($vendor_template_id, array $f) {
global $mysqli, $session_user_id;
$vendor_template_id = intval($vendor_template_id);
$user_id = intval($session_user_id ?? 0);
$set = [];
foreach (vendorTemplateColumns() as $key => $pairs) {
foreach ($pairs as [$template_column]) {
$field = substr($template_column, strlen('vendor_template_'));
$set[] = "$template_column = '" . escapeSql((string) ($f[$field] ?? '')) . "'";
}
}
$set = implode(', ', $set);
if ($vendor_template_id) {
mysqli_query($mysqli, "UPDATE vendor_templates SET $set, vendor_template_updated_by = $user_id WHERE vendor_template_id = $vendor_template_id");
return $vendor_template_id;
}
mysqli_query($mysqli, "INSERT INTO vendor_templates SET $set, vendor_template_created_by = $user_id");
return intval(mysqli_insert_id($mysqli));
}
/** What changed between two sets of fields - the labels, in order: ['Support line', 'Support hours']. */
function vendorTemplateChanged(array $before, array $after) {
$changed = [];
foreach (vendorTemplateColumns() as $key => $pairs) {
foreach ($pairs as [$template_column]) {
$field = substr($template_column, strlen('vendor_template_'));
if ((string) ($before[$field] ?? '') !== (string) ($after[$field] ?? '')) {
$changed[$key] = vendorTemplateFieldLabels()[$key]['label'];
break;
}
}
}
return $changed;
}
/* A new template starting from this one - "Copy of". Its id, or 0. */
function vendorTemplateCopy($vendor_template_id) {
$row = vendorTemplateInfo($vendor_template_id);
if (!$row) {
return 0;
}
$fields = vendorTemplateRowFields($row);
$fields['name'] = mb_substr('Copy of ' . $fields['name'], 0, 200);
return vendorTemplateSave(0, $fields);
}
/*
* Delete a template. The vendors made from it stay, no longer linked to it,
* and license templates that took their vendor from it have none.
*/
function vendorTemplateDelete($vendor_template_id) {
global $mysqli;
$vendor_template_id = intval($vendor_template_id);
mysqli_query($mysqli, "UPDATE vendors SET vendor_template_id = 0, vendor_updated_at = vendor_updated_at WHERE vendor_template_id = $vendor_template_id");
mysqli_query($mysqli, "UPDATE software_templates SET software_template_vendor_template_id = 0, software_template_updated_at = software_template_updated_at WHERE software_template_vendor_template_id = $vendor_template_id");
mysqli_query($mysqli, "DELETE FROM vendor_templates WHERE vendor_template_id = $vendor_template_id");
}
/*
* Put the template's fields picked ($keys, from vendorTemplateColumns()) on
* every vendor made from it - for Change them on the client vendors too.
* The vendors changed: vendor id => ['name', 'client_id'].
*/
function vendorTemplatePush($vendor_template_id, array $keys) {
global $mysqli;
$vendor_template_id = intval($vendor_template_id);
$row = vendorTemplateInfo($vendor_template_id);
$columns = vendorTemplateColumns();
$keys = array_values(array_intersect(array_keys($columns), $keys));
if (!$row || !$keys) {
return [];
}
$set = [];
foreach ($keys as $key) {
foreach ($columns[$key] as [$template_column, $vendor_column]) {
$set[] = "$vendor_column = '" . escapeSql((string) $row[$template_column]) . "'";
}
}
$vendors = [];
$sql = mysqli_query($mysqli, "SELECT vendor_id, vendor_name, vendor_client_id FROM vendors WHERE vendor_template_id = $vendor_template_id");
while ($vendor = mysqli_fetch_assoc($sql)) {
$vendors[intval($vendor['vendor_id'])] = ['name' => $vendor['vendor_name'], 'client_id' => intval($vendor['vendor_client_id'])];
}
if ($vendors) {
mysqli_query($mysqli, "UPDATE vendors SET " . implode(', ', $set) . " WHERE vendor_template_id = $vendor_template_id");
}
return $vendors;
}
/*
* The vendors made from a template, for its page - current ones first, by
* client: vendor id => vendor_id, vendor_name, client_id (0 for ours),
* client_name, client_abbreviation, account_number, code, created_at,
* archived (the vendor), client_archived.
*/
function vendorTemplateVendors($vendor_template_id) {
global $mysqli;
$vendor_template_id = intval($vendor_template_id);
$vendors = [];
$sql = mysqli_query($mysqli, "SELECT vendor_id, vendor_name, vendor_client_id, vendor_account_number, vendor_code, vendor_created_at, vendor_archived_at,
client_name, client_abbreviation, client_archived_at
FROM vendors LEFT JOIN clients ON client_id = vendor_client_id AND vendor_client_id > 0
WHERE vendor_template_id = $vendor_template_id
ORDER BY vendor_archived_at IS NOT NULL, client_archived_at IS NOT NULL, vendor_client_id = 0, client_name ASC, vendor_name ASC");
while ($row = mysqli_fetch_assoc($sql)) {
$vendors[intval($row['vendor_id'])] = [
'vendor_id' => intval($row['vendor_id']),
'vendor_name' => (string) $row['vendor_name'],
'client_id' => intval($row['vendor_client_id']),
'client_name' => (string) $row['client_name'],
'client_abbreviation' => (string) $row['client_abbreviation'],
'account_number' => trim((string) $row['vendor_account_number']),
'code' => trim((string) $row['vendor_code']),
'created_at' => $row['vendor_created_at'],
'archived' => !empty($row['vendor_archived_at']),
'client_archived' => !empty($row['client_archived_at']),
];
}
return $vendors;
}
/** The license templates that take their vendor from it - id => name, archived. */
function vendorTemplateLicenseTemplates($vendor_template_id) {
global $mysqli;
$vendor_template_id = intval($vendor_template_id);
$templates = [];
$sql = mysqli_query($mysqli, "SELECT software_template_id, software_template_name, software_template_archived_at FROM software_templates
WHERE software_template_vendor_template_id = $vendor_template_id ORDER BY software_template_archived_at IS NOT NULL, software_template_name ASC");
while ($row = mysqli_fetch_assoc($sql)) {
$templates[intval($row['software_template_id'])] = ['name' => $row['software_template_name'], 'archived' => !empty($row['software_template_archived_at'])];
}
return $templates;
}
/** A template's tile - its initials on the grey of the other template lists. $size as vendorTile(). */
function vendorTemplateTile($name, $size = '') {
$size_class = in_array($size, ['xs', 'sm', 'lg'], true) ? " itflow-asset-icon-$size itflow-vendor-tile-$size" : '';
return '<span class="itflow-asset-icon itflow-vendor-tile itflow-asset-tone-other' . $size_class . '" aria-hidden="true">' . escapeHtml(vendorInitials($name)) . '</span>';
}
/** The support line as it reads - "(412) 555-0100, then 2" - or ''. Raw in, escaped out. */
function vendorTemplatePhoneHtml($phone, $country_code, $extension) {
if (preg_replace('/[^0-9]/', '', (string) $phone) === '') {
return '';
}
$html = escapeHtml(formatPhoneNumber($phone, $country_code));
$dial = vendorDialNumber($phone, $country_code);
if ($dial !== '') {
$html = '<a href="tel:' . escapeHtml($dial) . '">' . $html . '</a>';
}
return $html . (trim((string) $extension) !== '' ? ' <span class="text-body-secondary">then ' . escapeHtml($extension) . '</span>' : '');
}