function guifi_device_form($form_state, $params = array()) { global $user; $form = array(); //$form['#suffix'] = '<script type="text/javascript">' // . 'jQuery(\'input#edit-funders-0-user\').focus();' // . '</script>'; guifi_log(GUIFILOG_TRACE, 'function guifi_device_form()', $params); // Local javascript validations not actve because of bug in Firefox // Errors are not displayed when fieldset folder is collapsed // guifi_validate_js("#guifi-device-form"); // $form['#attributes'] = array('onsubmit' => 'kk'); if (empty($form_state['values'])) { $form_state['values'] = $params; } $form_state['#redirect'] = FALSE; // if new device, initializing variables if ($form_state['values']['nid'] == NULL && $params['add'] != NULL) { $form_state['values']['nid'] = $params['add']; $form_state['values']['new'] = TRUE; $form_state['values']['type'] = $params['type']; $form_state['values']['links'] = array(); $form_state['values']['netmask'] = '255.255.255.224'; if ($form_state['values']['type'] == 'radio') { $form_state['values']['variable']['firmware_id'] = '23'; $form_state['values']['variable']['model_id'] = '70'; } } drupal_set_breadcrumb(guifi_node_ariadna($form_state['values']['nid'])); // Check permissions if ($params['edit']) { if (!guifi_device_access('update', $params['edit'])) { drupal_set_message(t('You are not authorized to edit this device', 'error')); return; } } // Loading node & zone where the device belongs to (some information will be used) $node = node_load(array('nid' => $form_state['values']['nid'])); $zone = node_load($node->zone_id); // Setting the breadcrumb drupal_set_breadcrumb(guifi_node_ariadna($form_state['values']['nid'])); // if contact is NULL, then get it from the node or the user logged in drupal if (is_null($form_state['values']['notification'])) { if (guifi_notification_validate($node->notification)) { $form_state['values']['notification'] = $node->notification; } else { drupal_set_message(t('The node has not a valid email address as a contact. Using your email as a default. Change the contact mail address if necessary.')); $form_state['values']['notification'] = $user->mail; } } // if nick is NULL, get a default name if ($form_state['values']['nick'] == "") { $form_state['values']['nick'] = guifi_device_get_default_nick($node, $form_state['values']['type'], $form_state['values']['nid']); } if (isset($form_state['action'])) { guifi_log(GUIFILOG_TRACE, 'action', $form_state['action']); if (function_exists($form_state['action'])) { if (!call_user_func_array($form_state['action'], array(&$form, &$form_state))) { return $form; } } } $form_weight = 0; if ($form_state['values']['id']) { $form['id'] = array('#type' => 'hidden', '#name' => 'id', '#value' => $form_state['values']['id'], '#weight' => $form_weight++); } else { $form['new'] = array('#type' => 'hidden', '#name' => 'new', '#weight' => $form_weight++, '#value' => TRUE); } $form['type'] = array('#type' => 'hidden', '#name' => 'type', '#weight' => $form_weight++, '#value' => $form_state['values']['type']); if ($params['add'] != NULL) { drupal_set_title(t('adding a new %device at %node', array('%node' => $node->nick, '%device' => $form_state['values']['type']))); } else { drupal_set_title(t('edit device %dname', array('%dname' => $form_state['values']['nick']))); } // All preprocess is complete, now going to create the form $form['main'] = array('#type' => 'fieldset', '#title' => t('Device name, status and main settings') . ' (' . $form_state['values']['nick'] . ') - ' . $form_state['values']['flag'], '#weight' => $form_weight++, '#collapsible' => TRUE, '#attributes' => array('class' => 'fieldset-device-main'), '#collapsed' => is_null($params['edit'])); $form['main']['movenode'] = array('#type' => 'textfield', '#title' => t('Node'), '#maxlength' => 60, '#weight' => $form_weight++, '#default_value' => $form_state['values']['nid'] . '-' . guifi_get_zone_nick(guifi_get_zone_of_node($form_state['values']['nid'])) . ', ' . guifi_get_nodename($form_state['values']['nid']), '#autocomplete_path' => 'guifi/js/select-node', '#element_validate' => array('guifi_nodename_validate'), '#description' => t('Select the node where the device is.<br />' . 'You can find the node by introducing part of the node id number, ' . 'zone name or node name. A list with all matching values ' . 'with a maximum of 50 values will be created.<br />' . 'You can refine the text to find your choice.'), '#prefix' => '<div class"form-newline">', '#suffix' => '</div>'); $form['main']['nid'] = array('#type' => 'hidden', '#weight' => $form_weight++, '#value' => $form_state['values']['nid']); $form['main']['nick'] = array('#type' => 'textfield', '#size' => 20, '#maxlength' => 128, '#title' => t('nick'), '#required' => TRUE, '#attributes' => array('class' => 'required'), '#default_value' => $form_state['values']['nick'], '#weight' => $form_weight++, '#description' => t('The name of the device.<br />Used as a hostname, SSID, etc...')); $form['main']['flag'] = array('#type' => 'select', '#title' => t("Status"), '#required' => TRUE, '#default_value' => $form_state['values']['flag'], '#options' => guifi_types('status'), '#weight' => $form_weight++, '#description' => t("Current status of this device.")); $form['main']['notification'] = array('#type' => 'textfield', '#size' => 60, '#maxlength' => 1024, '#title' => t('contact'), '#required' => TRUE, '#element_validate' => array('guifi_emails_validate'), '#default_value' => $form_state['values']['notification'], '#weight' => $form_weight++, '#description' => t('Mailid where changes on the device will be notified, ' . 'if many, separated by \',\'<br />' . 'used for network administration.')); if (!empty($form_state['values']['ipv4'])) { $form['main']['mainipv4'] = array('#type' => 'select', '#title' => t('Main IPv4'), '#options' => guifi_get_currentDeviceIpv4s($form_state['values']), '#default_value' => $form_state['values']['mainipv4'], '#weight' => $form_weight++, '#description' => t('Used for monitoring.<br>Save and continue to refresh available addresses')); } $form['main']['logserver'] = array('#type' => 'textfield', '#size' => 60, '#maxlength' => 60, '#title' => t('Log Server'), '#default_value' => $form_state['values']['logserver'], '#weight' => $form_weight++, '#description' => t('If you have a log server for mikrotik (dude), add your ip.')); $form['main']['graph_server'] = array('#type' => 'select', '#title' => t("Server which collects traffic and availability data"), '#required' => FALSE, '#default_value' => $node->graph_server ? $node->graph_server : 0, '#options' => array('0' => t('Default'), '-1' => t('None')) + guifi_services_select('SNPgraphs'), '#weight' => $form_weight++, '#description' => t("If not specified, inherits zone properties.")); if (!user_access('administer guifi zones') || $device['user_created'] == $user->uid and $form_state['values']['type'] == 'radio') { $form['main']['graph_server']['#disabled'] = true; $form['main']['graph_server']['#description'] .= '<br>' . t('To change the value, you are required for maintainer privilege.'); } /* * maintainers fieldset */ $form['maintainers'] = guifi_maintainers_form(array2object($form_state['values']), $form_weight); guifi_log(GUIFILOG_TRACE, 'function guifi_device_form(maintainers)', $form_state['values']['maintainers']); /* * funders fieldset */ $form['funders'] = guifi_funders_form(array2object($form_state['values']), $form_weight); guifi_log(GUIFILOG_TRACE, 'function guifi_device_form(funders)', $form_state['values']['funders']); guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans type)'), $form_weight); // create the device-type depenedent form // looking for a "guifi_"<device_type>"_form()" function if (function_exists('guifi_' . $form_state['values']['type'] . '_form')) { $form = array_merge($form, call_user_func_array('guifi_' . $form_state['values']['type'] . '_form', array($form_state['values'], &$form_weight))); } // Cable interfaces/networking guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans if cable links)'), $form_weight); $form['if'] = guifi_interfaces_cable_form($form_state['values'], $form_weight); $form['if']['#weight'] = $form_weight++; // Cable interfaces/ports guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans if ports)'), $form_weight); if (isset($form_state['values']['interfaces'])) { foreach ($form_state['values']['interfaces'] as $k => $v) { unset($form_state['values']['interfaces'][$k]['ipv4']); } guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans if ports)'), $form_state['values']['interfaces']); $form['interfaces'] = guifi_ports_form($form_state['values'], $form_weight); $form['interfaces']['#weight'] = $form_weight++; } // VLANs (VLans, VRRPs, WDS...) guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans if vLANs)'), $form_weight); $form['vlans'] = guifi_vinterfaces_form('vlans', $form_state['values'], $form_weight); $form['vlans']['#weight'] = $form_weight++; // Aggregations (Bridges & Bondings) guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans if Aggregations)'), $form_weight); $form['aggregations'] = guifi_vinterfaces_form('aggregations', $form_state['values'], $form_weight); $form['aggregations']['#weight'] = $form_weight++; // ipv4 guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans if ipv4)'), $form_weight); $form['ipv4'] = guifi_ipv4s_form($form_state['values'], $form_weight); $form['ipv4']['#weight'] = $form_weight++; guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans comments)'), $form_weight); // Comments $form['comment'] = array('#type' => 'textarea', '#title' => t('Comments'), '#default_value' => $form_state['values']['comment'], '#description' => t('This text will be displayed as an information of the device.'), '#cols' => 60, '#rows' => 5, '#weight' => $form_weight++); guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans buttons)'), $form_weight); // save/validate/reset buttons $form['dbuttons'] = guifi_device_buttons(FALSE, '', 0, $form_weight); $form['dbuttons']['#weight'] = $form_weight++; guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(form_final)'), $form); return $form; }
/** * E-mail address update form submit */ function guifi_tools_mail_update_form_submit($form, &$form_state) { global $user; guifi_log(GUIFILOG_TRACE, 'guifi_tools_mail_update_submit()', $form_state['values']); // perform the massive update to the granted rows, using guifi db api // instead of straight SQL to create the notificaton messages. $tables = array('guifi_zone', 'guifi_location', 'guifi_devices', 'guifi_services', 'guifi_users'); foreach ($tables as $table) { $sqlm = db_query('SELECT * FROM {%s} WHERE notification LIKE "%s"', $table, $form_state['values']['mail_search']); while ($amails = db_fetch_object($sqlm)) { // Check that the user has update access and creates the link $continue = FALSE; if (!user_access('administer guifi networks')) { switch ($table) { case 'guifi_users': $title = $amails->username; $type = t('User'); if (guifi_user_access('update', $amails->id)) { $continue = TRUE; } break; case 'guifi_devices': $title = $amails->nick; $type = t('Device'); if (guifi_device_access('update', $amails->id)) { $continue = TRUE; } break; case 'guifi_zone': $title = $amails->nick; $type = t('Zone'); if (guifi_zone_access('update', $amails->id)) { $continue = TRUE; } break; case 'guifi_location': $title = $amails->nick; $type = t('Node'); if (guifi_node_access('update', $amails->id)) { $continue = TRUE; } break; case 'guifi_service': $title = $amails->nick; $type = t('Service'); if (guifi_service_access('update', $amails->id)) { $continue = TRUE; } break; } } else { $continue = TRUE; } if (!$continue) { continue; } // here we have update access, so perform the update // Notify prevuious mail id, just in case... $to_mail = $amails->notification; $amails->notification = str_ireplace($form_state['values']['mail_search'], strtolower($form_state['values']['mail_replacewith']), $amails->notification); if ($to_mail == $amails->notification) { //no changes, so next continue; } $n = _guifi_db_sql($table, array('id' => $amails->id), (array) $amails, $log, $to_mail); guifi_notify($to_mail, t('The notification %notify for %type %title has been CHANGED to %new by %user.', array('%notify' => $form_state['values']['mail_search'], '%new' => $form_state['values']['mail_replacewith'], '%type' => $type, '%title' => $title, '%user' => $user->name)), $log); } // foreach row with the email found } // foreach table drupal_goto('guifi/menu/ip/mailsearch/' . $form_state['values']['mail_replacewith']); }
function theme_guifi_node_devices_list($node, $links = FALSE) { $id = $node->id; $rows = array(); $header = array('<h2>' . t('device') . '</h2>', t('type'), t('ip'), t('status'), array('data' => t('last available'), 'style' => 'text-align: right;'), t('unsolclic')); // Form for adding a new device $form = drupal_get_form('guifi_device_create_form', $node); $query = db_query("SELECT d.id FROM {guifi_devices} d WHERE nid=%d", $id); while ($d = db_fetch_object($query)) { $device = guifi_device_load($d->id); // Edit and delete buttons if (guifi_device_access('update', $device['id'])) { $edit_device = l(guifi_img_icon('edit.png'), 'guifi/device/' . $device['id'] . '/edit', array('html' => TRUE, 'attributes' => array('target' => '_blank', 'title' => t('edit device')))); $delete_device = l(guifi_img_icon('drop.png'), 'guifi/device/' . $device['id'] . '/delete', array('html' => TRUE, 'attributes' => array('target' => '_blank', 'title' => t('delete device')))); } else { $edit_device = ''; $delete_device = ''; } // Traceroute button if (user_access('create guifi nodes')) { $traceroute = l(guifi_img_icon('discover-routes.png'), 'guifi/menu/ip/traceroute/' . $device['id'], array('html' => TRUE, 'attributes' => array('target' => '_blank', 'title' => t('trace routes, discover services from this device')))); } else { $traceroute = ''; } // Firmware text which links to unsolclic feature if ($device->variable['firmware'] != "n/d") { $unsolclic = l($device[variable]['firmware'], 'guifi/device/' . $device['id'] . '/view/unsolclic', array('attributes' => array('title' => t("Get radio configuration with singleclick")))); } // Get IP assigned to the device $ip = guifi_main_ip($device[id]); // Availability image $status_url = guifi_cnml_availability(array('device' => $device['id'], 'format' => 'short')); // Device main attributes if (!empty($device['manufacturer'])) { $mDescr = $device['manufacturer'] . '-' . $device['model']; } else { $mDescr = ''; } $uCreated = db_fetch_object(db_query('SELECT u.name FROM {users} u WHERE u.uid = %d', $device[user_created])); $deviceAttr = $device[id] . ' ' . $mDescr . ' ' . t('created by') . ': ' . $uCreated->name . ' ' . t('at') . ' ' . format_date($device[timestamp_created], 'small'); if (!empty($device[timestamp_changed])) { $uChanged = db_fetch_object(db_query('SELECT u.name FROM {users} u WHERE u.uid = %d', $device[user_changed])); $deviceAttr .= ' ' . t('updated by') . ': ' . $uChanged->name . ' ' . t('at') . ' ' . format_date($device[timestamp_changed], 'small'); } // Groups all this data in an array for the theme() function $rows[] = array(l($device[nick], 'guifi/device/' . $device[id], array('attributes' => array('title' => $deviceAttr))), $device[type], array('data' => $ip[ipv4] . '/' . $ip[maskbits], 'align' => 'left'), empty($ip[ipv4]) ? ' ' : array('data' => t($device[flag]), 'class' => $device['flag']), empty($ip[ipv4]) ? ' ' : array('data' => $status_url, 'class' => $device['flag']), $unsolclic, $edit_device, $delete_device, empty($ip[ipv4]) ? ' ' : $traceroute); } // Creates the table with devices if any, otherwise just outputs the node has not devices if (count($rows)) { $output = '<h4>' . t('devices') . '</h4>' . theme('table', $header, $rows, array('class' => 'device-data')) . $form; } else { $output = theme('box', t('This node does not have any device'), $form); } // Again, it creates a table with the links, if they exist if ($links) { $node = node_load(array('nid' => $node->id)); drupal_set_title(t('devices @ %node', array('%node' => $node->title))); drupal_set_breadcrumb(guifi_node_ariadna($node)); $output .= theme_links(module_invoke_all('link', 'node', $node, FALSE)); print theme('page', $output, FALSE); return; } return $output; }
/** * * @param GuifiAPI $gapi GuifiAPI object * * @param mixed[] $parameters * * @return */ function guifi_api_link_remove($gapi, $parameters) { if (!guifi_api_check_fields($gapi, array('link_id'), $parameters)) { return FALSE; } $link_id = $parameters['link_id']; $link_query = db_query('SELECT * FROM {guifi_links} WHERE id = %d', $link_id); if ($link = db_fetch_object($link_query)) { do { if (!$link->device_id) { $gapi->addError(500, "link not found: {$link_id}"); return FALSE; } $device = guifi_device_load($link->device_id); if (!guifi_device_access('update', $device)) { $gapi->addError(501); return FALSE; } $interface = db_fetch_object(db_query('SELECT * FROM {guifi_interfaces} WHERE id = %d LIMIT 1', $link->interface_id)); if ($interface->interface_type == 'Wan' || $interface->interface_type == 'wds/p2p') { $device['radios'][$interface->radiodev_counter]['interfaces'][$link->interface_id]['ipv4'][$link->ipv4_id]['deleted'] = TRUE; $device['radios'][$interface->radiodev_counter]['interfaces'][$link->interface_id]['ipv4'][$link->ipv4_id]['links'][$link->id]['deleted'] = TRUE; } else { $device['radios'][$interface->radiodev_counter]['interfaces'][$link->interface_id]['ipv4'][$link->ipv4_id]['links'][$link->id]['deleted'] = TRUE; } guifi_device_save($device); } while ($link = db_fetch_object($link_query)); } else { $gapi->addError(500, "link not found: {$link_id}"); return FALSE; } return TRUE; }
function _guifi_user_queue_devices($u) { $query = db_query('SELECT d.id ' . 'FROM {guifi_devices} d ' . 'WHERE d.nid=%d' . ' AND type="radio"', $u['nid']); $rows = array(); while ($d = db_fetch_array($query)) { $d = guifi_device_load($d['id']); $d['uid'] = $u['id']; if (guifi_device_access('update', $d['id'])) { $edit_device_icon = l(guifi_img_icon('edit.png'), 'guifi/device/' . $d['id'] . '/edit', array('html' => TRUE, 'attributes' => array('target' => '_blank'))) . l(guifi_img_icon('drop.png'), 'guifi/device/' . $d['id'] . '/delete', array('html' => TRUE, 'attributes' => array('target' => '_blank'))); } else { $edit_device_icon = ''; } if (user_access('administer guifi users')) { $edit_ok_icon = drupal_get_form('_guifi_user_queue_device_form', $d); } else { $edit_ok_icon = $d['flag']; if (count($d['radios']) == 1 and $d['radios'][0]['mode'] == 'client') { $edit_ok_icon .= ' ' . $d['radios'][0]['mac']; } } $ip = guifi_main_ip($d['id']); $status_url = guifi_cnml_availability(array('device' => $d['id'], 'format' => 'short')); $rows[] = array($edit_device_icon . l($d['nick'], 'guifi/device/' . $d['id'], array('attributes' => array('target' => '_blank'))), array('data' => l($ip['ipv4'] . '/' . $ip['maskbits'], guifi_device_admin_url($d, $ip['ipv4']), array('attributes' => array('title' => t('Connect to the device on a new window'), 'target' => '_blank'))), 'align' => 'right'), array('data' => $edit_ok_icon, 'class' => $d['flag']), array('data' => $status_url, 'class' => $d['flag'])); } return $rows; }
function _guifi_zone_availability_devices($nid) { $oneyearfromnow = time() - '31622400'; $qry = db_query('SELECT d.id did, d.nick dnick, d.flag dflag, d.timestamp_changed changed ' . 'FROM {guifi_devices} d ' . 'WHERE d.type = "radio" ' . ' AND d.nid=%d ' . 'ORDER BY d.nick', $nid); $rows = array(); while ($d = db_fetch_array($qry)) { $dev = guifi_device_load($d['did']); if (guifi_device_access('update', $dev)) { $edit = l(guifi_img_icon('edit.png'), 'guifi/device/' . $d['did'] . '/edit', array('html' => TRUE, 'attributes' => array('title' => t('edit device'), 'target' => '_blank'))) . l(guifi_img_icon('drop.png'), 'guifi/device/' . $d['did'] . '/delete', array('html' => TRUE, 'attributes' => array('title' => t('delete device'), 'target' => '_blank'))); } else { $edit = NULL; } $ip = guifi_main_ip($d['did']); $status_url = guifi_cnml_availability(array('device' => $d['did'], 'format' => 'long')); if (!empty($d['changed'])) { if ($d['changed'] < $oneyearfromnow) { $dchanged = array('data' => '<b><font color="#AA0000">' . format_date($d['changed'], 'custom', t('d/m/Y')) . '</font></b>'); } else { $dchanged = array('data' => format_date($d['changed'], 'custom', t('d/m/Y'))); } } else { $dchanged = array('data' => t('never changed')); } $rows[] = array(array('data' => $edit . l($d['dnick'], 'guifi/device/' . $d['did'])), array('data' => l($ip['ipv4'] . '/' . $ip['maskbits'], guifi_device_admin_url($d['did'], $ip['ipv4']), array('attributes' => array('title' => t('Connect to the device on a new window'), 'target' => '_blank'))), 'align' => 'right'), array('data' => $d['dflag'] . $status_url, 'class' => $d['dflag']), $dchanged); } guifi_log(GUIFILOG_TRACE, 'function guifi_zone_availability_device()', $rows); return $rows; }