PHP OA ServerConnect v1.1 - bash script php -S

PHP OA ServerConnect v1.1 - download here

Screenshots: shot1 - shot2

<?php
/**
 * PHP OA ServerConnect
 *
 * save the script as connect.php
 *
 * Serverlist powered by dpmaster.deathmask.net
 * please change the $openarena variable for your needs
 * you only need php5-cli, kill, grep, ps and a browser to run this:
 * php -S localhost:8080 connect.php
 * load the page localhost:8080 in your browser
 * the script uses the build-in php webserver
 *
 * @version 1.1 - 27.08.16
 * @changes 1.1: minor fixes
 *
 * (C) 2015-2016 by ThEPaniC/Evil|Pan!C -> mail (at) thepanic.eu
 * Released under GPL v2 or later
 * http://www.gnu.org/licenses/gpl-2.0.html
 */

#########################################################
$openarena= "openarena"; #command or path to openarena ##
#########################################################


$script = $_SERVER['SCRIPT_NAME'];

        if(isset($_GET['killphpserver']))
        {
        ob_end_clean();
        header("Connection: close");
        ob_start();
       
        printhead();#print the html head
        printserverbody();
        echo 'Send kill signal to PHP local Webserver... Thanks for using PHP OA ServerConnect</div>'; #show something..
        printfooter();
       
        $size = ob_get_length();
        header("Content-Length: $size");
        ob_end_flush();
        flush();
        $exec ="kill $(ps -ef | grep php | grep connect.php |  grep -v grep  | awk ' { print $2 } ' )";#kill command for the build in php webserver
        exec($exec); #do it baby!
        }
        elseif(isset($_POST['connect']) && isset($_POST['servername']))
        {
        $servername = $_POST['servername'];
        $connect = $_POST['connect'];
       
        ob_end_clean();
        header("Connection: close");
        ob_start();
       
        printhead();#print the html head
        printserverbody();
        printbutton($script, $connect, $servername);
        echo '<p><strong>Connecting to "'.$servername.'" address: '.$connect.'</strong></p>';
       
        printsingleserver($servername, $connect);#output the single server lookup from dpmaster, cleaned up
        printfooter();
        $exec = "$openarena +skip +connect $connect > /dev/null 2>/dev/null &"; #execution line, start OA, skip the logo and connect to chosen server.
        exec($exec); #do it baby!
        $size = ob_get_length();
        header("Content-Length: $size");
        ob_end_flush();
        flush();
        }
        elseif(isset($_GET['server']) && isset($_GET['servername']))
        {
        printhead();
        printserverbody();
        $connect = $_GET['server'];
        $servername = $_GET['servername'];
        printbutton($script, $connect, $servername);
       
        printsingleserver($servername, $connect);#output the single server lookup from dpmaster, cleaned up
       
        printfooter();
        }
        else
        {      
        $hideempty = "0";
                if(isset($_GET['hideempty']))
                $hideempty = "1";
               
        $openmafia = "0";
                if(isset($_GET['openmafia']))
                $openmafia = "1";
        $search = "";
                if(isset($_POST['search']))
                $search = $_POST['search'];
               
        printhead();#output the hdml head
        printserverlistbody($script,$search);#print the html body for the serverlist
        $servers= file_get_contents('http://dpmaster.deathmask.net/?game=openarena&showplayers=1&hideheader=1&hidegames=1');
                if(!$servers)
                die('<p>unable to fetch serverlist from dpmaster.deathmask.net</p>');
        $cuthead = explode('<div class="serverlist">',$servers);
        $serverlist = str_replace('<br />', '<br>', $cuthead[1]); #we're using html4 here.. mostly
        $serverlist = str_replace('double domination', 'doubledom.', $serverlist); #fixes some layout problems..
        $cutfooter = explode('<p style="size: -1;">', $serverlist);
        $teile = explode('<a id="show_', $cutfooter[0]);
        $count = 0;
                foreach($teile as $server)
                {
                        if($count ==0)
                        {
                        $count=1;
                        continue;
                        }
                        if(!empty($search))
                        {
                                $found = playersearch($server,$search);
                                if(empty($found))
                                continue;
                        }

                       
                       
                        if(!empty($openmafia))
                        {
                                if(stristr($server, '144.76.100.87') === FALSE)
                                continue;
                        }
                $ping = "";
                $server = '<a id="show_'.$server;

                        if(!empty($hideempty))
                        {
                        $botserver = "1";
                        $checkplayers = explode('<div id="spacer">&nbsp;</div>', $server);
                                foreach($checkplayers as $player)
                                {
                                $ping = matchplayerping($player);
                                        if(!empty($ping))
                                        {
                                        $botserver = "0";
                                        break;
                                        }
                                }
                               
                                if(empty($botserver))
                                {
                                $srvarray[] = buildserverarray($server,$script);
                                $count=2;
                                }
                        }
                        else
                        {
                                $srvarray[] = buildserverarray($server,$script);
                                $count=2;
                        }
                       
                       

                }
               
                if(!isset($srvarray))
                {
                echo '<a href="'.$script.'"><span><div id="address"><font size="3" color="yellow">&nbsp;&nbsp;No Servers found!</font></div><div id="players">&nbsp;</div><div id="map">&nbsp;</div><div id="gametype">&nbsp;</div><div id="name">&nbsp;</div></span></a><br />';
                }
                else
                {
                usort($srvarray, 'compareserver'); #sort the $srvarray by humans>bots>empty servers!
                        foreach($srvarray as $server)
                        {
                        echo '<div>'.$server['server']; #output the server-data
                        }
                }
               
        echo '<center>'.$cutfooter[1].'</center><br><br>';#print the powered by dpmaster at the end
        printfooter();
        }
       
       
