PHP
  Home arrow PHP arrow Page 5 - Completing a Query Processor in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Completing a Query Processor in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2006-05-31


    Table of Contents:
  • Completing a Query Processor in PHP
  • A brief look at the QueryProcessor class
  • Expanding the functionality of the QueryProcessor class: defining the getMXRecords() and checkDNSRecords() methods
  • Scanning TCP ports, running a WHOIS service and more: defining the scanPort() and getWhois() methods
  • Getting the class completed: listing the full source code of the QueryProcessor class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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! 



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT