Completed
Push — master ( 44753c...7d9c12 )
by Владислав
02:17
created

DeCaptchaBase::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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 = 1;
13
    const ACTION_UNIVERSAL = 2;
14
    const ACTION_UNIVERSAL_WITH_CAPTCHA = 3;
15
16
    const ACTION_FIELD_METHOD = 1;
17
    const ACTION_FIELD_KEY = 2;
18
    const ACTION_FIELD_FILE = 3;
19
    const ACTION_FIELD_PHRASE = 4;
20
    const ACTION_FIELD_REGSENSE = 5;
21
    const ACTION_FIELD_NUMERIC = 6;
22
    const ACTION_FIELD_MIN_LEN = 7;
23
    const ACTION_FIELD_MAX_LEN = 8;
24
    const ACTION_FIELD_LANGUAGE = 9;
25
    const ACTION_FIELD_SOFT_ID = 10;
26
    const ACTION_FIELD_CAPTCHA_ID = 11;
27
    const ACTION_FIELD_ACTION = 12;
28
    const ACTION_FIELD_QUESTION = 13;
29
    const ACTION_FIELD_CALC = 14;
30
    const ACTION_FIELD_HEADER_ACAO = 15;
31
    const ACTION_FIELD_INSTRUCTIONS = 16;
32
    const ACTION_FIELD_PINGBACK = 17;
33
    const ACTION_FIELD_TASK = 18;
34
    const ACTION_FIELD_IS_RUSSIAN = 19;
35
    const ACTION_FIELD_LABEL = 20;
36
    const ACTION_FIELD_GOOGLEKEY = 21;
37
    const ACTION_FIELD_RECAPTCHA = 22;
38
    const ACTION_FIELD_PROXY = 22;
39
    const ACTION_FIELD_PROXYTYPE = 23;
40
    const ACTION_FIELD_PAGEURL = 24;
41
    const ACTION_FIELD_GOOGLETOKEN = 25;
42
    const ACTION_FIELD_PROXYLOGIN = 26;
43
    const ACTION_FIELD_PROXYPASS = 27;
44
    const ACTION_FIELD_USERAGENT = 28;
45
    const ACTION_FIELD_COOKIES = 29;
46
    const ACTION_FIELD_PROXYPORT = 30;
47
    const ACTION_FIELD_COORDINATE = 31;
48
49
    const RESPONSE_RECOGNIZE_OK = 'OK';
50
    const RESPONSE_RECOGNIZE_REPEAT = 'ERROR_NO_SLOT_AVAILABLE';
51
    const RESPONSE_GET_OK = 'OK';
52
    const RESPONSE_GET_REPEAT = 'CAPCHA_NOT_READY';
53
54
    const SLEEP_RECOGNIZE = 5;
55
    const SLEEP_GET = 2;
56
    const SLEEP_BETWEEN = 5;
57
58
    const DECODE_ACTION_RECOGNIZE = 1;
59
    const DECODE_ACTION_GET = 2;
60
    const DECODE_ACTION_UNIVERSAL = 3;
61
62
    const DECODE_PARAM_RESPONSE = 1;
63
    const DECODE_PARAM_CAPTCHA = 2;
64
    const DECODE_PARAM_CODE = 3;
65
66
    public $actions = [
67
        self::ACTION_RECOGNIZE              => [],
68
        self::ACTION_UNIVERSAL              => [],
69
        self::ACTION_UNIVERSAL_WITH_CAPTCHA => [],
70
    ];
71
72
    protected $decodeSettings = [
73
        self::DECODE_FORMAT => self::RESPONSE_TYPE_STRING,
74
        self::DECODE_ACTION => [
75
            self::DECODE_ACTION_RECOGNIZE => [],
76
            self::DECODE_ACTION_GET       => [],
77
        ],
78
    ];
79
80
    protected $limitSettings = [
81
        self::ACTION_RECOGNIZE              => 3,
82
        self::ACTION_UNIVERSAL_WITH_CAPTCHA => 20,
83
    ];
84
85
    /**
86
     * @var DeCaptchaWiki
87
     */
88
    protected $wiki;
89
90
    /**
91
     * DeCaptchaBase constructor.
92
     *
93
     * @param $params
94
     */
95
    public function __construct($params)
96
    {
97
        $this->setParams($params);
98
        $this->wiki = new DeCaptchaWiki($this);
99
        $this->init();
100
    }
101
102
    public function init()
103
    {
104
    }
105
106
    /**
107
     * @param $filePath
108
     * @param array $additionally
109
     *
110
     * @throws DeCaptchaErrors
111
     *
112
     * @return bool
113
     */
114
    public function recognize($filePath, $additionally = [])
