Files
City10/app/Http/Requests/Traits/ValidatesMathCaptcha.php
T
marcocosta10000 79d12fa2f4 feat: add City10 platform with Laravel 13, AI agents, 3D viewer, and LGPD compliance
Initial commit of the complete City10 virtual city simulation platform.

Backend (Laravel 13):
- REST API with Sanctum tokens and session-based web authentication
- Role-based access control (admin/user) with middleware protection
- AI agent CRUD with OpenRouter.ai integration restricted to free models
- Store management with 3D coordinates and AI shopkeeper assignment
- Versioned legal documents (Terms, Privacy, Cookies) with LGPD audit trail
- Math CAPTCHA on public forms and multi-language support (pt_BR, en, es)

Frontend:
- Responsive Blade templates with Tailwind CSS v4
- Three.js 3D city viewer with SSE chat streaming to AI agents
- Admin panel for store and user administration

Infrastructure:
- MariaDB/MySQL database with comprehensive migrations
- Database seeders for demo content and test accounts
- PHPUnit test suite and Laravel Pint code styling
- Vite build configuration with hot reload
2026-10-17 20:46:15 -03:00

59 lines
1.6 KiB
PHP
Executable File

<?php
namespace App\Http\Requests\Traits;
use Illuminate\Support\Facades\Session;
/**
* Simple arithmetic CAPTCHA implemented with pure Laravel primitives.
*
* Generates a sum of two random integers, stores the expected result in
* the session and validates the user answer. It introduces no external
* dependency and is therefore safe from third-party licensing concerns.
*/
trait ValidatesMathCaptcha
{
/**
* Generate a new math challenge and persist it in the session.
*
* @return array{a: int, b: int, question: string}
*/
protected static function generateMathCaptcha(): array
{
$a = random_int(1, 9);
$b = random_int(1, 9);
Session::put('captcha_result', $a + $b);
// Sliding expiry: challenge is valid for 10 minutes.
Session::put('captcha_expires_at', now()->addMinutes(10)->getTimestamp());
return [
'a' => $a,
'b' => $b,
'question' => sprintf('%d + %d = ?', $a, $b),
];
}
/**
* Validate the submitted CAPTCHA answer against the session value.
*/
protected function validateMathCaptcha(): bool
{
$expected = Session::get('captcha_result');
$expiresAt = Session::get('captcha_expires_at');
// Invalidate the challenge after it is consumed.
Session::forget(['captcha_result', 'captcha_expires_at']);
if ($expected === null || $expiresAt === null) {
return false;
}
if (now()->getTimestamp() > (int) $expiresAt) {
return false;
}
return (int) $this->input('captcha') === (int) $expected;
}
}