2015-03-03 20:38:52 +00:00
|
|
|
<?php
|
|
|
|
|
2019-01-20 01:52:57 +00:00
|
|
|
namespace App\Controller;
|
2015-03-03 20:38:52 +00:00
|
|
|
|
2019-01-20 04:11:30 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2019-01-19 18:26:28 +00:00
|
|
|
use DT\Bundle\GeshiBundle\Highlighter\HighlighterInterface;
|
2019-01-20 01:52:57 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2016-12-18 23:18:28 +00:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
2019-01-20 01:52:57 +00:00
|
|
|
use Symfony\Component\Form\FormInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\{Request, Response};
|
2019-01-20 04:11:30 +00:00
|
|
|
use App\Entity\Paste;
|
|
|
|
use App\Form\PasteType;
|
2015-03-03 20:38:52 +00:00
|
|
|
use \GeSHi\GeSHi;
|
|
|
|
|
2019-01-20 01:52:57 +00:00
|
|
|
class PasteController extends AbstractController
|
2015-03-03 20:38:52 +00:00
|
|
|
{
|
2019-01-20 01:52:57 +00:00
|
|
|
public function createAction(Request $request): Response
|
2015-03-03 20:38:52 +00:00
|
|
|
{
|
2019-01-20 04:11:30 +00:00
|
|
|
$paste = new Paste();
|
2015-03-03 20:38:52 +00:00
|
|
|
$form = $this->createCreateForm($paste);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
2016-12-19 00:23:45 +00:00
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
2015-10-14 03:53:54 +00:00
|
|
|
// Check "captcha"
|
|
|
|
// @todo check internally in transformation or somewhere else
|
|
|
|
if (null === $form->get('captcha')->getData()) {
|
|
|
|
throw $this->createNotFoundException('This isn\'t the service you\'re spamming to.');
|
|
|
|
}
|
|
|
|
|
2015-03-03 20:38:52 +00:00
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
|
|
|
|
if ($form->get('private')->getData()) {
|
|
|
|
// Private paste
|
|
|
|
$paste->setSecret(substr(md5($paste->getText()), 0, 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
$expire = (int) $form->get('expire')->getData();
|
|
|
|
//var_dump($expire); exit();
|
|
|
|
if ($expire) {
|
|
|
|
$dateExpire = new \DateTime();
|
|
|
|
$dateExpire->add(new \DateInterval('PT' . $expire . 'S'));
|
|
|
|
$paste->setDateExpire($dateExpire);
|
|
|
|
}
|
|
|
|
|
|
|
|
$paste->setIp($request->getClientIp());
|
|
|
|
|
|
|
|
$em->persist($paste);
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
if ($paste->isPrivate()) {
|
2019-01-19 18:26:28 +00:00
|
|
|
return $this->redirect($this->generateUrl('paste_show_private', ['id' => $paste->getId(), 'secret' => $paste->getSecret()]));
|
2015-03-03 20:38:52 +00:00
|
|
|
} else {
|
2019-01-19 18:26:28 +00:00
|
|
|
return $this->redirect($this->generateUrl('paste_show_public', ['id' => $paste->getId()]));
|
2015-03-03 20:38:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-14 03:53:54 +00:00
|
|
|
throw $this->createAccessDeniedException('Sorry :(');
|
2015-03-03 20:38:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 01:52:57 +00:00
|
|
|
public function newAction(): Response
|
2015-03-03 20:38:52 +00:00
|
|
|
{
|
2019-01-20 04:11:30 +00:00
|
|
|
$paste = new Paste();
|
2015-03-03 20:38:52 +00:00
|
|
|
$createForm = $this->createCreateForm($paste);
|
|
|
|
|
2019-01-19 18:26:28 +00:00
|
|
|
return $this->render('Paste/new.html.twig', [
|
2015-03-03 20:38:52 +00:00
|
|
|
'entity' => $paste,
|
|
|
|
'form_create' => $createForm->createView(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-01-20 04:11:30 +00:00
|
|
|
public function showAction(int $id, ?string $secret, EntityManagerInterface $em, HighlighterInterface $highlighter): Response
|
2015-03-03 20:38:52 +00:00
|
|
|
{
|
2019-01-20 04:11:30 +00:00
|
|
|
/* @var $paste \App\Entity\Paste */
|
|
|
|
$paste = $em->getRepository(Paste::class)->findOneBy([
|
2015-03-03 20:38:52 +00:00
|
|
|
'id' =>$id,
|
|
|
|
'secret' => $secret
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!$paste) {
|
|
|
|
throw $this->createNotFoundException('Unable to find copypaste.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$editForm = $this->createCreateForm($paste);
|
|
|
|
|
|
|
|
$highlightedCode = $highlighter->highlight($paste->getText(), $paste->getLanguage()->getCode(), function(GeSHi $geshi) {
|
|
|
|
$geshi->set_header_type(GESHI_HEADER_PRE);
|
|
|
|
$geshi->enable_line_numbers(GESHI_NO_LINE_NUMBERS);
|
|
|
|
});
|
|
|
|
|
2019-01-19 18:26:28 +00:00
|
|
|
return $this->render('Paste/show.html.twig', [
|
2015-03-03 20:38:52 +00:00
|
|
|
'paste' => $paste,
|
|
|
|
'highlighted_text' => $highlightedCode,
|
|
|
|
'form_create' => $editForm->createView(),
|
|
|
|
]);
|
|
|
|
}
|
2019-01-20 01:52:57 +00:00
|
|
|
|
|
|
|
public function sidebarAction(): Response
|
2015-03-03 20:38:52 +00:00
|
|
|
{
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
|
2019-01-20 04:11:30 +00:00
|
|
|
$pastes = $em->getRepository(Paste::class)->findBy(
|
2015-03-03 20:38:52 +00:00
|
|
|
['secret' => null],
|
|
|
|
['id' => 'DESC'],
|
|
|
|
// @todo move to the config
|
|
|
|
15
|
|
|
|
);
|
|
|
|
|
2019-01-19 18:26:28 +00:00
|
|
|
return $this->render('sidebar.html.twig', ['pastes' => $pastes]);
|
2015-03-03 20:38:52 +00:00
|
|
|
}
|
2019-01-20 01:52:57 +00:00
|
|
|
|
2019-01-20 04:11:30 +00:00
|
|
|
private function createCreateForm(Paste $entity): FormInterface
|
2019-01-20 01:52:57 +00:00
|
|
|
{
|
2019-01-20 04:11:30 +00:00
|
|
|
$form = $this->createForm(PasteType::class, $entity, [
|
2019-01-20 01:52:57 +00:00
|
|
|
'action' => $this->generateUrl('paste_create'),
|
|
|
|
'method' => 'POST',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$form->add('submit', SubmitType::class, ['label' => 'Create']);
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
2015-03-03 20:38:52 +00:00
|
|
|
}
|