From 6d7fdca6d269edafc05280bdb74d6834e7fbf439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 29 Jul 2018 12:40:29 +0200 Subject: [PATCH] dismiss greeter, better view counting --- app/Http/Controllers/Controller.php | 38 ++++++++++++++++++++++++ app/Http/Controllers/DashController.php | 15 ++++++++-- app/Http/Controllers/TableController.php | 27 ++++++++++++----- app/Http/Kernel.php | 3 +- resources/views/welcome.blade.php | 4 +-- routes/web.php | 10 +++++++ 6 files changed, 85 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 5b2cec2..e05d058 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -17,6 +17,44 @@ class Controller extends BaseController ValidatesRequests::validateWithBag as validateWithBag_orig; } + const BOT_USER_AGENTS = [ + // generic + 'crawler', + // cli / scripting + 'httpie', + 'curl', + 'wget', + 'lwp-request', + 'python-requests', + 'python-urllib', + 'libwww', + 'go-http-client', + // commercial + 'googlebot', + 'google (+', + 'bingbot', + 'slurp', + 'duckduckbot', + 'baiduspider', + 'yandexbot', + 'sogou', + 'exabot', + 'facebot', + 'ia_archiver', + 'linkdexbot', + 'gigabot', + 'adsbot', + // misc + 'gigablast', + 'phpcrawl', + 'mj12bot', + 'simplepie', + 'sitelockspider', + 'scoutjet', + 'grub.org', + 'mastodon', // mastodon fetching previews + ]; + // Hacks to allow recursive nesting of validations in string and array format public function makeValidator($data, $rules, $messages = array(), $customAttributes = array()) diff --git a/app/Http/Controllers/DashController.php b/app/Http/Controllers/DashController.php index 2d8e854..ac2a07f 100644 --- a/app/Http/Controllers/DashController.php +++ b/app/Http/Controllers/DashController.php @@ -6,11 +6,20 @@ namespace App\Http\Controllers; use App\Models\Table; use App\Models\User; +use Illuminate\Http\Request; +use MightyPork\Utils\Utils; class DashController extends Controller { - public function view() + public function view(Request $request) { + $dismiss = false; + if ($request->has('dismiss-greeter')) { + $dismiss = Utils::parseBool01($request->get('dismiss-greeter')); + \Cookie::queue('dismiss-greeter', $dismiss); + return back(); + } + $users = User::orderBy('updated_at', 'desc') ->paginate(15, ['id', 'title', 'name'], 'pageu'); @@ -19,6 +28,8 @@ class DashController extends Controller ->orderBy('visits', 'desc') ->paginate(10, ['*'], 'paget'); - return view('welcome', compact('users', 'tables')); + $showGreeter = !$dismiss && !$request->cookie('dismiss-greeter', false); + + return view('welcome', compact('users', 'tables', 'showGreeter')); } } diff --git a/app/Http/Controllers/TableController.php b/app/Http/Controllers/TableController.php index 64c3ece..45c8b94 100644 --- a/app/Http/Controllers/TableController.php +++ b/app/Http/Controllers/TableController.php @@ -34,13 +34,7 @@ class TableController extends Controller $revision = $tableModel->revision; } - $cookieName = "view!$user->name!$table"; - if (!$request->cookie($cookieName, false)) { - - $tableModel->countVisit(); - - \Cookie::queue($cookieName, true, 86400); - } + $this->countTableVisit($request, $tableModel); return view('table.view', [ 'table' => $tableModel, @@ -191,4 +185,23 @@ class TableController extends Controller return redirect(route('table.view', ['user' => $u, 'table' => $input->name])); } + + /** + * Check unique visit, filter bots / scripts, and increment visits count. + * + * @param Request $request + * @param Table $table + */ + private function countTableVisit(Request $request, Table $table) + { + $cookieName = "view_$table->id"; + if (!$request->cookie($cookieName, false)) { + $ua = $request->userAgent(); + // Filter out suspicious user agents + if (! str_contains(strtolower($ua), Controller::BOT_USER_AGENTS)) { + $table->countVisit(); + \Cookie::queue($cookieName, true, 24*60); // in minutes + } + } + } } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 3439540..5053af9 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -28,13 +28,14 @@ class Kernel extends HttpKernel */ protected $middlewareGroups = [ 'web' => [ + 'throttle:60,15', // try to prevent people refresh-spamming the server to game table visit counts \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, // \Illuminate\Session\Middleware\AuthenticateSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, - \Illuminate\Routing\Middleware\SubstituteBindings::class, + 'bindings', ], 'api' => [ diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index 8b284af..07fb236 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -3,12 +3,12 @@ @extends('layouts.app') @section('content') - @guest + @if($showGreeter)
- + @sr(Close) diff --git a/routes/web.php b/routes/web.php index 899826d..e142372 100644 --- a/routes/web.php +++ b/routes/web.php @@ -3,6 +3,16 @@ require "login.php"; +Route::get('/ping', function () { + die( + json_encode([ + 'user_agent' => request()->userAgent(), + 'ip' => request()->ip(), + 'server_time' => date('r'), + ], 128) + ); +}); + Route::get('/about/terms', function () { return view('about.terms'); })->name('terms');