Просмотр файла antirip.php

Размер файла: 2.9Kb
<?php 
#########################################################################
# High Activity AntiRip                                                 #
# --------------------------------------------------------------------- #
# Скрипт служит для защиты сайта от тотального скачивания "под корень". #
# Запись запрещающей строки в .htaccess осуществляется автоматически.   #
# --------------------------------------------------------------------- #
# http://deb.telenet.ru/ | http://www.woweb.ru/load/74-1-0-4183         #
#########################################################################

//error_reporting(0);
$address = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['HTTP_REFERER'];
$url = urldecode($_SERVER['REQUEST_URI']);
$limit = 20;
$timenow = time();
$browser = $_SERVER['HTTP_USER_AGENT'];
$htaccess = $_SERVER['DOCUMENT_ROOT']."/.htaccess";
$dirfiles = $_SERVER['DOCUMENT_ROOT']."/antirip/logfiles/";
$logfiles = "$dirfiles".$address;
$hostname = gethostbyaddr($address);
$datetime = date("Y-m-d H:i:s");
$ip1 = getenv("HTTP_X_FORWARDED_FOR");
$ip2 = getenv("REMOTE_ADDR");
$hostip1 = gethostbyaddr($ip1);
$hostip2 = gethostbyaddr($ip2);
if ($ip1 != $ip2) {
  $htstring = NULL;
  if (!empty($ip1)) {
    preg_match_all('/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/', $ip1, $ip1);
    $ip1 = array_unique($ip1[0]);
    foreach ($ip1 as $v) {
      $htstring.="Deny from ".$v."	\\\\ Заблокирован Внутренний IP	\\\\ $hostip1\r\n";
    }
  }
  if (!empty($ip2)) {
    $htstring.="Deny from ".$ip2."	\\\\ Заблокирован IP Proxy	\\\\ $hostip2\r\n";
  }
} else {
  $htstring = "Deny from ".$address."	\\\\ Заблокирован Внешний IP	\\\\ $hostname\r\n";
}
$excludes = array(
"yandex.ru",
"rambler.ru",
"googlebot.com",
"webaltabot.com",
"inktomisearch.com",
"search.live.com",
"deb.telenet.ru",
"213.248.56.160",
"81.19.77.184"
);

if ($opendir = opendir($dirfiles)) {
  while (false !== ($log = readdir($opendir))) {
    if ($log != "." and $log != "..") {
      $timelog = date(filemtime("$dirfiles"."$log"));
      if ($timelog < ($timenow - 60)) {
        unlink("$dirfiles"."$log");
      }
    }
  }
}

foreach ($excludes as $v) {
  if (ereg($v, $hostname)) {exit;}
}

if (!file_exists($logfiles)) {fopen($logfiles, "w+");}
$write = "$datetime - $hostname<br>Browser: $browser<br>Referer: $ref<br>URL: $url<br>\r\n";
if ($logfiles) {
  if (is_writable($logfiles)) {
    if (!$handle = fopen($logfiles, 'a')) {exit;}
    if (fwrite($handle, $write) === FALSE) {exit;}
    fclose($handle);
  }	
}

if ((count(file($logfiles)) > $limit) and ($timelog > ($timenow - 60))) {
  if ($htaccess) {
    foreach (file($htaccess) as $h) {
      if ($h === $htstring) {
        exit;
      }
    }
    if (is_writable($htaccess)) {
      if (!$handle = fopen($htaccess, 'a')) {exit;}
      if (fwrite($handle, $htstring) === FALSE) {exit;}
      fclose($handle);
    }	
  }
}
?>