Code Duplication    Length = 117-117 lines in 2 locations

lib/Payone/SessionStatus/Validator/Ip.php 1 location

@@ 33-149 (lines=117) @@
30
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
31
 * @link            http://www.noovias.com
32
 */
33
class Payone_SessionStatus_Validator_Ip
34
    extends Payone_SessionStatus_Validator_Abstract
35
{
36
    /** @var array */
37
    protected $validIps = array();
38
39
    /** @var Payone_SessionStatus_Config */
40
    protected $config = null;
41
42
    /**
43
     * @param Payone_SessionStatus_Request_Interface $request
44
     * @throws Payone_SessionStatus_Exception_Validation
45
     * @return bool
46
     */
47
    public function validateRequest(Payone_SessionStatus_Request_Interface $request)
48
    {
49
        $remoteAddress = $this->getRemoteAddress();
50
        $validIps = $this->getValidIps();
51
52
        if (in_array($remoteAddress, $validIps)) {
53
            // this is for exact matches
54
            return true;
55
        }
56
57
        foreach ($validIps as $ip) {
58
            $ip = $this->checkForDelimiter($ip);
59
            if (preg_match($ip, $remoteAddress)) {
60
                return true;
61
            }
62
        }
63
64
        throw new Payone_SessionStatus_Exception_Validation();
65
    }
66
67
    /**
68
     * Check if IP-String has delimiter, because preg_match needs string-delimiter
69
     * @param $ip
70
     * @return string
71
     */
72
    protected function checkForDelimiter($ip)
73
    {
74
        if (substr($ip, 0, 1) !== '/') {
75
            $ip = '/' . $ip;
76
        }
77
78
        if (substr($ip, -1, 1) !== '/') {
79
            $ip = $ip . '/';
80
        }
81
82
        return $ip;
83
    }
84
85
    /**
86
     * @param array $validIps
87
     */
88
    public function setValidIps(array $validIps)
89
    {
90
        $this->validIps = $validIps;
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function getValidIps()
97
    {
98
        return $this->validIps;
99
    }
100
101
    /**
102
     * Checks if ProxyCheck should be used. Returns the Remote-IP
103
     *
104
     * @return string
105
     */
106
    public function getRemoteAddress()
107
    {
108
        $remoteAddr = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_STRING);
109
        if ($this->getProxyCheckEnabled() == 1) {
110
            $proxy = filter_input(INPUT_SERVER, 'HTTP_X_FORWARDED_FOR', FILTER_SANITIZE_STRING);
111
            if(!empty($proxy)) {
112
                $proxyIps = explode(',', $proxy);
113
                $relevantIp = array_shift($proxyIps);
114
                $relevantIp = trim($relevantIp);
115
                if (!empty($relevantIp)) {
116
                    return $relevantIp;
117
                }
118
            }
119
        }
120
121
        return $remoteAddr;
122
123
    }
124
125
    /**
126
     * @return boolean
127
     */
128
    public function getProxyCheckEnabled()
129
    {
130
        return $this->getConfig()->getValue('validator/proxy/enabled');
131
    }
132
133
    /**
134
     * @param Payone_SessionStatus_Config $config
135
     */
136
    public function setConfig($config)
137
    {
138
        $this->config = $config;
139
    }
140
141
    /**
142
     * @return Payone_SessionStatus_Config
143
     */
144
    public function getConfig()
145
    {
146
        return $this->config;
147
    }
148
149
}

lib/Payone/TransactionStatus/Validator/Ip.php 1 location

@@ 33-149 (lines=117) @@
30
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
31
 * @link            http://www.noovias.com
32
 */
33
class Payone_TransactionStatus_Validator_Ip
34
    extends Payone_TransactionStatus_Validator_Abstract
35
{
36
    /** @var array */
37
    protected $validIps = array();
38
39
    /** @var Payone_TransactionStatus_Config */
40
    protected $config = null;
41
42
    /**
43
     * @param Payone_TransactionStatus_Request_Interface $request
44
     * @throws Payone_TransactionStatus_Exception_Validation
45
     * @return bool
46
     */
47
    public function validateRequest(Payone_TransactionStatus_Request_Interface $request)
48
    {
49
        $remoteAddress = $this->getRemoteAddress();
50
        $validIps = $this->getValidIps();
51
52
        if (in_array($remoteAddress, $validIps)) {
53
            // this is for exact matches
54
            return true;
55
        }
56
57
        foreach ($validIps as $ip) {
58
            $ip = $this->checkForDelimiter($ip);
59
            if (preg_match($ip, $remoteAddress)) {
60
                return true;
61
            }
62
        }
63
64
        throw new Payone_TransactionStatus_Exception_Validation();
65
    }
66
67
    /**
68
     * Check if IP-String has delimiter, because preg_match needs string-delimiter
69
     * @param $ip
70
     * @return string
71
     */
72
    protected function checkForDelimiter($ip)
73
    {
74
        if (substr($ip, 0, 1) !== '/') {
75
            $ip = '/' . $ip;
76
        }
77
78
        if (substr($ip, -1, 1) !== '/') {
79
            $ip = $ip . '/';
80
        }
81
82
        return $ip;
83
    }
84
85
    /**
86
     * @param array $validIps
87
     */
88
    public function setValidIps(array $validIps)
89
    {
90
        $this->validIps = $validIps;
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function getValidIps()
97
    {
98
        return $this->validIps;
99
    }
100
101
    /**
102
     * Checks if ProxyCheck should be used. Returns the Remote-IP
103
     *
104
     * @return string
105
     */
106
    public function getRemoteAddress()
107
    {
108
        $remoteAddr = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_STRING);
109
        if ($this->getProxyCheckEnabled() == 1) {
110
            $proxy = filter_input(INPUT_SERVER, 'HTTP_X_FORWARDED_FOR', FILTER_SANITIZE_STRING);
111
            if(!empty($proxy)) {
112
                $proxyIps = explode(',', $proxy);
113
                $relevantIp = array_shift($proxyIps);
114
                $relevantIp = trim($relevantIp);
115
                if (!empty($relevantIp)) {
116
                    return $relevantIp;
117
                }
118
            }
119
        }
120
121
        return $remoteAddr;
122
123
    }
124
125
    /**
126
     * @return boolean
127
     */
128
    public function getProxyCheckEnabled()
129
    {
130
        return $this->getConfig()->getValue('validator/proxy/enabled');
131
    }
132
133
    /**
134
     * @param Payone_TransactionStatus_Config $config
135
     */
136
    public function setConfig($config)
137
    {
138
        $this->config = $config;
139
    }
140
141
    /**
142
     * @return Payone_TransactionStatus_Config
143
     */
144
    public function getConfig()
145
    {
146
        return $this->config;
147
    }
148
149
}