Пример #1
0
function renderDiscoveredNeighbors($object_id)
{
    global $tabno;
    $opcode_by_tabno = array('livecdp' => 'getcdpstatus', 'livelldp' => 'getlldpstatus');
    try {
        $neighbors = queryDevice($object_id, $opcode_by_tabno[$tabno]);
        $neighbors = sortPortList($neighbors);
    } catch (RTGatewayError $e) {
        showError($e->getMessage());
        return;
    }
    $mydevice = spotEntity('object', $object_id);
    amplifyCell($mydevice);
    // reindex by port name
    $myports = array();
    foreach ($mydevice['ports'] as $port) {
        if (mb_strlen($port['name'])) {
            $myports[$port['name']][] = $port;
        }
    }
    // scroll to selected port
    if (isset($_REQUEST['hl_port_id'])) {
        assertUIntArg('hl_port_id');
        $hl_port_id = intval($_REQUEST['hl_port_id']);
        addAutoScrollScript("port-{$hl_port_id}");
    }
    switchportInfoJS($object_id);
    // load JS code to make portnames interactive
    printOpFormIntro('importDPData');
    echo '<br><table cellspacing=0 cellpadding=5 align=center class=widetable>';
    echo '<tr><th colspan=2>local port</th><th></th><th>remote device</th><th colspan=2>remote port</th><th><input type="checkbox" checked id="cb-toggle"></th></tr>';
    $inputno = 0;
    foreach ($neighbors as $local_port => $remote_list) {
        $initial_row = TRUE;
        // if port has multiple neighbors, the first table row is initial
        // array of local ports with the name specified by DP
        $local_ports = isset($myports[$local_port]) ? $myports[$local_port] : array();
        foreach ($remote_list as $dp_neighbor) {
            $error_message = NULL;
            $link_matches = FALSE;
            $portinfo_local = NULL;
            $portinfo_remote = NULL;
            $variants = array();
            do {
                // once-cyle fake loop used only to break out of it
                if (!empty($local_ports)) {
                    $portinfo_local = $local_ports[0];
                }
                // find remote object by DP information
                $dp_remote_object_id = searchByMgmtHostname($dp_neighbor['device']);
                if (!$dp_remote_object_id) {
                    $dp_remote_object_id = lookupEntityByString('object', $dp_neighbor['device']);
                }
                if (!$dp_remote_object_id) {
                    $error_message = "No such neighbor <i>{$dp_neighbor['device']}</i>";
                    break;
                }
                $dp_remote_object = spotEntity('object', $dp_remote_object_id);
                amplifyCell($dp_remote_object);
                $dp_neighbor['port'] = shortenIfName($dp_neighbor['port'], NULL, $dp_remote_object['id']);
                // get list of ports that have name matching CDP portname
                $remote_ports = array();
                // list of remote (by DP info) ports
                foreach ($dp_remote_object['ports'] as $port) {
                    if ($port['name'] == $dp_neighbor['port']) {
                        $portinfo_remote = $port;
                        $remote_ports[] = $port;
                    }
                }
                // check if ports with such names exist on devices
                if (empty($local_ports)) {
                    $error_message = "No such local port <i>{$local_port}</i>";
                    break;
                }
                if (empty($remote_ports)) {
                    $error_message = "No such port on " . formatPortLink($dp_remote_object['id'], $dp_remote_object['name'], NULL, NULL);
                    break;
                }
                // determine match or mismatch of local link
                foreach ($local_ports as $portinfo_local) {
                    if ($portinfo_local['remote_id']) {
                        if ($portinfo_local['remote_object_id'] == $dp_remote_object_id and $portinfo_local['remote_name'] == $dp_neighbor['port']) {
                            // set $portinfo_remote to corresponding remote port
                            foreach ($remote_ports as $portinfo_remote) {
                                if ($portinfo_remote['id'] == $portinfo_local['remote_id']) {
                                    break;
                                }
                            }
                            $link_matches = TRUE;
                            unset($error_message);
                        } elseif ($portinfo_local['remote_object_id'] != $dp_remote_object_id) {
                            $error_message = "Remote device mismatch - port linked to " . formatLinkedPort($portinfo_local);
                        } else {
                            // ($portinfo_local['remote_name'] != $dp_neighbor['port'])
                            $error_message = "Remote port mismatch - port linked to " . formatPortLink($portinfo_local['remote_object_id'], NULL, $portinfo_local['remote_id'], $portinfo_local['remote_name']);
                        }
                        break 2;
                    }
                }
                // no local links found, try to search for remote links
                foreach ($remote_ports as $portinfo_remote) {
                    if ($portinfo_remote['remote_id']) {
                        $remote_link_html = formatLinkedPort($portinfo_remote);
                        $remote_port_html = formatPortLink($portinfo_remote['object_id'], NULL, $portinfo_remote['id'], $portinfo_remote['name']);
                        $error_message = "Remote port {$remote_port_html} is already linked to {$remote_link_html}";
                        break 2;
                    }
                }
                // no links found on both sides, search for a compatible port pair
                $port_types = array();
                foreach (array('left' => $local_ports, 'right' => $remote_ports) as $side => $port_list) {
                    foreach ($port_list as $portinfo) {
                        $tmp_types = $portinfo['iif_id'] == 1 ? array($portinfo['oif_id'] => $portinfo['oif_name']) : getExistingPortTypeOptions($portinfo['id']);
                        foreach ($tmp_types as $oif_id => $oif_name) {
                            $port_types[$side][$oif_id][] = array('id' => $oif_id, 'name' => $oif_name, 'portinfo' => $portinfo);
                        }
                    }
                }
                foreach ($port_types['left'] as $left_id => $left) {
                    foreach ($port_types['right'] as $right_id => $right) {
                        if (arePortTypesCompatible($left_id, $right_id)) {
                            foreach ($left as $left_port) {
                                foreach ($right as $right_port) {
                                    $variants[] = array('left' => $left_port, 'right' => $right_port);
                                }
                            }
                        }
                    }
                }
                if (!count($variants)) {
                    // no compatible ports found
                    $error_message = "Incompatible port types";
                }
            } while (FALSE);
            // do {
            $tr_class = $link_matches ? 'trok' : (isset($error_message) ? 'trerror' : 'trwarning');
            echo "<tr class=\"{$tr_class}\">";
            if ($initial_row) {
                $count = count($remote_list);
                $td_class = '';
                if (isset($hl_port_id) and $hl_port_id == $portinfo_local['id']) {
                    $td_class = "class='border_highlight'";
                }
                echo "<td rowspan=\"{$count}\" {$td_class} NOWRAP>" . ($portinfo_local ? formatPortLink($mydevice['id'], NULL, $portinfo_local['id'], $portinfo_local['name'], 'interactive-portname port-menu') : "<a class='interactive-portname port-menu nolink'>{$local_port}</a>") . ($count > 1 ? "<br> ({$count} neighbors)" : '') . '</td>';
                $initial_row = FALSE;
            }
            echo "<td>" . ($portinfo_local ? formatPortIIFOIF($portinfo_local) : '&nbsp;') . "</td>";
            echo "<td>" . formatIfTypeVariants($variants, "ports_{$inputno}") . "</td>";
            echo "<td>{$dp_neighbor['device']}</td>";
            echo "<td>" . ($portinfo_remote ? formatPortLink($dp_remote_object_id, NULL, $portinfo_remote['id'], $portinfo_remote['name']) : $dp_neighbor['port']) . "</td>";
            echo "<td>" . ($portinfo_remote ? formatPortIIFOIF($portinfo_remote) : '&nbsp;') . "</td>";
            echo "<td>";
            if (!empty($variants)) {
                echo "<input type=checkbox name=do_{$inputno} class='cb-makelink'>";
                $inputno++;
            }
            echo "</td>";
            if (isset($error_message)) {
                echo "<td style=\"background-color: white; border-top: none\">{$error_message}</td>";
            }
            echo "</tr>";
        }
    }
    if ($inputno) {
        echo "<input type=hidden name=nports value={$inputno}>";
        echo '<tr><td colspan=7 align=center>' . getImageHREF('CREATE', 'import selected', TRUE) . '</td></tr>';
    }
    echo '</table></form>';
    addJS(<<<END
\$(document).ready(function () {
\t\$('#cb-toggle').click(function (event) {
\t\tvar list = \$('.cb-makelink');
\t\tfor (var i in list) {
\t\t\tvar cb = list[i];
\t\t\tcb.checked = event.target.checked;
\t\t}
\t}).triggerHandler('click');
});
END
, TRUE);
}
Пример #2
0
function searchEntitiesByText($terms)
{
    $summary = array();
    if (preg_match(RE_IP4_ADDR, $terms)) {
        if ($net_id = getIPv4AddressNetworkId(ip4_parse($terms))) {
            $summary['ipv4addressbydq'][$terms] = array('net_id' => $net_id, 'ip' => $terms);
        }
    } elseif (FALSE !== ($ip_bin = ip6_checkparse($terms))) {
        if ($net_id = getIPv6AddressNetworkId($ip_bin)) {
            $summary['ipv6addressbydq'][$net_id] = array('net_id' => $net_id, 'ip' => $ip_bin);
        }
    } elseif (preg_match(RE_IP4_NET, $terms)) {
        list($base, $len) = explode('/', $terms);
        if (NULL !== ($net_id = getIPv4AddressNetworkId(ip4_parse($base), $len + 1))) {
            $summary['ipv4network'][$net_id] = spotEntity('ipv4net', $net_id);
        }
    } elseif (preg_match('@(.*)/(\\d+)$@', $terms, $matches) && FALSE !== ($ip_bin = ip6_checkparse($matches[1]))) {
        if (NULL !== ($net_id = getIPv6AddressNetworkId($ip_bin, $matches[2] + 1))) {
            $summary['ipv6network'][$net_id] = spotEntity('ipv6net', $net_id);
        }
    } else {
        // search by FQDN has special treatment - if single object found, do not search by other fields
        $object_id_by_fqdn = NULL;
        $domains = preg_split('/\\s*,\\s*/', strtolower(getConfigVar('SEARCH_DOMAINS')));
        if (!empty($domains) and $object_id = searchByMgmtHostname($terms)) {
            // get FQDN
            $attrs = getAttrValues($object_id);
            $fqdn = '';
            if (isset($attrs[3]['value'])) {
                $fqdn = strtolower(trim($attrs[3]['value']));
            }
            foreach ($domains as $domain) {
                if ('.' . $domain === substr($fqdn, -strlen($domain) - 1)) {
                    $object_id_by_fqdn = $object_id;
                    break;
                }
            }
        }
        if ($object_id_by_fqdn) {
            $summary['object'][$object_id_by_fqdn] = array('id' => $object_id_by_fqdn, 'method' => 'fqdn');
        } else {
            $summary['object'] = getObjectSearchResults($terms);
            $summary['ipv4addressbydescr'] = getIPv4AddressSearchResult($terms);
            $summary['ipv6addressbydescr'] = getIPv6AddressSearchResult($terms);
            $summary['ipv4network'] = getIPv4PrefixSearchResult($terms);
            $summary['ipv6network'] = getIPv6PrefixSearchResult($terms);
            $summary['ipv4rspool'] = getIPv4RSPoolSearchResult($terms);
            $summary['ipv4vs'] = getIPv4VServiceSearchResult($terms);
            $summary['user'] = getAccountSearchResult($terms);
            $summary['file'] = getFileSearchResult($terms);
            $summary['rack'] = getRackSearchResult($terms);
            $summary['vlan'] = getVLANSearchResult($terms);
        }
    }
    # Filter search results in a way in some realms to omit records, which the
    # user would not be able to browse anyway.
    if (isset($summary['object'])) {
        foreach ($summary['object'] as $key => $record) {
            if (!isolatedPermission('object', 'default', spotEntity('object', $record['id']))) {
                unset($summary['object'][$key]);
            }
        }
    }
    if (isset($summary['ipv4network'])) {
        foreach ($summary['ipv4network'] as $key => $netinfo) {
            if (!isolatedPermission('ipv4net', 'default', $netinfo)) {
                unset($summary['ipv4network'][$key]);
            }
        }
    }
    if (isset($summary['ipv6network'])) {
        foreach ($summary['ipv6network'] as $key => $netinfo) {
            if (!isolatedPermission('ipv6net', 'default', $netinfo)) {
                unset($summary['ipv6network'][$key]);
            }
        }
    }
    if (isset($summary['file'])) {
        foreach ($summary['file'] as $key => $fileinfo) {
            if (!isolatedPermission('file', 'default', $fileinfo)) {
                unset($summary['file'][$key]);
            }
        }
    }
    // clear empty search result realms
    foreach ($summary as $key => $data) {
        if (!count($data)) {
            unset($summary[$key]);
        }
    }
    return $summary;
}