Passed
Push — master ( 7960d7...6271d2 )
by Cody
03:32
created

Textcha   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
c 0
b 0
f 0
dl 0
loc 64
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isCorrectAnswer() 0 3 1
A getQuestion() 0 13 3
1
<?php
2
3
namespace RssApp\Components;
4
5
class Textcha
6
{
7
    const LOCAL_QUESTIONS = [
8
        [
9
            'q' => 'Of the numbers seventy one, 4, fifty seven, 70, 49 or forty four, which is the highest?',
10
            'a' => [
11
                'e2c420d928d4bf8ce0ff2ec19b371514',
12
                '8456ebf0e76ac3f80fc5adc4dfb46e42'
13
            ]
14
        ],
15
        [
16
            'q' => 'In the number 4519096, what is the 4th digit?',
17
            'a' => [
18
                '45c48cce2e2d7fbdea1afc51c7c6ad26',
19
                'c785e1ed2950e3e36b1e2ca01f299a54'
20
            ]
21
        ],
22
        [
23
            'q' => 'Steven\'s name is?',
24
            'a' => [
25
                '6ed61d4b80bb0f81937b32418e98adca'
26
            ]
27
        ],
28
        [
29
            'q' => 'Which digit is 5th in the number 2993975?',
30
            'a' => [
31
                '45c48cce2e2d7fbdea1afc51c7c6ad26',
32
                'c785e1ed2950e3e36b1e2ca01f299a54'
33
            ]
34
        ],
35
        [
36
            'q' => 'Which is the highest number? Ninety four, 56 or seventy seven?',
37
            'a' => [
38
                'f4b9ec30ad9f68f89b29639786cb62ef',
39
                '96b0d8d3390fa62eb2f26db0bca9b2ef'
40
            ]
41
        ],
42
        [
43
            'q' => 'In the number 222457, what is the 6th digit?',
44
            'a' => [
45
                '8f14e45fceea167a5a36dedd4bea2543',
46
                'bb3aec0fdcdbc2974890f805c585d432'
47
            ]
48
        ]
49
    ];
50
51
    public static function getQuestion(): array
52
    {
53
        if (getenv('CAPTCHA_ALLOW_THIRD_PARTY') === 'true') {
54
            $question = json_decode(file_get_contents('http://api.textcaptcha.com/rssapp.json'), true);
55
        } else {
56
            $question = self::LOCAL_QUESTIONS[array_rand(self::LOCAL_QUESTIONS)];
57
        }
58
59
        foreach ($question['a'] as $index => $answer) {
60
            $question['a'][$index] = sha1(getenv('CAPTCHA_SALT').$answer);
61
        }
62
63
        return $question;
64
    }
65
66
    public static function isCorrectAnswer(string $answer, string $hash): bool
67
    {
68
        return (sha1(getenv('CAPTCHA_SALT').md5(strtolower(trim($answer)))) === $hash);
69
    }
70
}
71