View Single Post
Old 03-21-2009, 07:06 PM   #9 (permalink)
allworknoplay
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Ok Salathe:

Here is my attempt at commenting your code. Please let me know where I am wrong. Also, the method accept(), if you can explain that further because I don't see that called anywhere??

Thanks!!

Code:
<?php

	/* Create custom class called ApacheFilter that extends from existing
	class FilterIterator from PHP's built-in SPL */
	
	class ApacheFilter extends FilterIterator
{
    
    /* This is a protected property only available to the ApacheFilter class methods */
    protected $filter;
    
    /* This is the class's main construct. It accepts 2 attributes. Type casting the "$it" with Iterator?  */
    public function __construct(Iterator $it, $filter)
    {
        /* You are passing the "$it" attribute to the static parent??? */
        parent::__construct($it);
        
        /* You are passing this attribute to the class pointer "filter". Type casted as array? */
        $this->filter = (array) $filter;
    }
			
			/* Create a method accessible outside of the class */
			public function accept()
    {
        /* NEED HELP UNDERSTANDING THIS */
        $line = $this->getInnerIterator()->current();
        
        /* substr function. Take the first part of the line that is before the space ''  
        Start at the beginning of the line and up to the space ''
        */
        $ip   = substr($line, 0, strpos($line, ' '));
        
        /* If TRUE, return the line that has the IP address found in the array */
        return (bool) in_array($ip, $this->filter);
    }
}

/* This is the object we create to access the file we want to read */
$file   = new SplFileObject('error_log');

/* This is the object we create to access the ApacheFilter class and its methods.
We pass the newly created file object and array data */

$filter = new ApacheFilter($file, array('206.107.147.230', '75.149.134.235'));


/* This is just a simple array loop to echo out our data */
foreach ($filter as $line)
{
    echo $line;
}
allworknoplay is offline  
Reply With Quote