root/trunk/wifidog-auth/wifidog/node_list.php @ 1258

Revision 1258, 7.1 KB (checked in by benoitg, 6 years ago)

Fix uptime formatting

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2
3
4/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5
6// +-------------------------------------------------------------------+
7// | WiFiDog Authentication Server                                     |
8// | =============================                                     |
9// |                                                                   |
10// | The WiFiDog Authentication Server is part of the WiFiDog captive  |
11// | portal suite.                                                     |
12// +-------------------------------------------------------------------+
13// | PHP version 5 required.                                           |
14// +-------------------------------------------------------------------+
15// | Homepage:     http://www.wifidog.org/                             |
16// | Source Forge: http://sourceforge.net/projects/wifidog/            |
17// +-------------------------------------------------------------------+
18// | This program is free software; you can redistribute it and/or     |
19// | modify it under the terms of the GNU General Public License as    |
20// | published by the Free Software Foundation; either version 2 of    |
21// | the License, or (at your option) any later version.               |
22// |                                                                   |
23// | This program is distributed in the hope that it will be useful,   |
24// | but WITHOUT ANY WARRANTY; without even the implied warranty of    |
25// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     |
26// | GNU General Public License for more details.                      |
27// |                                                                   |
28// | You should have received a copy of the GNU General Public License |
29// | along with this program; if not, contact:                         |
30// |                                                                   |
31// | Free Software Foundation           Voice:  +1-617-542-5942        |
32// | 59 Temple Place - Suite 330        Fax:    +1-617-542-2652        |
33// | Boston, MA  02111-1307,  USA       gnu@gnu.org                    |
34// |                                                                   |
35// +-------------------------------------------------------------------+
36
37/**
38 * Network status page
39 *
40 * @package    WiFiDogAuthServer
41 * @author     Benoit Grégoire <bock@step.polymtl.ca>
42 * @copyright  2004-2006 Benoit Grégoire, Technologies Coeus inc.
43 * @version    Subversion $Id$
44 * @link       http://www.wifidog.org/
45 */
46
47/**
48 * Define default sort parameter
49 */
50define('DEFAULT_SORT_BY_PARAM', "name");
51
52/**
53 * Load required files
54 */
55require_once (dirname(__FILE__) . '/include/common.php');
56require_once ('classes/SmartyWifidog.php');
57require_once ('classes/Node.php');
58require_once ('classes/Utils.php');
59$smarty = SmartyWifidog :: getObject();
60$db = AbstractDb :: getObject();
61
62// Set the sort parameter, defaults to name
63if (empty ($_REQUEST["sort_by"])) {
64    $sort_by_param = DEFAULT_SORT_BY_PARAM;
65    $sort_by_using_sql = true;
66}
67else {
68    // Validate sort parameters
69    switch ($_REQUEST["sort_by"]) {
70        // SQL sort parameters
71        case "last_heartbeat_user_agent" :
72        case "name" :
73        case "creation_date" :
74        case "gw_id" :
75            // Fall-through valid parameters
76            $sort_by_param = $_REQUEST["sort_by"];
77            $sort_by_using_sql = true;
78            break;
79            // Abstraction-driven sort parameters
80        case "node_id" :
81        case "num_online_users" :
82            $sort_by_param = $_REQUEST["sort_by"];
83            $sort_by_using_sql = false;
84            break;
85        default :
86            $sort_by_param = DEFAULT_SORT_BY_PARAM;
87            $sort_by_using_sql = true;
88    }
89}
90
91// Sort according to above instructions
92if ($sort_by_using_sql === true) {
93    // Check if ordering should ignore uppper and lower case
94    if ($sort_by_param == "name" || $sort_by_param == "node_id") {
95        $sort_by_param_sql = "lower(" . $sort_by_param . ")";
96    }
97    else {
98        $sort_by_param_sql = $sort_by_param;
99    }
100    $sql = "SELECT node_id, gw_id, name, last_heartbeat_user_agent, (CURRENT_TIMESTAMP-last_heartbeat_timestamp) AS since_last_heartbeat, last_heartbeat_ip, CASE WHEN ((CURRENT_TIMESTAMP-last_heartbeat_timestamp) < interval '5 minutes') THEN true ELSE false END AS online, creation_date, node_deployment_status, last_heartbeat_wifidog_uptime FROM nodes WHERE node_deployment_status != 'PERMANENTLY_CLOSED' ORDER BY {$sort_by_param_sql}";
101}
102else {
103    $sql = "SELECT node_id, gw_id, name, last_heartbeat_user_agent, (CURRENT_TIMESTAMP-last_heartbeat_timestamp) AS since_last_heartbeat, last_heartbeat_ip, CASE WHEN ((CURRENT_TIMESTAMP-last_heartbeat_timestamp) < interval '5 minutes') THEN true ELSE false END AS online, creation_date, node_deployment_status, last_heartbeat_wifidog_uptime FROM nodes WHERE node_deployment_status != 'PERMANENTLY_CLOSED' ORDER BY " . DEFAULT_SORT_BY_PARAM;
104}
105$nodes_results = null;
106$db->execSql($sql, $nodes_results, false);
107
108if ($nodes_results == null)
109throw new Exception(_("No nodes could not be found in the database"));
110
111$deploymentStatuses = array (
112"DEPLOYED" => _("Deployed"
113), "IN_PLANNING" => _("In planning"), "IN_TESTING" => _("In testing"), "NON_WIFIDOG_NODE" => _("Non-Wifidog node"), "PERMANENTLY_CLOSED" => _("Permanently closed"), "TEMPORARILY_CLOSED" => _("Temporarily closed"));
114
115$nodes_list = array ();
116foreach ($nodes_results as $node_row) {
117    $node = Node :: getObject($node_row['node_id']);
118    $node_row['duration'] = $db->GetDurationArrayFromIntervalStr($node_row['since_last_heartbeat']);
119    if($node_row['last_heartbeat_wifidog_uptime']) {
120        $previousRemainder = $node_row['last_heartbeat_wifidog_uptime'];
121        $remainder = $previousRemainder % (3600*24);
122        $node_row['wifidog_uptime']['days'] = ($previousRemainder - $remainder)  / (3600*24);
123        $previousRemainder = $remainder;
124        $remainder = $previousRemainder % (3600);
125        $node_row['wifidog_uptime']['hours'] = ($previousRemainder - $remainder)  / (3600);
126        $previousRemainder = $remainder;
127        $remainder = $previousRemainder % (60);
128        $node_row['wifidog_uptime']['minutes'] = ($previousRemainder - $remainder)  / (60);
129    }
130    $node_row['num_online_users'] = $node->getNumOnlineUsers();
131    $nodeDeploymentStatus = $node_row['node_deployment_status'];
132    $node_row['node_deployment_status'] = $deploymentStatuses["$nodeDeploymentStatus"];
133    $nodes_list[] = $node_row;
134}
135
136// Sort using PHP
137if ($sort_by_using_sql === false) {
138    // Using natural-sort algorithm .
139    switch ($sort_by_param) {
140        case "node_id" :
141            Utils :: natsort2d($nodes_list, "node_id");
142            break;
143        case "num_online_users" :
144            Utils :: natsort2d($nodes_list, "num_online_users");
145            break;
146    }
147}
148
149// Pass values to Smarty
150$smarty->assign("nodes", $nodes_list);
151$smarty->assign("sort_by_param", $sort_by_param);
152
153require_once ('classes/MainUI.php');
154
155$ui = MainUI :: getObject();
156$ui->addContent('main_area_middle', $smarty->fetch("templates/node_list.html"));
157$ui->display();
158
159/*
160 * Local variables:
161 * tab-width: 4
162 * c-basic-offset: 4
163 * c-hanging-comment-ender-p: nil
164 * End:
165 */
166?>
Note: See TracBrowser for help on using the browser.