home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Vyzkuste / phptriad / phptriad2-2-1.exe / php / pear / Net / Dig.php < prev    next >
Encoding:
PHP Script  |  2001-03-13  |  9.7 KB  |  449 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2001 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Colin Viebrock <colin@easyDNS.com>                          |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Dig.php,v 1.2 2001/03/13 17:40:24 cmv Exp $
  20. //
  21. // A nice friendly OO interface to dig
  22. //
  23. require_once('PEAR.php');
  24.  
  25. class Net_Dig extends PEAR
  26. {
  27.     // {{{ Public Properties
  28.     
  29.     /**
  30.      * The address to dig
  31.      *
  32.      * @var string $address
  33.      * @access public
  34.      */
  35.     var $address;
  36.     
  37.     /**
  38.      * The server to use for digging
  39.      *
  40.      * @var string $server
  41.      * @access public
  42.      */
  43.     var $server;
  44.     
  45.     /**
  46.      * The type of DNS records to dig for
  47.      *
  48.      * @var string $query_type
  49.      * @access public
  50.      */
  51.     var $query_type;
  52.  
  53.     /**
  54.      * The last system command executed (for debugging)
  55.      *
  56.      * @var string $cmd
  57.      * @access public
  58.      */
  59.     var $cmd;
  60.  
  61.     /**
  62.      * The raw output of the system command (for debugging)
  63.      *
  64.      * @var string $raw_data
  65.      * @access public
  66.      */
  67.     var $raw_data;
  68.  
  69.     /**
  70.      * The location of the system dig program
  71.      *
  72.      * @var string $dig_prg
  73.      * @access public
  74.      */
  75.     var $dig_prog;
  76.  
  77.     /**
  78.      * The parsed result of the last dig
  79.      *
  80.      * @var string $result
  81.      * @access public
  82.      */
  83.     var $result;
  84.     
  85.     // }}}
  86.  
  87.  
  88.     // {{{ Net_Dig()
  89.     
  90.     /**
  91.      * The Net_Dig constructor
  92.          * Called when a new Net_Dig object is initialized
  93.      *
  94.      * @param string     [$address]  The address to dig (can be set 
  95.      *                               using the $address property as well)
  96.      *
  97.      * @param string     [$server]   The server to dig at (can be set 
  98.      *                               using the $server property as well)
  99.      *
  100.      * @return object Net_Dig   $obj   A new Net_Dig object
  101.      *
  102.      * @access public
  103.      * @author Colin Viebrock <colin@easyDNS.com>
  104.      * @since  PHP 4.0.5
  105.      */
  106.     function Net_Dig($address = false, $server = false )
  107.     {
  108.  
  109.         $this->address = $address;
  110.         $this->server = $server;
  111.         $this->query_type = false;
  112.  
  113.         $this->cmd = '';
  114.         $this->raw_data = '';
  115.  
  116.         $this->result = false;
  117.  
  118.         $this->dig_prog = trim(`which dig`);
  119.         if (!$this->dig_prog) {
  120.             $this = new PEAR_Error("Couldn't find system dig program");
  121.         }
  122.  
  123.     }
  124.  
  125.     // }}}
  126.  
  127.  
  128.  
  129.     // {{{ dig()
  130.     
  131.     /**
  132.      * Does a dig of the given address (or $this->address)
  133.      *
  134.      * @param string           [$address] The address to dig (can be set 
  135.      *                                using the $address property as well)
  136.      *
  137.      * @return object Net_Dig_result    $obj   A new Net_Dig_result object
  138.      *
  139.      * @access public
  140.      * @author Colin Viebrock <colin@easyDNS.com>
  141.      * @since  PHP 4.0.5
  142.      */
  143.     function dig($address=false)
  144.     {
  145.  
  146.         if ($address) {
  147.             $this->address = $address;
  148.         }
  149.  
  150.         if (!$this->address) {
  151.             return new PEAR_Error("No address specified");
  152.         }
  153.  
  154.         if (!$this->_validate_type()) {
  155.             return new PEAR_Error($this->query_type." is an invalid query type");
  156.         }
  157.  
  158.         $cmd = escapeshellcmd(
  159.             sprintf("%s %s %s %s",
  160.                 $this->dig_prog,
  161.                 ($this->server        ? '@'.$this->server : ''),
  162.                 $this->address,
  163.                 ($this->query_type    ? $this->query_type : '' )
  164.             )
  165.         );
  166.  
  167.         $this->cmd = $cmd;
  168.  
  169.  
  170.         $this->raw_data = `$cmd`;
  171.         $this->raw_data = trim(    $this->raw_data );
  172.  
  173.         return $this->_parse_data();
  174.  
  175.     }
  176.     
  177.     // }}}
  178.  
  179.  
  180.     // {{{ _validate_type()
  181.     
  182.     /**
  183.      * Validates the value of $this->query_type
  184.      *
  185.      * @return boolean    $return   True if $this->query_type is a 
  186.      *                                valid dig query, otherwise false
  187.      *
  188.      * @access private
  189.      * @author Colin Viebrock <colin@easyDNS.com>
  190.      * @since  PHP 4.0.5
  191.      */
  192.     function _validate_type()
  193.     {
  194.         $return = true;
  195.         if ($this->query_type) {
  196.             $this->query_type = strtolower($this->query_type);
  197.             switch ($this->query_type) {
  198.                 case 'a':
  199.                 case 'any':
  200.                 case 'mx':
  201.                 case 'ns':
  202.                 case 'soa':
  203.                 case 'hinfo':
  204.                 case 'axfr':
  205.                 case 'txt':
  206.                 break;
  207.                 default:
  208.                 $return = false;
  209.             }
  210.         }
  211.         return $return;
  212.     }
  213.     
  214.     // }}}
  215.  
  216.  
  217.  
  218.     // {{{ _parse_data()
  219.     
  220.     /**
  221.      * Parses the raw data in $this->raw_data
  222.      *
  223.      * @return obj Net_Dig_result  $return   A Net_Dig_result object
  224.      *
  225.      * @access private
  226.      * @author Colin Viebrock <colin@easyDNS.com>
  227.      * @since  PHP 4.0.5
  228.      */
  229.     function _parse_data()
  230.     {
  231.  
  232.         if (!$this->raw_data) {
  233.             return new PEAR_Error("No raw data to parse");
  234.         }
  235.  
  236.         $regex = '/' .
  237.             '^;(.*?)' .
  238.             ';; QUESTION SECTION\:(.*?)' .
  239.             '(;; ANSWER SECTION\:(.*?))?' .
  240.             '(;; AUTHORITY SECTION\:(.*?))?' .
  241.             '(;; ADDITIONAL SECTION\:(.*?))?' .
  242.             '(;;.*)' .
  243.             '/ims';
  244.  
  245.         if (preg_match($regex, $this->raw_data, $matches)) {
  246.  
  247.             $result = new Net_Dig_result;
  248.  
  249.             /* Start parsing the data */
  250.  
  251.  
  252.             /* the header ... */
  253.  
  254.  
  255.             $temp = explode("\n", trim($matches[1]));
  256.             if (preg_match('/DiG (.*?) /i', $temp[0], $m)) {
  257.                 $result->dig_version         = trim($m[1]);
  258.             }
  259.             if (preg_match('/status: (.*?), id: (.*?)$/i', $temp[3], $m)) {
  260.                 $result->status            = trim($m[1]);
  261.                 $result->id            = trim($m[2]);
  262.             }
  263.  
  264.             if (preg_match('/flags: (.*?); query: (.*?), answer: (.*?), authority: (.*?), additional: (.*?)$/i', $temp[4], $m)) {
  265.                 $result->flags            = trim($m[1]);
  266.                 $result->query_count        = (int)trim($m[2]);
  267.                 $result->answer_count        = (int)trim($m[3]);
  268.                 $result->authority_count    = (int)trim($m[4]);
  269.                 $result->additional_count    = (int)trim($m[5]);
  270.             }
  271.  
  272.  
  273.             /* query section */
  274.  
  275.             $line = trim(preg_replace('/^(;*)/', '', trim($matches[2])));
  276.             list($host, $class, $type) = preg_split('/[\s]+/', $line, 3);
  277.             $result->query[] = new Net_Dig_resource($host, false, $class, $type, false);
  278.  
  279.  
  280.             /* answer section */
  281.  
  282.             $temp = trim($matches[4]);
  283.             if ($temp) {
  284.                 $temp = explode("\n", $temp);
  285.                 if (count($temp)) {
  286.                     foreach($temp as $line) {
  287.                         $result->answer[] = $this->_parse_resource($line);
  288.                     }
  289.                 }
  290.             }
  291.  
  292.  
  293.             /* authority section */
  294.  
  295.             $temp = trim($matches[6]);
  296.             if ($temp) {
  297.                 $temp = explode("\n", $temp);
  298.                 if (count($temp)) {
  299.                     foreach($temp as $line) {
  300.                         $result->authority[] = $this->_parse_resource($line);
  301.                     }
  302.                 }
  303.             }
  304.  
  305.  
  306.             /* additional section */
  307.  
  308.             $temp = trim($matches[8]);
  309.             if ($temp) {
  310.                 $temp = explode("\n", $temp);
  311.                 if (count($temp)) {
  312.                     foreach($temp as $line) {
  313.                         $result->additional[] = $this->_parse_resource($line);
  314.                     }
  315.                 }
  316.             }
  317.  
  318.             /* footer */
  319.  
  320.             $temp = explode("\n", trim($matches[9]));
  321.             if (preg_match('/query time: (.*?)$/i', $temp[0], $m)) {
  322.                 $result->query_time    = trim($m[1]);
  323.             }
  324.             if (preg_match('/server: (.*?)#(.*?)\(/i', $temp[1], $m)) {
  325.                 $result->dig_server    = trim($m[1]);
  326.                 $result->dig_port    = trim($m[2]);
  327.             }
  328.  
  329.             /* done */
  330.  
  331.             $result->consistency_check = (
  332.                 (count($result->query) == $result->query_count) &&
  333.                 (count($result->answer) == $result->answer_count) &&
  334.                 (count($result->authority) == $result->authority_count) &&
  335.                 (count($result->additional) == $result->additional_count)
  336.             );
  337.  
  338.             return $result;
  339.  
  340.         }
  341.  
  342.         return new PEAR_Error("Can't parse raw data");
  343.     }
  344.     
  345.     // }}}
  346.  
  347.  
  348.     // {{{ _parse_resource()
  349.     
  350.     /**
  351.      * Parses a resource record line
  352.      *
  353.      * @param string           $line    The line to parse
  354.      *
  355.      * @return obj Net_Dig_resource  $return   A Net_Dig_resource object
  356.      *
  357.      * @access private
  358.      * @author Colin Viebrock <colin@easyDNS.com>
  359.      * @since  PHP 4.0.5
  360.      */
  361.     function _parse_resource($line)
  362.     {
  363.  
  364.         /* trim and remove leading ;, if present */        
  365.  
  366.         $line = trim(preg_replace('/^(;*)/', '', trim($line)));
  367.  
  368.         if ($line) {
  369.             list($host, $ttl, $class, $type, $data) = preg_split('/[\s]+/', $line, 5);
  370.             return new Net_Dig_resource($host, $ttl, $class, $type, $data);
  371.         }
  372.  
  373.         return false;
  374.  
  375.     }
  376.  
  377.     // }}}
  378.  
  379. }
  380.  
  381.  
  382.  
  383. class Net_Dig_result {
  384.  
  385.     // {{{ Public Properties
  386.  
  387.     var $status;
  388.     var $id;
  389.     var $flags;
  390.     var $query_count;
  391.     var $answer_count;
  392.     var $authority_count;
  393.     var $additional_count;
  394.  
  395.     var $dig_version;
  396.     var $dig_server;
  397.     var $dig_port;
  398.  
  399.     var $query;
  400.     var $answer;
  401.     var $authority;
  402.     var $additional;
  403.  
  404.     var $consistency_check;
  405.  
  406.     function Net_Dig_result() {
  407.         $this->status = false;
  408.         $this->id = false;
  409.         $this->flags = false;
  410.         $this->query_count = false;
  411.         $this->answer_count = false;
  412.         $this->authority_count = false;
  413.         $this->additional_count = false;
  414.  
  415.         $this->dig_version = false;
  416.         $this->dig_server = false;
  417.         $this->dig_port = false;
  418.  
  419.         $this->query = array();
  420.         $this->answer = array();
  421.         $this->authority = array();
  422.         $this->additional = array();
  423.  
  424.         $this->consistency_check = false;
  425.  
  426.     }
  427.  
  428. }
  429.  
  430. class Net_Dig_resource {
  431.  
  432.     var $host;
  433.     var $ttl;
  434.     var $class;
  435.     var $type;
  436.     var $data;
  437.  
  438.     function Net_Dig_resource($host=false, $ttl=false, $class=false, $type=false, $data=false) {
  439.         $this->host    = $host;
  440.         $this->ttl    = $ttl;
  441.         $this->class    = $class;
  442.         $this->type    = $type;
  443.         $this->data    = $data;
  444.     }
  445.  
  446. }
  447.  
  448. ?>
  449.