mirror of
https://github.com/itflow-org/itflow
synced 2026-11-23 21:07:22 +00:00
Have Yealink and Polycom desk phones report themselves to ITFlow and become Phone assets of their domain client, matched by MAC address and kept up to date unless changed in ITFlow, with a phone system card on the asset and a Phones count on the Domains page
This commit is contained in:
@@ -21,6 +21,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
|
||||
- Phone Systems: Admin > Phone Systems takes FusionPBX - without its API or its database. FreeSWITCH, underneath it, sends ITFlow each call's record as it hangs up and asks ITFlow for caller ID names; the system's Setup window gives the lines to paste, with its own key in them. Each of its domains is linked to the client it is, with the client it looks like suggested. A call on a client's domain is that client's; on your own company's domain it goes to whoever called, from any client.
|
||||
- Calls: a new Calls page, on the menu and on each client, lists the calls with who they were with - matched to a contact by their number, or to a client by its main number - the extension and agent that took them, how long they talked, and whether they were missed or went to voicemail, with a link to the call's page in FusionPBX and its recording. A contact's page has a Calls tab, and the menu counts the calls missed today that aren't on a ticket. Add to Ticket puts a call on one of the client's open tickets, or a new one, as an internal note with its talk time as time worked.
|
||||
- Phone Systems: as a call rings, desk phones show the contact and their client - Marla Whitfield - Ravenwood Dental - when ITFlow knows the number, and the carrier's name otherwise.
|
||||
- Phone Systems: Yealink and Polycom desk phones report themselves as they start up and register - the Setup window has the lines for each - and become Phone assets of their domain's client, named after their extension, with their make, model, firmware, IP and MAC address. A client's asset that already has the phone's MAC address is used instead. The asset keeps up with new firmware and IP addresses, except for fields changed in ITFlow, and has a card saying whether the phone is registered and its line. The Domains page counts each domain's phones.
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
@@ -56,6 +57,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
|
||||
- Database 4.1.4: `phone_systems`, `phone_domains`, `phone_calls`, `phone_devices`, `phone_events` and `user_settings.user_config_phone_line`. Re-runnable. `functions/phone.php` has them; nothing in ITFlow connects to a phone system - it sends to `api/phone/`, keyed by the address's `key` or the HTTP Basic password (`phoneSystemByKey()`), 404 for an unknown key and 503 for a system turned off.
|
||||
- `api/phone/cdr.php` takes mod_xml_cdr's post - the `cdr` field URL-encoded or base64, or the body - through `phoneCdrParse()` (its variables URL-decoded, the rest from the first caller profile) and `phoneCallFromCdr()` (FusionPBX's `call_direction`, else worked out from the numbers). A second leg (`originating_leg_uuid`, or `uuid` starting `b_`) is set aside, and a call is kept once per uuid. `api/phone/cid.php` answers mod_cidlookup with `phoneCallerName()`.
|
||||
- `phoneWho($number, $domain_client_id)` matches a number with `smsContactsFor()` - a mobile before an office number many share - then clients' location phones; a number more than one client has is nobody's. On a client's domain only that client counts; on your own company's (`config_internal_client_id`) or one not linked, every client does. `phoneDomainLink()` matches a domain's calls again. `phoneCallToTicket()` adds the internal note, its talk time as `ticket_reply_time_worked` with the work start and end, and `add_ticket` takes `phone_call_id` (ticket source Phone). `phoneCleanup()` runs nightly: calls over 400 days old that aren't on a ticket go. Client delete and merge take the phone tables.
|
||||
- `api/phone/hook.php` takes desk phones' events through `phoneHookEvent()` - Yealink action URLs as GET (`e` = setup, registered, unregistered, failed, ringing, outgoing, answered or ended, with `mac`, `ip`, `model`, `fw`, `line`, `domain`, `number`, `name` and `call`; a variable the phone left unfilled counts as nothing) or Polycom telephony notifications posted as XML (`phoneHookPolycom()`), with the vendor, model and firmware from the User-Agent when the request doesn't say. `phoneDeviceSave()` keeps a phone by system and MAC in `phone_devices`, and `phoneDeviceAsset()` finds the client's asset by MAC (`asset_interfaces.interface_mac`, any separators) or makes a Phone asset with its interface and `extension` detail, then follows the phone the way RMM devices do: a field is written when empty or still what the phone said last. `phone_device_asset_id` -1 is an asset deleted in ITFlow, not made again. `phoneDomainLink()` runs it for the domain's phones.
|
||||
|
||||
## [26.12]
|
||||
|
||||
|
||||
@@ -24,10 +24,30 @@ $xml = function (string $url) {
|
||||
};
|
||||
$cdr_url = phoneUrl($row, 'cdr');
|
||||
$cid_url = phoneUrl($row, 'cid', 'number=${caller_id_number}&domain=${domain_name}');
|
||||
$hook_url = phoneUrl($row, 'hook');
|
||||
|
||||
// Yealink action URLs - the phone fills in its own $variables
|
||||
$account = 'line=$active_user&domain=$active_host';
|
||||
$yealink = [
|
||||
'setup_completed' => 'e=setup&mac=$mac&ip=$ip&model=$model&fw=$firmware',
|
||||
'registered' => "e=registered&mac=\$mac&ip=\$ip&model=\$model&fw=\$firmware&$account",
|
||||
'unregistered' => "e=unregistered&mac=\$mac&$account",
|
||||
'register_failed' => "e=failed&mac=\$mac&$account",
|
||||
'incoming_call' => "e=ringing&mac=\$mac&$account&number=\$remote&name=\$display_remote&call=\$call_id",
|
||||
'outgoing_call' => "e=outgoing&mac=\$mac&$account&number=\$remote&name=\$display_remote&call=\$call_id",
|
||||
'call_established' => "e=answered&mac=\$mac&$account&number=\$remote&call=\$call_id",
|
||||
'call_terminated' => "e=ended&mac=\$mac&$account&number=\$remote&call=\$call_id",
|
||||
];
|
||||
$yealink_lines = [];
|
||||
foreach ($yealink as $event => $query) {
|
||||
$yealink_lines[] = "action_url.$event = $hook_url&$query";
|
||||
}
|
||||
|
||||
$snippets = [
|
||||
'cdr' => '<param name="url" value="' . $xml($cdr_url) . "\"/>\n<param name=\"log-http-and-disk\" value=\"true\"/>\n<param name=\"retries\" value=\"2\"/>\n<param name=\"delay\" value=\"5\"/>",
|
||||
'cid' => '<param name="url" value="' . $xml($cid_url) . "\"/>\n<param name=\"cache\" value=\"false\"/>",
|
||||
'yealink' => implode("\n", $yealink_lines),
|
||||
'polycom' => '<telNotification apps.telNotification.URL="' . $xml($hook_url) . "\"\n apps.telNotification.incomingEvent=\"1\" apps.telNotification.outgoingEvent=\"1\"\n apps.telNotification.callStateChangeEvent=\"1\" apps.telNotification.lineRegistrationEvent=\"1\"\n apps.telNotification.uiInitializationEvent=\"1\"/>",
|
||||
'dialplan' => "<extension name=\"itflow_caller_id\" continue=\"true\">\n <condition field=\"\${caller_id_number}\" expression=\"^\\+?\\d{7,15}\$\">\n <action application=\"set\" data=\"itflow_cid=\${cidlookup(\${caller_id_number})}\" inline=\"true\"/>\n </condition>\n <condition field=\"\${itflow_cid}\" expression=\"^(?!UNKNOWN\$|-ERR).+\$\">\n <action application=\"set\" data=\"effective_caller_id_name=\${itflow_cid}\"/>\n </condition>\n</extension>",
|
||||
];
|
||||
|
||||
@@ -68,6 +88,12 @@ ob_start();
|
||||
<p class="small mt-2 mb-2">Load it with <code>fs_cli -x 'reload mod_cidlookup'</code> (add <code>mod_cidlookup</code> to <code>modules.conf.xml</code> if it isn't). Then, in <strong>Dialplan > Dialplan Manager</strong>, add this to the <code>public</code> context, ordered before the inbound routes - the name is only set when ITFlow knows the number, so the carrier's stays otherwise:</p>
|
||||
<?= $copy_block('phoneSnippetDialplan', $snippets['dialplan']) ?>
|
||||
|
||||
<h6 class="fw-semibold mt-3"><i class="fas fa-fw fa-phone text-secondary me-1"></i>Phones</h6>
|
||||
<p class="small mb-2">Desk phones tell ITFlow their model, firmware, IP address and line as they start up and register, and each becomes a Phone asset of its domain's client, kept by its MAC address. They reach ITFlow themselves, so they need to be able to get to its address. For Yealink phones, add these lines to the template <?= escapeHtml($kind['short']) ?> provisions them with and have the phones check for new settings - or, for one phone, enter each address in its web page under <strong>Features > Action URL</strong>:</p>
|
||||
<?= $copy_block('phoneSnippetYealink', $snippets['yealink']) ?>
|
||||
<p class="small mt-2 mb-2">For Polycom phones, add this to the configuration they are provisioned with:</p>
|
||||
<?= $copy_block('phoneSnippetPolycom', $snippets['polycom']) ?>
|
||||
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<a class="btn btn-outline-secondary me-auto" href="phone_domains.php?phone_system_id=<?= $phone_system_id ?>"><i class="fas fa-globe me-2"></i>Domains</a>
|
||||
|
||||
@@ -59,6 +59,7 @@ $filter_url = function (array $change) {
|
||||
$sortable_columns = [
|
||||
'phone_domain_name' => "phone_domain_name $order",
|
||||
'calls_month' => "calls_month $order, phone_domain_name ASC",
|
||||
'phones' => "phones $order, phone_domain_name ASC",
|
||||
'client_name' => "client_name IS NULL, client_name $order, phone_domain_name ASC",
|
||||
'phone_domain_seen_at' => "phone_domain_seen_at IS NULL, phone_domain_seen_at $order, phone_domain_name ASC",
|
||||
];
|
||||
@@ -67,7 +68,8 @@ if (!isset($sortable_columns[$sort])) {
|
||||
}
|
||||
|
||||
$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS phone_domains.*, client_name, client_abbreviation, client_archived_at,
|
||||
(SELECT COUNT(*) FROM phone_calls WHERE phone_call_domain_id = phone_domains.phone_domain_id AND phone_call_started_at >= NOW() - INTERVAL 30 DAY) AS calls_month
|
||||
(SELECT COUNT(*) FROM phone_calls WHERE phone_call_domain_id = phone_domains.phone_domain_id AND phone_call_started_at >= NOW() - INTERVAL 30 DAY) AS calls_month,
|
||||
(SELECT COUNT(*) FROM phone_devices WHERE phone_device_domain_id = phone_domains.phone_domain_id) AS phones
|
||||
FROM phone_domains
|
||||
LEFT JOIN clients ON client_id = phone_domain_client_id AND phone_domain_client_id > 0
|
||||
WHERE phone_domain_system_id = $phone_system_id AND {$shows[$show]['sql']}
|
||||
@@ -170,6 +172,7 @@ $own_client = intval($config_internal_client_id ?? 0);
|
||||
<tr>
|
||||
<th class="ps-3"><?= sortLink('phone_domain_name', 'Domain') ?></th>
|
||||
<th class="d-none d-md-table-cell"><?= sortLink('calls_month', 'Calls, 30 days') ?></th>
|
||||
<th class="d-none d-lg-table-cell"><?= sortLink('phones', 'Phones') ?></th>
|
||||
<th><?= sortLink('client_name', 'ITFlow client') ?></th>
|
||||
<th class="d-none d-lg-table-cell"><?= sortLink('phone_domain_seen_at', 'Last heard from') ?></th>
|
||||
<th></th>
|
||||
@@ -186,7 +189,7 @@ $own_client = intval($config_internal_client_id ?? 0);
|
||||
<tr data-phone-domain-id="<?= $phone_domain_id ?>">
|
||||
<td class="ps-3">
|
||||
<div class="fw-semibold font-monospace small"><?= escapeHtml($row['phone_domain_name']) ?></div>
|
||||
<div class="d-md-none small text-body-secondary"><?= number_format(intval($row['calls_month'])) ?> calls</div>
|
||||
<div class="d-md-none small text-body-secondary"><?= number_format(intval($row['calls_month'])) ?> calls<?= intval($row['phones']) ? ' · ' . number_format(intval($row['phones'])) . ' phones' : '' ?></div>
|
||||
</td>
|
||||
<td class="d-none d-md-table-cell text-nowrap">
|
||||
<?php if ($linked && intval($row['calls_month'])) { ?>
|
||||
@@ -196,6 +199,7 @@ $own_client = intval($config_internal_client_id ?? 0);
|
||||
<?= number_format(intval($row['calls_month'])) ?>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<td class="d-none d-lg-table-cell" title="Desk phones on it that told ITFlow about themselves"><?= number_format(intval($row['phones'])) ?></td>
|
||||
<td>
|
||||
<?php if ($linked) { ?>
|
||||
<div class="d-flex align-items-center gap-2">
|
||||
|
||||
@@ -514,6 +514,50 @@ if (mysqli_num_rows($sql) == 0) {
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php // The desk phone it is, as the phone last told its phone system's ITFlow (functions/phone.php)
|
||||
foreach (phoneDevicesForAsset($asset_id) as $phone_device) {
|
||||
$phone_status = phoneDeviceStatuses()[$phone_device['phone_device_status']] ?? phoneDeviceStatuses()[''];
|
||||
$phone_web = phoneWebUrl($phone_device);
|
||||
$phone_short = phoneKind($phone_device)['short'] ?? 'the phone system'; ?>
|
||||
<div class="card mb-3" data-phone-device="<?= escapeHtml($phone_device['phone_device_status']) ?>">
|
||||
<div class="card-header d-flex align-items-center">
|
||||
<h3 class="card-title mb-0"><i class="<?= escapeHtml(phoneKind($phone_device)['icon'] ?? 'fas fa-phone-volume') ?> fa-fw text-secondary me-2"></i><?= escapeHtml(phoneSystemName($phone_device)) ?></h3>
|
||||
<?php if ($phone_web !== '') { ?>
|
||||
<a class="ms-auto ps-2 text-secondary" href="<?= escapeHtml($phone_web) ?>" target="_blank" rel="noopener noreferrer" title="Open <?= escapeHtml($phone_short) ?>"><i class="fas fa-fw fa-external-link-alt"></i></a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<dl class="itflow-asset-dl mb-0">
|
||||
<dt>Status</dt>
|
||||
<dd><span class="text-<?= $phone_status[2] ?>-emphasis fw-semibold"><i class="fas fa-fw <?= $phone_status[1] ?> me-1"></i><?= escapeHtml($phone_status[0]) ?></span>
|
||||
<?php if ($phone_device['phone_device_status_at']) { ?>
|
||||
<span class="small text-body-secondary">- since <span title="<?= formatDateTime($phone_device['phone_device_status_at']) ?>"><?= escapeHtml(timeAgo($phone_device['phone_device_status_at'])) ?></span></span>
|
||||
<?php } ?>
|
||||
</dd>
|
||||
<?php if ((string) $phone_device['phone_device_line'] !== '') { ?>
|
||||
<dt>Line</dt>
|
||||
<dd><?= escapeHtml(phoneNumberText($phone_device['phone_device_line']) ?: $phone_device['phone_device_line']) ?><?php if ((string) $phone_device['phone_domain_name'] !== '') { ?> <span class="small text-body-secondary">on <?= escapeHtml($phone_device['phone_domain_name']) ?></span><?php } ?></dd>
|
||||
<?php } ?>
|
||||
<?php // What the asset shows already isn't said again - only where the phone says otherwise
|
||||
$phone_model = trim($phone_device['phone_device_vendor'] . ' ' . $phone_device['phone_device_model']);
|
||||
if ((string) $phone_device['phone_device_ip'] !== '' && $phone_device['phone_device_ip'] !== (string) $asset_row['interface_ip']) { ?>
|
||||
<dt>IP address</dt>
|
||||
<dd><span class="font-monospace"><?= escapeHtml($phone_device['phone_device_ip']) ?></span><button type="button" class="itflow-copy clipboardjs" title="Copy IP address" data-clipboard-text="<?= escapeHtml($phone_device['phone_device_ip']) ?>"><i class="far fa-copy"></i></button></dd>
|
||||
<?php } ?>
|
||||
<?php if ((string) $phone_device['phone_device_model'] !== '' && $phone_device['phone_device_model'] !== (string) $asset_row['asset_model']) { ?>
|
||||
<dt>Model</dt>
|
||||
<dd><?= escapeHtml($phone_model) ?></dd>
|
||||
<?php } ?>
|
||||
<?php if ((string) $phone_device['phone_device_firmware'] !== '' && $phone_device['phone_device_firmware'] !== (string) $asset_row['asset_os']) { ?>
|
||||
<dt>Firmware</dt>
|
||||
<dd class="small"><?= escapeHtml($phone_device['phone_device_firmware']) ?></dd>
|
||||
<?php } ?>
|
||||
</dl>
|
||||
<div class="small text-body-secondary mt-2" title="<?= escapeHtml((string) $phone_device['phone_device_heard_at']) ?>">As the phone said <?= escapeHtml(timeAgo($phone_device['phone_device_heard_at'])) ?> - a phone only says something when it starts up, registers or has a call, so this can be out of date.</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ($asset_ip || $asset_ipv6 || $asset_nat_ip || $asset_mac || $network_name) { ?>
|
||||
<div class="card mb-3">
|
||||
<div class="card-header">
|
||||
|
||||
25
api/phone/hook.php
Normal file
25
api/phone/hook.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - desk phones telling ITFlow about themselves. A Yealink phone calls this at startup, as
|
||||
* its lines register, and as calls ring, start and end (its action URLs, with e= the event and the
|
||||
* phone's own variables - the system's setup window in Admin > Phone Systems has them); a Polycom
|
||||
* phone posts its telephony notifications here as XML. Each phone is kept by its MAC address, and
|
||||
* becomes a Phone asset of its domain's client (phoneDeviceSave(), functions/phone.php). Anything
|
||||
* that isn't from a phone is turned down (400).
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/phone_system.php';
|
||||
|
||||
$phone_body = $_SERVER['REQUEST_METHOD'] === 'POST' ? (string) file_get_contents('php://input', false, null, 0, PHONE_HOOK_MAX_BYTES + 1) : '';
|
||||
$phone_event = phoneHookEvent($_GET, $phone_body, (string) ($_SERVER['HTTP_USER_AGENT'] ?? ''));
|
||||
if (!$phone_event) {
|
||||
$phone_reply(400, 'Not a phone event');
|
||||
}
|
||||
if ($phone_event['mac'] === '') {
|
||||
$phone_reply(400, 'No MAC address');
|
||||
}
|
||||
|
||||
phoneDeviceSave($phone_system, $phone_event);
|
||||
|
||||
$phone_reply(200, 'OK');
|
||||
@@ -9,6 +9,8 @@
|
||||
* the other party matched to a contact (phoneWho()) and the extension to an agent
|
||||
* cid.php the name for a caller ID, asked for as a call comes in (mod_cidlookup) - so desk
|
||||
* phones show the contact and their client
|
||||
* hook.php desk phones saying what they are and what they are doing (Yealink action URLs,
|
||||
* Polycom telephony notifications) - a phone_devices row, and a Phone asset
|
||||
*
|
||||
* Each domain (a FusionPBX tenant) it names is a phone_domains row, linked on the system's Domains
|
||||
* page (admin/phone_domains.php) to the client it is. A call on a client's domain is that client's,
|
||||
@@ -21,6 +23,8 @@
|
||||
const PHONE_CALLS_KEEP_DAYS = 400;
|
||||
/** The longest a call record from FreeSWITCH is taken to be */
|
||||
const PHONE_CDR_MAX_BYTES = 4194304;
|
||||
/** The longest a phone's notification is taken to be */
|
||||
const PHONE_HOOK_MAX_BYTES = 65536;
|
||||
|
||||
/** The kinds of phone system ITFlow knows */
|
||||
function phoneSystemKinds(): array
|
||||
@@ -304,7 +308,7 @@ function phoneDomainSuggestions(int $phone_system_id): array
|
||||
}
|
||||
|
||||
/**
|
||||
* Link a domain to a client (0 unlinks it). Its phones become the client's, and its calls are
|
||||
* Link a domain to a client (0 unlinks it). Its phones become the client's assets (phoneDeviceAsset()), and its calls are
|
||||
* matched again by the new rule - on a client's domain they are the client's; on your own
|
||||
* company's, or unlinked, the caller's. A call already on a ticket keeps who it was with.
|
||||
*/
|
||||
@@ -313,6 +317,11 @@ function phoneDomainLink(int $phone_domain_id, int $client_id): void
|
||||
global $mysqli, $config_internal_client_id;
|
||||
mysqli_query($mysqli, "UPDATE phone_domains SET phone_domain_client_id = $client_id WHERE phone_domain_id = $phone_domain_id");
|
||||
mysqli_query($mysqli, "UPDATE phone_devices SET phone_device_client_id = $client_id WHERE phone_device_domain_id = $phone_domain_id");
|
||||
// Its phones become the client's assets
|
||||
$devices = mysqli_query($mysqli, "SELECT phone_device_id FROM phone_devices WHERE phone_device_domain_id = $phone_domain_id");
|
||||
while ($device = mysqli_fetch_row($devices)) {
|
||||
phoneDeviceAsset(intval($device[0]));
|
||||
}
|
||||
|
||||
$own = $client_id && $client_id !== intval($config_internal_client_id ?? 0);
|
||||
$who = [];
|
||||
@@ -890,6 +899,478 @@ function phoneCallToTicket(array $call, array $ticket, int $user_id, bool $count
|
||||
return $reply_id;
|
||||
}
|
||||
|
||||
/*
|
||||
* Phones - desk phones that tell ITFlow about themselves (api/phone/hook.php): Yealink action URLs,
|
||||
* each a GET with e= the event, or Polycom telephony notifications, XML posted. A phone is kept by
|
||||
* its MAC address in phone_devices with what it last said - model, firmware, IP address, its line
|
||||
* (extension and domain) and whether it is registered - and becomes a Phone asset of its domain's
|
||||
* client once the domain is linked (phoneDeviceAsset()).
|
||||
*/
|
||||
|
||||
/** A MAC address as 12 lower-case hex digits - '' when it isn't one */
|
||||
function phoneMac($raw): string
|
||||
{
|
||||
$mac = strtolower((string) preg_replace('/[^0-9a-fA-F]/', '', (string) $raw));
|
||||
return strlen($mac) === 12 && $mac !== '000000000000' && $mac !== 'ffffffffffff' ? $mac : '';
|
||||
}
|
||||
|
||||
/** A MAC address for reading - "00:15:65:AB:CD:EF" */
|
||||
function phoneMacText($mac): string
|
||||
{
|
||||
$mac = (string) $mac;
|
||||
return strlen($mac) === 12 ? strtoupper(implode(':', str_split($mac, 2))) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Something a phone sent, cleaned up - '' for nothing, and for a variable the phone left as it was
|
||||
* ("$active_user"), cut to $max characters.
|
||||
*/
|
||||
function phoneHookValue($raw, int $max = 100): string
|
||||
{
|
||||
$value = (string) $raw;
|
||||
if (!mb_check_encoding($value, 'UTF-8')) {
|
||||
$value = mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1');
|
||||
}
|
||||
$value = trim((string) preg_replace('/[\x00-\x1F\x7F]/', '', $value));
|
||||
if ($value === '' || preg_match('/^\$\w+$|^\{\$[\w.]+\}$/', $value)) {
|
||||
return '';
|
||||
}
|
||||
return mb_substr($value, 0, $max);
|
||||
}
|
||||
|
||||
/** An extension as a phone gives it - "101" - or '' */
|
||||
function phoneHookLine($raw): string
|
||||
{
|
||||
$line = phoneHookValue(rawurldecode((string) $raw), 50);
|
||||
return preg_match('/^[A-Za-z0-9_.+*#-]+$/', $line) ? $line : '';
|
||||
}
|
||||
|
||||
/** [user, host] of a SIP address - "sip:101@pbx.example.com", "<sip:101@pbx.example.com>" - or ['', ''] */
|
||||
function phoneSipParts($raw): array
|
||||
{
|
||||
$raw = trim(rawurldecode((string) $raw));
|
||||
if (preg_match('/<([^>]*)>/', $raw, $m)) {
|
||||
$raw = $m[1];
|
||||
}
|
||||
if (preg_match('/^(?:sips?:)?([^@;:\s]+)@([^;>:\s\/]+)/i', $raw, $m)) {
|
||||
return [$m[1], $m[2]];
|
||||
}
|
||||
return ['', ''];
|
||||
}
|
||||
|
||||
/** A model as phones give it, without the SIP- some put in front - "SIP-T46S" is T46S */
|
||||
function phoneHookModel($raw): string
|
||||
{
|
||||
return (string) preg_replace('/^SIP[-\s]+|-SIP$/i', '', phoneHookValue($raw));
|
||||
}
|
||||
|
||||
/** [vendor, model, firmware] a phone's User-Agent says - '' for what it doesn't */
|
||||
function phoneAgentFacts(string $agent): array
|
||||
{
|
||||
// Yealink SIP-T46S 66.86.0.15 00:15:65:aa:bb:cc
|
||||
if (preg_match('/\bYealink\s+([A-Z0-9][\w-]*)\s+(\d[\d.]*)/i', $agent, $m)) {
|
||||
return ['Yealink', phoneHookModel($m[1]), $m[2]];
|
||||
}
|
||||
// PolycomVVX-VVX_410-UA/5.3.0.3844
|
||||
if (preg_match('/\bPolycom\w*-(\w+?)-UA\/(\d[\d.]*)/i', $agent, $m)) {
|
||||
return ['Polycom', str_replace('_', ' ', $m[1]), $m[2]];
|
||||
}
|
||||
if (preg_match('/\b(Grandstream|Fanvil|snom|Htek)\s*([A-Z0-9][\w-]*)?[\s\/]+(\d[\d.]*)/i', $agent, $m)) {
|
||||
$vendors = ['grandstream' => 'Grandstream', 'fanvil' => 'Fanvil', 'snom' => 'Snom', 'htek' => 'Htek'];
|
||||
return [$vendors[strtolower($m[1])], phoneHookModel($m[2] ?? ''), $m[3]];
|
||||
}
|
||||
return ['', '', ''];
|
||||
}
|
||||
|
||||
/**
|
||||
* What a phone said, from its request - ['kind', 'mac', 'ip', 'vendor', 'model', 'firmware', 'line',
|
||||
* 'domain', 'number', 'name', 'call'], or null when it isn't a phone's. kind is setup, registered,
|
||||
* unregistered, failed, ringing, outgoing, answered, ended, missed, or heard (nothing more than that).
|
||||
* Yealink sends e= and the rest as the setup window's action URLs have them; Polycom posts XML, and
|
||||
* says its model and firmware in its User-Agent.
|
||||
*/
|
||||
function phoneHookEvent(array $get, string $body, string $agent): ?array
|
||||
{
|
||||
[$vendor, $model, $firmware] = phoneAgentFacts($agent);
|
||||
$event = ['kind' => '', 'mac' => '', 'ip' => '', 'vendor' => $vendor, 'model' => $model, 'firmware' => $firmware,
|
||||
'line' => '', 'domain' => '', 'number' => '', 'name' => '', 'call' => ''];
|
||||
|
||||
if (stripos($body, '<PolycomIPPhone') !== false) {
|
||||
return phoneHookPolycom($body, $event);
|
||||
}
|
||||
|
||||
$kind = strtolower(phoneHookValue($get['e'] ?? '', 20));
|
||||
if (!in_array($kind, ['setup', 'registered', 'unregistered', 'failed', 'ringing', 'outgoing', 'answered', 'ended', 'missed'], true)) {
|
||||
return null;
|
||||
}
|
||||
$event['kind'] = $kind;
|
||||
$event['mac'] = phoneMac(phoneHookValue($get['mac'] ?? ''));
|
||||
$event['ip'] = (string) filter_var(phoneHookValue($get['ip'] ?? '', 64), FILTER_VALIDATE_IP);
|
||||
$event['model'] = phoneHookModel($get['model'] ?? '') ?: $event['model'];
|
||||
$event['firmware'] = phoneHookValue($get['fw'] ?? '') ?: $event['firmware'];
|
||||
// Yealink's own MAC address blocks, for a phone whose User-Agent didn't say
|
||||
if ($event['vendor'] === '' && $event['mac'] !== '' && in_array(substr($event['mac'], 0, 6), ['001565', '805ec0', '805e0c', '249ad8', '44dbd2'], true)) {
|
||||
$event['vendor'] = 'Yealink';
|
||||
}
|
||||
// The account the event is about - its extension and domain, or a whole SIP address in line
|
||||
[$user, $host] = phoneSipParts($get['line'] ?? '');
|
||||
$event['line'] = phoneHookLine($user !== '' ? $user : ($get['line'] ?? ''));
|
||||
$event['domain'] = phoneHookValue($host !== '' ? $host : ($get['domain'] ?? ''), 255);
|
||||
$event['number'] = phoneNumber(phoneHookValue($get['number'] ?? '', 255));
|
||||
$event['name'] = phoneHookValue(rawurldecode((string) ($get['name'] ?? '')));
|
||||
$event['call'] = phoneHookValue($get['call'] ?? '');
|
||||
return $event;
|
||||
}
|
||||
|
||||
/** A Polycom telephony notification (phoneHookEvent()) - its event, or null */
|
||||
function phoneHookPolycom(string $body, array $event): ?array
|
||||
{
|
||||
if (strlen($body) > PHONE_HOOK_MAX_BYTES) {
|
||||
return null;
|
||||
}
|
||||
$was = libxml_use_internal_errors(true);
|
||||
$doc = simplexml_load_string($body, 'SimpleXMLElement', LIBXML_NONET);
|
||||
libxml_clear_errors();
|
||||
libxml_use_internal_errors($was);
|
||||
if (!$doc || $doc->getName() !== 'PolycomIPPhone' || !$doc->children()->count()) {
|
||||
return null;
|
||||
}
|
||||
$node = $doc->children()[0];
|
||||
$type = $node->getName();
|
||||
|
||||
// Every value in it, by name - the first of each, attributes too
|
||||
$values = [];
|
||||
$walk = function (SimpleXMLElement $element) use (&$walk, &$values) {
|
||||
foreach ($element->attributes() as $name => $value) {
|
||||
$values[(string) $name] ??= trim((string) $value);
|
||||
}
|
||||
foreach ($element->children() as $name => $child) {
|
||||
if ($child->count()) {
|
||||
$walk($child);
|
||||
} else {
|
||||
$values[(string) $name] ??= trim((string) $child);
|
||||
}
|
||||
foreach ($child->attributes() as $attribute => $value) {
|
||||
$values[(string) $attribute] ??= trim((string) $value);
|
||||
}
|
||||
}
|
||||
};
|
||||
$walk($node);
|
||||
$value = function (string $name) use ($values) {
|
||||
return phoneHookValue($values[$name] ?? '', 255);
|
||||
};
|
||||
|
||||
$event['mac'] = phoneMac($value('MACAddress'));
|
||||
$event['ip'] = (string) filter_var($value('PhoneIP'), FILTER_VALIDATE_IP);
|
||||
if ($event['vendor'] === '') {
|
||||
$event['vendor'] = 'Polycom';
|
||||
}
|
||||
|
||||
$ours = '';
|
||||
$theirs = ['', ''];
|
||||
switch ($type) {
|
||||
case 'IncomingCallEvent':
|
||||
$event['kind'] = 'ringing';
|
||||
$ours = $value('CalledPartyNumber');
|
||||
$theirs = [$value('CallingPartyNumber'), $value('CallingPartyName')];
|
||||
break;
|
||||
case 'OutgoingCallEvent':
|
||||
$event['kind'] = 'outgoing';
|
||||
$ours = $value('CallingPartyNumber');
|
||||
$theirs = [$value('CalledPartyNumber'), $value('CalledPartyName')];
|
||||
break;
|
||||
case 'LineRegistrationEvent':
|
||||
case 'LineUnregistrationEvent':
|
||||
$event['kind'] = $type === 'LineRegistrationEvent' ? 'registered' : 'unregistered';
|
||||
// The line's SIP address, whatever it is called in this firmware
|
||||
foreach ($values as $name => $each) {
|
||||
if (!in_array($name, ['MACAddress', 'PhoneIP', 'TimeStamp'], true) && phoneSipParts($each)[0] !== '') {
|
||||
$ours = $each;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'CallStateChangeEvent':
|
||||
$state = strtolower($value('CallState'));
|
||||
$event['kind'] = $state === 'connected' ? 'answered' : (in_array($state, ['disconnected', 'free'], true) ? 'ended' : 'heard');
|
||||
$incoming = strtolower($value('CallType')) !== 'outgoing';
|
||||
$ours = $value('LineDirNum') ?: $value($incoming ? 'CalledPartyDirNum' : 'CallingPartyDirNum');
|
||||
$theirs = $incoming ? [$value('CallingPartyDirNum'), $value('CallingPartyName')] : [$value('CalledPartyDirNum'), $value('CalledPartyName')];
|
||||
$event['call'] = $value('CallReference');
|
||||
break;
|
||||
case 'UIInitializationEvent':
|
||||
case 'NetworkUpEvent':
|
||||
$event['kind'] = 'setup';
|
||||
break;
|
||||
default:
|
||||
$event['kind'] = 'heard';
|
||||
}
|
||||
[$user, $host] = phoneSipParts($ours);
|
||||
$event['line'] = phoneHookLine($user !== '' ? $user : $ours);
|
||||
$event['domain'] = phoneHookValue($host, 255);
|
||||
$event['number'] = phoneNumber($theirs[0]);
|
||||
$event['name'] = $theirs[1] !== '' && $theirs[1] !== $theirs[0] ? mb_substr($theirs[1], 0, 100) : '';
|
||||
return $event;
|
||||
}
|
||||
|
||||
/** What a phone's status is - [label, icon, tone] by status ('' is a phone that hasn't said) */
|
||||
function phoneDeviceStatuses(): array
|
||||
{
|
||||
return [
|
||||
'registered' => ['Registered', 'fa-check-circle', 'success'],
|
||||
'unregistered' => ['Not registered', 'fa-minus-circle', 'secondary'],
|
||||
'failed' => ["Can't register", 'fa-exclamation-circle', 'danger'],
|
||||
'' => ['Heard from', 'fa-circle', 'secondary'],
|
||||
];
|
||||
}
|
||||
|
||||
/** A phone_devices row by id - with its system and domain - or null */
|
||||
function phoneDeviceRow($phone_device_id): ?array
|
||||
{
|
||||
global $mysqli;
|
||||
$phone_device_id = intval($phone_device_id);
|
||||
if (!$phone_device_id || !phoneTablesReady()) {
|
||||
return null;
|
||||
}
|
||||
return mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT phone_devices.*, phone_systems.*, phone_domain_name FROM phone_devices
|
||||
JOIN phone_systems ON phone_system_id = phone_device_system_id
|
||||
LEFT JOIN phone_domains ON phone_domain_id = phone_device_domain_id
|
||||
WHERE phone_device_id = $phone_device_id")) ?: null;
|
||||
}
|
||||
|
||||
/** The phones an asset is - most recently heard from first */
|
||||
function phoneDevicesForAsset(int $asset_id): array
|
||||
{
|
||||
global $mysqli;
|
||||
if ($asset_id <= 0 || !phoneTablesReady()) {
|
||||
return [];
|
||||
}
|
||||
$sql = mysqli_query($mysqli, "SELECT phone_devices.*, phone_systems.*, phone_domain_name FROM phone_devices
|
||||
JOIN phone_systems ON phone_system_id = phone_device_system_id
|
||||
LEFT JOIN phone_domains ON phone_domain_id = phone_device_domain_id
|
||||
WHERE phone_device_asset_id = $asset_id ORDER BY phone_device_heard_at DESC");
|
||||
return mysqli_fetch_all($sql, MYSQLI_ASSOC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep what a phone said (phoneHookEvent()): its phone_devices row made or brought up to date - a
|
||||
* phone ringing or making a call is registered - and its asset (phoneDeviceAsset()). The row, or
|
||||
* null for a phone that didn't say its MAC address.
|
||||
*/
|
||||
function phoneDeviceSave(array $system, array $event): ?array
|
||||
{
|
||||
global $mysqli;
|
||||
if ($event['mac'] === '') {
|
||||
return null;
|
||||
}
|
||||
$system_id = intval($system['phone_system_id']);
|
||||
$mac = escapeSql($event['mac']);
|
||||
$find = "SELECT * FROM phone_devices WHERE phone_device_system_id = $system_id AND phone_device_mac = '$mac'";
|
||||
$before = mysqli_fetch_assoc(mysqli_query($mysqli, $find));
|
||||
if (!$before) {
|
||||
mysqli_query($mysqli, "INSERT IGNORE INTO phone_devices SET phone_device_system_id = $system_id, phone_device_mac = '$mac'");
|
||||
$before = mysqli_fetch_assoc(mysqli_query($mysqli, $find));
|
||||
}
|
||||
if (!$before) {
|
||||
return null;
|
||||
}
|
||||
$phone_device_id = intval($before['phone_device_id']);
|
||||
|
||||
$set = ['phone_device_heard_at = NOW()'];
|
||||
foreach (['vendor' => 30, 'model' => 100, 'firmware' => 100, 'ip' => 64] as $field => $max) {
|
||||
if ($event[$field] !== '') {
|
||||
$set[] = "phone_device_$field = '" . escapeSql(mb_substr($event[$field], 0, $max)) . "'";
|
||||
}
|
||||
}
|
||||
if ($event['line'] !== '') {
|
||||
$set[] = "phone_device_line = '" . escapeSql($event['line']) . "'";
|
||||
// An address rather than a name isn't a domain of the phone system's
|
||||
$domain = $event['domain'] !== '' && !filter_var(preg_replace('/:\d+$/', '', $event['domain']), FILTER_VALIDATE_IP) ? phoneDomain($system, $event['domain']) : null;
|
||||
if ($domain) {
|
||||
$set[] = 'phone_device_domain_id = ' . intval($domain['phone_domain_id']);
|
||||
$set[] = 'phone_device_client_id = ' . intval($domain['phone_domain_client_id']);
|
||||
}
|
||||
}
|
||||
$status = ['registered' => 'registered', 'unregistered' => 'unregistered', 'failed' => 'failed'][$event['kind']]
|
||||
?? (in_array($event['kind'], ['ringing', 'outgoing', 'answered', 'ended', 'missed'], true) ? 'registered' : null);
|
||||
if ($status !== null && $status !== $before['phone_device_status']) {
|
||||
$set[] = "phone_device_status = '$status'";
|
||||
$set[] = 'phone_device_status_at = NOW()';
|
||||
}
|
||||
mysqli_query($mysqli, "UPDATE phone_devices SET " . implode(', ', $set) . " WHERE phone_device_id = $phone_device_id");
|
||||
mysqli_query($mysqli, "UPDATE phone_systems SET phone_system_heard_at = NOW() WHERE phone_system_id = $system_id");
|
||||
|
||||
phoneDeviceAsset($phone_device_id, $before);
|
||||
return phoneDeviceRow($phone_device_id);
|
||||
}
|
||||
|
||||
/** The name a phone's asset gets - PH-101, or PH- and the end of its MAC address until its extension is known */
|
||||
function phoneDeviceAssetName(array $device): string
|
||||
{
|
||||
$line = (string) $device['phone_device_line'];
|
||||
return 'PH-' . ($line !== '' ? $line : strtoupper(substr((string) $device['phone_device_mac'], -6)));
|
||||
}
|
||||
|
||||
/**
|
||||
* A phone's asset - found, made, or kept up to date - its id (0 for none yet, -1 for one deleted in
|
||||
* ITFlow). Once its domain is linked a phone is an asset of the domain's client: the client's asset
|
||||
* with its MAC address, else a new Phone asset. Its name, make, model, firmware, IP address and
|
||||
* extension follow what the phone says, unless changed in ITFlow - a field is written when it is
|
||||
* empty or still holds what the phone said last time ($before, its row before this). A phone whose
|
||||
* asset was deleted isn't made one again; it takes an asset with its MAC address made by hand.
|
||||
* On a domain linked to another client its asset stays with the client it was at.
|
||||
*/
|
||||
function phoneDeviceAsset(int $phone_device_id, ?array $before = null): int
|
||||
{
|
||||
global $mysqli;
|
||||
$device = phoneDeviceRow($phone_device_id);
|
||||
if (!$device) {
|
||||
return 0;
|
||||
}
|
||||
$client_id = intval($device['phone_device_client_id']);
|
||||
$asset_id = intval($device['phone_device_asset_id']);
|
||||
$who = phoneSystemName($device);
|
||||
$before = $before ?? $device;
|
||||
|
||||
if ($asset_id > 0) {
|
||||
$asset = phoneDeviceAssetCurrent($asset_id);
|
||||
if (!$asset) {
|
||||
mysqli_query($mysqli, "UPDATE phone_devices SET phone_device_asset_id = -1 WHERE phone_device_id = $phone_device_id");
|
||||
$asset_id = -1;
|
||||
} elseif (!$client_id) {
|
||||
// Its domain isn't linked now - its asset is left as it is
|
||||
return $asset_id;
|
||||
} elseif (intval($asset['asset_client_id']) !== $client_id) {
|
||||
rmmAssetHistory($asset_id, $who, 'no longer keeps it up - its domain is linked to another client');
|
||||
mysqli_query($mysqli, "UPDATE phone_devices SET phone_device_asset_id = 0 WHERE phone_device_id = $phone_device_id");
|
||||
$asset_id = 0;
|
||||
} else {
|
||||
$changed = phoneDeviceAssetFollow($device, $before, $asset);
|
||||
if ($changed) {
|
||||
$last = array_pop($changed);
|
||||
rmmAssetHistory($asset_id, $who, 'changed its ' . ($changed ? implode(', ', $changed) . " and $last" : $last));
|
||||
}
|
||||
return $asset_id;
|
||||
}
|
||||
}
|
||||
if (!$client_id) {
|
||||
return $asset_id;
|
||||
}
|
||||
|
||||
// The client's asset with its MAC address, that isn't another phone's
|
||||
$mac = escapeSql((string) $device['phone_device_mac']);
|
||||
$found = mysqli_fetch_row(mysqli_query($mysqli, "SELECT asset_id FROM assets JOIN asset_interfaces ON interface_asset_id = asset_id
|
||||
WHERE asset_client_id = $client_id AND asset_archived_at IS NULL
|
||||
AND LOWER(REPLACE(REPLACE(REPLACE(interface_mac, ':', ''), '-', ''), '.', '')) = '$mac'
|
||||
AND asset_id NOT IN (SELECT phone_device_asset_id FROM phone_devices WHERE phone_device_id <> $phone_device_id AND phone_device_asset_id > 0)
|
||||
ORDER BY interface_primary DESC, asset_id ASC LIMIT 1"));
|
||||
if ($found) {
|
||||
$asset_id = intval($found[0]);
|
||||
mysqli_query($mysqli, "UPDATE phone_devices SET phone_device_asset_id = $asset_id WHERE phone_device_id = $phone_device_id");
|
||||
// An asset made by hand: only what it hasn't got is filled in
|
||||
$blank = array_merge($device, ['phone_device_vendor' => '', 'phone_device_model' => '', 'phone_device_firmware' => '', 'phone_device_ip' => '', 'phone_device_line' => '', 'phone_device_mac' => '']);
|
||||
phoneDeviceAssetFollow($device, $blank, phoneDeviceAssetCurrent($asset_id));
|
||||
rmmAssetHistory($asset_id, $who, 'found it by its MAC address, and keeps it up from now on');
|
||||
return $asset_id;
|
||||
}
|
||||
if ($asset_id === -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
$name = phoneDeviceAssetName($device);
|
||||
$name_sql = escapeSql($name);
|
||||
$make = escapeSql((string) $device['phone_device_vendor']);
|
||||
$model = escapeSql((string) $device['phone_device_model']);
|
||||
$firmware = escapeSql((string) $device['phone_device_firmware']);
|
||||
mysqli_query($mysqli, "INSERT INTO assets SET asset_name = '$name_sql', asset_type = 'Phone', asset_make = '$make', asset_model = '$model',
|
||||
asset_os = '$firmware', asset_status = 'Deployed', asset_client_id = $client_id");
|
||||
$asset_id = intval(mysqli_insert_id($mysqli));
|
||||
if (!$asset_id) {
|
||||
return 0;
|
||||
}
|
||||
$mac_text = escapeSql(phoneMacText((string) $device['phone_device_mac']));
|
||||
$ip = escapeSql((string) $device['phone_device_ip']);
|
||||
mysqli_query($mysqli, "INSERT INTO asset_interfaces SET interface_name = '01', interface_mac = '$mac_text', interface_ip = '$ip', interface_primary = 1, interface_asset_id = $asset_id");
|
||||
phoneDeviceAssetExtension($asset_id, (string) $device['phone_device_line']);
|
||||
mysqli_query($mysqli, "UPDATE phone_devices SET phone_device_asset_id = $asset_id WHERE phone_device_id = $phone_device_id");
|
||||
rmmAssetHistory($asset_id, $who, 'added it - the phone told it about itself');
|
||||
logAudit('Asset', 'Create', escapeSql("$who added phone $name"), $client_id, $asset_id);
|
||||
return $asset_id;
|
||||
}
|
||||
|
||||
/** What a phone's asset has now (phoneDeviceAsset()) - or null when it is gone */
|
||||
function phoneDeviceAssetCurrent(int $asset_id): ?array
|
||||
{
|
||||
global $mysqli;
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT asset_id, asset_client_id, asset_name, asset_make, asset_model, asset_os,
|
||||
interface_id, interface_ip FROM assets
|
||||
LEFT JOIN asset_interfaces ON interface_asset_id = asset_id AND interface_primary = 1
|
||||
WHERE asset_id = $asset_id"));
|
||||
if (!$row) {
|
||||
return null;
|
||||
}
|
||||
$row['extension'] = (string) (function_exists('assetSpecs') && assetSpecsReady() ? (assetSpecs($asset_id)['extension'] ?? '') : '');
|
||||
return $row;
|
||||
}
|
||||
|
||||
/** A phone asset's Extension detail - set, when there is one */
|
||||
function phoneDeviceAssetExtension(int $asset_id, string $line): void
|
||||
{
|
||||
global $mysqli;
|
||||
if ($line === '' || !function_exists('assetSpecsReady') || !assetSpecsReady()) {
|
||||
return;
|
||||
}
|
||||
$line = escapeSql($line);
|
||||
mysqli_query($mysqli, "INSERT INTO asset_specs SET asset_spec_asset_id = $asset_id, asset_spec_key = 'extension', asset_spec_value = '$line'
|
||||
ON DUPLICATE KEY UPDATE asset_spec_value = VALUES(asset_spec_value)");
|
||||
}
|
||||
|
||||
/** Bring a phone's asset up to what the phone says, by the follow rule (phoneDeviceAsset()) - what changed, for its history */
|
||||
function phoneDeviceAssetFollow(array $device, array $before, ?array $asset): array
|
||||
{
|
||||
global $mysqli;
|
||||
if (!$asset) {
|
||||
return [];
|
||||
}
|
||||
$asset_id = intval($asset['asset_id']);
|
||||
// field => [label, what it has, what the phone says, what the phone said]
|
||||
$fields = [
|
||||
'asset_name' => ['name', $asset['asset_name'], phoneDeviceAssetName($device), phoneDeviceAssetName($before)],
|
||||
'asset_make' => ['make', $asset['asset_make'], $device['phone_device_vendor'], $before['phone_device_vendor']],
|
||||
'asset_model' => ['model', $asset['asset_model'], $device['phone_device_model'], $before['phone_device_model']],
|
||||
'asset_os' => ['firmware', $asset['asset_os'], $device['phone_device_firmware'], $before['phone_device_firmware']],
|
||||
'ip' => ['IP address', $asset['interface_ip'], $device['phone_device_ip'], $before['phone_device_ip']],
|
||||
'extension' => ['extension', $asset['extension'], $device['phone_device_line'], $before['phone_device_line']],
|
||||
];
|
||||
$changed = [];
|
||||
$sets = [];
|
||||
foreach ($fields as $field => [$label, $has, $says, $said]) {
|
||||
$has = (string) $has;
|
||||
$says = (string) $says;
|
||||
if ($says === '' || $has === $says || ($has !== '' && $has !== (string) $said)) {
|
||||
continue;
|
||||
}
|
||||
$changed[] = $label;
|
||||
if ($field === 'ip') {
|
||||
$ip = escapeSql($says);
|
||||
if ($asset['interface_id']) {
|
||||
mysqli_query($mysqli, "UPDATE asset_interfaces SET interface_ip = '$ip' WHERE interface_id = " . intval($asset['interface_id']));
|
||||
} else {
|
||||
$mac_text = escapeSql(phoneMacText((string) $device['phone_device_mac']));
|
||||
mysqli_query($mysqli, "INSERT INTO asset_interfaces SET interface_name = '01', interface_mac = '$mac_text', interface_ip = '$ip', interface_primary = 1, interface_asset_id = $asset_id");
|
||||
}
|
||||
} elseif ($field === 'extension') {
|
||||
phoneDeviceAssetExtension($asset_id, $says);
|
||||
} else {
|
||||
$sets[] = "$field = '" . escapeSql($says) . "'";
|
||||
}
|
||||
}
|
||||
if ($sets) {
|
||||
mysqli_query($mysqli, "UPDATE assets SET " . implode(', ', $sets) . " WHERE asset_id = $asset_id");
|
||||
}
|
||||
return $changed;
|
||||
}
|
||||
|
||||
/** Each night (cron/nightly_tasks.php): calls older than PHONE_CALLS_KEEP_DAYS that aren't on a ticket go */
|
||||
function phoneCleanup(): void
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user