<?php
session_start();
echo "<font face=arial size=2>";

//edit this
$host='localhost'; // this is usually localhost, but might be something else
$username='root'; //database username here
$pass=''; //database password here
$database='servers'; // database name here

$sql = 'CREATE TABLE `servers` ('
        . ' `id` INT(11) NOT NULL, '
        . ' `host` VARCHAR(250) NOT NULL, '
        . ' `port` VARCHAR(250) NOT NULL'
        . ' )'
        . ' ENGINE = myisam'
        . ' CHARACTER SET latin1 COLLATE latin1_bin;';

mysql_connect ($host, $username, $pass);
mysql_select_db($database) or die('Could not connect to the Database');

$action = $_GET['action'];
if(!$action){
echo "<a href=$_SERVER[PHP_SELF]?action=1>Add new server</a><br /><table width=400><tr><td><font size=2><b>Address</d></font></td><td><font size=2><b>Port</b></font></td><td><font size=2><b>Status</b></font></td></tr>";
$query = mysql_query("SELECT * FROM servers");
while ($row = @mysql_fetch_array($query)){
@$fp = fsockopen($row[host], $row[port], $no, $str, 4);
if($fp){
echo "<tr><td>$row[host]</td><td>$row[port]</td><td><font face=arial color=#00ff00><b>Online</b></font></td></tr>";
}else{
echo "<tr><td>$row[host]</td><td><font face=arial color=#ff0000><b>Offline</b></font></td></tr>";
}}
echo "</table><br /><a href=$_SERVER[PHP_SELF]?action=1>Add new server</a>"; }
if($action>0){
echo "<a href=$_SERVER[PHP_SELF]>Back to server list</a><br />";
if(isset($submit)){
@$fp = fsockopen($_POST[host], $_POST[port], $no, $str, 4);
if($fp){
$number = @mysql_num_rows(mysql_query("SELECT * FROM servers WHERE host='$_POST[host]'"));
if($number==0){
mysql_query("INSERT INTO servers(`host`, `port`)VALUES('$_POST[host]', '$_POST[port]')");
echo "$_POST[host] $_POST[port] has succesfully been added to the database"; }
if($number>0){ echo "This host has already been submited to our database"; }
}else{
echo "There was a problem connecting to the set server, please check the feilds<br />
<form action='$_SERVER[PHP_SELF]?action=1' method=post name=form>
<table width=400>
<tr><td valign=left>host:(without http://)</td><td><input type=text name=host value=$_POST[host]&#160; /></td></tr>
<tr><td valign=left>Port:</td><td><input type=text name=port value=$_POST[port]&#160; /></td></tr>
<tr><td valign=left><input type=submit name=submit value=Submit&#160; /></td></tr>
</table></form>
"; }}
if(!isset($submit)){
echo "
<form action='$_SERVER[PHP_SELF]?action=1' method=POST name=form>
<table width=400>
<tr><td valign=left><font size=2>Host:(without http://)</font></td><td><input type=text name=host value=$_POST[host]&#160; /></td></tr>
<tr><td valign=left><font size=2>Port:</font></td><td><input type=text name=port value=$_POST[port]&#160; /></td></tr>
<tr><td valign=left><input name=submit type=submit value=Submit></td></tr>
</table></form>
"; }
echo "<br /><a href=$_SERVER[PHP_SELF]>Back to server list</a>";
}


echo "</font>";

?>