Просмотр файла engine/classes/floodblocker.php

Размер файла: 2.62Kb
  1. <?php
  2. // Author:
  3. // Vagharshak Tozalakyan <vagh@armdex.com>
  4. // This module was written by author on its leasure time.
  5.  
  6. define ( 'E_TMP_DIR', 'Incorrect temprorary directory specified.' );
  7. define ( 'E_IP_ADDR', 'Incorrect IP address specified.' );
  8. define ( 'E_LOG_FILE', 'Log file access error! Check permissions to write.' );
  9. define ( 'E_CRON_FNAME', 'The name of cron file must begin with dot.' );
  10. define ( 'E_CRON_FILE', 'Cron file access error! Check permissions to write.' );
  11. define ( 'E_CRON_JOB', 'Unable to perform the cron job.' );
  12.  
  13. class FloodBlocker
  14. {
  15.  
  16. var $logs_path = tmpDir;
  17. var $ip_addr;
  18. var $rules;
  19. var $cron_file;
  20. var $cron_interval;
  21. var $logs_timeout;
  22.  
  23. function __construct($ip = null)
  24. {
  25.  
  26. if (!is_dir ( $this->logs_path ))
  27. trigger_error ( E_TMP_DIR, E_USER_ERROR );
  28.  
  29. $logs_path = str_replace ( '\\', '/', $this->logs_path);
  30. if ( substr ( $logs_path, -1 ) != '/' )
  31. $logs_path .= '/';
  32.  
  33. $this->logs_path = $logs_path;
  34.  
  35. if (empty($ip))
  36. $ip = $_SERVER['REMOTE_ADDR'];
  37.  
  38. $ip = ip2long ($ip);
  39. if ($ip == -1 || $ip === FALSE)
  40. trigger_error ( E_IP_ADDR, E_USER_ERROR );
  41.  
  42. $this->ip_addr = $ip;
  43.  
  44. $this->rules = unserialize(file_get_contents(H . 'engine/files/data/flood_config.swi'));
  45. $this->cron_file = '.sfb';
  46. $this->cron_interval = 1800; // 30 minutes
  47. $this->logs_timeout = 7200; // 2 hours
  48.  
  49. }
  50.  
  51.  
  52. function RawCheck ( &$info )
  53. {
  54.  
  55. $no_flood = TRUE;
  56.  
  57. foreach ( $this->rules as $interval=>$limit )
  58. {
  59. if ($interval < 1)
  60. continue;
  61. if ( ! isset ( $info[$interval] ) )
  62. {
  63. $info[$interval]['time'] = time ( );
  64. $info[$interval]['count'] = 0;
  65. }
  66.  
  67. $info[$interval]['count'] += 1;
  68.  
  69. if ( time ( ) - $info[$interval]['time'] > $interval )
  70. {
  71. $info[$interval]['count'] = 1;
  72. $info[$interval]['time'] = time ( );
  73. }
  74.  
  75. if ( $info[$interval]['count'] > $limit )
  76. {
  77. $info[$interval]['time'] = time ( );
  78. $no_flood = FALSE;
  79. }
  80. } // foreach
  81.  
  82. return $no_flood;
  83.  
  84. }
  85.  
  86. function CheckFlood ( )
  87. {
  88. $path = $this->logs_path . $this->ip_addr;
  89.  
  90. if ( ! ( $f = fopen ( $path, 'a+' ) ) )
  91. trigger_error ( E_LOG_FILE, E_USER_ERROR);
  92.  
  93. flock ( $f, LOCK_EX );
  94.  
  95. $info = fread ( $f, filesize ( $path ) + 10 );
  96. $info = unserialize( $info );
  97.  
  98. $result = $this->RawCheck ( $info );
  99.  
  100. ftruncate ( $f, 0 );
  101. fwrite ( $f, serialize( $info ) );
  102. fflush ( $f );
  103.  
  104. flock($f, LOCK_UN);
  105.  
  106. fclose($f);
  107.  
  108. return $result;
  109.  
  110. }
  111. }