function guifi_device_create_form($form_state, $node)
{
    guifi_log(GUIFILOG_TRACE, 'function guifi_device_create_form()', $node);
    $types = guifi_types('device');
    $zone = guifi_zone_load($node->zone_id);
    if (!guifi_node_access('create', $node->nid)) {
        $form['text_add'] = array('#type' => 'item', '#value' => t('You are not allowed to update this node.'), '#weight' => 0);
        return $form;
    }
    $form['nid'] = array('#type' => 'hidden', '#value' => $node->id);
    $form['device_type'] = array('#type' => 'select', '#title' => t('Add a new device'), '#description' => t('Type of device to be created'), '#options' => $types, '#prefix' => '<table style="width: 0px"><tr><td>', '#suffix' => '</td>');
    $form['submit'] = array('#type' => 'submit', '#value' => t('add'), '#prefix' => '<td>', '#suffix' => '</td></tr></table>');
    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 guifi_users_queue($zone)
{
    function _guifi_user_queue_device_form($form_state, $d = array())
    {
        guifi_log(GUIFILOG_TRACE, 'function guifi_user_queue_device_form()', $d);
        if (count($d['radios']) != 1) {
            return;
        }
        if ($d['radios'][0]['mode'] != 'client') {
            return;
        }
        if (!isset($d['radios'][0]['interfaces'])) {
            return;
        }
        $iid = key($d['radios'][0]['interfaces']);
        if (!isset($d['radios'][0]['interfaces'][$iid]['ipv4'][0]['links'])) {
            return;
        }
        $lid = key($d['radios'][0]['interfaces'][$iid]['ipv4'][0]['links']);
        if (empty($iid) or empty($lid)) {
            return;
        }
        if (empty($form_state['values'])) {
            $form_state['values'] = $d;
        }
        $f['flag'] = array('#type' => 'item', '#value' => $form_state['values']['flag'], '#prefix' => '<table><tr><td>', '#suffix' => '</td>');
        $f['mac'] = array('#type' => 'textfield', '#required' => TRUE, '#size' => 17, '#maxlength' => 17, '#default_value' => $form_state['values']['radios'][0]['mac'], '#element_validate' => array('guifi_mac_validate'), '#prefix' => '<td>', '#suffix' => '</td>');
        $f['did'] = array('#type' => 'hidden', '#value' => $form_state['values']['id']);
        $f['nid'] = array('#type' => 'hidden', '#value' => $form_state['values']['nid']);
        $f['uid'] = array('#type' => 'hidden', '#value' => $form_state['values']['uid']);
        $f['iid'] = array('#type' => 'hidden', '#value' => $iid);
        $f['lid'] = array('#type' => 'hidden', '#value' => $lid);
        $f['approve'] = array('#type' => 'image_button', '#src' => drupal_get_path('module', 'guifi') . '/icons/ok.png', '#attributes' => array('title' => t('Set the device and link Online, confirm MAC & approve user.')), '#prefix' => '<td>', '#suffix' => '</td></tr></table>');
        return $f;
    }
    function _guifi_user_queue_form($form_state, $params = array())
    {
        guifi_log(GUIFILOG_TRACE, 'function guifi_user_queue_form()', $params);
        if (empty($form_state['values'])) {
            $form_state['values'] = $params;
        }
        $f['status'] = array('#type' => 'select', '#options' => guifi_types('user_status'), '#default_value' => $form_state['values']['status'], '#prefix' => '<table><tr><td>', '#suffix' => '</td>');
        $f['uid'] = array('#type' => 'hidden', '#value' => $form_state['values']['id']);
        $f['saveUser'] = array('#type' => 'image_button', '#src' => drupal_get_path('module', 'guifi') . '/icons/save.png', '#attributes' => array('title' => t('Change & Save users Status.')), '#submit' => array('_guifi_user_queue_device_form_submit'), '#prefix' => '<td>', '#suffix' => '</td></tr></table>');
        return $f;
    }
    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;
    }
    global $user;
    $owner = $user->uid;
    guifi_log(GUIFILOG_TRACE, 'function guifi_users_node_list()', $zone);
    drupal_set_breadcrumb(guifi_zone_ariadna($zone->id, 'node/%d/view/userqueue'));
    $title = t('Queue of pending users @') . ' ' . $zone->title;
    drupal_set_title($title);
    $childs = guifi_zone_childs($zone->id);
    $childs[] = $zone->id;
    $sql = 'SELECT ' . '  u.*, l.id nid, l.nick nnick, l.status_flag nflag, l.zone_id ' . 'FROM {guifi_users} u, {guifi_location} l ' . 'WHERE u.nid=l.id' . '  AND (l.status_flag != "Working" OR u.status != "Approved") ' . '  AND l.zone_id IN (' . implode(',', $childs) . ') ' . 'ORDER BY FIND_IN_SET(u.status,"New,Pending,Approved,Rejected"),' . '  u.timestamp_created';
    $query = pager_query($sql, variable_get("guifi_pagelimit", 50));
    $rows = array();
    $nrow = 0;
    if (user_access('administer guifi networks') || user_access('administer guifi zones') || user_access('administer guifi users')) {
        $administer = TRUE;
    } else {
        $administer = FALSE;
    }
    while ($u = db_fetch_array($query)) {
        $pUser = (object) guifi_user_load($u['id']);
        $proxy = node_load(array('nid' => $pUser->services['proxy']));
        $srows = _guifi_user_queue_devices($u);
        $nsr = count($srows);
        if (empty($nsr)) {
            $nsr = 1;
        }
        $node = node_load(array('nid' => $u['nid']));
        if (guifi_node_access('update', $node)) {
            $edit_node_icon = l(guifi_img_icon('edit.png'), 'node/' . $u['nid'] . '/edit', array('html' => TRUE, 'attributes' => array('target' => '_blank'))) . l(guifi_img_icon('drop.png'), 'node/' . $u['nid'] . '/delete', array('html' => TRUE, 'attributes' => array('target' => '_blank')));
        } else {
            $edit_node_icon = '';
        }
        if (guifi_user_access('update', $u)) {
            $edit_user_icon = l(guifi_img_icon('edit.png'), 'guifi/user/' . $u['id'] . '/edit', array('html' => TRUE, 'attributes' => array('target' => '_blank'))) . l(guifi_img_icon('drop.png'), 'guifi/user/' . $u['id'] . '/delete', array('html' => TRUE, 'attributes' => array('target' => '_blank')));
        } else {
            $edit_user_icon = '';
        }
        if ($administer) {
            $edit_user_form = drupal_get_form('_guifi_user_queue_form', $u);
        } else {
            $edit_user_form = $u['status'];
        }
        if (user_access('administer guifi users') or $node->uid == $owner) {
            $rn3 = $u['lastname'] . ", " . $u['firstname'];
            $u3 = $u['username'];
        } else {
            $rn3 = ereg_replace("_", " ", $u['lastname'] . ", " . $u['firstname']);
            if (strlen($rn3) > 3) {
                $rn3 = substr($u['lastname'] . ", " . $u['firstname'], 0, 3);
                $rn3 .= "..";
            }
            $u3 = $u['username'];
            if (strlen($u3) > 3) {
                $u3 = substr($u['username'], 0, 5);
                $u3 .= "..";
            }
        }
        $rows[] = array(array('data' => $edit_user_icon . l($u3, 'node/' . $u['nid'] . '/view/users', array('attributes' => array('title' => $rn3, 'target' => '_blank'))) . "\n<br />" . '<small>' . format_date($u['timestamp_created']) . '<br />' . l($proxy->nick, "node/" . $proxy->id, array('attributes' => array('title' => $proxy->title))), 'rowspan' => $nsr), array('data' => guifi_get_zone_nick($u['zone_id']) . "<br /><strong>" . $edit_node_icon . l($u['nnick'], 'node/' . $u['nid'], array('html' => TRUE, 'attributes' => array('target' => '_blank'))) . '</strong><br /><small>' . l(t('add a comment'), 'comment/reply/' . $u['nid'], array('fragment' => 'comment-form', 'html' => TRUE, 'attributes' => array('title' => t('Add a comment to the page of this node'), 'target' => '_blank'))) . '</small>', 'class' => $u['nflag'], 'rowspan' => $nsr), array('data' => $edit_user_form, 'rowspan' => $nsr));
        end($rows);
        $krow = key($rows);
        if (count($srows)) {
            // merge current row with first element
            $rows[$krow] = array_merge($rows[$krow], array_shift($srows));
            // adding rest og the elements
            foreach ($srows as $k => $v) {
                $rows[] = $v;
            }
        }
    }
    $header = array(t('Username'), t('Node'), t('User status'), t('Device'), t('IP v4 address'), t('Status & MAC'), t('Current status'));
    $output .= theme('table', $header, $rows);
    $output .= theme_pager(NULL, variable_get("guifi_pagelimit", 50));
    // Full screen (no lateral bars, etc...)
    print theme('page', $output, FALSE);
    // If normal output, retrurn $output...
}
/**
 * Removes a node from guifi.net
 * @param GuifiAPI $gapi
 * @param mixed[] $parameters
 * @return unknown_type
 */
