HomePHP Page 2 - Completing a Query Processor in PHP
A brief look at the QueryProcessor class - PHP
Welcome to the third part of the series “Network Programming in PHP.” In three tutorials, this series explains the basics of network programming in PHP, by developing a query processor class in PHP 5, which uses some of the most popular PHP network functions, in order to implement the logic of its methods.
Naturally, before I continue adding more methods to the query processor class, it's a good idea to recall how this class was defined. For this reason, below I listed the full source code of the class, as it was written during the previous tutorial:
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); public function __construct($host='myhost.com'){ $this->host=$host; } // get IP address public function getIp(){ if(!$ip=gethostbyname($this->host)){ throw new Exception('Error resolving host IP address.'); } return $ip; } // get list of IP addresses public function getIpList(){ if(!$ips=implode(' - ',gethostbynamel($this->host))){ throw new Exception('Error getting list of IP addresses for the provided hostname.'); } return $ips; } // get host name public function getHost(){ if(!$host=gethostbyaddr($this->getIp())){ throw new Exception('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; } // get MX records on Windows systems public function getMXRecordsWin(){ $output='Retrieving MX Records...please wait.<br />'; exec("nslookup -type=mx $this->host",$mxhosts); foreach($mxhosts as $mxhost){ $output.=$mxhost.'<br />'; } return $output; } }
Okay, that's the complete definition for the "QueryProcessor" class. Now, it's time to move on and continue adding more methods to the class. Go ahead and read the next section.