Пример #1
0
function guifi_ipcalc_get_subnet_by_nid($nid, $mask_allocate = '255.255.255.224', $network_type = 'public', $ips_allocated = NULL, $allocate = 'No', $verbose = FALSE)
{
    if (empty($nid)) {
        drupal_set_message(t('Error: trying to search for a network for unknown node or zone'), 'error');
        return;
    }
    if (empty($mask_allocate)) {
        drupal_set_message(t('Error: trying to search for a network of unknown size'), 'error');
        return;
    }
    if (empty($network_type)) {
        drupal_set_message(t('Error: trying to search for a network for unknown type'), 'error');
        return;
    }
    // print "Going to allocate network ".$mask_allocate."-".$network_type;
    global $user;
    $tbegin = microtime(TRUE);
    $zone = node_load(array('nid' => $nid));
    if ($zone->type == 'guifi_node') {
        $zone = guifi_zone_load($zone->zone_id);
    }
    $rzone = $zone;
    $depth = 0;
    $root_zone = $zone->id;
    $lbegin = microtime(TRUE);
    $search_mask = $mask_allocate;
    do {
        // while next is not the master, check within the already allocated ranges
        $result = db_query('SELECT n.id, n.base, n.mask ' . 'FROM {guifi_networks} n ' . 'WHERE n.zone = "%s" ' . '  AND network_type="%s" ' . 'ORDER BY n.id', $zone->id, $network_type);
        if ($verbose) {
            drupal_set_message(t('Searching if %mask is available at %zone, elapsed: %secs', array('%mask' => $mask_allocate, '%zone' => $zone->title, '%secs' => round(microtime(TRUE) - $lbegin, 4))));
        }
        // if there are already networks defined, increase network mask, up to /20 level
        // here, getting the total # of nets defined
        $tnets = 0;
        while ($net = db_fetch_object($result)) {
            $tnets++;
            $item = _ipcalc($net->base, $net->mask);
            // if looking for mesh ip (255.255.255.255) base address & broadcast
            // should be considered as used
            if ($search_mask == '255.255.255.255') {
                $ips_allocated[ip2long($net->base)] = 32;
                $ips_allocated[ip2long($item['netend'])] = 32;
            }
            if ($ip = guifi_ipcalc_find_subnet($net->base, $net->mask, $mask_allocate, $ips_allocated)) {
                if ($verbose) {
                    drupal_set_message(t('Found %ip/%rmask available at %netbase/%amask, got from %zone, elapsed: %secs', array('%amask' => $net->mask, '%netbase' => $net->base, '%ip' => $ip, '%rmask' => guifi_ipcalc_get_maskbits($mask_allocate), '%zone' => $zone->title, '%secs' => round(microtime(TRUE) - $lbegin, 4))));
                }
                // reserve the available range fount into database?
                if ($depth and ($allocate == 'Yes' and $network_type == 'public' and $mask_allocate != '255.255.255.255')) {
                    $msg = strip_tags(t('A new network (%base / %mask) has been allocated for zone %name, got from %name2 by %user.', array('%base' => $ip, '%mask' => $mask_allocate, '%name' => $rzone->title, '%name2' => $zone->title, '%user' => $user->name)));
                    $to_mail = explode(',', $rzone->notification);
                    $to_mail = explode(',', $zone->notification);
                    $nnet = array('new' => TRUE, 'base' => $ip, 'mask' => $mask_allocate, 'zone' => $root_zone, 'newtwork_type' => $network_type);
                    $nnet = _guifi_db_sql('guifi_networks', NULL, $nnet, $log, $to_mail);
                    guifi_notify($to_mail, $msg, $log);
                    drupal_set_message($msg);
                    if ($search_mask == '255.255.255.255') {
                        $ip = long2ip(ip2long($ip) + 1);
                    }
                }
                return $ip;
            }
        }
        // while there is a network defined at the zone
        // Network was not allocated
        if ($verbose) {
            drupal_set_message(t('Unable to find space at %zone, will look at parents, elapsed: %secs', array('%zone' => $zone->title, '%secs' => round(microtime(TRUE) - $lbegin, 4))));
        }
        // Need for an unused range,
        // already allocated networks from others than parents should be considered
        // as allocated ips (skipped)
        // This have to be done once, so do if is the zone being asked for
        if ($root_zone == $zone->id) {
            $parents = guifi_zone_get_parents($root_zone);
            $query = db_query('SELECT base ipv4, mask ' . 'FROM {guifi_networks} ' . 'WHERE zone NOT IN (' . implode(',', guifi_zone_get_parents($root_zone)) . ')');
            while ($nip = db_fetch_array($query)) {
                $ips_allocated[ip2long($nip['ipv4']) + 1] = guifi_ipcalc_get_maskbits($nip['mask']);
            }
            // once merged, sort
            ksort($ips_allocated);
            // calculating the needed mask
            if ($network_type == 'public') {
                $depth++;
                if ($tnets > 0 and $tnets < 5) {
                    // between 1 and 4, 24 - nets defined
                    $maskbits = 24 - $tnets;
                } else {
                    if ($tnets >= 5) {
                        // greater than 4, /20 - 255.255.240.0
                        $maskbits = 20;
                    } else {
                        // first net, /24 - 255.255.255.0
                        $maskbits = 24;
                    }
                }
                $mitem = _ipcalc_by_netbits($net->base, $maskbits);
                $mbits_allocate = guifi_ipcalc_get_maskbits($mask_allocate);
                if ($mbits_allocate > $maskbits or $mask_allocate == '255.255.255.255') {
                    $mask_allocate = $mitem['netmask'];
                }
            }
        }
        // Take a look at the parent network zones
        $master = $zone->master;
        if ($zone->master > 0) {
            $zone = guifi_zone_load($zone->master);
        }
    } while ($master > 0);
    return FALSE;
}
Пример #2
0
/**
 * 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']);
}
Пример #3
0
function guifi_device_delete($device, $notify = TRUE, $verbose = TRUE)
{
    global $user;
    guifi_log(GUIFILOG_TRACE, 'function guifi_device_delete()');
    $to_mail = explode(',', $device['notification']);
    if ($_POST['confirm']) {
        $log = _guifi_db_delete('guifi_devices', array('id' => $device['id']), $to_mail);
        drupal_set_message($log);
        $subject = t('The device %name has been DELETED by %user.', array('%name' => $device['nick'], '%user' => $user->name));
        drupal_set_message($subject);
        guifi_notify($to_mail, $subject, $log, $verbose, $notify);
        guifi_node_set_flag($device['nid']);
        drupal_goto('node/' . $device['nid']);
    }
    $node = node_load(array('nid' => $device['nid']));
    drupal_set_breadcrumb(guifi_node_ariadna($node));
    $output = drupal_get_form('guifi_device_delete_confirm', array('name' => $device['nick'], 'id' => $device['id']));
    print theme('page', $output, FALSE);
    return;
}
Пример #4
0
function budgets_supplier_save($node)
{
    global $user;
    guifi_log(GUIFILOG_TRACE, 'function budgets_save()', $node->ack);
    foreach ($node->caps as $cap1 => $cap2) {
        $elements = array();
        foreach ($cap2 as $key => $value) {
            if ($value and guifi_type_relation($cap1, $key, $node->role)) {
                $elements[] = $key . '=' . $value;
            }
        }
        $node->{$cap1} = implode(',', $elements);
    }
    foreach ($node->certs as $cap1 => $cap2) {
        $elements = array();
        foreach ($cap2 as $key => $value) {
            $value ? $elements[] = $key . '=' . $value : NULL;
        }
        $node->{$cap1} = implode(',', $elements);
    }
    $zones = array();
    foreach ($node->zones as $k => $value) {
        if ($value != '') {
            $zone_exploded = explode('-', $value);
            $zones[] = $zone_exploded[0];
        }
    }
    $node->zones = implode(',', $zones);
    $node->zone_id = $zones[0];
    $node->self_rating = $node->sr_commitment . $node->sr_experience;
    $node->official_rating = $node->or_commitment . $node->or_experience . $node->or_trend;
    $node->accounting_urls = serialize($node->accounting_urls);
    $to_mail = $user->mail;
    $log = '';
    $sid = _guifi_db_sql('supplier', array('id' => $node->nid), (array) $node, $log, $to_mail);
    if ($node->deleted) {
        $action = t('DELETED');
    } else {
        if ($node->new) {
            $action = t('CREATED');
        } else {
            $action = t('UPDATED');
        }
    }
    $subject = t('The supplier %title has been %action by %user.', array('%title' => $node->title, '%action' => $action, '%user' => $user->name));
    drupal_set_message($subject);
    guifi_notify($to_mail, $subject, $log);
}
Пример #5
0
function guifi_devel_paramusc_delete_confirm_submit($form, &$form_state)
{
    global $user;
    $depth = 0;
    if ($form_state['values']['op'] != t('Delete')) {
        return;
    }
    $to_mail = explode(',', $node->notification);
    $log = _guifi_db_delete('guifi_parametresConfiguracioUnsolclic', array('id' => $form_state['values']['id']), $to_mail, $depth);
    drupal_set_message($log);
    guifi_notify($to_mail, t('The USC Configuration parameter %parametre has been DELETED by %user.', array('%parametre' => $form_state['values']['nom'], '%user' => $user->name)), $log);
    drupal_goto('guifi/menu/devel/parameter');
}
Пример #6
0
/** guifi_node_delete(): deletes a given node
 **/
