Completed
Push — master ( f3c44f...e63783 )
by Владислав
02:12
created

DeCaptchaBase::decodeResponse()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 16
nc 5
nop 2
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
    const PARAM_FIELD_CAPTCHA_ID = 10;
25
    const PARAM_FIELD_ACTION = 11;
26
27
    const ACTION_RECOGNIZE = 0;
28
    const ACTION_UNIVERSAL = 1;
29
    const ACTION_UNIVERSAL_WITH_CAPTCHA = 2;
30
31
    protected $paramsNames = [
32
        self::PARAM_FIELD_METHOD     => 'method',
33
        self::PARAM_FIELD_KEY        => 'key',
34
        self::PARAM_FIELD_FILE       => 'file',
35
        self::PARAM_FIELD_PHRASE     => 'phrase',
36
        self::PARAM_FIELD_REGSENSE   => 'regsense',
37
        self::PARAM_FIELD_NUMERIC    => 'numeric',
38
        self::PARAM_FIELD_MIN_LEN    => 'min_len',
39
        self::PARAM_FIELD_MAX_LEN    => 'max_len',
40
        self::PARAM_FIELD_LANGUAGE   => 'language',
41
        self::PARAM_FIELD_SOFT_ID    => 'soft_id',
42
        self::PARAM_FIELD_CAPTCHA_ID => 'id',
43
        self::PARAM_FIELD_ACTION     => 'action',
44
    ];
45
46
    protected $paramsSettings = [
47
        self::ACTION_RECOGNIZE => [
48
            self::PARAM_FIELD_METHOD => [
49
                self::PARAM_SLUG_DEFAULT => 'post',
50
                self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
51
            ],
52
            self::PARAM_FIELD_KEY => [
53
                self::PARAM_SLUG_REQUIRE => true,
54
                self::PARAM_SLUG_SPEC    => self::PARAM_SPEC_KEY,
55
                self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
56
            ],
57
            self::PARAM_FIELD_FILE => [
58
                self::PARAM_SLUG_REQUIRE => true,
59
                self::PARAM_SLUG_SPEC    => self::PARAM_SPEC_FILE,
60
                self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
61
            ],
62
            self::PARAM_FIELD_PHRASE => [
63
                self::PARAM_SLUG_DEFAULT => 0,
64
                self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
65
            ],
66
            self::PARAM_FIELD_REGSENSE => [
67
                self::PARAM_SLUG_DEFAULT => 0,
68
                self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
69
            ],
70
            self::PARAM_FIELD_NUMERIC => [
71
                self::PARAM_SLUG_DEFAULT => 0,
72
                self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
73
            ],
74
            self::PARAM_FIELD_MIN_LEN => [
75
                self::PARAM_SLUG_DEFAULT => 0,
76
                self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
77
            ],
78
            self::PARAM_FIELD_MAX_LEN => [
79
                self::PARAM_SLUG_DEFAULT => 0,
80
                self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
81
            ],
82
            self::PARAM_FIELD_LANGUAGE => [
83
                self::PARAM_SLUG_DEFAULT => 0,
84
                self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
85
            ],
86
            self::PARAM_FIELD_SOFT_ID => [
87
                self::PARAM_SLUG_VARIABLE => false,
88
                self::PARAM_SLUG_TYPE     => self::PARAM_FIELD_TYPE_INTEGER,
89
            ],
90
        ],
91
        self::ACTION_UNIVERSAL => [
92
            self::PARAM_FIELD_KEY => [
93
                self::PARAM_SLUG_REQUIRE => true,
94
                self::PARAM_SLUG_SPEC    => self::PARAM_SPEC_KEY,
95
                self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
96
            ],
97
            self::PARAM_FIELD_ACTION => [
98
                self::PARAM_SLUG_REQUIRE => true,
99
                self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
100
            ],
101
        ],
102
        self::ACTION_UNIVERSAL_WITH_CAPTCHA => [
103
            self::PARAM_FIELD_KEY => [
104
                self::PARAM_SLUG_REQUIRE => true,
105
                self::PARAM_SLUG_SPEC    => self::PARAM_SPEC_KEY,
106
                self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
107
            ],
108
            self::PARAM_FIELD_CAPTCHA_ID => [
109
                self::PARAM_SLUG_REQUIRE => true,
110
                self::PARAM_SLUG_SPEC    => self::PARAM_SPEC_CAPTCHA,
111
                self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
112
            ],
113
            self::PARAM_FIELD_ACTION => [
114
                self::PARAM_SLUG_REQUIRE => true,
115
                self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
116
            ],
117
        ],
118
    ];
119
120
    public function recognize($filePath)
