HumanVerification.php 3.37 KB
Newer Older
Dominik Hebeler's avatar
Dominik Hebeler committed
1
2
3
4
<?php

namespace App\Http\Middleware;

5
6
use Captcha;
use Carbon;
Dominik Hebeler's avatar
Dominik Hebeler committed
7
8
use Closure;
use DB;
9
use Illuminate\Http\Response;
Dominik Hebeler's avatar
Dominik Hebeler committed
10
11
12
13
14
15
16
17
18
19
20
21

class HumanVerification
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
22
23
        try {
            $id = md5($request->ip());
Dominik Hebeler's avatar
Dominik Hebeler committed
24

25
26
27
28
29
30
31
32
33
            /**
             * If the user sends a Password or a key
             * We will not verificate the user.
             * If someone that uses a bot finds this out we
             * might have to change it at some point.
             */
            if ($request->has('password') || $request->filled('key') || $request->filled('appversion') || !env('BOT_PROTECTION', false)) {
                return $next($request);
            }
Dominik Hebeler's avatar
Dominik Hebeler committed
34

35
            $user = DB::table('humanverification')->where('id', $id)->first();
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
            $createdAt = Carbon::now();
            $unusedResultPages = 1;
            $locked = false;
            # If this user doesn't have an entry we will create one
            if ($user === null) {
                DB::table('humanverification')->insert(
                    ['id' => $id, 'unusedResultPages' => 1, 'locked' => false, "lockedKey" => "", 'updated_at' => Carbon::now()]
                );
                # Insert the URL the user tries to reach
                $url = url()->full();
                DB::table('usedurls')->insert(['user_id' => $id, 'url' => $url]);
                $user = DB::table('humanverification')->where('id', $id)->first();
            } else if ($user->locked !== 1) {
                $unusedResultPages = intval($user->unusedResultPages);
                $unusedResultPages++;
                # We have different security gates:
                #   50, 75, 85, >=90 => Captcha validated Result Pages
                # If the user shows activity on our result page the counter will be deleted
                # Maybe I'll add a ban if the user reaches 100
                if ($unusedResultPages === 50 || $unusedResultPages === 75 || $unusedResultPages === 85 || $unusedResultPages >= 90) {
                    $locked = true;
                }
                DB::table('humanverification')->where('id', $id)->update(['unusedResultPages' => $unusedResultPages, 'locked' => $locked, 'updated_at' => $createdAt]);
                # Insert the URL the user tries to reach
                DB::table('usedurls')->insert(['user_id' => $id, 'url' => url()->full()]);
Dominik Hebeler's avatar
Dominik Hebeler committed
61
62
            }

63
            $request->request->add(['verification_id' => $id, 'verification_count' => $unusedResultPages]);
Dominik Hebeler's avatar
Dominik Hebeler committed
64

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
            # If the user is locked we will force a Captcha validation
            if ($user->locked === 1) {
                $captcha = Captcha::create("default", true);
                DB::table('humanverification')->where('id', $id)->update(['lockedKey' => $captcha["key"]]);
                return
                new Response(
                    view('captcha')
                        ->with('title', "Bestätigung erforderlich")
                        ->with('id', $id)
                        ->with('url', url()->full())
                        ->with('image', $captcha["img"])
                );
            }
        } catch (\Illuminate\Database\QueryException $e) {
            // Failure in contacting metager3.de
Dominik Hebeler's avatar
Dominik Hebeler committed
80
81
82
83
84
        }

        return $next($request);
    }
}