Completed
Pull Request — master (#189)
by Mark
12:10
created

IpDetector::check()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
3
/*
4
 * This file is part of Crawler Detect - the web crawler detection library.
5
 *
6
 * (c) Mark Beech <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Jaybizzle\CrawlerDetect\Detectors;
13
14
use Jaybizzle\CrawlerDetect\Fixtures\Ips;
15
16
class IpDetector
17
{
18
    public function __construct($userIp)
19
    {
20
        $this->userIp = $userIp;
0 ignored issues
show
Bug introduced by
The property userIp does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
    }
22
23
    public function check()
24
    {
25
        $ips = new Ips();
26
27
        foreach ($ips->getAll() as $ip) {
28
            if ($this->ipInRange($this->userIp, $ip) === true) {
29
                return true;
30
            }
31
        }
32
33
        return false;
34
    }
35
36
    public function ipInRange($ip, $range)
37
    {
38
        if (strpos($range, '/') !== false) {
39
            // $range is in IP/NETMASK format
40
            list($range, $netmask) = explode('/', $range, 2);
41
42
            if (strpos($netmask, '.') !== false) {
43
                // $netmask is a 255.255.0.0 format
44
                $netmask = str_replace('*', '0', $netmask);
45
                $netmask_dec = ip2long($netmask);
46
47
                return ((ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec));
48
            } else {
49
                // $netmask is a CIDR size block
50
                // fix the range argument
51
                $x = explode('.', $range);
52
                while (count($x) < 4) {
53
                    $x[] = '0';
54
                }
55
                list($a, $b, $c, $d) = $x;
56
                $range = sprintf('%u.%u.%u.%u', empty($a) ? '0' : $a, empty($b) ? '0' : $b, empty($c) ? '0' : $c, empty($d) ? '0' : $d);
57
                $range_dec = ip2long($range);
58
                $ip_dec = ip2long($ip);
59
60
          # Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s
61
          #$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
63
          # Strategy 2 - Use math to create it
64
          $wildcard_dec = pow(2, (32 - $netmask)) - 1;
65
                $netmask_dec = ~$wildcard_dec;
66
67
                return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec));
68
            }
69
        } else {
70
            // range might be 255.255.*.* or 1.2.3.0-1.2.3.255
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
        if (strpos($range, '*') !== false) { // a.b.*.* format
72
          // Just convert to A-B format by setting * to 0 for A and 255 for B
73
          $lower = str_replace('*', '0', $range);
74
            $upper = str_replace('*', '255', $range);
75
            $range = "$lower-$upper";
76
        }
77
78
            if (strpos($range, '-') !== false) { // A-B format
79
          list($lower, $upper) = explode('-', $range, 2);
80
                $lower_dec = (float) sprintf('%u', ip2long($lower));
81
                $upper_dec = (float) sprintf('%u', ip2long($upper));
82
                $ip_dec = (float) sprintf('%u', ip2long($ip));
83
84
                return (($ip_dec >= $lower_dec) && ($ip_dec <= $upper_dec));
85
            }
86
87
            echo 'Range argument is not in 1.2.3.4/24 or 1.2.3.4/255.255.255.0 format';
88
89
            return false;
90
        }
91
    }
92
93
    // decbin32
94
    // In order to simplify working with IP addresses (in binary) and their
95
    // netmasks, it is easier to ensure that the binary strings are padded
96
    // with zeros out to 32 characters - IP addresses are 32 bit numbers
97
    public function decbin32($dec)
98
    {
99
        return str_pad(decbin($dec), 32, '0', STR_PAD_LEFT);
100
    }
101
}
102