<?php
require_once ($_SERVER['DOCUMENT_ROOT']."/inc/auth.php");
require_once ($_SERVER['DOCUMENT_ROOT']."/inc/languages/" . HTML_LANG . ".php");
require_once ($_SERVER['DOCUMENT_ROOT']."/inc/header.php");
require_once ($_SERVER['DOCUMENT_ROOT']."/inc/datetimefilter.php");

$f_ip = getParam('ip', $page_url, '');
$_SESSION[$page_url]['ip'] = $f_ip;

print_log_submenu($page_url);

?>

<div id="cont">
<br>
<?php echo WEB_log_mac_history_hint; ?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<?php print_date_fields($date1,$date2,$date_shift); ?>
<?php echo WEB_log_select_ip_mac; ?>:&nbsp<input type="text" name="ip" value="<?php echo $f_ip; ?>" pattern="^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])|([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}|([0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4})|[0-9A-Fa-f]{12})$"/>
<?php print WEB_rows_at_page."&nbsp"; print_row_at_pages('rows',$displayed); ?>
<input type="submit" value="<?php echo WEB_btn_show; ?>">
</form>

<?php
$params = [$date1, $date2];
$conditions = [];

if (!empty($f_ip)) {
    if (checkValidIp($f_ip)) {
        $ip_long = sprintf('%u', ip2long($f_ip));
        $conditions[] = "ip_int = ?";
        $params[] = $ip_long;
    } elseif (checkValidMac($f_ip)) {
        $conditions[] = "mac = ?";
        $params[] = mac_dotted($f_ip);
    }
}

$whereClause = !empty($conditions) ? ' AND ' . implode(' AND ', $conditions) : '';

$countSQL = "SELECT COUNT(*) FROM user_auth WHERE ts >= ? AND ts < ?" . $whereClause;
$count_records = (int)get_single_field($db_link, $countSQL, $params);
$total = ceil($count_records / $displayed);
$page = max(1, min($page, $total));
$start = ($page - 1) * $displayed;

print_navigation($page_url, $page, $displayed, $count_records, $total);

$dataParams = array_merge($params, [$displayed, $start]);

$sSQL = "
    SELECT * FROM user_auth 
    WHERE ts >= ? AND ts < ?" . $whereClause . "
    ORDER BY id DESC 
    LIMIT ? OFFSET ?
";

$iplog = get_records_sql($db_link, $sSQL, $dataParams);

?>
<br>
<table class="data">
		<tr align="center">
				<td class="data"><b>id</b></td>
				<td class="data" width=150><b><?php echo WEB_cell_created; ?></b></td>
				<td class="data" width=150><b><?php echo WEB_cell_last_found; ?></b></td>
				<td class="data"><b><?php echo WEB_cell_ip; ?></b></td>
				<td class="data"><b><?php echo WEB_cell_mac; ?></b></td>
				<td class="data"><b><?php echo WEB_cell_dhcp_hostname; ?></b></td>
				<td class="data"><b><?php echo WEB_cell_dns_name; ?></b></td>
		</tr>

<?php

foreach ($iplog as $row) {
    print "<tr align=center class=\"tr1\" onmouseover=\"className='tr2'\" onmouseout=\"className='tr1'\">\n";
    print "<td class=\"data\">" . $row['id'] . "</td>\n";
    print "<td class=\"data\">" . get_datetime_display($row['ts']) . "</td>\n";
    print "<td class=\"data\">" . get_datetime_display($row['last_found']) . "</td>\n";
    if (isset($row['id']) and $row['id'] > 0) {
        print "<td class=\"data\"><a href=/admin/users/editauth.php?id=".$row['id'].">" . $row['ip'] . "</a></td>\n";
    } else {
        print "<td class=\"data\">" . $row['ip'] . "</td>\n";
    }
    print "<td class=\"data\">" . expand_mac($db_link,mac_dotted($row['mac'])) . "</td>\n";
    print "<td class=\"data\">" . $row['dhcp_hostname'] . "</td>\n";
    print "<td class=\"data\">" . $row['dns_name'] . "</td>\n";
    print "</tr>\n";
}
print "</table>\n";
print_navigation($page_url,$page,$displayed,$count_records,$total);
require_once ($_SERVER['DOCUMENT_ROOT']."/inc/footer.php");
?>