function compareserver($value_a, $value_b) #compare servers by human players, for usort
{
$a = $value_a['humans'];
$b = $value_b['humans'];
        if ($a == $b)
        return 0;
       
        return ($a < $b) ? +1 : -1;
}

function matchserver($server) #Server IP:Port
{
  $txt=$server;
  $re1='(<)';   # Any Single Character 1
  $re2='(a)';   # Any Single Character 2
  $re3='(\\s+)';        # White Space 1
  $re4='(href)';        # Word 1
  $re5='(=)';   # Any Single Character 3
  $re6='(")';   # Any Single Character 4
  $re7='(\\?)'; # Any Single Character 5
  $re8='(game)';        # Word 2
  $re9='(=)';   # Any Single Character 6
  $re10='(openarena)';  # Word 3
  $re11='(&)';  # Any Single Character 7
  $re12='(server)';     # Word 4
  $re13='(=)';  # Any Single Character 8
  $re14='((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?![\\d])';        # IPv4 IP Address 1
  $re15='(:)';  # Any Single Character 9
  $re16='(\\d+)';       # Integer Number 1
  $re17='(&)';  # Any Single Character 10
  $re18='(showplayers)';        # Word 5
  $re19='(=)';  # Any Single Character 11
  $re20='(1)';  # Day 1

        if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6.$re7.$re8.$re9.$re10.$re11.$re12.$re13.$re14.$re15.$re16.$re17.$re18.$re19.$re20."/is", $txt, $matches))
        {
        $ipaddress1=$matches[14][0];
        $c9=$matches[15][0];
        $int1=$matches[16][0];
        $address = $ipaddress1.$c9.$int1;
        return $address;
        }
}

function printbutton($script, $connect, $servername)#print connect and back buttons
{
        echo '<form method="post" action="'.$script.'"><input type="hidden" name="connect" value="'.$connect.'"><input type="hidden" name="servername" value="'.$servername.'"><input type="submit" value="Connect"></form>';
        echo '<form method="post" action="'.$script.'"><input type="button" value="Back without reload" onclick="window.history.back()"> <input type="hidden" name="back" value=""><input type="submit" value="Back (reload)"> </form>';
}

