xref: /webtrees/app/Services/CaptchaService.php (revision 4a213054c13c5caf2ed7b812a0d260527e167ef6)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Services;
21
22use Fisharebest\Webtrees\Session;
23use Psr\Http\Message\ServerRequestInterface;
24use Ramsey\Uuid\Uuid;
25
26use function view;
27
28/**
29 * Completely Automated Public Turing test to tell Computers and Humans Apart.
30 */
31class CaptchaService
32{
33    // If the form is completed faster than this, then suspect a robot.
34    private const MINIMUM_FORM_TIME = 3.0;
35
36    /**
37     * Create the captcha
38     *
39     * @return string
40     */
41    public function createCaptcha(): string
42    {
43        $x = Uuid::uuid4()->toString();
44        $y = Uuid::uuid4()->toString();
45        $z = Uuid::uuid4()->toString();
46
47        Session::put('captcha-t', microtime(true));
48        Session::put('captcha-x', $x);
49        Session::put('captcha-y', $y);
50        Session::put('captcha-z', $z);
51
52        return view('captcha', [
53            'x' => $x,
54            'y' => $y,
55            'z' => $z,
56        ]);
57    }
58
59    /**
60     * Check the user's response.
61     *
62     * @param ServerRequestInterface $request
63     *
64     * @return bool
65     */
66    public function isRobot(ServerRequestInterface $request): bool
67    {
68        $t = Session::pull('captcha-t', '');
69        $x = Session::pull('captcha-x', '');
70        $y = Session::pull('captcha-y', '');
71        $z = Session::pull('captcha-z', '');
72
73        $value_x = $request->getParsedBody()[$x] ?? '';
74        $value_y = $request->getParsedBody()[$y] ?? '';
75
76        // The captcha uses javascript to copy value z from field y to field x.
77        // Expect it in both fields.
78        if ($value_x !== $z || $value_y !== $z) {
79            return true;
80        }
81
82        // If the form was returned too quickly, the probably a robot.
83        return microtime(true) < $t + self::MINIMUM_FORM_TIME;
84    }
85}
86