82 lines
2.9 KiB
PHP
82 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
use App\Entity\Comment;
|
|
use App\Entity\Conference;
|
|
use App\Form\CommentType;
|
|
use App\Repository\CommentRepository;
|
|
use App\Repository\ConferenceRepository;
|
|
use App\SpamChecker;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
|
|
class ConferenceController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private EntityManagerInterface $entityManager,
|
|
) {
|
|
}
|
|
#[Route('/', name: 'homepage')]
|
|
public function index(ConferenceRepository $conferenceRepository): Response
|
|
{
|
|
return $this->render('conference/index.html.twig', [
|
|
'conferences' => $conferenceRepository->findAll(),
|
|
]);
|
|
}
|
|
|
|
#[Route('/conference/{slug}', name: 'conference')]
|
|
public function show(
|
|
Request $request,
|
|
Conference $conference,
|
|
CommentRepository $commentRepository,
|
|
SpamChecker $spamChecker,
|
|
ConferenceRepository $conferenceRepository,
|
|
#[Autowire('%photo_dir%')] string $photoDir,
|
|
): Response {
|
|
$comment = new Comment();
|
|
$form = $this->createForm(CommentType::class, $comment);
|
|
|
|
$form->handleRequest($request);
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$comment->setConference($conference);
|
|
if ($photo = $form['photo']->getData()) {
|
|
$filename = bin2hex(random_bytes(6)).'.'.$photo->guessExtension();
|
|
$photo->move($photoDir, $filename);
|
|
$comment->setPhotoFilename($filename);
|
|
}
|
|
$this->entityManager->persist($comment);
|
|
$context = [
|
|
'user_ip' => $request->getClientIp(),
|
|
'user_agent' => $request->headers->get('user-agent'),
|
|
'referrer' => $request->headers->get('referer'),
|
|
'permalink' => $request->getUri(),
|
|
];
|
|
if (2 === $spamChecker->getSpamScore($comment, $context)) {
|
|
throw new \RuntimeException('Blatant spam, go away!');
|
|
}
|
|
$this->entityManager->flush();
|
|
|
|
return $this->redirectToRoute('conference', ['slug' => $conference->getSlug()]);
|
|
}
|
|
|
|
$offset = max(0, $request->query->getInt('offset', 0));
|
|
$paginator = $commentRepository->getCommentPaginator($conference, $offset);
|
|
|
|
return $this->render('conference/show.html.twig', [
|
|
'conferences' => $conferenceRepository->findAll(),
|
|
'conference' => $conference,
|
|
'comments' => $paginator,
|
|
'previous' => $offset - CommentRepository::COMMENTS_PER_PAGE,
|
|
'next' => min(count($paginator), $offset + CommentRepository::COMMENTS_PER_PAGE),
|
|
'comment_form' => $form,
|
|
]);
|
|
}
|
|
}
|