2023-02-23 11:26:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Lib\Helpers;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
2024-01-09 15:20:18 +00:00
|
|
|
use Illuminate\Validation\Rule;
|
2023-02-23 11:26:03 +00:00
|
|
|
|
|
|
|
|
class ToolboxHelper
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Validate recaptcha
|
|
|
|
|
*
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @param boolean $isV3
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function validateRecaptcha(Request $request, bool $isV3 = true)
|
|
|
|
|
{
|
|
|
|
|
Validator::validate($request->all(), [
|
|
|
|
|
'g-recaptcha-response' => [
|
|
|
|
|
'required', 'string',
|
|
|
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed, Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
|
|
|
|
function ($attribute, $value, $fail) use ($isV3) {
|
|
|
|
|
try {
|
|
|
|
|
$response = Http::asForm()->post('https://www.google.com/recaptcha/api/siteverify', [
|
|
|
|
|
'secret' => config('services.google.recaptcha.private'),
|
|
|
|
|
'response' => \strval($value),
|
|
|
|
|
'remoteip' => app('clientIp')
|
|
|
|
|
]);
|
|
|
|
|
if ($e = $response->toException()) {
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
if ($isV3) {
|
|
|
|
|
if (
|
|
|
|
|
!$response->json('success') or
|
|
|
|
|
!$response->json('challenge_ts') or
|
|
|
|
|
Carbon::parse($response->json('challenge_ts'))->lessThan(\now('utc')->subMinutes(5)) or
|
|
|
|
|
$response->json('hostname') !== \parse_url(config('app.url'), \PHP_URL_HOST) or
|
|
|
|
|
!$response->json('score')
|
|
|
|
|
) {
|
|
|
|
|
throw new \RuntimeException();
|
|
|
|
|
}
|
|
|
|
|
if (\floatval($response->json('score')) < 0.8) {
|
|
|
|
|
$fail("Vous n'avez pas obtenu un score assez élevé lors de la validation du recaptcha");
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
!$response->json('success') or
|
|
|
|
|
!$response->json('challenge_ts') or
|
|
|
|
|
Carbon::parse($response->json('challenge_ts'))->lessThan(\now('utc')->subMinutes(5)) or
|
|
|
|
|
$response->json('hostname') !== \parse_url(config('app.url'), \PHP_URL_HOST)
|
|
|
|
|
) {
|
|
|
|
|
throw new \RuntimeException();
|
|
|
|
|
}
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$fail('Erreur lors de la validation du formulaire veuillez réssayer');
|
|
|
|
|
} //end try
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
], [], ['g-recaptcha-response' => 'reCaptcha']);
|
|
|
|
|
}
|
2024-01-09 15:20:18 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Asserts that the field is unique
|
|
|
|
|
*
|
|
|
|
|
* @param string $table
|
|
|
|
|
* @param string $field
|
|
|
|
|
* @param string|integer|float|boolean|null|array $value
|
|
|
|
|
* @param integer|null $id
|
|
|
|
|
* @param callable|null $query
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws \Illuminate\Validation\ValidationException If the field is not unique.
|
|
|
|
|
*/
|
|
|
|
|
public static function assertFieldIsUnique(
|
|
|
|
|
string $table,
|
|
|
|
|
string $field,
|
|
|
|
|
$value,
|
|
|
|
|
?int $id = null,
|
|
|
|
|
?callable $query = null
|
|
|
|
|
) {
|
|
|
|
|
if (!$query) {
|
|
|
|
|
Validator::make([$field => $value], [
|
|
|
|
|
self::mbReplace('.', '\.', $field) => Rule::unique($table, $field)->ignore($id),
|
|
|
|
|
], [
|
|
|
|
|
self::mbReplace('.', '\.', "{$field}.unique")
|
|
|
|
|
=> trans('crud.libs.toolbox.assertFieldIsUnique', ['attribute' => $field, 'value' => $value])
|
|
|
|
|
])->validate();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Validator::make([$field => $value], [
|
|
|
|
|
$field => Rule::unique($table)->where($query)->ignore($id),
|
|
|
|
|
], [
|
|
|
|
|
$field . '.unique' => trans(
|
|
|
|
|
'crud.libs.toolbox.assertFieldIsUnique',
|
|
|
|
|
['attribute' => $field, 'value' => $value]
|
|
|
|
|
)
|
|
|
|
|
])->validate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mutltibyte string replace
|
|
|
|
|
*
|
|
|
|
|
* @param array|string $search
|
|
|
|
|
* @param array|string $replace
|
|
|
|
|
* @param array|string $subject
|
|
|
|
|
* @param integer $count
|
|
|
|
|
* @return array|string
|
|
|
|
|
*/
|
|
|
|
|
public static function mbReplace(
|
|
|
|
|
$search,
|
|
|
|
|
$replace,
|
|
|
|
|
$subject,
|
|
|
|
|
int &$count = 0
|
|
|
|
|
) {
|
|
|
|
|
if (!is_array($search) && is_array($replace)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (is_array($subject)) {
|
|
|
|
|
// Call mb_replace for each single string in $subject .
|
|
|
|
|
foreach ($subject as &$string) {
|
|
|
|
|
$string = &self::mbReplace($search, $replace, $string, $count);
|
|
|
|
|
}
|
|
|
|
|
} elseif (is_array($search)) {
|
|
|
|
|
if (!is_array($replace)) {
|
|
|
|
|
foreach ($search as &$string) {
|
|
|
|
|
$subject = self::mbReplace($string, $replace, $subject, $count);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$n = max(count($search), count($replace));
|
|
|
|
|
while ($n--) {
|
|
|
|
|
$subject = self::mbReplace(current($search), current($replace), $subject, $count);
|
|
|
|
|
next($search);
|
|
|
|
|
next($replace);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$parts = mb_split(preg_quote($search), $subject);
|
|
|
|
|
$count = count($parts) - 1;
|
|
|
|
|
$subject = implode($replace, $parts);
|
|
|
|
|
} //end if
|
|
|
|
|
return $subject;
|
|
|
|
|
}
|
2023-02-23 11:26:03 +00:00
|
|
|
}
|