function ona_find_custom_attribute($search = "") { global $self; // Validate input if ($search == "") { return array(1, 0, array()); } // If it's numeric, search by record ID if (is_numeric($search)) { $field = 'id'; list($status, $rows, $record) = ona_get_custom_attribute_record(array($field => $search)); // If we got it, return it if ($status == 0 and $rows == 1) { printmsg("DEBUG => ona_find_custom_attribute() found custom attribute record by {$field}", 2); return array(0, $rows, $record); } } // Split the description based on the () enclosed type list($ca_type, $ca_value) = preg_split("/\\(|\\)/", $search); printmsg("DEBUG => ona_find_custom_attribute(): Split is {$ca_type},{$ca_value}", 3); // It's a string - do several sql queries and see if we can get a unique match list($status, $rows, $type) = ona_get_custom_attribute_type_record(array('name' => trim($ca_type))); printmsg("DEBUG => ona_find_custom_attribute(): Found {$rows} custom attribute type record", 3); // Find the ID using the type id and value list($status, $rows, $record) = ona_get_custom_attribute_record(array('value' => $ca_value, 'custom_attribute_type_id' => $type['id'])); // If we got it, return it if ($status == 0 and $rows == 1) { printmsg("DEBUG => ona_find_custom_attribute(): Found custom attribute record by its full name", 2); return array(0, $rows, $record); } // We didn't find it - return and error code, 0 matches, and an empty record. $self['error'] = "NOTICE => couldn't find a unique custom attribute record with specified search criteria"; printmsg($self['error'], 2); return array(2, 0, array()); }
function custom_attribute_display($options = "") { // The important globals global $conf, $self, $onadb; $text_array = array(); // Version - UPDATE on every edit! $version = '1.02'; printmsg("DEBUG => custom_attribute_display({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Return the usage summary if we need to if ($options['help'] or !$options['host'] and !$options['id'] and !$options['subnet'] and !$options['vlan']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM custom_attribute_display-v{$version} Display the custom attribute specified or attributes for a host Synopsis: custom_attribute_display Where: id=ID custom attribute ID OR host=ID or NAME[.DOMAIN] display custom attributes for specified host OR subnet=ID or NAME display custom attributes for specified subnet OR vlan=NAME display custom attributes for specified VLAN Optional: type=ID or NAME If you specify a type and a host or subnet you will only get back a 1 or a 0 indicating that that type is set or not set for the host or subnet EOM ); } // if a type was set, check if it is associated with the host or subnet and return 1 or 0 if ($options['type']) { $field = is_numeric($options['type']) ? 'id' : 'name'; list($status, $rows, $catype) = ona_get_custom_attribute_type_record(array($field => $options['type'])); // error if we cant find the type specified if (!$catype['id']) { $self['error'] = "ERROR => The custom attribute type specified, {$options['type']}, does not exist!"; return array(5, $self['error']); } $where['custom_attribute_type_id'] = $catype['id']; } // Search for the host first if ($options['host']) { list($status, $rows, $host) = ona_find_host($options['host']); // Error if the host doesn't exist if (!$host['id']) { $self['error'] = "ERROR => The host specified, {$options['host']}, does not exist!"; return array(2, $self['error']); } else { $where['table_id_ref'] = $host['id']; $where['table_name_ref'] = 'hosts'; list($status, $rows, $cas) = db_get_records($onadb, 'custom_attributes', $where); } $anchor = 'host'; $desc = $host['fqdn']; } // Search for subnet if ($options['subnet']) { list($status, $rows, $subnet) = ona_find_subnet($options['subnet']); // Error if the record doesn't exist if (!$subnet['id']) { $self['error'] = "ERROR => The subnet specified, {$options['subnet']}, does not exist!"; return array(3, $self['error']); } else { $where['table_id_ref'] = $subnet['id']; $where['table_name_ref'] = 'subnets'; list($status, $rows, $cas) = db_get_records($onadb, 'custom_attributes', $where); } $anchor = 'subnet'; $desc = $subnet['description']; } // Search for vlan if ($options['vlan']) { list($status, $rows, $vlan) = ona_find_vlan($options['vlan']); // Error if the record doesn't exist if (!$vlan['id']) { $self['error'] = "ERROR => The VLAN specified, {$options['vlan']}, does not exist!"; return array(3, $self['error']); } else { $where['table_id_ref'] = $vlan['id']; $where['table_name_ref'] = 'vlans'; list($status, $rows, $cas) = db_get_records($onadb, 'custom_attributes', $where); } $anchor = 'vlan'; $desc = $vlan['description']; } // Now find the ID of the record, returns a specific record only if ($options['id']) { list($status, $rows, $ca) = ona_get_custom_attribute_record(array('id' => $options['id'])); if (!$ca['id']) { $self['error'] = "ERROR => The custom attribute specified, {$options['id']}, is invalid!"; return array(4, $self['error']); } $text_array = $ca; $text .= "CUSTOM ATTRIBUTE ENTRY RECORD ({$ca['id']})\n"; $text .= format_array($ca); } elseif ($options['type']) { // If we requested type, now is the time to return a response if it is found associated. if ($cas[0]) { $text .= '1'; $text_array['has_attribute'] = 'Y'; } else { $text .= '0'; $text_array['has_attribute'] = 'N'; } } else { // Build text to return $text .= strtoupper($anchor) . " CUSTOM ATTRIBUTE RECORDS ({$desc})\n"; // Display the record(s) $i = 0; do { $text .= "\nASSOCIATED CUSTOM ATTRIBUTE ENTRY RECORD ({$i} of {$rows})\n"; $text .= format_array($cas[$i]); list($status, $carows, $ca) = ona_get_custom_attribute_type_record(array('id' => $cas[$i]['custom_attribute_type_id'])); $text_array[$ca['name']] = $cas[$i]['value']; $i++; } while ($i < $rows); } // 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 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_save($window_name, $form = '') { global $conf, $self, $onadb; // Check permissions if (!auth('advanced')) { $response = new xajaxResponse(); $response->addScript("alert('Permission denied!');"); return $response->getXML(); } // Instantiate the xajaxResponse object $response = new xajaxResponse(); $js = ''; // Strip whitespace // FIXME: (PK) What about SQL injection attacks? This is a user-entered string... $form['cust_attrib_type_name'] = trim($form['cust_attrib_type_name']); // Don't insert a string of all white space! if ($form['cust_attrib_type_name'] == "") { $self['error'] = "ERROR => Blank names not allowed."; printmsg($self['error'], 0); $response->addScript("alert('{$self['error']}');"); return $response->getXML(); } // If you get a numeric in $form, update the record if (is_numeric($form['id'])) { // Get the manufacturer record before updating (logging) list($status, $rows, $original_manufacturer) = ona_get_custom_attribute_type_record(array('id' => $form['id'])); if ($form['cust_attrib_type_name'] !== $original_type['name']) { list($status, $rows) = db_update_record($onadb, 'custom_attribute_types', array('id' => $form['id']), array('name' => $form['cust_attrib_type_name'], 'field_validation_rule' => $form['field_validation_rule'], 'failed_rule_text' => $form['failed_rule_text'], 'notes' => $form['notes'])); if ($status or !$rows) { $self['error'] = "ERROR => cust_attrib_type edit update ws_save() failed: " . $self['error']; printmsg($self['error'], 0); $response->addScript("alert('{$self['error']}');"); } else { // Get the manufacturer record after updating (logging) list($status, $rows, $new_type) = ona_get_custom_attribute_type_record(array('id' => $form['id'])); // Return the success notice $self['error'] = "INFO => Custom Attribute Type UPDATED:{$new_type['id']}: {$new_type['name']}"; printmsg($self['error'], 0); $log_msg = "INFO => Custom Attribute Type UPDATED:{$new_type['id']}: name[{$original_type['name']}=>{$new_type['name']}]"; printmsg($log_msg, 0); } } } else { $id = ona_get_next_id('custom_attribute_types'); if (!$id) { $self['error'] = "ERROR => The ona_get_next_id('custom_attribute_types') call failed!"; printmsg($self['error'], 0); } else { list($status, $rows) = db_insert_record($onadb, "custom_attribute_types", array('id' => $id, 'name' => $form['cust_attrib_type_name'], 'field_validation_rule' => $form['field_validation_rule'], 'failed_rule_text' => $form['failed_rule_text'], 'notes' => $form['notes'])); if ($status or !$rows) { $self['error'] = "ERROR => Custom attribute type add ws_save() failed: " . $self['error']; printmsg($self['error'], 0); } else { $self['error'] = "INFO => Custom Attribute Type ADDED: {$form['cust_attrib_type_name']} "; printmsg($self['error'], 0); } } } // If the module returned an error code display a popup warning if ($status or !$rows) { $js .= "alert(\"Save failed. " . trim($self['error']) . " (Hint: Does the name you're trying to insert already exist?)\");"; } else { $js .= "removeElement('{$window_name}');"; $js .= "xajax_window_submit('app_custom_attribute_type_list', xajax.getFormValues('app_custom_attribute_type_list_filter_form'), 'display_list');"; } // Return some javascript to the browser $response->addScript($js); return $response->getXML(); }