Passed
Push — master ( ec2c00...5a182f )
by Владислав
46s queued 10s
created

RuCaptchaGeeTest::decodeResponse()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 22

Duplication

Lines 7
Ratio 31.82 %

Importance

Changes 0
Metric Value
dl 7
loc 22
rs 8.9457
c 0
b 0
f 0
cc 6
nc 3
nop 2
1
<?php
2
3
namespace jumper423\decaptcha\services;
4
5
use jumper423\decaptcha\core\DeCaptchaErrors;
6
7
/**
8
 * Class RuCaptchaGeeTest.
9
 */
10
class RuCaptchaGeeTest extends RuCaptcha
11
{
12
    public function init()
13
    {
14
        parent::init();
15
16
        unset(
17
            $this->paramsNames[static::ACTION_FIELD_FILE],
18
            $this->paramsNames[static::ACTION_FIELD_PHRASE],
19
            $this->paramsNames[static::ACTION_FIELD_REGSENSE],
20
            $this->paramsNames[static::ACTION_FIELD_NUMERIC],
21
            $this->paramsNames[static::ACTION_FIELD_MIN_LEN],
22
            $this->paramsNames[static::ACTION_FIELD_MAX_LEN],
23
            $this->paramsNames[static::ACTION_FIELD_LANGUAGE],
24
            $this->paramsNames[static::ACTION_FIELD_LANG],
25
            $this->paramsNames[static::ACTION_FIELD_QUESTION],
26
            $this->paramsNames[static::ACTION_FIELD_INSTRUCTIONS],
27
            $this->paramsNames[static::ACTION_FIELD_CALC],
28
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_FILE],
29
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_PHRASE],
30
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_REGSENSE],
31
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_NUMERIC],
32
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_MIN_LEN],
33
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_MAX_LEN],
34
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_LANGUAGE],
35
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_LANG],
36
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_QUESTION],
37
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_INSTRUCTIONS],
38
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_CALC]
39
        );
40
41
        $this->paramsNames[static::ACTION_FIELD_PAGEURL] = 'pageurl';
42
        $this->paramsNames[static::ACTION_FIELD_GT] = 'gt';
43
        $this->paramsNames[static::ACTION_FIELD_CHALLENGE] = 'challenge';
44
        $this->paramsNames[static::ACTION_FIELD_API_SERVER] = 'api_server';
45
46
        $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_METHOD][static::PARAM_SLUG_DEFAULT] = 'geetest';
47
        $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_PAGEURL] = [
48
            static::PARAM_SLUG_REQUIRE => true,
49
            static::PARAM_SLUG_TYPE    => static::PARAM_FIELD_TYPE_STRING,
50
        ];
51
        $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_GT] = [
52
            static::PARAM_SLUG_REQUIRE => true,
53
            static::PARAM_SLUG_TYPE    => static::PARAM_FIELD_TYPE_STRING,
54
        ];
55
        $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_CHALLENGE] = [
56
            static::PARAM_SLUG_REQUIRE => true,
57
            static::PARAM_SLUG_TYPE    => static::PARAM_FIELD_TYPE_STRING,
58
        ];
59
        $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_API_SERVER] = [
60
            static::PARAM_SLUG_REQUIRE => true,
61
            static::PARAM_SLUG_TYPE    => static::PARAM_FIELD_TYPE_STRING,
62
        ];
63
64
        $this->wiki->setText(['service', 'name'], [
65
            'ru' => 'RuCaptcha GeeTest',
66
            'en' => 'RuCaptcha GeeTest',
67
        ]);
68
        $this->wiki->setText(['recognize', 'price'], [
69
            'ru' => '1000 решений стоят 39 рублей.',
70
            'en' => '1000 for $0,7',
71
        ]);
72
        $this->wiki->setText(['recognize', 'desc'], [
73
            'ru' => '1) Найти следующие параметры на странице сайта с капчей (обычно их можно найти внутри функции initGeetest):
74
75
gt - публичный ключ сайта (статический)
76
challenge - динамический ключ задания
77
api_server - домен API (обязателен для некоторых сайтов)
78
79
2) Отправьте запрос 
80
81
3) Используйте значения, полученные в ответе в запросе к сайту, передавая их в соотстветствующих полях запроса:
82
83
geetest_challenge
84
geetest_validate
85
geetest_seccode',
86
            'en' => '1) Find the following parameters on the site page with captcha (they can usually be found inside the initGeetest function):
87
88
gt - site public key (static)
89
challenge - dynamic task key
90
api_server - API domain (required for some sites)
91
92
2) Submit a request
93
94
3) Use the values received in the response in the request to the site, passing them in the corresponding request fields:
95
96
geetest_challenge
97
geetest_validate
98
geetest_seccode',
99
        ]);
100
        $this->wiki->setText(['recognize', 'data'], [
101
            static::ACTION_FIELD_PAGEURL    => 'http://mysite.com/page/with/gettest/',
102
            static::ACTION_FIELD_GT         => 'f1ab2cdefa3456789012345b6c78d90e',
103
            static::ACTION_FIELD_CHALLENGE  => '12345678abc90123d45678ef90123a456b',
104
            static::ACTION_FIELD_API_SERVER => 'api-na.geetest.com',
105
        ]);
106
        $this->wiki->setText(['recognize', 'file'], false);
107
        $this->wiki->setText(['menu', 'from_service'], [
108
            RuCaptcha::class,
109
            RuCaptchaClick::class,
110
            RuCaptchaInstruction::class,
111
            RuCaptchaGrid::class,
112
            RuCaptchaReCaptcha::class,
113
            RuCaptchaFunCaptcha::class,
114
            RuCaptchaReCaptchaV3::class,
115
            RuCaptchaKeyCaptcha::class,
116
        ]);
117
    }
118
119
    public function recognize($additionally = [], $null = null)
120
    {
121
        return parent::recognize(null, $additionally);
122
    }
123
124
    /**
125
     * @param $action
126
     * @param $data
127
     *
128
     * @throws DeCaptchaErrors
129
     *
130
     * @return array
131
     */
132
    protected function decodeResponse($action, $data)
133
    {
134
        if (!array_key_exists($action, $this->decodeSettings[static::DECODE_ACTION])) {
135
            throw new DeCaptchaErrors('нет action');
136
        }
137
        $decodeSetting = $this->decodeSettings[static::DECODE_ACTION][$action];
138
        $values = [];
139
        if ($data === '{') {
140
            $values[static::DECODE_PARAM_RESPONSE] = 'OK';
141
            $values[static::DECODE_PARAM_CODE] = json_decode($data, true);
142
        } else {
143 View Code Duplication
            foreach (explode($decodeSetting[static::DECODE_SEPARATOR], $data) as $key => $value) {
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...
144
                foreach ($decodeSetting[static::DECODE_PARAMS] as $param => $paramSetting) {
145
                    if ($key === $paramSetting[static::DECODE_PARAM_SETTING_MARKER]) {
146
                        $values[$param] = $value;
147
                    }
148
                }
149
            }
150
        }
151
152
        return $values;
153
    }
154
}
155