add Moderation.php #14
@@ -47,7 +47,8 @@
|
|||||||
"symfonycasts/verify-email-bundle": "^1.17",
|
"symfonycasts/verify-email-bundle": "^1.17",
|
||||||
"twig/extra-bundle": "^2.12|^3.0",
|
"twig/extra-bundle": "^2.12|^3.0",
|
||||||
"twig/twig": "^2.12|^3.0",
|
"twig/twig": "^2.12|^3.0",
|
||||||
"vich/uploader-bundle": "^2.3"
|
"vich/uploader-bundle": "^2.3",
|
||||||
|
"ext-curl": "*"
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"allow-plugins": {
|
"allow-plugins": {
|
||||||
|
@@ -6,6 +6,7 @@
|
|||||||
parameters:
|
parameters:
|
||||||
trusted_proxies: '%env(TRUSTED_PROXIES)%'
|
trusted_proxies: '%env(TRUSTED_PROXIES)%'
|
||||||
env(TRUSTED_PROXIES): 127.0.0.1
|
env(TRUSTED_PROXIES): 127.0.0.1
|
||||||
|
app.API_KEY_SIGHT_ENGINE: '%env(API_KEY_SIGHT_ENGINE)%'
|
||||||
|
|
||||||
services:
|
services:
|
||||||
# default configuration for services in *this* file
|
# default configuration for services in *this* file
|
||||||
|
@@ -8,8 +8,11 @@ use App\Entity\User;
|
|||||||
use App\Form\CommentType;
|
use App\Form\CommentType;
|
||||||
use App\Form\PostType;
|
use App\Form\PostType;
|
||||||
use App\Repository\PostRepository;
|
use App\Repository\PostRepository;
|
||||||
|
use App\Security\Moderation\Moderation;
|
||||||
|
use CURLFile;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
@@ -45,15 +48,19 @@ class PostController extends AbstractController
|
|||||||
|
|
||||||
#[Route('/post/new', name: 'app_post_new', methods: ['GET', 'POST'])]
|
#[Route('/post/new', name: 'app_post_new', methods: ['GET', 'POST'])]
|
||||||
#[IsGranted('ROLE_USER')]
|
#[IsGranted('ROLE_USER')]
|
||||||
public function new(Request $request, EntityManagerInterface $entityManager): Response
|
public function new(Request $request, EntityManagerInterface $entityManager, ParameterBagInterface $env): Response
|
||||||
{
|
{
|
||||||
$post = new Post();
|
$post = new Post();
|
||||||
$form = $this->createForm(PostType::class, $post);
|
$form = $this->createForm(PostType::class, $post);
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
|
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
|
$api_key = $this->getParameter('app.API_KEY_SIGHT_ENGINE');
|
||||||
|
$moderation = new Moderation($api_key);
|
||||||
|
if($moderation->valide($post->getImageFile()->getRealPath())){
|
||||||
$entityManager->persist($post);
|
$entityManager->persist($post);
|
||||||
$entityManager->flush();
|
$entityManager->flush();
|
||||||
|
}
|
||||||
|
|
||||||
return $this->redirectToRoute('app_posts', [], Response::HTTP_SEE_OTHER);
|
return $this->redirectToRoute('app_posts', [], Response::HTTP_SEE_OTHER);
|
||||||
}
|
}
|
||||||
|
39
src/Security/Moderation/Moderation.php
Normal file
39
src/Security/Moderation/Moderation.php
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Security\Moderation;
|
||||||
|
|
||||||
|
use CURLFile;
|
||||||
|
|
||||||
|
class Moderation
|
||||||
|
{
|
||||||
|
private String $api_key;
|
||||||
|
|
||||||
|
public function __construct(String $api_key)
|
||||||
|
{
|
||||||
|
$this->api_key = $api_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function valide($file_image)
|
||||||
|
{
|
||||||
|
$params_api = array(
|
||||||
|
'media' => new CurlFile($file_image),
|
||||||
|
'models' => 'nudity-2.1',
|
||||||
|
'api_user' => '26959338',
|
||||||
|
'api_secret' => $this->api_key,
|
||||||
|
);
|
||||||
|
|
||||||
|
$ch = curl_init('https://api.sightengine.com/1.0/check.json');
|
||||||
|
curl_setopt($ch, CURLOPT_POST, true);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $params_api);
|
||||||
|
$response = curl_exec($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
$output = json_decode($response, true);
|
||||||
|
|
||||||
|
$score_nudity = $output["nudity"];
|
||||||
|
return $score_nudity["sexual_activity"] < 0.8 &&
|
||||||
|
$score_nudity["sexual_display"] < 0.8 &&
|
||||||
|
$score_nudity["erotica"] < 0.8;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user