Environment::isRemoteIpValid()   C
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 26
rs 6.7272
cc 7
eloc 18
nc 5
nop 0
1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2016 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Helper;
28
29
/**
30
 * Helper class for everything that has to do with the server environment
31
 */
32
class Environment extends \Payone\Core\Helper\Base
33
{
34
    /**
35
     * Get encoding
36
     *
37
     * @return string
38
     */
39
    public function getEncoding()
40
    {
41
        return 'UTF-8';
42
    }
43
44
    /**
45
     * Get the IP of the requesting client
46
     *
47
     * @return string
48
     */
49
    public function getRemoteIp()
50
    {
51
        return $this->_remoteAddress->getRemoteAddress();
52
    }
53
54
    /**
55
     * Validate if the user-ip-address is in the configured whitelist
56
     *
57
     * @return bool
58
     */
59
    public function isRemoteIpValid()
60
    {
61
        $sRemoteIp = $this->getRemoteIp();
62
        $sValidIps = $this->getConfigParam('valid_ips', 'processing', 'payone_misc');
63
        $aWhitelist = explode("\n", $sValidIps);
64
        $aWhitelist = array_filter(array_map('trim', $aWhitelist));
65
        if (array_search($sRemoteIp, $aWhitelist) !== false) {
66
            return true;
67
        }
68
        foreach ($aWhitelist as $sIP) {
69
            if (stripos($sIP, '*') !== false) {
70
                $sIP = str_replace(array("\r", "\n"), '', $sIP);
71
                $sDelimiter = '/';
72
73
                $sRegex = preg_quote($sIP, $sDelimiter);
74
                $sRegex = str_replace('\*', '\d{1,3}', $sRegex);
75
                $sRegex = $sDelimiter.'^'.$sRegex.'$'.$sDelimiter;
76
                
77
                preg_match($sRegex, $sRemoteIp, $aMatches);
78
                if (is_array($aMatches) && !empty($aMatches) && $aMatches[0] == $sRemoteIp) {
79
                    return true;
80
                }
81
            }
82
        }
83
        return false;
84
    }
85
}
86