diff --git a/composer.json b/composer.json index 184d29b..093497a 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ "symfony/security-bundle": "^4.2", "symfony/translation": "^4.2", "symfony/twig-bundle": "^4.2", + "symfony/validator": "^4.2", "symfony/yaml": "^4.2", "theodordiaconu/geshi": "dev-master", "theodordiaconu/geshi-bundle": "dev-master" diff --git a/composer.lock b/composer.lock index ca20d29..26f68ec 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "12a1646d5d2aeff9249a996492e96c85", + "content-hash": "b2efcf2213eb068842d3ad2b85667014", "packages": [ { "name": "doctrine/annotations", @@ -4009,6 +4009,95 @@ "homepage": "https://symfony.com", "time": "2019-01-03T09:07:35+00:00" }, + { + "name": "symfony/validator", + "version": "v4.2.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/validator.git", + "reference": "f89d2fab883a6a20d0d0093537392763cf52f0ba" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/validator/zipball/f89d2fab883a6a20d0d0093537392763cf52f0ba", + "reference": "f89d2fab883a6a20d0d0093537392763cf52f0ba", + "shasum": "" + }, + "require": { + "php": "^7.1.3", + "symfony/contracts": "^1.0.2", + "symfony/polyfill-ctype": "~1.8", + "symfony/polyfill-mbstring": "~1.0" + }, + "conflict": { + "phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0", + "symfony/dependency-injection": "<3.4", + "symfony/http-kernel": "<3.4", + "symfony/intl": "<4.1", + "symfony/translation": "<4.2", + "symfony/yaml": "<3.4" + }, + "require-dev": { + "doctrine/annotations": "~1.0", + "doctrine/cache": "~1.0", + "egulias/email-validator": "^1.2.8|~2.0", + "symfony/cache": "~3.4|~4.0", + "symfony/config": "~3.4|~4.0", + "symfony/dependency-injection": "~3.4|~4.0", + "symfony/expression-language": "~3.4|~4.0", + "symfony/http-foundation": "~4.1", + "symfony/http-kernel": "~3.4|~4.0", + "symfony/intl": "~4.1", + "symfony/property-access": "~3.4|~4.0", + "symfony/translation": "~4.2", + "symfony/var-dumper": "~3.4|~4.0", + "symfony/yaml": "~3.4|~4.0" + }, + "suggest": { + "doctrine/annotations": "For using the annotation mapping. You will also need doctrine/cache.", + "doctrine/cache": "For using the default cached annotation reader and metadata cache.", + "egulias/email-validator": "Strict (RFC compliant) email validation", + "psr/cache-implementation": "For using the metadata cache.", + "symfony/config": "", + "symfony/expression-language": "For using the Expression validator", + "symfony/http-foundation": "", + "symfony/intl": "", + "symfony/property-access": "For accessing properties within comparison constraints", + "symfony/translation": "For translating validation errors.", + "symfony/yaml": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.2-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Validator\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Validator Component", + "homepage": "https://symfony.com", + "time": "2019-01-06T14:13:54+00:00" + }, { "name": "symfony/var-exporter", "version": "v4.2.2", diff --git a/config/packages/doctrine.yaml b/config/packages/doctrine.yaml index a0e7da7..242d78b 100644 --- a/config/packages/doctrine.yaml +++ b/config/packages/doctrine.yaml @@ -8,3 +8,11 @@ doctrine: charset: 'UTF8' orm: auto_generate_proxy_classes: "%kernel.debug%" + auto_mapping: true + mappings: + App\Entity: + type: annotation + dir: '%kernel.project_dir%/src/Entity' + is_bundle: false + prefix: App\Entity + alias: App diff --git a/config/packages/test/doctrine.yaml b/config/packages/test/doctrine.yaml index 4b2ec5d..a4013b4 100644 --- a/config/packages/test/doctrine.yaml +++ b/config/packages/test/doctrine.yaml @@ -1,6 +1,5 @@ -doctrine: - dbal: - dbname: "%test_database_name%" - port: "%test_database_port%" - user: "%test_database_user%" - password: "%test_database_password%" +#doctrine: +# dbal: +# driver: 'mysqli' +# url: '%env(resolve:DATABASE_URL)%' +# charset: 'UTF8' diff --git a/config/packages/validator.yaml b/config/packages/validator.yaml new file mode 100644 index 0000000..a695e1a --- /dev/null +++ b/config/packages/validator.yaml @@ -0,0 +1,3 @@ +framework: + validation: + email_validation_mode: html5 diff --git a/config/routes.yaml b/config/routes.yaml index 49e4ce1..39fb7dd 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -1,3 +1,7 @@ +paste_new: + path: / + defaults: { _controller: 'App\Controller\PasteController::newAction' } + paste_show_public: path: /{id} defaults: { _controller: 'App\Controller\PasteController::showAction', secret: null } @@ -11,10 +15,6 @@ paste_show_private: id: \d+ secret: \w{16} -paste_new: - path: / - defaults: { _controller: 'App\Controller\PasteController::newAction' } - paste_create: path: /create defaults: { _controller: 'App\Controller\PasteController::createAction' } diff --git a/config/services.yaml b/config/services.yaml index fe6be92..6678f6d 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -22,3 +22,7 @@ services: App\Controller\: resource: '../src/Controller' tags: ['controller.service_arguments'] + + # Overriding old bundle service definition + DT\Bundle\GeshiBundle\Highlighter\HighlighterInterface: + class: DT\Bundle\GeshiBundle\Highlighter\GeshiHighlighter diff --git a/public/index.php b/public/index.php index c52f821..e30f90c 100644 --- a/public/index.php +++ b/public/index.php @@ -1,17 +1,27 @@ =1.2) -if (is_array($env = @include dirname(__DIR__).'/.env.local.php')) { - $_SERVER += $env; - $_ENV += $env; -} elseif (!class_exists(Dotenv::class)) { - throw new RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.'); -} else { - // load all the .env files - (new Dotenv())->loadEnv(dirname(__DIR__).'/.env'); + +use App\Kernel; +use Symfony\Component\Debug\Debug; +use Symfony\Component\HttpFoundation\Request; + +require dirname(__DIR__).'/config/bootstrap.php'; + +if ($_SERVER['APP_DEBUG']) { + umask(0000); + + Debug::enable(); } -$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = ($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null) ?: 'dev'; -$_SERVER['APP_DEBUG'] = $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? 'prod' !== $_SERVER['APP_ENV']; -$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = (int) $_SERVER['APP_DEBUG'] || filter_var($_SERVER['APP_DEBUG'], FILTER_VALIDATE_BOOLEAN) ? '1' : '0'; + +if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? $_ENV['TRUSTED_PROXIES'] ?? false) { + Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST); +} + +if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? $_ENV['TRUSTED_HOSTS'] ?? false) { + Request::setTrustedHosts([$trustedHosts]); +} + +$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); +$request = Request::createFromGlobals(); +$response = $kernel->handle($request); +$response->send(); +$kernel->terminate($request, $response); diff --git a/src/Command/DropExpiredPasteCommand.php b/src/Command/DropExpiredPasteCommand.php index 91c5db9..de28607 100644 --- a/src/Command/DropExpiredPasteCommand.php +++ b/src/Command/DropExpiredPasteCommand.php @@ -3,7 +3,7 @@ namespace App\Command; use Doctrine\ORM\EntityManagerInterface; -use App\Entity\Copypaste; +use App\Entity\Paste; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -39,7 +39,7 @@ class DropExpiredPasteCommand extends Command // @todo move to repository $qb = $this->em->createQueryBuilder() - ->delete(Copypaste::class, 'c') + ->delete(Paste::class, 'c') ->where('c.dateExpire < :now') ->andWhere('c.dateExpire IS NOT NULL') ->setParameter('now', new \DateTime()); diff --git a/src/Controller/PasteController.php b/src/Controller/PasteController.php index 6d90231..9712f88 100644 --- a/src/Controller/PasteController.php +++ b/src/Controller/PasteController.php @@ -2,20 +2,21 @@ namespace App\Controller; +use Doctrine\ORM\EntityManagerInterface; use DT\Bundle\GeshiBundle\Highlighter\HighlighterInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\{Request, Response}; -use App\Entity\Copypaste; -use App\Form\CopypasteType; +use App\Entity\Paste; +use App\Form\PasteType; use \GeSHi\GeSHi; class PasteController extends AbstractController { public function createAction(Request $request): Response { - $paste = new Copypaste(); + $paste = new Paste(); $form = $this->createCreateForm($paste); $form->handleRequest($request); @@ -58,7 +59,7 @@ class PasteController extends AbstractController public function newAction(): Response { - $paste = new Copypaste(); + $paste = new Paste(); $createForm = $this->createCreateForm($paste); return $this->render('Paste/new.html.twig', [ @@ -67,12 +68,10 @@ class PasteController extends AbstractController ]); } - public function showAction(int $id, ?string $secret, HighlighterInterface $highlighter): Response + public function showAction(int $id, ?string $secret, EntityManagerInterface $em, HighlighterInterface $highlighter): Response { - $em = $this->getDoctrine()->getManager(); - - /* @var $paste \App\Entity\Copypaste */ - $paste = $em->getRepository(Copypaste::class)->findOneBy([ + /* @var $paste \App\Entity\Paste */ + $paste = $em->getRepository(Paste::class)->findOneBy([ 'id' =>$id, 'secret' => $secret ]); @@ -99,7 +98,7 @@ class PasteController extends AbstractController { $em = $this->getDoctrine()->getManager(); - $pastes = $em->getRepository(Copypaste::class)->findBy( + $pastes = $em->getRepository(Paste::class)->findBy( ['secret' => null], ['id' => 'DESC'], // @todo move to the config @@ -109,9 +108,9 @@ class PasteController extends AbstractController return $this->render('sidebar.html.twig', ['pastes' => $pastes]); } - private function createCreateForm(Copypaste $entity): FormInterface + private function createCreateForm(Paste $entity): FormInterface { - $form = $this->createForm(CopypasteType::class, $entity, [ + $form = $this->createForm(PasteType::class, $entity, [ 'action' => $this->generateUrl('paste_create'), 'method' => 'POST', ]); diff --git a/src/Entity/Copypaste.php b/src/Entity/Paste.php similarity index 97% rename from src/Entity/Copypaste.php rename to src/Entity/Paste.php index 5e65fb6..b2b8ebd 100644 --- a/src/Entity/Copypaste.php +++ b/src/Entity/Paste.php @@ -6,14 +6,12 @@ use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** - * Copypaste - * * @ORM\Table(name="copypastes", indexes={ * @ORM\Index(name="idx_expire", columns={"date_expire"}) * }) * @ORM\Entity */ -class Copypaste +class Paste { /** * @var integer @@ -111,7 +109,7 @@ class Copypaste return $this; } - public function getText(): string + public function getText(): ?string { return $this->text; } @@ -135,7 +133,7 @@ class Copypaste return $this; } - public function getLanguage(): Language + public function getLanguage(): ?Language { return $this->language; } diff --git a/src/Form/FakeCaptchaType.php b/src/Form/FakeCaptchaType.php index 2e6750a..a1179f8 100644 --- a/src/Form/FakeCaptchaType.php +++ b/src/Form/FakeCaptchaType.php @@ -9,11 +9,6 @@ use Symfony\Component\OptionsResolver\OptionsResolver; class FakeCaptchaType extends AbstractType { - public function buildForm(FormBuilderInterface $builder, array $options) - { - - } - public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ diff --git a/src/Form/CopypasteType.php b/src/Form/PasteType.php similarity index 85% rename from src/Form/CopypasteType.php rename to src/Form/PasteType.php index de645a4..aedfcd2 100644 --- a/src/Form/CopypasteType.php +++ b/src/Form/PasteType.php @@ -2,25 +2,17 @@ namespace App\Form; -use App\Entity\Copypaste; -use App\Entity\Language; +use App\Entity\{Paste, Language}; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\Extension\Core\Type\CheckboxType; -use Symfony\Component\Form\Extension\Core\Type\ChoiceType; -use Symfony\Component\Form\Extension\Core\Type\TextareaType; -use Symfony\Component\Form\Extension\Core\Type\TextType; +use Symfony\Component\Form\Extension\Core\Type\{CheckboxType, ChoiceType, TextareaType, TextType}; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\QueryBuilder; -class CopypasteType extends AbstractType +class PasteType extends AbstractType { - /** - * @param FormBuilderInterface $builder - * @param array $options - */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder @@ -83,7 +75,7 @@ class CopypasteType extends AbstractType public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ - 'data_class' => Copypaste::class + 'data_class' => Paste::class ]); } } diff --git a/symfony.lock b/symfony.lock index a709f77..57bda98 100644 --- a/symfony.lock +++ b/symfony.lock @@ -317,6 +317,15 @@ "ref": "369b5b29dc52b2c190002825ae7ec24ab6f962dd" } }, + "symfony/validator": { + "version": "4.1", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "master", + "version": "4.1", + "ref": "0cdc982334f45d554957a6167e030482795bf9d7" + } + }, "symfony/var-dumper": { "version": "v4.2.2" }, diff --git a/templates/Form/fields.html.twig b/templates/Form/fields.html.twig index dec8ba7..9420c54 100644 --- a/templates/Form/fields.html.twig +++ b/templates/Form/fields.html.twig @@ -1,4 +1,4 @@ -{% block copypaste_fake_captcha_widget %} +{% block fake_captcha_widget %} {% spaceless %}
+ {% endblock %} {%- block header -%} diff --git a/templates/layout.html.twig b/templates/layout.html.twig index 7755374..ad7a4cb 100644 --- a/templates/layout.html.twig +++ b/templates/layout.html.twig @@ -4,7 +4,7 @@ {%- block head -%}