protected function runProviderTest(ProviderInterface $provider) { $this->assertTrue($provider->isSpamReferrer($this->parser->parseUrl('http://example.org'))); $this->assertTrue($provider->isSpamReferrer($this->parser->parseUrl('http://dev.example.org'))); $this->assertTrue($provider->isSpamReferrer($this->parser->parseUrl('http://dev.example.com'))); $this->assertFalse($provider->isSpamReferrer($this->parser->parseUrl('http://xyz.example.com'))); }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $referer = $request->server->get('referer'); // Referer is not provided, we continue if (is_null($referer) or env('APP_ENV', 'production') !== 'production') { return $next($request); } // Handle the dictionnary. // @todo Refactor that $dir = realpath(dirname(__FILE__) . '/../../../../../') . '/'; $path = $dir . 'spammers.json'; $file = file_get_contents($path); // Dictionnary is not readable, abort. if (empty($file)) { abort(500, 'Unable to read spammers.json'); } $dictionary = json_decode($file); // Parse the referer $url = new Parser(new PublicSuffixList([])); $host = $url->parseHost($referer)->host; // Compare dictionary against the referer... $search = Arr::where($dictionary, function ($key, $value) use($host) { return mb_stripos($host, $value) !== false; }); // ...and check for results if (count($search) > 0) { abort(500, 'Spammers protection'); } // No spam, we can continue :) return $next($request); }
protected function setUp() { parent::setUp(); $file = realpath(dirname(__DIR__) . '/../../../data/' . PublicSuffixListManager::PDP_PSL_PHP_FILE); $psl = new PublicSuffixList($file); $parser = new Parser($psl); $this->url = $parser->parseUrl($this->spec); }
/** * @param string|Url $url * @return bool */ public function isSpamReferrer($url) { if (!$url instanceof Url) { try { $url = $this->domainParser->parseUrl($url); } catch (\Exception $exp) { return false; } } return $this->provider->isSpamReferrer($url); }
/** * Wrapper around Jeremy Kendall's PHP Domain Parser that parses the * domain/url passed to the function and returns the Tld and Valid domain * @param $domain * @throws \InvalidArgumentException when the tld is not valid * @return array returns an associative array with the domain and tld */ private function parse($domain) { $pslManager = new PublicSuffixListManager(); $parser = new Parser($pslManager->getList()); // First check if the suffix is actually valid if (!$parser->isSuffixValid($domain)) { throw new \InvalidArgumentException("Invalid TLD"); } $components = []; $components["tld"] = $parser->getPublicSuffix($domain); $components["domain"] = $parser->getRegisterableDomain($domain); return $components; }
/** * Scheme should be output in lowercase regardless of case of original arg * * @group issue51 * * @see https://tools.ietf.org/html/rfc3986#section-3.1 */ public function testSchemeAlwaysConvertedToLowerCasePerRFC3986() { $spec = 'HttPS://www.google.com'; $expected = 'https://www.google.com'; $url = $this->parser->parseUrl($spec); $this->assertEquals($expected, $url->__toString()); }
/** * @group issue29 * @see https://github.com/jeremykendall/php-domain-parser/issues/29 */ public function testIdnToAscii() { $idn = 'Яндекс.РФ'; $expected = 'http://яндекс.рф'; $url = $this->parser->parseUrl($idn); $actual = $url->__toString(); $this->assertEquals($expected, $actual); }
/** * @return string|null */ public function getDomain() { if (!isset($this->uri['host'])) { return null; } $pslManager = new PublicSuffixListManager(); $parser = new Parser($pslManager->getList()); $parse = $parser->parseUrl($this->uri['host'] . '://' . $this->uri['host']); return $parse->host->registerableDomain; }
/** * @param bool $withSuffix * * @return null|string */ public function getDomain(bool $withSuffix = true) { if (!isset($this->uri['host'])) { return null; } $pslManager = new PublicSuffixListManager(); $parser = new Parser($pslManager->getList()); $parse = $parser->parseUrl($this->uri['host'] . '://' . $this->uri['host']); if ($withSuffix) { return $parse->host->registerableDomain; } else { return substr($parse->host->registerableDomain, 0, -strlen($parse->host->publicSuffix) - 1); } }
/** * Scheme should return null when scheme is not provided. * * @group issue53 * * @see https://github.com/jeremykendall/php-domain-parser/issues/53 */ public function testSchemeReturnsNullIfNotProvidedToParser() { $spec = 'google.com'; $url = $this->parser->parseUrl($spec); $this->assertNull($url->getScheme()); }
public function getLiveDns($hostname) { $pslManager = new PublicSuffixListManager(); $domainParser = new Parser($pslManager->getList()); $hostname = strtolower($hostname); $baseDomain = $domainParser->getRegisterableDomain($hostname); $ipUtils = $this->ipUtils; $records = Cache::remember($hostname, 60 * 24, function () use($ipUtils, $hostname) { $dns = new Dns(['8.8.8.8', '8.8.4.4', 2]); $records = $dns->getDomainRecords($hostname, $testNameserver = false); ksort($records); if (isset($records['A']) === true) { $records['A'] = array_unique($records['A']); foreach ($records['A'] as $key => $address) { $geoip = $ipUtils->geoip($address); if ($geoip->country->isoCode) { $country_code = $geoip->country->isoCode; $country_name = $geoip->country->name; $city_name = $geoip->city->name; } else { $ipDec = $this->ipUtils->ip2dec($address); $prefix = IPv4BgpPrefix::where('ip_dec_start', '<=', $ipDec)->where('ip_dec_end', '>=', $ipDec)->orderBy('cidr', 'asc')->first(); if ($prefix && ($prefixWhois = $prefix->whois())) { $country_code = $prefixWhois->counrty_code; $country_name = $prefixWhois->counrty_code ? trans('countries.' . $prefixWhois->counrty_code) : null; $city_name = null; } else { $country_code = null; $country_name = 'Unknown'; $city_name = null; } } $output['address'] = $address; $output['country_code'] = $country_code; if ($city_name) { $output['location'] = $city_name . ', ' . $country_name; } else { $output['location'] = $country_name; } $records['A'][$key] = $output; } } if (isset($records['AAAA']) === true) { $records['AAAA'] = array_unique($records['AAAA']); foreach ($records['AAAA'] as $key => $address) { $geoip = $ipUtils->geoip($address); if ($geoip->country->isoCode) { $country_code = $geoip->country->isoCode; $country_name = $geoip->country->name; $city_name = $geoip->city->name; } else { $ipDec = $this->ipUtils->ip2dec($address); $prefix = IPv6BgpPrefix::where('ip_dec_start', '<=', $ipDec)->where('ip_dec_end', '>=', $ipDec)->orderBy('cidr', 'asc')->first(); if ($prefix && ($prefixWhois = $prefix->whois())) { $country_code = $prefixWhois->counrty_code; $country_name = $prefixWhois->counrty_code ? trans('countries.' . $prefixWhois->counrty_code) : null; $city_name = null; } else { $country_code = null; $country_name = 'Unknown'; $city_name = null; } } $output['address'] = $address; $output['country_code'] = $country_code; if ($city_name) { $output['location'] = $city_name . ', ' . $country_name; } else { $output['location'] = $country_name; } $records['AAAA'][$key] = $output; } } return $records; }); $data['hostname'] = $hostname; $data['base_domain'] = $baseDomain; $data['dns_records'] = $records; return $this->sendData($data); }
/** * @group issue18 * @see https://github.com/jeremykendall/php-domain-parser/issues/18 */ public function testFtpUrlToString() { $ftpUrl = 'ftp://ftp.somewhere.com'; $url = $this->parser->parseUrl($ftpUrl); $this->assertEquals($ftpUrl, $url->__toString()); }
<?php require_once __DIR__ . '/vendor/autoload.php'; use Pdp\PublicSuffixListManager; use Pdp\Parser; // Obtain an instance of the parser $pslManager = new PublicSuffixListManager(); $parser = new Parser($pslManager->getList()); // Parse a URL $url = $parser->parseUrl('http://*****:*****@www.pref.okinawa.jp:8080/path/to/page.html?query=string#fragment'); // Accessing elements of the URL var_dump($url); var_dump($url->__toString()); var_dump($url->getPath()); var_dump($url->getFragment()); // Getting the Host object from the URL $host = $url->getHost(); // Accessing elements of the Host var_dump($host); var_dump($host->__toString()); var_dump($host->getSubdomain()); var_dump($host->getRegistrableDomain()); var_dump($host->getPublicSuffix()); // It's possible to parse a host only, if you prefer $host = $parser->parseHost('a.b.c.cy'); // Accessing elements of the Host var_dump($host); var_dump($host->__toString()); var_dump($host->getSubdomain()); var_dump($host->getRegistrableDomain()); var_dump($host->getPublicSuffix());
/** * Get the fully qualified naked domain name. * * @param Buzz\Message\Request $request * @return string */ protected function getDomain($request) { if ($host = filter_var($this->metadata['host'], FILTER_VALIDATE_IP)) { return $host; } $manager = new PublicSuffixListManager(); $parser = new DomainParser($manager->getList()); return $parser->parseUrl($this->metadata['url'])->host->registerableDomain; }