View file system/inc/include/iplisting.php

File size: 2.78Kb
<?php
/*
=============================================
 Движок:  SHCMS Engine
=============================================
 Official website: http://shcms.ru
=============================================
 Данный код защищен авторскими правами
=============================================
*/

/*
------------------------
Массивы с настройками
------------------------
*/

class iplist {
    public $ip;
    public $flood_chk = 1; 
    public $flood_interval = '120';   
    public $flood_limit = '50';    
    public $flood_file = "SHCMS_Cache.tmp";  
    private $requests;    

    function __construct() {
        $this->ip = ip2long($this->getip());
        if ($this->flood_chk) {
            $this->requests = $this->reqcount();}
        }

        /*
        -------------------------
        Получаем реальный адрес IP
        -------------------------
        */
    private function getip() {
        if (isset ($_SERVER['HTTP_X_FORWARDED_FOR']) && $this->ip_valid($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
        }elseif ($_SERVER['REMOTE_ADDR']) {
            return $_SERVER['REMOTE_ADDR'];
        }else {
            die('Unknown IP');
        }}
        /*
        ---------------------------------------
        Счетчик числа обращений с данного IP
        ---------------------------------------
        */
    private function reqcount() {
        global $rootpath;
        $tmp = array();
        $requests = 1;
        if (!file_exists('system/tmp/'. $this->flood_file))
        $in = fopen('system/tmp/'. $this->flood_file, "w+");
        else
        $in = fopen('system/tmp/'. $this->flood_file, "r+");
    
        $now = time();
        while ($block = fread($in, 8)) {
        $arr = unpack("Lip/Ltime", $block);
		
        if (($now - $arr['time']) > $this->flood_interval) {
        continue;}
		
        if ($arr['ip'] == $this->ip) {
        $requests++;}
        $tmp[] = $arr;
		}
		
        fseek($in, 0);
        ftruncate($in, 0);
        for ($i = 0; $i < count($tmp); $i++) {
        fwrite($in, pack('LL', $tmp[$i]['ip'], $tmp[$i]['time']));
		}
        fwrite($in, pack('LL', $this->ip, $now));
        fclose($in);
        return $requests;
        }

	/*
	--------------------
	Валидация IP адреса
	--------------------
	*/
    function ip_valid($ip = '') {
        $d = explode('.', $ip);
        for ($x = 0; $x < 4; $x++)
        if (!is_numeric($d[$x]) || ($d[$x] < 0) || ($d[$x] > 255))
        return false;
        return $ip;}
}
$ipinit = new iplist();
$vi_shcms = $ipinit->ip;

$ipp = long2ip($ipl);
unset ($ipinit);


?>