function guifi_device_form_validate($form, &$form_state)
{
    global $user;
    guifi_log(GUIFILOG_TRACE, 'function guifi_device_form_validate()', $form_state['values']);
    guifi_log(GUIFILOG_TRACE, 'function guifi_device_form_validate(vlans)', $form_state['values'][vlans]);
    guifi_log(GUIFILOG_TRACE, 'function guifi_device_form_validate(aggregations)', $form_state['values'][aggregations]);
    guifi_log(GUIFILOG_TRACE, 'function guifi_device_form_validate(ipv4)', $form_state['values'][ipv4]);
    // nick
    if (isset($form['main']['nick'])) {
        guifi_validate_nick($form_state['values']['nick']);
        $query = db_query("\n      SELECT nick\n      FROM {guifi_devices}\n      WHERE lcase(nick)=lcase('%s')\n       AND id <> %d", strtolower($form_state['values']['nick']), $form_state['values']['id']);
        while (db_fetch_object($query)) {
            form_set_error('nick', t('Nick already in use.'));
        }
    }
    // ssid
    if (empty($form_state['values']['ssid'])) {
        $form_state['values']['ssid'] = $form_state['values']['nick'];
    }
    // Validate ip address(es)
    // Finding duplicates
    $ips = array();
    if (!empty($form_state['values']['ipv4'])) {
        // first checking local IPs
        foreach ($form_state['values']['ipv4'] as $keyS => $valueS) {
            if (empty($valueS[ipv4])) {
                continue;
            }
            // duplicate ip?
            if (in_array($valueS[ipv4], $ips)) {
                form_set_error("ipv4][{$keyS}][ipv4", t('address %addr is duplicated', array('%addr' => $valueS[ipv4])));
            }
            $ips[] = $valueS[ipv4];
            $valueSIPCalc = _ipcalc($valueS[ipv4], $valueS[netmask]);
            // Now looking into remote IPs
            foreach ($valueS[subnet] as $keyI => $valueI) {
                if (empty($valueI[ipv4])) {
                    continue;
                }
                guifi_log(GUIFILOG_TRACE, 'function guifi_device_form_validate(ipv4s)', $valueS[ipv4] . ' / ' . $valueI[ipv4] . ' ' . $valueI[did]);
                // duplicate ip?
                if (in_array($valueI[ipv4], $ips)) {
                    if ($valueI['new']) {
                        $field = "ipv4][{$keyS}][subnet][{$keyI}][ipv4txt";
                    } else {
                        $field = "ipv4][{$keyS}][subnet][{$keyI}][ipv4";
                    }
                    form_set_error($field, t('address %addr is duplicated', array('%addr' => $valueI[ipv4])));
                }
                $ips[] = $valueI[ipv4];
                // same subnet as related IP?
                $valueIIPCalc = _ipcalc($valueI[ipv4], $valueS[netmask]);
                if ($valueSIPCalc[netid] != $valueIIPCalc[netid] or $valueSIPCalc[maskbits] != $valueIIPCalc[maskbits]) {
                    form_set_error("ipv4][{$keyS}][subnet][{$keyI}][ipv4", t('address %addr1 not at same subnet as %addr2', array('%addr1' => $valueI[ipv4], '%addr2' => $valueS[ipv4])));
                }
                // remote id should be populated
                if (empty($valueI[did])) {
                    form_set_error("ipv4][{$keyS}][subnet][{$keyI}][did", t('Remote device for address %addr1 not specified', array('%addr1' => $valueI[ipv4])));
                }
            }
            // for remote IPs
        }
        // for local IPs
        /*   if (db_affected_rows(db_query("
              SELECT i.id
              FROM {guifi_interfaces} i,{guifi_ipv4} a
              WHERE i.id=a.interface_id AND a.ipv4='%s'
                AND i.device_id != %d",
              $form_state['values']['ipv4'],
              $form_state['values']['id']))) {
              $message = t('IP %ipv4 already taken in the database. Choose another or leave the address blank.',
                array('%ipv4' => $form_state['values']['ipv4']));
              form_set_error('ipv4',$message); 
            } */
    }
    // Validating vlans & aggregations
    foreach (array('vlans', 'aggregations') as $vtype) {
        foreach ($form_state['values'][$vtype] as $kvlan => $vlan) {
            // interface_type (name) should have a value
            if (empty($vlan['interface_type']) and !form_get_errors()) {
                $vlan['interface_type'] = substr($vtype, 0, 4) . $kvlan;
                form_set_value(array('#parents' => array($vtype, $kvlan, 'interface_type')), $vlan['interface_type'], $form_state);
                $form_state['rebuild'] = TRUE;
                drupal_set_message(t('Setting interface name to %name', array('%name' => $vlan['interface_type'])), 'warning');
            }
            // parent should exists
            if (empty($vlan['related_interfaces'])) {
                form_set_error("{$vtype}][{$kvlan}][related_interfaces", t('%name should have related interface(s)', array('%name' => $vlan[interface_type])));
            }
        }
    }
    // foreach vlans, aggregations
    // No duplicate names on interface names
    $ifs = guifi_get_currentInterfaces($form_state['values']);
    $iChecked = array();
    foreach ($ifs as $k => $iname) {
        if (in_array($iname, $iChecked)) {
            guifi_log(GUIFILOG_TRACE, 'function guifi_device_form_validate()', $iname);
            foreach (array('interfaces', 'vlans', 'aggregations', 'tunnels') as $iClass) {
                if (isset($form_state[values][$iClass][$k])) {
                    $f = $iClass . "][{$k}][interface_type";
                }
                form_set_error($f, t('Interface name %name duplicated', array('%name' => $iname)));
            }
        }
        $iChecked[] = $iname;
    }
    if (count($form_state['values']['radios'])) {
        foreach ($form_state['values']['radios'] as $k => $v) {
            guifi_radios_validate($k, $v, $form_state['values']);
        }
    }
    guifi_maintainers_validate(array2object($form_state['values']));
    guifi_funders_validate(array2object($form_state['values']));
}
Beispiel #2
0
/**
 *
 * @param GuifiAPI $gapi GuifiAPI object
 *
 * @param mixed[] $parameters
 *
 * @return
 */
