leth / php-ipaddress Goto Github PK
View Code? Open in Web Editor NEWIP Address utility classes for PHP
License: Other
IP Address utility classes for PHP
License: Other
Any chance of creating a 1.0.2 tag that includes the fix to the math_biginteger dependency?
Your documentation claims that PEAR bigint library is optional, but your composer.json requires it. Even more, the version you require is not considered stable, so to install leth/ip-address
one needs to explicitly require an unstable version of bigint.
I wanted to submit a pull request making bigint a suggest
in composer.json, instead of a require
and simply throw exceptions if user tries to access features that require it.
But that would make your library not backwards-compatible with existing clients who have minimum-stability set to dev in their composer.json. Your new version would imply that bigint is no longer required, and if your users do use the functionality, it will suddenly begin throwing exceptions. Of course, clients can easily fix it by adding an explicit require for bigint.
Depending on your attitude towards backwards compatibility this may or may not be an option, what do you think?
It would be easer to get IPs array of network.
You require at least PHP 5.3 so why not use proper namespacing and add support for composer.
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
http://getcomposer.org/
This will allow people to easily install this and get updates.
I'd be happy to create a pull request for this :)
Trying to create an IP with extra zeros in ::factory("192.0.2.01") fails with "Unable to guess IP address type from '192.0.2.01'.".
While that is a somewhat unconventional format, I would still expect it to work.
I have seen some applications where textboxes for ip input will default to a format like 000.000.000.000.
The IP ::ffff:141.44.23.50
is categorized as IPv4 but it's IPv6.
It seems to be valid (based on http://www.helpsystems.com/intermapper/ipv6-address-validation).
I got the error report from podlove/podlove-publisher#600.
This is (roughly) how I use the library in my code:
<?php
use Leth\IPAddress\IP, Leth\IPAddress\IPv4, Leth\IPAddress\IPv6;
$ip = IP\Address::factory($_SERVER['REMOTE_ADDR']);
if (method_exists($ip, 'as_IPv6_address')) {
$ip = $ip->as_IPv6_address();
}
$ip_string = $ip->format(IP\Address::FORMAT_COMPACT);
I'm using version 1.0.1 (but the categorization method didn't change in 1.0.2 so I don't think it matters).
I have been looking for CIDR merge functionality for PHP, but apparently noone has it yet.
Python has a good library - https://github.com/drkjam/netaddr/blob/rel-0.7.x/netaddr/ip/__init__.py#L1558 that implements it, together with all the unit tests. If you have some spare time, please implement. Thanks!!! :)
Hi
There is a few issues that prevents proper support for php v 8.1. I have made a fork with minimal changes, but I'd be happy to contribute here. I would then suggest a v 2.0.0 as backwards comparability will be broken. Just writing this issue to test if there is activity in the project.
Compare these:
$foo = IP\NetworkAddress::factory("127.0.0.1");
$foo->get_cidr(); // 32
$foo->get_network_start(); // Null
$foo->get_network_end(); // Null
$bar = IP\NetworkAddress::factory("127.0.0.1/32");
$bar->get_cidr(); // 32
$bar->get_network_start(); // Not Null
$bar->get_network_start(); // Not Null
For consistent behavior - I think that the network start/end on a single IP should work the same as if it were loaded with a single address CIDR (i.e. 32 for IPv4).
I can work on the code for this, but I'm opening an issue for tracking.
Using the following IPv6 address for example
2001:0504:0000:0001:0000:0003:1898:0001
When returning the string representation with it returns this as the compressed address.
2001:504::1:0:3:1898:1
But it should actually be this without compressing the first 0000 hextet in the example. You should only be compressing 2 or more 0000s in a row. Handling One 16-Bit 0 Field - RFC-5952
2001:504:0:1:0:3:1898:1
I believe inet_ntop will return compressed ipv6 address string :)
For example
$ip = IPAddress::factory('2001:0504:0000:0001:0000:0003:1898:0001');
// 2001:504::1:0:3:1898:1
(string) $ip
// 2001:504:0:1:0:3:1898:1
inet_ntop(inet_pton((string) $ip))
When using this library for doing ip mathematics ive noticed some strange results.
<?php
error_reporting(E_ALL | E_STRICT);
function autoload($class_name) {
$file = str_replace('\\', DIRECTORY_SEPARATOR, $class_name);
$file = str_replace('_', DIRECTORY_SEPARATOR, $file).'.php';
$local_file = 'classes/'.$file;
if (file_exists($local_file))
include_once $local_file;
else
include_once $file;
}
spl_autoload_register('autoload');
use Leth\IPAddress\IP, Leth\IPAddress\IPv4, Leth\IPAddress\IPv6;
$ip_addr = 'fc00:1234::df';
$ip = IP\Address::factory($ip_addr);
$ip = $ip->add(1);
echo "IP: ".$ip."\n";
The expected result from this code should be fc00:1234::e0
, Yet it returns fc00:1234::
It would be nice to see this fixed as this library is quite helpful.
If you have a moment can you set up a repo webhook for packagist per https://packagist.org/about#how-to-update-packages ?
Not urgent, and I can manually update for now, but it helps guard against me forgetting which is a serious possibility at all times.
The only option on Packagist is dev-master. Would you consider tagging a release?
This is totally minor.
I'm wondering why you've separated the implementations into Impl classes?
I understand that pattern in Java where you would rather type check against the interface, and the style prefers not naming interfaces IWidget
or WidgetInterface
.
In PHP land, it's pretty common to name things WidgetInterface
and name classes what they are. But here, Impl is an abstract class and there is no interface.
So, just not clear what the extra classes provide.
@essjayhch points out that we are inconsistent in how we format addresses as strings: IPv4 addresses are abbreviated, whereas IPv6 addresses are not.
IPv4 Addresses have two forms, and IPv6 addresses have three:
127.0.0.1
127.000.000.001
::1
0:0:0:0:0:0:0:1
0000:0000:0000:0000:0000:0000:0000:0001
Firstly, we should be consistent in our output.
Secondly, the level of abbreviation should be configurable.
An input set of array '0.0.0.0/22', '0.0.0.0/24', '0.0.1.0/24', '0.0.2.0/24', '0.0.3.0/24' should result in '0.0.0.0/22', but instead emits the following error.
1) IP_NetworkAddress_Test::testMerge with data set #12 (array(Leth\IPAddress\IPv4\NetworkAddress Object (...), Leth\IPAddress\IPv4\NetworkAddress Object (...), Leth\IPAddress\IPv4\NetworkAddress Object (...), Leth\IPAddress\IPv4\NetworkAddress Object (...), Leth\IPAddress\IPv4\NetworkAddress Object (...)), array(Leth\IPAddress\IPv4\NetworkAddress Object (...)))
Error: Call to a member function compare_to() on null
/home/.../PHP-IPAddress/classes/Leth/IPAddress/IP/NetworkAddress.php:190
/home/.../PHP-IPAddress/tests/IPNetworkAddressTest.php:519
Patch and test case addition
dupe-merge.txt
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.