121
    {
122
        try {
123
            $this->setParamSpec(static::PARAM_SPEC_FILE, $this->getFilePath($filePath));
124
            $result = $this->getCurlResponse($this->getInUrl(), $this->getParams(static::ACTION_RECOGNIZE));
125
            $this->setError($result);
0 ignored issues
show
Bug introduced by
The method setError() does not exist on jumper423\decaptcha\core\DeCaptchaBase. Did you maybe mean setErrorLang()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
126
            list(, $this->captchaId) = explode('|', $result);
127
            $waitTime = 0;
128
            sleep($this->requestTimeout);
0 ignored issues
show
Bug introduced by
The property requestTimeout does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
129 View Code Duplication
            while (true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
130
                $result = $this->getResponse('get');
131
                $this->setError($result);
0 ignored issues
show
Bug introduced by
The method setError() does not exist on jumper423\decaptcha\core\DeCaptchaBase. Did you maybe mean setErrorLang()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
132
                if ($result == 'CAPCHA_NOT_READY') {
133
                    $waitTime += $this->requestTimeout;
134
                    if ($waitTime > $this->maxTimeout) {
0 ignored issues
show
Bug introduced by
The property maxTimeout does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
135
                        break;
136
                    }
137
                    sleep($this->requestTimeout);
138
                } else {
139
                    $ex = explode('|', $result);
140
                    if (trim($ex[0]) == 'OK') {
141
                        $this->result = trim($ex[1]);
0 ignored issues
show
Bug introduced by
The property result does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
142
143
                        return true;
144
                    }
145
                }
146
            }
147
            throw new Exception('Лимит времени превышен');
148
        } catch (Exception $e) {
149
            $this->error = $e->getMessage();
0 ignored issues
show
Bug introduced by
The property error does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
150
151
            return false;
152
        }
153
    }
154
155
    public function getCode()
156
    {
157
    }
158
159
    public function getError()
160
    {
161
    }
162
163
    /**
164
     * Запуск распознавания капчи.
165
     *
166
     * @deprecated
167
     *
168
     * @param string $filePath Путь до файла или ссылка на него
169
     *
170
     * @return bool
171
     */
172
    public function run($filePath)
173
    {
174
        return $this->recognize($filePath);
175
    }
176
177
    /**
178
     * Не верно распознана.
179
     */
180
    public function notTrue()
181
    {
182
        $this->getResponse('reportbad');
183
    }
184
185
    const DECODE_FORMAT = 0;
186
    const DECODE_ACTION = 1;
187
    const DECODE_SEPARATOR = 2;
188
    const DECODE_PARAMS = 3;
189
    const DECODE_PARAM_SETTING_MARKER = 4;
190
191
    const DECODE_ACTION_RECOGNIZE = 0;
192
193
    const DECODE_PARAM_RESPONSE = 0;
194
    const DECODE_PARAM_CODE = 1;
195
196
    protected $decodeSettings = [
197
        self::DECODE_FORMAT => self::RESPONSE_TYPE_STRING,
198
        self::DECODE_ACTION => [
199
            self::DECODE_ACTION_RECOGNIZE => [
200
                self::DECODE_SEPARATOR => '|',
201
                self::DECODE_PARAMS => [
202
                    self::DECODE_PARAM_RESPONSE => [
203
                        self::DECODE_PARAM_SETTING_MARKER => 0,
204
                    ],
205
                    self::DECODE_PARAM_CODE => [
206
                        self::DECODE_PARAM_SETTING_MARKER => 1,
207
                    ],
208
                ],
209
            ],
210
        ],
211
    ];
212
213
    protected function decodeResponse($action, $data)
214
    {
215
        if (!array_key_exists($action, $this->decodeSettings[static::DECODE_ACTION])) {
216
            throw new DeCaptchaErrors('нет action');
217
        }
218
        $decodeSetting = $this->decodeSettings[static::DECODE_ACTION][$action];
219
        $decodeFormat = array_key_exists(static::DECODE_FORMAT, $decodeSetting) ?
220
            $decodeSetting[static::DECODE_FORMAT] :
221
            $this->decodeSettings[static::DECODE_FORMAT];
222
        $values = [];
223
        switch ($decodeFormat) {
224
            case static::RESPONSE_TYPE_STRING:
225
                foreach (explode($decodeFormat[static::DECODE_SEPARATOR], $data) as $key => $value) {
226
                    foreach ($decodeFormat[static::DECODE_PARAMS] as $param => $paramKey) {
227
                        if ($key === $paramKey) {
228
                            $values[$param] = $value;
229
                        }
230
                    }
231
                }
232
                break;
233
        }
234
        return $values;
235
    }
236
}
237