Home arrow PHP arrow Page 5 - Completing a Query Processor in PHP

Getting the class completed: listing the full source code of 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.

TABLE OF CONTENTS:
  1. Completing a Query Processor in PHP
  2. A brief look at the QueryProcessor class
  3. Expanding the functionality of the QueryProcessor class: defining the getMXRecords() and checkDNSRecords() methods
  4. Scanning TCP ports, running a WHOIS service and more: defining the scanPort() and getWhois() methods
  5. Getting the class completed: listing the full source code of the QueryProcessor class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
May 31, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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! 



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: