<?php

#
# dhpcd.leases php parsing class by peter krausgrill
#        

class Lease                    # lease
{                            #
    
var $ip;                # 172.23.5.10 {

    
var $starts;            # starts 5 2003/11/28 19:29:39;                -> STARTS weekday year/month/day hour:minute:second;
    
var $ends;                # ends 6 2003/11/29 19:29:39;                -> ENDS weekday year/month/day hour:minute:second;
    
var $hardware;            # hardware ethernet 00:0a:95:ea:2c:ea;        -> HARDWARE hardware-type mac-address;
    
var $uid;                # uid 01:00:0a:95:ea:2c:ea;                    -> UID client-identifier;
    
var $hostname;            # client-hostname "nyarlathotep";            -> CLIENT-HOSTNAME "hostname"; OR HOSTNAME "hostname";
    
var $abandoned;            # abandoned;                                -> The DHCP server may determine that a lease has been misused in some way

                            # }

    
function Lease($ip$starts$ends$hardware$uid$hostname=''$abandoned=0)
    {
        
$this->ip            $ip;

        
$this->starts        $starts;
        
$this->ends            $ends;
        
$this->hardware        $hardware;
        
$this->uid            $uid;

        
$this->hostname        $hostname;
        
$this->abandoned    $abandoned;
    }

    function 
Show()
    {
        echo 
            
"IP: ".$this->ip."<br>"
            
."-> MAC: ".$this->uid."<br>";
    }

}                            

class 
DHCPD
{
    var 
$filename;

    var 
$leases        = array();
    var 
$records    = array();
    var 
$lines        = array();

    function 
DHCPD($filename)
    {
        
$fh fopen($filename,"r");

        if (!
$fh) exit;

        while (!
feof ($fh)) 
        {
            
# Get Line and strip Whitespaces
            
$temp trim(fgets($fh));

            
# Ignore Comment and empty Lines
            
if ($temp[0] != '#')
                
$this->lines[] = $temp;
        }

        
fclose ($fh);

        
$this->filename $filename;

        
# Take it appart
        
$this->records split("lease"implode($this->lines));
        
        for (
$i=1$i<sizeof($this->records); $i++)
        {
            
# Strip Whitespaces
            
$this->records[$i] = trim(@$this->records[$i]);

            
$ip substr($this->records[$i], 0strpos($this->records[$i]," "));

            
$elements split(";",
                            
# Strip Whitespaces
                            
trim(
                                
# Remove Brackets IP { }
                                
substr(
                                    
$this->records[$i], 
                                    
strpos($this->records[$i],"{") + 1
                                    
strpos($this->records[$i],"}") - strpos($this->records[$i],"{") - 
                                
)
                            )
                        );
            
            for (
$x=0$x<sizeof($elements); $x++)
            {
                
$title substr($elements[$x], 0strpos($elements[$x]," "));
                
$value trim(substr($elements[$x], strpos($elements[$x]," "), strlen($elements[$x]) - strpos($elements[$x]," ") ));

                switch ( 
$title ) {

                    
# Fixed
                    
case "starts":            $starts        split(" ",$value);    # weekday year/month/day hour:minute:second
                    
case "ends":            $ends        split(" ",$value);    # weekday year/month/day hour:minute:second
                    
case "hardware":        $hardware    split(" ",$value);    # hardware-type mac-address
                    
case "uid":                $uid        $value;

                    
# Optionally
                    
case "hostname":        $hostname    substr($value,1,-1);    # "hostname"
                    
case "client-hostname":    $hostname    substr($value,1,-1);    # "hostname"
                    
case "abandoned":        $abandoned    1;

                }

            }

            
$this->leases[] = new Lease($ip$starts$ends$hardware$uid$hostname$abandoned+0);

        }
    }

    function 
Show()
    {
        echo 
$this->filename."<br>";
        for (
$i=0$i<sizeof($this->leases); $i++)
        {
            
$this->leases[$i]->Show();
        }
    }

    function 
IP2MAC($ip)
    {
        for (
$i=0$i<sizeof($this->leases); $i++)
        {
            if ( (
$ip == $this->leases[$i]->ip) && ($this->leases[$i]->ends[1]." ".$this->leases[$i]->ends[2] > date("Y/m/d h:i:s")) )
                return 
$this->leases[$i]->hardware[1];
        }

        return 
FALSE;
    }

    function 
MAC2IP($mac)
    {
        for (
$i=0$i<sizeof($this->leases); $i++)
        {
            if ( (
$mac == $this->leases[$i]->hardware[1]) && ($this->leases[$i]->ends[1]." ".$this->leases[$i]->ends[2] > date("Y/m/d h:i:s")) )
                return 
$this->leases[$i]->ip;
        }

        return 
FALSE;
    }

    function 
MAC2HOST($mac)
    {
        for (
$i=0$i<sizeof($this->leases); $i++)
        {
            if ( (
$mac == $this->leases[$i]->hardware[1]) && ($this->leases[$i]->ends[1]." ".$this->leases[$i]->ends[2] > date("Y/m/d h:i:s")) && ($this->leases[$i]->hostname) )
                return 
$this->leases[$i]->hostname;
        }

        return 
FALSE;
    }

}

?>