1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JAAulde\IP\V4; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Represents an IP(V4) Address. |
7
|
|
|
* |
8
|
|
|
* @author Jim Auldridge <[email protected]> |
9
|
|
|
* @copyright 2006-2015 Jim Auldridge |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
class Address |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Used to request dot-notated string representation of IP from \JAAulde\IP\V4\Address::get. |
16
|
|
|
* |
17
|
|
|
* @const |
18
|
|
|
* |
19
|
|
|
* @see \JAAulde\IP\V4\Address::get |
20
|
|
|
*/ |
21
|
|
|
const FORMAT_DOTTED_NOTATION = 0; |
22
|
|
|
/** |
23
|
|
|
* Used to request integer representation of IP from \JAAulde\IP\V4\Address::get. |
24
|
|
|
* |
25
|
|
|
* @const |
26
|
|
|
* |
27
|
|
|
* @see \JAAulde\IP\V4\Address::get |
28
|
|
|
*/ |
29
|
|
|
const FORMAT_LONG_NOTATION = 1; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int The integer value of the address (generally produced by ip2long()) |
33
|
|
|
*/ |
34
|
|
|
protected $address; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor |
38
|
|
|
* |
39
|
|
|
* @param string|int $ip The IP address (in dot-notation-string format or integer) to be represented by the instance |
40
|
|
|
*/ |
41
|
|
|
public function __construct($ip) |
42
|
|
|
{ |
43
|
|
|
$this->setFromMixedSource($ip); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Format and validate for given string or integer formatted IPV4 network address. |
48
|
|
|
* |
49
|
|
|
* @param string|int $ip The IP address (in dot-notation-string format or integer) to be represented by the instance |
50
|
|
|
* |
51
|
|
|
* @throws Exception |
52
|
|
|
*/ |
53
|
|
|
protected function setFromMixedSource($ip) |
54
|
|
|
{ |
55
|
|
|
if (is_int($ip) || is_string($ip)) { |
56
|
|
|
if (is_int($ip)) { |
57
|
|
|
/* |
58
|
|
|
* Convert int to dotted IP string |
59
|
|
|
*/ |
60
|
|
|
$ip = long2ip($ip); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/* |
64
|
|
|
* We might have been given a string, or we may have converted to string. Attempt to make it an int. |
65
|
|
|
*/ |
66
|
|
|
if (is_string($ip)) { |
67
|
|
|
$ip = ip2long($ip); |
68
|
|
|
} |
69
|
|
|
} else { |
70
|
|
|
$ip = false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/* |
74
|
|
|
* If given an improper data type, we set the $ip to false. |
75
|
|
|
* Also, the conversion through ip2long() could result in it becoming false. |
76
|
|
|
* Either way, we want to bail out. |
77
|
|
|
*/ |
78
|
|
|
if ($ip === false) { |
79
|
|
|
throw new \Exception(__METHOD__.' requires valid IPV4 address string in dot-notation (aaa.bbb.ccc.ddd).'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->set($ip); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set the value of the address to the integer representation (compensating for addresses which converted to negative on 32bit systems). |
87
|
|
|
* |
88
|
|
|
* @param integer $address |
89
|
|
|
*/ |
90
|
|
|
protected function set($address) |
91
|
|
|
{ |
92
|
|
|
/* |
93
|
|
|
PHP notes that some IP conversions will result in negative numbers on 32Bit architectures ( http://php.net/manual/en/function.ip2long.php#refsect1-function.ip2long-notes ) |
94
|
|
|
We're accounting for this. See note at http://php.net/manual/en/function.ip2long.php#88345 about "Convert IP to unsigned long" |
95
|
|
|
*/ |
96
|
|
|
$this->address = (int) $address + ((int) $address < 0 ? 4294967296 : 0); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the integer or dot-notated-string representation of the address. |
101
|
|
|
* |
102
|
|
|
* @param int $format Whether to return as integer (\JAAulde\IP\V4\Address::FORMAT_LONG_NOTATION) or dot-notation-string (\JAAulde\IP\V4\Address::FORMAT_DOTTED_NOTATION) |
103
|
|
|
* |
104
|
|
|
* @return string|int |
105
|
|
|
*/ |
106
|
|
|
public function get($format = self::FORMAT_LONG_NOTATION) |
107
|
|
|
{ |
108
|
|
|
return $format === self::FORMAT_DOTTED_NOTATION ? long2ip($this->address) : $this->address; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Output dotted notation on conversion to string. |
113
|
|
|
* |
114
|
|
|
* @uses \JAAulde\IP\V4\Address::get |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function __toString() |
119
|
|
|
{ |
120
|
|
|
return $this->get(self::FORMAT_DOTTED_NOTATION); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|