unset($threads[$index]); } } usleep(200000); } } //update statuses for online # re-initialize classes $Database = new Database_PDO(); $Scan = new Scan($Database); // reset debugging $Scan->reset_debugging(false); # update all active statuses foreach ($addresses as $k => $a) { if ($a['newStatus'] == 0) { $Scan->ping_update_lastseen($a['id']); } } } # print change if ($Scan->debugging) { print "\nAddress changes:\n----------\n"; print_r($address_change); } # all done, mail diff? if (sizeof($address_change) > 0 && $send_mail) { if (!is_object(@$Scan)) { $Database = new Database_PDO(); $Subnets = new Subnets($Database); $Addresses = new Addresses($Database); $Tools = new Tools($Database);
/** * Read address functions * * identifiers can be: * - {id} * - {id}/ping/ // pings address * - /search/{ip_address}/ // searches for addresses in database, returns multiple if found * - custom_fields * - tags // all tags * - tags/{id}/ // specific tag * - tags/{id}/addresses // returns all addresses that are tagged with this tag ***if subnetId is provided it will be filtered to specific subnet * * @access public * @return void */ public function GET() { // subnet Id > read all addresses in subnet if ($this->_params->id == "custom_fields") { // check result if (sizeof($this->custom_fields) == 0) { $this->Response->throw_exception(404, 'No custom fields defined'); } else { return array("code" => 200, "data" => $this->custom_fields); } } elseif ($this->_params->id == "tags") { // validate $this->validate_tag(); // all addresses with tag if (@$this->_params->id3 == "addresses") { // fetch $result = $this->Tools->fetch_multiple_objects("ipaddresses", "state", $this->_params->id2); // filter by subnetId if ($result !== false) { if (isset($this->_params->subnetId)) { if (is_numeric($this->_params->subnetId)) { // filter foreach ($result as $k => $v) { if ($v->subnetId != $this->_params->subnetId) { unset($result[$k]); } } // any left if (sizeof($result) == 0) { $result = false; } } } } // result if ($result === false) { $this->Response->throw_exception(404, 'No addresses found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, "addresses", true, false)); } } else { // fetch all by tag if (isset($this->_params->id2)) { // numeric if (is_numeric($this->_params->id2)) { $result = $this->Tools->fetch_object("ipTags", "id", $this->_params->id2); } else { $result = $this->Tools->fetch_multiple_objects("ipTags", "type", $this->_params->id2); } } else { $result = $this->Tools->fetch_all_objects("ipTags"); } // result if ($result === false) { $this->Response->throw_exception(404, 'Tag not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, "addresses/tags", true, false)); } } } elseif (!isset($this->_params->id)) { $this->Response->throw_exception(400, 'Address ID is required'); } elseif (is_numeric($this->_params->id)) { // ping if (@$this->_params->id2 == "ping") { # scan class $Scan = new Scan($this->Database); $Scan->ping_set_exit(false); // check address $this->validate_address_id(); // set result $result['scan_type'] = $Scan->icmp_type; $result['exit_code'] = $Scan->ping_address($this->old_address->ip_addr); // success if ($result['exit_code'] == 0) { $Scan->ping_update_lastseen($this->_params->id); return array("code" => 200, "data" => $result); } else { $this->Response->throw_exception(404, "Address offline. Exit code: " . $result['exit_code'] . "( " . $Scan->ping_exit_explain($result['exit_code']) . " )"); } } else { // fetch $result = $this->Addresses->fetch_address("id", $this->_params->id); // check result if ($result == false) { $this->Response->throw_exception(404, "Invalid Id"); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, true, true)); } } } elseif (@$this->_params->id == "search") { // validate if (!$this->Addresses->validate_address($this->_params->id2)) { $this->Response->throw_exception(404, 'Invalid address'); } // search $result = $this->Tools->fetch_multiple_objects("ipaddresses", "ip_addr", $this->Subnets->transform_address($this->_params->id2, "decimal")); // check result if ($result === false) { $this->Response->throw_exception(404, 'Address not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, true, true)); } } else { $this->Response->throw_exception(400, "Invalid Id"); } }
print "<div style='display:none'>alert alert-danger</div>"; # result print "<div class='alert alert-warning'>"; print "<strong>" . _("Warning") . "</strong>: " . _("Are you sure you want to delete IP address") . "?"; print "<hr><div style='text-align:right'>"; print "<div class='btn-group'>"; print "\t<a class='btn btn-sm btn-danger editIPSubmitDelete' id='editIPSubmitDelete'>" . _("Confirm") . "</a>"; print "</div>"; print "</div>"; print "</div>"; } else { //fail if (!$Addresses->modify_address($address)) { $Result->show("danger", _('Error inserting IP address') . "!", false); } else { $Result->show("success", _("IP {$action} successful"), false); // try to ping if ($subnet['pingSubnet'] == "1" && $action == "add") { $pingRes = $Ping->ping_address($Subnets->transform_address($address['ip_addr'], "dotted")); // update status if ($pingRes == 0) { // print alive $Result->show("success", _("IP address") . " " . $Subnets->transform_address($address['ip_addr'], "dotted") . " " . _("is alive"), false); // update status @$Ping->ping_update_lastseen($Addresses->lastId); } } } } } }
unset($threads[$index]); } } usleep(200000); } } //update statuses for online # re-initialize classes $Database = new Database_PDO(); $Scan = new Scan($Database, $Subnets->settings); // reset debugging $Scan->reset_debugging(false); # update all active statuses foreach ($addresses as $k => $a) { if ($a['newStatus'] == 0) { $Scan->ping_update_lastseen($a['id'], $nowdate); } } } /** * Now check for diffs - if time change between lastSeenOld and lastSeen > statuses[1] */ // loop foreach ($address_change as $k => $change) { // null old - set to epoch time if (strtotime($change['lastSeenOld']) === false) { $change['lastSeenOld'] = date("Y-m-d H:i:s", 0); } // set general diffs $deviceDiff = $now - strtotime($change['lastSeenOld']); // now - device last seen
$Ping = new Scan($Database, $User->settings); # verify that user is logged in $User->check_user_session(); # validate post is_numeric($_POST['subnetId']) ?: $Result->show("danger", _("Invalid ID"), true); is_numeric($_POST['id']) || strlen($_POST['id']) == 0 ?: $Result->show("danger", _("Invalid ID"), true); # set and check permissions $subnet_permission = $Subnets->check_permission($User->user, $_POST['subnetId']); $subnet_permission > 2 ?: $Result->show("danger", _('Cannot edit IP address details') . '! <br>' . _('You do not have write access for this network'), true, true); # fetch address $address = (array) $Addresses->fetch_address(null, $_POST['id']); # try to ping it $pingRes = $Ping->ping_address($address['ip']); # update last seen if success if ($pingRes == 0) { @$Ping->ping_update_lastseen($address['id']); } ?> <!-- header --> <div class="pHeader"><?php print _('Ping check result'); ?> </div> <!-- content --> <div class="pContent"> <?php # online if ($pingRes == 0) { $Result->show("success", _("IP address") . " {$address['ip']} " . _("is alive"), false);
/** * Read address functions * * identifiers can be: * - /addresses/{id}/ * - /addresses/{id}/ping/ // pings address * - /addresses/{ip}/{subnetId}/ // Returns address from subnet * - /addresses/search/{ip_address}/ // searches for addresses in database, returns multiple if found * - /addresses/search_hostname/{hostname}/ // searches for addresses in database by hostname, returns multiple if found * - /addresses/search_hostbase/{hostbase}/ // searches for addresses by leading substring (base) of hostname, returns ordered multiple * - /addresses/first_free/{subnetId}/ // returns first available address (subnetId can be provided with parameters) * - /addresses/custom_fields/ // custom fields * - /addresses/tags/ // all tags * - /addresses/tags/{id}/ // specific tag * - /addresses/tags/{id}/addresses/ // returns all addresses that are tagged with this tag ***if subnetId is provided it will be filtered to specific subnet * * @access public * @return void */ public function GET() { // subnet Id > read all addresses in subnet if ($this->_params->id == "custom_fields") { // check result if (sizeof($this->custom_fields) == 0) { $this->Response->throw_exception(404, 'No custom fields defined'); } else { return array("code" => 200, "data" => $this->custom_fields); } } elseif ($this->_params->id == "first_free") { // check for isFull if (isset($this->_params->subnetId)) { $subnet = $this->Tools->fetch_object("subnets", "id", $this->_params->subnetId); } else { $subnet = $this->Tools->fetch_object("subnets", "id", $this->_params->id2); } if ($subnet->isFull == 1) { $this->Response->throw_exception(404, "No free addresses found"); } $this->_params->ip_addr = $this->Addresses->get_first_available_address($subnet->id, $this->Subnets); // null if ($this->_params->ip_addr == false) { $this->Response->throw_exception(404, 'No free addresses found'); } else { return array("code" => 200, "data" => $this->Addresses->transform_address($this->_params->ip_addr, "dotted")); } } elseif ($this->Tools->validate_ip($this->_params->id) !== false && isset($this->_params->id2)) { // fetch all in subnet $result = $this->Tools->fetch_multiple_objects("ipaddresses", "subnetId", $this->_params->id2); if ($result !== false) { foreach ($result as $k => $r) { if ($r->ip !== $this->_params->id) { unset($result[$k]); } else { $result_filtered = $r; } } if (sizeof($result) == 0) { $result = false; } else { $result = $result_filtered; } } if ($result == false) { $this->Response->throw_exception(404, 'No addresses found'); } else { return array("code" => 200, "data" => $result); } } elseif ($this->_params->id == "tags") { // validate $this->validate_tag(); // all addresses with tag if (@$this->_params->id3 == "addresses") { // fetch $result = $this->Tools->fetch_multiple_objects("ipaddresses", "state", $this->_params->id2); // filter by subnetId if ($result !== false) { if (isset($this->_params->subnetId)) { if (is_numeric($this->_params->subnetId)) { // filter foreach ($result as $k => $v) { if ($v->subnetId != $this->_params->subnetId) { unset($result[$k]); } } // any left if (sizeof($result) == 0) { $result = false; } } } } // result if ($result === false) { $this->Response->throw_exception(404, 'No addresses found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, "addresses", true, false)); } } else { // fetch all by tag if (isset($this->_params->id2)) { // numeric if (is_numeric($this->_params->id2)) { $result = $this->Tools->fetch_object("ipTags", "id", $this->_params->id2); } else { $result = $this->Tools->fetch_multiple_objects("ipTags", "type", $this->_params->id2); } } else { $result = $this->Tools->fetch_all_objects("ipTags"); } // result if ($result === false) { $this->Response->throw_exception(404, 'Tag not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, "addresses/tags", true, false)); } } } elseif (!isset($this->_params->id)) { $this->Response->throw_exception(400, 'Address ID is required'); } elseif (is_numeric($this->_params->id)) { // ping if (@$this->_params->id2 == "ping") { # scan class $Scan = new Scan($this->Database); $Scan->ping_set_exit(false); // check address $this->validate_address_id(); // set result $result = array(); $result['scan_type'] = $Scan->icmp_type; $result['exit_code'] = $Scan->ping_address($this->old_address->ip); $result['result_code'] = $Scan->ping_exit_explain($result['exit_code']); $result['message'] = $result['exit_code'] == 0 ? "Address online" : "Address offline"; // success if ($result['exit_code'] == 0) { $Scan->ping_update_lastseen($this->_params->id); } return array("code" => 200, "data" => $result); } else { // fetch $result = $this->Addresses->fetch_address("id", $this->_params->id); // check result if ($result == false) { $this->Response->throw_exception(404, "Invalid Id"); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, true, true)); } } } elseif (@$this->_params->id == "search") { // validate if (!$this->Addresses->validate_address($this->_params->id2)) { $this->Response->throw_exception(404, 'Invalid address'); } // search $result = $this->Tools->fetch_multiple_objects("ipaddresses", "ip_addr", $this->Subnets->transform_address($this->_params->id2, "decimal")); // check result if ($result === false) { $this->Response->throw_exception(404, 'Address not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, true, true)); } } elseif (@$this->_params->id == "search_hostname") { $result = $this->Tools->fetch_multiple_objects("ipaddresses", "dns_name", $this->_params->id2); // check result if ($result === false) { $this->Response->throw_exception(404, 'Host name not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, false, false)); } } elseif (@$this->_params->id == "search_hostbase") { $target = $this->_params->id2 . "%"; $result = $this->Tools->fetch_multiple_objects("ipaddresses", "dns_name", $target, "dns_name", true, true); // check result if ($result === false) { $this->Response->throw_exception(404, 'Host name not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, false, false)); } } else { $this->Response->throw_exception(400, "Invalid Id"); } }