-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKeypic.php
More file actions
248 lines (193 loc) · 8.97 KB
/
Keypic.php
File metadata and controls
248 lines (193 loc) · 8.97 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/* Copyright 2010-2014 Keypic Inc. (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Works only with PHP >= 5.3.0
*/
class Keypic
{
private static $Instance;
private static $version = '2.0';
private static $UserAgent = 'User-Agent: Keypic PHP Class, Version: 2.0';
private static $SpamPercentage = 70;
private static $host = 'ws.keypic.com';
private static $url = '/';
private static $FormID;
private static $PublisherID;
private static $Token;
private static $RequestType;
private static $WidthHeight;
private static $Debug;
private function __clone(){}
public function __construct(){}
public static function getInstance()
{
if (!self::$Instance)
{
self::$Instance = new self();
}
return self::$Instance;
}
public static function getHost(){return self::$host;}
public static function setHost($host){self::$host = $host;}
public static function getSpamPercentage(){return self::$SpamPercentage;}
public static function setSpamPercentage($SpamPercentage){self::$SpamPercentage = $SpamPercentage;}
public static function getVersion(){return self::$version;}
public static function setVersion($version){self::$version = $version;}
public static function setUserAgent($UserAgent){self::$UserAgent = $UserAgent;}
public static function setFormID($FormID){self::$FormID = $FormID;}
public static function getFormID(){return self::$FormID;}
public static function setPublisherID($PublisherID){self::$PublisherID = $PublisherID;}
public static function setDebug($Debug){self::$Debug = $Debug;}
public static function checkFormID($FormID)
{
$fields['RequestType'] = 'checkFormID';
$fields['ResponseType'] = '2';
$fields['FormID'] = $FormID;
if($response = json_decode(self::sendRequest($fields), true)){return $response;}
return false;
}
public static function getToken($Token, $ClientEmailAddress = '', $ClientUsername = '', $ClientMessage = '', $ClientFingerprint = '', $Quantity = 1)
{
if($Token)
{
self::$Token = $Token;
return self::$Token;
}
else
{
$fields['FormID'] = self::$FormID;
$fields['RequestType'] = 'RequestNewToken';
$fields['ResponseType'] = '2';
$fields['Quantity'] = $Quantity;
$fields['ServerName'] = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '';
$fields['ClientIP'] = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$fields['ClientUserAgent'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$fields['ClientAccept'] = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : '';
$fields['ClientAcceptEncoding'] = isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : '';
$fields['ClientAcceptLanguage'] = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
$fields['ClientAcceptCharset'] = isset($_SERVER['HTTP_ACCEPT_CHARSET']) ? $_SERVER['HTTP_ACCEPT_CHARSET'] : '';
$fields['ClientHttpReferer'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$fields['ClientUsername'] = $ClientUsername;
$fields['ClientEmailAddress'] = $ClientEmailAddress;
$fields['ClientMessage'] = $ClientMessage;
$fields['ClientFingerprint'] = $ClientFingerprint;
$response = json_decode(self::sendRequest($fields), true);
if($response['status'] == 'new_token')
{
self::$Token = $response['Token'];
return $response['Token'];
}
}
self::$Token = false;
return false;
}
public static function setToken()
{
if(self::$Token)
{
return '<input type="hidden" name="Token" value="' . self::$Token . '" />';
}
return false;
}
public static function getIt($RequestType = 'getScript', $WidthHeight = '336x280', $Debug = null)
{
if(self::$Token)
{
switch($RequestType)
{
case 'getImage':
return '<a href="http://' . self::$host . '/?RequestType=getClick&Token=' . self::$Token . '" target="_blank"><img src="//' . self::$host . '/?RequestType=getImage&Token=' . self::$Token . '&WidthHeight=' . $WidthHeight . '&PublisherID=' . self::$PublisherID . '" alt="Form protected by Keypic" /></a>';
break;
// case 'getScript':
default:
return '<script type="text/javascript" src="//' . self::$host . '/?RequestType=getScript&Token=' . self::$Token . '&WidthHeight=' . $WidthHeight . '&PublisherID=' . self::$PublisherID . '"></script>';
break;
}
}
return '<a href="http://keypic.com" target="_blank">At the moment Keypic is not properly configured in your system, please check if everything is configured correctly Subscribe to the service or check your FormID</a>';
}
// Is Spam? from 0% to 100%
public static function isSpam($Token, $ClientEmailAddress = '', $ClientUsername = '', $ClientMessage = '', $ClientFingerprint = '')
{
self::$Token = $Token;
if((self::$Token) && (self::$FormID))
{
$fields['Token'] = self::$Token;
$fields['FormID'] = self::$FormID;
$fields['RequestType'] = 'RequestValidation';
$fields['ResponseType'] = '2';
$fields['ServerName'] = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '';
$fields['ClientIP'] = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$fields['ClientUserAgent'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$fields['ClientAccept'] = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : '';
$fields['ClientAcceptEncoding'] = isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : '';
$fields['ClientAcceptLanguage'] = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
$fields['ClientAcceptCharset'] = isset($_SERVER['HTTP_ACCEPT_CHARSET']) ? $_SERVER['HTTP_ACCEPT_CHARSET'] : '';
$fields['ClientHttpReferer'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$fields['ClientUsername'] = $ClientUsername;
$fields['ClientEmailAddress'] = $ClientEmailAddress;
$fields['ClientMessage'] = $ClientMessage;
$fields['ClientFingerprint'] = $ClientFingerprint;
$response = json_decode(self::sendRequest($fields), true);
if($response['status'] == 'response'){return $response['spam'];}
else if($response['status'] == 'error'){return $response['error'];}
}
return false;
}
public static function reportSpam($Token)
{
if($Token == ''){return 'error';}
if(self::$FormID == ''){return 'error';}
$fields['Token'] = $Token;
$fields['FormID'] = self::$FormID;
$fields['RequestType'] = 'ReportSpam';
$fields['ResponseType'] = '2';
if($response = json_decode(self::sendRequest($fields), true)){return $response;}
else{return false;}
}
// makes a request to the Keypic Web Service
private static function sendRequest($fields)
{
// boundary generation
srand((double)microtime()*1000000);
$boundary = "---------------------".substr(md5(rand(0,32000)),0,10);
// Build the header
$header = "POST " . self::$url . " HTTP/1.0\r\n";
$header .= "Host: " . self::$host . "\r\n";
$header .= "Content-Type: multipart/form-data, boundary=$boundary\r\n";
$header .= self::$UserAgent . "\r\n";
$data = '';
// attach post vars
foreach($fields as $index => $value)
{
$data .="--$boundary\r\n";
$data .= "Content-Disposition: form-data; name=\"$index\"\r\n";
$data .= "\r\n$value\r\n";
$data .="--$boundary\r\n";
}
$header .= "Content-length: " . strlen($data) . "\r\n\r\n";
$opts = array('http' =>
array(
'method' => 'POST',
'header' => $header,
'content' => $data,
'timeout' => 3
)
);
$context = stream_context_create($opts);
if($result = @file_get_contents('http://' . self::$host, false, $context, -1, 40000))
{
return $result;
}
return false;
}
}