Esempio n. 1
0
function guifi_ahah_add_remoteipv4()
{
    $SNet = arg(3);
    $ipv4 = $_POST['ipv4'][$SNet];
    //  $tree = array('ipv4',$SNetId);
    guifi_log(GUIFILOG_TRACE, sprintf('guifi_ahah_add_remoteipv4 (id=%d):', $SNet), $ipv4[subnet]);
    // find the next free ip address
    $deletedPresent = false;
    $ips = array(ip2long($ipv4[ipv4]));
    foreach ($ipv4[subnet] as $ki => $i) {
        if ($ki != 'add-ipv4') {
            if ($i['deleted'] == true) {
                $deletedPresent = true;
            }
        }
        if (empty($i[ipv4]) and !empty($i[ipv4value])) {
            $ipv4[subnet][$ki][ipv4] = $i[ipv4] = $i[ipv4value];
        }
        if ($ip = ip2long($i[ipv4])) {
            if (!in_array($ip, $ips)) {
                $ips[] = $ip;
            }
        }
    }
    sort($ips);
    guifi_log(GUIFILOG_BASIC, sprintf('guifi_ahah_add_remoteipv4 (id=%d):', $SNet), $ips);
    $ipc = _ipcalc($ipv4[ipv4], $ipv4[netmask]);
    $lstart = ip2long($ipc[netstart]);
    $c = 0;
    while ($ips[$c] == $lstart + $c) {
        $c++;
    }
    $free = long2ip($lstart + $c);
    // Create new element
    $newI = array('new' => true, 'ipv4' => $free, 'netmask' => $ipv4['netmask']);
    if ($deletedPresent == true) {
        drupal_set_message(t('Deleted addresses found. To reuse them you must save first to confirm changes.'), 'warning');
    }
    drupal_set_message(t('Next available address at %base/%maskbits is %addr', array('%addr' => $free, '%base' => $ipc[netid], '%maskbits' => $ipc[maskbits])));
    guifi_log(GUIFILOG_TRACE, sprintf('guifi_ahah_add_remoteipv4 (start=%s) new', long2ip($lstart)), $newI);
    guifi_log(GUIFILOG_TRACE, sprintf('guifi_ahah_add_remoteipv4 ipcalc'), $ipc);
    $k = 0;
    while (!empty($ipv4[subnet][$k][ipv4])) {
        $k++;
    }
    $ipv4[subnet][$k] = $newI;
    guifi_log(GUIFILOG_FULL, sprintf('guifi_ahah_add_remoteipv4 (id=%d):', $SNet), $ipv4[subnet]);
    // Build our new form element.
    $form_element = guifi_ipv4subnet_form($ipv4, $SNet, true);
    // Build the new form.
    $form_state = array('submitted' => FALSE);
    $form_build_id = $_POST['form_build_id'];
    // Add the new element to the stored form. Without adding the element to the
    // form, Drupal is not aware of this new elements existence and will not
    // process it. We retreive the cached form, add the element, and resave.
    $form = form_get_cache($form_build_id, $form_state);
    guifi_log(GUIFILOG_TRACE, sprintf('guifi_ahah_add_remoteipv4 (id=%d) state:', $SNet), $form_state);
    $choice_form = $form[ipv4][$SNet]['subnet'];
    $form['ipv4'][$SNet]['subnet'] = $form_element;
    $choice_form = $form[ipv4][$SNet]['subnet'];
    form_set_cache($form_build_id, $form, $form_state);
    $form += array('#post' => $_POST, '#programmed' => FALSE);
    // Rebuild the old form.
    $form = form_builder('guifi_device_form', $form, $form_state);
    // Render the new output.
    $choice_form = $form[ipv4][$SNet][subnet];
    unset($choice_form['#prefix'], $choice_form['#suffix']);
    // Prevent duplicate wrappers.
    //unset($choice_form[$delta]);
    // build new form
    //  $fs = array();
    //  $form_element['#post'] = array();
    //  $form_element = form_builder($form_element['form_id']['#value'] , $form_element, $fs);
    //  $newfields = drupal_render($form_element);
    //  guifi_log(GUIFILOG_BASIC,sprintf('choice_form %d',$delta),htmlspecialchars($newfield));
    $output = theme('status_messages') . drupal_render($choice_form);
    drupal_json(array('status' => TRUE, 'data' => $output));
    exit;
}
Esempio n. 2
0
function guifi_ipv4i_form($ipv4, $k, $first_port = true, $eInterfaces)
{
    //  if (empty($ipv4[ipv4]))
    //    return;
    guifi_log(GUIFILOG_TRACE, 'function guifi_ipv4i_form (ipv4)', $ipv4);
    $prefix = '';
    $suffix = '';
    $form = array('#type' => 'fieldset', '#parents' => array('ipv4', $k), '#attributes' => array('class' => 'fieldset-interface-port'), '#prefix' => '<div id="fieldset-ipv4-' . $k . '">', '#suffix' => '</div>', '#tree' => TRUE);
    $form['ipv4'] = array('#type' => 'textfield', '#title' => $first_port ? t('IPv4 address') : false, '#default_value' => $ipv4[ipv4], '#size' => 24, '#maxlength' => 16);
    $form['netmask'] = array('#type' => 'select', '#title' => $first_port ? t("Mask") : false, '#default_value' => $ipv4['netmask'], '#options' => guifi_types('netmask', 30, 0));
    $iid_eInterfaces = array();
    foreach ($eInterfaces as $key => $iface) {
        if (strpos($key, ',')) {
            continue;
        }
        $radio_iid = db_fetch_array(db_query("SELECT * FROM {guifi_interfaces} WHERE id = %d", $key));
        if ($radio_iid['interface_type'] === 'wds/p2p') {
            if (empty($radio_iid['interface_class'])) {
                $iface = 'WDS-wlan' . ($radio_iid['radiodev_counter'] + 1);
            } else {
                $iface = $radio_iid['interface_class'];
            }
        }
        if ($radio_iid['interface_type'] === 'wLan') {
            $iface = 'wlan' . ($radio_iid['radiodev_counter'] + 1);
        }
        $iid_eInterfaces[$key] = $iface;
    }
    $form['id'] = array('#type' => 'hidden', '#value' => $ipv4['id']);
    $form['interface_id'] = array('#type' => 'select', '#title' => $first_port ? t('interface') : false, '#options' => $iid_eInterfaces, '#default_value' => $ipv4['interface_id']);
    /* Commenting from now: I was unable to do it successfully with ahah :() 
     $form['esubnet'] = array(
           '#type' => 'image_button',
           '#src'  => drupal_get_path('module', 'guifi').'/icons/Cable-Network-16-edit.png',
           '#attributes' => array(
              'title' => t('Edit subnetwork members').' - '.
                (count($ipv4[subnet]) + 1).
                ' '.t('address(es)'),
            ),
           '#ahah' => array(
             'path' => 'guifi/js/edit_subnet/'.$k,
             'wrapper' => 'fieldset-ipv4subnet-'.$k,
             'method' => 'replace',
             'effect' => 'fade',
            ),
            '#prefix' => ($first_port) ? '<div class="form-item"><div>&nbsp</div>':'<div class="form-item">',
            '#suffix' => '</div>',
            '#weight' => $form_weight++,
         ); 
      $form['nsnets'] = array(
        '#type'  => 'item',
        '#value' => (count($ipv4[subnet]) + 1).' '.t('address(es)'),
        '#title' => ($first_port) ? '&nbsp;' : false,
      ); 
      /* */
    if (!$ipv4[deleted]) {
        $form['delete'] = array('#type' => 'image_button', '#title' => $first_port ? t('delete') : false, '#src' => drupal_get_path('module', 'guifi') . '/icons/drop.png', '#attributes' => array('title' => t('Delete address')), '#submit' => array('guifi_ipv4i_delete_submit'), '#prefix' => $first_port ? '<div class="form-item"><label>&nbsp</label>' : false, '#suffix' => $first_port ? '</div>' : false);
    }
    // Subnet members
    $form['subnet'] = guifi_ipv4subnet_form($ipv4, $k, false);
    // guifi_form_hidden($form['snet'],$ipv4[subnet]);
    /*  if (!$ipv4[deleted])
      $form['delete'] = array(
        '#type' => 'image_button',
        '#title' => ($first_port) ? t('delete') : false,
        '#src' => drupal_get_path('module', 'guifi').'/icons/drop.png',
        '#attributes' => array('title' => t('Delete ipv4')),
        '#submit' => array('guifi_ipv4i_delete_submit'),
        '#prefix' => ($first_port) ?
          '<div class="form-item"><label>&nbsp</label>' : false,
        '#suffix' => ($first_port) ?
          '</div>' : false,
      );
    */
    // Hidden fields
    $form['id'] = array('#type' => 'hidden', '#value' => $ipv4['id']);
    $form['ipv4_type'] = array('#type' => 'hidden', '#value' => $ipv4['ipv4_type']);
    $form['zone_id'] = array('#type' => 'hidden', '#value' => $ipv4['zone_id']);
    if ($ipv4['deleted']) {
        $form['deleted'] = array('#value' => $ipv4['deleted']);
    }
    if ($ipv4['new']) {
        $form['new'] = array('#type' => 'hidden', '#value' => $ipv4['new']);
    }
    return $form;
}