function mactrack_view() {
	global $title, $colors, $mactrack_rows, $config;

	/* ================= input validation ================= */
	input_validate_input_number(get_request_var_request("rows"));
	input_validate_input_number(get_request_var_request("page"));
	input_validate_input_number(get_request_var_request("issues"));
	input_validate_input_number(get_request_var_request("bwusage"));
	input_validate_input_number(get_request_var_request("device"));
	input_validate_input_number(get_request_var_request("site"));
	input_validate_input_number(get_request_var_request("type"));
	/* ==================================================== */

	/* clean up filter */
	if (isset($_REQUEST["filter"])) {
		$_REQUEST["filter"] = sanitize_search_string(get_request_var("filter"));
	}

	/* clean up totals */
	if (isset($_REQUEST["totals"])) {
		$_REQUEST["totals"] = sanitize_search_string(get_request_var("totals"));
	}

	/* clean up */
	if (isset($_REQUEST["sort_column"])) {
		$_REQUEST["sort_column"] = sanitize_search_string(get_request_var("sort_column"));
	}

	/* clean up */
	if (isset($_REQUEST["sort_direction"])) {
		$_REQUEST["sort_direction"] = sanitize_search_string(get_request_var("sort_direction"));
	}

	/* if the user pushed the 'clear' button */
	if (isset($_REQUEST["clear_x"]) || isset($_REQUEST["reset"])) {
		kill_session_var("sess_mactrack_int_current_page");
		kill_session_var("sess_mactrack_int_rows");
		kill_session_var("sess_mactrack_int_totals");
		kill_session_var("sess_mactrack_int_device_id");
		kill_session_var("sess_mactrack_int_state");
		kill_session_var("sess_mactrack_int_site");
		kill_session_var("sess_mactrack_int_device");
		kill_session_var("sess_mactrack_int_issues");
		kill_session_var("sess_mactrack_int_bwusage");
		kill_session_var("sess_mactrack_int_type");
		kill_session_var("sess_mactrack_int_period");
		kill_session_var("sess_mactrack_int_filter");
		kill_session_var("sess_mactrack_int_sort_column");
		kill_session_var("sess_mactrack_int_sort_direction");

		$_REQUEST["page"] = 1;

		if (isset($_REQUEST["clear_x"])) {
			unset($_REQUEST["device_id"]);
			unset($_REQUEST["totals"]);
			unset($_REQUEST["state"]);
			unset($_REQUEST["site"]);
			unset($_REQUEST["totals"]);
			unset($_REQUEST["device"]);
			unset($_REQUEST["issues"]);
			unset($_REQUEST["bwusage"]);
			unset($_REQUEST["type"]);
			unset($_REQUEST["period"]);
			unset($_REQUEST["filter"]);
			unset($_REQUEST["rows"]);
			unset($_REQUEST["sort_column"]);
			unset($_REQUEST["sort_direction"]);
		}
	}else{
		/* if any of the settings changed, reset the page number */
		$changed = 0;
		$changed += mactrack_check_changed("device_id", "sess_mactrack_int_device_id");
		$changed += mactrack_check_changed("site", "sess_mactrack_int_site");
		$changed += mactrack_check_changed("totals", "sess_mactrack_int_totals");
		$changed += mactrack_check_changed("device", "sess_mactrack_int_device");
		$changed += mactrack_check_changed("issues", "sess_mactrack_int_issues");
		$changed += mactrack_check_changed("bwusage", "sess_mactrack_int_bwusage");
		$changed += mactrack_check_changed("type", "sess_mactrack_int_type");
		$changed += mactrack_check_changed("period", "sess_mactrack_int_period");
		$changed += mactrack_check_changed("filter", "sess_mactrack_int_filter");
		$changed += mactrack_check_changed("rows", "sess_mactrack_int_rows");
		$changed += mactrack_check_changed("sort_column", "sess_mactrack_int_sort_column");
		$changed += mactrack_check_changed("sort_direction", "sess_mactrack_int_sort_direction");

		if ($changed) {
			$_REQUEST["page"] = "1";
		}
	}

	/* remember these search fields in session vars so we don't have to keep passing them around */
	load_current_session_value("page", "sess_mactrack_int_current_page", "1");
	load_current_session_value("totals", "sess_mactrack_int_totals", "on");
	load_current_session_value("rows", "sess_mactrack_int_rows", read_config_option("num_rows_device"));
	load_current_session_value("device_id", "sess_mactrack_int_device_id", "-1");
	load_current_session_value("site", "sess_mactrack_int_site", "-1");
	load_current_session_value("issues", "sess_mactrack_int_issues", "-3");
	load_current_session_value("bwusage", "sess_mactrack_int_bwusage", read_config_option("mactrack_interface_high"));
	load_current_session_value("type", "sess_mactrack_int_type", "-1");
	load_current_session_value("device", "sess_mactrack_int_device", "-1");
	load_current_session_value("period", "sess_mactrack_int_period", "-2");
	load_current_session_value("filter", "sess_mactrack_int_filter", "");
	load_current_session_value("sort_column", "sess_mactrack_int_sort_column", "device_name");
	load_current_session_value("sort_direction", "sess_mactrack_int_sort_direction", "DESC");

	include_once("./plugins/mactrack/general_header.php");
	print "<script type='text/javascript' src='" . $config["url_path"] . "plugins/mactrack/mactrack.js'></script>";

	$sql_where  = "";

	if ($_REQUEST["rows"] == -1) {
		$row_limit = read_config_option("num_rows_device");
	}elseif ($_REQUEST["rows"] == -2) {
		$row_limit = 99999999;
	}else{
		$row_limit = $_REQUEST["rows"];
	}

	$stats = mactrack_get_records($sql_where, TRUE, $row_limit);

	mactrack_tabs();
	html_start_box("<strong>Scanned Device Interfaces</strong>", "100%", $colors["header"], "3", "center", "");
	mactrack_filter_table();
	html_end_box();
	
	//Last device restart:
	if ($_REQUEST["device"]!=-1){
		html_start_box("", "100%", $colors["header"], "3", "center", "");
		print "<tr>";
		if (sizeof($stats)>0)
			$last_run_time = max(strtotime($stats[0]["last_up_time"]),strtotime($stats[0]["last_down_time"]));
		else 
			$last_run_time = read_config_option("mt_last_run_time", TRUE);
		$diff = strtotime("now") - $last_run_time;
		$upTime = ($stats[0]["sysUptime"]/100) + $diff;
		$days      = intval($upTime / (60*60*24));
		$remainder = $upTime % (60*60*24);
		$hours     = intval($remainder / (60*60));
		$remainder = $remainder % (60*60);
		$minutes   = intval($remainder / (60));
		$upTime    = $days . "d:" . $hours . "h:" . $minutes . "m";
		print "<td style='text-align:right'><strong>Last device restart:</strong> ".$upTime."</td>"; 
		print "</tr>";
		html_end_box(false);
	}
	
	html_start_box("", "100%", $colors["header"], "3", "center", "");

	$rows_query_string = "SELECT COUNT(*)
		FROM mac_track_interfaces
		INNER JOIN mac_track_devices
		ON mac_track_interfaces.device_id=mac_track_devices.device_id
		INNER JOIN mac_track_device_types
		ON mac_track_device_types.device_type_id=mac_track_devices.device_type_id
		$sql_where";

	//echo $rows_query_string;

	$total_rows = db_fetch_cell($rows_query_string);

	/* generate page list */
	if ($total_rows > 0) {
		$url_page_select = get_page_list($_REQUEST["page"], MAX_DISPLAY_PAGES, $row_limit, $total_rows, "mactrack_view_interfaces.php?report=interfaces");

		$nav = "<tr bgcolor='#" . $colors["header"] . "' class='noprint'>
					<td colspan='22'>
						<table width='100%' cellspacing='0' cellpadding='0' border='0'>
							<tr>
							<td align='left' class='textHeaderDark'>
									<strong>&lt;&lt; "; if ($_REQUEST["page"] > 1) { $nav .= "<a class='linkOverDark' href='mactrack_view_interfaces.php?page=" . ($_REQUEST["page"]-1) . "'>"; } $nav .= "Previous"; if ($_REQUEST["page"] > 1) { $nav .= "</a>"; } $nav .= "</strong>
								</td>\n
								<td align='center' class='textHeaderDark'>
									Showing Rows " . (($row_limit*($_REQUEST["page"]-1))+1) . " to " . ((($total_rows < $row_limit) || ($total_rows < ($row_limit*$_REQUEST["page"]))) ? $total_rows : ($row_limit*$_REQUEST["page"])) . " of $total_rows [$url_page_select]
								</td>\n
								<td align='right' class='textHeaderDark'>
									<strong>"; if (($_REQUEST["page"] * $row_limit) < $total_rows) { $nav .= "<a class='linkOverDark' href='mactrack_view_interfaces.php?page=" . ($_REQUEST["page"]+1) . "'>"; } $nav .= "Next"; if (($_REQUEST["page"] * $row_limit) < $total_rows) { $nav .= "</a>"; } $nav .= " &gt;&gt;</strong>
								</td>\n
							</tr>
						</table>
					</td>
				</tr>\n";
	}else{
		$nav = "<tr bgcolor='#" . $colors["header"] . "' class='noprint'>
					<td colspan='22'>
						<table width='100%' cellspacing='0' cellpadding='0' border='0'>
							<tr>
								<td align='center' class='textHeaderDark'>
									No Rows Found
								</td>\n
							</tr>
						</table>
					</td>
				</tr>\n";
	}

	print $nav;

	$display_text = mactrack_display_array();

	html_header_sort($display_text, $_REQUEST["sort_column"], $_REQUEST["sort_direction"]);

	$i = 0;
	if (sizeof($stats) > 0) {
		//$stats = order_by_name($stats);
		foreach ($stats as $stat) {
			/* find the background color and enclose it */
			$bgc = mactrack_int_row_color($stat);
			if ($bgc) {
				print "<tr id='row_" . $stat["device_id"] . "_" . $stat["ifName"] . "' style='background-color:#$bgc;'>\n"; $i++;
			}else{
				if (($i % 2) == 1) {
					$current_color = $colors["alternate"];
				}else{
					$current_color = $colors["light"];
				}
				print "<tr id='row_" . $stat["device_id"] . "' style='background-color:#$bgc;'>\n"; $i++;
			}
			print mactrack_format_interface_row($stat);
		}

	}else{
		print "<tr><td colspan='7'><em>No Scanner Devices Found</em></td></tr>";
	}

	/* put the nav bar on the bottom as well */
	print $nav;

	html_end_box(false);
	html_start_box("", "100%", $colors["header"], "3", "center", "");
	print "<tr>";
	mactrack_legend_row("mt_int_up_bgc", "Interface Up");
	mactrack_legend_row("mt_int_up_wo_alias_bgc", "No Alias");
	mactrack_legend_row("mt_int_errors_bgc", "Errors Present");
	mactrack_legend_row("mt_int_discards_bgc", "Discards Present");
	mactrack_legend_row("mt_int_unmapped_bgc", "Unmapped to Tree");
	mactrack_legend_row("mt_int_no_graph_bgc", "No Graphs");
	mactrack_legend_row("mt_int_no_device_bgc", "Not Integrated");
	mactrack_legend_row("mt_int_down_bgc", "Interface Down");
	print "</tr>";
	html_end_box(false);

	mactrack_display_stats();

	print "<div id='response'></div>";
	print "<script>
    	function printreport(id){
			if (id==-1 ||typeof id == 'undefined')
				alert('To generate the report, you must select one device.');
			else
         		window.open('https://monitorizacion.urjc.es/cacti/plugins/mactrack/pdfreport.php?did='+id, '_blank');
    	}
	</script>";
	include_once("./include/bottom_footer.php");
}
function mactrack_view()
{
    global $title, $mactrack_rows, $config;
    mactrack_interfaces_request_validation();
    general_header();
    $sql_where = '';
    if (get_request_var('rows') == -1) {
        $row_limit = read_config_option('num_rows_table');
    } elseif (get_request_var('rows') == -2) {
        $row_limit = 99999999;
    } else {
        $row_limit = get_request_var('rows');
    }
    $stats = mactrack_get_records($sql_where, TRUE, $row_limit);
    mactrack_tabs();
    html_start_box($title, '100%', '', '3', 'center', '');
    mactrack_filter_table();
    html_end_box();
    $rows_query_string = "SELECT COUNT(*)\n\t\tFROM mac_track_interfaces\n\t\tINNER JOIN mac_track_devices\n\t\tON mac_track_interfaces.device_id=mac_track_devices.device_id\n\t\tINNER JOIN mac_track_device_types\n\t\tON mac_track_device_types.device_type_id=mac_track_devices.device_type_id\n\t\t{$sql_where}";
    $total_rows = db_fetch_cell($rows_query_string);
    $nav = html_nav_bar('mactrack_view_interfaces.php?report=interfaces', MAX_DISPLAY_PAGES, get_request_var('page'), $row_limit, $total_rows, 22, __('Interfaces'));
    print $nav;
    html_start_box('', '100%', '', '3', 'center', '');
    $display_text = mactrack_display_array();
    html_header_sort($display_text, get_request_var('sort_column'), get_request_var('sort_direction'));
    $i = 0;
    if (sizeof($stats)) {
        foreach ($stats as $stat) {
            /* find the background color and enclose it */
            $class = mactrack_int_row_class($stat);
            if ($bgc) {
                print "<tr id='row_" . $stat['device_id'] . '_' . $stat['ifName'] . "' class='{$class}'>\n";
                $i++;
            } else {
                if ($i % 2 == 1) {
                    $class = 'odd';
                } else {
                    $class = 'even';
                }
                print "<tr id='row_" . $stat['device_id'] . "' class='{$class}'>\n";
                $i++;
            }
            print mactrack_format_interface_row($stat);
        }
    } else {
        print '<tr><td colspan="7"><em>' . __('No MacTrack Interfaces Found') . '</em></td></tr>';
    }
    html_end_box(false);
    if (sizeof($stats)) {
        print $nav;
    }
    print '<div class="center" style="position:fixed;left:0;bottom:0;display:table;margin-left:auto;margin-right:auto;width:100%;">';
    html_start_box('', '100%', '', '3', 'center', '');
    print '<tr>';
    mactrack_legend_row('int_up', 'Interface Up');
    mactrack_legend_row('int_up_wo_alias', 'No Alias');
    mactrack_legend_row('int_errors', 'Errors Present');
    mactrack_legend_row('int_discards', 'Discards Present');
    mactrack_legend_row('int_no_graph', 'No Graphs');
    mactrack_legend_row('int_down', 'Interface Down');
    print '</tr>';
    html_end_box(false);
    print '</div>';
    if (sizeof($stats)) {
        mactrack_display_stats();
    }
    print '<div id="response"></div>';
    bottom_footer();
}