-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptchafa.php
213 lines (186 loc) · 4.94 KB
/
captchafa.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
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
<?php
/**
* @package Joomla.Plugin
* @subpackage Captcha
*
* @copyright Copyright (C) 2013 - 2014 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
/**
* CaptchaFa Plugin.
* Based on the oficial captcha library ( http://captchafa.net/ )
*
* @package Joomla.Plugin
* @subpackage Captcha
* @since 2.5
*/
class PlgCaptchaCaptchafa extends JPlugin
{
const CAPTCHAFA_API_SERVER = "http://www.captchafa.net/Api";
const CAPTCHAFA_VERIFY_SERVER = "www.captchafa.net";
/**
* Load the language file on instantiation.
*
* @var boolean
* @since 3.1
*/
protected $autoloadLanguage = true;
public function __construct($subject, $config)
{
parent::__construct($subject, $config);
$this->loadLanguage();
}
/**
* Initialise the captcha
*
* @param string $id The id of the field.
*
* @return Boolean True on success, false otherwise
*
* @since 2.5
*/
public function onInit($id)
{
$pubkey = $this->params->get('public_key', '');
if ($pubkey == null || $pubkey == '')
{
throw new Exception(JText::_('PLG_CAPTCHAFA_ERROR_NO_PUBLIC_KEY'));
}
return true;
}
/**
* Gets the challenge HTML
*
* @return string The HTML to be embedded in the form.
*
* @since 2.5
*/
public function onDisplay($name, $id, $class)
{
$document = JFactory::getDocument();
$style = '.captchafatable{direction:ltr !important} .captchafa_input_area input{direction:rtl !important; text-align:right !important}';
$document->addStyleDeclaration($style);
$pubkey = $this->params->get('public_key', '');
$server = self::CAPTCHAFA_API_SERVER;
return '<script type="text/javascript" src="'. $server . '/challenge/k=' . $pubkey . '"></script>';
}
/**
* Calls an HTTP POST function to verify if the user's guess was correct
*
* @return True if the answer is correct, false otherwise
*
* @since 2.5
*/
public function onCheckAnswer($code)
{
$input = JFactory::getApplication()->input;
$privatekey = $this->params->get('private_key');
$remoteip = $input->server->get('REMOTE_ADDR', '', 'string');
$challenge = $input->get('captchafa_challenge_field', '', 'string');
$response = $input->get('captchafa_response_field', '', 'string');
// Check for Private Key
if (empty($privatekey))
{
$this->_subject->setError(JText::_('PLG_CAPTCHAFA_ERROR_NO_PRIVATE_KEY'));
return false;
}
// Check for IP
if (empty($remoteip))
{
$this->_subject->setError(JText::_('PLG_CAPTCHAFA_ERROR_NO_IP'));
return false;
}
// Discard spam submissions
if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0)
{
$this->_subject->setError(JText::_('PLG_CAPTCHAFA_ERROR_EMPTY_SOLUTION'));
return false;
}
$response = $this->_captchafa_http_post(
self::CAPTCHAFA_VERIFY_SERVER, "/Api/verify",
array(
'privatekey' => $privatekey,
'remoteip' => $remoteip,
'challenge' => $challenge,
'response' => $response
)
);
$answers = explode("\n", $response[1]);
if (trim($answers[0]) == 'true')
{
return true;
}
else
{
if($answers[0] == 'invalid query! please retry')
{
$answers[1] = 'INVALID-QUERY';
}
switch($answers[1])
{
case 'کد وارد شده ا': $answers[1] = 'INCORRECT-CAPTCHA-SOL';
}
//@todo use exceptions here
$this->_subject->setError(JText::_('PLG_CAPTCHAFA_ERROR_'.strtoupper(str_replace('-', '_', $answers[1]))));
return false;
}
}
/**
* Encodes the given data into a query string format.
*
* @param string $data Array of string elements to be encoded
*
* @return string Encoded request
*
* @since 2.5
*/
private function _captchafa_qsencode($data)
{
$req = "";
foreach ($data as $key => $value)
{
$req .= $key . '=' . urlencode(stripslashes($value)) . '&';
}
// Cut the last '&'
$req = rtrim($req, '&');
return $req;
}
/**
* Submits an HTTP POST to a CAPTCHAfa server.
*
* @param string $host
* @param string $path
* @param array $data
* @param int $port
*
* @return array Response
*
* @since 2.5
*/
private function _captchafa_http_post($host, $path, $data, $port = 80)
{
$req = $this->_captchafa_qsencode($data);
$http_request = "POST $path HTTP/1.0\r\n";
$http_request .= "Host: $host\r\n";
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .= "Content-Length: " . strlen($req) . "\r\n";
$http_request .= "User-Agent: CAPTCHAfa/PHP\r\n";
$http_request .= "\r\n";
$http_request .= $req;
$response = '';
if (($fs = @fsockopen($host, $port, $errno, $errstr, 10)) == false )
{
die('Could not open socket');
}
fwrite($fs, $http_request);
while (!feof($fs))
{
// One TCP-IP packet
$response .= fgets($fs, 1160);
}
fclose($fs);
$response = explode("\r\n\r\n", $response, 2);
return $response;
}
}