dismiss greeter, better view counting

pull/26/head
Ondřej Hruška 6 years ago
parent a79b5ae7d8
commit 6d7fdca6d2
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 38
      app/Http/Controllers/Controller.php
  2. 15
      app/Http/Controllers/DashController.php
  3. 27
      app/Http/Controllers/TableController.php
  4. 3
      app/Http/Kernel.php
  5. 4
      resources/views/welcome.blade.php
  6. 10
      routes/web.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())

@ -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'));
}
}

@ -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
}
}
}
}

@ -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' => [

@ -3,12 +3,12 @@
@extends('layouts.app')
@section('content')
@guest
@if($showGreeter)
<div class="row justify-content-center">
<div class="col-md-12 mb-2 mb-md-4">
<div class="card">
<div class="card-body">
<a href="" class="button close" aria-label="Close">
<a href="{{ route('dash', ['dismiss-greeter' => 1]) }}" class="button close" aria-label="Close">
<span aria-hidden="true">&times;</span>@sr(Close)
</a>

@ -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');

Loading…
Cancel
Save