115
    {
116
        try {
117
            $this->resetLimits();
118
            $additionally[static::PARAM_SPEC_FILE] = $filePath;
119
            $this->setParams($additionally);
120
121
            return $this->requestRecognize() && $this->requestCode();
122
        } catch (DeCaptchaErrors $e) {
123
            if ($this->causeAnError) {
124
                throw $e;
125
            }
126
            $this->errorObject = $e;
127
128
            return false;
129
        }
130
    }
131
132
    /**
133
     * Запуск распознавания капчи.
134
     *
135
     * @deprecated
136
     *
137
     * @param string $filePath Путь до файла или ссылка на него
138
     *
139
     * @return bool
140
     */
141
    public function run($filePath)
142
    {
143
        return $this->recognize($filePath);
144
    }
145
146
    /**
147
     * Универсальная отправка повторяющихся запросов.
148
     *
149
     * @param int      $action
150
     * @param int      $decodeAction
151
     * @param int      $setParam
152
     * @param int      $decodeSerParam
153
     * @param int      $ok
154
     * @param int      $sleep
155
     * @param int      $repeat
156
     * @param int|null $error
157
     *
158
     * @throws DeCaptchaErrors
159
     *
160
     * @return bool
161
     */
162
    protected function requestRepeat($action, $decodeAction, $setParam, $decodeSerParam, $ok, $sleep, $repeat, $error = null)
163
    {
164
        if (is_null($error)) {
165
            $error = static::DECODE_PARAM_RESPONSE;
166
        }
167
        while ($this->limitHasNotYetEnded($action)) {
168
            $this->executionDelayed($sleep);
169
            $response = $this->getResponse($action);
170
            $dataRecognize = $this->decodeResponse($decodeAction, $response);
171
            if ($dataRecognize[static::DECODE_PARAM_RESPONSE] === $ok && !empty($dataRecognize[$decodeSerParam])) {
172
                $this->setParam($setParam, $dataRecognize[$decodeSerParam]);
173
                $this->executionDelayed(static::SLEEP_BETWEEN);
174
175
                return true;
176
            } elseif ($dataRecognize[static::DECODE_PARAM_RESPONSE] === $repeat) {
177
                continue;
178
            }
179
            throw new DeCaptchaErrors($dataRecognize[$error]);
180
        }
181
        throw new DeCaptchaErrors(DeCaptchaErrors::ERROR_LIMIT);
182
    }
183
184
    /**
185
     * Универсальная отправка.
186
     *
187
     * @param string $action
188
     *
189
     * @return array
190
     */
191
    protected function requestUniversal($action)
192
    {
193
        $this->setParam(static::ACTION_FIELD_ACTION, $action);
194
        $response = $this->getResponse(static::ACTION_UNIVERSAL);
195
196
        return $this->decodeResponse(static::DECODE_ACTION_UNIVERSAL, $response);
197
    }
198
199
    /**
200
     * @throws DeCaptchaErrors
201
     *
202
     * @return bool
203
     */
204
    protected function requestRecognize()
205
    {
206
        return $this->requestRepeat(static::ACTION_RECOGNIZE, static::DECODE_ACTION_RECOGNIZE, static::PARAM_SPEC_CAPTCHA, static::DECODE_PARAM_CAPTCHA, static::RESPONSE_RECOGNIZE_OK, static::SLEEP_RECOGNIZE, static::RESPONSE_RECOGNIZE_REPEAT);
207
    }
208
209
    /**
210
     * @throws DeCaptchaErrors
211
     *
212
     * @return bool
213
     */
214
    protected function requestCode()
215
    {
216
        return $this->requestRepeat(static::ACTION_UNIVERSAL_WITH_CAPTCHA, static::DECODE_ACTION_GET, static::PARAM_SPEC_CODE, static::DECODE_PARAM_CODE, static::RESPONSE_GET_OK, static::SLEEP_GET, static::RESPONSE_GET_REPEAT);
217
    }
218
219
    /**
220
     * @return \CURLFile|mixed|null|string
221
     */
222
    public function getCode()
223
    {
224
        return $this->getParamSpec(static::PARAM_SPEC_CODE);
225
    }
226
227
    /**
228
     * @return string
229
     */
230
    public function getError()
231
    {
232
        return $this->errorObject->getMessage();
233
    }
234
235
    /**
236
     * @param bool $causeAnError
237
     */
238
    public function setCauseAnError($causeAnError)
239
    {
240
        $this->causeAnError = $causeAnError;
241
    }
242
243
    public function getWiki($lang = 'en'){
244
        $this->wiki->setLang($lang);
245
        return $this->wiki;
246
    }
247
}
248