function guifi_node_delete($node)
{
    global $user;
    $depth = 0;
    $to_mail = explode(',', $node->notification);
    $log = _guifi_db_delete('guifi_location', array('id' => $node->nid), $to_mail, $depth);
    drupal_set_message($log);
    guifi_notify($to_mail, t('The node %name has been DELETED by %user.', array('%name' => $node->title, '%user' => $user->name)), $log);
    cache_clear_all();
    variable_set('guifi_refresh_cnml', time());
    variable_set('guifi_refresh_maps', time());
    return;
}
Пример #7
0
function budgets_quote_save($node)
{
    global $user;
    $to_mail = $user->mail;
    $log = '';
    if (isset($node->supplier)) {
        $sup = explode('-', $node->supplier);
        $node->supplier_id = $sup[0];
    }
    $node->expires = mktime(0, 0, 0, $node->arrexpires['month'], $node->arrexpires['day'], $node->arrexpires['year']);
    $sid = _guifi_db_sql('supplier_quote', array('id' => $node->nid), (array) $node, $log, $to_mail);
    if ($node->deleted) {
        $action = t('DELETED');
    } else {
        if ($node->new) {
            $action = t('CREATED');
        } else {
            $action = t('UPDATED');
        }
    }
    $subject = t('The supplier quote %title has been %action by %user.', array('%title' => $node->title, '%action' => $action, '%user' => $user->name));
    drupal_set_message($subject);
    guifi_notify($to_mail, $subject, $log);
}
Пример #8
0
/**
 * Remove a device from guifi.net
 *
 * @param GuifiAPI $gapi
 * @param mixed[] $parameters Parameters to remove the device (device_id, basically)
 * @return boolean Whether the device was removed or not
 */
