#!/usr/bin/php -q
<?php
include("/system/init.php");

class 
Engine {
    var 
$address '0.0.0.0';
    var 
$port '1984';
    var 
$max_clients 5;
    var 
$this->clientss = array();
    var 
$banned = array();

    var 
$sock

    
function init() {
        
set_time_limit (0); 

        
$this->sock socket_create(AF_INETSOCK_STREAM0); 
        if (!@
socket_setopt($this->sock,SOL_SOCKET,SO_REUSEADDR,1)) {
               echo 
"socket_setopt() failed: reason: ".socket_strerror(socket_last_error($this->sock))."\n";
               exit;
        }

        
socket_bind($this->sock$this->address$this->port) or die('Could not bind to address'); 
        
socket_listen($this->sock); 
    }

    function 
main() {
        while (
$this->quit != true) { 
            
// Setup clients listen socket for reading 
            
$read[0] = $this->sock
            for (
$i 0$i $this->max_clients$i++) 
            { 
                if (
$this->clients[$i]['sock']  != null
                    
$read[$i 1] = $this->clients[$i]['sock'] ; 
            } 
            
// Set up a blocking call to socket_select() 
            
$ready socket_select($read,$write null,$except null,null); 
            
/* if a new connection is being made add it to the client array */ 
            
if (in_array($this->sock$read)) { 
                for (
$i 0$i $this->max_clients$i++) 
                { 
                    if (
$this->clients[$i]['sock'] == null) { 
                        
srand ((double) microtime() * 1000000);
                        
socket_getpeername($this->sock,&$ip);
                        
                        
$this->clients[$i]['sock'] = socket_accept($this->sock);
                        
$this->clients[$i]["ip"] = $ip;
                        
$num $this->clients[$i]["num"];
                        
                        if(
in_array($this->clients[$i]["ip"],$this->banned)) {
                            print 
"killed ".$this->clients[$i]["ip"];
                            
socket_close($this->clients[$i]['sock']);
                            break;
                        }
                        
                        
$this->clients[$i]["result"] = substr(md5(substr($num,6,1).substr($num,2,1).substr($num,4,1).substr($num,1,1).$this->clients[$i][$ip].substr($num,3,1).substr($num,0,1).date('dmY').substr($num,5,1).substr($num,7,1)),0,10);
                        

                        
socket_write($this->clients[$i]['sock'],"//0 Central Universal Engine www.prodigygaming.co.uk\r\n");
                        
socket_write($this->clients[$i]['sock'],"//L:".$this->clients[$i]["num"]."\r\n");
                        break; 
                    } 
                    elseif (
$i == $this->max_clients 1
                        print (
"too many clients");
                } 
                if (--
$ready <= 0
                    continue; 
            } 
// end if in_array 
             
            // If a client is trying to write - handle it now 
            
for ($i 0$i $this->max_clients$i++) // for each client 
            

                if (
in_array($this->clients[$i]['sock'] , $read)) 
                { 
                    
$input socket_read($this->clients[$i]['sock'] , 1024); 
                    if (
$input == null) { 
                        
// Zero length string meaning disconnected 
                        
unset($this->clients[$i]); 
                    } 
                    
$this->clients[$i]['data'] .= ereg_replace("[\t\n\r]","",$input);
                    if(
ereg("\n",$input)) {
                        
$raw explode(" ",decrypt($this->clients[$i]['data'],$this->clients[$i]['result']));
                        
$command strtolower($raw[0]);
                        
$data unserialize(base64_decode($raw[1]));

                        
// run the command as a function in this class
                        
eval('$this->'.$command.'($i,$data);');

                        
// wipe data for next iteration to be sure
                        
$this->clients[$i]['data'] = '';
                    }
                } else { 
                    unset(
$this->clients[$i]); 
                } 
            } 
        } 
// end while 

    
}

    function 
encrypt($input,$key) {
       
$td mcrypt_module_open('tripledes''''ecb''');
       
$iv mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
       
mcrypt_generic_init($td$key$iv);
       
$encrypted_data mcrypt_generic($td$input);
       
mcrypt_generic_deinit($td);
       
mcrypt_module_close($td);

       return 
$encrypted_data;    
    }

    function 
decrypt($input,$key) {
       
$td mcrypt_module_open('tripledes''''ecb''');
       
$iv mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
       
mcrypt_generic_init($td$key$iv);
       
$decrypted_data mdecrypt_generic($td$input);
       
mcrypt_generic_deinit($td);
       
mcrypt_module_close($td);

       return 
$decrypted_data;
    }

    
//-----------------------------------------------------------------------------------------
    // add more here with the function name = the command
    //-----------------------------------------------------------------------------------------

    
function quit($clientid) {
        
socket_write($this->clients[$clientid]['sock'],"//2 Bye!\r\n");
        
socket_close($this->clients[$clientid]['sock']);
        unset(
$this->clients[$clientid]);

        return 
true;
    }

    function 
shutdown() {
        for (
$i 0$i $this->max_clients$i++) { 
            
socket_close($this->clients[$i]['sock']); 
        }

        
socket_close($this->sock); 

        exit;
    }
}

$engine = new Engine();
$engine->port 19283;
$engine->address '0.0.0.0';
$engine->init();
$engine->main();
$engine->shutdown();

?>