getMessage()); exit($exception->getMessage()); } } else { LOG_VERBOSE($db_link,"API: Not found."); } } else { LOG_VERBOSE($db_link,"API: not enough parameters"); } } //return user auth record if ($action ==='get_dhcp_all') { $result=[]; LOG_VERBOSE($db_link,"API: Get all dhcp records"); $sql = "SELECT ua.id, ua.ip, ua.ip_int, ua.mac, ua.comments, ua.dns_name, ua.dhcp_option_set, ua.dhcp_acl, ua.ou_id, SUBSTRING_INDEX(s.subnet, '/', 1) AS subnet_base FROM User_auth ua JOIN subnets s ON ua.ip_int BETWEEN s.ip_int_start AND s.ip_int_stop WHERE ua.dhcp = 1 AND ua.deleted = 0 AND s.dhcp = 1 ORDER BY ua.ip_int"; $result = get_records_sql($db_link, $sql); if (!empty($result)) { LOG_VERBOSE($db_link, "API: " . count($result) . " records found."); try { header('Content-Type: application/json'); echo json_encode($result, JSON_THROW_ON_ERROR); } catch (JsonException $exception) { LOG_ERROR($db_link, "API: JSON encoding error: " . $exception->getMessage()); exit("JSON error"); } } else { LOG_VERBOSE($db_link, "API: No records found."); header('Content-Type: application/json'); echo json_encode([]); } } //return user auth record if ($action ==='get_dhcp_subnet' and !empty($f_subnet)) { $result=[]; $f_subnet = trim($f_subnet, "'"); LOG_VERBOSE($db_link,"API: Get dhcp records for subnet ".$f_subnet); $sql = "SELECT ua.id, ua.ip, ua.ip_int, ua.mac, ua.comments, ua.dns_name, ua.dhcp_option_set, ua.dhcp_acl, ua.ou_id, SUBSTRING_INDEX(s.subnet, '/', 1) AS subnet_base FROM User_auth ua JOIN subnets s ON ua.ip_int BETWEEN s.ip_int_start AND s.ip_int_stop WHERE ua.dhcp = 1 AND ua.deleted = 0 AND s.dhcp = 1 AND SUBSTRING_INDEX(s.subnet, '/', 1) = '".$f_subnet."' ORDER BY ua.ip_int"; $result = get_records_sql($db_link, $sql); if (!empty($result)) { LOG_VERBOSE($db_link, "API: " . count($result) . " records found."); try { header('Content-Type: application/json'); echo json_encode($result, JSON_THROW_ON_ERROR); } catch (JsonException $exception) { LOG_ERROR($db_link, "API: JSON encoding error: " . $exception->getMessage()); exit("JSON error"); } } else { LOG_VERBOSE($db_link, "API: No records found."); header('Content-Type: application/json'); echo json_encode([]); } } //add dhcp log record if ($action ==='send_dhcp') { if (!empty($ip) and !empty($mac)) { $dhcp_hostname = ''; if (!empty($_GET["hostname"])) { $dhcp_hostname = trim($_GET["hostname"]); } if (!empty($_POST["hostname"])) { $dhcp_hostname = trim($_POST["hostname"]); } $faction = $_GET["action"] * 1; $dhcp_action = 'add'; if ($faction == 1) { $dhcp_action = 'add'; } if ($faction == 0) { $dhcp_action = 'del'; } LOG_VERBOSE($db_link, "API: external dhcp request for $ip [$mac] $dhcp_action"); if (checkValidIp($ip) and is_our_network($db_link, $ip)) { $new['action']=$dhcp_action; $new['mac']=$mac; $new['ip']=$ip; $new['dhcp_hostname']=$dhcp_hostname; insert_record($db_link,"dhcp_queue",$new); } else { LOG_ERROR($db_link, "$ip - wrong network!"); } } } } else { LOG_WARNING($db_link,"API: Unknown request"); } unset($_GET); unset($_POST); logout($db_link,TRUE); ?>