function block_display($options = "") { global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.00'; printmsg("DEBUG => block_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['block']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM block_display-v{$version} Displays a block record from the database Synopsis: block_display [KEY=VALUE] ... Required: block=NAME or ID Block name or ID of the block display Optional: verbose=[yes|no] Display additional info (DEFAULT: yes) EOM ); } // The formatting rule on block names is all upper and trim it $options['block'] = trim($options['block']); $options['block'] = preg_replace('/\\s+/', '-', $options['block']); $options['block'] = strtoupper($options['block']); // If the block provided is numeric, check to see if it's an block if (is_numeric($options['block'])) { // See if it's an block_id list($status, $rows, $block) = ona_get_block_record(array('id' => $options['block'])); if (!$block['id']) { printmsg("DEBUG => Unable to find block using the ID {$options['block']}!", 3); $self['error'] = "ERROR => Unable to find block using the ID {$options['block']}!"; return array(2, $self['error'] . "\n"); } } else { list($status, $rows, $block) = ona_get_block_record(array('name' => $options['block'])); if (!$block['id']) { $self['error'] = "ERROR => Unable to find block using the name {$options['block']}!"; printmsg("DEBUG => Unable to find block using the name {$options['block']}!", 3); return array(2, $self['error'] . "\n"); } } printmsg("DEBUG => Found block: {$block['name']}", 3); // Build text to return $text = "BLOCK RECORD\n"; $text .= format_array($block); // If 'verbose' is enabled, grab some additional info to display if ($options['verbose'] == 'Y') { $where .= " ip_addr >= " . $block['ip_addr_start'] . " AND ip_addr <= " . $block['ip_addr_end']; list($status, $netrows, $nets) = db_get_records($onadb, 'subnets', $where, "ip_addr"); // subnet record(s) $i = 0; foreach ($nets as $record) { list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $record['id'])); if ($rows == 0) { break; } $i++; $text .= "\nASSOCIATED SUBNET RECORD ({$i} of {$netrows})\n"; $text .= format_array($subnet); } } // Return the success notice return array(0, $text); }
function dns_record_display($options = "") { global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.00'; printmsg("DEBUG => dns_record_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['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_display-v{$version} Displays a DNS record from the database Synopsis: dns_record_display [KEY=VALUE] ... Required: name=NAME[.DOMAIN] or ID hostname or ID of the dns record to display Optional: verbose=[yes|no] display additional info (yes) EOM ); } // If the name we were passed has a leading . in it then remove the dot. $options['name'] = preg_replace("/^\\./", '', $options['name']); // Find the DNS record from $options['name'] list($status, $rows, $record) = ona_find_dns_record($options['name']); printmsg("DEBUG => dns_record_del() DNS record: {$record['name']}", 3); if (!$record['id']) { printmsg("DEBUG => Unknown DNS record: {$options['name']}", 3); $self['error'] = "ERROR => Unknown DNS record: {$options['name']}"; return array(2, $self['error'] . "\n"); } // Build text to return $text = "DNS {$record['type']} RECORD ({$record['fqdn']})\n"; $text .= format_array($record); // If 'verbose' is enabled, grab some additional info to display if ($options['verbose'] == 'Y') { // PTR record(s) $i = 0; do { list($status, $rows, $ptr) = ona_get_dns_record(array('dns_id' => $record['id'], 'type' => 'PTR')); if ($rows == 0) { break; } $i++; $text .= "\nASSOCIATED PTR RECORD ({$i} of {$rows})\n"; $text .= format_array($ptr); } while ($i < $rows); // CNAME record(s) $i = 0; do { list($status, $rows, $cname) = ona_get_dns_record(array('dns_id' => $record['id'], 'type' => 'CNAME')); if ($rows == 0) { break; } $i++; $text .= "\nASSOCIATED CNAME RECORD ({$i} of {$rows})\n"; $text .= format_array($cname); } while ($i < $rows); // FIXME: MP display other types of records like NS,MX,SRV etc etc, also support dns views better } // Return the success notice return array(0, $text); }
function config_display($options = "") { // The important globals global $conf; global $self; global $onadb; // Version - UPDATE on every edit! $version = '1.02'; printmsg('DEBUG => config_display(' . $options . ') called', 3); // Parse incoming options string to an array $options = parse_options($options); // Set "options[verbose] to yes if it's not set if (!array_key_exists('verbose', $options)) { $options['verbose'] = 'Y'; } else { $options['verbose'] = sanitize_YN($options['verbose']); } // Return the usage summary if we need to if ($options['help'] or !$options['config'] and (!$options['host'] or !$options['type'])) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console return array(1, <<<EOM config_display-v{$version} Displays a config text record from the database Synopsis: config_display [KEY=VALUE] ... Required: config=ID display config by record ID - or - host=ID or NAME[.DOMAIN] display most recent config for specified host type=TYPE type of config to display - usually "IOS_VERSION" or "IOS_CONFIG" Optional: verbose=[yes|no] display entire record (yes) "no" displays only the actual config text EOM ); } // Get a config record if there is one $self['error'] = ""; list($status, $rows, $config) = ona_find_config($options); // Error if an error was returned if ($status or !$config['id']) { $text = ""; if ($self['error']) { $text = $self['error'] . "\n"; } $text .= "ERROR => No config text entries found!\n"; return array(2, $text); } // If 'verbose' is enabled, we display the entire record if ($options['verbose'] == 'Y') { // Build text to return $text = "CONFIG TEXT RECORD (1 of {$rows})\n"; $text .= format_array($config); } else { $text = $config['config_body']; } // Return the success notice return array(0, $text); }
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); }
/** * 修改订单的运输方式(批量订单号) 在unshiped表中 * @param array $ids 订单号数组 * @param int $transportId 运输方式ID * @return bool * @author zqt */ public function updateOrderShipInfoById($ids, $transportId) { $ids = array_filter(array_map('intval', format_array($ids))); $transportId = intval($transportId); return $this->sql("UPDATE " . C('DB_PREFIX') . "unshipped_order SET transportId='{$transportId}' WHERE id IN (" . implode(',', $ids) . ")")->update(); }
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); }
/** * Get the modified number * @param $route Array of params representing the modificatios resulted usually from an sql query * @param $nr Number before rewriting * @return Number resulted after the modifications were applied. If resulted number is empty then original number is returned * Note!! The order in with the operations are performed is : cut, replace, add. So replacing will be performed on the resulted number after cutting. One must keep this in mind when using multiple transformations. */ function rewrite_digits($route, $nr) { $result = $nr; if ($route["nr_of_digits_to_cut"] && $route["position_to_start_cutting"]) { $result = substr($nr, 0, $route["position_to_start_cutting"] - 1) . substr($nr, $route["position_to_start_cutting"] - 1 + $route["nr_of_digits_to_cut"], strlen($nr)); } if ($route["position_to_start_replacing"] && $route["digits_to_replace_with"]) { if (!$route["nr_of_digits_to_replace"]) { return $route["digits_to_replace_with"]; } $result = substr($result, 0, $route["position_to_start_replacing"] - 1) . $route["digits_to_replace_with"] . substr($result, $route["position_to_start_replacing"] + $route["nr_of_digits_to_replace"] - 1, strlen($result)); } if ($route["position_to_start_adding"] && $route["digits_to_add"]) { $result = substr($result, 0, $route["position_to_start_adding"] - 1) . $route["digits_to_add"] . substr($result, $route["position_to_start_adding"] - 1, strlen($result)); } if (!$result) { debug("Wrong: resulted number is empty when nr='{$nr}' and route=" . format_array($route)); return $nr; } return $result; }
function testIsAnagram() { $word = func_get_args()[0]; $expected = func_get_args()[1]; for ($i = 2; $i < count(func_get_args()); $i++) { $to_test[] = func_get_args()[$i]; } $wp = new wordProcessor($word); if (count($to_test) == 1) { $to_test = $to_test[0]; } $status = $wp->isAnagram($to_test); $success = $status == $expected; $tested = is_array($to_test) ? format_array($to_test) : $to_test; update_test("isAnagram()", "(" . $tested . ") becomes (" . $word . ")", tf($status), tf($expected), $success); }
/** * 修改订单状态 未完成 * @return boolean * @author czq 2014年6月17日 */ public function updateOrderStatus() { $_POST; if (isset($condition['id'])) { $ids = format_array($condition['id']); $fromstatus = M('Order')->getOrderStatusById($ids); //是否有移动到对应分类权限验证 待开发 if (false) { return false; } return M('OrderManage')->updateOrderStatusById($condition['id'], $tostatus); } if (isset($condition['status'])) { //是否有移动到对应分类权限验证 if (false) { return false; } return M('OrderManage')->updateOrderStatusByStatus($condition['status'], $tostatus); } self::$errMsg[10029] = get_promptmsg(10029, json_encode($condition)); return false; }
function subnet_del($options = "") { global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.06'; printmsg('DEBUG => subnet_del(' . $options . ') called', 3); // 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['subnet']) { // 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_del-v{$version} Deletes a subnet (subnet) from the database Synopsis: subnet_del [KEY=VALUE] ... Required: subnet=IP or ID select subnet by search string Optional: commit=[yes|no] commit db transaction (no) EOM ); } // Find the subnet record we're deleting 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_del') or !authlvl($subnet['lvl'])) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(3, $self['error'] . "\n"); } // If "commit" is yes, delete the subnet if ($options['commit'] == 'Y') { $text = ""; // FIXME: (add all this) ... // SUMMARY: // Delete assignments to any DHCP servers // Delete any DHCP pools on the current subnet // Delete any DHCP options associated with this subnet // Delete any interfaces belonging to hosts with more than one interface // Delete any hosts (and all their associated info) that have only one interface // Delete subnet Record // Delete custom attributes // // FIXME: display a warning if there are no more subnets that a dhcp server is serving dhcp for? // Delete DHCP server assignments list($status, $rows) = db_delete_records($onadb, 'dhcp_server_subnets', array('subnet_id' => $subnet['id'])); if ($status) { $self['error'] = "ERROR => DHCP server assignment delete failed: {$self['error']}"; return array(5, $self['error'] . "\n"); } // Delete DHCP pools list($status, $rows) = db_delete_records($onadb, 'dhcp_pools', array('subnet_id' => $subnet['id'])); if ($status) { $self['error'] = "ERROR => DHCP pool delete failed: {$self['error']}"; return array(5, $self['error'] . "\n"); } // Delete DHCP options list($status, $rows) = db_delete_records($onadb, 'dhcp_option_entries', array('subnet_id' => $subnet['id'])); if ($status) { $self['error'] = "ERROR => DHCP parameter delete failed: {$self['error']}"; return array(5, $self['error'] . "\n"); } // Delete tag entries list($status, $rows, $records) = db_get_records($onadb, 'tags', array('type' => 'subnet', 'reference' => $subnet['id'])); $log = array(); $i = 0; foreach ($records as $record) { $log[$i] = "INFO => Tag DELETED: {$record['name']} from {$subnet['name']}"; $i++; } //do the delete list($status, $rows) = db_delete_records($onadb, 'tags', array('type' => 'subnet', 'reference' => $subnet['id'])); if ($status) { $self['error'] = "ERROR => subnet_del() Tag delete SQL Query failed: {$self['error']}"; printmsg($self['error'], 0); return array(5, $add_to_error . $self['error'] . "\n"); } //log deletions foreach ($log as $log_msg) { printmsg($log_msg, 0); $add_to_error .= $log_msg . "\n"; } // Delete custom attribute entries // get list for logging list($status, $rows, $records) = db_get_records($onadb, 'custom_attributes', array('table_name_ref' => 'subnets', 'table_id_ref' => $subnet['id'])); $log = array(); $i = 0; foreach ($records as $record) { list($status, $rows, $ca) = ona_get_custom_attribute_record(array('id' => $record['id'])); $log[$i] = "INFO => Custom Attribute DELETED: {$ca['name']} ({$ca['value']}) from {$subnet['name']}"; $i++; } //do the delete list($status, $rows) = db_delete_records($onadb, 'custom_attributes', array('table_name_ref' => 'subnets', 'table_id_ref' => $subnet['id'])); if ($status) { $self['error'] = "ERROR => subnet_del() Custom attribute delete SQL Query failed: {$self['error']}"; printmsg($self['error'], 0); return array(5, $self['error'] . "\n"); } //log deletions foreach ($log as $log_msg) { printmsg($log_msg, 0); //$add_to_error .= $log_msg . "\n"; } // Delete associated host / interface records that need to be deleted // BUSINESS RULE: We delete hosts that have only one interface (and it's on this subnet) // BUSINESS RULE: We delete interfaces from hosts that have multiple interfaces list($status, $rows, $interfaces) = db_get_records($onadb, 'interfaces', array('subnet_id' => $subnet['id'])); $hosts_to_delete = array(); $interfaces_to_delete = array(); foreach ($interfaces as $interface) { // Select all interfaces for the associated host where the subnet ID is not our subnet ID $where = "host_id = {$interface['host_id']} AND subnet_id != {$subnet['id']}"; list($status, $rows, $tmp) = db_get_records($onadb, 'interfaces', $where, '', 0); // We'll delete hosts that have only one interface (i.e. no interfaces on any other subnets) if ($rows == 0) { array_push($hosts_to_delete, $interface['host_id']); } else { array_push($interfaces_to_delete, $interface['id']); } } unset($interfaces); // make sure we only have one reference for each host and interface $interfaces_to_delete = array_unique($interfaces_to_delete); $hosts_to_delete = array_unique($hosts_to_delete); // Delete interfaces we have selected foreach ($interfaces_to_delete as $interface_id) { list($status, $output) = run_module('interface_del', array('interface' => $interface_id, 'commit' => 'Y')); if ($status) { return array(5, $output); } } // Delete hosts we have selected foreach ($hosts_to_delete as $host_id) { list($status, $output) = run_module('host_del', array('host' => $host_id, 'commit' => 'Y')); if ($status) { return array(5, $output); } } // Delete the subnet list($status, $rows) = db_delete_records($onadb, 'subnets', array('id' => $subnet['id'])); if ($status or !$rows) { $self['error'] = "ERROR => Subnet delete failed: {$self['error']}"; return array(5, $self['error'] . "\n"); } // Return the success notice $ip = ip_mangle($subnet['ip_addr'], 'dotted'); $cidr = ip_mangle($subnet['ip_mask'], 'cidr'); $self['error'] = "INFO => Subnet DELETED: {$subnet['name']} IP: {$ip}/{$cidr}"; printmsg($self['error'], 0); return array(0, $self['error'] . "\n"); } // // We are just displaying records that would have been deleted // // SUMMARY: // Display assignments to any DHCP servers // Display any DHCP pools on the current subnet // Display any DHCP parameters associated with this subnet // Display subnet Record // Display Host records (and all their sub-records) // Display custom attributes // 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"; // Display the Subnet's complete record list($status, $tmp) = subnet_display("subnet={$subnet['id']}&verbose=N"); $text .= "\n" . $tmp; // Display assignments to any DHCP servers list($status, $rows, $records) = db_get_records($onadb, 'dhcp_server_subnets', array('subnet_id' => $subnet['id'])); if ($rows) { $text .= "\nASSOCIATED DHCP SERVER ASSIGNMENT RECORDS ({$rows}):\n"; } foreach ($records as $record) { $text .= format_array($record); } // Display any DHCP pools on the current subnet list($status, $rows, $records) = db_get_records($onadb, 'dhcp_pools', array('subnet_id' => $subnet['id'])); if ($rows) { $text .= "\nASSOCIATED DHCP POOL RECORDS ({$rows}):\n"; } foreach ($records as $record) { $text .= format_array($record); } // Display associated DHCP entries list($status, $rows, $records) = db_get_records($onadb, 'dhcp_option_entries', array('subnet_id' => $subnet['id'])); if ($rows) { $text .= "\nASSOCIATED DHCP ENTRY RECORDS ({$rows}):\n"; } foreach ($records as $record) { list($status, $rows, $dhcp) = ona_get_dhcp_option_entry_record(array('id' => $record['id'])); $text .= " {$dhcp['display_name']} => {$dhcp['value']}\n"; } // Display associated tags list($status, $rows, $records) = db_get_records($onadb, 'tags', array('type' => 'subnet', 'reference' => $subnet['id'])); if ($rows) { $text .= "\nASSOCIATED TAG RECORDS ({$rows}):\n"; } foreach ($records as $record) { $text .= " {$record['name']}\n"; } // Display associated custom attributes list($status, $rows, $records) = db_get_records($onadb, 'custom_attributes', array('table_name_ref' => 'subnets', 'table_id_ref' => $subnet['id'])); if ($rows) { $text .= "\nASSOCIATED CUSTOM ATTRIBUTE RECORDS ({$rows}):\n"; } foreach ($records as $record) { list($status, $rows, $ca) = ona_get_custom_attribute_record(array('id' => $record['id'])); $text .= " {$ca['name']} => {$ca['value']}\n"; } // Display associated host / interface records that would be deleted // BUSINESS RULE: We delete hosts that have only one interface (and it's on this subnet) // BUSINESS RULE: We delete interfaces from hosts that have multiple interfaces (including at least one on a different subnet) list($status, $rows, $interfaces) = db_get_records($onadb, 'interfaces', array('subnet_id' => $subnet['id'])); $hosts_to_delete = array(); $interfaces_to_delete = array(); foreach ($interfaces as $interface) { // Select all interfaces for the associated host where the subnet ID is not our subnet ID $where = "host_id = {$interface['host_id']} AND subnet_id != {$subnet['id']}"; list($status, $rows, $tmp) = db_get_records($onadb, 'interfaces', $where, '', 0); // We'll delete hosts that have only one interface (i.e. no interfaces on any other subnets) if ($rows == 0) { array_push($hosts_to_delete, $interface['host_id']); } else { array_push($interfaces_to_delete, $interface['id']); } } unset($interfaces); // make sure we only have one reference for each host and interface $interfaces_to_delete = array_unique($interfaces_to_delete); $hosts_to_delete = array_unique($hosts_to_delete); // Display interfaces we would have deleted $rows = count($interfaces_to_delete); if ($rows) { $text .= "\n----- ASSOCIATED HOST INTERFACE RECORDS ({$rows}) -----\n"; } foreach ($interfaces_to_delete as $interface_id) { list($status, $output) = run_module('interface_del', array('interface' => $interface_id), false); $output = preg_replace('/^(.*)?\\n(.*)?\\n/', '', $output); $text .= $output; } // Display hosts we would have deleted $rows = count($hosts_to_delete); if ($rows) { $text .= "\n-----ASSOCIATED HOSTS ({$rows}) -----\n"; } foreach ($hosts_to_delete as $host_id) { list($status, $output) = run_module('host_del', array('host' => $host_id), false); $output = preg_replace('/^(.*)?\\n(.*)?\\n/', '', $output); $text .= $output; } return array(7, $text); }
function vlan_campus_display($options = "") { global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.00'; printmsg("DEBUG => vlan_campus_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['campus']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM vlan_campus_display-v{$version} Displays a vlan campus record from the database Synopsis: vlan_campus_display [KEY=VALUE] ... Required: campus=NAME or ID Campus name or ID of the campus display Optional: verbose=[yes|no] Display additional info (DEFAULT: yes) EOM ); } // The formatting rule on campus names is all upper and trim it $options['campus'] = strtoupper(trim($options['campus'])); // If the campus provided is numeric, check to see if it's valid if (is_numeric($options['campus'])) { // See if it's an vlan_campus_id list($status, $rows, $campus) = ona_get_vlan_campus_record(array('id' => $options['campus'])); if (!$campus['id']) { printmsg("DEBUG => Unable to find campus using the ID {$options['campus']}!", 3); $self['error'] = "ERROR => Unable to find campus using the ID {$options['campus']}!"; return array(2, $self['error'] . "\n"); } } else { list($status, $rows, $campus) = ona_get_vlan_campus_record(array('name' => $options['campus'])); if (!$campus['id']) { $self['error'] = "ERROR => Unable to find campus using the name {$options['campus']}!"; printmsg("DEBUG => Unable to find campus using the name {$options['campus']}!", 3); return array(2, $self['error'] . "\n"); } } printmsg("DEBUG => Found campus: {$campus['name']}", 3); // Build text to return $text = "VLAN CAMPUS RECORD\n"; $text .= format_array($campus); // If 'verbose' is enabled, grab some additional info to display if ($options['verbose'] == 'Y') { // vlan record(s) $i = 0; do { list($status, $rows, $vlan) = ona_get_vlan_record(array('vlan_campus_id' => $campus['id'])); if ($rows == 0) { break; } $i++; $text .= "\nASSOCIATED VLAN RECORD ({$i} of {$rows})\n"; $text .= format_array($vlan); } while ($i < $rows); } // Return the success notice return array(0, $text); }
function setState($newstate) { global $ourcallid; global $partycallid; global $state; global $uploaded_prompts; global $keys; global $wait_time; global $caller; global $hold_keys; global $destination; global $called; global $ev; // are we exiting? if ($state == "") { return; } debug("setState('{$newstate}') state: {$state}"); $state = $newstate; // always obey a return to prompt switch ($newstate) { case "greeting": // check what prompt to use for this time of day //$query = "select prompts.prompt_id, prompts.file as prompt from time_frames, prompts /*where numeric_day=extract(dow from now()) and cast(start_hour as integer)<=extract(HOUR FROM now()) AND cast(end_hour as integer)>extract(HOUR FROM now()) and time_frames.prompt_id=prompts.prompt_id*/ UNION select prompt_id, file as prompt from prompts where status='online'"; $status = 'offline'; $query = "select prompt_id, day, start_hour, end_hour, numeric_day FROM time_frames"; $res = query_to_array($query); if (count($res)) { $day_week = date('w'); $hour = date('H') * 1; debug("Current week index '{$day_week}' : hour '{$hour}'"); //$day_week = 2; //$hour = 12; $status = 'offline'; foreach ($res as $row) { if ($row["numeric_day"] == $day_week && $row["start_hour"] <= $hour && $hour < $row["end_hour"]) { $hold_keys = ''; // Reset default key $status = 'online'; } } } $query = "select prompts.prompt_id, prompts.file as prompt from prompts where status='{$status}'"; $res = query_to_array($query); debug('greeting:' . format_array($res)); if (!count($res)) { debug("Auto-Attendant is not configured!!"); setState("goodbye"); return; } $prompt_id = $res[0]["prompt_id"]; $prompt = $res[0]["prompt"]; // here we must have ".au" //$prompt = str_ireplace(".mp3", ".slin", $prompt); $query = "SELECT keys.key, destination FROM `keys` WHERE prompt_id={$prompt_id}"; $keys = query_to_array($query); debug('keys:' . format_array($keys)); $m = new Yate("chan.attach"); // ------------------------ /* prompt based on called */ $prompt_file = "{$uploaded_prompts}/auto_attendant/{$prompt}"; //debug('source[called]:<' . $prompt_file.".".$called.">"); if (file_exists($prompt_file . "." . $called)) { $prompt_file = $prompt_file . "." . $called; } $m->params["source"] = "wave/play/{$prompt_file}"; debug('source:' . "wave/play/{$prompt_file}"); // ------------------------ /* $m->params["source"] = "wave/play/$uploaded_prompts/auto_attendant/$prompt"; debug('source:' . "wave/play/$uploaded_prompts/auto_attendant/$prompt"); */ $m->params["notify"] = $ourcallid; $m->Dispatch(); break; case "prolong_greeting": $m = new Yate("chan.attach"); $m->params["consumer"] = "wave/record/-"; $m->params["notify"] = $ourcallid; $m->params["maxlen"] = $wait_time * 10000; $m->Dispatch(); break; case "goodbye": $m = new Yate("chan.attach"); $m->params["source"] = "tone/congestion"; $m->params["consumer"] = "wave/record/-"; $m->params["maxlen"] = 32000; $m->params["notify"] = $ourcallid; $m->Dispatch(); break; case "call.route": $to_call = null; debug('$keys = ' . $keys); for ($i = 0; $i < count($keys); $i++) { if ($keys[$i]["key"] == $hold_keys) { $to_call = $keys[$i]["destination"]; //$hold_keys = null; break; } } if ($hold_keys == '') { debug('$called = ' . $called); $query = "SELECT (CASE WHEN extension_id IS NOT NULL THEN (SELECT extension FROM extensions WHERE extensions.extension_id=dids.extension_id) ELSE (SELECT extension FROM groups WHERE groups.group_id=dids.group_id) END) as called FROM dids WHERE number='{$called}'"; $res = query_to_array($query); if (!count($res) || !strlen($res[0]["called"])) { // this should never happen setState("goodbye"); return; } $to_call = $res[0]["called"]; } if (!$to_call) { $to_call = $hold_keys; } $m = new Yate("call.route"); $m->params["caller"] = $caller; $m->params["called"] = $to_call; $m->params["id"] = $ourcallid; $m->params["already-auth"] = "yes"; $m->params["call_type"] = "from outside"; $m->Dispatch(); break; case "send_call": $m = new Yate("chan.masquerade"); $m->params = $ev->params; $m->params["message"] = "call.execute"; $m->params["id"] = $partycallid; $m->params["callto"] = $destination; $m->Dispatch(); break; } }
function dhcp_entry_display($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.00'; printmsg("DEBUG => dhcp_entry_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['server'] and !$options['subnet']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM dhcp_entry_display-v{$version} Displays an dhcp_entry record from the database Synopsis: dhcp_entry_display [KEY=VALUE] ... Required: host=NAME[.DOMAIN] or id hostname or id of the host to display OR subnet=NAME or id description or id of the subnet to display OR server=NAME[.DOMAIN] or id hostname or id of the server to display Notes: * DOMAIN will default to {$conf['dns_defaultdomain']} if not specified EOM ); } if ($options['host']) { // Determine the host is valid list($status, $rows, $host) = ona_find_host($options['host']); if (!$host['id']) { printmsg("DEBUG => The host specified, {$options['host']}, does not exist!", 3); $self['error'] = "ERROR => The host specified, {$options['host']}, does not exist!"; return array(2, $self['error'] . "\n"); } $anchor = 'host'; $desc = $host['FQDN']; $where = array('HOST_id' => $host['id']); } elseif ($options['subnet']) { // Determine the subnet is valid list($status, $rows, $subnet) = ona_find_subnet($options['subnet']); if (!$subnet['id']) { printmsg("DEBUG => The subnet specified, {$options['subnet']}, does not exist!", 3); $self['error'] = "ERROR => The subnet specified, {$options['subnet']}, does not exist!"; return array(3, $self['error'] . "\n"); } $anchor = 'subnet'; $desc = "{$subnet['DESCRIPTION']} (" . ip_mangle($subnet['IP_ADDRESS']) . ")"; $where = array('NETWORK_id' => $subnet['id']); } elseif ($options['server']) { // Determine the server is valid list($status, $rows, $host) = ona_find_host($options['server']); if (!$host['id']) { printmsg("DEBUG => The server specified, {$options['server']}, does not exist!", 3); $self['error'] = "ERROR => The server specified, {$options['server']}, does not exist!"; return array(4, $self['error'] . "\n"); } // Determine the host that was found is actually a server list($status, $rows, $server) = ona_get_server_record(array('HOST_id' => $host['id'])); if (!$server['id']) { printmsg("DEBUG => The host specified, {$host['FQDN']}, is not a server!", 3); $self['error'] = "ERROR => The host specified, {$host['FQDN']}, is not a server!"; return array(5, $self['error'] . "\n"); } $anchor = 'server'; $desc = $host['FQDN']; $where = array('SERVER_id' => $server['id']); } // Debugging printmsg("DEBUG => dhcp_entry_display(): Found {$anchor}: {$desc}", 3); // Build text to return $text = strtoupper($anchor) . " RECORD ({$desc})\n"; // Display the record(s) $i = 0; do { list($status, $rows, $entry) = ona_get_dhcp_entry_record($where); if ($rows == 0) { $text .= "\nNO ASSOCIATED DHCP ENTRY RECORDS\n"; break; } $i++; $text .= "\nASSOCIATED DHCP ENTRY RECORD ({$i} of {$rows})\n"; $text .= format_array($entry); } while ($i < $rows); // Return the success notice return array(0, $text); }
function dhcp_failover_group_display($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.00'; printmsg("DEBUG => dhcp_failover_group_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['id'] or $options['pri_server'] and $options['sec_server'])) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM dhcp_failover_group_display-v{$version} Displays an DHCP failover group record from the database Synopsis: dhcp_failover_group_display [KEY=VALUE] ... Required: id=id id of the DHCP failover group to display OR pri_server=NAME[.DOMAIN] or id identifier of the primary server sec_server=NAME[.DOMAIN] or id identifier of the secondary server EOM ); } $search = array(); if ($options['pri_server'] and $options['sec_server']) { // Determine the server is valid list($status, $rows, $pri_host) = ona_find_host($options['pri_server']); if (!$pri_host['id']) { printmsg("DEBUG => The server specified, {$options['pri_server']}, does not exist!", 3); $self['error'] = "ERROR => The server specified, {$options['pri_server']}, does not exist!"; return array(2, $self['error'] . "\n"); } // Determine the host that was found is actually a server // MP: FIXME: dont think I'm going to pursue doing a seperate server table.. lets remove // list($status, $rows, $pri_server) = ona_get_server_record(array('host_id' => $pri_host['id'])); // // if (!$pri_server['id']) { // printmsg("DEBUG => The host specified, {$pri_host['FQDN']}, is not a server!",3); // $self['error'] = "ERROR => The host specified, {$pri_host['FQDN']}, is not a server!"; // return(array(5, $self['error'] . "\n")); // } // Determine the server is valid list($status, $rows, $sec_host) = ona_find_host($options['sec_server']); if (!$sec_host['id']) { printmsg("DEBUG => The server specified, {$options['sec_server']}, does not exist!", 3); $self['error'] = "ERROR => The server specified, {$options['sec_server']}, does not exist!"; return array(2, $self['error'] . "\n"); } // Determine the host that was found is actually a server // MP: FIXME: dont think I'm going to pursue doing a seperate server table.. lets remove // list($status, $rows, $sec_server) = ona_get_server_record(array('HOST_id' => $sec_host['id'])); // // if (!$sec_server['id']) { // printmsg("DEBUG => The host specified, {$sec_host['fqdn']}, is not a server!",3); // $self['error'] = "ERROR => The host specified, {$sec_host['fqdn']}, is not a server!"; // return(array(5, $self['error'] . "\n")); // } $search['primary_server_id'] = $pri_server['id']; $search['secondary_server_id'] = $sec_server['id']; } if ($options['id']) { $search['id'] = $options['id']; } // Determine the entry itself exists list($status, $rows, $failovergroup) = ona_get_dhcp_failover_group_record($search); // Test to see that we were able to find the specified record if (!$failovergroup['id']) { printmsg("DEBUG => Unable to find the DHCP failover group record using {$options['id']}!", 3); $self['error'] = "ERROR => Unable to find the DHCP failover group record using {$options['id']}!"; return array(4, $self['error'] . "\n"); } list($status, $rows, $pri_server) = ona_find_host($failovergroup['primary_server_id']); list($status, $rows, $sec_server) = ona_find_host($failovergroup['secondary_server_id']); $failovergroup['pri_server_name'] = $pri_server['fqdn']; $failovergroup['sec_server_name'] = $sec_server['fqdn']; // Debugging printmsg("DEBUG => dhcp_failover_group_display(): Found id:{$failovergroup['id']}", 3); // Build text to return $text = "DHCP FAILOVER GROUP RECORD:\n"; $text .= format_array($failovergroup); // Return the success notice return array(0, $text); }
function location_del($options = "") { // The important globals global $conf, $self, $onadb; // Version - UPDATE on every edit! $version = '1.01'; printmsg("DEBUG => location_del({$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['reference']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM locaiton_del-v{$version} Deletes a location from the database Synopsis: location_del [KEY=VALUE] ... Required: reference=NAME or ID Reference or ID of the location to delete Optional: commit=[yes|no] commit db transaction (no) EOM ); } // Sanitize options[commit] (default is no) $options['commit'] = sanitize_YN($options['commit'], 'N'); // Find the Location to use list($status, $rows, $loc) = ona_find_location($options['reference']); if ($status or !$rows) { printmsg("DEBUG => The location specified, {$options['reference']}, does not exist!", 3); return array(2, "ERROR => The location specified, {$options['reference']}, does not exist!\n"); } printmsg("DEBUG => Location selected: {$loc['reference']}, location name: {$loc['name']}", 3); list($status, $rows, $usage) = db_get_records($onadb, 'devices', array('location_id' => $loc['id']), '', 0); if ($rows != 0) { printmsg("DEBUG => The location ({$loc['reference']}) is in use by {$rows} devices(s)!", 3); $self['error'] = "ERROR => The location ({$loc['reference']}) is in use by {$rows} devices(s)!"; return array(6, $self['error'] . "\n"); } // If "commit" is yes, delete the record if ($options['commit'] == 'Y') { // Check permissions if (!auth('location_del')) { $self['error'] = "Permission denied!"; printmsg($self['error'], 0); return array(10, $self['error'] . "\n"); } list($status, $rows) = db_delete_records($onadb, 'locations', array('id' => $loc['id'])); if ($status or !$rows) { $self['error'] = "ERROR => location_del() SQL Query failed: " . $self['error']; printmsg($self['error'], 0); return array(4, $self['error'] . "\n"); } // Return the success notice $self['error'] = "INFO => Location DELETED: {$loc['reference']} ({$loc['name']})"; printmsg($self['error'], 0); return array(0, $self['error'] . "\n"); } // Otherwise display the record that would have been deleted $text = <<<EOL Record(s) NOT DELETED (see "commit" option) Displaying record(s) that would have been deleted: EOL; $text .= format_array($loc); $text .= "\n"; return array(6, $text); }
function interface_display($options = "") { global $conf, $self; $text_array = array(); // Version - UPDATE on every edit! $version = '1.03'; printmsg("DEBUG => interface_display({$options}) called", 3); // Parse incoming options string to an array $options = parse_options($options); // Return the usage summary if we need to if ($options['help'] or !$options['host'] and !$options['interface']) { // NOTE: Help message lines should not exceed 80 characters for proper display on a console $self['error'] = 'ERROR => Insufficient parameters'; return array(1, <<<EOM interface_display-v{$version} Displays an interface record from the database Synopsis: interface_display [KEY=VALUE] ... Required: interface=[ID|IP|MAC] display interface by search string or host=NAME[.DOMAIN] or ID display interface by hostname or host_id Optional: verbose=[yes|no] display additional info (yes) Notes: * If search returns more than one interface, an error is displayed * DOMAIN will default to {$conf['dns_defaultdomain']} if not specified EOM ); } // Sanitize "options[verbose]" (yes is the default) $options['verbose'] = sanitize_YN($options['verbose'], 'Y'); // They provided a interface ID, IP address, interface name, or MAC address if ($options['interface']) { // Find an interface record by something in that interface's record list($status, $rows, $interface) = ona_find_interface($options['interface']); } else { if ($options['host']) { // Find a host by the user's input list($status, $rows, $host) = ona_find_host($options['host']); if (!$host['id']) { printmsg("DEBUG => Host not found ({$options['host']})!", 3); $self['error'] = "ERROR => Host not found ({$options['host']})"; return array(2, $self['error'] . "\n"); } // If we got one, load an associated interface list($status, $rows, $interface) = ona_get_interface_record(array('host_id' => $host['id'])); } } // If we didn't get a record then exit if (!$interface['id']) { if ($rows > 1) { $self['error'] = "ERROR => More than one interface matches"; } else { $self['error'] = "ERROR => Interface not found ({$options['interface']})"; } return array(4, $self['error'] . "\n"); } $text_array = $interface; // Build text to return $text = "INTERFACE RECORD\n"; $text .= format_array($interface); // If 'verbose' is enabled, grab some additional info to display if ($options['verbose'] == 'Y') { // Host record list($status, $rows, $host) = ona_get_host_record(array('ID' => $interface['host_id'])); if ($rows >= 1) { $text .= "\nASSOCIATED HOST RECORD\n"; $text .= format_array($host); } // Subnet record list($status, $rows, $subnet) = ona_get_subnet_record(array('ID' => $interface['subnet_id'])); if ($rows >= 1) { $text .= "\nASSOCIATED SUBNET RECORD\n"; $text .= format_array($subnet); } // Device record list($status, $rows, $device) = ona_get_device_record(array('id' => $host['device_id'])); if ($rows >= 1) { $text .= "\nASSOCIATED DEVICE RECORD\n"; $text .= format_array($device); } } // change the output format if other than default if ($options['format'] == 'json') { $text = $text_array; } if ($options['format'] == 'yaml') { $text = $text_array; } // Return the success notice return array(0, $text); }