Gregwar   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 10
dl 0
loc 44
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isFull() 0 3 1
A get() 0 3 1
A validate() 0 17 5
1
<?php
2
3
namespace Extend\Core\Captcha;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Exception\SyntaxException;
7
use Ffcms\Core\Helper\FileSystem\File;
8
use Ffcms\Core\Helper\Type\Str;
9
use Ffcms\Core\Interfaces\iCaptcha;
10
11
class Gregwar implements iCaptcha
12
{
13
14
    /**
15
     * Check is captcha provide 'full-based' output realisation
16
     * @return bool
17
     */
18
    public function isFull()
19
    {
20
        return false;
21
    }
22
23
    /**
24
     * Get captcha image link(isFull():=false) or builded JS code(isFull():=true)
25
     * @return string
26
     */
27
    public function get()
28
    {
29
        return App::$Alias->scriptUrl . '/api/captcha/gregwar?time=' . microtime(true) . '&lang=' . App::$Request->getLanguage();
30
    }
31
32
    /**
33
     * Validate input data from captcha
34
     * @param string|null $data
35
     * @return bool
36
     * @throws SyntaxException
37
     */
38
    public static function validate($data = null)
39
    {
40
        // check if test suite is enabled and test going on
41
        if (App::$Properties->get('testSuite') === true && App::$Request->getClientIp() === '127.0.0.1') {
42
            // captcha value should be equal to config file md5 sum :)
43
            return $data === File::getMd5('/Private/Config/Default.php');
44
        }
45
        // allow to validate captcha by codeception tests
46
        $captchaValue = App::$Session->get('captcha');
47
        // unset session value to prevent duplication. Security fix.
48
        App::$Session->remove('captcha');
49
        // check if session has value
50
        if ($captchaValue === null || Str::length($captchaValue) < 1) {
51
            return false;
52
        }
53
54
        return $data === $captchaValue;
55
    }
56
}
57