function buildserverarray($server,$script) #build a singleserverarray for the serverlist
{
$servername = matchservername($server);
$servername = strip_tags($servername);
$servername = trim($servername);
$address = matchserver($server);
$replace = '?servername='.$servername.'&amp;server='.$address.'';
$server=replace_js($server,$replace);
$replace = '</span></a><br><form method="post" action="'.$script.'"><input type="hidden" name="connect" value="'.$address.'"><input type="hidden" name="servername" value="'.$servername.'"><input type="submit" value="Connect"></form>';
$server= str_replace('</span></a>', $replace,$server);
$server = str_replace('<a href="?game=openarena&server=', '<a href="?servername='.$servername.'&amp;server=', $server);
$server = str_replace('&showplayers=1&hidegames=1&hideheader=1">', '">', $server);
$arrays = replace_bot($server);
$server = $arrays[0];
$countbots = $arrays[1];
$humancalc = 1;
        if(!empty($countbots))
        {
                if($countbots < 2)
                $replace = '<div id="handle">NAME</div><br><div id="spacer">&nbsp;</div><div id="ping">-</div><div id="frags">-</div><div id="handle"><font color="yellow">'.$countbots.' bot online</font></div><br>';
                else
                $replace = '<div id="handle">NAME</div><br><div id="spacer">&nbsp;</div><div id="ping">-</div><div id="frags">-</div><div id="handle"><font color="yellow">'.$countbots.' bots online</font></div><br>';
        $server = str_replace('<div id="handle">NAME</div><br>', $replace, $server);
        $humancalc = 2;
        }
$humancount = substr_count($server, '<div id="spacer">&nbsp;</div>') - $humancalc;
$serverprint = '<br>'.$server.'<br>';
$serverarray['server'] = $serverprint;
$serverarray['humans'] = $humancount;
return $serverarray;
$count = 2;
}

function matchplayerping($player) #playerping to find botservers
{
  $txt=$player;
  $re1='()';    # Tag 1
  $re2='(\\d+)';        # Integer Number 1
  $re3='(ms)';  # ping
  $re4='(<\\/div>)';    # Tag 2

        if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4."/is", $txt, $matches))
        {
        $int1=$matches[2][0];
        $ping = $int1;
        return $ping;
        }
}

function matchservername($server) #get the servername
{
  $txt=$server;
  $re1='(<\\/div>)';    # Tag 1
  $re2='(<div id="name">)';     # Tag 2
  $re3='.*?';   # Non-greedy match on filler
  $re4='(<\\/div>)';    # Tag 3

        if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4."/is", $txt, $matches))
        {
        $servername=$matches[0][0];
        return $servername;
        }

}

function printserverlistbody($script,$search) #output serverlist body with killPHPserver-button and a few links
{
echo '<body><div class="serverlist">';
echo '<form method="get" action="'.$script.'"><input type="hidden" name="killphpserver" value="1"><input type="submit" value="kill PHP Webserver"></form>';
        echo '
        <form action="'
.$script.'" method="post">
        search for: <input name="search" size="50" type="text" value="'
.htmlentities($search).'">
        <input value="send" type="submit">
        </form>'
;
echo '<p  style="font-size:medium;"><a href="'.$script.'?openmafia">show only openmafia servers</a> - <a href="'.$script.'?hideempty">show only populated servers</a> - <a href="'.$script.'">show all servers</a></p><hr>';
echo '
        <div style="float: left;">&nbsp;&nbsp;&nbsp;</div><div id="address" class="serverlistheader">Address</div>
        <div id="players" class="serverlistheader">Players</div>
        <div id="map" class="serverlistheader">Map</div>
        <div id="gametype" class="serverlistheader">Gametype</div>
        <div id="name" class="serverlistheader">Name</div><br />'
;
}

function printserverbody()
{
echo '<body><div class="serverlist">';
}

function printfooter()
{
echo '</body></html>';
}

