1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace jumper423\decaptcha\core; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Распознавание капчи. |
9
|
|
|
* |
10
|
|
|
* Class DeCaptchaBase |
11
|
|
|
*/ |
12
|
|
|
class DeCaptchaBase extends DeCaptchaAbstract implements DeCaptchaInterface |
13
|
|
|
{ |
14
|
|
|
const PARAM_FIELD_METHOD = 0; |
15
|
|
|
const PARAM_FIELD_KEY = 1; |
16
|
|
|
const PARAM_FIELD_FILE = 2; |
17
|
|
|
const PARAM_FIELD_PHRASE = 3; |
18
|
|
|
const PARAM_FIELD_REGSENSE = 4; |
19
|
|
|
const PARAM_FIELD_NUMERIC = 5; |
20
|
|
|
const PARAM_FIELD_MIN_LEN = 6; |
21
|
|
|
const PARAM_FIELD_MAX_LEN = 7; |
22
|
|
|
const PARAM_FIELD_LANGUAGE = 8; |
23
|
|
|
const PARAM_FIELD_SOFT_ID = 9; |
24
|
|
|
|
25
|
|
|
const PARAM_FIELD_TYPE_STRING = 0; |
26
|
|
|
const PARAM_FIELD_TYPE_INTEGER = 1; |
27
|
|
|
|
28
|
|
|
const PARAM_SLUG_DEFAULT = 0; |
29
|
|
|
const PARAM_SLUG_TYPE = 1; |
30
|
|
|
const PARAM_SLUG_REQUIRE = 2; |
31
|
|
|
const PARAM_SLUG_SPEC = 3; |
32
|
|
|
const PARAM_SLUG_VARIABLE = 4; |
33
|
|
|
|
34
|
|
|
const PARAM_SPEC_KEY = 0; |
35
|
|
|
const PARAM_SPEC_FILE = 1; |
36
|
|
|
|
37
|
|
|
protected $paramsNames = [ |
38
|
|
|
self::PARAM_FIELD_METHOD => 'method', |
39
|
|
|
self::PARAM_FIELD_KEY => 'key', |
40
|
|
|
self::PARAM_FIELD_FILE => 'file', |
41
|
|
|
self::PARAM_FIELD_PHRASE => 'phrase', |
42
|
|
|
self::PARAM_FIELD_REGSENSE => 'regsense', |
43
|
|
|
self::PARAM_FIELD_NUMERIC => 'numeric', |
44
|
|
|
self::PARAM_FIELD_MIN_LEN => 'min_len', |
45
|
|
|
self::PARAM_FIELD_MAX_LEN => 'max_len', |
46
|
|
|
self::PARAM_FIELD_LANGUAGE => 'language', |
47
|
|
|
self::PARAM_FIELD_SOFT_ID => 'soft_id', |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
protected $paramsSettings = [ |
51
|
|
|
self::PARAM_FIELD_METHOD => [ |
52
|
|
|
self::PARAM_SLUG_DEFAULT => 'post', |
53
|
|
|
self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_STRING, |
54
|
|
|
], |
55
|
|
|
self::PARAM_FIELD_KEY => [ |
56
|
|
|
self::PARAM_SLUG_REQUIRE => true, |
57
|
|
|
self::PARAM_SLUG_SPEC => self::PARAM_SPEC_KEY, |
58
|
|
|
self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_STRING, |
59
|
|
|
], |
60
|
|
|
self::PARAM_FIELD_FILE => [ |
61
|
|
|
self::PARAM_SLUG_REQUIRE => true, |
62
|
|
|
self::PARAM_SLUG_SPEC => self::PARAM_SPEC_FILE, |
63
|
|
|
self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_STRING, |
64
|
|
|
], |
65
|
|
|
self::PARAM_FIELD_PHRASE => [ |
66
|
|
|
self::PARAM_SLUG_DEFAULT => 0, |
67
|
|
|
self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_INTEGER, |
68
|
|
|
], |
69
|
|
|
self::PARAM_FIELD_REGSENSE => [ |
70
|
|
|
self::PARAM_SLUG_DEFAULT => 0, |
71
|
|
|
self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_INTEGER, |
72
|
|
|
], |
73
|
|
|
self::PARAM_FIELD_NUMERIC => [ |
74
|
|
|
self::PARAM_SLUG_DEFAULT => 0, |
75
|
|
|
self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_INTEGER, |
76
|
|
|
], |
77
|
|
|
self::PARAM_FIELD_MIN_LEN => [ |
78
|
|
|
self::PARAM_SLUG_DEFAULT => 0, |
79
|
|
|
self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_INTEGER, |
80
|
|
|
], |
81
|
|
|
self::PARAM_FIELD_MAX_LEN => [ |
82
|
|
|
self::PARAM_SLUG_DEFAULT => 0, |
83
|
|
|
self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_INTEGER, |
84
|
|
|
], |
85
|
|
|
self::PARAM_FIELD_LANGUAGE => [ |
86
|
|
|
self::PARAM_SLUG_DEFAULT => 0, |
87
|
|
|
self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_INTEGER, |
88
|
|
|
], |
89
|
|
|
self::PARAM_FIELD_SOFT_ID => [ |
90
|
|
|
self::PARAM_SLUG_VARIABLE => false, |
91
|
|
|
self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_INTEGER, |
92
|
|
|
], |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
protected $paramsSpec = [ |
96
|
|
|
self::PARAM_SPEC_KEY => null, |
97
|
|
|
self::PARAM_SPEC_FILE => null, |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
protected $params = []; |
101
|
|
|
|
102
|
|
|
protected function getParams(){ |
103
|
|
|
$params = []; |
104
|
|
|
foreach ($this->paramsSettings as $field => $settings) { |
105
|
|
|
$value = null; |
106
|
|
|
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))) { |
107
|
|
|
$value = $this->params[$field]; |
108
|
|
|
} |
109
|
|
|
if(array_key_exists(self::PARAM_SLUG_DEFAULT, $settings)) { |
110
|
|
|
$value = $settings[self::PARAM_SLUG_DEFAULT]; |
111
|
|
|
} |
112
|
|
|
if(array_key_exists(self::PARAM_SLUG_SPEC, $settings) && array_key_exists($settings[self::PARAM_SLUG_SPEC], $this->paramsSpec)) { |
113
|
|
|
$value = $this->paramsSpec[$settings[self::PARAM_SLUG_SPEC]]; |
114
|
|
|
} |
115
|
|
|
if(array_key_exists(self::PARAM_SLUG_REQUIRE, $settings) && $settings[self::PARAM_SLUG_REQUIRE] === true && is_null($value)) { |
116
|
|
|
throw new Exception('Нет данных'); |
117
|
|
|
} |
118
|
|
|
if(array_key_exists($field, $this->paramsNames)) { |
119
|
|
|
switch ($settings[self::PARAM_SLUG_TYPE]) { |
120
|
|
|
case self::PARAM_FIELD_TYPE_INTEGER: |
121
|
|
|
$params[$this->paramsNames[$field]] = (int)$value; |
122
|
|
|
break; |
123
|
|
|
case self::PARAM_FIELD_TYPE_STRING: |
124
|
|
|
$params[$this->paramsNames[$field]] = (string)$value; |
125
|
|
|
break; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function recognize($filePath) |
132
|
|
|
{ |
133
|
|
|
$this->result = null; |
|
|
|
|
134
|
|
|
$this->error = null; |
|
|
|
|
135
|
|
|
try { |
136
|
|
|
$filePath = $this->getFilePath($filePath); |
137
|
|
|
$result = $this->getCurlResponse($this->getPostData($filePath)); |
138
|
|
|
$this->setError($result); |
|
|
|
|
139
|
|
|
list(, $this->captchaId) = explode('|', $result); |
140
|
|
|
$waitTime = 0; |
141
|
|
|
sleep($this->requestTimeout); |
|
|
|
|
142
|
|
View Code Duplication |
while (true) { |
|
|
|
|
143
|
|
|
$result = $this->getResponse('get'); |
144
|
|
|
$this->setError($result); |
|
|
|
|
145
|
|
|
if ($result == 'CAPCHA_NOT_READY') { |
146
|
|
|
$waitTime += $this->requestTimeout; |
147
|
|
|
if ($waitTime > $this->maxTimeout) { |
|
|
|
|
148
|
|
|
break; |
149
|
|
|
} |
150
|
|
|
sleep($this->requestTimeout); |
151
|
|
|
} else { |
152
|
|
|
$ex = explode('|', $result); |
153
|
|
|
if (trim($ex[0]) == 'OK') { |
154
|
|
|
$this->result = trim($ex[1]); |
155
|
|
|
|
156
|
|
|
return true; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
throw new Exception('Лимит времени превышен'); |
161
|
|
|
} catch (Exception $e) { |
162
|
|
|
$this->error = $e->getMessage(); |
163
|
|
|
|
164
|
|
|
return false; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function getCode() |
169
|
|
|
{ |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getError() |
173
|
|
|
{ |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Запуск распознавания капчи. |
178
|
|
|
* |
179
|
|
|
* @deprecated |
180
|
|
|
* |
181
|
|
|
* @param string $filePath Путь до файла или ссылка на него |
182
|
|
|
* |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
|
|
public function run($filePath) |
186
|
|
|
{ |
187
|
|
|
return $this->recognize($filePath); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Не верно распознана. |
192
|
|
|
*/ |
193
|
|
|
public function notTrue() |
194
|
|
|
{ |
195
|
|
|
$this->getResponse('reportbad'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param string $filePath |
200
|
|
|
*/ |
201
|
|
|
protected function getPostData($filePath) |
202
|
|
|
{ |
203
|
|
|
return [ |
204
|
|
|
'method' => 'post', |
205
|
|
|
'key' => $this->apiKey, |
206
|
|
|
'file' => (version_compare(PHP_VERSION, '5.5.0') >= 0) ? new \CURLFile($filePath) : '@'.$filePath, |
207
|
|
|
'phrase' => $this->isPhrase, |
|
|
|
|
208
|
|
|
'regsense' => $this->isRegSense, |
|
|
|
|
209
|
|
|
'numeric' => $this->isNumeric, |
|
|
|
|
210
|
|
|
'min_len' => $this->minLen, |
|
|
|
|
211
|
|
|
'max_len' => $this->maxLen, |
|
|
|
|
212
|
|
|
'language' => $this->language, |
|
|
|
|
213
|
|
|
'soft_id' => 882, |
214
|
|
|
]; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
protected function decodeResponse($data, $type, $format = self::RESPONSE_TYPE_STRING) |
218
|
|
|
{ |
219
|
|
|
$result = [ |
|
|
|
|
220
|
|
|
'type' => null, |
221
|
|
|
'data' => null, |
222
|
|
|
]; |
223
|
|
|
switch ($this->responseType) { |
224
|
|
|
case self::RESPONSE_TYPE_STRING: |
225
|
|
|
if ($type) { |
226
|
|
|
$array = explode('|', $this->responseType); |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
break; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: