Jump to content

Can not get result from Array


leest003

Recommended Posts

Hi all,

 

I have a ip class which gets an ip address and then out puts the information in an array which can then be split down to show certain information only.  My problem is that when i try to change the result that i want, it just says the word array.  For example if i try to change

 $IP[admin-c'] 

  to say descr, or org  instead of the admin-c the result just says Array and it doesn't give a value, however if i change it back it works fine.

 

below are my two peices of code.

 


include("ipology.class.php");
$ipology = new ipology(array("94.30.9.163"));
$out = $ipology->out();
foreach($out as $ip)
{
    if(is_array($ip))
    {
        echo "Block: " . $ip['admin-c'] . "\n";
        echo "Country: " . $ip['country'] . "\n";
        echo "Netname: " . $ip['netname'] . "\n";
        echo "Person: " . implode(", ", (array) $ip['person']) . "\n";
        echo "------------------------------\n";
    }
    else
    {
        echo "ERR: $ip\n------------------------------\n";
    }
}

 

 


class ipology
{
    var $_ip = "255.255.255.255";
    var $_whois = "whois.ripe.net";
    var $_port = 43;
    var $_timeout = 10;
    var $_buffer = "";
    var $out = array();
    var $array_return_mode;
    var $_total = 0;

    function ipology($ip_array, $array_return_mode = "array")
    {
        $this->array_return_mode = $array_return_mode;
        $ip_array = (array) $ip_array;
        $this->_total = count($ip_array);
        $i = 0;
        foreach($ip_array as $ip)
        {
            if($this->isValidIP($ip))
            {
                $this->_ip = $ip;
                if($this->_fetch())
                {
                    $this->out[$ip] = $this->_extract();
                    if($this->out[$ip]["inetnum"] == "0.0.0.0 - 255.255.255.255")
                        $this->out[$ip] = 102;
                }
                else
                {
                    $this->out[$ip] = 101; //could not connect
                }
            }
            else
                $this->out[$ip] = 100; //incorrect IP format
        }
    }

    function _fetch()
    {
        if(!$sock = @fsockopen($this->_whois, $this->_port, &$errno, &$errstr, 10))
            return false;
        else
        {
            fputs($sock, "{$this->_ip}\n");
            while(!feof($sock))
                $buffer .= fgets($sock, 10240);
            fclose($sock);
            $this->_buffer = $buffer;
            return true;
        }
    }

    function _extract()
    {
        $ret = array();
        $w = array("inetnum","org","netname","descr","country","admin-c","tech-c","status","mnt-by","mnt-lower","mnt-routes","source","organisation","org-name","org-type","address","phone","fax-no","e-mail","mnt-ref","person","nic-hdl","route","origin");
        foreach($w as $get)
        {
            if(preg_match_all("/$get:\s*(.*+)/i", $this->_buffer, $matches))
            {
                if(count($matches[1]) > 1)
                {
                    if($this->array_return_mode == "array")
                        $ret[$get] = $matches[1];
                    else
                        foreach($matches[1] as $ras)
                            $ret[$get] .= $ras . "\n";
                }
                else
                    $ret[$get] = $matches[1][0];
            }
        }
        return $ret;
    }

    function isValidIp($ip)
    {
        return preg_match("/(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/", $ip) ? true : false;
    }

    function out($w=0)
    {
        return $this->out;
    }
}

 

Any advice would be much appreciated.

 

Thanks

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.