function _guifi_api_device_check_parameters($gapi, &$parameters)
{
    extract($parameters);
    if (isset($status)) {
        if (guifi_validate_types('status', $status)) {
            $parameters['flag'] = $status;
        } else {
            $gapi->addError(403, "status: {$status}");
            return FALSE;
        }
    }
    if (isset($mac)) {
        if (!_guifi_validate_mac($mac)) {
            $gapi->addError(403, "mac: {$mac}");
            return FALSE;
        }
    }
    if (!empty($nick)) {
        guifi_validate_nick($nick);
        if ($errors = form_get_errors()) {
            foreach ($errors as $err) {
                $gapi->addError(403, $err);
            }
            return FALSE;
        }
        $query = db_query("SELECT nick FROM {guifi_devices} WHERE lcase(nick) = lcase('%s') AND id != %d", strtolower($nick), intval($parameters['device_id']));
        while (db_fetch_object($query)) {
            $gapi->addError(403, 'nick already in use');
            return FALSE;
        }
    }
    switch ($type) {
        case 'radio':
            if (!guifi_api_check_fields($gapi, array('mac', 'model_id', 'firmware'), $parameters)) {
                return FALSE;
            }
            $model = db_fetch_object(db_query("SELECT model name FROM {guifi_model_specs} WHERE mid = '%d' LIMIT 1", $model_id));
            if (!guifi_validate_types('firmware', $firmware, $model->name)) {
                $gapi->addError(403, "firmware is not supported: {$firmware}");
                return FALSE;
            }
            break;
        case 'mobile':
            break;
        case 'server':
            break;
        case 'nat':
            break;
        case 'generic':
            break;
        case 'adsl':
            if (!guifi_api_check_fields($gapi, array('download', 'upload', 'mrtg_index'), $parameters)) {
                return FALSE;
            }
            break;
        case 'cam':
            break;
        case 'phone':
            break;
    }
    return TRUE;
}
/** guifi_node_validate(): Confirm that an edited guifi item has fields properly filled in.
 */
function guifi_node_validate($node, $form)
{
    guifi_validate_nick($node->nick);
    // not at root zone
    if ($node->zone_id == 0 or $node->zone_id == guifi_zone_root()) {
        form_set_error('zone_id', t('Can\'t be assigned to root zone, please assign the node to an appropiate zone.'));
    } else {
        $nz = 0;
        guifi_zone_childs_tree_depth($node->zone_id, 3, $nz);
        if ($nz > 2) {
            form_set_error('zone_id', t('Can\'t be assigned to parent zone, please assign the node to an final zone.'));
        }
    }
    if ($node->elevation == 0) {
        $node->elevation = NULL;
    }
    if ($node->elevation < -1 && $node->elevation != NULL) {
        form_set_error('elevation', t('Elevation must be above the floor! :)'));
    }
    if ($node->elevation > 261 && $node->elevation != NULL) {
        form_set_error('elevation', t('Do you mean that you are flying over the earth??? :)'));
    }
    /*
     * Validate maintainer(s)
     */
    guifi_maintainers_validate($node);
    /*
     * Validate funder(s)
     */
    guifi_funders_validate($node);
}
function guifi_service_nick_validate($element, &$form_state)
{
    if (empty($element['#value'])) {
        $nick = guifi_abbreviate($form_state['values']['title']);
        drupal_set_message(t('Service nick has been set to:') . ' ' . $nick);
        $form_state['values']['nick'] = $nick;
        return;
    }
    guifi_validate_nick($element['#value']);
    $query = db_query("SELECT nick FROM {guifi_services} WHERE lcase(nick)='%s' AND id <> %d", strtolower($element['#value']), $form_state['values']['nid']);
    if (db_result($query)) {
        form_set_error('nick', t('Nick already in use.'));
    }
}