You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
4.8 KiB
PHP
126 lines
4.8 KiB
PHP
<?php
|
|
namespace App\Controller;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
|
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
use App\Entity\Pad;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
|
Class PadController extends AbstractController
|
|
{
|
|
private function get_padform($name = null){
|
|
return $this->createFormBuilder()
|
|
->add('content', TextareaType::class)
|
|
->add('save', SubmitType::class, ['label' => 'Enregistrer'])
|
|
->add('name',HiddenType::class,[ 'attr' => ['value' => is_null($name)? '' : $name ]])
|
|
->add('crypt_iv', HiddenType::class,[])
|
|
->add('crypt_v', HiddenType::class,[])
|
|
->add('crypt_iter', HiddenType::class,[])
|
|
->add('crypt_ks', HiddenType::class,[])
|
|
->add('crypt_ts', HiddenType::class,[])
|
|
->add('crypt_mode', HiddenType::class,[])
|
|
->add('crypt_adata', HiddenType::class,[])
|
|
->add('crypt_cipher', HiddenType::class,[])
|
|
->add('crypt_salt', HiddenType::class,[])
|
|
->setAction($this->generateUrl('api_post_new'))
|
|
->getForm();
|
|
|
|
}
|
|
public function showForm($name = null){
|
|
|
|
return $this->render('pad.html.twig', [
|
|
'head_title' => is_null($name) ? 'Créer un nouveau PAD' : 'Créer le pad id ' . $name ,
|
|
'page_title' => 'Simple Pad',
|
|
'form' => $this->get_padform($name)->createView()
|
|
]);
|
|
}
|
|
public function view($name)
|
|
{
|
|
|
|
$pads = $this->getDoctrine()
|
|
->getRepository(Pad::class)
|
|
->findBy(array('name' => $name));
|
|
|
|
if(count($pads) == 0 )
|
|
{
|
|
|
|
|
|
return $this->showForm($name);
|
|
//throw new NotFoundHttpException('This pad does not exist');
|
|
}
|
|
|
|
$pad = $pads[0];
|
|
|
|
return $this->render('pad-view.html.twig', [
|
|
'head_title' => 'Pad id: ' . $pad->getName(),
|
|
'page_title' => 'Pad id: ' . $pad->getName(),
|
|
'pad_content' => $pad->getContent(),
|
|
'crypted' => (bool)($pad->getCryptCipher() <> "none"),
|
|
'sjclArr' => array(
|
|
"iv" => $pad->getCryptIv(),
|
|
"v" => $pad->getCryptV(),
|
|
"iter" => $pad->getCryptIter(),
|
|
"ks" => $pad->getCryptKs(),
|
|
"ts" => $pad->getCryptTs(),
|
|
"mode" => $pad->getCryptMode(),
|
|
"adata" => $pad->getCryptAdata(),
|
|
"cipher" => $pad->getCryptCipher(),
|
|
"salt" => $pad->getCryptSalt()
|
|
)
|
|
]);
|
|
}
|
|
private function get_free_name( $depth = 0, $length=6)
|
|
{
|
|
|
|
if($depth > 3 ){
|
|
throw new \UnexpectedValueException("I cant generate an unique key");
|
|
}
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
$randstring = '';
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$randstring .= $characters[rand(0, strlen($characters) -1)];
|
|
}
|
|
|
|
$pads = $this->getDoctrine()
|
|
->getRepository(Pad::class)
|
|
->findBy(array('name' => $randstring));
|
|
if(count($pads) > 0){
|
|
return $this->get_free_name( $depth + 1);
|
|
}
|
|
return $randstring;
|
|
|
|
}
|
|
public function post(Request $request)
|
|
{
|
|
|
|
$form = $this->get_padform();
|
|
$form->handleRequest($request);
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$data = $form->getData();
|
|
//save the pad
|
|
$entityManager = $this->getDoctrine()->getManager();
|
|
$pad = new PAD();
|
|
$pad->setContent($data["content"])
|
|
->setName(( $data["name"] == '' )? $this-> get_free_name() : $data["name"] )
|
|
->setCryptIv( $data["crypt_iv"] )
|
|
->setCryptMode( $data["crypt_mode"] )
|
|
->setCryptAdata( is_null( $data["crypt_adata"])? "" : $data["crypt_adata"] )
|
|
->setCryptCipher( $data["crypt_cipher"] )
|
|
->setCryptSalt( $data["crypt_salt"] )
|
|
->setCryptV( $data["crypt_v"] )
|
|
->setCryptIter( $data["crypt_iter"] )
|
|
->setCryptKs( $data["crypt_ks"] )
|
|
->setCryptTs( $data["crypt_ts"] );
|
|
$entityManager->persist($pad);
|
|
$entityManager->flush();
|
|
return $this->redirectToRoute('view',["name" => $pad->getName() ]);
|
|
}
|
|
}
|
|
} |