<?php
	class ASAdminComponent extends Component
	{
		public function GetUUIDs()
		{
			$uuids[] = 'admin\/asadmin\/([0-9]+)$';
			$uuids[] = 'admin\/asadmin\/([0-9]+)\/([^\/]*)';
			return $uuids;
		}

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

			$asnum = isset( $matches[1] ) ? intval( $matches[1] ) : 0;
			$action = isset( $matches[2] ) ? $matches[2] : false;
			
			_page_header( SITE_NAME . ' :: AS Administration' . ( $asnum ? ' :: AS' . $asnum : '' ) );

			if( !$action && $asnum)
			{
				if( !_ownas( $asnum ) && !_hasflag( FLAG_GLOBALADMIN ) )
				{
					header( 'Location: /admin' );
					die();
				}
				echo '<h2>AS Administration for AS' . $asnum . '</h2>' . PHP_EOL;
				$query = get_db_rows( 'SELECT * FROM blacklist WHERE asn=' . $asnum );
				$ips = array();
				$result = array();
				while( $tuple = get_db_tuple( $query ) )
				{
					if (!isset($ips[$tuple['ip']]))						
						$ips[$tuple['ip']][] = array('type' => $tuple['type'], 'listed' => $tuple['listed']);
					else if (in_array(array('type' => $tuple['type'], 'listed' => $tuple['listed']), $ips[$tuple['ip']]))
						continue;
					$result[] = $tuple;
				}
				$total = count( $result );
				echo '<p>A total of ' . $total . ' records match</p>' . PHP_EOL;
?>
<table class="listing2">
	<thead>
		<tr>
			<th class="left">Added</th>
			<th class="left">IP</th>
			<th class="left">Active</th>
			<th class="left">Type</th>
			<th class="left">Description</th>
			<th class="left">Comment</th>
		</tr>
	</thead>
	<tbody>
<?php
				foreach($result as $tuple)
				{
?>
		<tr>
			<td><?= date( 'd-m-Y H:i:s T', $tuple['timestamp'] ) ?></td>
			<td><a href="/lookup?ip=<?= $tuple['ip'] ?>"><?= $tuple['ip'] ?></a></td>
			<td><?= $tuple['listed'] ? 'Yes' : 'No' ?></td>
			<td><?= $tuple['type'] ?></td>
			<td><?= ClassInfo( $tuple['type'] ) ?></td>
			<td><?= $tuple['comment'] ?></td>
		</tr>
<?php
				}
?>
	</tbody>
</table>
<?php
			}
			else if( $action == 'assign' && $asnum )
			{
				if( !_hasflag( FLAG_GLOBALADMIN ) )
				{
					header( 'Location: /admin/rpckey' );
					die();
				}

				$udata = get_db_tuple( get_db_rows( 'SELECT * FROM rpckey WHERE id=' . $asnum ) );
				if( !$udata )
				{
					header( 'Location: /admin/rpckey');
					die();
				}
				echo '<h2>AS Administration for ' . $udata['notes'] . ' (' . $udata['email'] . ')</h2>' . PHP_EOL .
					'<a href="/admin/rpckey">&laquo; Return to rpckey manager</a><br/><br/>' . PHP_EOL;

				if( isset( $_REQUEST['asn'] ) )
				{
					if( isset( $_REQUEST['doit'] ) )
					{
						$add = get_db_rows( 'INSERT INTO asadmins (keyid, asnum) VALUES (' . $asnum . ',' . intval( $_REQUEST['asn'] ) . ')' );
						header( 'Location: /admin/asadmin/'.$asnum.'/assign');
						exit;
					}
					else
					{
						$asn = intval($_REQUEST['asn']);
?>
<form method="post" action="">
	<input type="hidden" name="asn" value="<?=$asn?>" />
	<input type="hidden" name="doit" value="1" />
	<h2>WHOIS information about AS<?=$asn?></h2>
	<div class="asadmin">
		<pre><?php system('/usr/bin/whois AS'.$asn); ?></pre>
	</div>
	<input type="submit" value="Yes, assign this entry" />
	<input type="button" onclick="history.go(-1)" value="No, return to the previous page" /> 
</form>
<?php
					}
				}
				else
				{
?>
<form method="post" action="">
	<label for="asn">Assign ASN:</label><input type="text" id="asn" name="asn" value="" /><input type="submit" value="add" />
</form>
<h3>Current assignments</h3>
<?php
					$aslist = get_db_rows( 'SELECT * FROM asadmins WHERE keyid=' . $asnum );
					while( $row = get_db_tuple( $aslist ) )
					{
						$count = get_db_tuple( get_db_rows( 'SELECT COUNT(DISTINCT(ip)) FROM blacklist WHERE listed=1 AND asn=' . $row['asnum'] ) );
						echo 	'<a href="/admin/asadmin/' . $row['asnum'] . '">AS' . $row['asnum'] . '</a> (' . $count[0] . ' active listings) ' .
							'<a href="/admin/asadmin/' . $asnum . '/unassign?id=' . $row['id'] . '" onclick="return confirm(\'Are you sure you want to unassign AS' . $row['asnum'] .'\');">Unassign</a><br/>' . PHP_EOL;
					}
				}
			}
			else if( $action == 'unassign' && is_numeric( $_REQUEST['id'] ) )
			{
				$remove = get_db_rows( 'DELETE FROM asadmins WHERE id=' . $_REQUEST['id'] );
				header( 'Location: /admin/asadmin/'.$asnum.'/assign' );
				exit;
			}
			else
			{
?>
<p>An unsupported action was requested: <code><?= $action ?></code></p>
<?php
			}

			_page_footer();
		}

	};

	new ASAdminComponent;
?>
