Completed
Pull Request — master (#18)
by Tomas Norre
03:41
created

getIdentifierValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.5021

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 11
cp 0.5455
rs 9.9
c 0
b 0
f 0
cc 4
nc 3
nop 0
crap 5.5021
1
<?php
2
namespace Aoe\FeloginBruteforceProtection\Domain\Service;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2019 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use Aoe\FeloginBruteforceProtection\System\Configuration;
29
use Aoe\FeloginBruteforceProtection\Utility\CIDRUtility;
30
31
/**
32
 * @package Aoe\FeloginBruteforceProtection\Domain\Service
33
 * @author Patrick Roos <[email protected]>
34
 */
35
class RestrictionIdentifierClientIp extends RestrictionIdentifierAbstract
36
{
37
    /**
38
     * @var Configuration
39
     */
40
    protected $configuration;
41
42
    /**
43
     * the value of the restriction identifier
44
     * @return string
45
     */
46 16
    public function getIdentifierValue()
47
    {
48 16
        if (!isset($this->identifierValue)) {
49 16
            if ($this->configuration->getXForwardedFor() && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
50 4
                $this->identifierValue = $_SERVER['HTTP_X_FORWARDED_FOR'];
51
            } else {
52 12
                $this->identifierValue = $_SERVER['REMOTE_ADDR'];
53
            }
54
        }
55 16
        return $this->identifierValue;
56
    }
57
58
    /**
59
     * when IP is excluded no restriction check is necessary
60
     * @return boolean
61
     */
62 8
    public function checkPreconditions()
63
    {
64 8
        return !$this->isIpExcluded();
65
    }
66
67
    /**
68
     * checks if the IP is excluded for restriction
69
     * @return boolean
70
     */
71 8
    protected function isIpExcluded()
72
    {
73 8
        if (in_array($this->getIdentifierValue(), $this->configuration->getExcludedIps())) {
74 2
            return true;
75
        }
76 6
        foreach ($this->configuration->getExcludedIps() as $excludedIp) {
77
            // CIDR notation is used within excluded IPs
78 6
            if (CIDRUtility::isCIDR($excludedIp)) {
79 4
                if (CIDRUtility::matchCIDR($this->getIdentifierValue(), $excludedIp)) {
80 6
                    return true;
81
                }
82
            }
83
        }
84 4
        return false;
85
    }
86
87
    /**
88
     * @param Configuration $configuration
89
     * @return void
90
     **/
91 22
    public function setConfiguration(Configuration $configuration)
92
    {
93 22
        $this->configuration = $configuration;
94 22
    }
95
}
96