Просмотр файла system/inc/include/iplisting.php

Размер файла: 2.78Kb
  1. <?php
  2. /*
  3. =============================================
  4. Движок: SHCMS Engine
  5. =============================================
  6. Official website: http://shcms.ru
  7. =============================================
  8. Данный код защищен авторскими правами
  9. =============================================
  10. */
  11.  
  12. /*
  13. ------------------------
  14. Массивы с настройками
  15. ------------------------
  16. */
  17.  
  18. class iplist {
  19. public $ip;
  20. public $flood_chk = 1;
  21. public $flood_interval = '120';
  22. public $flood_limit = '50';
  23. public $flood_file = "SHCMS_Cache.tmp";
  24. private $requests;
  25.  
  26. function __construct() {
  27. $this->ip = ip2long($this->getip());
  28. if ($this->flood_chk) {
  29. $this->requests = $this->reqcount();}
  30. }
  31.  
  32. /*
  33. -------------------------
  34. Получаем реальный адрес IP
  35. -------------------------
  36. */
  37. private function getip() {
  38. if (isset ($_SERVER['HTTP_X_FORWARDED_FOR']) && $this->ip_valid($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  39. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  40. }elseif ($_SERVER['REMOTE_ADDR']) {
  41. return $_SERVER['REMOTE_ADDR'];
  42. }else {
  43. die('Unknown IP');
  44. }}
  45. /*
  46. ---------------------------------------
  47. Счетчик числа обращений с данного IP
  48. ---------------------------------------
  49. */
  50. private function reqcount() {
  51. global $rootpath;
  52. $tmp = array();
  53. $requests = 1;
  54. if (!file_exists('system/tmp/'. $this->flood_file))
  55. $in = fopen('system/tmp/'. $this->flood_file, "w+");
  56. else
  57. $in = fopen('system/tmp/'. $this->flood_file, "r+");
  58. $now = time();
  59. while ($block = fread($in, 8)) {
  60. $arr = unpack("Lip/Ltime", $block);
  61. if (($now - $arr['time']) > $this->flood_interval) {
  62. continue;}
  63. if ($arr['ip'] == $this->ip) {
  64. $requests++;}
  65. $tmp[] = $arr;
  66. }
  67. fseek($in, 0);
  68. ftruncate($in, 0);
  69. for ($i = 0; $i < count($tmp); $i++) {
  70. fwrite($in, pack('LL', $tmp[$i]['ip'], $tmp[$i]['time']));
  71. }
  72. fwrite($in, pack('LL', $this->ip, $now));
  73. fclose($in);
  74. return $requests;
  75. }
  76.  
  77. /*
  78. --------------------
  79. Валидация IP адреса
  80. --------------------
  81. */
  82. function ip_valid($ip = '') {
  83. $d = explode('.', $ip);
  84. for ($x = 0; $x < 4; $x++)
  85. if (!is_numeric($d[$x]) || ($d[$x] < 0) || ($d[$x] > 255))
  86. return false;
  87. return $ip;}
  88. }
  89. $ipinit = new iplist();
  90. $vi_shcms = $ipinit->ip;
  91.  
  92. $ipp = long2ip($ipl);
  93. unset ($ipinit);
  94.  
  95.  
  96. ?>