function _guifi_radio_prepare_add_radio($edit)
{
    // next id
    $rc = 0;
    // Radio radiodev_counter next pointer
    $tc = 0;
    // Total active radios
    // fills $rc & $tc proper values
    if (isset($edit['radios'])) {
        foreach ($edit['radios'] as $k => $r) {
            if ($k + 1 > $rc) {
                $rc = $k + 1;
                if (!$edit['radios'][$k]['delete']) {
                    $tc++;
                }
            }
        }
    }
    $node = node_load(array('nid' => $edit['nid']));
    $zone = node_load(array('nid' => $node->zone_id));
    if ($zone->nick == '' or $zone->nick == NULL) {
        $zone->nick = guifi_abbreviate($zone->nick);
    }
    if (strlen($zone->nick . $edit['nick']) > 10) {
        $nick = guifi_abbreviate($edit['nick']);
    } else {
        $nick = $edit['nick'];
    }
    $ssid = $zone->nick . $nick;
    $radio = _guifi_radio_get_default();
    $radio['id'] = $edit['id'];
    $radio['nid'] = $edit['nid'];
    $radio['mode'] = $edit['newradio_mode'];
    switch ($radio['mode']) {
        case 'ap':
            $radio['antenna_angle'] = 120;
            $radio['clients_accepted'] = "Yes";
            $radio['ssid'] = $ssid . 'AP' . $rc;
            $radio['mac'] = '';
            break;
        case 'client':
        case 'client-routed':
            $radio['antenna_angle'] = 30;
            $radio['clients_accepted'] = "No";
            $radio['ssid'] = $ssid . 'CPE' . $rc;
            $radio['interfaces'][0]['new'] = TRUE;
            $radio['interfaces'][0]['interface_type'] = 'Wan';
            $radio['mac'] = '';
            break;
        case 'mesh':
            $radio['antenna_angle'] = 0;
            $radio['clients_accepted'] = "No";
            $radio['ssid'] = $ssid . t('MESH');
            // first radio, force wlan/Lan bridge and get an IP
            if ($tc == 0) {
                $radio['interfaces'][1] = array();
                $radio['interfaces'][1]['new'] = TRUE;
                $radio['interfaces'][1]['interface_type'] = 'wLan/Lan';
                $ips_allocated = guifi_ipcalc_get_ips('0.0.0.0', '0.0.0.0', $edit, 1);
                // $net = guifi_ipcalc_get_meship($edit['nid'],$ips_allocated);
                $net = guifi_ipcalc_get_subnet_by_nid($edit['nid'], '255.255.255.224', 'mesh', $ips_allocated, 'Yes', TRUE);
                guifi_log(GUIFILOG_BASIC, "IPS allocated: " . count($ips_allocated) . " got net: " . $net . '/27');
                $radio['interfaces'][1]['ipv4'][$rc] = array();
                $radio['interfaces'][1]['ipv4'][$rc]['new'] = TRUE;
                $radio['interfaces'][1]['ipv4'][$rc]['ipv4_type'] = 1;
                //$radio['interfaces'][1]['ipv4'][$rc]['ipv4'] = $net;
                $radio['interfaces'][1]['ipv4'][$rc]['ipv4'] = long2ip(ip2long($net) + 1);
                guifi_log(GUIFILOG_TRACE, "Assigned IP: " . $radio['interfaces'][1]['ipv4'][$rc]['ipv4']);
                $radio['interfaces'][1]['ipv4'][$rc]['netmask'] = '255.255.255.224';
            }
            $radio['mac'] = '';
            break;
    }
    $radio['rc'] = $rc;
    return $radio;
}
Example #2
0
function guifi_api_service_add($gapi, $parameters)
{
    global $user;
    if (!guifi_api_check_fields($gapi, array('name', 'server_id', 'service_type'), $parameters)) {
        return FALSE;
    }
    //Check if this service exist
    $server_type = $parameters['service_type'];
    $types = db_fetch_object(db_query("SELECT count(*) as cnt FROM {guifi_types} WHERE type='service' AND text='%s'", $server_type));
    if ($types->cnt == 0) {
        //This server type is not defined in guifi_db.
        $gapi->addError(403, "This server type is not defined.");
        return FALSE;
    }
    $server = guifi_device_load($parameters['server_id']);
    if (!is_array($server)) {
        //This server did not exist.
        $gapi->addError(403, "This server did not exist.");
        return FALSE;
    }
    //Need create drupal node (type guifi_service)
    global $user;
    $node = new stdClass();
    $node->type = 'guifi_service';
    $node->name = $user->name;
    $node->uid = $user->uid;
    $node->comment = variable_get('comment_' . $node->type, 2);
    $node->status = 1;
    $node->format = FILTER_FORMAT_DEFAULT;
    $node->title = $parameters['name'];
    $node = node_submit($node);
    $status_flag = isset($parameters['status']) ? $parameters['status'] : 'Working';
    $types = db_fetch_object(db_query("SELECT count(*) as cnt FROM {guifi_types} WHERE type='status' AND text='%s'", $status_flag));
    if ($types->cnt == 0) {
        //This status_flag is not defined in guifi_db.
        $gapi->addError(403, "This status is not defined.");
        return FALSE;
    }
    $service = array('new' => TRUE, 'title' => $parameters['name'], 'service_type' => $server_type, 'status_flag' => $status_flag, 'notification' => $user->mail, 'device_id' => $server['id']);
    // Nick assinged
    $nick = !isset($parameters['nick']) ? guifi_abbreviate($parameters['name']) : $parameters['nick'];
    $nick = _guifi_api_check_nick($nick, $node);
    if (is_array($nick)) {
        $gapi->addError($nick['error_code'], $nick['error_message']);
        return FALSE;
    }
    $service['nick'] = $nick;
    // Determine Zone
    $zone = _guifi_api_get_zone($server);
    if (is_array($zone)) {
        $gapi->addError($zone['error_code'], $zone['error_message']);
        return FALSE;
    }
    $service['zone_id'] = $zone;
    // Pass all parameters of service to node.
    foreach ($service as $k => $v) {
        $node->{$k} = $v;
    }
    $node->name = $node->title;
    node_save($node);
    $nservice = _guifi_db_sql('guifi_services', array('id' => $service->id), (array) $node);
    $gapi->addResponseField('service', $nservice);
    return TRUE;
}
Example #3
0
function guifi_node_nick_validate($element, &$form_state)
{
    if (empty($element['#value'])) {
        $nick = guifi_abbreviate($form_state['values']['title']);
        drupal_set_message(t('Zone nick has been set to:') . ' ' . $nick);
        $form_state['values']['nick'] = $nick;
        return;
    }
    guifi_validate_nick($element['#value']);
    $query = db_query("SELECT nick FROM {guifi_location} 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.'));
    }
}
Example #4
0
function guifi_zone_nick_validate($element, &$form_state)
{
    if (empty($element['#value'])) {
        $nick = guifi_abbreviate($form_state['values']['title']);
        drupal_set_message(t('Zone nick has been set to:') . ' ' . $nick);
        $form_state['values']['nick'] = $nick;
    }
}