-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFloodGuard.php
More file actions
118 lines (88 loc) · 2.72 KB
/
FloodGuard.php
File metadata and controls
118 lines (88 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* FloodGuard is a class that helps you to protect your web application from sh*ty information flood
*
* Example usage:
* $fg = new FloodGuard("localhost",11211,2,2);
*
* if($fg->checkPermissionToProceed()) {
* echo "yes! you can proceed!";
* } else {
* echo "no! you shall not pass!";
* }
*
* @package FloodGuard
* @author Daniel Stoinov <daniel.stoinov@gmail.com>
* @version $Revision: 0.02 $
* @access public
* @see http://github.com/ctmnz/
*/
class FloodGuard
{
private static $mcache;
private $fsec;
private $ftimes;
private $raddress;
private $addressPrefix = 'classfloodprotect';
private $mcachevarname;
/**
* Constructor
*
* The constructor of the FloodGuard Class
*
* @param string $mcacheAddr The address of the memcache server
* @param int $mcachePort The port of the memcache server
* @param int $floodsec The time limit
* @param int $floodtimes The maximum user requests per $floodsec
*/
function __construct($mcacheAddr,$mcachePort,$floodsec,$floodtimes) {
$this->mcache = new Memcache();
$this->mcache->connect($mcacheAddr,$mcachePort);
$this->fsec = $floodsec;
$this->ftimes = $floodtimes;
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
$this->raddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$this->raddress = $_SERVER['REMOTE_ADDR'];
}
$this->mcachevarname = $this->addressPrefix.$this->raddress;
}
/**
* returns true or false
*
* @param string $sample the sample data
* @return boolean true/false if the remote IP address has (false) or hasn't (true) reached the information flood limit
* @access public
*/
public function checkPermissionToProceed()
{
if($this->mcache->get($this->mcachevarname)) {
// echo "It was set";
$tmpSetVar = $this->mcache->get($this->mcachevarname);
if ($tmpSetVar>$this->ftimes) {
// echo "Nope... TMI (too much information). Your request wont be processed! ";
$this->mcache->set($this->mcachevarname, $tmpSetVar , MEMCACHE_COMPRESSED, $this->fsec);
// STOP THE PROCESS
return false;
} else {
$tmpSetVar = $tmpSetVar + 1;
$this->mcache->set($this->mcachevarname, $tmpSetVar , MEMCACHE_COMPRESSED, $this->fsec);
//echo $this->mcache->get($mcachevarname);
return true;
}
}
else {
$this->mcache->set($this->mcachevarname, '1', MEMCACHE_COMPRESSED, $this->fsec);
// echo "initial process for this IP address";
return true;
}
}
public function dumpInfo(){
echo "<br>";
echo "raddress = " . $this->raddress . "<br>";
echo "mcachevarname = " . $this->mcachevarname . "<br>";
echo "raddress = " . $this->raddress . "<br>";
echo "<br>";
}
}
?>