app/Plugin/CustomerSupportPro42/Form/Type/ContactCommentType.php line 31

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the ContactManagement Plugin
  4.  *
  5.  * Copyright (C) 2020 Diezon.
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Plugin\CustomerSupportPro42\Form\Type;
  11. use Eccube\Common\EccubeConfig;
  12. use Symfony\Component\Form\AbstractType;
  13. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  14. use Symfony\Component\Form\Extension\Core\Type\FileType;
  15. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  16. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  17. use Symfony\Component\Form\FormBuilderInterface;
  18. use Symfony\Component\Form\FormError;
  19. use Symfony\Component\Form\FormEvent;
  20. use Symfony\Component\Form\FormEvents;
  21. use Symfony\Component\Form\FormInterface;
  22. use Symfony\Component\OptionsResolver\OptionsResolver;
  23. use Symfony\Component\Validator\Constraints as Assert;
  24. use Plugin\CustomerSupportPro42\Entity\ContactComment;
  25. use Eccube\Form\Validator\TwigLint;
  26. class ContactCommentType extends AbstractType
  27. {
  28.     /**
  29.      * @var EccubeConfig
  30.      */
  31.     protected $eccubeConfig;
  32.     /**
  33.      * ContactCommentType constructor.
  34.      *
  35.      * @param EccubeConfig $eccubeConfig
  36.      */
  37.     public function __construct(
  38.         EccubeConfig $eccubeConfig
  39.     ) {
  40.         $this->eccubeConfig $eccubeConfig;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function buildForm(FormBuilderInterface $builder, array $options)
  46.     {
  47.         $builder
  48.             ->add('comment'TextAreaType::class, [
  49.                 'constraints' => [
  50.                     new Assert\NotBlank(),
  51.                     new Assert\Length(['max' => $this->eccubeConfig['eccube_ltextarea_len']]),
  52.                     new TwigLint(),
  53.                 ],
  54.             ])
  55.             ->add('image_file_upload_1'FileType::class, [
  56.                 'required' => false,
  57.                 'mapped' => false,
  58.             ])
  59.             ->add('image_file_upload_2'FileType::class, [
  60.                 'required' => false,
  61.                 'mapped' => false,
  62.             ])
  63.             ->add('image_name_1'HiddenType::class, [
  64.                 'mapped' => false,
  65.                 'required' => false,
  66.             ])
  67.             ->add('image_name_2'HiddenType::class, [
  68.                 'mapped' => false,
  69.                 'required' => false,
  70.             ])
  71.             ->add('delete_images'CollectionType::class, [
  72.                 'entry_type' => HiddenType::class,
  73.                 'prototype' => true,
  74.                 'mapped' => false,
  75.                 'allow_add' => true,
  76.                 'allow_delete' => true,
  77.             ]);
  78.         $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  79.             /** @var FormInterface $form */
  80.             $form $event->getForm();
  81.             $this->validateFilePath($form->get('image_name_1'), $this->eccubeConfig['eccube_temp_image_contact_comment_dir']);
  82.             $this->validateFilePath($form->get('image_name_2'), $this->eccubeConfig['eccube_temp_image_contact_comment_dir']);
  83.         });
  84.     }
  85.     /**
  86.      * 指定したディレクトリ以下のパスかどうかを確認。
  87.      *
  88.      * @param $form FormInterface
  89.      * @param $dir string
  90.      */
  91.     private function validateFilePath($form$dir)
  92.     {
  93.         $fileName $form->getData();
  94.         if ($fileName) {
  95.             $topDirPath realpath($dir);
  96.             $filePath realpath($dir.'/'.$fileName);
  97.             if (strpos($filePath$topDirPath) !== || $filePath === $topDirPath) {
  98.                 if ($form->getName() == 'image_name_1') {
  99.                     $rootForm $form->getRoot();
  100.                     if ($rootForm->getName() == 'contact') {
  101.                         $rootForm['ContactComment']['image_file_upload_1']->addError(new FormError('画像のパスが不正です。'));
  102.                     } else {
  103.                         $rootForm['image_file_upload_1']->addError(new FormError('画像のパスが不正です。'));
  104.                     }
  105.                 } else {
  106.                     $rootForm $form->getRoot();
  107.                     if ($rootForm->getName() == 'contact') {
  108.                         $rootForm['ContactComment']['image_file_upload_2']->addError(new FormError('画像のパスが不正です。'));
  109.                     } else {
  110.                         $rootForm['image_file_upload_2']->addError(new FormError('画像のパスが不正です。'));
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.     }
  116.     /**
  117.      * {@inheritdoc}
  118.      */
  119.     public function configureOptions(OptionsResolver $resolver)
  120.     {
  121.         $resolver->setDefaults([
  122.             'data_class' => ContactComment::class,
  123.         ]);
  124.     }
  125.     /**
  126.      * {@inheritdoc}
  127.      */
  128.     public function getBlockPrefix()
  129.     {
  130.         return 'contact_comment';
  131.     }
  132. }