<?php
/**
 * PageOps access-log viewer  —  password protected.
 * Put it in the same folder as log.php / connector.html.
 * CHANGE THE PASSWORD on the next line before uploading.
 */
session_start();
$PASSWORD = "400666";                       // <-- CHANGE THIS
$logFile  = __DIR__ . '/logs/access.jsonl';
$csvFile  = __DIR__ . '/logs/access.csv';

/* logout */
if (isset($_GET['logout'])) { $_SESSION = array(); session_destroy(); header('Location: logview.php'); exit; }

/* login */
$err = '';
if (isset($_POST['pw'])) {
    if (hash_equals($PASSWORD, (string)$_POST['pw'])) { $_SESSION['logview_ok'] = true; header('Location: logview.php'); exit; }
    else { $err = 'Wrong password.'; }
}
$authed = !empty($_SESSION['logview_ok']);

/* authed CSV download (served via PHP so the .htaccess deny doesn't block you) */
if ($authed && isset($_GET['download']) && $_GET['download'] === 'csv' && file_exists($csvFile)) {
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="access-log-' . date('Ymd') . '.csv"');
    readfile($csvFile);
    exit;
}

function e($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }

/* ---------- Login screen ---------- */
if (!$authed) { ?>
<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Access Log — PageOps</title>
<style>
  body{background:#1e1e1e;color:#f0ede8;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;display:flex;min-height:100vh;align-items:center;justify-content:center;margin:0;}
  .box{background:#252525;border:0.5px solid rgba(255,255,255,0.2);border-radius:12px;padding:26px 28px;width:300px;text-align:center;}
  .logo{width:40px;height:40px;border-radius:50%;border:2px solid #EF9F27;display:flex;align-items:center;justify-content:center;margin:0 auto 14px;background:#0d0d0f;font-weight:800;}
  .logo b{color:#fff}.logo i{color:#EF9F27;font-style:normal}
  h1{font-size:15px;margin:0 0 16px;font-weight:600;}
  input{width:100%;padding:9px 10px;border-radius:8px;border:0.5px solid rgba(255,255,255,0.22);background:#1e1e1e;color:#f0ede8;font-size:14px;box-sizing:border-box;margin-bottom:10px;}
  button{width:100%;padding:9px;border-radius:8px;border:none;background:#EF9F27;color:#1a1a1a;font-weight:700;font-size:14px;cursor:pointer;}
  .err{color:#F09595;font-size:12px;margin-bottom:8px;}
</style></head><body>
  <form class="box" method="post">
    <div class="logo"><b>P</b><i>O</i></div>
    <h1>Access Log</h1>
    <?php if($err) echo '<div class="err">'.e($err).'</div>'; ?>
    <input type="password" name="pw" placeholder="Password" autofocus>
    <button type="submit">View log</button>
  </form>
</body></html>
<?php exit; }

/* ---------- Read log ---------- */
$rows = array(); $total = 0; $uniq = array();
if (file_exists($logFile)) {
    $lines = file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $total = count($lines);
    foreach ($lines as $ln) { $o = json_decode($ln, true); if ($o && isset($o['ip'])) $uniq[$o['ip']] = 1; }
    $last = array_reverse(array_slice($lines, -500));
    foreach ($last as $ln) { $o = json_decode($ln, true); if ($o) $rows[] = $o; }
}
?>
<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Access Log — PageOps</title>
<style>
  *{box-sizing:border-box}
  body{background:#1e1e1e;color:#f0ede8;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;margin:0;padding:18px;font-size:13px;}
  .top{display:flex;align-items:center;gap:10px;border-bottom:2px solid #EF9F27;padding-bottom:12px;margin-bottom:14px;flex-wrap:wrap;}
  .logo{width:30px;height:30px;border-radius:50%;border:1.5px solid #EF9F27;display:flex;align-items:center;justify-content:center;background:#0d0d0f;font-weight:800;font-size:13px;}
  .logo b{color:#fff}.logo i{color:#EF9F27;font-style:normal}
  h1{font-size:16px;margin:0;font-weight:600;}
  .stats{color:#aaa;font-size:12px;margin-left:auto;display:flex;gap:14px;align-items:center;flex-wrap:wrap;}
  .stats b{color:#EF9F27}
  a.btn,input.search{border:0.5px solid rgba(255,255,255,0.22);background:#252525;color:#f0ede8;border-radius:7px;padding:6px 12px;font-size:12px;text-decoration:none;display:inline-block;}
  input.search{width:220px;}
  table{width:100%;border-collapse:collapse;}
  th,td{text-align:left;padding:6px 8px;border-bottom:0.5px solid rgba(255,255,255,0.08);white-space:nowrap;font-size:12px;}
  th{color:#888;font-weight:600;text-transform:uppercase;font-size:10px;letter-spacing:0.04em;position:sticky;top:0;background:#1e1e1e;}
  td.wrap{white-space:normal;max-width:280px;overflow:hidden;text-overflow:ellipsis;color:#aaa;}
  tr:hover td{background:#252525;}
  .ip{color:#85B7EB;font-family:monospace;}
  .muted{color:#777;}
</style></head><body>
  <div class="top">
    <div class="logo"><b>P</b><i>O</i></div>
    <h1>Access Log</h1>
    <div class="stats">
      <span>Total visits: <b><?php echo $total; ?></b></span>
      <span>Unique IPs: <b><?php echo count($uniq); ?></b></span>
      <span>Showing last <b><?php echo count($rows); ?></b></span>
      <input class="search" id="q" placeholder="Filter (IP, city, page…)" oninput="filt()">
      <a class="btn" href="logview.php?download=csv">Download CSV</a>
      <a class="btn" href="logview.php?logout=1">Log out</a>
    </div>
  </div>
  <div style="overflow-x:auto;">
  <table id="tbl">
    <thead><tr>
      <th>Time</th><th>IP</th><th>City</th><th>Region</th><th>Country</th><th>ISP</th><th>Page</th><th>Browser</th>
    </tr></thead>
    <tbody>
    <?php if (!$rows): ?>
      <tr><td colspan="8" class="muted" style="padding:2rem;text-align:center;">No visits logged yet.</td></tr>
    <?php else: foreach ($rows as $r): ?>
      <tr>
        <td class="muted"><?php echo e(isset($r['time'])?str_replace('T',' ',substr($r['time'],0,19)):''); ?></td>
        <td class="ip"><?php echo e(isset($r['ip'])?$r['ip']:''); ?></td>
        <td><?php echo e(isset($r['city'])?$r['city']:''); ?></td>
        <td><?php echo e(isset($r['region'])?$r['region']:''); ?></td>
        <td><?php echo e(isset($r['country'])?$r['country']:''); ?></td>
        <td class="muted"><?php echo e(isset($r['isp'])?$r['isp']:''); ?></td>
        <td><?php echo e(isset($r['page'])?$r['page']:''); ?></td>
        <td class="wrap" title="<?php echo e(isset($r['ua'])?$r['ua']:''); ?>"><?php echo e(isset($r['ua'])?$r['ua']:''); ?></td>
      </tr>
    <?php endforeach; endif; ?>
    </tbody>
  </table>
  </div>
<script>
function filt(){
  var q=document.getElementById('q').value.toLowerCase();
  var rows=document.querySelectorAll('#tbl tbody tr');
  rows.forEach(function(tr){ tr.style.display = tr.innerText.toLowerCase().indexOf(q)>-1 ? '' : 'none'; });
}
</script>
</body></html>