Completed
Push — master ( 9d13eb...060bbf )
by Antonio Carlos
06:41
created

IpAddress::hostToIp()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 3
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace PragmaRX\Firewall\Support;
4
5
use PragmaRX\Support\IpAddress as SupportIpAddress;
6
7
class IpAddress
8
{
9
    use ServiceInstances;
10
11
    /**
12
     * Check if IP address is valid.
13
     *
14
     * @param $ip
15
     *
16
     * @return bool
17
     */
18
    public function isValid($ip)
19
    {
20
        $ip = $this->hostToIp($ip);
21
22
        try {
23
            return SupportIpAddress::ipV4Valid($ip) || $this->countries()->validCountry($ip);
24
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type PragmaRX\Firewall\Support\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
25
            return false;
26
        }
27
    }
28
29
    /**
30
     * Get the ip address of a host.
31
     *
32
     * @param $ip
33
     *
34
     * @return string
35
     */
36
    public function hostToIp($ip)
37
    {
38
        if (is_string($ip) && starts_with($ip, $string = 'host:')) {
39
            return gethostbyname(str_replace($string, '', $ip));
40
        }
41
42
        return $ip;
43
    }
44
45
    /**
46
     * Check if IP is in a valid range.
47
     *
48
     * @param $ip_address
49
     * @param $range
50
     *
51
     * @return bool
52
     */
53
    public function validRange($ip_address, $range)
54
    {
55
        return $this->config()->get('enable_range_search') &&
56
            SupportIpAddress::ipV4Valid($range->ip_address) &&
57
            SupportIpAddress::ipv4InRange($ip_address, $range->ip_address);
58
    }
59
60
    /**
61
     * Check if an ip v4 is valid.
62
     *
63
     * @param $item
64
     * @return bool
65
     */
66
    public function ipV4Valid($item)
67
    {
68
        return SupportIpAddress::ipV4Valid($item);
69
    }
70
71
    public function isCidr($country)
72
    {
73
        return SupportIpAddress::isCidr($country);
74
    }
75
}
76