forked from HelgeSverre/SpamProtection
/
SpamProtection.php
177 lines (146 loc) · 4.63 KB
/
SpamProtection.php
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* Class used for spam prevention, primarily in contact forms, but can also be used in forum software and comment fields.
*
* @author Helge Sverre <email@helgesverre.com>
*
* @return object SpamProtection object
*
*/
class SpamProtection {
private $AllowTor = false;
private $APIUrl = "http://api.stopforumspam.org/api?f=json";
private $APIKey;
/**
* Internal function used to query the StopForumSpam API for an IP address.
*
* @author Helge Sverre <email@helgesverre.com>
*
* @param string $IP the IP address to search the API for.
* @return object returns a json decoded object, used by the public CheckIP function.
*/
private function APICheckIP($IP) {
$FullAPIURL = $this->APIUrl . "&ip=" . $IP;
if ($this->AllowTor) {
$FullAPIURL .= "&notorexit";
}
$json = file_get_contents($FullAPIURL);
if(!$json === false) {
return json_decode($json);
} else {
throw new Exception("API Check Unsuccsessfull");
}
}
/**
* Internal function used to query the StopForumSpam API for an Email address.
*
* @author Helge Sverre <email@helgesverre.com>
*
* @param string $Email the Email address to search the API for.
* @return object returns a json decoded object, used by the public CheckEmail function.
*/
private function APICheckEmail($Email) {
$FullAPIURL = $this->APIUrl . "&email=" . $Email;
if ($this->AllowTor) {
$FullAPIURL .= "&notorexit";
}
$json = file_get_contents($FullAPIURL);
if(!$json === false) {
return json_decode($json);
} else {
throw new Exception("Could not fetch contents of URL");
}
}
/**
* Function used to query the StopForumSpam API for an IP address and return if it is registered as a spamer IP.
*
* @author Helge Sverre <email@helgesverre.com>
*
* @param string $IP the IP address to search the API for.
* @return bool true means the IP is a spammy ip, if false, it's not, throws an exception on failure
*/
public function CheckIP($IP) {
$Result = $this->APICheckIP($IP);
if ($Result->success == 1) {
return $Result->ip->appears;
} else {
throw new Exception("Error: " . $Result->error);
}
}
/**
* Function used to query the StopForumSpam API for an Email address and return if it is registered as a spam email.
*
* @author Helge Sverre <email@helgesverre.com>
*
* @param string $Email the Email address to search the API for.
* @return bool true means the IP is a spammy email, if false, it's not, throws an exception on failure
*/
public function CheckEmail($Email) {
$Result = $this->APICheckEmail($Email);
if ($Result->success == 1) {
return $Result->email->appears;
} else {
throw new Exception("Error: " . $Result->error);
}
}
/**
* Function used to submit a spam report to StopForumSpam,
*
* @author Helge Sverre <email@helgesverre.com>
*
* @param string $username Username of the spammer.
* @param string $ip the ip of the spammer
* @param string $evidence evidence of spam, usually you pass it a copy of the original email(with all the headers etc).
* @param string $email the spammers email.
* @return bool returns true if report was submitted, exception on failure.
*/
public function SubmitReport($username, $ip, $evidence, $email) {
$Request = "http://www.stopforumspam.com/add.php"
. "?username=" . urlencode($username)
. "&ip_addr=" . urlencode($ip)
. "&evidence=" . urlencode($evidence)
. "&email=" . urlencode($email)
. "&api_key=" . urlencode($this->APIKey);
$Response = file_get_contents($Request);
if (!strpos($Response, "data submitted successfully") === FALSE) {
return true;
} else {
throw new Exception("Submission failed.");
}
}
/**
* Function used to enable or disable blocking of TOR Exit node IP's, disabled by default
*
* @author Helge Sverre <email@helgesverre.com>
*
* @param bool $bool wether or not you want tor users to be flagged as spammers.
*/
public function AllowTor($bool) {
$this->AllowTor = $bool;
}
/**
* Function used to set your API key, this is only neccesary if you need the ability to submit spam reports.
*
* @author Helge Sverre <email@helgesverre.com>
*
* @param string $APIKey your API key, can be obtained from here: http://www.stopforumspam.com/signup
*/
public function SetAPIKey($APIKey) {
$this->APIKey = $APIKey;
}
/**
* returns the api key
*
* @author Helge Sverre <email@helgesverre.com>
*
* @return string the set api key, throws an exception on failure.
*/
public function GetAPIKey() {
if (isset($this->APIKey)){
return $this->APIKey;
} else {
throw new Exception("API Key not set, cannot return empty api key.");
}
}
}
?>