Looking for a comprehensive introduction to creating networking applications with AJAX? Then look no further, because you’ve come to the right place! Welcome to the concluding installment of the series “Creating a Network Processor with AJAX.” As you may have guessed, this series leads you through the development of a highly expansible networking application which uses the capacity of AJAX for sending queries in the background.
Completing a Network Processor with AJAX - Performing real networking tasks on the server: defining the QueryProcessor PHP class (Page 3 of 5 )
As I said in the previous section, the link required for executing real networking commands on the web server will consist simply of a PHP class, which will be capable of performing the variety of tasks that were discussed in the introduction of this article.
Having explained how all the networking operations will be executed on the server, let me show you now the partial definition of this new PHP class , which I called "QueryProcessor." Its signature is as follows:
class QueryProcessor{ private $host; private $services=array('http','https','ftp','telnet','imap', 'smtp','nicname','gopher','finger','pop3','www'); private $ports=array(21,23,25,43,70,79,80,110,143,443); private $validRecTypes=array'A','MX','NS','SOA','PTR','CNAME', 'AAAA','A6','SRV','NAPTR','ANY'); public function __construct($host='myhost.com'){ $this->host=$host; } // get IP address public function getIp(){ if(!$ip=gethostbyname($this->host)){ return 'Error resolving host IP address.'; } return $ip; } // get list of IP addresses public function getIpList(){ if(!$ips=implode(' - ',gethostbynamel($this->host))){ return 'Error getting list of IP addresses for the provided hostname.'; } return $ips; } // get host name public function getHost(){ if(!$host=gethostbyaddr($this->getIp())){ return 'Error resolving host name.'; } return $host; } // get TCP ports of Internet services public function getServicePorts(){ $output='Retrieving services ports...Please wait.<br />'; foreach($this->services as $service){ if(!$port=getservbyname($service,'tcp')){ $output.='Error retrieving port of service '.$service.'<br />'; } else{ $output.='Service '.$service. ' runs on TCP port: '. $port.'<br />'; } } return $output; } // get Services by TCP ports public function getServiceNames(){ $output='Retrieving services names...Please wait.<br />'; foreach($this->ports as $port){ if(!$service=getservbyport($port,'tcp')){ $output.='Error retrieving service name on port '.$port.'<br />'; } else{ $output.='TCP Port '.$port. ' is used by service: '. $service.'<br />'; } } return $output; } // execute 'ipconfig' command on Windows systems public function IpConfig(){ $output='Running ipconfig command...Please wait.<br />'; exec('ipconfig',$lines); foreach($lines as $line){ $output.=$line.'<br />'; } return $output; } // execute 'ping' command on Windows systems public function Ping(){ $output='Running ping command...Please wait.<br />'; exec('ping '.$this->host,$lines); foreach($lines as $line){ $output.=$line.'<br />'; } return $output; } // execute 'netstat' command on Windows systems public function Netstat(){ $output='Running netstat command...Please wait.<br />'; exec('netstat',$lines); foreach($lines as $line){ $output.=$line.'<br />'; } return $output; } }
Well, as I explained before, this is the partial definition for the above "QueryProcessor" class. As you can see, the class in question is capable of doing some useful things, like finding the IP address of a given Internet host and listing the name of different services according to their port numbers, running the "ping," "ipconfig" and "netstat" commands on Windows-based systems, retrieving the distinct TCP ports for several services, and so on. Pretty handy, right?
However, as I explained previously, this is only the partial signature for the above class, since it should also be capable of performing some additional tasks, such as searching for DNS records, and scanning specific TCP ports.
Therefore, taking into account this condition, in the next few lines, I'll add some extra methods to the "QueryProcessor," in this way completing its definition.
Wan to see how this will done? Please, keep reading.