<?php
	class KeyentriesComponent extends Component {
		function GetUUIDs() {
			$uuids[] = 'admin\/keyentries$';
			$uuids[] = 'admin\/keyentries\/([^\/]*)';
			return $uuids;
		}

		function Run($page, $matches) {
			if (!isset($_SESSION['username']))
			{
				$_SESSION['needlogin'] = 1;
				header("Location: /admin/login");
				die();
			}
			
			$title = 'Your Entries';
			$uid = $_SESSION['uid'];
			if (_hasFlag(FLAG_RPCKEY) && isset($matches[1]))
			{
				$uid = intval($matches[1]);
				$user = get_db_tuple(get_db_rows("SELECT * FROM rpckey WHERE id=".$uid." LIMIT 1"));
				if (!$user) {
					if (!isset($_REQUEST['ajax']))
						header("Location: /admin");
					die();
				}
				$title = ucfirst($user['notes']).'\'s entries';
			}

			if (isset($_REQUEST['json']))
			{
				$ret = [];
				$q = get_db_rows("SELECT * FROM blacklist WHERE uid_add='".$uid."' ORDER BY timestamp DESC LIMIT 50000");
				while ($tuple = get_db_tuple($q))
				{
					$ret[] = [
						date('Y-m-d H:i:s',$tuple['timestamp']),
						$tuple['id'],
						'<a href="/lookup?ip='.$tuple['ip'].'">'.$tuple['ip'].'</a>',
						($tuple['listed']==1?'Yes':'No'),
						($tuple['ts_del'] >0 ? date('Y-m-d H:i:s',$tuple['ts_del']):'-'),
						$tuple['type'],
						ClassInfo($tuple['type']),
						$tuple['comment']
					];
				}
				die(json_encode(['data'=>$ret]));
			}
			
			_page_header(SITE_NAME." :: Admin :: ".$title);
?>
<a href="/">home</a> / <a href="/admin">administrate</a> / <?=strtolower($title)?>
<h2><?=$title?></h1>
<table class="listing3 stripe compact top10 colsearch" data-src="?json=1">
	<thead>
		<tr>
			<th>Timestamp</th>
			<th>Id</th>
			<th>IP</th>
			<th>Active</th>
			<th>Remove TS</th>
			<th>Type</th>
			<th>Description</th>
			<th>Comment</th>
		</tr>
	</thead>
	<tbody></tbody>
</table>
<?php
			_page_footer();
		}
	};

	new KeyentriesComponent;
?>