function guifi_api_device_remove($gapi, $parameters)
{
    if (!guifi_api_check_fields($gapi, array('device_id'), $parameters)) {
        return FALSE;
    }
    global $user;
    $device = guifi_device_load($parameters['device_id']);
    if (!$device['id']) {
        $gapi->addError(500, "device_id = {$parameters['device_id']}");
        return FALSE;
    }
    guifi_log(GUIFILOG_TRACE, 'function guifi_device_delete()');
    $to_mail = explode(',', $device['notification']);
    $log = _guifi_db_delete('guifi_devices', array('id' => $device['id']), $to_mail);
    drupal_set_message($log);
    $subject = t('The device %name has been DELETED by %user.', array('%name' => $device['nick'], '%user' => $user->name));
    guifi_notify($to_mail, $subject, $log, $verbose, $notify);
    guifi_node_set_flag($device['nid']);
    return TRUE;
}
Пример #9
0
/**
 * Save changes to the database.
 */
function guifi_user_save($edit)
{
    global $user;
    $n = node_load($edit['nid']);
    $to_mail = $n->notification;
    $log = '';
    if (isset($edit['services'])) {
        if (isset($edit['services']['proxystr'])) {
            unset($edit['services']['proxystr']);
        }
        $edit['services'] = serialize($edit['services']);
    }
    if (isset($edit['var'])) {
        $edit['extra'] = serialize($edit['var']);
    }
    if (isset($edit['content_filters'])) {
        $edit['content_filters'] = serialize($edit['content_filters']);
    }
    if (isset($edit['username_created'])) {
        $cuser = user_load(array('name' => $edit['username_created']));
        $edit['user_created'] = $cuser->uid;
    }
    guifi_log(GUIFILOG_TRACE, 'function guifi_user_save()', $edit);
    _guifi_db_sql('guifi_users', array('id' => $edit['id']), $edit, $log, $to_mail);
    drupal_set_message(t('%user saved. Note that in some cases the change will not take effect until after some time.', array('%user' => $edit['username'])));
    guifi_notify($to_mail, t('The user !username has been UPDATED by !user.', array('!username' => $edit['username'], '!user' => $user->name)), $log);
}
Пример #10
0
function guifi_service_update($node)
{
    global $user;
    $log = '';
    $to_mail = explode(',', $node->notification);
    guifi_log(GUIFILOG_TRACE, 'function guifi_service_update()', $node);
    guifi_service_multiplefield_clean($node->var['domains']);
    guifi_service_multiplefield_clean($node->var['homepages']);
    guifi_service_multiplefield_clean($node->var['irc']);
    $node->extra = serialize($node->var);
    $nnode = _guifi_db_sql('guifi_services', array('id' => $node->id), (array) $node, $log, $to_mail);
    guifi_notify($to_mail, t('The service %name has been UPDATED by %user.', array('%name' => $node->nick, '%user' => $user->name)), $log);
}
Пример #11
0
/**
 * Save changes to a guifi network into the database.
 */
