Advertisement
mochitto

Untitled

Aug 21st, 2011
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.88 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.   * Telnet class<BR>
  5.   * PHP class for l2jserver what connect to telnet server and execute some command.
  6.   *
  7.   * @author mochitto
  8.   * @version 1.0.0.0
  9.   */
  10.  
  11. final class telnet
  12. {
  13.     private $ip;
  14.     private $port;
  15.     private $timeout;
  16.     private $password;
  17.     private $socket;
  18.     public $authed;
  19.  
  20.     function __construct($ip, $port, $password, $timeout = 1)
  21.     {
  22.         $this->ip = $ip;
  23.         $this->port = $port;
  24.         $this->timeout = $timeout;
  25.         $this->password = $password;
  26.         $this->authed = false;
  27.     }  
  28.    
  29.     function __destruct()
  30.     {
  31.         $this->disconnect();
  32.     }
  33.  
  34.     /**
  35.      * This function initializing connection to telnet server and log-in to that server.
  36.      */
  37.     public function init()
  38.     {
  39.         // Open socket to server
  40.         $this->socket = @fsockopen($this->ip, $this->port, $errno, $errstr, $this->timeout);
  41.        
  42.         // When connection was failed say error and die...
  43.         if ($this->socket === false)
  44.         {
  45.             echo '[TELNET]: Can\'t connect to server '.$this->ip.':'.$this->port;
  46.             $this->socket = false;
  47.             return;
  48.         }
  49.  
  50.         // We can read data what sending telnet server but why? We dont need it at this time...
  51.         $data = $this->read();
  52.  
  53.         // Yea, so send password and check it..
  54.         if(!$this->auth())
  55.         {
  56.             echo '[TELNET]: The password was incorrect, please check your password...';
  57.             return;
  58.         }
  59.     }
  60.    
  61.     /**
  62.      * Function for disconnect from the server...
  63.      */
  64.     public function disconnect()
  65.     {
  66.         if ($this->socket)
  67.         {
  68.             // If we are authed, send the quit command for serious disc.
  69.             if($this->authed)
  70.                 fwrite($this->socket, "quit\r");
  71.                
  72.             // close socket
  73.             fclose($this->socket);
  74.             $this->socket = false;
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * Here send password to telnet server and check if we are authed or not..
  80.      *
  81.      * @return boolean
  82.      */
  83.     private function auth()
  84.     {  
  85.         $data = $this->write($this->password, true);
  86.        
  87.         if(strpos($data, "Password Correct!") !== false)
  88.             $this->authed = true;
  89.         else
  90.             $this->disconnect();
  91.            
  92.         return $this->authed;
  93.     }
  94.  
  95.     /**
  96.      * Basic read data from telnet server with max lenght 512 bytes...
  97.      *
  98.      * @param int $size size of data to read
  99.      * @return data from steam
  100.      */
  101.     private function read($size = 512)
  102.     {
  103.         if ($this->socket)
  104.         {    
  105.             return @fread($this->socket, $size);
  106.         }
  107.     }
  108.    
  109.     /**
  110.      * Write a command, automatic add \r at end of command what means enter after command and we check if socket exist and if we are authed.<BR>
  111.      * We can send force command like password w/o auth.
  112.      *
  113.      * @param String $command command for telnet server
  114.      * @param boolen $force true - send command w/o auth.
  115.      * @return $data from server as reply, there using $this->read() func
  116.      */
  117.     public function write($command, $force = false)
  118.     {
  119.         if (!$this->socket)
  120.             return;
  121.        
  122.         if (!$this->authed && !$force)
  123.         {
  124.             $this->disconnect();
  125.             return;
  126.         }
  127.  
  128.         @fwrite($this->socket, $command . "\r");
  129.         return $this->read();
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement