| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ |
|---|
| 4 | |
|---|
| 5 | // +-------------------------------------------------------------------+ |
|---|
| 6 | // | WiFiDog Authentication Server | |
|---|
| 7 | // | ============================= | |
|---|
| 8 | // | | |
|---|
| 9 | // | The WiFiDog Authentication Server is part of the WiFiDog captive | |
|---|
| 10 | // | portal suite. | |
|---|
| 11 | // +-------------------------------------------------------------------+ |
|---|
| 12 | // | PHP version 5 required. | |
|---|
| 13 | // +-------------------------------------------------------------------+ |
|---|
| 14 | // | Homepage: http://www.wifidog.org/ | |
|---|
| 15 | // | Source Forge: http://sourceforge.net/projects/wifidog/ | |
|---|
| 16 | // +-------------------------------------------------------------------+ |
|---|
| 17 | // | This program is free software; you can redistribute it and/or | |
|---|
| 18 | // | modify it under the terms of the GNU General Public License as | |
|---|
| 19 | // | published by the Free Software Foundation; either version 2 of | |
|---|
| 20 | // | the License, or (at your option) any later version. | |
|---|
| 21 | // | | |
|---|
| 22 | // | This program is distributed in the hope that it will be useful, | |
|---|
| 23 | // | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|---|
| 24 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|---|
| 25 | // | GNU General Public License for more details. | |
|---|
| 26 | // | | |
|---|
| 27 | // | You should have received a copy of the GNU General Public License | |
|---|
| 28 | // | along with this program; if not, contact: | |
|---|
| 29 | // | | |
|---|
| 30 | // | Free Software Foundation Voice: +1-617-542-5942 | |
|---|
| 31 | // | 59 Temple Place - Suite 330 Fax: +1-617-542-2652 | |
|---|
| 32 | // | Boston, MA 02111-1307, USA gnu@gnu.org | |
|---|
| 33 | // | | |
|---|
| 34 | // +-------------------------------------------------------------------+ |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * @package WiFiDogAuthServer |
|---|
| 38 | * @subpackage Statistics |
|---|
| 39 | * @author Benoit Grégoire <bock@step.polymtl.ca> |
|---|
| 40 | * @copyright 2004-2006 Benoit Grégoire, Technologies Coeus inc. |
|---|
| 41 | * @version Subversion $Id$ |
|---|
| 42 | * @link http://www.wifidog.org/ |
|---|
| 43 | */ |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * Load required files |
|---|
| 47 | */ |
|---|
| 48 | require_once('include/common.php'); |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * Gives various statistics about the status of the network or of a specific node |
|---|
| 52 | * |
|---|
| 53 | * @package WiFiDogAuthServer |
|---|
| 54 | * @subpackage Statistics |
|---|
| 55 | * @author Benoit Grégoire <bock@step.polymtl.ca> |
|---|
| 56 | * @copyright 2004-2006 Benoit Grégoire, Technologies Coeus inc. |
|---|
| 57 | */ |
|---|
| 58 | class Statistics |
|---|
| 59 | { |
|---|
| 60 | /** An array of the of the selected networks. |
|---|
| 61 | * The key is the network_id, the value is the Network object. */ |
|---|
| 62 | private $report_selected_networks = array (); |
|---|
| 63 | private $report_date_min; /**< Minimum timestamp */ |
|---|
| 64 | /** Maximum timestamp */ |
|---|
| 65 | private $report_date_max; |
|---|
| 66 | /** How to distinguish unique users. Either 'user_mac' (MAC adresses) or 'user_id' */ |
|---|
| 67 | private $report_distinguish_users_by = 'user_id'; |
|---|
| 68 | |
|---|
| 69 | /* Options on how to distinguish users ,see constructor */ |
|---|
| 70 | private $user_distinguish_by_options = array (); |
|---|
| 71 | |
|---|
| 72 | /** An array of the node_ids to which the statistics should be restricted. |
|---|
| 73 | * The key is the network_id, the value is the Network object.*/ |
|---|
| 74 | private $report_selected_nodes = array (); |
|---|
| 75 | /** An array of the selected reports to be generated. |
|---|
| 76 | * The key is the classname, the value is the report object */ |
|---|
| 77 | private $report_selected_reports = array (); |
|---|
| 78 | |
|---|
| 79 | /** An array of the selected users to which the reports should be restricted. |
|---|
| 80 | * The key is the user_id or user_mac, the value is the User object, |
|---|
| 81 | * or null if the key is a MAC address */ |
|---|
| 82 | private $report_selected_users = array (); |
|---|
| 83 | function __construct() |
|---|
| 84 | { |
|---|
| 85 | $network = Network :: GetCurrentNetwork(); |
|---|
| 86 | $this->report_selected_networks[$network->getId()] = $network; |
|---|
| 87 | $this->user_distinguish_by_options = array ('user_id' => _("Usernames"), 'user_mac' => _("MAC adresses")); |
|---|
| 88 | |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /** |
|---|
| 92 | * Get the list of available report types |
|---|
| 93 | * |
|---|
| 94 | * @return array An array of class names |
|---|
| 95 | */ |
|---|
| 96 | public function getAvailableReports() |
|---|
| 97 | { |
|---|
| 98 | $dir = WIFIDOG_ABS_FILE_PATH . 'classes/StatisticReport'; |
|---|
| 99 | if ($handle = opendir($dir)) |
|---|
| 100 | { |
|---|
| 101 | $tab = Array (); |
|---|
| 102 | /* This is the correct way to loop over the directory. */ |
|---|
| 103 | while (false !== ($file = readdir($handle))) |
|---|
| 104 | { |
|---|
| 105 | if ($file != '.' && $file != '..') |
|---|
| 106 | { |
|---|
| 107 | $matches = null; |
|---|
| 108 | if (preg_match("/^(.*)\.php$/", $file, $matches) > 0) |
|---|
| 109 | { |
|---|
| 110 | //pretty_print_r($matches); |
|---|
| 111 | $filename = $dir.'/'.$matches[0]; |
|---|
| 112 | require_once ($filename); |
|---|
| 113 | $classname = $matches[1]; |
|---|
| 114 | if (call_user_func(array ($classname, 'isAvailable'), $this)) |
|---|
| 115 | { |
|---|
| 116 | $name = call_user_func(array ($classname, 'getReportName'), $this); |
|---|
| 117 | |
|---|
| 118 | $tab[$classname] = $name; |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | closedir($handle); |
|---|
| 124 | } |
|---|
| 125 | else |
|---|
| 126 | { |
|---|
| 127 | throw new Exception(_('Unable to open directory ').$dir); |
|---|
| 128 | } |
|---|
| 129 | $tab = str_ireplace('.php', '', $tab); |
|---|
| 130 | asort($tab); |
|---|
| 131 | return $tab; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * UI for selecting the date ragnge for the report |
|---|
| 136 | * |
|---|
| 137 | * @return string HTML markup |
|---|
| 138 | */ |
|---|
| 139 | private function getDateRangeUI() |
|---|
| 140 | { |
|---|
| 141 | $html = ''; |
|---|
| 142 | $html .=<<<EOF |
|---|
| 143 | <script language="javascript"> |
|---|
| 144 | function change_value(value, from, to) { |
|---|
| 145 | if (value != "") { |
|---|
| 146 | var values_array = value.split(","); |
|---|
| 147 | from.value = values_array[0]; |
|---|
| 148 | if (values_array[1]) { |
|---|
| 149 | to.value = values_array[1]; |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | </script> |
|---|
| 154 | EOF; |
|---|
| 155 | $from_presets = array (_("No restriction...") => "", _("yesterday") => strftime("%Y-%m-%d 00:00", strtotime("-1 day")), _("today") => strftime("%Y-%m-%d 00:00", time()), _("2 days ago") => strftime("%Y-%m-%d 00:00", strtotime("-2 days")), _("3 days ago") => strftime("%Y-%m-%d 00:00", strtotime("-3 days")), _("1 week ago") => strftime("%Y-%m-%d 00:00", strtotime("-1 week")), _("2 weeks ago") => strftime("%Y-%m-%d 00:00", strtotime("-2 weeks")), _("3 weeks ago") => strftime("%Y-%m-%d 00:00", strtotime("-3 weeks")), _("1 month ago") => strftime("%Y-%m-%d 00:00", strtotime("-1 month")), _("2 months ago") => strftime("%Y-%m-%d 00:00", strtotime("-2 months")), _("6 months ago") => strftime("%Y-%m-%d 00:00", strtotime("-6 months")), _("1 year ago") => strftime("%Y-%m-%d 00:00", strtotime("-1 year")), _("-") => "", _("Select from and to...") => "", _("yesterday (whole day)") => strftime("%Y-%m-%d 00:00", strtotime("-1 day")).",".strftime("%Y-%m-%d 11:59", strtotime("-1 day")), _("today (whole day)") => strftime("%Y-%m-%d 00:00", time()).",".strftime("%Y-%m-%d %H:%M", time()), _("this month") => strftime("%Y-%m-01 00:00", time()).",".strftime("%Y-%m-%d %H:%M", time()), _("last month") => strftime("%Y-%m-01 00:00", strtotime("-1 month")).",".strftime("%Y-%m-01 00:00", time()), _("this year") => strftime("%Y-01-01 00:00", time()).",".strftime("%Y-%m-%d %H:%M", time()), _("forever") => "1970-01-01 00:00,".strftime("%Y-%m-%d %H:%M", time())); |
|---|
| 156 | |
|---|
| 157 | $to_presets = array (_("No restriction...") => "", _("yesterday") => strftime("%Y-%m-%d 11:59", strtotime("-1 day")), _("today") => strftime("%Y-%m-%d 11:59", time()), _("2 days ago") => strftime("%Y-%m-%d 11:59", strtotime("-2 days")), _("3 days ago") => strftime("%Y-%m-%d 11:59", strtotime("-3 days")), _("1 week ago") => strftime("%Y-%m-%d 11:59", strtotime("-1 week")), _("2 weeks ago") => strftime("%Y-%m-%d 11:59", strtotime("-2 weeks")), _("3 weeks ago") => strftime("%Y-%m-%d 11:59", strtotime("-3 weeks")), _("1 month ago") => strftime("%Y-%m-%d 11:59", strtotime("-1 month")), _("2 months ago") => strftime("%Y-%m-%d 11:59", strtotime("-2 months")), _("6 months ago") => strftime("%Y-%m-%d 11:59", strtotime("-6 months")), _("1 year ago") => strftime("%Y-%m-%d 11:59", strtotime("-1 year")), _("-") => "", _("Select from and to...") => "", _("yesterday (whole day)") => strftime("%Y-%m-%d 11:59", strtotime("-1 day")).",".strftime("%Y-%m-%d 00:00", strtotime("-1 day")), _("today (whole day)") => strftime("%Y-%m-%d %H:%M", time()).",".strftime("%Y-%m-%d 00:00", time()), _("this month") => strftime("%Y-%m-%d %H:%M", time()).",".strftime("%Y-%m-01 00:00", time()), _("last month") => strftime("%Y-%m-01 00:00", time()).",".strftime("%Y-%m-01 00:00", strtotime("-1 month")), _("this year") => strftime("%Y-%m-%d %H:%M", time()).",".strftime("%Y-01-01 00:00", time()), _("forever") => strftime("%Y-%m-%d %H:%M", time()).",1970-01-01 00:00"); |
|---|
| 158 | |
|---|
| 159 | $html .= "<table class='admin_element_list'>"; |
|---|
| 160 | $html .= "<tr class='admin_element_item_container'>"; |
|---|
| 161 | $html .= " <th class='admin_element_label'>"._("From").":</th>"; |
|---|
| 162 | $html .= " <td class='admin_element_data'><input type='text' name='date_from' value='{$this->report_date_min}'></td>"; |
|---|
| 163 | $html .= " <td class='admin_element_tools'>"; |
|---|
| 164 | $html .= " <select onChange=\"javascript:change_value(this.value,this.form.date_from,this.form.date_to);\">"; |
|---|
| 165 | |
|---|
| 166 | foreach ($from_presets as $label => $value) |
|---|
| 167 | { |
|---|
| 168 | //echo "<p>$value, $this->report_date_min </p>"; |
|---|
| 169 | $value == $this->report_date_min ? $selected = 'SELECTED' : $selected = ''; |
|---|
| 170 | $html .= "<option value=\"{$value}\" $selected>{$label}"; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | $html .= " </select>\n"; |
|---|
| 174 | $html .= " </td>\n"; |
|---|
| 175 | $html .= "</tr>\n"; |
|---|
| 176 | $html .= "<tr class='admin_element_item_container'>\n"; |
|---|
| 177 | $html .= " <th class='admin_element_label'>"._("To").":</th>\n"; |
|---|
| 178 | $html .= " <td class='admin_element_data'><input type=\"text\" name=\"date_to\" value=\"{$this->report_date_max}\"></td>\n"; |
|---|
| 179 | $html .= " <td class='admin_element_data'>\n"; |
|---|
| 180 | $html .= " <select onChange=\"javascript:change_value(this.value,this.form.date_to,this.form.date_from);\">\n"; |
|---|
| 181 | |
|---|
| 182 | foreach ($to_presets as $label => $value) |
|---|
| 183 | { |
|---|
| 184 | $value == $this->report_date_max ? $selected = 'SELECTED' : $selected = ''; |
|---|
| 185 | $html .= "<option value=\"{$value}\" $selected>{$label}"; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | $html .= " </select>\n"; |
|---|
| 189 | $html .= " </td>\n"; |
|---|
| 190 | $html .= "</tr>\n"; |
|---|
| 191 | $html .= "</table>\n"; |
|---|
| 192 | return $html; |
|---|
| 193 | |
|---|
| 194 | } |
|---|
| 195 | /** |
|---|
| 196 | * Process the date range selection UI |
|---|
| 197 | * |
|---|
| 198 | * @return void |
|---|
| 199 | */ |
|---|
| 200 | private function processDateRangeUI() |
|---|
| 201 | { |
|---|
| 202 | if (isset($_REQUEST['date_from'])) { |
|---|
| 203 | $this->report_date_min = $_REQUEST['date_from']; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | if (isset($_REQUEST['date_to'])) { |
|---|
| 207 | $this->report_date_max = $_REQUEST['date_to']; |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | /** |
|---|
| 212 | * Get the actual SQL fragment to restrict a report to a specific date |
|---|
| 213 | * |
|---|
| 214 | * @param string $column The column in the database that must be higher or equal to |
|---|
| 215 | * the min date and lower or equal to the max date |
|---|
| 216 | * @return string SQL AND clauses |
|---|
| 217 | */ |
|---|
| 218 | public function getSqlDateConstraint($column = 'timestamp_in') |
|---|
| 219 | { |
|---|
| 220 | global $db; |
|---|
| 221 | $column = $db->escapeString($column); |
|---|
| 222 | $sql = ''; |
|---|
| 223 | if ($date_min = $db->escapeString($this->report_date_min)) |
|---|
| 224 | { |
|---|
| 225 | $sql .= " AND $column >= '$date_min' "; |
|---|
| 226 | } |
|---|
| 227 | if ($date_max = $db->escapeString($this->report_date_max)) |
|---|
| 228 | { |
|---|
| 229 | $sql .= " AND $column <= '$date_max' "; |
|---|
| 230 | } |
|---|
| 231 | return $sql; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | /** |
|---|
| 235 | * Get the actual SQL fragment to restrict a report to the specific |
|---|
| 236 | * node(s) selected |
|---|
| 237 | * |
|---|
| 238 | * @param string $column The column in the database that must match the node_id |
|---|
| 239 | * @return string SQL AND clauses |
|---|
| 240 | */ |
|---|
| 241 | public function getSqlNodeConstraint($column) |
|---|
| 242 | { |
|---|
| 243 | global $db; |
|---|
| 244 | $column = $db->escapeString($column); |
|---|
| 245 | $sql = ''; |
|---|
| 246 | if (count($this->report_selected_nodes) > 0) |
|---|
| 247 | { |
|---|
| 248 | $sql .= " AND ("; |
|---|
| 249 | $first = true; |
|---|
| 250 | foreach ($this->report_selected_nodes as $node_id => $node) |
|---|
| 251 | { |
|---|
| 252 | $node_id = $db->escapeString($node_id); |
|---|
| 253 | $first ? $sql .= "" : $sql .= " OR "; |
|---|
| 254 | $sql .= "$column = '$node_id'"; |
|---|
| 255 | $first = false; |
|---|
| 256 | } |
|---|
| 257 | $sql .= ") \n"; |
|---|
| 258 | } |
|---|
| 259 | return $sql; |
|---|
| 260 | } |
|---|
| 261 | /** |
|---|
| 262 | * Get the actual SQL fragment to restrict a report to a specific network(s) |
|---|
| 263 | * selected |
|---|
| 264 | * @param string $column The column in the database that must match the network_id |
|---|
| 265 | * @return string SQL AND clauses |
|---|
| 266 | */ |
|---|
| 267 | public function getSqlNetworkConstraint($column) |
|---|
| 268 | { |
|---|
| 269 | global $db; |
|---|
| 270 | $column = $db->escapeString($column); |
|---|
| 271 | $sql = ''; |
|---|
| 272 | if (count($this->report_selected_networks) > 0) |
|---|
| 273 | { |
|---|
| 274 | $sql .= " AND ("; |
|---|
| 275 | $first = true; |
|---|
| 276 | foreach ($this->report_selected_networks as $network_id => $network) |
|---|
| 277 | { |
|---|
| 278 | $network_id = $db->escapeString($network_id); |
|---|
| 279 | $first ? $sql .= "" : $sql .= " OR "; |
|---|
| 280 | $sql .= "$column = '$network_id'"; |
|---|
| 281 | $first = false; |
|---|
| 282 | } |
|---|
| 283 | $sql .= ") \n"; |
|---|
| 284 | } |
|---|
| 285 | return $sql; |
|---|
| 286 | } |
|---|
| 287 | /** |
|---|
| 288 | * Get the actual SQL fragment to restrict a report to the specific |
|---|
| 289 | * user(s) selected |
|---|
| 290 | * |
|---|
| 291 | * @return string SQL AND clauses |
|---|
| 292 | */ |
|---|
| 293 | public function getSqlUserConstraint() |
|---|
| 294 | { |
|---|
| 295 | global $db; |
|---|
| 296 | $column = $db->escapeString($this->report_distinguish_users_by); |
|---|
| 297 | $sql = ''; |
|---|
| 298 | if (count($this->report_selected_users) > 0) |
|---|
| 299 | { |
|---|
| 300 | $sql .= " AND ("; |
|---|
| 301 | $first = true; |
|---|
| 302 | foreach ($this->report_selected_users as $id => $user) |
|---|
| 303 | { |
|---|
| 304 | $id = $db->escapeString($id); |
|---|
| 305 | $first ? $sql .= "" : $sql .= " OR "; |
|---|
| 306 | $sql .= "users.$column = '$id'"; |
|---|
| 307 | $first = false; |
|---|
| 308 | } |
|---|
| 309 | $sql .= ") \n"; |
|---|
| 310 | } |
|---|
| 311 | return $sql; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | /** |
|---|
| 315 | * Get the actual SQL fragment to get the candidates rows from the connections table, |
|---|
| 316 | * once obeying all the report configuration constraints. Only connections |
|---|
| 317 | * with actuall data transferred is considered. Connections is always |
|---|
| 318 | * joined to the nodes table, but not to network or users. |
|---|
| 319 | * |
|---|
| 320 | * @param string $select_columns The selected columns, will be inserted between |
|---|
| 321 | * between SELECT and FROM |
|---|
| 322 | * @param string $join_users true or false, Should we join with the users table? |
|---|
| 323 | * @return string SQL select statemnt. You can append additional AND and GROUP BY |
|---|
| 324 | * clauses |
|---|
| 325 | */ |
|---|
| 326 | public function getSqlCandidateConnectionsQuery($select_columns = '*', $join_users = false) |
|---|
| 327 | { |
|---|
| 328 | $sql = ''; |
|---|
| 329 | $date_constraint = $this->getSqlDateConstraint('timestamp_in'); |
|---|
| 330 | $node_constraint = $this->getSqlNodeConstraint('connections.node_id'); |
|---|
| 331 | $network_constraint = $this->getSqlNetworkConstraint('nodes.network_id'); |
|---|
| 332 | $user_constraint = $this->getSqlUserConstraint(); |
|---|
| 333 | $join_users_sql = ''; |
|---|
| 334 | if ($join_users || !empty($user_constraint)) |
|---|
| 335 | { |
|---|
| 336 | $join_users_sql = "JOIN users ON (connections.user_id = users.user_id)"; |
|---|
| 337 | } |
|---|
| 338 | $sql .= "SELECT $select_columns \n"; |
|---|
| 339 | |
|---|
| 340 | $sql .= "FROM connections \n"; |
|---|
| 341 | $sql .= "JOIN nodes ON (connections.node_id = nodes.node_id) \n"; |
|---|
| 342 | $sql .= "$join_users_sql \n"; |
|---|
| 343 | $sql .= "WHERE (incoming!=0 OR outgoing!=0) \n"; |
|---|
| 344 | $sql .= " {$date_constraint} {$node_constraint} {$network_constraint} {$user_constraint}"; |
|---|
| 345 | return $sql; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | /** |
|---|
| 349 | * Get the actual SQL fragment to get all the conn_id of the all users first successfull connections from the connections table. Only connections |
|---|
| 350 | * with actuall data transferred is considered. It will ignore all report |
|---|
| 351 | * configuration except getDistinguishUsersBy() and selected users, because |
|---|
| 352 | * doing otherwise would not give the real first connection. |
|---|
| 353 | * |
|---|
| 354 | * @return string SQL query |
|---|
| 355 | */ |
|---|
| 356 | public function getSqlRealFirstConnectionsQuery($select_columns = '*', $join_users = false) |
|---|
| 357 | { |
|---|
| 358 | $sql = ''; |
|---|
| 359 | $distinguish_users_by = $this->getDistinguishUsersBy(); |
|---|
| 360 | $user_constraint = $this->getSqlUserConstraint(); |
|---|
| 361 | $join_users_sql = ''; |
|---|
| 362 | if ($join_users || !empty($user_constraint)) |
|---|
| 363 | { |
|---|
| 364 | $join_users_sql = "JOIN users ON (connections.user_id = users.user_id)"; |
|---|
| 365 | } |
|---|
| 366 | $sql .= "SELECT DISTINCT ON(connections.$distinguish_users_by) $select_columns \n"; |
|---|
| 367 | $sql .= "FROM connections \n"; |
|---|
| 368 | $sql .= "$join_users_sql \n"; |
|---|
| 369 | $sql .= "WHERE (incoming!=0 OR outgoing!=0) \n"; |
|---|
| 370 | $sql .= " {$user_constraint}"; |
|---|
| 371 | $sql .= " ORDER BY connections.$distinguish_users_by, timestamp_in"; |
|---|
| 372 | |
|---|
| 373 | return $sql; |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | /** |
|---|
| 377 | * Get an interface to pick to which nodes the statistics apply. |
|---|
| 378 | * |
|---|
| 379 | * @return string HTML markup |
|---|
| 380 | */ |
|---|
| 381 | private function getSelectedNodesUI() |
|---|
| 382 | { |
|---|
| 383 | global $db; |
|---|
| 384 | $html = ''; |
|---|
| 385 | $name = "statistics_selected_nodes[]"; |
|---|
| 386 | $user = User :: getCurrentUser(); |
|---|
| 387 | if ($user->isSuperAdmin()) |
|---|
| 388 | { |
|---|
| 389 | $sql_join = ''; |
|---|
| 390 | } |
|---|
| 391 | else |
|---|
| 392 | { |
|---|
| 393 | $user_id = $db->escapeString($user->getId()); |
|---|
| 394 | $sql_join = " JOIN node_stakeholders ON (nodes.node_id=node_stakeholders.node_id AND user_id='$user_id') "; |
|---|
| 395 | } |
|---|
| 396 | $sql = "SELECT nodes.node_id, nodes.name from nodes $sql_join WHERE 1=1 ORDER BY node_id"; |
|---|
| 397 | $node_rows = null; |
|---|
| 398 | $db->execSql($sql, $node_rows, false); |
|---|
| 399 | $html .= "<select multiple size = 6 name='$name'>\n"; |
|---|
| 400 | |
|---|
| 401 | /*count($this->report_selected_nodes)==0?$selected=' SELECTED ':$selected=''; |
|---|
| 402 | $html.= "<option value='' $selected>"._("Statistics for all nodes")."</option>\n"; |
|---|
| 403 | */ |
|---|
| 404 | if ($node_rows != null) |
|---|
| 405 | { |
|---|
| 406 | foreach ($node_rows as $node_row) |
|---|
| 407 | { |
|---|
| 408 | $html .= "<option "; |
|---|
| 409 | if (array_key_exists($node_row['node_id'], $this->report_selected_nodes)) |
|---|
| 410 | { |
|---|
| 411 | $html .= " SELECTED "; |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | $nom = $node_row['node_id'].": ".$node_row['name']; |
|---|
| 415 | $nom = htmlspecialchars($nom, ENT_QUOTES, 'UTF-8'); |
|---|
| 416 | $primary_key = htmlentities($node_row['node_id'], ENT_QUOTES, 'UTF-8'); |
|---|
| 417 | $html .= "value='$primary_key'>$nom</option>\n"; |
|---|
| 418 | } |
|---|
| 419 | } |
|---|
| 420 | $html .= "</select>\n"; |
|---|
| 421 | return $html; |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | /** |
|---|
| 425 | * Get the select node interface. |
|---|
| 426 | */ |
|---|
| 427 | private function processSelectedNodesUI() |
|---|
| 428 | { |
|---|
| 429 | $name = "statistics_selected_nodes"; |
|---|
| 430 | //pretty_print_r($_REQUEST[$name]); |
|---|
| 431 | $this->report_selected_nodes = array (); |
|---|
| 432 | if (!empty ($_REQUEST[$name])) |
|---|
| 433 | { |
|---|
| 434 | foreach ($_REQUEST[$name] as $value) |
|---|
| 435 | { |
|---|
| 436 | if (!empty ($value)) |
|---|
| 437 | $this->report_selected_nodes[$value] = Node :: getObject($value); |
|---|
| 438 | } |
|---|
| 439 | } |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | /** |
|---|
| 443 | * Get the selected nodes for the reports. |
|---|
| 444 | * @return array An array of Node objects, with the node_id as the key, or |
|---|
| 445 | * an empty array |
|---|
| 446 | */ |
|---|
| 447 | public function getSelectedNodes() |
|---|
| 448 | { |
|---|
| 449 | return $this->report_selected_nodes; |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | /** |
|---|
| 453 | * UI for selecting how the database determines if a user is unique |
|---|
| 454 | * |
|---|
| 455 | * @return string HTML markup |
|---|
| 456 | */ |
|---|
| 457 | private function getDistinguishUsersByUI() |
|---|
| 458 | { |
|---|
| 459 | $html = ''; |
|---|
| 460 | |
|---|
| 461 | /* $html .= " < input type = \ "radio\" name=\"group_connections\" value=\"\""; |
|---|
| 462 | $html .= empty ($_REQUEST['group_connections']) ? 'CHECKED' : ''; |
|---|
| 463 | $html .= ">"._("No")."<br>"; |
|---|
| 464 | */ |
|---|
| 465 | $html .= " <select name=\"distinguish_users_by\">"; |
|---|
| 466 | |
|---|
| 467 | foreach ($this->user_distinguish_by_options as $value => $label) |
|---|
| 468 | { |
|---|
| 469 | //echo "<p>$value, $this->report_date_min </p>"; |
|---|
| 470 | $value == $this->report_distinguish_users_by ? $selected = 'SELECTED' : $selected = ''; |
|---|
| 471 | $html .= "<option value=\"{$value}\" $selected>{$label}"; |
|---|
| 472 | } |
|---|
| 473 | |
|---|
| 474 | $html .= " </select>\n"; |
|---|
| 475 | |
|---|
| 476 | return $html; |
|---|
| 477 | } |
|---|
| 478 | |
|---|
| 479 | /** |
|---|
| 480 | * Process the date range selection UI |
|---|
| 481 | */ |
|---|
| 482 | private function processDistinguishUsersByUI() |
|---|
| 483 | { |
|---|
| 484 | if (!isset ($this->user_distinguish_by_options[$_REQUEST['distinguish_users_by']])) |
|---|
| 485 | throw new exception(_("Invalid parameter")); |
|---|
| 486 | $this->report_distinguish_users_by = $_REQUEST['distinguish_users_by']; |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | /** |
|---|
| 490 | * Get how are users to be ddistinguished |
|---|
| 491 | * |
|---|
| 492 | * @return string Either 'user_id' our 'user_mac' |
|---|
| 493 | */ |
|---|
| 494 | public function getDistinguishUsersBy() |
|---|
| 495 | { |
|---|
| 496 | return $this->report_distinguish_users_by; |
|---|
| 497 | } |
|---|
| 498 | |
|---|
| 499 | /** |
|---|
| 500 | * UI for selecting to which users to restrict the reports |
|---|
| 501 | * |
|---|
| 502 | * @return string HTML markup |
|---|
| 503 | * |
|---|
| 504 | * @todo Allow to select more than one user |
|---|
| 505 | */ |
|---|
| 506 | private function getSelectedUsersUI() |
|---|
| 507 | { |
|---|
| 508 | $html = ''; |
|---|
| 509 | $value = ''; |
|---|
| 510 | foreach ($this->report_selected_users as $id => $user) |
|---|
| 511 | { |
|---|
| 512 | if ($this->report_distinguish_users_by == 'user_id') |
|---|
| 513 | { |
|---|
| 514 | $value .= $user->getUsername(); |
|---|
| 515 | } |
|---|
| 516 | else |
|---|
| 517 | { |
|---|
| 518 | $value .= $id; |
|---|
| 519 | } |
|---|
| 520 | } |
|---|
| 521 | $html .= " <input type='text' name=\"stats_selected_users\" value='$value'>"; |
|---|
| 522 | |
|---|
| 523 | $type_caption = $this->user_distinguish_by_options[$this->report_distinguish_users_by]; |
|---|
| 524 | $html .= " $type_caption\n"; |
|---|
| 525 | |
|---|
| 526 | return $html; |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | /** |
|---|
| 530 | * Process the users selection UI |
|---|
| 531 | * |
|---|
| 532 | * @todo Allow to select more than one user |
|---|
| 533 | */ |
|---|
| 534 | private function processSelectedUsersUI() |
|---|
| 535 | { |
|---|
| 536 | $this->report_selected_users = array (); |
|---|
| 537 | $user_obj = null; |
|---|
| 538 | if(!empty($_REQUEST['stats_selected_users'])) |
|---|
| 539 | { |
|---|
| 540 | if ($this->report_distinguish_users_by == 'user_id') |
|---|
| 541 | { |
|---|
| 542 | global $db; |
|---|
| 543 | $username = $db->escapeString($_REQUEST['stats_selected_users']); |
|---|
| 544 | $row = null; |
|---|
| 545 | $db->execSqlUniqueRes("SELECT user_id FROM users WHERE username='$username'", $row, false); |
|---|
| 546 | if ($row) |
|---|
| 547 | { |
|---|
| 548 | $user_id = $row['user_id']; |
|---|
| 549 | $user_obj = User :: getObject($user_id); |
|---|
| 550 | $this->report_selected_users[$user_id] = $user_obj; |
|---|
| 551 | } |
|---|
| 552 | } |
|---|
| 553 | else |
|---|
| 554 | { |
|---|
| 555 | //We have a MAC address |
|---|
| 556 | if (!empty ($_REQUEST['stats_selected_users'])) |
|---|
| 557 | $this->report_selected_users[$_REQUEST['stats_selected_users']] = null; |
|---|
| 558 | } |
|---|
| 559 | } |
|---|
| 560 | } |
|---|
| 561 | |
|---|
| 562 | /** |
|---|
| 563 | * Get the selected users for the reports. |
|---|
| 564 | * |
|---|
| 565 | * @return array An empty array or an array of user_id or MAC addresses as the |
|---|
| 566 | * key and a User object as the value, unless it's a MAC in which case the |
|---|
| 567 | * value is null |
|---|
| 568 | */ |
|---|
| 569 | public function getSelectedUsers() |
|---|
| 570 | { |
|---|
| 571 | return $this->report_selected_users; |
|---|
| 572 | } |
|---|
| 573 | |
|---|
| 574 | /** |
|---|
| 575 | * Get the selected nodes for the reports. |
|---|
| 576 | * |
|---|
| 577 | * @return array An array of Network objects, with the network_id as the |
|---|
| 578 | * key, or an empty array |
|---|
| 579 | */ |
|---|
| 580 | public function getSelectedNetworks() |
|---|
| 581 | { |
|---|
| 582 | return $this->report_selected_networks; |
|---|
| 583 | } |
|---|
| 584 | |
|---|
| 585 | /** |
|---|
| 586 | * UI for selecting how the database determines if a user is unique |
|---|
| 587 | * |
|---|
| 588 | * @return string HTML markup |
|---|
| 589 | */ |
|---|
| 590 | private function getSelectedReportsUI() |
|---|
| 591 | { |
|---|
| 592 | $html = ''; |
|---|
| 593 | $html .= "<ul class='admin_element_list'>\n"; |
|---|
| 594 | |
|---|
| 595 | foreach (self :: getAvailableReports() as $key => $name) |
|---|
| 596 | { |
|---|
| 597 | array_key_exists($key, $this->report_selected_reports) ? $checked = ' CHECKED ' : $checked = ''; |
|---|
| 598 | $html .= "<li class='admin_element_item_container'>\n"; |
|---|
| 599 | $html .= "<div class='admin_element_tools'><input type='checkbox' name='$key' $checked /></div>\n"; |
|---|
| 600 | $html .= "<div class='admin_element_label'>$name</div>\n"; |
|---|
| 601 | |
|---|
| 602 | $html .= "</li>\n"; |
|---|
| 603 | } |
|---|
| 604 | $html .= "</ul>\n"; |
|---|
| 605 | |
|---|
| 606 | return $html; |
|---|
| 607 | } |
|---|
| 608 | |
|---|
| 609 | /** |
|---|
| 610 | * Process the date range selection UI |
|---|
| 611 | */ |
|---|
| 612 | private function processSelectedReportsUI() |
|---|
| 613 | { |
|---|
| 614 | $this->report_selected_reports = array (); |
|---|
| 615 | foreach (self :: getAvailableReports() as $key => $name) |
|---|
| 616 | { |
|---|
| 617 | if (array_key_exists($key, $_REQUEST)) |
|---|
| 618 | { |
|---|
| 619 | $this->report_selected_reports[$key] = call_user_func(array ($key, 'getObject'), $key, $this); |
|---|
| 620 | } |
|---|
| 621 | } |
|---|
| 622 | //pretty_print_r($this->report_selected_reports); |
|---|
| 623 | } |
|---|
| 624 | |
|---|
| 625 | public function getAdminUI() |
|---|
| 626 | { |
|---|
| 627 | $html = ''; |
|---|
| 628 | $html .= "<fieldset class='admin_container'>\n"; |
|---|
| 629 | $html .= "<legend>"._("Report configuration")."</legend>\n"; |
|---|
| 630 | $html .= "<ul class='admin_element_list'>\n"; |
|---|
| 631 | // Network |
|---|
| 632 | $html .= "<li class='admin_element_item_container'>\n"; |
|---|
| 633 | $html .= "<div class='admin_element_data'>\n"; |
|---|
| 634 | $html .= Network :: getSelectNetworkUI('Statistics', reset($this->report_selected_networks)); |
|---|
| 635 | $html .= "</div>\n"; |
|---|
| 636 | $html .= "</li>\n"; |
|---|
| 637 | |
|---|
| 638 | // Date range |
|---|
| 639 | $html .= "<li class='admin_element_item_container'>\n"; |
|---|
| 640 | $html .= "<fieldset class='admin_element_group'>\n"; |
|---|
| 641 | $html .= "<legend>"._("Restrict the time range for which statistics will be computed")." : </legend>\n"; |
|---|
| 642 | $html .= $this->getDateRangeUI(); |
|---|
| 643 | $html .= "</fieldset>\n"; |
|---|
| 644 | $html .= "</li>\n"; |
|---|
| 645 | |
|---|
| 646 | // Selected nodes |
|---|
| 647 | $html .= "<li class='admin_element_item_container'>\n"; |
|---|
| 648 | $html .= "<fieldset class='admin_element_group'>\n"; |
|---|
| 649 | $html .= "<legend>"._("Restrict stats to the following nodes")." : </legend>\n"; |
|---|
| 650 | $html .= "<div class='admin_element_data'>\n"; |
|---|
| 651 | $html .= $this->getSelectedNodesUI(); |
|---|
| 652 | $html .= "</div>\n"; |
|---|
| 653 | $html .= "</fieldset>\n"; |
|---|
| 654 | $html .= "</li>\n"; |
|---|
| 655 | |
|---|
| 656 | |
|---|
| 657 | // Unique user criteria |
|---|
| 658 | $html .= "<li class='admin_element_item_container'>\n"; |
|---|
| 659 | $html .= "<fieldset class='admin_element_group'>\n"; |
|---|
| 660 | $html .= "<legend>"._("Distinguish users by")." : </legend>\n"; |
|---|
| 661 | $html .= "<div class='admin_element_data'>\n"; |
|---|
| 662 | $html .= $this->getDistinguishUsersByUI(); |
|---|
| 663 | $html .= "</div>\n"; |
|---|
| 664 | $html .= "</fieldset>\n"; |
|---|
| 665 | $html .= "</li>\n"; |
|---|
| 666 | // Selected users |
|---|
| 667 | $html .= "<li class='admin_element_item_container'>\n"; |
|---|
| 668 | $html .= "<fieldset class='admin_element_group'>\n"; |
|---|
| 669 | $html .= "<legend>"._("Restrict stats to the selected users")." : </legend>\n"; |
|---|
| 670 | $html .= "<div class='admin_element_data'>\n"; |
|---|
| 671 | $html .= $this->getSelectedUsersUI(); |
|---|
| 672 | $html .= "</div>\n"; |
|---|
| 673 | $html .= "</fieldset class='admin_element_group'>\n"; |
|---|
| 674 | $html .= "</li>\n"; |
|---|
| 675 | // Reports |
|---|
| 676 | $html .= "<li class='admin_element_item_container'>\n"; |
|---|
| 677 | $html .= "<fieldset>\n"; |
|---|
| 678 | $html .= "<legend>"._("Selected reports")." : </legend>\n"; |
|---|
| 679 | $html .= "<div class='admin_element_data'>\n"; |
|---|
| 680 | $html .= $this->getSelectedReportsUI(); |
|---|
| 681 | $html .= "</div>\n"; |
|---|
| 682 | $html .= "</fieldset>\n"; |
|---|
| 683 | $html .= "</ul>\n"; |
|---|
| 684 | $html .= "</fieldset>\n"; |
|---|
| 685 | $html .= "</li>\n"; |
|---|
| 686 | return $html; |
|---|
| 687 | } |
|---|
| 688 | |
|---|
| 689 | public function processAdminUI() |
|---|
| 690 | { |
|---|
| 691 | $network = Network :: processSelectNetworkUI('Statistics'); |
|---|
| 692 | $this->report_selected_networks = array (); |
|---|
| 693 | $this->report_selected_networks[$network->getId()] = $network; |
|---|
| 694 | $this->processDateRangeUI(); |
|---|
| 695 | $this->processSelectedNodesUI(); |
|---|
| 696 | $this->processDistinguishUsersByUI(); |
|---|
| 697 | $this->processSelectedUsersUI(); |
|---|
| 698 | $this->processSelectedReportsUI(); |
|---|
| 699 | } |
|---|
| 700 | |
|---|
| 701 | /** |
|---|
| 702 | * Get the output of all the selected reports |
|---|
| 703 | * |
|---|
| 704 | * @return string HTML markup |
|---|
| 705 | */ |
|---|
| 706 | public function getReportUI() |
|---|
| 707 | { |
|---|
| 708 | $html = ''; |
|---|
| 709 | foreach ($this->report_selected_reports as $classname => $report) |
|---|
| 710 | { |
|---|
| 711 | $html .= $report->getReportUI(); |
|---|
| 712 | //$html.='<hr />'; |
|---|
| 713 | } |
|---|
| 714 | |
|---|
| 715 | return $html; |
|---|
| 716 | } |
|---|
| 717 | |
|---|
| 718 | } |
|---|
| 719 | |
|---|
| 720 | /* |
|---|
| 721 | * Local variables: |
|---|
| 722 | * tab-width: 4 |
|---|
| 723 | * c-basic-offset: 4 |
|---|
| 724 | * c-hanging-comment-ender-p: nil |
|---|
| 725 | * End: |
|---|
| 726 | */ |
|---|
| 727 | |
|---|
| 728 | |
|---|