Thread: PHP help plz.
View Single Post
  #1  
Old 10-13-2014, 11:28 AM
scrubtasticx's Avatar
scrubtasticx
Sarnak
 
Join Date: Apr 2014
Location: Florida
Posts: 44
Default PHP help plz.

So i have found a few threads and put together this script through a bunch of posts i found searching the forums here. it is OUTPUTTING Server UP or Down correctly but NOTHING else, and i cannot seem to catch the reason why. here is the code.


Code:
<?php

class telnet {

    // Variables
    var $message = array();
    var $connection;

    // Connects to the World Telnet Server
    function connect(){
        
        // Open Socket Connection
        $this->connection = fsockopen('184.33.56.151', 9000);

        if(!$this->connection){
            $this->message[] = "Error connecting to World Telnet Server";
            return false;
        }else{
            fputs($this->connection, sprintf("%s\r", 'usernamehidden'));
            usleep(125000);   
              fputs($this->connection, sprintf("%s\r", 'passwordhidden'));
            usleep(125000);
            return true;
        }
    }
    
    
    // Close the Telnet Connection
    function close(){
        fclose($this->connection);
    }
    
    
    // Server Uptime
    function uptime(){
        fputs ($this->connection, "uptime\r"); 
        usleep(125000);
            
        while (!feof($this->connection)){
            $ret = fgets($this->connection);
            if (ereg("Uptime", $ret)) {
                $ret = str_replace("$user", "", $ret);
                return str_replace(">", "", $ret);
                break;
            }
        }
    }
    
    
    // Zone Status
    function zonestatus($zone=''){
        fputs ($this->connection, sprintf("zonestatus %s\r", $zone)); 
        usleep(125000);
        
        while(!feof($this->connection)){
            $ret = fgets($this->connection);
            
            if (ereg("$Server:", $ret)){
                echo "$ret \n <br>";
            }
            
            if(ereg("avail", $ret)){
                echo "$ret \n <br>";
                break;
            }                
        }
    }
    
    // Kick Character from World Server
    function kick($character, $message='YOU ARE BEING KICKED FROM THE SERVER IN 4 SECONDS'){
        fputs($this->connection, sprintf("tell %s %s\r", $character, $message));
        sleep(4); 
        fputs($this->connection, sprintf("kick %s\r", $character));
        $this->message[] = "Character has been kicked from server";
        sleep(1);
    }
    
    
    // Send Character a Tell
    function tell($character, $message){
        fputs($this->connection, sprintf("tell %s %s\r", $character, $message));
        sleep(1);
        $this->message[] = sprintf("Message Sent to Character: %s", $character);
    }
    
    
    // Send Server a OOC Message
    function ooc($message){
        fputs($this->connection, sprintf("ooc %s\r", $message));
        sleep(1);
        $this->message[] = "Message sent to OOC";
    }
    
    
    // Broadcast Message Server Wide
    function broadcast($message){
        fputs($this->connection, sprintf("broadcast %s\r", $message));
        sleep(1);
        $this->message[] = "Broadcast Message Sent to World Server";
    }
    
    
    // Who/Who All
    function whoall($who=''){
    
        fputs ($this->connection, sprintf("who%s\r", $who)); 
        usleep(125000);

        while (!feof($this->connection)) {
            $ret_tmp = fgets($this->connection);

            if(ereg("zone", $ret_tmp)){ 
                $ret[++$x] = $ret_tmp;
            }
            
            if(ereg("online", $ret_tmp)){
                break;
            }
        }
        
        if( !is_array($ret) ) {
            return;
        }

        // Process each line of the returned results
        foreach ($ret as $key => $value) {
        
            // Original Return
            $players[$key]['original'] = $value;

            // GM Status
            if (preg_match("/\*.*\*/", $value, $matches)) {
                $players[$key]['GM'] = trim($matches[0], "* ");
            }
        
            // Role/Anon, Level & Class
            if (preg_match("/\[.*\]/", $value, $matches)) {
                $tmp = explode(" ", trim($matches[0], "[] "));
                if (!is_numeric($tmp[0])) {
                    $players[$key]['Visible'] = $tmp[0];
                    $players[$key]['Level'] = $tmp[1];
                    $players[$key]['Class'] = rtrim($tmp[2] . " " . $tmp[3], " ");
                } else {
                    $players[$key]['Level'] = $tmp[0];
                    $players[$key]['Class'] = rtrim($tmp[1] . " " . $tmp[2], " ");
                }
            }
        
            // Character's Name
            if(preg_match("/\].*\(/", $value, $matches)){
                $players[$key]['Name'] = trim($matches[0], "] (");
            }
        
            // Race
            if(preg_match("/\(.*\)/", $value, $matches)){
                $players[$key]['Race'] = trim($matches[0], "( )");
            }
        
            // Zone
            if(preg_match("/zone:.*AccID:/", $value, $matches)){
                $players[$key]['ZoneLong'] = substr($matches[0], 6, -7);
            }
        
            // Account ID
            if(preg_match("/AccID:.*AccName:/", $value, $matches)){
                $players[$key]['AcctID'] = substr($matches[0], 7, -9);
            }
        
            // Account Name
            if(preg_match("/AccName:.*LSID:/", $value, $matches)){
                $players[$key]['AcctName'] = substr($matches[0], 9, -6);
            }
        
            // Login Server ID
            if(preg_match("/LSID:.*Status:/", $value, $matches)){
                $players[$key]['LSID'] = substr($matches[0], 6, -8);
            }
        
            // Status
            if(preg_match("/Status:.*/", $value, $matches)){
                $players[$key]['Status'] = substr($matches[0], 8, 3);
            }
        }
        
        // Return Player Array
        return $players;
    }

}

$telnet = new telnet;
if($telnet->connect() ) {
    // We are connected... yay!
    echo "<h1>Server is <span style='background-color:green;'>UP</span></h1>";
    $uptime = $telnet->uptime();
    $uptime = preg_replace( '/^.*:/','' , $uptime );
    echo "<p> Uptime: " . $uptime . "</p>";
    $players = $telnet->whoall();
    if( is_array($players) ) {
        echo "<table><tr><td>Name</td><td>Class</td><td>Level</td><td>Zone</td></tr>";
        foreach( array_keys($players) as $key) {
            echo "<tr><td>" . $players[$key]['Name'] . "</td>";
            echo "<td>" . $players[$key]['Class']. "</td>";
            echo "<td>" . $players[$key]['Level']. "</td>";
            echo "<td>" . $players[$key]['ZoneLong']. "</td><tr>";
        }
        echo "</table>";
    } else {
        echo "<p>Nobody is on</p>";
    }
    
} else {
    // OH NOS!
    echo "<h1>Server is <span style='background-color:red;'>DOWN</span></h1><p>" . $telnet->message[0] ."</p>";
} 
?>
it says Server UP but nobody online and i have like 6 people online and its doing nothing else.. what did i miss? thanks in advance.
Reply With Quote