-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dont crash on geoip and display flags where appropreate
- Loading branch information
1 parent
65fa920
commit 921f308
Showing
7 changed files
with
228 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
gpslab_geoip: | ||
license: '%env(MAXMIND_LICENCE)%' | ||
edition: 'GeoLite2-Country' | ||
path: '%kernel.project_dir%/var/data/GeoLite2-Country.mmdb' | ||
databases: | ||
default: | ||
license: '%env(MAXMIND_LICENCE)%' | ||
edition: 'GeoLite2-Country' | ||
path: '%kernel.project_dir%/var/data/GeoLite2-Country.mmdb' | ||
locales: ['en'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
namespace App\Twig; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
|
||
use Ante\DnsParser\Dns; | ||
use GeoIp2\Database\Reader; | ||
|
||
class Domain2Ip extends AbstractExtension | ||
{ | ||
private $reader; | ||
|
||
public function __construct(Reader $reader) | ||
{ | ||
$this->reader = $reader; | ||
} | ||
|
||
public function getFilters(): array | ||
{ | ||
return [ | ||
new TwigFilter('domain2ip', [$this, 'domain2ipFilter']), | ||
]; | ||
} | ||
|
||
public function domain2ipFilter($domain) | ||
{ | ||
if (empty($domain)) { | ||
return ''; | ||
} | ||
$email = null; | ||
if (is_array($domain)) { | ||
$email = $domain[1]; | ||
$domain = $domain[0]; | ||
} | ||
|
||
$dns = new Dns(); | ||
try { | ||
$ipv4 = $dns->getRecords($domain, 'A')[0]->toArray()["ip"]; | ||
} catch (\Exception $e) { | ||
$ipv4 = ''; | ||
} | ||
try { | ||
$ipv6 = $dns->getRecords($domain, 'AAAA')[0]->toArray()["ipv6"]; | ||
} catch (\Exception $e) { | ||
$ipv6 = ''; | ||
} | ||
|
||
if(!empty($ipv6)) { | ||
$ip = $ipv6; | ||
} else { | ||
$ip = $ipv4; | ||
} | ||
|
||
if (!is_null($email)) { | ||
return array($ip, $email); | ||
} else { | ||
return array($ip, $domain); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
namespace App\Twig; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
|
||
class Email2Domain extends AbstractExtension | ||
{ | ||
public function getFilters(): array | ||
{ | ||
return [ | ||
new TwigFilter('email2domain', [$this, 'email2domainFilter']), | ||
]; | ||
} | ||
|
||
public function email2domainFilter($email) | ||
{ | ||
if (empty($email)) { | ||
return ''; | ||
} | ||
preg_match('/(\S*)?@(\S*)/', $email, $match); | ||
$domain = $match[2]; | ||
return array($domain, $email); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
namespace App\Twig; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
use GpsLab\Bundle\GeoIP2Bundle\Reader\ReaderFactory; | ||
use GeoIp2\Database\Reader; | ||
|
||
class GeoIp extends AbstractExtension | ||
{ | ||
private $reader; | ||
private $cityDatabasePath = __DIR__ . '/../../var/GeoLite2/GeoLite2-City.mmdb'; | ||
private $countryDatabasePath = __DIR__ . '/../../var/GeoLite2/GeoLite2-Country.mmdb'; | ||
|
||
|
||
public function __construct(Reader $reader) | ||
{ | ||
$this->reader = $reader; | ||
} | ||
|
||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('geoip', [$this, 'geoipFunction'], ['is_safe' => ['html']]), | ||
]; | ||
} | ||
|
||
public function geoipFunction($type, $ip) | ||
{ | ||
|
||
if (empty($ip)) { | ||
return ''; | ||
} | ||
$domain = null; | ||
if (is_array($ip)) { | ||
$domain = $ip[1]; | ||
$ip = $ip[0]; | ||
} | ||
|
||
try { | ||
$iso = strtolower($this->reader->country($ip)->country->isoCode); | ||
} catch (\Exception $e) { | ||
$iso = ''; | ||
} | ||
try { | ||
switch ($type) { | ||
case 'country' || 'default': | ||
$response = '<span class="flag flag-xxs flag-country-'.$iso.'"></span>'; | ||
|
||
if (!is_null($domain)) { | ||
return $domain.' '.$response; | ||
} else { | ||
return $ip.' '.$response; | ||
} | ||
|
||
default: | ||
return $ip; | ||
} | ||
} | ||
catch (\Exception $e) { | ||
return $ip; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.