function guifi_ipv4_save($edit)
{
    global $user;
    if (!isset($edit['id'])) {
        $edit['new'] = TRUE;
        $msg = t('The network %base/%mask (%type) has been CREATED by %user.', array('%base' => $edit['base'], '%mask' => $edit['mask'], '%type' => $edit['network_type'], '%user' => $user->name));
    } else {
        $msg = t('The network %base/%mask (%type) has been UPDATED by %user.', array('%base' => $edit['base'], '%mask' => $edit['mask'], '%type' => $edit['network_type'], '%user' => $user->name));
    }
    $nnetwork = _guifi_db_sql('guifi_networks', array('id' => $edit['id']), (array) $edit, $log, $to_mail);
    guifi_notify($to_mail, $msg, $log);
}
Пример #12
0
/** guifi_zone_delete(): Delete a zone
**/
function guifi_zone_delete(&$node)
{
    global $user;
    $log = '';
    $delete = TRUE;
    $qn = db_fetch_object(db_query("\n    SELECT count(*) count\n    FROM {guifi_networks}\n    WHERE zone=%d", $node->nid));
    if ($qn->count) {
        drupal_set_message(t('FATAL ERROR: Can\'t delete a zone which have networks allocated. Database broken. Contact your system administrator'), 'error');
        $delete = FALSE;
    }
    $ql = db_fetch_object(db_query("\n    SELECT count(*) count\n    FROM {guifi_location}\n    WHERE zone_id=%d", $node->nid));
    if ($ql->count) {
        drupal_set_message(t('FATAL ERROR: Can\'t delete a zone with nodes. Database broken. Contact your system administrator'), 'error');
        $delete = FALSE;
    }
    $to = explode(',', $node->notification);
    $to[] = variable_get('guifi_contact', '*****@*****.**');
    if (!$delete) {
        $messages = drupal_get_messages(NULL, FALSE);
        guifi_notify($to, t('ALERT: Zone %nick-%name has been deleted, but have errors:', array('%nick' => $node->nick, '%name' => $node->title)), implode("\n", $messages['error']));
        return;
    }
    // perform deletion
    $node->deleted = TRUE;
    $nzone = _guifi_db_sql('guifi_zone', array('id' => $node->id), (array) $node, $log, $to);
    guifi_notify($to, t('Zone %nick-%name has been deleted', array('%nick' => $node->nick, '%name' => $node->title)), $log);
    cache_clear_all();
    variable_set('guifi_refresh_cnml', time());
    variable_set('guifi_refresh_maps', time());
    return;
}