PHP Classes

File: include/AccessUpdate.php

Recommend this page to a friend!
  Classes of Rolands Kusins  >  PHP Block Host  >  include/AccessUpdate.php  >  Download  
File: include/AccessUpdate.php
Role: Class source
Content type: text/plain
Description: Class that is used to update Apache access files and hosts.deny file
Class: PHP Block Host
Parse logs and block suspicious hosts
Author: By
Last change: bugfixes
Date: 8 years ago
Size: 3,105 bytes
 

Contents

Class file image Download
<?php
/**
 * Access file (.htaccess and hosts.deny) update class.
 *
 * @author Rolands Kusiņš
 * @license GPL
 *
 */
class AccessUpdate{
   
// Log object to write some info in log files
   
public $log = null;
   
   
/**
     * Update Apache access file with "Deny from" entries
     *
     * @param string $path
     * @param array $blacklistedIps
     */
   
public function updateApacheAccessFile(&$path, &$blacklistedIps){
       
$newContents = "";
       
$alreadyInFile = array();
       
// Open access file
       
$f = @fopen($path,"r");
        if(
$f){
           
// Check which lines we need to keep and which ones we need to remove
           
while(!feof($f)){
               
// Read line
               
$line = fgets($f,4096);
               
// We are interested only in lines that contain "deny from"
               
if(preg_match("/deny from/i", $line)){
                   
// Trim whitespaces
                   
$line = trim($line);
                   
// Split by space or whitespace
                   
$parts = preg_split("/\s+/", $line);
                    foreach(
$parts as &$part){
                        if(
ip2long($part) !== false){
                           
// Check if ip that is written in access file is in blacklist
                           
if(in_array($part, $blacklistedIps)){
                               
$newContents .= $line."\n";
                               
$alreadyInFile[] = $part;
                            }
                        }
                    }
                } else{
                   
$newContents .= $line;
                }
               
// Slepp for 1 microsecond (so that we don't take all CPU resources and leave small part for other processes
               
usleep(1);
            }
           
// Append with new "Deny from" entries
           
foreach($blacklistedIps as &$blacklistedIp){
                if(!
in_array($blacklistedIp,$alreadyInFile)){
                   
$newContents .= "Deny from ".$blacklistedIp."\n";
                }
            }
           
// Close file
           
@fclose($f);
           
// Writing new contents to file
           
file_put_contents($path, $newContents);
        }
    }
   
   
/**
     * Update hosts.deny file with "sshd: " entries
     *
     * @param string $path
     * @param array $blacklistedIps
     */
   
public function updateHostsDenyFile(&$path, &$blacklistedIps){
       
$newContents = "";
       
$alreadyInFile = array();
       
// Open hosts.deny file
       
$f = @fopen($path,"r");
        if(
$f){
           
// Check which lines we need to keep and which ones we need to remove
           
while(!feof($f)){
               
// Read line
               
$line = fgets($f,4096);
               
// We are interested only in lines that contain "sshd"
               
if(preg_match("/sshd/i", $line)){
                   
// Trim whitespaces
                   
$line = trim($line);
                   
// Split by space or whitespace
                   
$parts = preg_split("/\s+/", $line);
                    foreach(
$parts as &$part){
                        if(
ip2long($part) !== false){
                           
// If ip that is written in access file is in blacklist
                           
if(in_array($part, $blacklistedIps)){
                               
$newContents .= $line."\n";
                               
$alreadyInFile[] = $part;
                            }
                        }
                    }
                } else{
                   
$newContents .= $line;
                }
               
// Slepp for 1 microsecond (so that we don't take all CPU resources and leave small part for other processes
               
usleep(1);
            }
           
// Append with new "Deny from" entries
           
foreach($blacklistedIps as &$blacklistedIp){
                if(!
in_array($blacklistedIp,$alreadyInFile)){
                   
$newContents .= "sshd: ".$blacklistedIp."\n";
                }
            }
           
// Close file
           
@fclose($f);
           
// Writing new contents to file
           
file_put_contents($path, $newContents);
        }
    }
}
?>