I need to check if a client IP is withing an IP range. Does Concrete CMS v9 have its extended class for Symfony\Component\HttpFoundation\IpUtils? I need to use the Symfony\Component\HttpFoundation\IpUtils::checkIp().
Concrete comes with ip-lib, which offers a great set of ip-related functions
and where can I find it?
Take a look in concrete/vendor/mlocati/ip-lib/
You can find the documentation here: https://github.com/mlocati/ip-lib#readme
PS: IP addresses are represented by instances of IPLib\Address\AddressInterface
, which is an interface that’s implemented by the IPLib\Address\IPv4
class for IPv4 addresses (eg 127.0.0.1
) and by IPLib\Address\IPv6
for IPv6 addresses (eg ::1
).
In Concrete, you can get the visitors’ IP address as an instance of IPLib\Address\AddressInterface
by simply calling
$ip = $app->make(\IPLib\Address\AddressInterface::class);
Oh, that looks more complicated for my needs. I just do this:
$app = Application::getFacadeApplication();
$r = $app->make(Request::class);
$ip = $r->getClientIp();
if (!IpUtils::checkIp($ip, '192.168.1.0/23')) {
exit;
}
By using ip-lib, you’d write something like this:
use IPLib\Address\AddressInterface;
use IPLib\Factory;
$ip = app(AddressInterface::class);
$range = Factory::parseRangeString('192.168.1.0/23');
if (!$range->contains($ip)) {
exit;
}
Wow, now we have the app()
Is it now official and documented?
Thank you, Michele, your lib looks cool! Although I can’t understand how you get the request IP from the interface.
GitHub - shahroq/whale_c5_cheat_sheet: concrete5 Cheat Sheet offers a ton of useful hints
Concrete does the hard work for you, so that you simply have to call app(AddressInterface::class)
And
$ip = app(AddressInterface::class);
returns a class (IPv4
or IPv6
), that implements the AddressInterface
interface.
Does the app() work everywhere or only in instantiated classes having the Application, i.e. only where you can use $this-app?
Is there a list of everything the app() can create with short notations, e.g. app(‘token’)?
Nope: you should take a look at the [Something]ServiceProvider.php
files in the concrete/src
directory
For example:
concrete/src/Validation/ValidationServiceProvider.php
defines'token'
concrete/src/Permission/PermissionServiceProvider.php
defines'IPLib\Address\AddressInterface'
i want to find…so can you tell mee how can i find…