SDK Script webpage.are

webpage.are
/* DESC: This script will generate a page which can be viewed in the Web Manager
 * Copyright (C) 2012 NetModule AG, Switzerland
 */
 
ID = 1;
 
/* show interface overview */
void show_interfaces ()
{
    interfaces = mkarray();
    nr_interfaces = 0;
 
    status = nb_status("lan");
    stlist = qsort(struct_fields(status));
 
    for (j = 0; j < length(stlist); j++) {
        key = stlist[j];
 
        pos = strstr(key, "_ADDRESS");
        if (!is_void(pos)) {
            ifc = left(key, pos);
            interfaces[nr_interfaces++] = ifc;
        }
    }
 
    nb_page_respond(page, '<h3>LAN Interfaces Overview</h3>\n');
    nb_page_respond(page, '<table>\n');
 
    interfaces = qsort(interfaces);
    for (i=0; i<nr_interfaces; i++) {
        ifc = interfaces[i];
        nb_page_respond(page, '<tr>\n');
        nb_page_respond(page, '<td>%s</td>', ifc);
        nb_page_respond(page, '<td><a href="sdkPage.php?id=%08X&ifc=%s">view</a></td>\n', ID, ifc);
        nb_page_respond(page, '</tr>\n');
    }
 
    nb_page_respond(page, '</table>\n');
    nb_page_finish(page);
}
 
/* show interface status */
void show_interface (string ifc)
{
    status = nb_status("lan");
    stlist = struct_fields(status);
 
    nb_page_respond(page, '<h3>Interface %s Status</h3>\n', ifc);
    nb_page_respond(page, '<table>\n');
 
    pattern = strcat(ifc, "_");
    plen = strlen(pattern);
    for (j = 0; j < length(stlist); j++) {
        key = stlist[j];
        if (left(key, plen) == pattern) {
            nb_page_respond(page, '<tr>\n');
            nb_page_respond(page, '<td>%s</td>', substr(key, plen));
            nb_page_respond(page, '<td>%s</td>', struct_get(status, key));
            nb_page_respond(page, '</tr>\n');
        }
    }
 
    nb_page_respond(page, '</table>\n');
    nb_page_respond(page, '<br><br><a href="sdkPage.php?id=%08X">back</a>\n', ID);
    nb_page_finish(page);
}
 
/* register page */
page = nb_page_register(ID, "LAN Interfaces", "Network Overview");
if (is_void(page)) {
    printf("unable to register page\n");
    exit(1);
}
 
printf("waiting for requests...\n");
while (1) {
    /* wait for page request */
    request = nb_page_request(page);
 
    if (!is_void(request)) {
        /* evaluate GET params */
        GET = struct_get(request, "GET");
 
        ifc = "";
        for (k = 0; k < length(GET); k++) {
            key = struct_get(GET[k], "key");
            if (key == "ifc") {
                ifc = struct_get(GET[k], "value");
                break;
            }
        }
        if (ifc != "") {
            /* show requested interface */
            show_interface(ifc);
        } else {
            /* show interface overview */
            show_interfaces();
        }
    }
    sleep(1);
}
 
nb_page_unregister(page);
 
exit(0);