|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace jumper423\decaptcha\core; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class DeCaptchaAbstract. |
|
9
|
|
|
*/ |
|
10
|
|
|
abstract class DeCaptchaAbstract implements DeCaptchaInterface |
|
11
|
|
|
{ |
|
12
|
|
|
const RESPONSE_TYPE_STRING = 0; |
|
13
|
|
|
const RESPONSE_TYPE_JSON = 1; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Сервис на который будем загружать капчу. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $domain; |
|
21
|
|
|
|
|
22
|
|
|
protected $errorLang = DeCaptchaErrors::LANG_EN; |
|
23
|
|
|
|
|
24
|
|
|
protected $responseType = self::RESPONSE_TYPE_STRING; |
|
25
|
|
|
/** |
|
26
|
|
|
* Ваш API key. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $apiKey; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var int |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $captchaId; |
|
35
|
|
|
|
|
36
|
|
|
protected $inUrl = 'in.php'; |
|
37
|
|
|
protected $resUrl = 'res.php'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
|
|
abstract public function notTrue(); |
|
43
|
|
|
|
|
44
|
|
|
public function setErrorLang($errorLang) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->errorLang = $errorLang; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
abstract protected function decodeResponse($data, $type, $format = self::RESPONSE_TYPE_STRING); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Узнаём путь до файла |
|
53
|
|
|
* Если передана ссылка, то скачиваем и кладём во временную директорию. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $fileName |
|
56
|
|
|
* |
|
57
|
|
|
* @throws Exception |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function getFilePath($fileName) |
|
62
|
|
|
{ |
|
63
|
|
|
if (strpos($fileName, 'http://') !== false || strpos($fileName, 'https://') !== false) { |
|
64
|
|
|
try { |
|
65
|
|
|
$current = file_get_contents($fileName); |
|
66
|
|
|
} catch (\Exception $e) { |
|
67
|
|
|
throw new DeCaptchaErrors(DeCaptchaErrors::ERROR_FILE_IS_NOT_LOADED, $fileName, $this->errorLang); |
|
68
|
|
|
} |
|
69
|
|
|
$path = tempnam(sys_get_temp_dir(), 'captcha'); |
|
70
|
|
|
file_put_contents($path, $current); |
|
71
|
|
|
|
|
72
|
|
|
return $path; |
|
73
|
|
|
} |
|
74
|
|
|
if (file_exists($fileName)) { |
|
75
|
|
|
return $fileName; |
|
76
|
|
|
} |
|
77
|
|
|
throw new DeCaptchaErrors(DeCaptchaErrors::ERROR_FILE_NOT_FOUND, $fileName, $this->errorLang); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getBaseUrl() |
|
84
|
|
|
{ |
|
85
|
|
|
return "http://{$this->domain}/"; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
const PARAM_FIELD_TYPE_STRING = 0; |
|
89
|
|
|
const PARAM_FIELD_TYPE_INTEGER = 1; |
|
90
|
|
|
|
|
91
|
|
|
const PARAM_SLUG_DEFAULT = 0; |
|
92
|
3 |
|
const PARAM_SLUG_TYPE = 1; |
|
93
|
|
|
const PARAM_SLUG_REQUIRE = 2; |
|
94
|
3 |
|
const PARAM_SLUG_SPEC = 3; |
|
95
|
1 |
|
const PARAM_SLUG_VARIABLE = 4; |
|
96
|
1 |
|
|
|
97
|
3 |
|
const PARAM_SPEC_KEY = 0; |
|
98
|
|
|
const PARAM_SPEC_FILE = 1; |
|
99
|
3 |
|
const PARAM_SPEC_CAPTCHA = 2; |
|
100
|
|
|
|
|
101
|
|
|
protected $paramsNames = []; |
|
102
|
|
|
|
|
103
|
|
|
protected $paramsSettings = []; |
|
104
|
|
|
|
|
105
|
|
|
protected $paramsSpec = []; |
|
106
|
|
|
|
|
107
|
|
|
protected $params = []; |
|
108
|
|
|
|
|
109
|
|
|
public function setParams($params) |
|
110
|
|
|
{ |
|
111
|
|
|
if (is_array($params)) { |
|
112
|
|
|
foreach ($params as $param => $value) { |
|
113
|
|
|
$this->params[$param] = $value; |
|
114
|
3 |
|
} |
|
115
|
3 |
|
} |
|
116
|
2 |
|
} |
|
117
|
2 |
|
|
|
118
|
1 |
|
public function setParamSpec($param, $value) |
|
119
|
1 |
|
{ |
|
120
|
1 |
|
$this->paramsSpec[$param] = $value; |
|
121
|
1 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function getParamSpec($param) |
|
124
|
1 |
|
{ |
|
125
|
1 |
|
if (!array_key_exists($param, $this->paramsSpec) || is_null($this->paramsSpec[$param])) { |
|
126
|
|
|
return null; |
|
127
|
2 |
|
} |
|
128
|
1 |
|
switch ($param) { |
|
129
|
|
|
case static::PARAM_SPEC_FILE: |
|
130
|
1 |
|
return (version_compare(PHP_VERSION, '5.5.0') >= 0) ? new \CURLFile($this->paramsSpec[$param]) : '@' . $this->paramsSpec[$param]; |
|
131
|
|
|
case static::PARAM_SPEC_KEY: |
|
132
|
|
|
return is_callable($this->paramsSpec[$param]) ? $this->paramsSpec[$param]() : $this->paramsSpec[$param]; |
|
133
|
|
|
case static::PARAM_SPEC_CAPTCHA: |
|
134
|
|
|
return $this->paramsSpec[$param]; |
|
135
|
|
|
} |
|
136
|
4 |
|
return null; |
|
137
|
4 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
protected function getParams($action) |
|
140
|
|
|
{ |
|
141
|
|
|
if (empty($this->paramsSettings[$action])) { |
|
142
|
|
|
return []; |
|
143
|
|
|
} |
|
144
|
2 |
|
$params = []; |
|
145
|
2 |
|
foreach ($this->paramsSettings[$action] as $field => $settings) { |
|
146
|
|
|
$value = null; |
|
147
|
|
|
if (array_key_exists($field, $this->params) && (!array_key_exists(self::PARAM_SLUG_VARIABLE, $settings) ^ (array_key_exists(self::PARAM_SLUG_VARIABLE, $settings) && $settings[self::PARAM_SLUG_VARIABLE] === false))) { |
|
148
|
|
|
$value = $this->params[$field]; |
|
149
|
|
|
} |
|
150
|
|
|
if (array_key_exists(self::PARAM_SLUG_DEFAULT, $settings)) { |
|
151
|
|
|
$value = $settings[self::PARAM_SLUG_DEFAULT]; |
|
152
|
1 |
|
} |
|
153
|
1 |
|
if (array_key_exists(self::PARAM_SLUG_SPEC, $settings) && array_key_exists($settings[self::PARAM_SLUG_SPEC], $this->paramsSpec)) { |
|
154
|
|
|
$value = $this->getParamSpec($settings[self::PARAM_SLUG_SPEC]); |
|
155
|
|
|
} |
|
156
|
|
|
if (array_key_exists(self::PARAM_SLUG_REQUIRE, $settings) && $settings[self::PARAM_SLUG_REQUIRE] === true && is_null($value)) { |
|
157
|
|
|
throw new DeCaptchaErrors(DeCaptchaErrors::ERROR_PARAM_REQUIRE, array_key_exists($field, $this->paramsNames) ? $this->paramsNames[$field] : $field, $this->errorLang); |
|
158
|
|
|
} |
|
159
|
1 |
|
if (array_key_exists($field, $this->paramsNames)) { |
|
160
|
1 |
|
switch ($settings[self::PARAM_SLUG_TYPE]) { |
|
161
|
|
|
case self::PARAM_FIELD_TYPE_INTEGER: |
|
162
|
|
|
$params[$this->paramsNames[$field]] = (int)$value; |
|
163
|
|
|
break; |
|
164
|
|
|
case self::PARAM_FIELD_TYPE_STRING: |
|
165
|
|
|
$params[$this->paramsNames[$field]] = (string)$value; |
|
166
|
|
|
break; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
2 |
|
} |
|
170
|
|
|
return $params; |
|
171
|
2 |
|
} |
|
172
|
1 |
|
|
|
173
|
|
|
/** |
|
174
|
1 |
|
* @param array $data |
|
175
|
|
|
* |
|
176
|
|
|
* @return string |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function getActionUrl($data) |
|
179
|
|
|
{ |
|
180
|
|
|
$uri = []; |
|
181
|
|
|
foreach ($data as $key => $value) { |
|
182
|
|
|
$uri[] = "$key=$value"; |
|
183
|
|
|
} |
|
184
|
|
|
return "{$this->getBaseUrl()}{$this->resUrl}?" . implode('&', $uri); |
|
185
|
1 |
|
} |
|
186
|
1 |
|
|
|
187
|
1 |
|
/** |
|
188
|
1 |
|
* @param string $action |
|
189
|
1 |
|
* |
|
190
|
1 |
|
* @return string |
|
191
|
1 |
|
*/ |
|
192
|
1 |
|
protected function getResponse($action) |
|
193
|
|
|
{ |
|
194
|
|
|
return file_get_contents($this->getActionUrl($action)); |
|
|
|
|
|
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @return string |
|
199
|
|
|
*/ |
|
200
|
|
|
protected function getInUrl() |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->getBaseUrl().$this->inUrl; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Проверка на то произошла ли ошибка. |
|
207
|
|
|
* |
|
208
|
|
|
* @param $error |
|
209
|
|
|
* |
|
210
|
|
|
* @throws DeCaptchaErrors |
|
211
|
|
|
*/ |
|
212
|
|
|
protected function isError($error) |
|
213
|
|
|
{ |
|
214
|
|
|
if (strpos($error, 'ERROR') !== false) { |
|
215
|
|
|
throw new DeCaptchaErrors($error, null, $this->errorLang); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
protected $lastRunTime = null; |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Задержка выполнения. |
|
223
|
|
|
* |
|
224
|
|
|
* @param int $delay Количество секунд |
|
225
|
|
|
* @param \Closure|null $callback |
|
226
|
|
|
* |
|
227
|
|
|
* @return mixed |
|
228
|
|
|
*/ |
|
229
|
|
|
protected function executionDelayed($delay = 0, $callback = null) |
|
230
|
|
|
{ |
|
231
|
|
|
$time = microtime(true); |
|
232
|
|
|
$timePassed = $time - $this->lastRunTime; |
|
233
|
|
|
if ($timePassed < $delay) { |
|
234
|
|
|
usleep(($delay - $timePassed) * 1000000); |
|
235
|
|
|
} |
|
236
|
|
|
$this->lastRunTime = microtime(true); |
|
237
|
|
|
|
|
238
|
|
|
return $callback instanceof \Closure ? $callback($this) : $callback; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @param $url |
|
243
|
|
|
* @param $data |
|
244
|
|
|
* @return mixed |
|
245
|
|
|
* @throws DeCaptchaErrors |
|
246
|
|
|
*/ |
|
247
|
|
|
protected function getCurlResponse($url, $data) |
|
248
|
|
|
{ |
|
249
|
|
|
$ch = curl_init(); |
|
250
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
251
|
|
|
if (version_compare(PHP_VERSION, '5.5.0') >= 0 && version_compare(PHP_VERSION, '7.0') < 0 && defined('CURLOPT_SAFE_UPLOAD')) { |
|
252
|
|
|
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); |
|
253
|
|
|
} |
|
254
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
255
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 60); |
|
256
|
|
|
curl_setopt($ch, CURLOPT_POST, true); |
|
257
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
|
258
|
|
|
$result = curl_exec($ch); |
|
259
|
|
|
if (curl_errno($ch)) { |
|
260
|
|
|
throw new DeCaptchaErrors(DeCaptchaErrors::ERROR_CURL, curl_error($ch), $this->errorLang); |
|
261
|
|
|
} |
|
262
|
|
|
curl_close($ch); |
|
263
|
|
|
|
|
264
|
|
|
return $result; |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: