Location: PHPKode > scripts > sIPb > sipb/sIPb.php
<?php 
/* Do not remove or alter this section***************************


************************Class Description************************
sIPb means  (s)imple (IP) (b)locker

The sIPb-object provides a easy way to handle unwelcome clients at your site. It saved the IP of these clients with a fixed number of seconds to be blocked from your site. 

The sIPb-object automatically updates the stored entries by deleting expired lines. If you want to do so, the SIPb-object redirects blocked clients to a specified URL, or it provides only the information that a client is blocked. 
*****************************************************************


************************ sorry and thx ***************************
Please excuse errors in this text. English isnt my native language,
and so  suggestions about the code and the spelling are welcome

*********************Contact and Bug report***********************
 uwe nodot stein at gmx dot de
******************************************************************


********************Licence****************************************
Copyright © 2005, Uwe Stein
All rights reserved.

Free for non-comercial use, please contact me for comercial use

Redistribution and use in source and binary forms, with or without modification, are permitted provided
that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
the following disclaimer in the documentation and/or other materials provided with the distribution.


THIS SOFTWARE
IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.

***************************************************************

**************End of do not remove or alter section*************************/





include_once("sIPb.conf.php");

class sIPb {

  var $blockFile;
  var $blockUrl;
    
  /** 
  *  sIPb([boolean withCheck = FALSE ]):void
  *
  *  Create a sIPb-objekt, if withCheck == TRUE automatically check 
  *  wether the client-IP is blocked and redirect to "BLOCK_URL"
  *  ( if withCheck == TRUE, remember that "header(location..." only works
  *    if there was no output before ) 
  *
  *  Param : opt. boolean withCheck ( default FALSE )
  *  Return: void 
  */  
  function sIPb($withCheck = FALSE) {
    $this->blockFile = BLOCK_FILE;
    $this->blockUrl  = BLOCK_URL;  
    
    // check the "BLOCK_FILE" and delete all 
    // expired entries
    $this->_cleanFile();
    
    if ($withCheck == TRUE) {
       if ( $this->isBlocked()) {
          header("Location: ".$this->blockUrl);
       }        
    } 
  }  // End func sIPb 
   
  /** 
  *  blockIP([seconds]):boolean
  *  get the current IP and save it in "blocked-IP's file 
  *  
  *  Param : seconds to block the IP default 24 hours
  *  Return: boolean  
  */  
  
        
  function blockIP($seconds = 86400){    
    $ip_no = $ip = getenv("REMOTE_ADDR"); 
    $blockTime = time() + $seconds;
    
    // the string to store
    $str = $ip_no.":".$blockTime."\n";
      
    // open file, lock it, write the string, unlock and close
    $fp = fopen($this->blockFile,"a+",1);
    if (!$fp) {
      echo "Failed opening $this->blockFile";
      return FALSE;
    }
    flock($fp,2);
    fputs($fp,$str);
    flock($fp,3);
    fclose($fp);
    return TRUE;    
  } // End func blockIP
  
  
  
  /**
  *  isBlocked() : boolean
  *  checks wether the clients-IP is blocked or not 
  *  param : void 
  *  return: boolean
  */
  function isBlocked() {
    $retval = FALSE;
    // get the current timestamp
    $currTime = time(); 
    // get the IP
    $currIP   = getenv("REMOTE_ADDR");    
        
    // open and lock the blockFile
    $fp = fopen($this->blockFile, "r");
    flock($fp, 1) or die("Cannot lock the file.");
      
    // read all lines from the blockFile 
    //until a succesful comparison or EOF
    while($line = fgets($fp, 1024)) {
        $arr = explode(":",$line);
        //  if its detected that the ip is blocked, leave the loop
        if ($arr[0] == $currIP &&
            $arr[1] > $currTime ) {
          $retval = TRUE;
          break;
        }        
    }
    fclose($fp);  
    return $retval;
  } // end func isBlocked
  
      
  /** 
  *  _cleanFile() : void
  *  read the blockFile and delete all expired entries 
  *  private method, called by the constructor
  *  
  *  Param : void 
  *  Return: void
  */    
  function _cleanFile() {
   
      // get the current timestamp
      $currTime = time(); 
         
           
      // open and lock the blockFile
      $old = fopen($this->blockFile, "r") or die("Cannot open file");
      flock($old, 1) or die("Cannot lock the file.");

      // Create and lock a tmp-file
      $new = fopen($this->blockFile.".tmp", "w") 
             or die("cannot open new file");
      flock($new, 2) or die("Cannot lock the file.");
          
      // read all lines from the blockFile
      while($line = fgets($old, 1024)) {
        $arr = explode(":",$line);
        // if the line isnt expired, save again
        if ($arr[1] > $currTime )
          fputs($new, $line);

      }
      fclose($old);
      unlink($this->blockFile);
      fclose($new);
      rename($this->blockFile.".tmp", $this->blockFile);
   
  } // end func _cleanFile()

    
} // class sIPb



?>
Return current item: sIPb