function guifi_api_node_remove($gapi, $parameters)
{
    if (!guifi_api_check_fields($gapi, array('node_id'), $parameters)) {
        return FALSE;
    }
    $node = node_load($parameters['node_id']);
    if (!$node->id) {
        $gapi->addError(500, "node_id = {$parameters['node_id']}");
        return FALSE;
    }
    if ($node->type != 'guifi_node') {
        $gapi->addError(500, "node_id = {$node->id} is not a guifi node");
        return FALSE;
    }
    if (node_access('delete', $node) && guifi_node_access('update', $node)) {
        node_delete($node->id);
    } else {
        $gapi->addError(501);
        return FALSE;
    }
    return TRUE;
}
function guifi_zone_availability($zone, $desc = "all")
{
    guifi_log(GUIFILOG_TRACE, sprintf('function guifi_zone_availability(%s)', $desc), $zone);
    $oneyearfromnow = time() - '31622400';
    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;
    }
    $childs = guifi_zone_childs($zone->id);
    $childs[] = $zone->id;
    switch ($desc) {
        case 'pending':
            $msg = t('Pending/to review & date last changed.');
            $lbreadcrumb = 'node/%d/view/pending';
            $qstatus = "Working";
            break;
        case 'all':
        default:
            $msg = t('Availability');
            $lbreadcrumb = 'node/%d/view/availability';
            $qstatus = "all";
    }
    drupal_set_breadcrumb(guifi_zone_ariadna($zone->id, $lbreadcrumb));
    $output = '<h2>' . $msg . ' @  ' . $zone->title . '</h2>';
    $sql = 'SELECT z.id zid, z.title ztitle, z.nick znick, ' . '  l.id nid, l.nick nnick, l.status_flag nstatus, l.notification as contact, l.timestamp_created ncreated, l.timestamp_changed nchanged ' . 'FROM {guifi_zone} z, {guifi_location} l ' . 'WHERE z.id=l.zone_id ' . '  AND l.status_flag != "' . $qstatus . '"' . '  AND z.id IN (' . implode(',', $childs) . ') ' . 'ORDER BY ncreated DESC';
    guifi_log(GUIFILOG_TRACE, 'function guifi_zone_availability()', $sql);
    $Msql = pager_query($sql, variable_get("guifi_pagelimit", 50));
    $rows = array();
    $currZ = -1;
    while ($d = db_fetch_array($Msql)) {
        $drows = _guifi_zone_availability_devices($d['nid']);
        $nsr = count($drows);
        if (empty($nsr)) {
            $nsr = 1;
        }
        if (guifi_node_access('update', $d['nid'])) {
            $edit = l(guifi_img_icon('edit.png'), 'node/' . $d['nid'] . '/edit', array('html' => TRUE, 'attributes' => array('target' => '_blank'))) . l(guifi_img_icon('drop.png'), 'node/' . $d['nid'] . '/delete', array('html' => TRUE, 'attributes' => array('target' => '_blank'))) . l(guifi_img_icon('mail.png'), 'mailto:' . $d['contact'], array('html' => TRUE, 'attributes' => array('target' => '_blank')));
        } else {
            $edit = NULL;
        }
        if (!empty($d['nchanged'])) {
            if ($d['nchanged'] < $oneyearfromnow) {
                $dnchanged = array('data' => '<b><font color="#AA0000">' . format_date($d['nchanged'], 'custom', t('d/m/Y')) . '</font></b>', 'class' => $d['nchanged'], 'rowspan' => $nsr);
            } else {
                $dnchanged = array('data' => format_date($d['nchanged'], 'custom', t('d/m/Y')), 'class' => $d['nchanged'], 'rowspan' => $nsr);
            }
        } else {
            $dnchanged = array('data' => t('never changed'));
        }
        $rows[] = array(array('data' => $d['nid'], 'align' => 'right', 'rowspan' => $nsr), array('data' => $edit . l($d['nnick'], 'node/' . $d['nid'], array('attributes' => array('target' => '_blank'))), 'rowspan' => $nsr), array('data' => $d['nstatus'], 'class' => $d['nstatus'], 'rowspan' => $nsr), array('data' => format_date($d['ncreated'], 'custom', t('d/m/Y')), 'class' => $d['ncreated'], 'rowspan' => $nsr), $dnchanged);
        end($rows);
        $krow = key($rows);
        if (count($drows)) {
            $rows[$krow] = array_merge($rows[$krow], array_shift($drows));
            foreach ($drows as $k2 => $v) {
                $rows[] = $v;
            }
        }
    }
    $header = array(array('data' => t('node ID')), array('data' => t('node'), NULL, NULL, 'style' => 'text-align: center'), array('data' => t('status'), NULL, NULL, 'style' => 'text-align: center'), array('data' => t('created'), NULL, NULL, 'style' => 'text-align: center'), array('data' => t('updated'), NULL, NULL, 'style' => 'text-align: center'), array('data' => t('device'), NULL, NULL, 'style' => 'text-align: center'), array('data' => t('device IP'), NULL, NULL, 'style' => 'text-align: center'), array('data' => t('device status'), NULL, NULL, 'style' => 'text-align: center'), array('data' => t('device updated'), NULL, NULL, 'style' => 'text-align: center'));
    $output .= theme('table', $header, $rows, array('width' => '100%'));
    $output .= theme_pager(NULL, variable_get("guifi_pagelimit", 50));
    $node = node_load(array('nid' => $zone->id));
    $output .= theme_links(module_invoke_all('link', 'node', $node, FALSE));
    print theme('page', $output, FALSE);
    return;
}