mirror of
https://github.com/projectsend/projectsend.git
synced 2026-09-16 16:45:07 +00:00
6e47d76ba6
Client file sharing, rebuilt from the ground up: a private area per client, resumable uploads, folders, groups and categories, sharing with expiry dates and download limits, comments, file versions, an activity log, a REST API, and sixteen languages. This repository begins here. ProjectSend 2 was developed privately, and that development history is not published — the previous generation remains available, with its own history, at projectsend/legacy. Free software under the GNU General Public License v2, or (at your option) any later version.
55 lines
1.8 KiB
PHP
55 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Platform\Captcha;
|
|
|
|
use Closure;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Support\Facades\Request;
|
|
use Illuminate\Translation\PotentiallyTranslatedString;
|
|
|
|
/**
|
|
* Verifies one token, as a validation rule.
|
|
*
|
|
* A rule rather than an explicit call in each controller, because all four
|
|
* protected forms already build a rule array — so enforcement is one array
|
|
* entry, "switched off" is an empty array rather than an `if`, and the
|
|
* error lands under a field key that both the Inertia forms and the JSON
|
|
* comment endpoint already render.
|
|
*
|
|
* It also removes, structurally, the worst bug in v1's version: there is
|
|
* one rule, so there is exactly one verification per request. v1's
|
|
* registration path called the provider twice for reCAPTCHA v2 — the
|
|
* second call on an already-consumed token — which failed every time and
|
|
* made self-registration impossible to complete.
|
|
*/
|
|
class CaptchaRule implements ValidationRule
|
|
{
|
|
public function __construct(
|
|
private readonly CaptchaForm $form,
|
|
) {}
|
|
|
|
/**
|
|
* @param Closure(string, string|null=): PotentiallyTranslatedString $fail
|
|
*/
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
{
|
|
if (! is_string($value) || trim($value) === '') {
|
|
// Reached only when a caller drops `required`; the message
|
|
// still has to make sense to whoever sees it.
|
|
$fail(__('Complete the security check and try again.'));
|
|
|
|
return;
|
|
}
|
|
|
|
$result = app(CaptchaVerifier::class)->verify($value, $this->form, Request::ip());
|
|
|
|
if ($result->allowsRequest()) {
|
|
return;
|
|
}
|
|
|
|
$fail(__('Security check failed. Please try again.'));
|
|
}
|
|
}
|