diff --git a/agent/ajax.php b/agent/ajax.php index 74cc0836c..799c08f9c 100644 --- a/agent/ajax.php +++ b/agent/ajax.php @@ -913,6 +913,47 @@ if (isset($_GET['client_duplicate_check'])) { echo json_encode($response); } +/* + * Live check behind the IP address field on the add/edit network IP modals. + * + * Runs the exact same checkIpForNetwork() the POST handler runs, so what the + * field says while you type is what the save will do - no second copy of the + * rules to drift. It's advisory only; the handler still checks on submit. + */ +if (isset($_GET['network_ip_check'])) { + enforceUserPermission('module_support', 2); + + $network_id = intval($_GET['network_id'] ?? 0); + $ip_id = intval($_GET['ip_id'] ?? 0); + + $client_id = intval(getFieldById('networks', $network_id, 'network_client_id')); + + enforceClientAccess(); + + $ip = $_GET['ip'] ?? ''; + + $response = ['ok' => false, 'ip' => '', 'message' => '']; + + if (trim($ip) !== '') { + + $ip_error = checkIpForNetwork($ip, $network_id, $ip_id); + + if ($ip_error === '') { + $response['ok'] = true; + $response['ip'] = $ip; + $response['message'] = "" . escapeHtml($ip) . " is available"; + } else { + // checkIpForNetwork() builds its message for flashAlert(), which + // escapes at render - this lands in innerHTML instead, so it has + // to be escaped here + $response['message'] = "" . alertMessageHtml($ip_error); + } + + } + + echo json_encode($response); +} + if (isset($_GET['contact_email_check'])) { enforceUserPermission('module_client', 2); diff --git a/agent/modals/network/network_ip_add.php b/agent/modals/network/network_ip_add.php index 0f85b61ce..d4e504a9e 100644 --- a/agent/modals/network/network_ip_add.php +++ b/agent/modals/network/network_ip_add.php @@ -15,6 +15,11 @@ $client_id = intval($row['network_client_id']); enforceClientAccess(); +// The part of the address this subnet fixes. Empty for IPv6, a prefix under +// /8, or a subnet that won't parse - then the field takes a whole address. +$ip_fixed_octets = ipSubnetFixedOctets($row['network']); +$ip_host_octets = $ip_fixed_octets ? 4 - substr_count($ip_fixed_octets, '.') : 0; + ob_start(); ?> @@ -31,9 +36,19 @@ ob_start();