index_dev.php ➔ checkAllowedIp()   B
last analyzed

Complexity

Conditions 11
Paths 12

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 12
nop 1
dl 0
loc 26
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
function checkAllowedIp($remoteAddress)
6
{
7
    if(in_array($remoteAddress, array('127.0.0.1', 'fe80::1', '::1'), true)) {
8
        return true;
9
    }
10
    $matches = array();
11
    // http://en.wikipedia.org/wiki/Private_network
12
    if(preg_match('/([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/', $remoteAddress, $matches) === 1) {
13
        for($i=1;$i<5;$i++) {
14
            $matches[$i] = (int) $matches[$i];
15
        }
16
        // localhost
17
        if($matches[1] === 127) {
18
            return true;
19
        }
20
        if($matches[1] === 10) {
21
            return true;
22
        }
23
        if($matches[1] === 172 && $matches[2] >= 16 && $matches[2] <= 31) {
24
            return true;
25
        }
26
        if($matches[1] === 192 && $matches[2] === 168) {
27
            return true;
28
        }
29
    }
30
}
31
32
if (isset($_SERVER['HTTP_CLIENT_IP'])
33
    || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
34
    || !(checkAllowedIp($_SERVER['REMOTE_ADDR']) || php_sapi_name() === 'cli-server')
35
) {
36
    header('HTTP/1.0 403 Forbidden');
37
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
38
}
39
40
$loader = require_once __DIR__.'/../app/autoload.php';
41
42
$env = 'dev';
43
44
/** @var \Slim\App $app */
45
$app = require_once __DIR__ . '/../app/app.php';
46
47
$app->run();
48