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 get_server_name_ip($server_id) { printmsg("DEBUG => get_server_name_ip(\$server_id = {$server_id}) called", 5); list($status, $rows, $host) = ona_get_host_record(array('id' => $server_id)); list($status, $rows, $interface) = ona_get_interface_record(array('host_id' => $server_id)); return array($host['fqdn'], $interface['ip_addr']); }
function ws_display($window_name, $form = '') { global $conf, $self, $onadb; 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['interface_id']) { list($status, $rows, $record) = ona_get_interface_record(array('id' => $form['interface_id'])); } $record['ip_addr'] = ip_mangle($record['ip_addr'], 'dotted'); if ($status or !$rows) { array_pop($_SESSION['ona']['work_space']['history']); $html .= "<br><center><font color=\"red\"><b>Interface 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['ip_addr']; 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}');"; // Associated Host info list($status, $rows, $host) = ona_get_host_record(array('id' => $record['host_id']), ''); // Subnet description list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $interface['subnet_id'])); $record['subnet_name'] = $subnet['name']; $record['ip_subnet_mask'] = ip_mangle($subnet['ip_mask'], 'dotted'); $record['ip_subnet_mask_cidr'] = ip_mangle($subnet['ip_mask'], 'cidr'); $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']); } $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;"> <!-- HOST INFORMATION --> <table width=100% cellspacing="0" border="0" cellpadding="0" style="margin-bottom: 8px;"> <tr><td colspan="99" nowrap="true" style="{$style['label_box']}"> <!-- LABEL --> <form id="form_interface_{$record['id']}" ><input type="hidden" name="interface_id" value="{$record['id']}" ><input type="hidden" name="js" value="{$refresh}" ></form> EOL; if (auth('host_modify', $debug_val)) { $html .= <<<EOL <a title="Edit interface. ID: {$record['id']}" class="act" onClick="xajax_window_submit('edit_interface', xajax.getFormValues('form_interface_{$record['id']}'), 'editor');" ><img src="{$images}/silk/page_edit.png" border="0"></a> EOL; } if (auth('host_del', $debug_val)) { $html .= <<<EOL <a title="Delete interface. ID: {$record['id']}" class="act" onClick="var doit=confirm('Are you sure you want to delete this interface?'); if (doit == true) xajax_window_submit('edit_interface', xajax.getFormValues('form_interface_{$record['id']}'), 'delete');" ><img src="{$images}/silk/delete.png" border="0"></a> EOL; } $html .= <<<EOL {$record['ip_addr']} </td></tr> <tr> <td align="right" nowrap="true"><b>MAC Address</b> </td> <td class="padding" align="left">{$record['mac_addr']} </td> </tr> <tr> <td align="right" nowrap="true"><b>Name</b> </td> <td class="padding" align="left">{$record['name']} </td> </tr> <tr> <td align="right" nowrap="true"><b>Description</b> </td> <td class="padding" align="left">{$record['description']} </td> </tr> </table> EOL; $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; $html .= <<<EOL </td> <!-- END OF THIRD COLUMN OF SMALL BOXES --> </tr></table> </div> <!-- END OF TOP SECTION --> 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 <!-- RECORD 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="interface_id" value="{$record['id']}" type="hidden"> <div id="{$form_id}_filter_overlay" style="position: relative; display: inline; color: #CACACA; cursor: text;" onClick="this.style.display = 'none'; el('{$form_id}_filter').focus();" >Filter</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('record_add', $debug_val)) { $html .= <<<EOL <!-- ADD DNS 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="interface_id" value="{$record['id']}" ><input type="hidden" name="js" value="{$refresh}" ></form> <a title="Add record" class="act" onClick="xajax_window_submit('edit_record', xajax.getFormValues('form_record_{$record['id']}'), 'editor');" ><img src="{$images}/silk/page_add.png" border="0"></a> <a title="Add record" class="act" onClick="xajax_window_submit('edit_record', xajax.getFormValues('form_record_{$record['id']}'), 'editor');" >Add 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; // 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 records 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 RECORD SEARCH *** // FIND RESULT SET // // Start building the "where" clause for the sql query to find the records to display $where = ""; $and = ""; $orderby = ""; // enable or disable wildcards $wildcard = '%'; if ($form['nowildcard']) { $wildcard = ''; } // RECORD ID if ($form['record_id']) { $where .= $and . "id = " . $onadb->qstr($form['record_id']); $and = " AND "; } // DNS VIEW ID if ($form['dns_view']) { if (is_string($form['dns_view'])) { list($status, $rows, $dnsview) = ona_get_dns_view_record(array('name' => $form['dns_view'])); } if (is_numeric($form['dns_view'])) { list($status, $rows, $dnsview) = ona_get_dns_view_record(array('id' => $form['dns_view'])); } $where .= $and . "dns_view_id = " . $onadb->qstr($dnsview['id']); $and = " AND "; } // INTERFACE ID if ($form['interface_id']) { $where .= $and . "interface_id = " . $onadb->qstr($form['interface_id']); $and = " AND "; } // DNS RECORD note if ($form['notes']) { $where .= $and . "notes LIKE " . $onadb->qstr($wildcard . $form['notes'] . $wildcard); $and = " AND "; } // DNS RECORD TYPE if ($form['dnstype']) { $where .= $and . "type = " . $onadb->qstr($form['dnstype']); $and = " AND "; } // HOSTNAME if ($form['hostname']) { $where .= $and . "id IN (SELECT id " . " FROM dns " . " WHERE name LIKE " . $onadb->qstr($wildcard . $form['hostname'] . $wildcard) . " )"; $and = " AND "; } // DOMAIN if ($form['domain']) { // FIXME: MP test if this clause works correctly? Not sure that anything even uses this? list($status, $rows, $tmpdomain) = ona_find_domain($form['domain']); $where .= $and . "domain_id = " . $onadb->qstr($tmpdomain['id']); $orderby .= "name, domain_id"; $and = " AND "; } // DOMAIN ID if ($form['domain_id']) { //$where .= $and . "primary_dns_id IN ( SELECT id " . // " FROM dns " . // " WHERE domain_id = " . $onadb->qstr($form['domain_id']) . " ) "; $where .= $and . "domain_id = " . $onadb->qstr($form['domain_id']); $orderby .= "name, domain_id"; $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 . "interface_id IN ( SELECT id " . " FROM interfaces " . " WHERE ip_addr >= " . $onadb->qstr($ip) . " AND ip_addr <= " . $onadb->qstr($ip_end) . " )"; $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 DNS records 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 = 'id > 0'; } // If we dont have DNS views turned on, limit data to just the default view. // Even if there is data associated with other views, ignore it if (!$conf['dns_views']) { $where .= ' AND dns_view_id = 0'; } // Do the SQL Query $filter = ''; if ($form['filter']) { // Host names should always be lower case $form['filter'] = strtolower($form['filter']); $filter = ' AND name LIKE ' . $onadb->qstr('%' . $form['filter'] . '%'); } // If we get a specific host to look for we must do the following // 1. get (A) records that match any interface_id associated with the host // 2. get CNAMES that point to dns records that are using an interface_id associated with the host if ($form['host_id']) { // If we dont have DNS views turned on, limit data to just the default view. // Even if there is data associated with other views, ignore it // MP: something strange with this, it should only limit to default view.. sometimes it does not??? if (!$conf['dns_views']) { $hwhere .= 'dns_view_id = 0 AND '; } // Get the host record so we know what the primary interface is list($status, $rows, $host) = ona_get_host_record(array('id' => $form['host_id']), ''); list($status, $rows, $results) = db_get_records($onadb, 'dns', $hwhere . 'interface_id in (select id from interfaces where host_id = ' . $onadb->qstr($form['host_id']) . ') OR interface_id in (select interface_id from interface_clusters where host_id = ' . $onadb->qstr($form['host_id']) . ')', "type", $conf['search_results_per_page'], $offset); // If we got less than search_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, 'dns', $hwhere . 'interface_id in (select id from interfaces where host_id = ' . $onadb->qstr($form['host_id']) . ') OR interface_id in (select interface_id from interface_clusters where host_id = ' . $onadb->qstr($form['host_id']) . ')' . $filter, "", 0); } } } else { list($status, $rows, $results) = db_get_records($onadb, 'dns', $where . $filter, $orderby, $conf['search_results_per_page'], $offset); // If we got less than search_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, 'dns', $where . $filter, "", 0); } } } $count = $rows; // // *** BUILD HTML LIST *** // $html .= <<<EOL <!-- dns record Results --> <table id="{$form['form_id']}_dns_record_list" class="list-box" cellspacing="0" border="0" cellpadding="0"> <!-- Table Header --> <tr> <td colspan="2" class="list-header" align="center" style="{$style['borderR']};">Name</td> <td class="list-header" align="center" style="{$style['borderR']};">Time to Live</td> <td class="list-header" align="center" style="{$style['borderR']};">Type</td> <td class="list-header" align="center" style="{$style['borderR']};">Data</td> <td class="list-header" align="center" style="{$style['borderR']};">Effective</td> EOL; if ($conf['dns_views']) { $html .= "<td class=\"list-header\" align=\"center\" style=\"{$style['borderR']};\">DNS View</td>"; } $html .= <<<EOL <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 // $last_record = array('name' => $results[0]['name'], 'domain_id' => $results[0]['domain_id']); // $last_record_count = 0; for ($i = 1; $i <= count($results); $i++) { $record = $results[$i]; // Get additional info about each host record $record = $results[$i - 1]; // if the interface is the primary_dns_id for the host then mark it $primary_record = ' '; if ($host['primary_dns_id'] == $record['id']) { $primary_record = '<img title="Primary DNS record" src="' . $images . '/silk/font_go.png" border="0">'; } // Check for interface records (and find out how many there are) list($status, $interfaces, $interface) = ona_get_interface_record(array('id' => $record['interface_id']), ''); if ($interfaces) { // Get the host record so we know what the primary interface is //list($status, $rows, $inthost) = ona_get_host_record(array('id' => $interface['host_id']), ''); // Make the type correct based on the IP passed in if (strlen($interface['ip_addr']) > 11 and $record['type'] == 'A') { $record['type'] = 'AAAA'; } $record['ip_addr'] = ip_mangle($interface['ip_addr'], 'dotted'); // 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'); // Create string to be embedded in HTML for display $data = <<<EOL {$record['ip_addr']} EOL; } else { // Get other DNS records which name this record as parent list($status, $rows, $dns_other) = ona_get_host_record(array('id' => $record['dns_id'])); // Create string to be embedded in HTML for display if ($rows) { $data = <<<EOL <a title="View host. ID: {$dns_other['id']}" class="nav" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_host\\', \\'host_id=>{$dns_other['id']}\\', \\'display\\')');" >{$dns_other['name']}</a >.<a title="View domain. ID: {$dns_other['domain_id']}" class="domain" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain\\', \\'domain_id=>{$dns_other['domain_id']}\\', \\'display\\')');" >{$dns_other['domain_fqdn']}</a> EOL; } } $record['notes_short'] = truncate($record['notes'], 30); // Add a dot to the end of record name for display purposes $record['name'] = $record['name'] . '.'; // Process PTR record if ($record['type'] == 'PTR') { list($status, $rows, $pointsto) = ona_get_dns_record(array('id' => $record['dns_id']), ''); list($status, $rows, $pdomain) = ona_get_domain_record(array('id' => $record['domain_id']), ''); // Flip the IP address $record['name'] = ip_mangle($record['ip_addr'], 'flip'); $record['domain'] = $pdomain['name']; if ($pdomain['parent_id']) { list($status, $rows, $parent) = ona_get_domain_record(array('id' => $pdomain['parent_id'])); $parent['name'] = ona_build_domain_name($parent['id']); $record['domain'] = $pdomain['name'] . '.' . $parent['name']; unset($parent['name']); } // strip down the IP to just the "host" part as it relates to the domain its in if (strstr($record['domain'], 'in-addr.arpa')) { $domain_part = preg_replace("/.in-addr.arpa\$/", '', $record['domain']); } else { $domain_part = preg_replace("/.ip6.arpa\$/", '', $record['domain']); } $record['name'] = preg_replace("/{$domain_part}\$/", '', $record['name']); $data = <<<EOL <a title="Edit DNS A record" class="act" onClick="xajax_window_submit('edit_record', 'dns_record_id=>{$record['dns_id']}', 'editor');" >{$pointsto['name']}</a>.<a title="View domain. ID: {$pointsto['domain_id']}" class="domain" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain\\', \\'domain_id=>{$pointsto['domain_id']}\\', \\'display\\')');" >{$pointsto['domain_fqdn']}</a>. EOL; } // Process CNAME record if ($record['type'] == 'CNAME') { list($status, $rows, $cname) = ona_get_dns_record(array('id' => $record['dns_id']), ''); $data = <<<EOL <a title="Edit DNS A record" class="act" onClick="xajax_window_submit('edit_record', 'dns_record_id=>{$record['dns_id']}', 'editor');" >{$cname['name']}</a>.<a title="View domain. ID: {$cname['domain_id']}" class="domain" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain\\', \\'domain_id=>{$cname['domain_id']}\\', \\'display\\')');" >{$cname['domain_fqdn']}</a>. EOL; } // Process NS record if ($record['type'] == 'NS') { // clear out the $record['domain'] value so it shows properly in the list $record['name'] = ''; list($status, $rows, $ns) = ona_get_dns_record(array('id' => $record['dns_id']), ''); $data = <<<EOL <a title="Edit DNS A record" class="act" onClick="xajax_window_submit('edit_record', 'dns_record_id=>{$record['dns_id']}', 'editor');" >{$ns['name']}</a>.<a title="View domain. ID: {$ns['domain_id']}" class="domain" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain\\', \\'domain_id=>{$ns['domain_id']}\\', \\'display\\')');" >{$ns['domain_fqdn']}</a>. EOL; } // Process MX record if ($record['type'] == 'MX') { // show the preference value next to the type $record['type'] = "{$record['type']} ({$record['mx_preference']})"; list($status, $rows, $mx) = ona_get_dns_record(array('id' => $record['dns_id']), ''); $data = <<<EOL <a title="Edit DNS A record" class="act" onClick="xajax_window_submit('edit_record', 'dns_record_id=>{$record['dns_id']}', 'editor');" >{$mx['name']}</a>.<a title="View domain. ID: {$mx['domain_id']}" class="domain" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain\\', \\'domain_id=>{$mx['domain_id']}\\', \\'display\\')');" >{$mx['domain_fqdn']}</a>. EOL; } // Process SRV record if ($record['type'] == 'SRV') { // show the preference value next to the type $record['type'] = "{$record['type']} ({$record['srv_port']})"; list($status, $rows, $srv) = ona_get_dns_record(array('id' => $record['dns_id']), ''); $data = <<<EOL <a title="Edit DNS A record" class="act" onClick="xajax_window_submit('edit_record', 'dns_record_id=>{$record['dns_id']}', 'editor');" >{$srv['name']}</a>.<a title="View domain. ID: {$srv['domain_id']}" class="domain" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_domain\\', \\'domain_id=>{$srv['domain_id']}\\', \\'display\\')');" >{$srv['domain_fqdn']}</a>. EOL; } // Process TXT record if ($record['type'] == 'TXT') { // some records will have an interfaceid and dnsid when associated to another dns name // some will just be un associated txt records or domain only records. Determine that here and // display appropriately. This is to ensure associated DNS records match up if the name changes if ($record['interface_id'] and $record['dns_id']) { list($status, $rows, $txtmain) = ona_get_dns_record(array('id' => $record['dns_id']), ''); $record['name'] = $txtmain['name'] . '.'; } $data = truncate($record['txt'], 70); } // Get the domain name and domain ttl $ttl_style = 'title="Time-to-Live"'; list($status, $rows, $domain) = ona_get_domain_record(array('id' => $record['domain_id'])); // Make record['domain'] have the right name in it if ($record['type'] != 'PTR') { $record['domain'] = $domain['fqdn']; } // clear out the $record['domain'] value so it shows properly in the list for NS records if ($record['type'] == 'NS') { $record['domain'] = $domain['fqdn']; } // if the ttl is blank, use the one in the domain (minimum) if ($record['ttl'] == 0) { $record['ttl'] = $domain['default_ttl']; $ttl_style = 'style="font-style: italic;" title="Using TTL from domain"'; } // format the ebegin using the configured date format $ebegin = ''; // If it is in the future, print the time if (strtotime($record['ebegin']) > time()) { $ebegin = '<span title="Active in DNS on: ' . $record['ebegin'] . '">' . date($conf['date_format'], strtotime($record['ebegin'])) . '</span>'; } // If it is 0 then show as disabled if (strtotime($record['ebegin']) < 0) { $ebegin = <<<EOL <span style="background-color:#FFFF99;" title="Disabled: Won't build in DNS" onClick="var doit=confirm('Are you sure you want to enable this DNS record?'); if (doit == true) xajax_window_submit('edit_record', xajax.getFormValues('{$form['form_id']}_list_record_{$record['id']}'), 'enablerecord');" >Disabled</span> EOL; } // If we get this far and the name we have built has a leading . in it then remove the dot. $record['name'] = preg_replace("/^\\./", '', $record['name']); // Get the name of the view and the description if ($conf['dns_views']) { list($status, $rows, $dnsview) = ona_get_dns_view_record(array('id' => $record['dns_view_id'])); $record['view_name'] = $dnsview['name']; $record['view_desc'] = $dnsview['description']; } // 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" style="padding-right: 2px; padding-left: 4px;" width="16px"> {$primary_record} </td> <td class="list-row"> <span title="Record. ID: {$record['id']}" onClick="" >{$record['name']}</span ><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"> <span onClick="" {$ttl_style} >{$record['ttl']} seconds</span> </td> <td class="list-row"> <span title="Record Type" onClick="" >{$record['type']}</span> </td> <td class="list-row" align="left"> EOL; // Put the data in! $html .= $data; $html .= <<<EOL </td> <td class="list-row" align="center"> {$ebegin} </td> EOL; // Display the view we are part of if ($conf['dns_views']) { $html .= <<<EOL <td class="list-row" align="center" title="{$record['view_desc']}"> {$record['view_name']} </td> EOL; } $html .= <<<EOL <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_record_{$record['id']}" ><input type="hidden" name="dns_record_id" value="{$record['id']}" ><input type="hidden" name="host_id" value="{$host['id']}" ><input type="hidden" name="js" value="{$refresh}" ></form> EOL; if (auth('dns_record_modify')) { // If it is an A record but not the primary, display an option to make it primary. and only if we are dealing with a specific host if (($record['type'] == 'A' or $record['type'] == 'AAAA') and $host['primary_dns_id'] != $record['id'] and $form['host_id']) { $html .= <<<EOL <a title="Make this the primary DNS record" class="act" onClick="var doit=confirm('Are you sure you want to make this the primary DNS record for this host?'); if (doit == true) xajax_window_submit('edit_record', xajax.getFormValues('{$form['form_id']}_list_record_{$record['id']}'), 'makeprimary');" ><img src="{$images}/silk/font_go.png" border="0"></a> EOL; } } // display a view host button on the dns record search form list if ($form['search_form_id'] == 'dns_record_search_form') { $html .= <<<EOL <a title="View associated host record: {$interface['host_id']}" class="act" onClick="xajax_window_submit('display_host', 'host_id=>{$interface['host_id']}', 'display');" ><img src="{$images}/silk/computer_go.png" border="0"></a> EOL; } if (auth('dns_record_modify')) { $html .= <<<EOL <a title="Edit DNS record" class="act" onClick="xajax_window_submit('edit_record', xajax.getFormValues('{$form['form_id']}_list_record_{$record['id']}'), 'editor');" ><img src="{$images}/silk/page_edit.png" border="0"></a> EOL; } if (auth('dns_record_del')) { $html .= <<<EOL <a title="Delete DNS record" class="act" onClick="xajax_window_submit('edit_record', xajax.getFormValues('{$form['form_id']}_list_record_{$record['id']}'), 'delete');" ><img src="{$images}/silk/delete.png" border="0"></a> EOL; } $html .= <<<EOL </td> </tr> EOL; // reset the record counter before we go back for the next iteration $last_record = array('name' => $record['name'], 'domain_id' => $record['domain_id']); $last_record_count = 1; } $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']); // 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 ws_editor($window_name, $form = '') { global $conf, $self, $onadb; global $font_family, $color, $style, $images; $window = array(); $window['js'] = ''; $typedisable = ''; // Check permissions if (!(auth('dns_record_modify') and auth('dns_record_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 DNS record (and associated info) if $form is a dns_record_id $host = array('fqdn' => '.'); $interface = array(); if (is_numeric($form['dns_record_id'])) { list($status, $rows, $dns_record) = ona_get_dns_record(array('id' => $form['dns_record_id'])); if ($rows) { // Load associated INTERFACE record(s) list($status, $interfaces, $interface) = ona_get_interface_record(array('id' => $dns_record['interface_id'])); $interface['ip_addr'] = ip_mangle($interface['ip_addr'], 'dotted'); list($status, $rows, $domain) = ona_get_domain_record(array('id' => $dns_record['domain_id'])); $dns_record['domain_fqdn'] = $domain['fqdn']; } } // 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'])); $dns_record['domain_fqdn'] = $domain['fqdn']; } // Load a interface record if we got passed a interface_id if ($form['interface_id']) { list($status, $rows, $int) = ona_get_interface_record(array('id' => $form['interface_id'])); $interface['ip_addr'] = $int['ip_addr_text']; $window['js'] .= "el('set_ip_{$window_name}').value = '{$int['ip_addr_text']}'"; $form['js'] = "xajax_window_submit('work_space', 'xajax_window_submit(\\'display_host\\', \\'host=>{$int['host_id']}\\', \\'display\\')');"; } // Escape data for display in html foreach (array_keys((array) $dns_record) as $key) { $dns_record[$key] = htmlentities($dns_record[$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 type to AAAA if it is an ipv6 address if ($dns_record['type'] == 'A' and strstr($interface['ip_addr'], ':')) { $dns_record['type'] = 'AAAA'; } // If its a CNAME, get the dns name for the A record it points to if ($dns_record['type'] == 'CNAME' or $dns_record['type'] == 'MX' or $dns_record['type'] == 'PTR' or $dns_record['type'] == 'NS' or $dns_record['type'] == 'SRV') { list($status, $rows, $existinga_data) = ona_get_dns_record(array('id' => $dns_record['dns_id'])); $dns_record['existinga_data'] = $existinga_data['fqdn']; } // If its a PTR we need to build the hostname part if ($dns_record['type'] == 'PTR') { // Flip the IP address $dns_record['name'] = ip_mangle($interface['ip_addr'], 'flip'); // strip down the IP to just the "host" part as it relates to the domain its in $domain_part = preg_replace("/.in-addr.arpa\$/", '', $dns_record['domain_fqdn']); $dns_record['name'] = preg_replace("/.{$domain_part}\$/", '', $dns_record['name']); // Disable the edit boxes related to the A record info $window['js'] .= "el('set_hostname_{$window_name}').disabled='1';el('set_domain_{$window_name}').disabled='1';el('set_a_record_{$window_name}').disabled='1';el('set_ip_{$window_name}').disabled='1';"; } // If its an A record,check to se if it has a PTR associated with it //FIXME: MP dont forget that if you change the ip of an A record that you must also update any PTR records reference to that interface $ptr_readonly = ''; if ($dns_record['type'] == 'A' or $dns_record['type'] == 'AAAA') { list($status, $rows, $hasptr) = ona_get_dns_record(array('interface_id' => $dns_record['interface_id'], 'type' => 'PTR')); if ($rows) { $hasptr_msg = '<- Already has PTR record'; $ptr_readonly = 'disabled="1"'; } } $ttl_style = ''; $editdisplay = ''; $record_types = array(); // Set the window title: if ($dns_record['id']) { $typedisable = 'disabled="1"'; if ($dns_record['dns_id']) { $viewdisable = 'disabled="1"'; } $auto_ptr_checked = ''; $window['title'] = "Edit DNS Record"; $window['js'] .= "el('record_type_select').onchange('fake event');updatednsinfo('{$window_name}');el('set_hostname_{$window_name}').focus();"; // If you are editing and there is no ttl set, use the one from the domain. if (!$dns_record['ttl']) { $ttl_style = 'style="font-style: italic;" title="Using TTL from domain"'; } // add PTR type as an editable option to the record_types array array_push($record_types, "PTR"); // if we are passing in default values for a record, set them here from form data. if (strlen($form['ip_addr']) > 1) { $interface['ip_addr'] = ip_mangle($form['ip_addr'], 'dotted'); } if (strlen($form['hostname']) > 1) { $dns_record['name'] = $form['hostname']; } } else { $auto_ptr_checked = 'checked="1"'; $window['title'] = "Add DNS Record"; $dns_record['srv_pri'] = 0; $dns_record['srv_weight'] = 0; $dns_record['ebegin'] = date('Y-m-j G:i:s', time()); $window['js'] .= "el('record_type_select').onchange('fake event');updatednsinfo('{$window_name}');el('set_hostname_{$window_name}').focus();"; // if we are passing in default values for a new record, set them here from form data. if (strlen($form['ip_addr']) > 1) { $interface['ip_addr'] = ip_mangle($form['ip_addr'], 'dotted'); } if (strlen($form['hostname']) > 1) { $dns_record['name'] = $form['hostname']; } } // Set up the types of records we can edit with this form //$record_types = array('A','CNAME','TXT','NS','MX','AAAA','SRV'); // FIXME: MP cool idea here-- support the loc record and have a google map popup to search for the location then have it populate the coords from that. // FIXME: MP it would probably be much better to use ajax to pull back the right form content than all this other javascript crap. array_push($record_types, 'A', 'AAAA', 'CNAME', 'MX', 'NS', 'SRV', 'TXT', 'PTR'); foreach (array_keys((array) $record_types) as $id) { $record_types[$id] = htmlentities($record_types[$id]); $selected = ''; if ($record_types[$id] == $dns_record['type']) { $selected = 'SELECTED'; } $record_type_list .= "<option value=\"{$record_types[$id]}\" {$selected}>{$record_types[$id]}</option>\n"; } //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 ($dns_record['id'] and $entry['id'] == $dns_record['dns_view_id']) { $selected = "SELECTED=\"selected\""; } elseif (!$dns_record['id'] and $entry['id'] == 0) { // Otherwise use the default record if we are adding a new entry $selected = "SELECTED=\"selected\""; } $dns_view_list .= "<option {$selected} value=\"{$entry['id']}\">{$entry['name']}</option>\n"; } } // 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_a_record_{$window_name}', 'suggest_set_a_record_{$window_name}'); el('set_hostname_{$window_name}').focus(); EOL; // Define the window's inner html $window['html'] = <<<EOL <!-- DNS Record Edit Form --> <form id="{$window_name}_edit_form" onSubmit="return false;"> <input type="hidden" name="dns_id" value="{$dns_record['id']}"> <input type="hidden" name="name" value="{$host['fqdn']}"> <input type="hidden" name="js" value="{$form['js']}"> EOL; // If we are editing and thus disabling the type selector, we need to put a hidden input field if ($typedisable) { $window['html'] .= "<input type=\"hidden\" name=\"set_type\" value=\"{$dns_record['type']}\">"; } // If we are editing and thus disabling the view selector, we need to put a hidden input field if ($viewdisable) { $window['html'] .= "<input type=\"hidden\" name=\"set_view\" value=\"{$dns_record['dns_view_id']}\">"; } $window['html'] .= <<<EOL <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;" width="100%"> <!-- DNS RECORD --> <tr> <td align="right" nowrap="true"> <b><u>DNS Record</u></b> </td> <td class="padding" align="left" width="100%"> </td> </tr> </table> <!-- RECORD TYPE CONTAINER --> <div id="type_container"> <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;" width="100%"> EOL; // Print a dns view selector if ($conf['dns_views']) { $window['html'] .= <<<EOL <tr> <td align="right" nowrap="true"> DNS View </td> <td class="padding" align="left" width="100%"> <select {$viewdisable} id="dns_view_select" name="set_view" alt="DNS View" class="edit" >{$dns_view_list}</select> </td> </tr> EOL; } $window['html'] .= <<<EOL <tr> <td class="input_required" align="right" nowrap="true"> DNS Record Type </td> <td class="padding" align="left" width="100%"> <select {$typedisable} id="record_type_select" name="set_type" alt="Record type" class="edit" onchange="var selectBox = el('record_type_select'); el('info_{$window_name}').innerHTML = ''; el('ptr_info_{$window_name}').innerHTML = ''; el('a_container').style.display = (selectBox.value == 'AAAA' || selectBox.value == 'A' || selectBox.value == 'PTR') ? '' : 'none'; el('autoptr_container').style.display = (selectBox.value == 'AAAA' || selectBox.value == 'A') ? '' : 'none'; el('mx_container').style.display = (selectBox.value == 'MX') ? '' : 'none'; el('srv_container').style.display = (selectBox.value == 'SRV') ? '' : 'none'; el('txt_container').style.display = (selectBox.value == 'TXT') ? '' : 'none'; el('name_container').style.display = (selectBox.value == 'NS' || selectBox.value == 'PTR') ? 'none' : ''; el('domain_name_container').style.display = (selectBox.value == 'PTR') ? 'none' : ''; el('existing_a_container').style.display = (selectBox.value == 'MX' || selectBox.value == 'PTR'|| selectBox.value == 'CNAME' || selectBox.value == 'NS' || selectBox.value == 'SRV') ? '' : 'none';" >{$record_type_list}</select> </td> </tr> </table> </div> <!-- COMMON CONTAINER --> <div id="common_container" style="background-color: #F2F2F2;"> <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;"> <tr id="name_container"> <td class="input_required" align="right" nowrap="true"> Host Name </td> <td class="padding" align="left" width="100%"> <input id="set_hostname_{$window_name}" name="set_name" alt="Hostname" value="{$dns_record['name']}" class="edit" type="text" size="25" maxlength="64" onblur="updatednsinfo('{$window_name}');" /> </td> </tr> <tr id="domain_name_container"> <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="{$dns_record['domain_fqdn']}" class="edit" type="text" size="25" maxlength="64" onblur="updatednsinfo('{$window_name}');" /> <div id="suggest_set_domain_{$window_name}" class="suggest"></div> </td> </tr> EOL; // If there is a ttl in the record then display it instead of the domain setting message $ttlrow_style = ''; if ($dns_record['ttl'] == 0) { $ttlrow_style = 'style="display:none;"'; $window['html'] .= <<<EOL <tr id="ttlrowdesc"> <td align="right" nowrap="true"> TTL </td> <td class="padding" align="left" width="100%" nowrap="true"> Defaults to domain setting, <a onclick="el('ttlrowdesc').style.display = 'none';el('ttlrow').style.display = '';">override</a> </td> </tr> EOL; } $window['html'] .= <<<EOL <tr id="ttlrow" {$ttlrow_style}> <td align="right" nowrap="true"> TTL </td> <td class="padding" align="left" width="100%"> <input {$ttl_style} id="set_ttl" name="set_ttl" alt="TTL" value="{$dns_record['ttl']}" class="edit" type="text" size="20" maxlength="20" onblur="updatednsinfo('{$window_name}');" onfocus="updatednsinfo('{$window_name}');" /> </td> </tr> <tr id="ebeginrow"> <td align="right" nowrap="true"> Begin </td> <td class="padding" align="left" width="100%"> EOL; if (strtotime($dns_record['ebegin']) < time() && strtotime($dns_record['ebegin']) > 1) { $window['html'] .= <<<EOL <input id="ebegin_input" style="display:none;" id="set_ebegin" name="set_ebegin" alt="Set a future begin time" value="{$dns_record['ebegin']}" class="edit" type="text" size="16" maxlength="30" /> <img id="ebegin_clock" style="margin-top: -6px;" src='{$images}/silk/clock.png' border='0' title="Set a future begin time" onclick="el('ebegin_clock').style.display = 'none';el('ebegin_input').style.display = '';" > -or- <input name="disable" alt="Disable this DNS entry" type="checkbox" > Disable EOL; } else { $ebegin_clockstyle = ''; $ebegin_style = 'style="display:none;"'; // If record is disabled, then check the box and hide the input box. Also set up a fake ebegin for now() in case they re-enable if (strtotime($dns_record['ebegin']) < 0) { $ebegin_disabled = 'checked="1"'; $dns_record['ebegin'] = date('Y-m-j G:i:s', time()); } if (strtotime($dns_record['ebegin']) > time()) { $ebegin_clockstyle = 'display:none;'; $ebegin_style = ''; } $window['html'] .= <<<EOL <input id="ebegin_input" {$ebegin_style} id="set_ebegin" name="set_ebegin" alt="TTL" value="{$dns_record['ebegin']}" class="edit" type="text" size="16" maxlength="30" /> <img id="ebegin_clock" style="margin-top: -6px;{$ebegin_clockstyle}" src='{$images}/silk/clock.png' border='0' title="Set a future begin time" onclick="el('ebegin_clock').style.display = 'none';el('ebegin_input').style.display = '';" > -or- <input name="disable" alt="Disable this DNS entry" type="checkbox" {$ebegin_disabled} > Disable EOL; } $window['html'] .= <<<EOL </td> </tr> <!-- A RECORD CONTAINER --> <tr id="a_container"> <td class="input_required" align="right" nowrap="true"> IP Address </td> <td class="padding" align="left" width="100%" nowrap="true"> <input id="set_ip_{$window_name}" name="set_ip" alt="IP Address" value="{$interface['ip_addr']}" class="edit" type="text" size="45" maxlength="64" onblur="updatednsinfo('{$window_name}');" /> </td> </tr> <tr id="autoptr_container"> <td align="right" nowrap="true"> Create PTR </td> <td class="padding" align="left" width="100%" nowrap> <input id="set_auto_ptr" name="set_addptr" alt="Automaticaly create PTR record" type="checkbox" {$auto_ptr_checked} {$ptr_readonly} onchange="updatednsinfo('{$window_name}');" />{$hasptr_msg} </td> </tr> <!-- TXT CONTAINER --> <tr id="txt_container" style="display:none;"> <td class="input_required" align="right" nowrap="true"> TXT value </td> <td class="padding" align="left" width="100%"> <input id="set_txt_{$window_name}" name="set_txt" alt="TXT value" value="{$dns_record['txt']}" class="edit" type="text" size="25" maxlength="255" onblur="updatednsinfo('{$window_name}');" /> </td> </tr> <!-- MX CONTAINER --> <tr id="mx_container" style="display:none;"> <td class="input_required" align="right" nowrap="true"> MX Preference </td> <td class="padding" align="left" width="100%"> <input id="set_mx_preference_{$window_name}" name="set_mx_preference" alt="MX preference" value="{$dns_record['mx_preference']}" class="edit" type="text" size="5" maxlength="5" onblur="updatednsinfo('{$window_name}');" /> </td> </tr> <!-- SRV CONTAINER --> <tr id="srv_container" style="display:none;"> <td class="input_required" align="right" nowrap="true"> Priority<br>Weight<br>Port </td> <td class="padding" align="left" width="100%"> <input style="margin-bottom:3px;" id="set_srv_pri_{$window_name}" name="set_srv_pri" alt="SRV Priority" value="{$dns_record['srv_pri']}" class="edit" type="text" size="5" maxlength="5" onblur="updatednsinfo('{$window_name}');" /><br /> <input style="margin-bottom:3px;" id="set_srv_weight_{$window_name}" name="set_srv_weight" alt="SRV Weight" value="{$dns_record['srv_weight']}" class="edit" type="text" size="5" maxlength="5" onblur="updatednsinfo('{$window_name}');" /><br /> <input id="set_srv_port_{$window_name}" name="set_srv_port" alt="SRV Port" value="{$dns_record['srv_port']}" class="edit" type="text" size="5" maxlength="5" onblur="updatednsinfo('{$window_name}');" /> </td> </tr> <!-- CNAME CONTAINER --> <tr id="existing_a_container" style="display:none;"> <td class="input_required" align="right" nowrap="true"> Existing A record </td> <td class="padding" align="left" width="100%"> <input id="set_a_record_{$window_name}" name="set_pointsto" alt="Points to existing A record" value="{$dns_record['existinga_data']}" class="edit" type="text" size="25" maxlength="64" onblur="updatednsinfo('{$window_name}');" /> <div id="suggest_set_a_record_{$window_name}" class="suggest"></div> </td> </tr> <!-- NOTES CONTAINER --> <tr id="notes_container"> <td align="right" nowrap="true"> Notes </td> <td class="padding" align="left" width="100%"> <input id="set_notes_{$window_name}" name="set_notes" alt="Notes" value="{$dns_record['notes']}" class="edit" type="text" size="45" maxlength="64" /> </td> </tr> <!-- RECORD INFO --> <tr> <td colspan="2" class="padding" align="center" width="100%" nowrap="true"> <span id="info_{$window_name}" style="color: green;font-family: monospace;"></span> </td> </tr> <tr> <td colspan="2" class="padding" align="center" width="100%" nowrap="true"> <span id="ptr_info_{$window_name}" style="color: green;font-family: monospace;"></span> </td> </tr> </table> </div> <table cellspacing="0" border="0" cellpadding="0" width="100%" style="background-color: {$color['window_content_bg']}; padding-left: 20px; padding-right: 20px; padding-top: 5px; padding-bottom: 5px;"> EOL; if (!$dns_record['id']) { $window['html'] .= <<<EOL <tr> <td align="right" nowrap="true"> </td> <td class="padding" align="left" width="100%" nowrap="true"> <input name="keepadding" alt="Keep adding more DNS records" type="checkbox" onfocus="updatednsinfo('{$window_name}');" > Keep adding more DNS records </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 colspan="2" 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 format_array($array = array()) { $text = ''; foreach (array_keys($array) as $key) { // Make some data look pretty if ($key == 'ip_addr') { $array[$key] = ip_mangle($array[$key], 'dotted'); } else { if ($key == 'ip_addr_start') { $array[$key] = ip_mangle($array[$key], 'dotted'); } else { if ($key == 'ip_addr_end') { $array[$key] = ip_mangle($array[$key], 'dotted'); } else { if ($key == 'ip_mask') { $array[$key] = ip_mangle($array[$key]) . ' (/' . ip_mangle($array[$key], 'cidr') . ')'; } else { if ($key == 'mac_addr') { $array[$key] = mac_mangle($array[$key]); if ($array[$key] == -1) { $array[$key] = ''; } } else { if ($key == 'host_id') { list($status, $rows, $host) = ona_find_host($array[$key]); if ($host['id']) { $array[$key] = str_pad($array[$key], 20) . strtolower("({$host['fqdn']})"); } } else { if ($key == 'server_id') { list($status, $rows, $server) = ona_get_server_record(array('id' => $array[$key])); list($status, $rows, $host) = ona_find_host($server['host_id']); if ($host['id']) { $array[$key] = str_pad($array[$key], 20) . strtolower("({$host['fqdn']})"); } } else { if ($key == 'subnet_id') { list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $array[$key])); if ($subnet['id']) { $array[$key] = str_pad($array[$key], 20) . strtoupper("({$subnet['name']})"); } } else { if ($key == 'domain_id' or $key == 'primary_dns_domain_id') { list($status, $rows, $domain) = ona_get_domain_record(array('id' => $array[$key])); $array[$key] = str_pad($array[$key], 20) . strtolower("({$domain['fqdn']})"); } else { if ($key == 'interface_id') { list($status, $rows, $interface) = ona_get_interface_record(array('id' => $array[$key])); $array[$key] = str_pad($array[$key], 20) . '(' . ip_mangle($interface['ip_addr'], 'dotted') . ')'; } else { if ($key == 'device_type_id') { list($status, $rows, $devtype) = ona_get_device_type_record(array('id' => $array[$key])); if ($devtype['id']) { list($status, $rows, $model) = ona_get_model_record(array('id' => $devtype['model_id'])); list($status, $rows, $role) = ona_get_role_record(array('id' => $devtype['role_id'])); list($status, $rows, $manu) = ona_get_manufacturer_record(array('id' => $model['manufacturer_id'])); $array[$key] = str_pad($array[$key], 20) . "({$manu['name']}, {$model['name']} ({$role['name']}))"; } } else { if ($key == 'custom_attribute_type_id') { list($status, $rows, $ca) = ona_get_custom_attribute_type_record(array('id' => $array[$key])); if ($ca['id']) { $array[$key] = str_pad($array[$key], 20) . "({$ca['name']})"; } } } } } } } } } } } } } // Align columns if ($array[$key]) { $text .= str_pad(" {$key}", 30) . $array[$key] . "\n"; } } // Return a nice string :) return $text; }
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 ona_find_interface($search = "") { printmsg("DEBUG => ona_find_interface({$search}) called", 3); // Validate input if ($search == "") { return array(1, 0, array()); } // If it's numeric if (is_numeric($search)) { // It's a number - do several sql queries and see if we can get a unique match foreach (array('id', 'host_id', 'ip_addr') as $field) { list($status, $rows, $record) = ona_get_interface_record("{$field} like '{$search}'"); // If we got it, return it if ($status == 0 and $rows == 1) { printmsg("DEBUG => ona_find_interface() found interface record by {$field}", 2); return array(0, $rows, $record); } } } // If it's an IP address... $ip = ip_mangle($search, 1); if ($ip != -1) { list($status, $rows, $record) = ona_get_interface_record("ip_addr like '{$ip}'"); // If we got it, return it if ($status == 0 and $rows == 1) { printmsg("DEBUG => ona_find_interface() found record by IP address", 2); return array(0, $rows, $record); } // Otherwise return an error if ($rows == 0) { printmsg("DEBUG => No interface has the IP address: {$search}", 2); return array(3, $rows, array()); } printmsg("DEBUG => More than one interface has the IP address: {$search}", 2); return array(4, $rows, array()); } // If it's a MAC address $mac = mac_mangle($search, 1); if ($mac != -1) { // Search for it list($status, $rows, $record) = ona_get_interface_record(array('mac_addr' => $mac)); // If we got it, return it if (!$status and $rows == 1) { printmsg("DEBUG => ona_find_interface() found record by MAC address", 2); return array(0, $rows, $record); } // Otherwise return an error if ($rows == 0) { printmsg("ERROR => No interface has the MAC address: {$search}", 2); return array(5, 0, array()); } printmsg("DEBUG => ona_find_interface() More than one interface has the MAC address: " . mac_mangle($mac, 1), 0); return array(6, 0, array()); } // We didn't find it - return and error code, 0 matches, and an empty record. printmsg("DEBUG => ona_find_interface() couldn't find a unique interface record with specified search criteria", 1); return array(2, 0, array()); }
function build_bind_domain($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.50'; printmsg("DEBUG => build_bind_domain({$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['domain']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOF build_bind_domain-v{$version} Builds a zone file for a dns server from the database Synopsis: build_bind_domain [KEY=VALUE] ... Required: domain=DOMAIN or ID build zone file for specified domain EOF ); } // Get the domain information list($status, $rows, $domain) = ona_find_domain($options['domain']); printmsg("DEBUG => build_bind_domain() Domain record: {$domain['domain']}", 3); if (!$domain['id']) { printmsg("DEBUG => Unknown domain record: {$options['domain']}", 3); $self['error'] = "ERROR => Unknown domain record: {$options['domain']}"; return array(2, $self['error'] . "\n"); } // if for some reason the domains default_ttl is not set, use the one from the $conf['dns']['default_ttl'] if ($domain['default_ttl'] == 0) { $domain['default_ttl'] = $conf['dns']['default_ttl']; } if ($domain['primary_master'] == '') { $domain['primary_master'] = 'localhost'; } // loop through records and display them $q = "\n SELECT *\n FROM dns\n WHERE domain_id = {$domain['id']}\n ORDER BY type"; // exectue the query $rs = $onadb->Execute($q); if ($rs === false or !$rs->RecordCount()) { $self['error'] = 'ERROR => build_zone(): SQL query failed: ' . $onadb->ErrorMsg(); printmsg($self['error'], 0); $exit += 1; } $rows = $rs->RecordCount(); // check if this is a ptr domain that has delegation if (strpos(str_replace('in-addr.arpa', '', $domain['fqdn']), '-')) { $ptrdelegation = true; } // Start building the named.conf - save it in $text $text = "; DNS zone file for {$domain['fqdn']} built on " . date($conf['date_format']) . "\n"; // print the opening host comment with row count $text .= "; TOTAL RECORDS (count={$rows})\n\n"; // FIXME: MP do more to ensure that dots are at the end as appropriate $text .= "\$ORIGIN {$domain['fqdn']}.\n"; $text .= "\$TTL {$domain['default_ttl']}\n"; $text .= ";Serial number is current unix timestamp (seconds since UTC)\n\n"; // NOTE: There are various ways that one could generate the serial. The bind book suggests YYYYMMDDXX where XX is 1/100th of the day or some counter in the day. // I feel this is too limiting. I prefer the Unix timestamp (seconds since UTC) method. TinyDNS uses this method as well and it allows for much more granularity. // Referr to the following for some discussion on the topic: http://www.lifewithdjbdns.com/#Migration // NOTE: for now I am generating the serial each time the zone is built. I'm ignoring, and may remove, the one stored in the database. $serial_number = time(); // Build the SOA record // FIXME: MP do a bit more to ensure that dots are where they should be $text .= "@ IN SOA {$domain['primary_master']}. {$domain['admin_email']} ({$serial_number} {$domain['refresh']} {$domain['retry']} {$domain['expiry']} {$domain['minimum']})\n\n"; // Loop through the record set while ($dnsrecord = $rs->FetchRow()) { // Dont build records that begin in the future if (strtotime($dnsrecord['ebegin']) > time()) { continue; } if (strtotime($dnsrecord['ebegin']) < 0) { continue; } // If there are notes, put the comment character in front of it if ($dnsrecord['notes']) { $dnsrecord['notes'] = '; ' . str_replace("\n", "; ", $dnsrecord['notes']); } // If the ttl is empty then make it truely empty if ($dnsrecord['ttl'] == 0) { $dnsrecord['ttl'] = ''; } // Also, if the records ttl is the same as the domains ttl then dont display it, just to keep it "cleaner" if (!strcmp($dnsrecord['ttl'], $domain['default_ttl'])) { $dnsrecord['ttl'] = ''; } // Dont print a dot unless hostname has a value if ($dnsrecord['name']) { $dnsrecord['name'] = $dnsrecord['name'] . '.'; } if ($dnsrecord['type'] == 'A') { // Find the interface record list($status, $rows, $interface) = ona_get_interface_record(array('id' => $dnsrecord['interface_id'])); if ($status or !$rows) { printmsg("ERROR => Unable to find interface record!", 3); $self['error'] = "ERROR => Unable to find interface record!"; return array(5, $self['error'] . "\n"); } $fqdn = $dnsrecord['name'] . $domain['fqdn']; $text .= sprintf("%-50s %-8s IN %-8s %-30s %s\n", $fqdn . '.', $dnsrecord['ttl'], $dnsrecord['type'], $interface['ip_addr_text'], $dnsrecord['notes']); } if ($dnsrecord['type'] == 'PTR') { // Find the interface record list($status, $rows, $interface) = ona_get_interface_record(array('id' => $dnsrecord['interface_id'])); if ($status or !$rows) { printmsg("ERROR => Unable to find interface record!", 3); $self['error'] = "ERROR => Unable to find interface record!"; return array(5, $self['error'] . "\n"); } // Get the name info that the cname points to list($status, $rows, $ptr) = ona_get_dns_record(array('id' => $dnsrecord['dns_id']), ''); // If this is a delegation domain, find the subnet cidr if ($ptrdelegation) { list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $interface['subnet_id'])); $ip_last = ip_mangle($interface['ip_addr'], 'flip'); $ip_last_digit = substr($ip_last, 0, strpos($ip_last, '.')); $ip_remainder = substr($ip_last, strpos($ip_last, '.')) . '.in-addr.arpa.'; $text .= sprintf("%-50s %-8s IN %-8s %s.%-30s %s\n", $ip_last_digit . '-' . ip_mangle($subnet['ip_mask'], 'cidr') . $ip_remainder, $dnsrecord['ttl'], $dnsrecord['type'], $ptr['name'], $ptr['domain_fqdn'] . '.', $dnsrecord['notes']); } else { $text .= sprintf("%-50s %-8s IN %-8s %s.%-30s %s\n", ip_mangle($interface['ip_addr'], 'flip') . '.in-addr.arpa.', $dnsrecord['ttl'], $dnsrecord['type'], $ptr['name'], $ptr['domain_fqdn'] . '.', $dnsrecord['notes']); } } if ($dnsrecord['type'] == 'CNAME') { // Find the interface record list($status, $rows, $interface) = ona_get_interface_record(array('id' => $dnsrecord['interface_id'])); if ($status or !$rows) { printmsg("ERROR => Unable to find interface record!", 3); $self['error'] = "ERROR => Unable to find interface record!"; return array(5, $self['error'] . "\n"); } // Get the name info that the cname points to list($status, $rows, $cname) = ona_get_dns_record(array('id' => $dnsrecord['dns_id']), ''); $fqdn = $dnsrecord['name'] . $domain['fqdn']; $text .= sprintf("%-50s %-8s IN %-8s %s.%-30s %s\n", $fqdn . '.', $dnsrecord['ttl'], $dnsrecord['type'], $cname['name'], $cname['domain_fqdn'] . '.', $dnsrecord['notes']); } if ($dnsrecord['type'] == 'NS') { // Find the interface record list($status, $rows, $interface) = ona_get_interface_record(array('id' => $dnsrecord['interface_id'])); if ($status or !$rows) { printmsg("ERROR => Unable to find interface record!", 3); $self['error'] = "ERROR => Unable to find interface record!"; return array(5, $self['error'] . "\n"); } // Get the name info that the cname points to list($status, $rows, $ns) = ona_get_dns_record(array('id' => $dnsrecord['dns_id']), ''); $text .= sprintf("%-50s %-8s IN %-8s %s.%-30s %s\n", $domain['fqdn'] . '.', $dnsrecord['ttl'], $dnsrecord['type'], $ns['name'], $ns['domain_fqdn'] . '.', $dnsrecord['notes']); } if ($dnsrecord['type'] == 'MX') { // Find the interface record list($status, $rows, $interface) = ona_get_interface_record(array('id' => $dnsrecord['interface_id'])); if ($status or !$rows) { printmsg("ERROR => Unable to find interface record!", 3); $self['error'] = "ERROR => Unable to find interface record!"; return array(5, $self['error'] . "\n"); } // Get the name info that the cname points to list($status, $rows, $mx) = ona_get_dns_record(array('id' => $dnsrecord['dns_id']), ''); if ($dnsrecord['name']) { $name = $dnsrecord['name'] . $domain['fqdn']; } else { $name = $domain['name']; } $text .= sprintf("%-50s %-8s IN %s %-5s %s.%-30s %s\n", $name . '.', $dnsrecord['ttl'], $dnsrecord['type'], $dnsrecord['mx_preference'], $mx['name'], $mx['domain_fqdn'] . '.', $dnsrecord['notes']); } if ($dnsrecord['type'] == 'SRV') { // Find the interface record list($status, $rows, $interface) = ona_get_interface_record(array('id' => $dnsrecord['interface_id'])); if ($status or !$rows) { printmsg("ERROR => Unable to find interface record!", 3); $self['error'] = "ERROR => Unable to find interface record!"; return array(5, $self['error'] . "\n"); } // Get the name info that the cname points to list($status, $rows, $srv) = ona_get_dns_record(array('id' => $dnsrecord['dns_id']), ''); if ($dnsrecord['name']) { $name = $dnsrecord['name'] . $domain['fqdn']; } else { $name = $domain['name']; } $text .= sprintf("%-50s %-8s IN %s %s %s %-8s %-30s %s\n", $name . '.', $dnsrecord['ttl'], $dnsrecord['type'], $dnsrecord['srv_pri'], $dnsrecord['srv_weight'], $dnsrecord['srv_port'], $srv['fqdn'] . '.', $dnsrecord['notes']); } if ($dnsrecord['type'] == 'TXT') { $fqdn = $dnsrecord['name'] . $domain['fqdn']; $text .= sprintf("%-50s %-8s IN %-8s %-30s %s\n", $fqdn . '.', $dnsrecord['ttl'], $dnsrecord['type'], '"' . $dnsrecord['txt'] . '"', $dnsrecord['notes']); } } ////////////// Footer stuff ////////////////// // MP: FIXME: For now I"m not using this.. bind errors out if the file doesnt exist. need a deterministic way to do this. // Allow for a local footer include.. I expect this to rarely be used // $text .= "\n; Allow for a local footer include.. I expect this to rarely be used.\n"; // $text .= "\$INCLUDE named-{$domain['fqdn']}-footer\n"; // Return the zone file return array(0, $text); }
function ws_editor($window_name, $form = '') { global $conf, $self, $onadb; global $font_family, $color, $style, $images, $interface; $window = array(); // Check permissions if (!auth('interface_modify')) { $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); // If $form is a number, it's an alias record ID- so we transform $form into an array if (is_numeric($form)) { $form = array('interface_id' => $form); } // Load an existing record (and associated info) if we're editing if (is_numeric($form['interface_id'])) { list($status, $rows, $interface) = ona_get_interface_record(array('id' => $form['interface_id'])); if ($rows) { list($status, $rows, $host) = ona_find_host($interface['host_id']); list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $interface['subnet_id'])); list($status, $rows_nat, $extnatint) = ona_get_interface_record(array('id' => $record['nat_interface_id'])); $interface['ip_addr'] = ip_mangle($interface['ip_addr'], 'dotted'); if ($interface['mac_addr']) { $interface['mac_addr'] = mac_mangle($interface['mac_addr']); } if ($rows_nat > 0) { $interface['natip_addr'] = ip_mangle($extnatint['ip_addr'], 'dotted'); } } } else { // Maybe we didn't get an interface record, but we got a host record (adding an interface) // Set it in $interface so it's available below. if (is_numeric($form['host_id'])) { $interface['host_id'] = $form['host_id']; } if (is_numeric($form['subnet_id'])) { list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $form['subnet_id'])); } // allow various default items to be set during an add that are passed in from the form if (isset($form['ip_addr'])) { $interface['ip_addr'] = $form['ip_addr']; } if (isset($form['name'])) { $interface['name'] = $form['name']; } if (isset($form['description'])) { $interface['description'] = $form['description']; } if (isset($form['mac_addr'])) { $interface['mac_addr'] = $form['mac_addr']; } if (isset($form['natip_addr'])) { $interface['natip_addr'] = $form['natip_addr']; } } // Load the host record for display if ($interface['host_id']) { list($status, $rows, $host) = ona_find_host($interface['host_id']); } //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 ($dns_record['id'] and $entry['id'] == $dns_record['dns_view_id']) { $selected = "SELECTED=\"selected\""; } elseif (!$dns_record['id'] and $entry['id'] == 0) { // Otherwise use the default record if we are adding a new entry $selected = "SELECTED=\"selected\""; } $dns_view_list .= "<option {$selected} value=\"{$entry['id']}\">{$entry['name']}</option>\n"; } } // Escape data for display in html foreach (array_keys((array) $interface) as $key) { $interface[$key] = htmlentities($interface[$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) $host) as $key) { $host[$key] = htmlentities($host[$key], ENT_QUOTES, $conf['php_charset']); } // Set the window title: $window['title'] = "Add Interface"; if ($interface['id']) { $window['title'] = "Edit Interface"; } // 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; /* Setup the Quick Find FREE IP icon */ 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}');" ); }; suggest_setup('hostname', 'suggest_int_hostname'); el('hostname').focus(); EOL; // Define the window's inner html $window['html'] = <<<EOL <!-- Interface Edit Form --> <form id="{$window_name}_edit_form" onSubmit="return false;"> <input type="hidden" name="interface_id" value="{$interface['id']}"> EOL; if ($host['fqdn']) { $window['html'] .= <<<EOL <input type="hidden" name="host" value="{$interface['host_id']}"> <input type="hidden" name="hostfqdn" value="{$host['fqdn']}"> EOL; } $window['html'] .= <<<EOL <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;"> <!-- INTERFACE RECORD --> <tr> <td align="left" nowrap="true"><b><u>Interface Record</u></b> </td> <td class="padding" align="left" width="100%"> </td> </tr> <tr> EOL; if ($host['fqdn']) { $window['html'] .= <<<EOL <td align="right" nowrap="true"> Host: </td> <td class="padding" align="left" width="100%"> {$host['fqdn']} </td> EOL; } else { $window['html'] .= <<<EOL <td class="input_required" align="right" nowrap="true"> Existing Host </td> <td class="padding" align="left" width="100%"> <input id="hostname" name="host" alt="Hostname" value="{$host['name']}" class="edit" type="text" size="20" maxlength="64" > <div id="suggest_int_hostname" class="suggest"></div> </td> EOL; } $window['html'] .= <<<EOL </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%" nowrap="true"> <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 id="set_mac" 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 nowrap 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; // Show a "keep adding" checkbox if they are adding records if (!isset($interface['id'])) { // Print a dns view selector if ($conf['dns_views']) { $window['html'] .= <<<EOL <tr> <td align="right" nowrap="true"> DNS View for PTR </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 <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 nowrap class="padding" align="left" width="100%"> <input name="keepadding" alt="Keep adding more interfaces" type="checkbox" > Keep adding more interfaces </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 subnet_modify($options = "") { global $conf, $self, $onadb; //printmsg('DEBUG => subnet_modify('.implode (";",$options).') called', 3); // Version - UPDATE on every edit! $version = '1.08'; // Parse incoming options string to an array $options = parse_options($options); // Return the usage summary if we need to if ($options['help'] or !$options['subnet'] or !($options['set_ip'] or $options['set_netmask'] or $options['set_type'] or $options['set_name'] or array_key_exists('set_vlan', $options) or $options['set_security_level'])) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM subnet_modify-v{$version} Modify a subnet (subnet) record Synopsis: subnet_modify [KEY=VALUE] ... Where: subnet=[ID|IP] select subnet by search string Update: set_ip=IP change subnet "subnet" address set_netmask=MASK change subnet netmask set_name=TEXT change subnet name (i.e. "LAN-1234") set_type=TYPE change subnet type by name or id set_vlan=VLAN change vlan by name, number campus=CAMPUS vlan campus name or id to help identify vlan set_security_level=LEVEL numeric security level ({$conf['ona_lvl']}) EOM ); } $check_boundaries = 0; // Find the subnet record we're modifying list($status, $rows, $subnet) = ona_find_subnet($options['subnet']); if ($status or !$rows) { $self['error'] = "ERROR => Subnet not found"; return array(2, $self['error'] . "\n"); } // Check permissions if (!auth('subnet_modify')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(3, $self['error'] . "\n"); } // Validate the ip address if (!$options['set_ip']) { $options['set_ip'] = $subnet['ip_addr']; } else { $check_boundaries = 1; $options['set_ip'] = $setip = ip_mangle($options['set_ip'], 'numeric'); // FIXME: what if ip_mangle returns a GMP object? if ($options['set_ip'] == -1) { $self['error'] = "ERROR => The IP address specified is invalid!"; return array(4, $self['error'] . "\n"); } } // Validate the netmask is okay if (!$options['set_netmask']) { $options['set_netmask'] = $subnet['ip_mask']; $cidr = ip_mangle($options['set_netmask'], 'cidr'); } else { $check_boundaries = 1; $cidr = ip_mangle($options['set_netmask'], 'cidr'); // FIXME: what if ip_mangle returns a GMP object? $options['set_netmask'] = ip_mangle($options['set_netmask'], 'numeric'); if ($cidr == -1 or $options['set_netmask'] == -1) { $self['error'] = "ERROR => The netmask specified is invalid!"; return array(5, $self['error'] . "\n"); } } if (is_ipv4($setip)) { $padding = 32; $fmt = 'dotted'; $ip1 = ip_mangle($setip, 'binary'); $num_hosts = 0xffffffff - $options['set_netmask']; $first_host = $options['set_ip'] + 1; $last_host = $options['set_ip'] + $num_hosts; $str_last_host = $last_host; $last_last_host = $last_host - 1; } else { $padding = 128; $fmt = 'ipv6gz'; $ip1 = ip_mangle($setip, 'bin128'); $first_host = gmp_strval(gmp_add($options['set_ip'], 1)); $sub = gmp_sub("340282366920938463463374607431768211455", $options['set_netmask']); $last_host = gmp_add($options['set_ip'], $sub); $str_last_host = gmp_strval($last_host); $last_last_host = gmp_strval(gmp_sub($last_host, 1)); } // Validate that the subnet IP & netmask combo are valid together. $ip2 = str_pad(substr($ip1, 0, $cidr), $padding, '0'); $ip1 = ip_mangle($ip1, $fmt); $ip2 = ip_mangle($ip2, $fmt); if ($ip1 != $ip2) { $self['error'] = "ERROR => Invalid subnet specified - did you mean: {$ip2}/{$cidr}?"; return array(6, $self['error'] . "\n"); } // If our IP or netmask changed we need to make sure that // we won't abandon any host interfaces. // We also need to verify that the new boundaries are valid and // don't interefere with any other subnets. if ($check_boundaries == 1) { // *** Check to see if the new subnet overlaps any existing ONA subnets *** // // I convert the IP address to dotted format when calling ona_find_subnet() // because it saves it from doing a few unnecessary sql queries. // Look for overlaps like this (where new subnet address starts inside an existing subnet): // [ -- new subnet -- ] // [ -- old subnet --] list($status, $rows, $record) = ona_find_subnet(ip_mangle($options['set_ip'], 'dotted')); if ($rows and $record['id'] != $subnet['id']) { $self['error'] = "ERROR => Subnet address conflict! New subnet starts inside an existing subnet."; return array(7, $self['error'] . "\n" . "ERROR => Conflicting subnet record ID: {$record['id']}\n"); } // Look for overlaps like this (where the new subnet ends inside an existing subnet): // [ -- new subnet -- ] // [ -- old subnet --] // Find last address of our subnet, and see if it's inside of any other subnet: list($status, $rows, $record) = ona_find_subnet(ip_mangle($str_last_host, 'dotted')); if ($rows and $record['id'] != $subnet['id']) { $self['error'] = "ERROR => Subnet address conflict! New subnet ends inside an existing subnet."; return array(8, $self['error'] . "\n" . "ERROR => Conflicting subnet record ID: {$record['id']}\n"); } // Look for overlaps like this (where the new subnet entirely overlaps an existing subnet): // [ -------- new subnet --------- ] // [ -- old subnet --] // // Do a cool SQL query to find all subnets whose start address is >= or <= the // new subnet base address. $where = "ip_addr >= {$options['set_ip']} AND ip_addr <= {$str_last_host}"; list($status, $rows, $record) = ona_get_subnet_record($where); if ($rows > 1 or $rows == 1 and $record['id'] != $subnet['id']) { $self['error'] = "ERROR => Subnet address conflict! New subnet would encompass an existing subnet."; return array(9, $self['error'] . "\n" . "ERROR => Conflicting subnet record ID: {$record['id']}\n"); } // Look for any hosts that are currently in our subnet that would be // abandoned if we were to make the proposed changes. // Look for hosts on either side of the new subnet boundaries: // [--- new subnet ---] // * ** * * <-- Hosts: the first and last host would be a problem! // [------- old subnet --------] // $where1 = "subnet_id = {$subnet['id']} AND ip_addr < {$first_host}"; $where2 = "subnet_id = {$subnet['id']} AND ip_addr > {$last_last_host}"; list($status, $rows1, $record) = ona_get_interface_record($where1); list($status, $rows2, $record) = ona_get_interface_record($where2); if ($rows1 or $rows2) { $num = $rows1 + $rows2; $self['error'] = "ERROR => Changes would abandon {$num} hosts in an unallocated ip space"; return array(10, $self['error'] . "\n"); } // Look for any dhcp pools that are currently in our subnet that would be // abandoned if we were to make the proposed changes. // Look for existin pools with start/end values outside of new subnet range // [--- new subnet ---] // [--cur pool--] // [------- old subnet --------] // $where1 = "subnet_id = {$subnet['id']} AND ip_addr_start < {$options['set_ip']}"; $where2 = "subnet_id = {$subnet['id']} AND ip_addr_end > {$str_last_host}"; list($status, $rows1, $record) = ona_get_dhcp_pool_record($where1); list($status, $rows2, $record) = ona_get_dhcp_pool_record($where2); if ($rows1 or $rows2) { $num = $rows1 + $rows2; $self['error'] = "ERROR => Changes would abandon a DHCP pool in an unallocated ip space, adjust pool sizes first"; return array(10, $self['error'] . "\n"); } } // // Define the fields we're updating // // This variable will contain the updated info we'll insert into the DB $SET = array(); $SET['ip_addr'] = $options['set_ip']; $SET['ip_mask'] = $options['set_netmask']; // Set options['set_security_level']? // Sanitize "security_level" option if (array_key_exists('set_security_level', $options)) { $options['set_security_level'] = sanitize_security_level($options['set_security_level']); if ($options['set_security_level'] == -1) { return array(11, $self['error'] . "\n"); } $SET['lvl'] = $options['set_security_level']; } // Set options['set_name']? if ($options['set_name']) { // BUSINESS RULE: We require subnet names to be in upper case and spaces are converted to -'s. $options['set_name'] = trim($options['set_name']); $options['set_name'] = preg_replace('/\\s+/', '-', $options['set_name']); $options['set_name'] = strtoupper($options['set_name']); // Make sure there's not another subnet with this name list($status, $rows, $tmp) = ona_get_subnet_record(array('name' => $options['set_name'])); if ($status or $rows > 1 or $rows == 1 and $tmp['id'] != $subnet['id']) { $self['error'] = "ERROR => That name is already used by another subnet!"; return array(12, $self['error'] . "\n"); } $SET['name'] = $options['set_name']; } // Set options['set_type']? if ($options['set_type']) { // Find the type from $options[type] list($status, $rows, $subnet_type) = ona_find_subnet_type($options['set_type']); if ($status or $rows != 1) { $self['error'] = "ERROR => Invalid subnet type specified!"; return array(13, $self['error'] . "\n"); } printmsg("Subnet type selected: {$subnet_type['display_name']} ({$subnet_type['short_name']})", 1); $SET['subnet_type_id'] = $subnet_type['id']; } // Set options['set_vlan']? if (array_key_exists('set_vlan', $options) or $options['campus']) { if (!$options['set_vlan']) { $SET['vlan_id'] = ''; } else { // Find the VLAN ID from $options[set_vlan] and $options[campus] list($status, $rows, $vlan) = ona_find_vlan($options['set_vlan'], $options['campus']); if ($status or $rows != 1) { $self['error'] = "ERROR => The vlan/campus pair specified is invalid!"; return array(15, $self['error'] . "\n"); } printmsg("VLAN selected: {$vlan['name']} in {$vlan['vlan_campus_name']} campus", 1); $SET['vlan_id'] = $vlan['id']; } } // Update the subnet record list($status, $rows) = db_update_record($onadb, 'subnets', array('id' => $subnet['id']), $SET); if ($status or !$rows) { return array(16, $self['error'] . "\n"); } // Load the updated record for display list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $subnet['id'])); // Return the (human-readable) success notice $text = format_array($SET); $self['error'] = "INFO => Subnet UPDATED"; return array(0, $self['error'] . ":\n{$text}\n"); }
print "ERROR => Couldn't find include folder {$include}!\n"; exit; } require_once $base . '/config/config.inc.php'; /* --------------------------------------------------------- */ global $conf, $self, $onadb; // Uncomment the following to get a ton o' debug //$conf['debug'] = 6; // Get the PTR records that dont have a domain_id list($status, $rows, $ptrs) = db_get_records($onadb, 'dns', "domain_id = 0 and type like 'PTR'", ''); echo "Found {$rows} PTR records with no domain.\n"; if (!$rows) { echo " Everything looks OK!\n"; } foreach ($ptrs as $ptr) { list($status, $rows, $interface) = ona_get_interface_record(array('id' => $ptr['interface_id'])); // Print an error if it doesnt find an IP if (!$interface['ip_addr']) { echo "Possible orphan PTR record in dns table at ID: {$ptr['id']}. You should delete this record manually.\n"; continue; } $ipflip = ip_mangle($interface['ip_addr'], 'flip'); $octets = explode(".", $ipflip); // Find a pointer domain for this record to associate with. list($status, $rows, $ptrdomain) = ona_find_domain($ipflip . ".in-addr.arpa", 0); // CRAPPY security cludge $_SESSION['ona']['auth']['user']['username'] = '******'; $_SESSION['ona']['auth']['perms']['advanced'] = 'Y'; $_SESSION['ona']['auth']['perms']['host_modify'] = 'Y'; if (!$ptrdomain['id']) { echo " {$interface['ip_addr_text']}: Unable to find a pointer domain for this IP! Creating the following DNS domain: {$octets[3]}.in-addr.arpa\n";
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 dns_record_del($options = "") { global $conf, $self, $onadb; printmsg("DEBUG => dns_record_del({$options}) called", 3); // Version - UPDATE on every edit! $version = '1.03'; // Parse incoming options string to an array $options = parse_options($options); // Sanitize options[commit] (default is no) $options['commit'] = sanitize_YN($options['commit'], 'N'); // Return the usage summary if we need to if ($options['help'] or !$options['name']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM dns_record_del-v{$version} Deletes a DNS record from the database Synopsis: dns_record_del [KEY=VALUE] ... Required: name=NAME[.DOMAIN] or ID hostname or ID of the record to delete type=TYPE record type (A,CNAME,PTR...) Optional: ip=ADDRESS ip address (numeric or dotted) commit=[yes|no] commit db transaction (no) EOM ); } /* thoughts on the flow of things: A records: remove any CNAMES using this A record remove any PTR records using this A record test that it is not a primary_dns_id, if it is, it must be reassigned should make a find_dns_record(s) function. a find by host option would be good. need to do a better delete of DNS records when deleting a host.. currently its a problem. MP: TODO: this delete will not handle DNS views unless you use the ID of the record to delete. add a view option at some point. */ // If the name we were passed has a leading . in it then remove the dot. $options['name'] = preg_replace("/^\\./", '', $options['name']); // FIXME: MP Fix this to use a find_dns_record function ID only for now // Find the DNS record from $options['name'] list($status, $rows, $dns) = ona_find_dns_record($options['name'], $options['type']); printmsg("DEBUG => dns_record_del() DNS record: {$options['name']}", 3); if (!$dns['id']) { printmsg("DEBUG => Unknown DNS record: {$options['name']} ({$options['type']})", 3); $self['error'] = "ERROR => Unknown DNS record: {$options['name']} ({$options['type']})"; return array(2, $self['error'] . "\n"); } // Check permissions if (!auth('host_del') or !authlvl($host['LVL'])) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } // If "commit" is yes, delete the host if ($options['commit'] == 'Y') { $text = ""; $add_to_error = ""; // SUMMARY: // Display any associated PTR records for an A record // Display any associated CNAMEs for an A record // Test if it is used as a primary_dns_id unless it is the host_del module calling if (!isset($options['delete_by_module'])) { list($status, $rows, $srecord) = db_get_record($onadb, 'hosts', array('primary_dns_id' => $dns['id'])); if ($rows) { $self['error'] = "ERROR => dns_record_del() The DNS record, {$dns['name']}.{$dns['domain_fqdn']}[{$dns['id']}], is a primary A record for a host! You can not delete it until you associate a new primary record, or delete the host."; printmsg($self['error'], 0); return array(5, $self['error'] . "\n"); } } // Delete related Points to records // get list for logging list($status, $rows, $records) = db_get_records($onadb, 'dns', array('dns_id' => $dns['id'])); // do the delete list($status, $rows) = db_delete_records($onadb, 'dns', array('dns_id' => $dns['id'])); if ($status) { $self['error'] = "ERROR => dns_record_del() Child record delete SQL Query failed: {$self['error']}"; printmsg($self['error'], 0); return array(5, $self['error'] . "\n"); } if ($rows) { // log deletions // FIXME: do better logging here printmsg("INFO => {$rows} child DNS record(s) DELETED from {$dns['fqdn']}", 0); $add_to_error .= "INFO => {$rows} child record(s) DELETED from {$dns['fqdn']}\n"; } // TRIGGER: flag the domains for rebuild foreach ($records as $record) { list($status, $rows) = db_update_record($onadb, 'dns_server_domains', array('domain_id' => $record['domain_id']), array('rebuild_flag' => 1)); if ($status) { $self['error'] = "ERROR => dns_record_del() Unable to update rebuild flags for domain.: {$self['error']}"; printmsg($self['error'], 0); return array(7, $self['error'] . "\n"); } } // Delete the DNS record list($status, $rows) = db_delete_records($onadb, 'dns', array('id' => $dns['id'])); if ($status) { $self['error'] = "ERROR => dns_record_del() DNS record delete SQL Query failed: {$self['error']}"; printmsg($self['error'], 0); return array(5, $add_to_error . $self['error'] . "\n"); } // TRIGGER: flag the current dnsrecords domain for rebuild list($status, $rows) = db_update_record($onadb, 'dns_server_domains', array('domain_id' => $dns['domain_id']), array('rebuild_flag' => 1)); if ($status) { $self['error'] = "ERROR => dns_record_del() Unable to update rebuild flags for domain.: {$self['error']}"; printmsg($self['error'], 0); return array(7, $self['error'] . "\n"); } // FIXME: if it is a NS or something display a proper FQDN message here // Display proper PTR information if ($dns['type'] == 'PTR') { list($status, $rows, $pointsto) = ona_get_dns_record(array('id' => $dns['dns_id']), ''); list($status, $rows, $ptrint) = ona_get_interface_record(array('id' => $dns['interface_id']), ''); $ipflip = ip_mangle($ptrint['ip_addr'], 'flip'); $octets = explode(".", $ipflip); if (count($octets) > 4) { $arpa = '.ip6.arpa'; $octcount = 31; } else { $arpa = '.in-addr.arpa'; $octcount = 3; } $dns['fqdn'] = "{$ipflip}{$arpa} -> {$pointsto['fqdn']}"; } // Return the success notice $self['error'] = "INFO => DNS {$dns['type']} record DELETED: {$dns['fqdn']}"; printmsg($self['error'], 0); return array(0, $add_to_error . $self['error'] . "\n"); } // // We are just displaying records that would have been deleted // // SUMMARY: // Display any associated PTR records for an A record // Display any associated CNAMEs for an A record // Otherwise just display the host record for the host we would have deleted $text = "Record(s) NOT DELETED (see \"commit\" option)\n" . "Displaying record(s) that would have been deleted:\n"; // Test if it is used as a primary_dns_id list($status, $rows, $srecord) = db_get_record($onadb, 'hosts', array('primary_dns_id' => $dns['id'])); if ($rows) { $text .= "\nWARNING! This DNS record is a primary A record for a host\n"; } // Display the complete dns record list($status, $tmp) = dns_record_display("name={$dns['id']}&verbose=N"); $text .= "\n" . $tmp; // Display associated Child records list($status, $rows, $records) = db_get_records($onadb, 'dns', array('dns_id' => $dns['id'])); if ($rows) { $text .= "\nASSOCIATED POINTS-TO RECORDS ({$rows}):\n"; } foreach ($records as $record) { if ($record['type'] == 'NS') { $record['name'] = ''; } // FIXME:I could fix this but I'm lazy if ($record['type'] == 'PTR') { $record['name'] = '??'; } list($status, $rows, $domain) = ona_get_domain_record(array('id' => $record['domain_id']), ''); $text .= " {$record['type']}: {$record['name']}.{$domain['fqdn']} -> {$dns['fqdn']}\n"; } return array(7, $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 get_interface_cluster_list_html($form) { global $conf, $self, $onadb; global $font_family, $color, $style, $images; $html = $js = ''; // Interface Record list($status, $introws, $interfaces) = db_get_records($onadb, 'interface_clusters', "interface_id = {$form['interface_id']}"); if ($introws == 0 or $status) { return array('', ''); } // Get primary host info list($status, $rows, $priint) = ona_get_interface_record(array('id' => $form['interface_id'])); list($status, $rows, $prihost) = ona_get_host_record(array('id' => $priint['host_id'])); $priip = ip_mangle($priint['ip_addr'], dotted); // add one for primary host $introws = $introws + 1; $style['label_box'] = <<<EOL font-weight: bold; padding: 2px 4px; text-align: center; border: solid 1px {$color['border']}; background-color: {$color['window_content_bg']}; EOL; $html .= <<<EOL <!-- INTERFACE CLUSTER INFORMATION --> <table cellspacing="0" border="0" cellpadding="0"> <!-- LABEL --> <tr> <td colspan=3 style="{$style['label_box']}">{$introws} hosts share IP:<br>{$priip}</td> </tr> <tr> <td align="left" class="padding" style="color: #FFFFFF;" nowrap="true"> <a title="View host. ID: {$prihost['id']}" style="color: #6CB3FF;" class="nav" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_host\\', \\'host_id=>{$prihost['id']}\\', \\'display\\')'); removeElement('{$form['id']}');" >{$prihost['fqdn']}</a> </td> <td align="left" class="padding" style="color: #FFFFFF;" nowrap="true">{$priint['name']}</td> </tr> EOL; $i = 0; foreach ($interfaces as $interface) { list($status, $rows, $host) = ona_get_host_record(array('id' => $interface['host_id'])); foreach (array_keys((array) $interface) as $key) { $interface[$key] = htmlentities($interface[$key], ENT_QUOTES, $conf['php_charset']); } foreach (array_keys((array) $host) as $key) { $host[$key] = htmlentities($host[$key], ENT_QUOTES, $conf['php_charset']); } // If there is no cluster name then use the name from the primary interface if (!$interface['name']) { $interface['name'] = $priint['name']; } $html .= <<<EOL <tr> <td align="left" class="padding" style="color: #FFFFFF;" nowrap="true"> <form id="quick_interface_share_del_form" onSubmit="return(false);"> <input type="hidden" name="ip" value="{$interface['interface_id']}"> <input type="hidden" name="host" value="{$host['id']}"> </form> <a title="View host. ID: {$host['id']}" style="color: #6CB3FF;" class="nav" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_host\\', \\'host_id=>{$host['id']}\\', \\'display\\')'); removeElement('{$form['id']}');" >{$host['fqdn']}</a> </td> <td align="left" class="padding" style="color: #FFFFFF;" nowrap="true">{$interface['name']}</td> <td align="left" class="padding" style="color: #FFFFFF;" nowrap="true"> <img src="{$images}/silk/delete.png" title="Remove interface share with {$host['fqdn']}" border="0" onClick="xajax_window_submit('tooltips', xajax.getFormValues('quick_interface_share_del_form'), 'interface_share_del');removeElement('{$form['id']}');" /> </td> </tr> EOL; // increment counter $i++; if ($i == 15) { $html .= <<<EOL <tr> <td align="center" class="padding" style="color: #FFFFFF;" nowrap="true" colspan=2> Only displaying first 15 hosts in cluster. <a title="View host. ID: {$interface['host_id']}" style="color: #6CB3FF;" class="nav" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_host\\', \\'host_id=>{$interface['host_id']}\\', \\'display\\')'); removeElement('{$form['id']}');" >View Primary Host</a> </td> </tr> EOL; break; } } $html .= <<<EOL </table> EOL; return array($html, $js); }
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_list($window_name, $form = '') { global $conf, $self, $onadb; global $images, $color, $style; $html = ''; $js = ''; // If the user supplied an array in a string, build the array and store it in $form $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 this is the display_host screen that called, add refresh for DNS records too if ($form['content_id'] == 'display_host_list_interfaces') { $refresh .= "xajax_window_submit('list_records', xajax.getFormValues('list_records_filter_form'), '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']; } // 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; // Start building the "where" clause for the sql query to find the interfaces to display $where = ""; $and = ""; // HOST ID if ($form['host_id']) { $where .= $and . "host_id = " . $onadb->qstr($form['host_id']) . " OR id in (select interface_id from interface_clusters where host_id = " . $onadb->qstr($form['host_id']) . ")"; $and = " AND "; } // Do the SQL Query $filter = ''; if ($form['filter']) { $form['filter'] = ip_mangle($form['filter']); $filter = $and . ' ip_addr LIKE ' . $onadb->qstr('%' . $form['filter'] . '%'); } list($status, $rows, $results) = db_get_records($onadb, 'interfaces', $where . $filter, "ip_addr ASC", $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); } // Re-Count only "internal" interfaces, not nat interfaces list($status, $rows, $records) = db_get_records($onadb, 'interfaces', 'nat_interface_id = \'0\' and ' . $where . $filter, "", 0); $count = $rows; $html .= <<<EOL <!-- Interface List --> <table id="{$form['form_id']}_interface_list" class="list-box" cellspacing="0" border="0" cellpadding="0"> <!-- Table Header --> <tr> <td colspan="2" class="list-header" align="center" style="{$style['borderR']};">Interface</td> <td class="list-header" align="center" style="{$style['borderR']};">Subnet</td> <td class="list-header" align="center" style="{$style['borderR']};">MAC</td> <td class="list-header" align="center" style="{$style['borderR']};">Name</td> <td class="list-header" align="center" style="{$style['borderR']};">Description</td> <td class="list-header" align="center" style="{$style['borderR']};">Last Response</td> <td class="list-header" align="center"> </td> </tr> EOL; // Loop and display each record foreach ($results as $record) { // Get additional info about each host record // // Check if this interface has an external NAT unset($extnatint, $extnatdisplay, $extnatdisplay, $extnatsubdisplay); if ($record['nat_interface_id'] > 0) { list($status, $rows, $extnatint) = ona_get_interface_record(array('id' => $record['nat_interface_id'])); // GDO: get the subnet object of the NATing interface, display it in both Interface and Subnet columns list($status, $rows, $extnatintsub) = ona_get_subnet_record(array('id' => $extnatint['subnet_id'])); $extnatint['ip_addr'] = ip_mangle($extnatint['ip_addr'], 'dotted'); //$extnatdisplay = "<span title='Interface is NATed to {$extnatint['ip_addr']}'> => {$extnatint['ip_addr']}</span>"; $extnatdisplay = "<span title='Interface is NATed to {$extnatint['ip_addr']} (on {$extnatintsub['name']})'> => {$extnatint['ip_addr']}</span>"; $extnatsubdisplay = " => <a title=\"View NATed subnet. ID: {$extnatintsub['id']}\"\n class=\"nav\"\n onClick=\"xajax_window_submit('work_space', 'xajax_window_submit(\\'display_subnet\\', \\'subnet_id=>{$extnatintsub['id']}\\', \\'display\\')');\"\n >{$extnatintsub['name']}</a>"; } // Check if this interface is an external NAT for another interface list($isnatstatus, $isnatrows, $isnat) = db_get_records($onadb, 'interfaces', "nat_interface_id = {$record['id']}", '', 0); // If the current interface is external NAT for another, dont display it in the list. if ($isnatrows > 0) { continue; } list($status, $intclusterrows, $intcluster) = db_get_records($onadb, 'interface_clusters', "interface_id = {$record['id']}"); // Grab some info from the associated subnet record list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $record['subnet_id'])); $record['ip_mask'] = $subnet['ip_mask']; $record['subnet_id'] = $subnet['id']; $record['subnet_description'] = $subnet['name']; // Convert IP and Netmask to a presentable format $record['ip_addr'] = ip_mangle($record['ip_addr'], 'dotted'); $record['ip_mask'] = ip_mangle($record['ip_mask'], 'dotted'); $record['ip_mask_cidr'] = ip_mangle($record['ip_mask'], 'cidr'); if ($record['mac_addr']) { $record['mac_addr'] = mac_mangle($record['mac_addr']); } $record['description_short'] = truncate($record['description'], 40); // Escape data for display in html foreach (array_keys($record) as $key) { $record[$key] = htmlentities($record[$key], ENT_QUOTES, $conf['php_charset']); } // Format the date and colorize if its older than 2 months if ($record['last_response']) { $record['last_response_fmt'] = date($conf['date_format'], strtotime($record['last_response'])); if (strtotime($record['last_response']) < strtotime('-2 month')) { $record['last_response_fmt'] = "<span style=\"color: red;\">" . $record['last_response_fmt'] . "</style>"; } } $html .= <<<EOL <tr onMouseOver="this.className='row-highlight'" onMouseOut="this.className='row-normal'"> <td nowrap="true" class="list-row" style="padding: 0px;" width="16px"> EOL; // Display cluster related information $clusterhtml = ' '; $clusterstyle = ''; $clusterscript = ''; if ($intclusterrows > 0) { $clusterstyle = 'font-weight: bold'; $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=>{$record['id']}\\');'\n );\""; $clusterhtml .= <<<EOL <img src="{$images}/silk/sitemap.png" border="0" {$clusterscript} /> EOL; } $html .= $clusterhtml; $html .= <<<EOL </td> <td class="list-row"> EOL; // MP: Disabling the display_interface link. I dont think this will be needed if (1 < 0) { $html .= <<<EOL <a title="View interface. ID: {$record['id']}" class="nav" onClick="xajax_window_submit('work_space', 'xajax_window_submit(\\'display_interface\\',\\'interface_id=>{$record['id']}\\', \\'display\\')');"> {$record['ip_addr']} </a> EOL; } else { $html .= "<span style='{$clusterstyle}' {$clusterscript}>{$record['ip_addr']}</span>"; } $html .= <<<EOL <span style="{$clusterstyle}" title="{$record['ip_mask']}">/{$record['ip_mask_cidr']}</span> {$extnatdisplay} </td> <td class="list-row" align="left"> <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_description']}</a> {$extnatsubdisplay} </td> <td class="list-row" align="right"> {$record['mac_addr']} </td> <td class="list-row" align="left"> {$record['name']} </td> <td class="list-row" align="left" title="{$record['description']}"> {$record['description_short']} </td> <td class="list-row" align="left" title="{$record['last_response']}"> {$record['last_response_fmt']} </td> <td class="list-row" align="right"> <form id="{$form['form_id']}_list_interface_{$record['id']}" ><input type="hidden" name="interface_id" value="{$record['id']}" ><input type="hidden" name="js" value="{$refresh}" ></form> EOL; if (auth('interface_modify')) { $html .= <<<EOL <a title="Interface Menu" id="int_menu_button_{$record['id']}" class="act" onmouseover="wwTT(this, event, 'id', 'tt_quick_interface_menu_{$record['id']}', 'type', 'velcro', 'delay', 0, 'styleClass', 'wwTT_int_menu', 'lifetime', 1000, 'direction', 'west', 'javascript', 'xajax_window_submit(\\'tooltips\\', \\'tooltip=>quick_interface_menu,id=>tt_quick_interface_menu_{$record['id']},interface_id=>{$record['id']},ip_addr=>{$record['ip_addr']},orig_host=>{$record['host_id']},form_id=>{$form['form_id']}_list_interface_{$record['id']},subnet_id=>{$subnet['id']},natip=>{$record['nat_interface_id']}\\');' );" ><img src="{$images}/silk/add.png" border="0"></a> EOL; } if (auth('interface_modify')) { $html .= <<<EOL <a title="Edit interface. ID: {$record['id']}" class="act" onClick="xajax_window_submit('edit_interface', xajax.getFormValues('{$form['form_id']}_list_interface_{$record['id']}'), 'editor');" ><img src="{$images}/silk/page_edit.png" border="0"></a> EOL; } if (auth('interface_del')) { $html .= <<<EOL <a title="Delete interface" class="act" onClick="xajax_window_submit('edit_interface', xajax.getFormValues('{$form['form_id']}_list_interface_{$record['id']}'), 'delete');" ><img src="{$images}/silk/delete.png" border="0"></a> EOL; } $html .= <<<EOL </td> </tr> EOL; } if ($count == 0 and $form['host_id'] and !$form['filter']) { $html .= <<<EOL <tr><td colspan="99" align="center" style="color: red;">Please add an interface to this host, or delete the host</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']); // 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_move($options = "") { global $conf, $self, $onadb; printmsg("DEBUG => interface_move({$options}) called", 3); // Version - UPDATE on every edit! $version = '1.04'; // Parse incoming options string to an array $options = parse_options($options); // Return the usage summary if we need to if ($options['help'] or !$options['start'] or !$options['new_start']) { // 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_move-v{$version} Moves all interface addresses from one subnet to another. The initial range of IPs does not have to be consecutive. The new range of IPs will be used sequentially. Synopsis: interface_move [KEY=VALUE] ... IP block to move: (source) start=IP first IP to move [end=IP] last IP to move New IP block: (destination) new_start=IP first new IP address [new_end=IP] last new IP address Optional: commit=[yes|no] commit db transaction (no) EOM ); } // Set options[force] and options[create_a] to N if it's not set $options['commit'] = sanitize_YN($options['commit'], 'N'); // Find the "start" subnet record by IP address list($status, $rows, $old_subnet) = ona_find_subnet($options['start']); if (!$old_subnet or !$old_subnet['id']) { printmsg("DEBUG => Source start address ({$options['start']}) isn't valid!", 3); $self['error'] = "ERROR => Source (start) address specified isn't valid!"; return array(2, $self['error'] . "\n"); } // If they specified an "END" address, make sure it's valid and on the same subnet if ($options['end']) { // Find an interface record by something in that interface's record list($status, $rows, $old_subnet_end) = ona_find_subnet($options['end']); // If we didn't get a record then exit if (!$old_subnet_end or !$old_subnet_end['id']) { printmsg("DEBUG => Source end address ({$options['end']}) isn't valid!", 3); $self['error'] = "ERROR => Source (end) address specified isn't valid!"; return array(3, $self['error'] . "\n"); } if ($old_subnet_end['id'] != $old_subnet['id']) { printmsg("DEBUG => Both source addresses ({$options['start']} and {$options['end']}) must be on the same subnet!", 3); $self['error'] = "ERROR => Both the source addresses (start and end) must be on the same subnet!"; return array(4, $self['error'] . "\n"); } } else { printmsg("DEBUG => Only moving one host source={$options['start']}!", 3); $options['end'] = $options['start']; } // Find the "end" subnet record by IP address list($status, $rows, $new_subnet) = ona_find_subnet($options['new_start']); // If we didn't get a record then exit if (!$new_subnet or !$new_subnet['id']) { printmsg("DEBUG => Destination start address ({$options['new_start']}) isn't valid!", 3); $self['error'] = "ERROR => Destination (new_start) address specified isn't valid!"; return array(2, $self['error'] . "\n"); } // Make sure the "old" and "new" subnets are different subnets if ($old_subnet['id'] == $new_subnet['id']) { printmsg("DEBUG => Both the source IP range ({$options['start']}+) and the destination IP range ({$options['new_start']}+) are on the same subnet!", 3); $self['error'] = "ERROR => Both the source IP range and the destination IP range are on the same subnet!"; return array(2, $self['error'] . "\n"); } // If they specified a "new_end" address, make sure it's valid and on the same subnet as the new_start subnet if ($options['new_end']) { // Find an interface record by something in that interface's record list($status, $rows, $new_subnet_end) = ona_find_subnet($options['new_end']); // If we didn't get a record then exit if (!$new_subnet_end or !$new_subnet_end['id']) { printmsg("DEBUG => Destination end address ({$options['new_end']}) isn't valid!", 3); $self['error'] = "ERROR => Destination (new_end) address specified isn't valid!"; return array(3, $self['error'] . "\n"); } if ($new_subnet_end['id'] != $new_subnet['id']) { printmsg("DEBUG => Both destination addresses ({$options['new_start']} and {$options['new_end']}) must be on the same subnet!", 3); $self['error'] = "ERROR => Both the destination addresses (new_start and new_end) must be on the same subnet!"; return array(4, $self['error'] . "\n"); } } else { printmsg("DEBUG => Only moving one host destination={$options['new_start']}!", 3); $options['new_end'] = $options['new_start']; } // Check permissions at the subnet level if (!auth('interface_modify')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(13, $self['error'] . "\n"); } // An array for all the interface records we'll be moving $to_move = array(); // Message to display if we succeed $message = ""; // Load all the interface records we'll be moving $i = 0; do { // FIXME: this should do a more advanced query someday! (like checking that the ipaddress is >= start and <= end list($status, $rows, $interface) = ona_get_interface_record(array('subnet_id' => $old_subnet['id']), 'ip_addr'); if ($rows == 0) { break; } $i++; if ($interface['ip_addr'] >= ip_mangle($options['start'], 'numeric')) { if ($interface['ip_addr'] <= ip_mangle($options['end'], 'numeric')) { $to_move[$i] = $interface; } } } while ($i < $rows); $total_to_move = count($to_move); $total_assigned = 0; // If there's nothing to do, tell them if ($total_to_move == 0) { printmsg("DEBUG => There are no interfaces in the source address block!", 3); $self['error'] = "ERROR => There are no interfaces in the source address block!"; return array(6, $self['error'] . "\n"); } // Make sure we have a high enough "LVL" to modify the associated hosts foreach ($to_move as $interface) { // Load the associated host record list($status, $rows, $host) = ona_get_host_record(array('id' => $interface['host_id'])); list($status, $rows, $dns) = ona_get_dns_record(array('id' => $host['primary_dns_id'], 'type' => 'A')); // Check permissions at the subnet level if (!authlvl($host['LVL'])) { $self['error'] = "Permission denied! Can't modify Host: {$host['id']} {$dns['fqdn']}"; printmsg($self['error'], 0); return array(14, $self['error'] . "\n"); } // Check to see if the host has any interfaces in the destination subnet // MP: this is now allowed // list($status, $rows, $interface) = ona_get_interface_record(array('host_id' => $interface['host_id'], 'subnet_id' => $new_subnet['id'])); // if ($status or $rows) { // printmsg("DEBUG => Source host {$ddns['fqdn']} already has an interface on the destination subnet!",3); // $self['error'] = "ERROR => Source host {$dns['fqdn']} (ID {$host['id']}) already has an interface on the destination subnet!"; // return(array(15, $self['error'] . "\n")); // } } // Get the numeric version of the start/end addresses we are moving interfaces to // .. and make sure that the $low_ip and $high_ip are not subnet or broadcast addresses! $low_ip = ip_mangle($options['new_start'], 'numeric'); $high_ip = ip_mangle($options['new_end'], 'numeric'); if ($low_ip == $new_subnet['ip_addr']) { $low_ip++; } $num_hosts = 0xffffffff - $new_subnet['ip_mask']; if ($high_ip == $new_subnet['ip_addr'] + $num_hosts) { $high_ip--; } printmsg("INFO => Asked to move {$total_to_move} interfaces to new range: " . ip_mangle($low_ip, 'dotted') . ' - ' . ip_mangle($high_ip, 'dotted'), 0); // Loop through each interface we need to move, and find an available address for it. $pool_interfering = 0; foreach (array_keys($to_move) as $i) { while ($low_ip <= $high_ip) { list($status, $rows, $interface) = ona_get_interface_record(array('ip_addr' => $low_ip)); if ($rows == 0 and $status == 0) { // Since the IP seems available, let's double check and make sure it's not in a DHCP address pool list($status, $rows, $pool) = ona_get_dhcp_pool_record("ip_addr_start < '{$low_ip}' AND ip_addr_end > '{$low_ip}'"); if ($rows == 0 and $status == 0) { // The IP is available, lets use it! $to_move[$i]['new_ip_address'] = $low_ip; $total_assigned++; $low_ip++; break; } $pool_interfering = 1; printmsg("DEBUG => Couldn't use the DHCP POOL address: " . ip_mangle($low_ip, 'dotted'), 3); } $low_ip++; } } // If total_assigned != total_to_move, error - not enough free IP addresses in destination subnet! if ($total_assigned != $total_to_move) { printmsg("DEBUG => The destination IP range doesn't have enough free IP addresses!", 3); $self['error'] = "ERROR => The destination IP range doesn't have enough free IP addresses!\n"; if ($pool_interfering) { $self['error'] .= "INFO => Some IPs in the destination range were part of a DHCP pool range.\n"; } return array(6, $self['error']); } // Display what we would have done if "commit" isn't "yes" if ($options['commit'] != "Y") { $self['error'] = "Interface(s) NOT MOVED (see \"commit\" option)"; $text = $self['error'] . "\n" . "Displaying {$total_to_move} interface(s) that would have been moved:\n\n"; foreach ($to_move as $interface) { // Get display the hostname we would have moved, as well as it's IP address. list($status, $rows, $host) = ona_get_host_record(array('id' => $interface['host_id'])); list($status, $rows, $dns) = ona_get_dns_record(array('id' => $host['primary_dns_id'], 'type' => 'A')); $hostname = strtolower("{$dns['fqdn']}"); $text .= " " . ip_mangle($interface['ip_addr'], 'dotted') . " -> " . ip_mangle($interface['new_ip_address'], 'dotted') . "\t({$hostname})\n"; } $text .= "\n"; return array(7, $text); } // Loop through and update each interface's IP_ADDRESS and SUBNET_ID $text = "SUCCESS => {$total_to_move} interface(s) moved\n"; $text .= "Interface(s) moved:\n\n"; foreach ($to_move as $interface) { list($status, $rows) = ona_update_record("interfaces", array('id' => $interface['id']), array('ip_addr' => $interface['new_ip_address'], 'subnet_id' => $new_subnet['id'])); if ($status != 0 or $rows != 1) { $self['error'] = "ERROR => Database update failed! {$self['error']}"; return array(8, $self['error'] . "\n"); } // Get display the hostname we would have moved, as well as its IP address. list($status, $rows, $host) = ona_get_host_record(array('id' => $interface['host_id'])); list($status, $rows, $dns) = ona_get_dns_record(array('id' => $host['primary_dns_id'], 'type' => 'A')); $hostname = strtolower("{$dns['fqdn']}"); $text .= " " . ip_mangle($interface['ip_addr'], 'dotted') . " -> " . ip_mangle($interface['new_ip_address'], 'dotted') . "\t({$hostname})\n"; } // Return the success notice return array(0, $text); }