Completing a Query Processor in PHP - Getting the class completed: listing the full source code of the QueryProcessor class (
Page 5 of 5 )
As I said right at the end of the previous section, here is the complete definition for the "QueryProcessor" class:
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;
}
// check for DNS records
public function checkDNSRecords($recType='MX'){
// allowed values for $recType
$validRecTypes=array
('A','MX','NS','SOA','PTR','CNAME','AAAA','A6','SRV',
'NAPTR','ANY');
if(!in_array($recType,$validRecTypes)){
throw new Exception('Invalid DNS record type.');
}
if(!checkdnsrr($this->host,$recType)){
throw new Exception('No '.$recType.' records were
found.');
}
return 'DNS records where found.';
}
// check for DNS records on Windows systems
public function checkDNSRecordsWin($recType='MX'){
// allowed values for $recType
$validRecTypes=array
('A','MX','NS','SOA','PTR','CNAME','AAAA','A6','SRV',
'NAPTR','ANY');
if(!in_array($recType,$validRecTypes)){
throw new Exception('Invalid DNS record type.');
}
exec('nslookup -type='.$recType.' '.$this->host,$result);
foreach($result as $line){
if(preg_match("/^$this->host/",$line)){
return 'DNS records were found.';
}
}
throw new Exception('No '.$recType.' records were
found.');
}
// scan TCP ports
public function scanPort($port=80){
if(!is_int($port)||$port<1||$port>65535){
throw new Exception('Invalid TCP port number.');
}
$output='Scanning port '.$port.'...please wait.<br />';
if(!$fp=@fsockopen($this->host,$port,$errno,$errstr,30)){
throw new Exception('Unable to open connection to
port '.$port);
}
fclose($fp);
return 'Port '.$port.' is open to connections.';
}
// get basic WHOIS info
public function getWhoIs(){
$output='Retrieving WHOIS information...please wait.<br />';
if(!$fp=fsockopen("whois.opensrs.net",43,$errno, $errstr,30)){
throw new Exception('Error opening socket connection
to WHOIS server.');
}
sleep(2);
fputs($fp,"$this->hostrn");
while(!feof($fp)){
$output.=fread($fp,128).'<br />';
}
fclose($fp);
return $output;
}
}
Right, I think that you now have a quite useful query processor class that can be utilized for performing some common networking operations in PHP. Also, in order to get things completed, below I coded an example that illustrates how to use some of the methods that I just added to the class:
// instantiate 'QueryProcessor' object
$queryProc=new QueryProcessor('google.com');
// display DNS records
echo $queryProc->checkDNSRecords();
/* displays the following output
DNS records were found.
*/
// scan TCP ports
echo $queryProc->scanPort();
/* displays the following output
Port 80 is open to connections.
*/
Final thoughts
In this series, I introduced you to the exciting terrain of network programming in PHP by building a query processor class. I hope that each method I wrote here will serve as a clear example of how to use popular PHP networking functions, at least at a basic level.
Now that you know how to check TCP ports, check for DNS records, obtain IP addresses and so forth, go ahead and develop sophisticated PHP network applications. See you in the next PHP tutorial!