<?php
 
/*+----------------------------------------------------------+
 
  | Generic Net Query Class                                  |
 
  | By Zachary Shaver                                        |
 
  | Copyright (c) 2003 Zachary Shaver                        |
 
  | Email: [email protected]                         |
 
  +----------------------------------------------------------+
 
  | Email bugs/suggestions to [email protected]            |
 
  +----------------------------------------------------------+
 
  | This script has been created and released under          |
 
  | the GNU GPL and is free to use and redistribute          |
 
  | only if this copyright statement is not removed          |
 
  +----------------------------------------------------------+*/
 
 
/*+----------------------------------------------------------+
 
  |Example of use:                                           |
 
  +----------------------------------------------------------+
 
  |This is an example using the reusable query()             |
 
  |In this example we are getting information from a game    |
 
  |server, in this example "Star Trek Voyager: Elite Force"  |
 
  |It is favourable to use this method if you are reusing    |
 
  |the same ports/addresses/commands.                        |
 
  +----------------------------------------------------------+
 
  |//initialize the class                                    |
 
  |$nq = new netquery;                                       |
 
  |//set transport method to udp                             |
 
  |$nq->method = 'udp';                                      |
 
  |//set address of host, IP/hostname                        |
 
  |$nq->addr = '127.0.0.1';                                  |
 
  |//set port to connect to                                  |
 
  |$nq->port = '27960';                                      |
 
  |//set the garbage filter, to filter out stuff we dont want|
 
  |$nq->garbage = '˙˙˙˙statusResponse\n\\\\|\^[0-9]';        |
 
  |//set the data we are sending to the host                 |
 
  |$nq->input = '˙˙˙˙getstatus';                             |
 
  |//execute the query and store results to variable.        |
 
  |$result = $nq->query();                                   |
 
  +----------------------------------------------------------+*/
 
 
/*+----------------------------------------------------------+
 
  |Notes:                                                    |
 
  | Oh... all errors/problems are stored                     |
 
  |in the array $errors[] within the class.                  |
 
  |might want to do a quick print_r($nq->errors) or something|
 
  +----------------------------------------------------------+*/
 
class netquery
 
{
 
   var $connection;
 
   var $method = '';
 
   var $addr = '';
 
   var $port = '';
 
   var $garbage = '';
 
   var $input = '';
 
   var $output = '';
 
   var $errors = array();
 
 
   function netquery()
 
   {
 
      $this->method = 'tcp';
 
      $this->garbage = '';
 
   }
 
   
 
   // Try connecting to the network server.
 
   function connect()
 
   {
 
      if($this->verify())
 
      {
 
         $host = $this->method . '://' . $this->addr;
 
         $this->connection = @fsockopen($host,$this->port);
 
         stream_set_timeout($this->connection,1);
 
         if($this->connection)
 
         {
 
            return true;
 
         }
 
         else
 
         {
 
            $this->errors[] = 'UNABLE_TO_CONNECT';
 
            return false;
 
         }
 
      }
 
      else
 
      {
 
         return false;
 
      }
 
   }
 
 
   // Attempt the query on the network server.
 
   function query()
 
   {
 
      if($this->connect())
 
      {
 
         fwrite ($this->connection,$this->input);
 
         $this->output = fread($this->connection,1);
 
         if(!empty($this->output))
 
         {
 
            do
 
            {
 
               $status_pre = socket_get_status($this->connection);
 
               $this->output .= fread($this->connection,1);
 
               $status_post = socket_get_status($this->connection);
 
            } while ($status_pre['unread_bytes'] != $status_post['unread_bytes']);
 
         }
 
         $this->disconnect();
 
         $this->output = trim(str_replace("\0",chr(253),$this->output),"\n\r");
 
         if (!empty($this->garbage))
 
         {
 
            $this->output = ereg_replace ($this->garbage, "", $this->output);
 
         }
 
         $this->output = str_replace (chr(253), "\0", $this->output);
 
         return $this->output;
 
      }
 
      else
 
      {
 
         return '';
 
      }
 
   }
 
 
   // Disconnect from the network server.
 
   function disconnect()
 
   {
 
      fclose($this->connection);
 
      return true;
 
   }
 
 
   // Reset all variables so we can re-use the class without worrying.
 
   function reset()
 
   {
 
      $this->method = '';
 
      $this->addr = '';
 
      $this->port = '';
 
      $this->input = '';
 
      $this->output = '';
 
      $this->garbage = '';
 
      return true;
 
   }
 
 
   // Verify that all the information required for
 
   // successful query is here.
 
   function verify()
 
   {
 
      if($this->method == '')      $this->errors[] = 'NO_METHOD';
 
      if($this->addr == '')        $this->errors[] = 'NO_ADDR';
 
      if($this->port == '')        $this->errors[] = 'NO_PORT';
 
      if($this->input == '')       $this->errors[] = 'NO_INPUT';
 
      if(strlen($this->input)>256) $this->errors[] = 'COMMAND_TOO_LONG';
 
      if($this->method!='' && $this->addr!='' && $this->port!='' && $this->input!='')
 
      {
 
         return true;
 
      }
 
      else
 
      {
 
         $this->errors[] = 'UNABLE_TO_VERIFY';
 
         return false;
 
      }
 
   }
 
}
 
?>
 
 |