function printhead() #output html head with favicon, and css
{
$css = file_get_contents('http://dpmaster.deathmask.net/game/openarena.css');
$css = str_replace('width:100px;', 'min-width:100px;', $css);
        echo '<html>
                <head>
                <title>PHP OA ServerConnect 1.0</title>
                <link rel="icon" type="image/png" href="data:image/png;base64,AAABAAEAEBACAAAAAACwAAAAFgAAACgAAAAQAAAAIAAAAAEAAQAAAAAAQAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAA////AP//AACBMwAAmTMAAJkDAACZMwAAgQMAAP//AAD//wAAAAAAAAAAAABCUAAAclwAAEvSAABKUgAAclwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" />
                <style type="text/css">
                        '
.$css.'
                        </style>
                </head>
'
;

}

function replace_bot($server) #remove bot players from a serverstring and count bots
{
  $txt = $server;
  $re1='(<div id="spacer">)';   # Tag 1
  $re2='(&)';   # Any Single Character 1
  $re3='(nbsp)';        # Word 1
  $re4='(;)';   # Any Single Character 2
  $re5='(<\\/div>)';    # Tag 2
  $re6='(<div id="ping">)';     # Tag 3
  $re7='(0)';   # Integer Number 1
  $re8='(ms)';  # Word 2
  $re9='(<\\/div>)';    # Tag 4
  $re10='(<div id="frags">)';   # Tag 5
  $re11='(\\d+)';       # Integer Number 2
  $re12='(<\\/div>)';   # Tag 6
  $re13='(<div id="handle">)';  # Tag 7
  $re14='.*?';  # Non-greedy match on filler
  $re15='(<\\/div>)';   # Tag 8
  $re16='(<br>)';       # Tag 9

        $suchmuster = "/".$re1.$re2.$re3.$re4.$re5.$re6.$re7.$re8.$re9.$re10.$re11.$re12.$re13.$re14.$re15.$re16."/is";
        $ersetzung = '';
        $txt = preg_replace($suchmuster, $ersetzung, $txt,-1,$count);
        $array[0] = $txt;
        $array[1] = $count;
        return $array;
}

function replace_js($txt,$replace)#replace javascript links
{
$re1='(javascript)';    # Word 1
$re2='(:)';     # Any Single Character 1
$re3='(showplayers)';   # Word 2
$re4='(\\()';   # Any Single Character 2
$re5='(\\d+)';  # Integer Number 1
$re6='(\\))';   # Any Single Character 3
 $muster = "/".$re1.$re2.$re3.$re4.$re5.$re6."/is";

  $ersetzung = $replace;
  $txt = preg_replace($muster, $ersetzung, $txt,-1,$count);
 
  $re3='(hideplayers)'; # Word 2
 $muster = "/".$re1.$re2.$re3.$re4.$re5.$re6."/is";
  $ersetzung = $replace;
  $txt = preg_replace($muster, $ersetzung, $txt,-1,$count);
  return $txt;
}
 
function printsingleserver($servername, $connect) #print out cleaned dpmaster single server lookup
{
$server= file_get_contents('http://dpmaster.deathmask.net/?game=openarena&server='.$connect.'&hideheader=1&hidegames=1');
                if(!$server)
                die('<p>unable to fetch serverinfos from dpmaster.deathmask.net</p>');
               
$replace = '?servername='.$servername.'&amp;server='.$connect.'';
$server=replace_js($server,$replace);

$server = str_replace('<a href="?game=openarena&server=', '<a href="?servername='.$servername.'&amp;server=', $server);
$server = str_replace('&hidegames=1&hideheader=1">', '">', $server);
$cut=explode('<div style="clear: both;">Server reply:', $server);
echo '<div style="clear: both;">'.$cut[1].'<br><br>';
}
function playersearch($server,$needle) #very simple player/server search, but works..
{
$server = strip_tags($server);
        if (stristr($server, $needle))
        return "1";
}

?>