<?php
	class CommentsComponent extends Component {
		function GetUUIDs() {
			$uuids[] = 'admin\/comments\/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})';
			return $uuids;
		}

		function Run($page, $matches) {
			if (!isset($_SESSION['username']))
			{
				$_SESSION['needlogin'] = 1;
				header("Location: /admin/login");
				die();
			}

			$ip = isset($matches[1])?$matches[1]:false;

			if (isset($_POST['notes']))
			{
				$notes = db_escape(htmlspecialchars($_POST['notes']));
				$ins = get_db_rows("INSERT INTO comments (ip,creator,notes) VALUES ('".$ip."',".$_SESSION['uid'].",E'".$notes."')");
			}

			_page_header(SITE_NAME." :: Comments about ".$ip);
?>
<h2><?=SITE_NAME?> :: Comments about <a href="/lookup?ip=<?=$ip?>"><?=$ip?></a></h2>
<table class="listing2 stripe compact">
	<tr>
		<th class="left">Date added</th>
		<th class="left">Creator</th>
		<th class="left">Notes</th>
	</tr>
<?php
			$res = get_db_rows("SELECT * FROM comments WHERE ip='".$ip."' ORDER BY id DESC");
			while($tuple = get_db_tuple($res))
			{
				$found = TRUE;
?>
	<tr>
		<td><?= date('d-m-Y H:i:s',$tuple['timestamp']) ?></td>
		<td>user<?=$tuple['creator']?></a></td>
		<td><?=nl2br($tuple['notes'])?></td>
	</tr>
<?php
			}
			if (!isset($found))
				echo "\t\t<tr><td colspan=\"3\">No entries found matching your query.</td></tr>\n";
?>
</table>
<form method="post" action="">
	<fieldset>
		<legend>Add your comment</legend>
		<textarea name="notes" cols="80" rows="5"></textarea><br/>
		<input type="submit" value="Submit comment" />
	</fieldset>
</form>
<?php
			_page_footer();
		}

	};

	new CommentsComponent;
?>
