/** * 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); }
// 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()); // If you just need to know subdomain/registrable domain/public suffix info // about a host, there are public methods available for that in the Parser var_dump($parser->getSubdomain('www.scottwills.co.uk')); var_dump($parser->getRegistrableDomain('www.scottwills.co.uk')); var_dump($parser->getPublicSuffix('www.scottwills.co.uk'));