Completed
Push — master ( 75ca6a...d59674 )
by Владислав
02:12
created

DeCaptchaBase::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace jumper423\decaptcha\core;
4
5
/**
6
 * Распознавание капчи.
7
 *
8
 * Class DeCaptchaBase
9
 */
10
class DeCaptchaBase extends DeCaptchaAbstract implements DeCaptchaInterface
11
{
12
    const ACTION_RECOGNIZE = 0;
13
    const ACTION_UNIVERSAL = 1;
14
    const ACTION_UNIVERSAL_WITH_CAPTCHA = 2;
15
16
    const ACTION_FIELD_METHOD = 0;
17
    const ACTION_FIELD_KEY = 1;
18
    const ACTION_FIELD_FILE = 2;
19
    const ACTION_FIELD_PHRASE = 3;
20
    const ACTION_FIELD_REGSENSE = 4;
21
    const ACTION_FIELD_NUMERIC = 5;
22
    const ACTION_FIELD_MIN_LEN = 6;
23
    const ACTION_FIELD_MAX_LEN = 7;
24
    const ACTION_FIELD_LANGUAGE = 8;
25
    const ACTION_FIELD_SOFT_ID = 9;
26
    const ACTION_FIELD_CAPTCHA_ID = 10;
27
    const ACTION_FIELD_ACTION = 11;
28
    const ACTION_FIELD_QUESTION = 12;
29
    const ACTION_FIELD_CALC = 13;
30
    const ACTION_FIELD_HEADER_ACAO = 14;
31
    const ACTION_FIELD_TEXTINSTRUCTIONS = 15;
32
    const ACTION_FIELD_PINGBACK = 16;
33
34
    const RESPONSE_RECOGNIZE_OK = 'OK';
35
    const RESPONSE_RECOGNIZE_REPEAT = 'ERROR_NO_SLOT_AVAILABLE';
36
    const RESPONSE_GET_OK = 'OK';
37
    const RESPONSE_GET_REPEAT = 'CAPCHA_NOT_READY';
38
39
    const SLEEP_RECOGNIZE = 5;
40
    const SLEEP_GET = 2;
41
    const SLEEP_BETWEEN = 5;
42
43
    const DECODE_ACTION_RECOGNIZE = 0;
44
    const DECODE_ACTION_GET = 1;
45
    const DECODE_ACTION_BALANCE = 2;
46
47
    const DECODE_PARAM_RESPONSE = 0;
48
    const DECODE_PARAM_CAPTCHA = 1;
49
    const DECODE_PARAM_CODE = 2;
50
51
    protected $paramsNames = [
52
        self::ACTION_FIELD_METHOD           => 'method',
53
        self::ACTION_FIELD_KEY              => 'key',
54
        self::ACTION_FIELD_FILE             => 'file',
55
        self::ACTION_FIELD_PHRASE           => 'phrase',
56
        self::ACTION_FIELD_REGSENSE         => 'regsense',
57
        self::ACTION_FIELD_NUMERIC          => 'numeric',
58
        self::ACTION_FIELD_MIN_LEN          => 'min_len',
59
        self::ACTION_FIELD_MAX_LEN          => 'max_len',
60
        self::ACTION_FIELD_LANGUAGE         => 'language',
61
        self::ACTION_FIELD_SOFT_ID          => 'soft_id',
62
        self::ACTION_FIELD_CAPTCHA_ID       => 'id',
63
        self::ACTION_FIELD_ACTION           => 'action',
64
        self::ACTION_FIELD_QUESTION         => 'question',
65
        self::ACTION_FIELD_HEADER_ACAO      => 'header_acao',
66
        self::ACTION_FIELD_TEXTINSTRUCTIONS => 'textinstructions',
67
        self::ACTION_FIELD_PINGBACK         => 'pingback',
68
    ];
69
70
    protected $actions = [
71
        self::ACTION_RECOGNIZE => [
72
            self::ACTION_URI    => 'in.php',
73
            self::ACTION_METHOD => self::ACTION_METHOD_POST,
74
            self::ACTION_FIELDS => [
75
                self::ACTION_FIELD_METHOD => [
76
                    self::PARAM_SLUG_DEFAULT => 'post',
77
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
78
                ],
79
                self::ACTION_FIELD_KEY => [
80
                    self::PARAM_SLUG_REQUIRE => true,
81
                    self::PARAM_SLUG_SPEC    => self::PARAM_SPEC_API_KEY,
82
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
83
                ],
84
                self::ACTION_FIELD_FILE => [
85
                    self::PARAM_SLUG_REQUIRE => true,
86
                    self::PARAM_SLUG_SPEC    => self::PARAM_SPEC_FILE,
87
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_MIX,
88
                ],
89
                self::ACTION_FIELD_PHRASE => [
90
                    self::PARAM_SLUG_DEFAULT => 0,
91
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
92
                ],
93
                self::ACTION_FIELD_REGSENSE => [
94
                    self::PARAM_SLUG_DEFAULT => 0,
95
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
96
                ],
97
                self::ACTION_FIELD_NUMERIC => [
98
                    self::PARAM_SLUG_DEFAULT => 0,
99
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
100
                ],
101
                self::ACTION_FIELD_MIN_LEN => [
102
                    self::PARAM_SLUG_DEFAULT => 0,
103
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
104
                ],
105
                self::ACTION_FIELD_MAX_LEN => [
106
                    self::PARAM_SLUG_DEFAULT => 0,
107
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
108
                ],
109
                self::ACTION_FIELD_LANGUAGE => [
110
                    self::PARAM_SLUG_DEFAULT => 0,
111
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
112
                ],
113
                self::ACTION_FIELD_QUESTION => [
114
                    self::PARAM_SLUG_DEFAULT => 0,
115
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
116
                ],
117
                self::ACTION_FIELD_CALC => [
118
                    self::PARAM_SLUG_DEFAULT => 0,
119
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
120
                ],
121
                self::ACTION_FIELD_HEADER_ACAO => [
122
                    self::PARAM_SLUG_DEFAULT => 0,
123
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
124
                ],
125
                self::ACTION_FIELD_TEXTINSTRUCTIONS => [
126
                    self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_STRING,
127
                ],
128
                self::ACTION_FIELD_PINGBACK => [
129
                    self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_STRING,
130
                ],
131
                self::ACTION_FIELD_SOFT_ID => [
132
                    self::PARAM_SLUG_VARIABLE => false,
133
                    self::PARAM_SLUG_TYPE     => self::PARAM_FIELD_TYPE_INTEGER,
134
                ],
135
            ],
136
        ],
137
        self::ACTION_UNIVERSAL => [
138
            self::ACTION_URI    => 'res.php',
139
            self::ACTION_METHOD => self::ACTION_METHOD_GET,
140
            self::ACTION_FIELDS => [
141
                self::ACTION_FIELD_KEY => [
142
                    self::PARAM_SLUG_REQUIRE => true,
143
                    self::PARAM_SLUG_SPEC    => self::PARAM_SPEC_API_KEY,
144
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
145
                ],
146
                self::ACTION_FIELD_ACTION => [
147
                    self::PARAM_SLUG_REQUIRE => true,
148
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
149
                ],
150
                self::ACTION_FIELD_HEADER_ACAO => [
151
                    self::PARAM_SLUG_DEFAULT => 0,
152
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
153
                ],
154
            ],
155
        ],
156
        self::ACTION_UNIVERSAL_WITH_CAPTCHA => [
157
            self::ACTION_URI    => 'res.php',
158
            self::ACTION_METHOD => self::ACTION_METHOD_GET,
159
            self::ACTION_FIELDS => [
160
                self::ACTION_FIELD_KEY => [
161
                    self::PARAM_SLUG_REQUIRE => true,
162
                    self::PARAM_SLUG_SPEC    => self::PARAM_SPEC_API_KEY,
163
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
164
                ],
165
                self::ACTION_FIELD_CAPTCHA_ID => [
166
                    self::PARAM_SLUG_REQUIRE => true,
167
                    self::PARAM_SLUG_SPEC    => self::PARAM_SPEC_CAPTCHA,
168
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
169
                ],
170
                self::ACTION_FIELD_ACTION => [
171
                    self::PARAM_SLUG_REQUIRE => true,
172
                    self::PARAM_SLUG_DEFAULT => 'get',
173
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
174
                ],
175
                self::ACTION_FIELD_HEADER_ACAO => [
176
                    self::PARAM_SLUG_DEFAULT => 0,
177
                    self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
178
                ],
179
            ],
180
        ],
181
    ];
182
183
    protected $decodeSettings = [
184
        self::DECODE_FORMAT => self::RESPONSE_TYPE_STRING,
185
        self::DECODE_ACTION => [
186
            self::DECODE_ACTION_RECOGNIZE => [
187
                self::DECODE_SEPARATOR => '|',
188
                self::DECODE_PARAMS    => [
189
                    self::DECODE_PARAM_RESPONSE => [
190
                        self::DECODE_PARAM_SETTING_MARKER => 0,
191
                    ],
192
                    self::DECODE_PARAM_CAPTCHA => [
193
                        self::DECODE_PARAM_SETTING_MARKER => 1,
194
                    ],
195
                ],
196
            ],
197
            self::DECODE_ACTION_GET => [
198
                self::DECODE_SEPARATOR => '|',
199
                self::DECODE_PARAMS    => [
200
                    self::DECODE_PARAM_RESPONSE => [
201
                        self::DECODE_PARAM_SETTING_MARKER => 0,
202
                    ],
203
                    self::DECODE_PARAM_CODE => [
204
                        self::DECODE_PARAM_SETTING_MARKER => 1,
205
                    ],
206
                ],
207
            ],
208
            self::DECODE_ACTION_BALANCE => [
209
                self::DECODE_SEPARATOR => '|',
210
                self::DECODE_PARAMS    => [
211
                    self::DECODE_PARAM_RESPONSE => [
212
                        self::DECODE_PARAM_SETTING_MARKER => 0,
213
                    ],
214
                ],
215
            ],
216
        ],
217
    ];
218
219
    protected $limitSettings = [
220
        self::ACTION_RECOGNIZE              => 3,
221
        self::ACTION_UNIVERSAL_WITH_CAPTCHA => 20,
222
    ];
223
224
    /**
225
     * DeCaptchaBase constructor.
226
     *
227
     * @param $params
228
     */
229
    public function __construct($params)
230
    {
231
        $this->setParams($params);
232
        $this->init();
233
    }
234
235
    public function init(){}
236
237
    /**
238
     * @param $filePath
239
     *
240
     * @throws DeCaptchaErrors
241
     *
242
     * @return bool
243
     */
244
    public function recognize($filePath)
245
    {
246
        try {
247
            $this->resetLimits();
248
            $this->setParam(static::PARAM_SPEC_FILE, $this->getFilePath($filePath));
249
250
            return $this->requestRecognize() && $this->requestCode();
251
        } catch (DeCaptchaErrors $e) {
252
            if ($this->causeAnError) {
253
                throw $e;
254
            }
255
            $this->errorObject = $e;
256
257
            return false;
258
        }
259
    }
260
261
    /**
262
     * Запуск распознавания капчи.
263
     *
264
     * @deprecated
265
     *
266
     * @param string $filePath Путь до файла или ссылка на него
267
     *
268
     * @return bool
269
     */
270
    public function run($filePath)
271
    {
272
        return $this->recognize($filePath);
273
    }
274
275
    /**
276
     * @throws DeCaptchaErrors
277
     *
278
     * @return bool
279
     */
280 View Code Duplication
    protected function requestRecognize()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
281
    {
282
        while ($this->limitHasNotYetEnded(static::ACTION_RECOGNIZE)) {
283
            $this->executionDelayed(static::SLEEP_RECOGNIZE);
284
            $response = $this->getResponse(static::ACTION_RECOGNIZE);
285
            $dataRecognize = $this->decodeResponse(static::DECODE_ACTION_RECOGNIZE, $response);
286
            if ($dataRecognize[static::DECODE_PARAM_RESPONSE] === static::RESPONSE_RECOGNIZE_OK && !empty($dataRecognize[static::DECODE_PARAM_CAPTCHA])) {
287
                $this->setParam(static::PARAM_SPEC_CAPTCHA, $dataRecognize[static::DECODE_PARAM_CAPTCHA]);
288
                $this->executionDelayed(static::SLEEP_BETWEEN);
289
290
                return true;
291
            } elseif ($dataRecognize[static::DECODE_PARAM_RESPONSE] === static::RESPONSE_RECOGNIZE_REPEAT) {
292
                continue;
293
            }
294
            throw new DeCaptchaErrors($dataRecognize[static::DECODE_PARAM_RESPONSE]);
295
        }
296
        throw new DeCaptchaErrors(DeCaptchaErrors::ERROR_LIMIT);
297
    }
298
299
    /**
300
     * @throws DeCaptchaErrors
301
     *
302
     * @return bool
303
     */
304 View Code Duplication
    protected function requestCode()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
305
    {
306
        while ($this->limitHasNotYetEnded(static::ACTION_UNIVERSAL_WITH_CAPTCHA)) {
307
            $this->executionDelayed(static::SLEEP_GET);
308
            $response = $this->getResponse(static::ACTION_UNIVERSAL_WITH_CAPTCHA);
309
            $dataGet = $this->decodeResponse(static::DECODE_ACTION_GET, $response);
310
            if ($dataGet[static::DECODE_PARAM_RESPONSE] === static::RESPONSE_GET_OK && !empty($dataGet[static::DECODE_PARAM_CODE])) {
311
                $this->setParam(static::PARAM_SPEC_CODE, $dataGet[static::DECODE_PARAM_CODE]);
312
313
                return true;
314
            } elseif ($dataGet[static::DECODE_PARAM_RESPONSE] === static::RESPONSE_GET_REPEAT) {
315
                continue;
316
            }
317
            throw new DeCaptchaErrors($dataGet[static::DECODE_PARAM_RESPONSE]);
318
        }
319
        throw new DeCaptchaErrors(DeCaptchaErrors::ERROR_LIMIT);
320
    }
321
322
    /**
323
     * @return \CURLFile|mixed|null|string
324
     */
325
    public function getCode()
326
    {
327
        return $this->getParamSpec(static::PARAM_SPEC_CODE);
328
    }
329
330
    /**
331
     * @return string
332
     */
333
    public function getError()
334
    {
335
        return $this->errorObject->getMessage();
336
    }
337
338
    /**
339
     * @param bool $causeAnError
340
     */
341
    public function setCauseAnError(bool $causeAnError) {
342
        $this->causeAnError = $causeAnError;
343
    }
344
}
345