From 82b873c071a23eaa619ed10b413883670d3bb42a Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Sat, 28 Mar 2020 19:07:46 -0400 Subject: [PATCH] Domain detection failing for .co.uk, etc #647 (#648) * Fix domain name detection #647 * Ignore page links check if no DB configured #641 --- app/Http/Composers/PageLinksComposer.php | 16 ++- app/Support/Utils.php | 18 ++- composer.json | 2 +- composer.lock | 149 ++++++++++++++++++++++- tests/UtilsTest.php | 3 +- 5 files changed, 169 insertions(+), 19 deletions(-) diff --git a/app/Http/Composers/PageLinksComposer.php b/app/Http/Composers/PageLinksComposer.php index 221bf5d5..afe9caf6 100644 --- a/app/Http/Composers/PageLinksComposer.php +++ b/app/Http/Composers/PageLinksComposer.php @@ -4,6 +4,7 @@ namespace App\Http\Composers; use App\Contracts\Composer; use App\Repositories\PageRepository; +use Exception; use Illuminate\Support\Facades\Auth; use Illuminate\View\View; @@ -26,13 +27,18 @@ class PageLinksComposer extends Composer */ public function compose(View $view) { - // If not logged in, then only get the public pages - $w = ['enabled' => true]; - if (!Auth::check()) { - $w = ['public' => true]; + try { + // If not logged in, then only get the public pages + $w = ['enabled' => true]; + if (!Auth::check()) { + $w = ['public' => true]; + } + + $pages = $this->pageRepo->findWhere($w, ['id', 'name', 'slug', 'icon']); + } catch (Exception $e) { + $pages = []; } - $pages = $this->pageRepo->findWhere($w, ['id', 'name', 'slug', 'icon']); $view->with('page_links', $pages); } } diff --git a/app/Support/Utils.php b/app/Support/Utils.php index 6eac5e66..98455e24 100644 --- a/app/Support/Utils.php +++ b/app/Support/Utils.php @@ -6,6 +6,7 @@ use App\Contracts\Model; use Hashids\Hashids; use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Support\Str; +use LayerShifter\TLDExtract\Extract; use Nwidart\Modules\Facades\Module; /** @@ -109,19 +110,14 @@ class Utils */ public static function getRootDomain(string $url): string { - if (!Str::contains($url, ['https://', 'http://'])) { - $url = 'http://'.$url; + if (Str::contains($url, ['https://', 'http://'])) { + $url = str_replace('https://', '', $url); + $url = str_replace('http://', '', $url); } - $domain = parse_url($url, PHP_URL_HOST); - $domain = explode('.', $domain); - $len = count($domain); - if ($len == 1) { - return $domain[0]; - } + $extract = new Extract(); + $result = $extract->parse($url); - $domain = $domain[$len - 2].'.'.$domain[$len - 1]; - - return $domain; + return $result->getRegistrableDomain(); } } diff --git a/composer.json b/composer.json index 4f7be286..48246d30 100755 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ "laracasts/flash": "^3.0", "laravel/helpers": "^1.0", "laravelcollective/html": "~6.0.0", + "layershifter/tld-extract": "^2.0", "league/csv": "9.2.*", "league/geotools": "0.8.*", "league/iso3166": "2.1.*", @@ -104,7 +105,6 @@ "@php -r \"array_map('unlink', glob('bootstrap/cache/*'));\"" ], "post-package-uninstall": [ - "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", "@php artisan package:discover --ansi", "@php artisan phpvms:caches" ], diff --git a/composer.lock b/composer.lock index 7cb63ccc..b9650d40 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "39ac8c1896bfc4427e9382c7e57e2e62", + "content-hash": "dd64c8c9fc4c4548a21073225e1dcebe", "packages": [ { "name": "akaunting/money", @@ -2840,6 +2840,153 @@ "homepage": "https://laravelcollective.com", "time": "2019-10-02T00:37:39+00:00" }, + { + "name": "layershifter/tld-database", + "version": "1.0.69", + "source": { + "type": "git", + "url": "https://github.com/layershifter/TLDDatabase.git", + "reference": "7423c7acdc5147268fb856f532a5d8875f6fe41e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/layershifter/TLDDatabase/zipball/7423c7acdc5147268fb856f532a5d8875f6fe41e", + "reference": "7423c7acdc5147268fb856f532a5d8875f6fe41e", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "layershifter/tld-support": "^1.0.0", + "php": "^5.5.0 || ^7.0" + }, + "require-dev": { + "codeclimate/php-test-reporter": "dev-master", + "mikey179/vfsstream": "^1.6", + "phpmd/phpmd": "@stable", + "phpunit/phpunit": "^4.8 || ^5.0", + "squizlabs/php_codesniffer": "~2.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "LayerShifter\\TLDDatabase\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Alexander Fedyashov", + "email": "a@fedyashov.com" + } + ], + "description": "Database abstraction for Public Suffix List", + "keywords": [ + "PSL", + "Public Suffix List", + "domain database", + "tld database" + ], + "time": "2019-08-04T10:03:36+00:00" + }, + { + "name": "layershifter/tld-extract", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/layershifter/TLDExtract.git", + "reference": "b49cb89a4ed59a9474a47589054e56ce57787348" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/layershifter/TLDExtract/zipball/b49cb89a4ed59a9474a47589054e56ce57787348", + "reference": "b49cb89a4ed59a9474a47589054e56ce57787348", + "shasum": "" + }, + "require": { + "layershifter/tld-database": "^1.0", + "layershifter/tld-support": "^1.1", + "php": "^5.5.0 || ^7.0", + "symfony/polyfill-intl-idn": "^1.10" + }, + "require-dev": { + "phpmd/phpmd": "@stable", + "phpunit/phpunit": "^4.8 || ^5.0", + "squizlabs/php_codesniffer": "~2.0" + }, + "type": "library", + "autoload": { + "files": [ + "src/static.php" + ], + "psr-4": { + "LayerShifter\\TLDExtract\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Alexander Fedyashov", + "email": "a@fedyashov.com" + } + ], + "description": "TLDExtract, library for extracting parts of domain, e.q. domain parser", + "keywords": [ + "TLDExtract", + "domain parser" + ], + "abandoned": true, + "time": "2019-02-11T14:37:05+00:00" + }, + { + "name": "layershifter/tld-support", + "version": "1.1.1", + "source": { + "type": "git", + "url": "https://github.com/layershifter/TLDSupport.git", + "reference": "9b9eb2350db82239289f4e6eb250a2fe4865b0db" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/layershifter/TLDSupport/zipball/9b9eb2350db82239289f4e6eb250a2fe4865b0db", + "reference": "9b9eb2350db82239289f4e6eb250a2fe4865b0db", + "shasum": "" + }, + "require": { + "php": ">=5.5", + "symfony/polyfill-mbstring": "^1.2" + }, + "require-dev": { + "codeclimate/php-test-reporter": "dev-master", + "phpmd/phpmd": "@stable", + "phpunit/phpunit": "4.8.*", + "squizlabs/php_codesniffer": "2.*" + }, + "type": "library", + "autoload": { + "psr-4": { + "LayerShifter\\TLDSupport\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Alexander Fedyashov", + "email": "a@fedyashov.com" + } + ], + "description": "Support package for TLDDatabase and TLDExtract", + "abandoned": true, + "time": "2016-06-18T12:28:04+00:00" + }, { "name": "league/commonmark", "version": "1.3.2", diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index cdd37a6c..c9d3269e 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -94,6 +94,7 @@ class UtilsTest extends TestCase $this->assertEquals('phpvms.net', Utils::getRootDomain($case)); } - $this->assertEquals('phpvms', Utils::getRootDomain('http://phpvms')); + $this->assertEquals('phpvms.co.uk', Utils::getRootDomain('http://phpvms.co.uk')); + $this->assertEquals('phpvms.co.uk', Utils::getRootDomain('http://www.phpvms.co.uk')); } }