Issues (3)

source/NetworkComponentsByDanielGP.php (3 issues)

1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2015 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\network_components;
30
31
trait NetworkComponentsByDanielGP
32
{
33
34
    /**
35
     * Determines if a given IP is with a defined range
36
     *
37
     * @param ipv4 $ipGiven
38
     * @param ipv4 $ipStart
39
     * @param ipv4 $ipEnd
0 ignored issues
show
The type danielgp\network_components\ipv4 was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
     * @return string
41
     */
42
    public function checkIpIsInRange($ipGiven, $ipStart, $ipEnd)
43
    {
44
        $sReturn     = 'out';
45
        $startNo     = $this->convertIpToNumber($ipStart);
46
        $endNo       = $this->convertIpToNumber($ipEnd);
47
        $evaluatedNo = $this->convertIpToNumber($ipGiven);
48
        if ($sReturn == 'out') {
0 ignored issues
show
The condition $sReturn == 'out' is always true.
Loading history...
49
            if (($evaluatedNo >= $startNo) && ($evaluatedNo <= $endNo)) {
50
                $sReturn = 'in';
51
            }
52
        }
53
        return $sReturn;
54
    }
55
56
    /**
57
     * Checks if given IP is a private or public one
58
     *
59
     * @param ipv4 $ipGiven
60
     * @return string
61
     */
62
    public function checkIpIsPrivate($ipGiven)
63
    {
64
        if (filter_var($ipGiven, FILTER_VALIDATE_IP)) {
65
            if (!filter_var($ipGiven, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_NO_PRIV_RANGE)) {
66
                return 'private';
67
            }
68
            return 'public';
69
        }
70
        return 'invalid IP';
71
    }
72
73
    /**
74
     * Checks if given IP is a V4 or V6
75
     *
76
     * @param ipv4 $ipGiven
77
     * @return string
78
     */
79
    public function checkIpIsV4OrV6($ipGiven)
80
    {
81
        if (filter_var($ipGiven, FILTER_VALIDATE_IP)) {
82
            if (filter_var($ipGiven, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
83
                return 'V4';
84
            } elseif (filter_var($ipGiven, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
85
                return 'V6';
86
            }
87
        }
88
        return 'invalid IP';
89
    }
90
91
    /**
92
     * Converts IP to a number
93
     *
94
     * @param type $ipGiven
0 ignored issues
show
The type danielgp\network_components\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
95
     * @return string|int
96
     */
97
    public function convertIpToNumber($ipGiven)
98
    {
99
        if (filter_var($ipGiven, FILTER_VALIDATE_IP)) {
100
            if (filter_var($ipGiven, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
101
                $ips = explode('.', $ipGiven);
102
                return $ips[3] + $ips[2] * 256 + $ips[1] * 65536 + $ips[0] * 16777216;
103
            } elseif (filter_var($ipGiven, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
104
                return $this->convertIpV6ToNumber($ipGiven);
105
            }
106
        }
107
        return 'invalid IP';
108
    }
109
110
    private function convertIpV6ToNumber($ipGiven)
111
    {
112
        $binNum = '';
113
        foreach (unpack('C*', inet_pton($ipGiven)) as $byte) {
114
            $binNum .= str_pad(decbin($byte), 8, "0", STR_PAD_LEFT);
115
        }
116
        return base_convert(ltrim($binNum, '0'), 2, 10);
117
    }
118
119
    /**
120
     * Returns the IP of the client
121
     *
122
     * @return string
123
     */
124
    public function getClientRealIpAddress()
125
    {
126
        $rqst = new \Symfony\Component\HttpFoundation\Request();
127
        return $rqst->createFromGlobals()->getClientIp();
128
    }
129
}
130