function ws_editor($window_name, $form = '') { global $conf, $self, $onadb; global $font_family, $color, $style, $images; $window = array(); // Check permissions if (!(auth('host_modify') or auth('host_add'))) { $response = new xajaxResponse(); $response->addScript("alert('Permission denied!');"); return $response->getXML(); } // If an array in a string was provided, build the array and store it in $form $form = parse_options_string($form); // Load an existing host record (and associated info) if $form is a host_id $host = array('fqdn' => '.', 'domain_fqdn' => $conf['dns_defaultdomain']); $interface = array(); if (is_numeric($form['host_id'])) { list($status, $rows, $host) = ona_get_host_record(array('id' => $form['host_id'])); if ($rows) { // Load associated INTERFACE record(s) list($status, $interfaces, $interface) = ona_get_interface_record(array('host_id' => $host['id'])); list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $interface['subnet_id'])); $interface['ip_addr'] = ip_mangle($interface['ip_addr'], 'dotted'); if ($interface['mac_addr']) { $interface['mac_addr'] = mac_mangle($interface['mac_addr'], 2); } //FIXME: (PK) should not use numeric format specifier here! } } else { if (strlen($form['ip_addr']) > 1) { $interface['ip_addr'] = $form['ip_addr']; } if (strlen($form['hostname']) > 1) { $host['name'] = $form['hostname']; } } // Set the default security level if there isn't one if (!array_key_exists('lvl', $host)) { $host['lvl'] = $conf['ona_lvl']; } // Load a subnet record if we got passed a subnet_id if ($form['subnet_id']) { list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $form['subnet_id'])); } // Load a domain record if we got passed a domain_id if ($form['domain_id']) { list($status, $rows, $domain) = ona_get_domain_record(array('id' => $form['domain_id'])); $host['domain_fqdn'] = $domain['fqdn']; } // If we dont have a domain_fqdn yet.. lets use the system default domain if (!$host['domain_fqdn']) { $host['domain_fqdn'] = $conf['dns_defaultdomain']; } // Build a device_types list list($status, $rows, $records) = db_get_records($onadb, 'device_types', 'id >= 1'); $device_types = array(); foreach ($records as $type) { list($status, $rows, $model) = ona_get_model_record(array('id' => $type['model_id'])); list($status, $rows, $role) = ona_get_role_record(array('id' => $type['role_id'])); list($status, $rows, $manufacturer) = ona_get_manufacturer_record(array('id' => $model['manufacturer_id'])); $device_types[$type['id']] = "{$manufacturer['name']} {$model['name']} ({$role['name']})"; } asort($device_types); list($status, $rows, $device) = ona_get_device_record(array('id' => $host['device_id'])); list($status, $rows, $location) = ona_get_location_record(array('id' => $device['location_id'])); $host['location'] = $location['reference']; $device_model_list = "<option value=\"\"></option>\n"; foreach (array_keys((array) $device_types) as $id) { $device_types[$id] = htmlentities($device_types[$id]); $selected = ''; if ($id == $device['device_type_id']) { $selected = 'SELECTED'; } $device_model_list .= "<option value=\"{$id}\" {$selected}>{$device_types[$id]}</option>\n"; } unset($device_types, $device, $manufacturer, $role, $model, $records); //Get the list of DNS views if ($conf['dns_views']) { list($status, $rows, $dnsviews) = db_get_records($onadb, 'dns_views', 'id >= 0', 'name'); foreach ($dnsviews as $entry) { $selected = ''; //$dnsviews['name'] = htmlentities($dnsviews['name']); // If this entry matches the record you are editing, set it to selected if ($host['id'] and $entry['id'] == $host['dns_view_id']) { $selected = "SELECTED=\"selected\""; $dns_view_name = $entry['name'] . '/'; } elseif (!$host['id'] and $entry['id'] == 0) { // Otherwise use the default record if we are adding a new entry $selected = "SELECTED=\"selected\""; $dns_view_name = $entry['name'] . '/'; } $dns_view_list .= "<option {$selected} value=\"{$entry['id']}\">{$entry['name']}</option>\n"; } } // Escape data for display in html foreach (array_keys((array) $host) as $key) { $host[$key] = htmlentities($host[$key], ENT_QUOTES, $conf['php_charset']); } foreach (array_keys((array) $subnet) as $key) { $subnet[$key] = htmlentities($subnet[$key], ENT_QUOTES, $conf['php_charset']); } foreach (array_keys((array) $interface) as $key) { $interface[$key] = htmlentities($interface[$key], ENT_QUOTES, $conf['php_charset']); } // Set the window title: $window['title'] = "Add Host"; if ($host['id']) { $window['title'] = "Edit Host"; } else { unset($dns_view_name); } // Javascript to run after the window is built $window['js'] = <<<EOL /* Put a minimize icon in the title bar */ el('{$window_name}_title_r').innerHTML = ' <a onClick="toggle_window(\\'{$window_name}\\');" title="Minimize window" style="cursor: pointer;"><img src="{$images}/icon_minimize.gif" border="0" /></a>' + el('{$window_name}_title_r').innerHTML; /* Put a help icon in the title bar */ el('{$window_name}_title_r').innerHTML = ' <a href="{$_ENV['help_url']}{$window_name}" target="null" title="Help" style="cursor: pointer;"><img src="{$images}/silk/help.png" border="0" /></a>' + el('{$window_name}_title_r').innerHTML; suggest_setup('set_domain_{$window_name}', 'suggest_set_domain_{$window_name}'); suggest_setup('set_location_{$window_name}', 'suggest_set_location_{$window_name}'); /* Setup the Quick Find location icon */ var _button = el('qf_location_{$window_name}'); _button.style.cursor = 'pointer'; _button.onclick = function(ev) { if (!ev) ev = event; /* Create the popup div */ wwTT(this, ev, 'id', 'tt_qf_location_{$window_name}', 'type', 'static', 'direction', 'south', 'delay', 0, 'styleClass', 'wwTT_qf', 'javascript', "xajax_window_submit('tooltips', '" + "tooltip=>qf_location," + "id=>tt_qf_location_{$window_name}," + "input_id=>set_location_{$window_name}');" ); }; el('set_host').focus(); EOL; // If we are modifying do not allow them to edit/change dns names. this should only be done when creating a new host $hideit = ''; if ($host['id']) { $hideit = 'style="display: none;"'; } // Define the window's inner html $window['html'] = <<<EOL <!-- Host Edit Form --> <form id="{$window_name}_edit_form" onSubmit="return false;"> <input type="hidden" name="host" value="{$dns_view_name}{$host['fqdn']}"> <input type="hidden" name="interface" value="{$interface['id']}"> <input type="hidden" name="js" value="{$form['js']}"> <table cellspacing="0" border="0" cellpadding="0" style="background-color: {$color['window_content_bg']}; padding-left: 20px; padding-right: 20px; padding-top: 5px; padding-bottom: 5px;"> <!-- HOST RECORD --> <tr> <td align="left" nowrap="true"> <b><u>Host Record</u></b> </td> <td class="padding" align="left" width="100%"> </td> </tr> EOL; // Print a dns view selector if ($conf['dns_views']) { $window['html'] .= <<<EOL <tr {$hideit}> <td align="right" nowrap="true"> DNS View </td> <td class="padding" align="left" width="100%"> <select id="dns_view_select" name="set_view" alt="DNS View" class="edit" >{$dns_view_list}</select> </td> </tr> EOL; } $window['html'] .= <<<EOL <tr {$hideit}> <td class="input_required" align="right" nowrap="true"> DNS Name </td> <td class="padding" align="left" width="100%"> <input id="set_host" name="set_host" alt="Hostname" value="{$host['name']}" class="edit" type="text" size="20" maxlength="64" > </td> </tr> <tr {$hideit}> <td class="input_required" align="right" nowrap="true"> Domain </td> <td class="padding" align="left" width="100%"> <input id="set_domain_{$window_name}" name="set_domain" alt="Domain name" value="{$host['domain_fqdn']}" class="edit" type="text" size="25" maxlength="64" > <div id="suggest_set_domain_{$window_name}" class="suggest"></div> </td> </tr> <tr> <td class="input_required" align="right" nowrap="true"> Device type </td> <td class="padding" align="left" width="100%"> <select name="set_type" alt="Device type" class="edit" >{$device_model_list}</select> </td> </tr> <tr> <td align="right" nowrap="true"> Notes </td> <td class="padding" align="left" width="100%"> <textarea name="set_notes" class="edit" cols="40" rows="1">{$host['notes']}</textarea> </td> </tr> <tr> <td align="right" nowrap="true"> Location </td> <td class="padding" align="left" width="100%"> <input id="set_location_{$window_name}" name="set_location" alt="Location" value="{$host['location']}" class="edit" type="text" size="7" maxlength="10" > <div id="suggest_set_location_{$window_name}" class="suggest"></div> <span id="qf_location_{$window_name}" title="Location Quick Search"><img src="{$images}/silk/find.png" border="0"/></span> </td> </tr> EOL; // Display an interface edit section if it's a new host or there were exactly one interface. if (!$interfaces) { $window['js'] .= <<<EOL /* Setup the Quick Find for available IPs */ var _button = el('qf_free_ip_{$window_name}'); _button.style.cursor = 'pointer'; _button.onclick = function(ev) { if (!ev) ev = event; /* Create the popup div */ wwTT(this, ev, 'id', 'tt_qf_free_ip_{$window_name}', 'type', 'static', 'direction', 'south', 'delay', 0, 'styleClass', 'wwTT_qf', 'javascript', "xajax_window_submit('tooltips', '" + "tooltip=>qf_free_ip," + "id=>tt_qf_free_ip_{$window_name}," + "text_id=>associated_subnet_{$window_name}," + "text_value=>" + el('associated_subnet_{$window_name}').innerHTML + "," + "input_id=>set_ip_{$window_name}');" ); }; EOL; $window['html'] .= <<<EOL <!-- FIRST INTERFACE --> <tr> <td align="left" nowrap="true"> <b><u>Interface</u></b> </td> <td class="padding" align="left" width="100%"> </td> </tr> <tr> <td align="right" nowrap="true"> Subnet </td> <td class="padding" align="left" width="100%" nowrap="true"> <span id="associated_subnet_{$window_name}" >{$subnet['name']}</span> </td> </tr> <tr> <td class="input_required" align="right" nowrap="true"> IP Address </td> <td class="padding" align="left" width="100%"> <input id="set_ip_{$window_name}" name="set_ip" alt="IP Address" value="{$interface['ip_addr']}" class="edit" type="text" size="25" maxlength="64" > <span id="qf_free_ip_{$window_name}" title="Available IP Quick Search"><img src="{$images}/silk/find.png" border="0"/></span> <div id="suggest_set_ip_{$window_name}" class="suggest"></div> </td> </tr> <tr> <td align="right" nowrap="true"> MAC Address </td> <td class="padding" align="left" width="100%"> <input name="set_mac" alt="MAC Address" value="{$interface['mac_addr']}" class="edit" type="text" size="17" maxlength="17" > <a class="nav" onClick="this.style.display = 'none'; el('force_{$window_name}').style.display = browser.isIE ? 'block' : 'table-row';">More >></a> </td> </tr> <tr id="force_{$window_name}" style="display: none;"> <td align="right" nowrap="true"> </td> <td class="padding" align="left" width="100%"> <input name="force" alt="Allow duplicate MAC addresses" type="checkbox" > Allow duplicate MAC addresses </td> </tr> <tr> <td align="right" nowrap="true"> Interface name </td> <td class="padding" align="left" width="100%"> <input name="set_name" alt="Interface name" value="{$interface['name']}" class="edit" type="text" size="17" maxlength="255" > </td> </tr> <tr> <td align="right" nowrap="true"> Interface description </td> <td class="padding" align="left" width="100%"> <input name="set_description" alt="Interface description" value="{$interface['description']}" class="edit" type="text" size="25" maxlength="255" > </td> </tr> EOL; } if (!$host['id']) { $window['html'] .= <<<EOL <td align="right" nowrap="true"> Auto create PTR </td> <td class="padding" align="left" width="100%" nowrap> <input id="set_addptr" name="set_addptr" alt="Automaticaly create PTR record" type="checkbox" checked="1" /> </td> <tr> <td align="right" nowrap="true"> </td> <td class="padding" align="left" width="100%"> <input name="keepadding" alt="Keep adding more hosts" type="checkbox" > Keep adding more hosts </td> </tr> <tr> <td colspan="2" class="padding" align="center" width="100%"> <span id="statusinfo_{$window_name}" style="color: green;" ></span> </td> </tr> EOL; } $window['html'] .= <<<EOL <tr> <td align="right" valign="top" nowrap="true"> </td> <td class="padding" align="right" width="100%"> <input class="edit" type="button" name="cancel" value="Cancel" onClick="removeElement('{$window_name}');"> <input class="edit" type="button" name="submit" value="Save" accesskey=" " onClick="xajax_window_submit('{$window_name}', xajax.getFormValues('{$window_name}_edit_form'), 'save');" > </td> </tr> </table> </form> EOL; return window_open($window_name, $window); }
function ona_find_device($search = "") { global $self; // Validate input if ($search == "") { return array(1, 0, array()); } // If it's numeric, assume its a device ID if (preg_match('/^\\d+$/', $search)) { list($status, $rows, $record) = ona_get_device_record(array('id' => $search)); // If we got it, return it if ($status == 0 and $rows == 1) { printmsg("DEBUG => ona_find_device() found device record by id", 2); return array(0, $rows, $record); } } // If it's an IP address or an FQDN: // $ip = ip_mangle($search, 'numeric'); // if ($ip != -1) { // Look up the host first list($status, $rows, $host) = ona_find_host($search); if ($status == 0 and $rows == 1) { list($status, $rows, $record) = ona_get_device_record(array('id' => $host['device_id'])); } // If we got it, return it if ($status == 0 and $rows == 1) { printmsg("DEBUG => ona_find_device() found record by IP address", 2); return array(0, $rows, $record); } // Otherwise return an error if ($rows == 0) { //$ip = ip_mangle($ip, 'dotted'); $self['error'] = "NOTICE => ona_find_device() was unable to locate the record by IP or fqdn"; printmsg($self['error'], 2); return array(3, $rows, array()); } // $self['error'] = "NOTICE => ona_find_device() found multiple matching records when searching by IP or fqdn. Data corruption?"; // printmsg($self['error'], 2); // return(array(4, $rows, array())); // } // We didn't find it - return and error code, 0 matches, and an empty record. $self['error'] = "NOTICE => ona_find_device() couldn't find a unique device record with specified search criteria"; printmsg($self['error'], 2); return array(2, 0, array()); }
function host_display($options = "") { global $conf, $self, $onadb; $text_array = array(); // Version - UPDATE on every edit! $version = '1.04'; printmsg("DEBUG => host_display({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Sanitize options[verbose] (default is yes) $options['verbose'] = sanitize_YN($options['verbose'], 'Y'); // Return the usage summary if we need to if ($options['help'] or !$options['host']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM host_display-v{$version} Displays a host record from the database Synopsis: host_display [KEY=VALUE] ... Required: host=NAME[.DOMAIN] or ID hostname or ID of the host display Optional: verbose=[yes|no] display additional info (yes) EOM ); } // Find the host (and domain) record from $options['host'] list($status, $rows, $host) = ona_find_host($options['host']); printmsg("DEBUG => Host: {$host['fqdn']}", 3); if (!$host['id']) { printmsg("DEBUG => Unknown host: {$options['host']}", 3); $self['error'] = "ERROR => Unknown host: {$options['host']}"; return array(2, $self['error'] . "\n"); } $text_array = $host; // Build text to return $text = "HOST RECORD ({$host['fqdn']})\n"; $text .= format_array($host); // If 'verbose' is enabled, grab some additional info to display if ($options['verbose'] == 'Y') { // TODO: if it is a nat interface, maybe process that IP and make it visible? // Interface record(s) $i = 0; do { list($status, $rows, $interface) = ona_get_interface_record(array('host_id' => $host['id'])); if ($rows == 0) { break; } $i++; $text .= "\nASSOCIATED INTERFACE RECORD ({$i} of {$rows})\n"; $text .= format_array($interface); $text_array['interfaces'][$i] = $interface; unset($text_array['interfaces'][$i]['host_id']); } while ($i < $rows); $text_array['interface_count'] = $rows; // Device record list($status, $rows, $device) = ona_get_device_record(array('id' => $host['device_id'])); if ($rows >= 1) { // Fill out some other device info list($status, $rows, $device_type) = ona_get_device_type_record(array('id' => $device['device_type_id'])); list($status, $rows, $role) = ona_get_role_record(array('id' => $device_type['role_id'])); list($status, $rows, $model) = ona_get_model_record(array('id' => $device_type['model_id'])); list($status, $rows, $manufacturer) = ona_get_manufacturer_record(array('id' => $model['manufacturer_id'])); $device['device_type'] = "{$manufacturer['name']}, {$model['name']} ({$role['name']})"; list($status, $rows, $location) = ona_get_location_record(array('id' => $device['location_id'])); $text_array['location'] = $location; $text_array['device'] = $device; $text .= "\nASSOCIATED DEVICE RECORD\n"; $text .= format_array($device); } // Tag records list($status, $rows, $tags) = db_get_records($onadb, 'tags', array('type' => 'host', 'reference' => $host['id'])); if ($rows) { $text .= "\nASSOCIATED TAG RECORDS\n"; foreach ($tags as $tag) { $text_array['tags'][] = $tag['name']; $text .= " {$tag['name']}\n"; } } } // Cleanup unused info unset($text_array['device_id']); unset($text_array['device']['asset_tag']); unset($text_array['device']['location_id']); unset($text_array['device']['serial_number']); // change the output format if other than default if ($options['format'] == 'json') { $text = $text_array; } if ($options['format'] == 'yaml') { $text = $text_array; } // Return the success notice return array(0, $text); }
function ws_display($window_name, $form = '') { global $conf, $self, $onadb, $base; global $images, $color, $style; $html = ''; $js = ''; $debug_val = 3; // used in the auth() calls to supress logging // If the user supplied an array in a string, build the array and store it in $form $form = parse_options_string($form); // Load the host record if ($form['host_id']) { list($status, $rows, $record) = ona_get_host_record(array('id' => $form['host_id'])); } else { if ($form['host']) { list($status, $rows, $record) = ona_find_host($form['host']); } } if ($status or !$rows) { array_pop($_SESSION['ona']['work_space']['history']); $html .= "<br><center><font color=\"red\"><b>Host doesn't exist!</b></font></center>"; $response = new xajaxResponse(); $response->addAssign("work_space_content", "innerHTML", $html); return $response->getXML(); } // Update History Title (and tell the browser to re-draw the history div) $history = array_pop($_SESSION['ona']['work_space']['history']); $js .= "xajax_window_submit('work_space', ' ', 'rewrite_history');"; if ($history['title'] == $window_name) { $history['title'] = $record['name']; array_push($_SESSION['ona']['work_space']['history'], $history); } // Create some javascript to refresh the current page $refresh = htmlentities(str_replace(array("'", '"'), array("\\'", '\\"'), $history['url']), ENT_QUOTES, $conf['php_charset']); $refresh = "xajax_window_submit('work_space', '{$refresh}');"; // FIXME: umm.. put this somewhere else if (!$record['name']) { $record['name'] = "NONE SET"; } // Interface (and find out how many there are) list($status, $interfaces, $interface) = ona_get_interface_record(array('host_id' => $record['id']), ''); $record['ip_address'] = ip_mangle($interface['ip_addr'], 'dotted'); $interface_style = ''; if ($interfaces > 1) { $interface_style = 'font-weight: bold;'; } // Subnet description list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $interface['subnet_id'])); $record['subnet'] = $subnet['name']; $record['subnet_id'] = $subnet['id']; $record['ip_subnet_mask'] = ip_mangle($subnet['ip_mask'], 'dotted'); $record['ip_subnet_mask_cidr'] = ip_mangle($subnet['ip_mask'], 'cidr'); // Device Description list($status, $rows, $device) = ona_get_device_record(array('id' => $record['device_id'])); $record['device_type_id'] = $device['device_type_id']; list($status, $rows, $device_type) = ona_get_device_type_record(array('id' => $device['device_type_id'])); list($status, $rows, $role) = ona_get_role_record(array('id' => $device_type['role_id'])); list($status, $rows, $model) = ona_get_model_record(array('id' => $device_type['model_id'])); list($status, $rows, $manufacturer) = ona_get_manufacturer_record(array('id' => $model['manufacturer_id'])); $record['devicefull'] = "{$manufacturer['name']}, {$model['name']} ({$role['name']})"; $record['device'] = str_replace('Unknown', '?', $record['devicefull']); $record['location_id'] = $device['location_id']; // Device serial number and/or asset tag $record['serial_number'] = $device['serial_number']; $record['asset_tag'] = $device['asset_tag']; // Get location_number from the location_id list($status, $rows, $location) = ona_get_location_record(array('id' => $record['location_id'])); // extra stuff to pass to ws_plugins $extravars['refresh'] = $refresh; $extravars['window_name'] = $window_name; // Escape data for display in html foreach (array_keys($record) as $key) { $record[$key] = htmlentities($record[$key], ENT_QUOTES, $conf['php_charset']); } // Create a div for workspace plugins to live $html .= "<div id='wsplugins' style='margin: 10px;'>"; // Start displaying all the ws plugins $wspl = workspace_plugin_loader('host_detail', $record, $extravars); $html .= $wspl[0]; $js .= $wspl[1]; $wsmenu[] = $wspl[2]; $wspl = workspace_plugin_loader('host_services', $record); $html .= $wspl[0]; $js .= $wspl[1]; $wsmenu[] = $wspl[2]; $wspl = workspace_plugin_loader('custom_attributes', $record, $extravars); $html .= $wspl[0]; $js .= $wspl[1]; $wsmenu[] = $wspl[2]; $wspl = workspace_plugin_loader('dhcp_entries', $record, $extravars); $html .= $wspl[0]; $js .= $wspl[1]; $wsmenu[] = $wspl[2]; $wspl = workspace_plugin_loader('config_archives', $record); $html .= $wspl[0]; $js .= $wspl[1]; $wsmenu[] = $wspl[2]; // Display the host_action workspace_plugin $wspl = workspace_plugin_loader('host_actions', $record); $html .= $wspl[0]; $js .= $wspl[1]; $wsmenu[] = $wspl[2]; // Display messages $wspl = workspace_plugin_loader('messages', $record, $extravars); $html .= $wspl[0]; $js .= $wspl[1]; $wsmenu[] = $wspl[2]; $wspl = workspace_plugin_loader('reports', $record, $extravars); $html .= $wspl[0]; $js .= $wspl[1]; $wsmenu[] = $wspl[2]; // Get all the plugin based worspace items $wspl_list = plugin_list('wspl_item'); // Load all the dynamic plugins foreach ($wspl_list as $p) { $wspl = workspace_plugin_loader($p['path'], $record, $extravars); $html .= $wspl[0]; $js .= $wspl[1]; $wsmenu[] = $wspl[2]; } // Gather our menuitems and build the HTML $wsmenuhtml = build_workspace_menu($wsmenu); $html .= <<<EOL </div> <br style="clear:both;"> <form id="form_host_{$record['id']}" ><input type="hidden" name="host_id" value="{$record['id']}" ><input type="hidden" name="js" value="{$refresh}" ></form> <div id='wsmenu' style='display:none;'>{$wsmenuhtml}</div> EOL; // RECORD LIST $tab = 'records'; $submit_window = "list_{$tab}"; $form_id = "{$submit_window}_filter_form"; $_SESSION['ona'][$form_id]['tab'] = $tab; $content_id = "{$window_name}_{$submit_window}"; $html .= <<<EOL <!-- INTERFACE LIST --> <div style="border: 1px solid {$color['border']}; margin: 10px 20px;"> <!-- Tab & Quick Filter --> <table id="{$form_id}_table" cellspacing="0" border="0" cellpadding="0"> <tr> <td id="{$form_id}_{$tab}_tab" class="table-tab-active"> Associated DNS {$tab} <span id="{$form_id}_{$tab}_count"></span> </td> <td id="{$form_id}_quick_filter" class="padding" align="right" width="100%"> <form id="{$form_id}" onSubmit="return false;"> <input id="{$form_id}_page" name="page" value="1" type="hidden"> <input name="content_id" value="{$content_id}" type="hidden"> <input name="form_id" value="{$form_id}" type="hidden"> <input name="host_id" value="{$record['id']}" type="hidden"> <div id="{$form_id}_filter_overlay" title="Filter" style="position: relative; display: inline; color: #CACACA; cursor: text;" onClick="this.style.display = 'none'; el('{$form_id}_filter').focus();" >Name</div> <input id="{$form_id}_filter" name="filter" class="filter" type="text" value="" size="10" maxlength="20" alt="Quick Filter" onFocus="el('{$form_id}_filter_overlay').style.display = 'none';" onBlur="if (this.value == '') el('{$form_id}_filter_overlay').style.display = 'inline';" onKeyUp=" if (typeof(timer) != 'undefined') clearTimeout(timer); code = 'if ({$form_id}_last_search != el(\\'{$form_id}_filter\\').value) {' + ' {$form_id}_last_search = el(\\'{$form_id}_filter\\').value;' + ' document.getElementById(\\'{$form_id}_page\\').value = 1;' + ' xajax_window_submit(\\'{$submit_window}\\', xajax.getFormValues(\\'{$form_id}\\'), \\'display_list\\');' + '}'; timer = setTimeout(code, 700);" > </form> </td> </tr> </table> <div id='{$content_id}'> {$conf['loading_icon']} </div> EOL; if (auth('host_add', $debug_val)) { $html .= <<<EOL <!-- ADD RECORD LINK --> <div class="act-box" style="padding: 2px 4px; border-top: 1px solid {$color['border']}"> <form id="form_record_{$record['id']}" ><input type="hidden" name="host_id" value="{$record['id']}" ><input type="hidden" name="js" value="{$refresh}" ></form> <a title="Add DNS record" class="act" onClick="xajax_window_submit('edit_record', xajax.getFormValues('form_record_{$record['id']}'), 'editor');" ><img src="{$images}/silk/font_add.png" border="0"></a> <a title="Add DNS record" class="act" onClick="xajax_window_submit('edit_record', xajax.getFormValues('form_record_{$record['id']}'), 'editor');" >Add DNS record</a> </div> EOL; } $html .= " </div>"; $js .= <<<EOL /* Setup the quick filter */ el('{$form_id}_filter_overlay').style.left = (el('{$form_id}_filter_overlay').offsetWidth + 10) + 'px'; {$form_id}_last_search = ''; /* Tell the browser to load/display the list */ xajax_window_submit('{$submit_window}', xajax.getFormValues('{$form_id}'), 'display_list'); EOL; // INTERFACE LIST $tab = 'interfaces'; $submit_window = "list_{$tab}"; $form_id = "{$submit_window}_filter_form"; $_SESSION['ona'][$form_id]['tab'] = $tab; $content_id = "{$window_name}_{$submit_window}"; $html .= <<<EOL <!-- INTERFACE LIST --> <div style="border: 1px solid {$color['border']}; margin: 10px 20px;"> <!-- Tab & Quick Filter --> <table id="{$form_id}_table" cellspacing="0" border="0" cellpadding="0"> <tr> <td id="{$form_id}_{$tab}_tab" class="table-tab-active"> Associated {$tab} <span id="{$form_id}_{$tab}_count"></span> </td> <td id="{$form_id}_quick_filter" class="padding" align="right" width="100%"> <form id="{$form_id}" onSubmit="return false;"> <input id="{$form_id}_page" name="page" value="1" type="hidden"> <input name="content_id" value="{$content_id}" type="hidden"> <input name="form_id" value="{$form_id}" type="hidden"> <input name="host_id" value="{$record['id']}" type="hidden"> <div id="{$form_id}_filter_overlay" title="Filter" style="position: relative; display: inline; color: #CACACA; cursor: text;" onClick="this.style.display = 'none'; el('{$form_id}_filter').focus();" >Full IP</div> <input id="{$form_id}_filter" name="filter" class="filter" type="text" value="" size="10" maxlength="20" alt="Quick Filter" onFocus="el('{$form_id}_filter_overlay').style.display = 'none';" onBlur="if (this.value == '') el('{$form_id}_filter_overlay').style.display = 'inline';" onKeyUp=" if (typeof(timer) != 'undefined') clearTimeout(timer); code = 'if ({$form_id}_last_search != el(\\'{$form_id}_filter\\').value) {' + ' {$form_id}_last_search = el(\\'{$form_id}_filter\\').value;' + ' document.getElementById(\\'{$form_id}_page\\').value = 1;' + ' xajax_window_submit(\\'{$submit_window}\\', xajax.getFormValues(\\'{$form_id}\\'), \\'display_list\\');' + '}'; timer = setTimeout(code, 700);" > </form> </td> </tr> </table> <div id='{$content_id}'> {$conf['loading_icon']} </div> EOL; if (auth('host_add', $debug_val)) { $html .= <<<EOL <!-- ADD INTERFACE LINK --> <div class="act-box" style="padding: 2px 4px; border-top: 1px solid {$color['border']}"> <form id="form_interface_{$record['id']}" ><input type="hidden" name="host_id" value="{$record['id']}" ><input type="hidden" name="js" value="{$refresh}" ></form> <a title="Add interface" class="act" onClick="xajax_window_submit('edit_interface', xajax.getFormValues('form_interface_{$record['id']}'), 'editor');" ><img src="{$images}/silk/page_add.png" border="0"></a> <a title="Add interface" class="act" onClick="xajax_window_submit('edit_interface', xajax.getFormValues('form_interface_{$record['id']}'), 'editor');" >Add interface</a> </div> EOL; } $html .= " </div>"; $js .= <<<EOL /* Setup the quick filter */ el('{$form_id}_filter_overlay').style.left = (el('{$form_id}_filter_overlay').offsetWidth + 10) + 'px'; {$form_id}_last_search = ''; /* Tell the browser to load/display the list */ xajax_window_submit('{$submit_window}', xajax.getFormValues('{$form_id}'), 'display_list'); EOL; // Insert the new html into the window // Instantiate the xajaxResponse object $response = new xajaxResponse(); $response->addAssign("work_space_content", "innerHTML", $html); if ($js) { $response->addScript($js); } return $response->getXML(); }
function ws_display($window_name, $form = '') { global $conf, $self, $onadb, $base; global $images, $color, $style, $msgtype; $html = ''; $js = ''; $debug_val = 3; // used in the auth() calls to supress logging // If the user supplied an array in a string, build the array and store it in $form $form = parse_options_string($form); // Load the host record if ($form['device_id']) { list($status, $rows, $record) = ona_get_device_record(array('id' => $form['device_id'])); } else { if ($form['device']) { // FIXME.. no find_device yet list($status, $rows, $record) = ona_find_device($form['device']); } } if ($status or !$rows) { array_pop($_SESSION['ona']['work_space']['history']); $html .= "<br><center><font color=\"red\"><b>Device doesn't exist!</b></font></center>"; $response = new xajaxResponse(); $response->addAssign("work_space_content", "innerHTML", $html); return $response->getXML(); } // Update History Title (and tell the browser to re-draw the history div) $history = array_pop($_SESSION['ona']['work_space']['history']); $js .= "xajax_window_submit('work_space', ' ', 'rewrite_history');"; if ($history['title'] == $window_name) { $history['title'] = $record['name']; array_push($_SESSION['ona']['work_space']['history'], $history); } // Create some javascript to refresh the current page $refresh = htmlentities(str_replace(array("'", '"'), array("\\'", '\\"'), $history['url']), ENT_QUOTES, $conf['php_charset']); $refresh = "xajax_window_submit('work_space', '{$refresh}');"; // Interface (and find out how many there are) list($status, $interfaces, $interface) = ona_get_interface_record(array('host_id' => $record['id']), ''); $record['ip_address'] = ip_mangle($interface['ip_addr'], 'dotted'); $interface_style = ''; if ($interfaces > 1) { $interface_style = 'font-weight: bold;'; } // Subnet description list($status, $rows, $subnet) = ona_get_subnet_record(array('ID' => $interface['subnet_id'])); $record['subnet'] = $subnet['name']; $record['ip_subnet_mask'] = ip_mangle($subnet['ip_mask'], 'dotted'); $record['ip_subnet_mask_cidr'] = ip_mangle($subnet['ip_mask'], 'cidr'); // Device Description list($status, $rows, $device_type) = ona_get_device_type_record(array('id' => $record['device_type_id'])); list($status, $rows, $role) = ona_get_role_record(array('id' => $device_type['role_id'])); list($status, $rows, $model) = ona_get_model_record(array('id' => $device_type['model_id'])); list($status, $rows, $manufacturer) = ona_get_manufacturer_record(array('id' => $model['manufacturer_id'])); $record['devicefull'] = "{$manufacturer['name']}, {$model['name']} ({$role['name']})"; $record['device'] = str_replace('Unknown', '?', $record['devicefull']); // Get location_number from the location_id list($status, $rows, $location) = ona_get_location_record(array('id' => $record['location_id'])); // extra stuff to pass to ws_plugins $extravars['refresh'] = $refresh; $extravars['window_name'] = $window_name; $style['content_box'] = <<<EOL margin: 10px 20px; padding: 2px 4px; background-color: #FFFFFF; vertical-align: top; EOL; $style['label_box'] = <<<EOL font-weight: bold; padding: 2px 4px; border: solid 1px {$color['border']}; background-color: {$color['window_content_bg']}; EOL; // Escape data for display in html foreach (array_keys($record) as $key) { $record[$key] = htmlentities($record[$key], ENT_QUOTES, $conf['php_charset']); } // force 300px width to Notes field if the length is longer than 50 characters and put Notes at the top of the td $notes_width = ""; $notes_valign = ""; if (strlen($record['notes']) > 50) { $notes_width = ' style="width: 300px" '; $notes_valign = ' valign="top" '; } $html .= <<<EOL <!-- FORMATTING TABLE --> <div style="{$style['content_box']}"> <table cellspacing="0" border="0" cellpadding="0"><tr> <!-- START OF FIRST COLUMN OF SMALL BOXES --> <td nowrap="true" valign="top" style="padding-right: 15px;"> <form id="form_device_{$record['id']}" ><input type="hidden" name="device_id" value="{$record['id']}" ><input type="hidden" name="js" value="{$refresh}" ></form> EOL; $wspl = workspace_plugin_loader('host_detail', $record, $extravars); $html .= $wspl[0]; $js .= $wspl[1]; $wspl = workspace_plugin_loader('location_detail', $record, $extravars); $html .= $wspl[0]; $js .= $wspl[1]; $html .= <<<EOL <!-- END OF FIRST COLUMN OF SMALL BOXES --> </td> <!-- START OF SECOND COLUMN OF SMALL BOXES --> <td valign="top" style="padding-right: 15px;"> EOL; $html .= <<<EOL <!-- END OF SECOND COLUMN OF SMALL BOXES --> </td> <!-- START OF THIRD COLUMN OF SMALL BOXES --> <td valign="top"> EOL; // START MESSAGES BOX // $tablename is a reference directly to the table that contains the item // we are displaying to the user. // It is possible that you can have the same ID in multiple tables, currently. $tablename = 'devices'; require_once 'winc/tooltips.inc.php'; list($lineshtml, $linesjs) = get_message_lines_html("table_id_ref = {$record['id']} AND table_name_ref LIKE '{$tablename}'"); if ($lineshtml) { $html .= <<<EOL <!-- MESSAGES LIST --> <table width=100% cellspacing="0" border="0" cellpadding="0" style="margin-bottom: 8px;"> <tr><td colspan="99" nowrap="true" style="{$style['label_box']}"> Messages </td></tr> <tr><td> EOL; $html .= $lineshtml; $js .= $linesjs; $html .= "</td></tr></table>"; } // END MESSAGES LIST $html .= <<<EOL </td> <!-- END OF THIRD COLUMN OF SMALL BOXES --> </tr></table> </div> <!-- END OF TOP SECTION --> EOL; // RECORD LIST $tab = 'hosts'; $submit_window = "list_{$tab}"; $form_id = "{$submit_window}_filter_form"; $_SESSION['ona'][$form_id]['tab'] = $tab; $content_id = "{$window_name}_{$submit_window}"; $html .= <<<EOL <!-- INTERFACE LIST --> <div style="border: 1px solid {$color['border']}; margin: 10px 20px;"> <!-- Tab & Quick Filter --> <table id="{$form_id}_table" cellspacing="0" border="0" cellpadding="0"> <tr> <td id="{$form_id}_{$tab}_tab" class="table-tab-active"> Associated Hosts {$tab} <span id="{$form_id}_{$tab}_count"></span> </td> <td id="{$form_id}_quick_filter" class="padding" align="right" width="100%"> <form id="{$form_id}" onSubmit="return false;"> <input id="{$form_id}_page" name="page" value="1" type="hidden"> <input name="content_id" value="{$content_id}" type="hidden"> <input name="form_id" value="{$form_id}" type="hidden"> <input name="device_id" value="{$record['id']}" type="hidden"> <div id="{$form_id}_filter_overlay" title="Filter" style="position: relative; display: inline; color: #CACACA; cursor: text;" onClick="this.style.display = 'none'; el('{$form_id}_filter').focus();" >Name</div> <input id="{$form_id}_filter" name="filter" class="filter" type="text" value="" size="10" maxlength="20" alt="Quick Filter" onFocus="el('{$form_id}_filter_overlay').style.display = 'none';" onBlur="if (this.value == '') el('{$form_id}_filter_overlay').style.display = 'inline';" onKeyUp=" if (typeof(timer) != 'undefined') clearTimeout(timer); code = 'if ({$form_id}_last_search != el(\\'{$form_id}_filter\\').value) {' + ' {$form_id}_last_search = el(\\'{$form_id}_filter\\').value;' + ' document.getElementById(\\'{$form_id}_page\\').value = 1;' + ' xajax_window_submit(\\'{$submit_window}\\', xajax.getFormValues(\\'{$form_id}\\'), \\'display_list\\');' + '}'; timer = setTimeout(code, 700);" > </form> </td> </tr> </table> <div id='{$content_id}'> {$conf['loading_icon']} </div> EOL; if (auth('host_add', $debug_val)) { $html .= <<<EOL <!-- ADD RECORD LINK --> <div class="act-box" style="padding: 2px 4px; border-top: 1px solid {$color['border']}"> <form id="form_record_{$record['id']}" ><input type="hidden" name="host_id" value="{$record['id']}" ><input type="hidden" name="js" value="{$refresh}" ></form> <a title="Add host" class="act" onClick="xajax_window_submit('edit_host', xajax.getFormValues('form_record_{$record['id']}'), 'editor');" ><img src="{$images}/silk/font_add.png" border="0"></a> <a title="Add host" class="act" onClick="xajax_window_submit('edit_host', xajax.getFormValues('form_record_{$record['id']}'), 'editor');" >Add host</a> </div> EOL; } $html .= " </div>"; $js .= <<<EOL /* Setup the quick filter */ el('{$form_id}_filter_overlay').style.left = (el('{$form_id}_filter_overlay').offsetWidth + 10) + 'px'; {$form_id}_last_search = ''; /* Tell the browser to load/display the list */ xajax_window_submit('{$submit_window}', xajax.getFormValues('{$form_id}'), 'display_list'); EOL; // Insert the new html into the window // Instantiate the xajaxResponse object $response = new xajaxResponse(); $response->addAssign("work_space_content", "innerHTML", $html); if ($js) { $response->addScript($js); } return $response->getXML(); }
function ws_display_list($window_name, $form = '') { global $conf, $self, $onadb; global $images, $color, $style; $html = ''; $js = ''; // If the user supplied an array in a string, transform it into an array $form = parse_options_string($form); // Find the "tab" we're on $tab = $_SESSION['ona'][$form['form_id']]['tab']; // Build js to refresh this list $refresh = "xajax_window_submit('{$window_name}', xajax.getFormValues('{$form['form_id']}'), 'display_list');"; // If it's not a new query, load the previous query from the session // into $form and save the current page and filter in the session. // Also find/set the "page" we're viewing $page = 1; if ($form['page'] and is_numeric($form['page'])) { $form = array_merge($form, (array) $_SESSION['ona'][$form['form_id']][$tab]['q']); $_SESSION['ona'][$form['form_id']][$tab]['page'] = $page = $form['page']; $_SESSION['ona'][$form['form_id']][$tab]['filter'] = $form['filter']; } printmsg("DEBUG => Displaying hosts list page: {$page}", 1); // Calculate the SQL query offset (based on the page being displayed) $offset = $conf['search_results_per_page'] * ($page - 1); if ($offset == 0) { $offset = -1; } // Search results go in here $results = array(); $count = 0; // // *** ADVANCED HOST SEARCH *** // FIND RESULT SET // // Start building the "where" clause for the sql query to find the hosts to display $where = ""; $and = ""; $orderby = ""; $from = 'hosts h'; // enable or disable wildcards $wildcard = '%'; if ($form['nowildcard']) { $wildcard = ''; } // DISPLAY ALL // MP: I dont think this is used.. remove it if you can if ($form['all_flag']) { $where .= $and . "h.id > 0"; $and = " AND "; } // HOST ID if ($form['host_id']) { $where .= $and . "h.id = " . $onadb->qstr($form['host_id']); $and = " AND "; } // DEVICE ID if ($form['device_id']) { $where .= $and . "h.device_id = " . $onadb->qstr($form['device_id']); $and = " AND "; } // HOSTNAME if ($form['hostname']) { // Find the domain name piece of the hostname assuming it was passed in as an fqdn. // FIXME: MP this was taken from the ona_find_domain function. make that function have the option // to NOT return a default domain. // lets test out if it has a / in it to strip the view name portion $view['id'] = 0; if (strstr($form['hostname'], '/')) { list($dnsview, $form['hostname']) = explode('/', $form['hostname']); list($status, $viewrows, $view) = db_get_record($onadb, 'dns_views', array('name' => strtoupper($dnsview))); if (!$viewrows) { $view['id'] = 0; } } // Split it up on '.' and put it in an array backwards $parts = array_reverse(explode('.', $form['hostname'])); // Find the domain name that best matches $name = ''; $domain = array(); foreach ($parts as $part) { if (!$rows) { if (!$name) { $name = $part; } else { $name = "{$part}.{$name}"; } list($status, $rows, $record) = ona_get_domain_record(array('name' => $name)); if ($rows) { $domain = $record; } } else { list($status, $rows, $record) = ona_get_domain_record(array('name' => $part, 'parent_id' => $domain['id'])); if ($rows) { $domain = $record; } } } $withdomain = ''; $hostname = $form['hostname']; // If you found a domain in the query, add it to the search, and strip the domain from the host portion. if (array_key_exists('id', $domain) and !$form['domain']) { $withdomain = "AND b.domain_id = {$domain['id']}"; // Now find what the host part of $search is $hostname = str_replace(".{$domain['fqdn']}", '', $form['hostname']); } // If we have a hostname and a domain name then use them both if ($form['domain']) { list($status, $rows, $record) = ona_find_domain($form['domain']); if ($record['id']) { $withdomain = "AND b.domain_id = {$record['id']}"; } // Now find what the host part of $search is $hostname = trim($form['hostname']); } // MP: Doing the many select IN statements was too slow.. I did this kludge: // 1. get a list of all the interfaces // 2. loop through the array and build a list of comma delimited host_ids to use in the final select list($status, $rows, $tmp) = db_get_records($onadb, 'interfaces a, dns b', "a.id = b.interface_id and b.name LIKE '{$wildcard}{$hostname}{$wildcard}' {$withdomain}"); $commait = ''; $hostids = ''; foreach ($tmp as $item) { $hostids .= $commait . $item['host_id']; $commait = ','; } // Just look for the host itself list($status, $rows, $r) = ona_find_host($form['hostname']); if ($rows) { $hostids .= ',' . $r['id']; } // MP: this is the old, slow query for reference. // // TODO: MP this seems to be kinda slow (gee I wonder why).. look into speeding things up somehow. // This also does not search for CNAME records etc. only things with interface_id.. how to fix that issue.......? // $where .= $and . "id IN (select host_id from interfaces where id in (SELECT interface_id " . // " FROM dns " . // " WHERE name LIKE '%{$hostname}%' {$withdomain} ))"; // Trim off extra commas $hostids = trim($hostids, ","); // If we got a list of hostids from interfaces then use them if ($hostids) { $idqry = "h.id IN ({$hostids})"; } else { $idqry = ""; } $where .= $and . $idqry; $and = " AND "; } // DOMAIN if ($form['domain'] and !$form['hostname']) { // FIXME: does this clause work correctly? printmsg("FIXME: => Does \$form['domain'] work correctly in list_hosts.inc.php?", 2); // Find the domain name piece of the hostname. // FIXME: MP this was taken from the ona_find_domain function. make that function have the option // to NOT return a default domain. // Split it up on '.' and put it in an array backwards $parts = array_reverse(explode('.', $form['domain'])); // Find the domain name that best matches $name = ''; $domain = array(); foreach ($parts as $part) { if (!$rows) { if (!$name) { $name = $part; } else { $name = "{$part}.{$name}"; } list($status, $rows, $record) = ona_get_domain_record(array('name' => $name)); if ($rows) { $domain = $record; } } else { list($status, $rows, $record) = ona_get_domain_record(array('name' => $part, 'parent_id' => $domain['id'])); if ($rows) { $domain = $record; } } } if (array_key_exists('id', $domain)) { // Crappy way of writing the query but it makes it fast. $from = "(\nSELECT distinct a.*\nfrom hosts as a, interfaces as i, dns as d\nwhere a.id = i.host_id\nand i.id = d.interface_id\nand d.domain_id = " . $onadb->qstr($domain['id']) . "\n) h"; $and = " AND "; } } // DOMAIN ID if ($form['domain_id'] and !$form['hostname']) { $where .= $and . "h.primary_dns_id IN ( SELECT id " . " FROM dns " . " WHERE domain_id = " . $onadb->qstr($form['domain_id']) . " ) "; $and = " AND "; } // MAC if ($form['mac']) { // Clean up the mac address $form['mac'] = strtoupper($form['mac']); $form['mac'] = preg_replace('/[^%0-9A-F]/', '', $form['mac']); // We do a sub-select to find interface id's that match $where .= $and . "h.id IN ( SELECT host_id " . " FROM interfaces " . " WHERE mac_addr LIKE " . $onadb->qstr($wildcard . $form['mac'] . $wildcard) . " ) "; $and = " AND "; } // IP ADDRESS $ip = $ip_end = ''; if ($form['ip']) { // Build $ip and $ip_end from $form['ip'] and $form['ip_thru'] $ip = ip_complete($form['ip'], '0'); if ($form['ip_thru']) { $ip_end = ip_complete($form['ip_thru'], '255'); } else { $ip_end = ip_complete($form['ip'], '255'); } // Find out if $ip and $ip_end are valid $ip = ip_mangle($ip, 'numeric'); $ip_end = ip_mangle($ip_end, 'numeric'); if ($ip != -1 and $ip_end != -1) { // We do a sub-select to find interface id's between the specified ranges $where .= $and . "h.id IN ( SELECT host_id " . " FROM interfaces " . " WHERE ip_addr >= " . $onadb->qstr($ip) . " AND ip_addr <= " . $onadb->qstr($ip_end) . " )"; $and = " AND "; } } // NOTES if ($form['notes']) { $where .= $and . "h.notes LIKE " . $onadb->qstr($wildcard . $form['notes'] . $wildcard); $and = " AND "; } // DEVICE MODEL if ($form['model_id']) { $where .= $and . "h.device_id in (select id from devices where device_type_id in (select id from device_types where model_id = {$form['model_id']}))"; $and = " AND "; } if ($form['model']) { $where .= $and . "h.device_id in (select id from devices where device_type_id in (select id from device_types where model_id in (select id from models where name like '{$form['model']}')))"; $and = " AND "; } // DEVICE TYPE if ($form['role']) { // Find model_id's that have a device_type_id of $form['role'] list($status, $rows, $records) = db_get_records($onadb, 'roles', array('name' => $form['role'])); // If there were results, add each one to the $where clause if ($rows > 0) { $where .= $and . " ( "; $and = " AND "; $or = ""; foreach ($records as $record) { // Yes this is one freakin nasty query but it works. $where .= $or . "h.device_id in (select id from devices where device_type_id in (select id from device_types where role_id = " . $onadb->qstr($record['id']) . "))"; $or = " OR "; } $where .= " ) "; } } // DEVICE MANUFACTURER if ($form['manufacturer']) { // Find model_id's that have a device_type_id of $form['manufacturer'] if (is_numeric($form['manufacturer'])) { list($status, $rows, $records) = db_get_records($onadb, 'models', array('manufacturer_id' => $form['manufacturer'])); } else { list($status, $rows, $manu) = db_get_record($onadb, 'manufacturers', array('name' => $form['manufacturer'])); list($status, $rows, $records) = db_get_records($onadb, 'models', array('manufacturer_id' => $manu['id'])); } // If there were results, add each one to the $where clause if ($rows > 0) { $where .= $and . " ( "; $and = " AND "; $or = ""; foreach ($records as $record) { // Yes this is one freakin nasty query but it works. $where .= $or . "h.device_id in (select id from devices where device_type_id in (select id from device_types where model_id = " . $onadb->qstr($record['id']) . "))"; $or = " OR "; } $where .= " ) "; } } // tag if ($form['tag_host']) { $where .= $and . "h.id in (select reference from tags where type like 'host' and name like " . $onadb->qstr($form['tag_host']) . ")"; $and = " AND "; } // custom attribute type if ($form['custom_attribute_type']) { $where .= $and . "h.id in (select table_id_ref from custom_attributes where table_name_ref like 'hosts' and custom_attribute_type_id = (SELECT id FROM custom_attribute_types WHERE name = " . $onadb->qstr($form['custom_attribute_type']) . "))"; $and = " AND "; $cavaluetype = "and custom_attribute_type_id = (SELECT id FROM custom_attribute_types WHERE name = " . $onadb->qstr($form['custom_attribute_type']) . ")"; } // custom attribute value if ($form['ca_value']) { $where .= $and . "h.id in (select table_id_ref from custom_attributes where table_name_ref like 'hosts' {$cavaluetype} and value like " . $onadb->qstr($wildcard . $form['ca_value'] . $wildcard) . ")"; $and = " AND "; } // LOCATION No. if ($form['location']) { list($status, $rows, $loc) = ona_find_location($form['location']); $where .= $and . "h.device_id in (select id from devices where location_id = " . $onadb->qstr($loc['id']) . ")"; $and = " AND "; } // subnet ID if (is_numeric($form['subnet_id'])) { // We do a sub-select to find interface id's that match $from = "(\nSELECT distinct a.*\nfrom hosts as a, interfaces as b\nwhere a.id = b.host_id\nand b.subnet_id = " . $onadb->qstr($form['subnet_id']) . "\norder by b.ip_addr) h"; $and = " AND "; } // display a nice message when we dont find all the records if ($where == '' and $form['content_id'] == 'search_results_list') { $js .= "el('search_results_msg').innerHTML = 'Unable to find hosts matching your query, showing all records';"; } // Wild card .. if $while is still empty, add a 'ID > 0' to it so you see everything. if ($where == '') { $where = 'h.id > 0'; } // Do the SQL Query $filter = ''; if ($form['filter']) { // Host names should always be lower case $form['filter'] = strtolower($form['filter']); // FIXME (MP) for now this uses primary_dns_id, this will NOT find multiple A records or other record types. Find a better way some day $filter = " AND h.primary_dns_id IN (SELECT id " . " FROM dns " . " WHERE name LIKE " . $onadb->qstr('%' . $form['filter'] . '%') . " ) "; } list($status, $rows, $results) = db_get_records($onadb, $from, $where . $filter, $orderby, $conf['search_results_per_page'], $offset); // If we got less than serach_results_per_page, add the current offset to it // so that if we're on the last page $rows still has the right number in it. if ($rows > 0 and $rows < $conf['search_results_per_page']) { $rows += $conf['search_results_per_page'] * ($page - 1); } else { if ($rows >= $conf['search_results_per_page']) { list($status, $rows, $records) = db_get_records($onadb, $from, $where . $filter, "", 0); } } $count = $rows; // // *** BUILD HTML LIST *** // $html .= <<<EOL <!-- Host Results --> <table id="{$form['form_id']}_host_list" class="list-box" cellspacing="0" border="0" cellpadding="0"> <!-- Table Header --> <tr> <td class="list-header" align="center" style="{$style['borderR']};">Name</td> <td class="list-header" align="center" style="{$style['borderR']};">Subnet</td> <td class="list-header" align="center" style="{$style['borderR']};">Interface</td> <td class="list-header" align="center" style="{$style['borderR']};">Device Type</td> <td class="list-header" align="center" style="{$style['borderR']};">Location</td> <td class="list-header" align="center" style="{$style['borderR']};">Notes</td> <td class="list-header" align="center"> </td> </tr> EOL; // Loop and display each record foreach ($results as $record) { // Get additional info about eash host record // If a subnet_id was passed use it as part of the search. Used to display the IP of the subnet you searched if (is_numeric($form['subnet_id'])) { list($status, $interfaces, $interface) = ona_get_interface_record(array('host_id' => $record['id'], 'subnet_id' => $form['subnet_id']), ''); // Count how many rows and assign it back to the interfaces variable list($status, $rows, $records) = db_get_records($onadb, 'interfaces', 'host_id = ' . $onadb->qstr($record['id']), "ip_addr", 0); $interfaces = $rows; } else { if (is_numeric($ip)) { list($status, $interfaces, $interface) = db_get_record($onadb, 'interfaces', 'host_id = ' . $onadb->qstr($record['id']) . ' AND ip_addr >= ' . $onadb->qstr($ip) . ' AND ip_addr <= ' . $onadb->qstr($ip_end), "ip_addr", 0); // Count how many rows and assign it back to the interfaces variable list($status, $rows, $records) = db_get_records($onadb, 'interfaces', 'host_id = ' . $onadb->qstr($record['id']), "ip_addr", 0); $interfaces = $rows; } else { // Interface (and find out how many there are) list($status, $interfaces, $interface) = ona_get_interface_record(array('host_id' => $record['id']), ''); } } // bz: why did someone add this?? You especially want to show hosts with no interfaces so you can fix them! // if (!$interfaces) {$count -1; continue;} // get interface cluster info $clusterhtml = ''; list($status, $intclusterrows, $intcluster) = db_get_records($onadb, 'interface_clusters', "interface_id = {$interface['id']}"); if ($intclusterrows > 0) { $clusterscript = "onMouseOver=\"wwTT(this, event,\n 'id', 'tt_interface_cluster_list_{$record['id']}',\n 'type', 'velcro',\n 'styleClass', 'wwTT_niceTitle',\n 'direction', 'south',\n 'javascript', 'xajax_window_submit(\\'tooltips\\', \\'tooltip=>interface_cluster_list,id=>tt_interface_cluster_list_{$record['id']},interface_id=>{$interface['id']}\\');'\n );\""; $clusterhtml .= <<<EOL <img src="{$images}/silk/sitemap.png" {$clusterscript} /> EOL; } $record['ip_addr'] = ip_mangle($interface['ip_addr'], 'dotted'); $interface_style = ''; if ($interfaces > 1) { $interface_style = 'font-weight: bold;'; } // DNS A record list($status, $rows, $dns) = ona_get_dns_record(array('id' => $record['primary_dns_id'])); $record['name'] = $dns['name']; // Domain Name list($status, $rows, $domain) = ona_get_domain_record(array('id' => $dns['domain_id'])); $record['domain'] = $domain['fqdn']; // Subnet description list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $interface['subnet_id'])); $record['subnet'] = $subnet['name']; $record['ip_mask'] = ip_mangle($subnet['ip_mask'], 'dotted'); $record['ip_mask_cidr'] = ip_mangle($subnet['ip_mask'], 'cidr'); // Device Description list($status, $rows, $device) = ona_get_device_record(array('id' => $record['device_id'])); list($status, $rows, $device_type) = ona_get_device_type_record(array('id' => $device['device_type_id'])); list($status, $rows, $model) = ona_get_model_record(array('id' => $device_type['model_id'])); list($status, $rows, $role) = ona_get_role_record(array('id' => $device_type['role_id'])); list($status, $rows, $manufacturer) = ona_get_manufacturer_record(array('id' => $model['manufacturer_id'])); $record['devicefull'] = "{$manufacturer['name']}, {$model['name']} ({$role['name']})"; $record['device'] = str_replace('Unknown', '?', $record['devicefull']); $record['notes_short'] = truncate($record['notes'], 40); // Get location_number from the location_id list($status, $rows, $location) = ona_get_location_record(array('id' => $device['location_id'])); // Escape data for display in html foreach (array_keys($record) as $key) { $record[$key] = htmlentities($record[$key], ENT_QUOTES, $conf['php_charset']); } $primary_object_js = "xajax_window_submit('work_space', 'xajax_window_submit(\\'display_host\\', \\'host_id=>{$record['id']}\\', \\'display\\')');"; $html .= <<<EOL <tr onMouseOver="this.className='row-highlight';" onMouseOut="this.className='row-normal';"> <td class="list-row"> <a title="View host. ID: {$record['id']}" class="nav" onClick="{$primary_object_js}" >{$record['name']}</a >.<a title="View domain. ID: {$domain['id']}" class="domain" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain\\', \\'domain_id=>{$domain['id']}\\', \\'display\\')');" >{$record['domain']}</a> </td> <td class="list-row"> <a title="View subnet. ID: {$subnet['id']}" class="nav" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_subnet\\', \\'subnet_id=>{$subnet['id']}\\', \\'display\\')');" >{$record['subnet']}</a> </td> <td class="list-row" align="left"> <span style="{$interface_style}" EOL; if ($interfaces > 1) { $html .= <<<EOL onMouseOver="wwTT(this, event, 'id', 'tt_host_interface_list_{$record['id']}', 'type', 'velcro', 'styleClass', 'wwTT_niceTitle', 'direction', 'south', 'javascript', 'xajax_window_submit(\\'tooltips\\', \\'tooltip=>host_interface_list,id=>tt_host_interface_list_{$record['id']},host_id=>{$record['id']}\\');' );" EOL; } $html .= <<<EOL >{$record['ip_addr']}</span> <span title="{$record['ip_mask']}">/{$record['ip_mask_cidr']}</span> <span>{$clusterhtml}</span> </td> <td class="list-row" title="{$record['devicefull']}">{$record['device']} </td> <td class="list-row" align="right"> <span onMouseOver="wwTT(this, event, 'id', 'tt_location_{$device['location_id']}', 'type', 'velcro', 'styleClass', 'wwTT_niceTitle', 'direction', 'south', 'javascript', 'xajax_window_submit(\\'tooltips\\', \\'tooltip=>location,id=>tt_location_{$device['location_id']},location_id=>{$device['location_id']}\\');' );" >{$location['reference']}</span> </td> <td class="list-row"> <span title="{$record['notes']}">{$record['notes_short']}</span> </td> <!-- ACTION ICONS --> <td class="list-row" align="right"> <form id="{$form['form_id']}_list_host_{$record['id']}" ><input type="hidden" name="host_id" value="{$record['id']}" ><input type="hidden" name="js" value="{$refresh}" ></form> EOL; if (auth('host_modify')) { $html .= <<<EOL <a title="Edit host" class="act" onClick="xajax_window_submit('edit_host', xajax.getFormValues('{$form['form_id']}_list_host_{$record['id']}'), 'editor');" ><img src="{$images}/silk/page_edit.png" border="0"></a> EOL; } if (auth('host_del')) { $html .= <<<EOL <a title="Delete host" class="act" onClick="xajax_window_submit('edit_host', xajax.getFormValues('{$form['form_id']}_list_host_{$record['id']}'), 'delete');" ><img src="{$images}/silk/delete.png" border="0"></a> EOL; } $html .= <<<EOL </td> </tr> EOL; } if ($count == 0 and $form['subnet_id'] and !$form['filter']) { $html .= <<<EOL <tr><td colspan="99" align="center" style="color: red;">Please add the gateway host (router) to this subnet</td></tr> EOL; } $html .= <<<EOL </table> EOL; // Build page links if there are any $html .= get_page_links($page, $conf['search_results_per_page'], $count, $window_name, $form['form_id']); // If there was only 1 result, and we're about to display results in the "Search Results" window, display it. if ($count == 1 and $form['content_id'] == 'search_results_list' and $form['filter'] == '') { $js .= $primary_object_js; } // Insert the new html into the content div specified // Instantiate the xajaxResponse object $response = new xajaxResponse(); $response->addAssign("{$form['form_id']}_{$tab}_count", "innerHTML", "({$count})"); $response->addAssign($form['content_id'], "innerHTML", $html); if ($js) { $response->addScript($js); } return $response->getXML(); }
function interface_display($options = "") { global $conf, $self; $text_array = array(); // Version - UPDATE on every edit! $version = '1.03'; printmsg("DEBUG => interface_display({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Return the usage summary if we need to if ($options['help'] or !$options['host'] and !$options['interface']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM interface_display-v{$version} Displays an interface record from the database Synopsis: interface_display [KEY=VALUE] ... Required: interface=[ID|IP|MAC] display interface by search string or host=NAME[.DOMAIN] or ID display interface by hostname or host_id Optional: verbose=[yes|no] display additional info (yes) Notes: * If search returns more than one interface, an error is displayed * DOMAIN will default to {$conf['dns_defaultdomain']} if not specified EOM ); } // Sanitize "options[verbose]" (yes is the default) $options['verbose'] = sanitize_YN($options['verbose'], 'Y'); // They provided a interface ID, IP address, interface name, or MAC address if ($options['interface']) { // Find an interface record by something in that interface's record list($status, $rows, $interface) = ona_find_interface($options['interface']); } else { if ($options['host']) { // Find a host by the user's input list($status, $rows, $host) = ona_find_host($options['host']); if (!$host['id']) { printmsg("DEBUG => Host not found ({$options['host']})!", 3); $self['error'] = "ERROR => Host not found ({$options['host']})"; return array(2, $self['error'] . "\n"); } // If we got one, load an associated interface list($status, $rows, $interface) = ona_get_interface_record(array('host_id' => $host['id'])); } } // If we didn't get a record then exit if (!$interface['id']) { if ($rows > 1) { $self['error'] = "ERROR => More than one interface matches"; } else { $self['error'] = "ERROR => Interface not found ({$options['interface']})"; } return array(4, $self['error'] . "\n"); } $text_array = $interface; // Build text to return $text = "INTERFACE RECORD\n"; $text .= format_array($interface); // If 'verbose' is enabled, grab some additional info to display if ($options['verbose'] == 'Y') { // Host record list($status, $rows, $host) = ona_get_host_record(array('ID' => $interface['host_id'])); if ($rows >= 1) { $text .= "\nASSOCIATED HOST RECORD\n"; $text .= format_array($host); } // Subnet record list($status, $rows, $subnet) = ona_get_subnet_record(array('ID' => $interface['subnet_id'])); if ($rows >= 1) { $text .= "\nASSOCIATED SUBNET RECORD\n"; $text .= format_array($subnet); } // Device record list($status, $rows, $device) = ona_get_device_record(array('id' => $host['device_id'])); if ($rows >= 1) { $text .= "\nASSOCIATED DEVICE RECORD\n"; $text .= format_array($device); } } // change the output format if other than default if ($options['format'] == 'json') { $text = $text_array; } if ($options['format'] == 'yaml') { $text = $text_array; } // Return the success notice return array(0, $text); }