src/Controller/MainController.php line 63

  1. <?php
  2. namespace App\Controller;
  3. use App\AppHelper\Helper;
  4. use App\AppHelper\Settings;
  5. use App\Entity\AppSites;
  6. use App\Entity\EmailTemplates;
  7. use App\Entity\MediaCategory;
  8. use App\Entity\Mediathek;
  9. use App\Entity\SystemSettings;
  10. use App\Entity\User;
  11. use App\Entity\UserDetails;
  12. use App\MakeQrcode\ImageWithLogo;
  13. use chillerlan\QRCode\{QRCodeQROptions};
  14. use Doctrine\DBAL\Exception;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\RequestStack;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\HttpKernel\KernelInterface;
  23. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Symfony\Component\Uid\Ulid;
  26. use Symfony\Component\Uid\UuidV1;
  27. use Symfony\Contracts\Translation\TranslatorInterface;
  28. use Ramsey\Uuid\Uuid;
  29. class MainController extends AbstractController
  30. {
  31.     use Settings;
  32.     private array $site_settings;
  33.     public function __construct(
  34.         private readonly EntityManagerInterface $em,
  35.         private readonly KernelInterface $kernel,
  36.         private RequestStack $requestStack,
  37.         private readonly TranslatorInterface $translator,
  38.         private readonly ImageWithLogo $imageWithLogo
  39.     )
  40.     {
  41.         $this->requestStack $requestStack;
  42.         $ss $this->em->getRepository(SystemSettings::class)->getSettings('site_settings');
  43.         if ($ss && isset($ss->record)) {
  44.             $this->site_settings $ss->record;
  45.         } else {
  46.             $this->site_settings = [];
  47.         }
  48.     }
  49.     #[Route('/nelmio/csp/report'name'app_csp_report')]
  50.     public function csp_report(Request $request): Response
  51.     {
  52.         return $this->render('main/404.html.twig', [
  53.         ]);
  54.     }
  55.     #[Route('/'name'app_main')]
  56.     public function index_main(Request $requestUserPasswordHasherInterface $passEncoder): Response
  57.     {
  58.       //  dd($this->getParameter('projectDir'),$_SERVER, $this->getParameter('projectDir'));
  59.        $this->imageWithLogo->make_qrcode_with_logo();
  60.        // dd($result);
  61.        // return new Response($result, 200, ['Content-Type' => 'image/png']);
  62.         $uuid = new Ulid();
  63.         //dd($uuid->toRfc4122());
  64.         $locale $request->getLocale();
  65.         // $request->setLocale('de');
  66.         $u $this->em->getRepository(UserDetails::class)->findAll();
  67.         if (!$u) {
  68.             $this->set_default_settings($request$passEncoder$request->getUri());
  69.         }
  70.         $siteSettings $this->em->getRepository(SystemSettings::class)->getSettings('site_settings');
  71.         if (strtolower(php_uname('s')) == 'linux'){
  72.             $dev '> /dev/null 2>&1';
  73.         } else {
  74.             $dev '';
  75.         }
  76.         $dirSrc $this->getParameter('projectDir');
  77.         $command sprintf('php %s/bin/console league:oauth2-server:clear-expired-tokens %s'$dirSrc$dev);
  78.         passthru($command);
  79.         return $this->render('main/index.html.twig', [
  80.             'settings' => $siteSettings->record,
  81.             'title' => $this->translator->trans('Homepage'),
  82.             'testImg' => ''
  83.         ]);
  84.     }
  85.     #[Route('/site/{slug}'name'site_slug')]
  86.     public function get_public_site(Request $request): Response
  87.     {
  88.         $slug $request->get('slug');
  89.         $site $this->em->getRepository(AppSites::class)->findOneBy(['site_slug' => $slug]);
  90.         if (!$site) {
  91.             return $this->render('main/404.html.twig', [
  92.             ]);
  93.         }
  94.         return $this->render('main/public-site-by-slug.html.twig', [
  95.             'title' => $site->getSiteTitle(),
  96.             'site_slug' => $site->getSiteSlug(),
  97.             'site_content' => html_entity_decode($site->getSiteContent()),
  98.             'site_excerpt' => html_entity_decode($site->getSiteExcerpt()),
  99.             'site_status' => $site->getSiteStatus(),
  100.             'site_comment_status' => $site->isSiteCommentStatus(),
  101.             'site_type' => $site->getSiteType(),
  102.             'site_created' => $site->getCreatedAt(),
  103.         ]);
  104.     }
  105.     /**
  106.      * @param $size
  107.      * @param Request $request
  108.      * @param Mediathek $mediathek
  109.      * @return BinaryFileResponse|void
  110.      */
  111.     #[Route('/media{id}/{size}'name'media_image')]
  112.     public function get_media_file($sizeRequest $requestMediathek $mediathek)
  113.     {
  114.         $dtaType $mediathek->getType();
  115.         $filename $mediathek->getBezeichnung();
  116.         if ($dtaType == 'data') {
  117.             $path 'data';
  118.         } else {
  119.             $path $size;
  120.         }
  121.         $file $this->getParameter('uploads') . '/' $path '/' $filename;
  122.         if (is_file($file)) {
  123.             return new BinaryFileResponse($file);
  124.         }
  125.     }
  126.     #[Route('/dashboard'name'app_app_main')]
  127.     #[IsGranted('ROLE_USER')]
  128.     public function app_main(Request $request): Response
  129.     {
  130.         $hasAccess $this->isGranted('ROLE_ADMIN');
  131.         if ($hasAccess) {
  132.             return $this->redirect($this->generateUrl('dashboard_app_dashboard'));
  133.         }
  134.         $hasAccess $this->isGranted('ROLE_USER');
  135.         if ($hasAccess) {
  136.             return $this->redirect($this->generateUrl('app_profile_profile'));
  137.         }
  138.         return $this->redirect($this->generateUrl('app_main'));
  139.     }
  140.     /**
  141.      * @throws Exception
  142.      */
  143.     private function set_default_settings($request$passEncoder$uri): void
  144.     {
  145.         //TODO set default settings
  146.         $defaultSettings $this->get_app_default_settings();
  147.         $ifSettings $this->em->getRepository(SystemSettings::class)->findAll();
  148.         if (!$ifSettings) {
  149.             $settings = new SystemSettings();
  150.             $settings->setSettingsId(SystemSettingsController::SETTINGS_ID);
  151.             $settings->setEmailSettings(['data' => json_encode($defaultSettings['email_settings'])]);
  152.             $settings->setUploadSettings(['data' => json_encode($defaultSettings['upload_settings'])]);
  153.             $settings->setSiteSettings(['data' => json_encode($defaultSettings['site_settings'])]);
  154.             $settings->setOauthServerSettings(['data' => json_encode($defaultSettings['oauth_server_settings'])]);
  155.             $settings->setHeader(['data' => json_encode($defaultSettings['header'])]);
  156.             $this->em->persist($settings);
  157.             $this->em->flush();
  158.         }
  159.         $isMediaCat $this->em->getRepository(MediaCategory::class)->findAll();
  160.         if (!$isMediaCat) {
  161.             $mediaCat = new MediaCategory();
  162.             $mediaCat->setPosition(0);
  163.             $mediaCat->setBezeichnung('Allgemein');
  164.             $mediaCat->setDescription('Allgemeine Kategorie');
  165.             $mediaCat->setFirstCat(1);
  166.             $this->em->persist($mediaCat);
  167.             $this->em->flush();
  168.         }
  169.         $helper Helper::instance();
  170.         $tempDir $this->getParameter('email_templates_dir');
  171.         $eTemplates = [
  172.             '0' => [
  173.                 'bezeichnung' => 'Aktivierungslink',
  174.                 'subject' => 'Konto aktivieren',
  175.                 'type' => 'system',
  176.                 'content' => htmlspecialchars($helper->replace_template(file_get_contents($tempDir 'aktivierungslink.html'))),
  177.                 'position' => 1
  178.             ],
  179.             '1' => [
  180.                 'bezeichnung' => 'Link zum Passwort erstellen',
  181.                 'subject' => 'Passwort erstellen',
  182.                 'type' => 'system',
  183.                 'content' => htmlspecialchars($helper->replace_template(file_get_contents($tempDir 'link-zum-pw-erstellen.html'))),
  184.                 'position' => 2
  185.             ],
  186.             '2' => [
  187.                 'bezeichnung' => 'E-Mail geändert (Bestätigung)',
  188.                 'subject' => 'E-Mail bestätigen',
  189.                 'type' => 'system',
  190.                 'content' => htmlspecialchars($helper->replace_template(file_get_contents($tempDir 'email-change.html'))),
  191.                 'position' => 3
  192.             ],
  193.             '3' => [
  194.                 'bezeichnung' => 'Passwort vergessen',
  195.                 'subject' => 'neues Passwort erstellen',
  196.                 'type' => 'system',
  197.                 'content' => htmlspecialchars($helper->replace_template(file_get_contents($tempDir 'passwort-vergessen.html'))),
  198.                 'position' => 4
  199.             ],
  200.             '4' => [
  201.                 'bezeichnung' => 'Signatur',
  202.                 'subject' => 'Signatur',
  203.                 'type' => 'signature',
  204.                 'content' => htmlspecialchars($helper->replace_template(file_get_contents($tempDir 'signature-default.html'))),
  205.                 'position' => 5
  206.             ],
  207.             '5' => [
  208.                 'bezeichnung' => 'Autoresponder',
  209.                 'subject' => 'Automatische Antwort',
  210.                 'type' => 'responder',
  211.                 'content' => 'Autoresponder',
  212.                 'position' => 6
  213.             ],
  214.         ];
  215.         $isEmailTemplate $this->em->getRepository(EmailTemplates::class)->findAll();
  216.         if (!$isEmailTemplate) {
  217.             foreach ($eTemplates as $tmp) {
  218.                 $emailTemplate = new EmailTemplates();
  219.                 $emailTemplate->setBezeichnung($tmp['bezeichnung']);
  220.                 $emailTemplate->setType($tmp['type']);
  221.                 $emailTemplate->setSubject($tmp['subject']);
  222.                 $emailTemplate->setContent($tmp['content']);
  223.                 $emailTemplate->setPosition($tmp['position']);
  224.                 $this->em->persist($emailTemplate);
  225.                 $this->em->flush();
  226.             }
  227.         }
  228.         $uuid = new Ulid();
  229.         $id $uuid->toBase32();
  230.         $clientId $helper->generate_identifier($id);
  231.         //$clientId = $helper->generate_callback_pw(32,0,16);
  232.         //Todo set SU-Admin
  233.         $suUserDetails = new UserDetails();
  234.         $suUserDetails->setRegisterIp($request->getClientIp());
  235.         $suUserDetails->setChangePw(true);
  236.         $suUserDetails->setAktiv(true);
  237.         $suUserDetails->setMustValidated(0);
  238.         $this->em->persist($suUserDetails);
  239.         $this->em->flush();
  240.         $uuid1 Uuid::uuid1();
  241.         $suUser = new User();
  242.         $suUser->setRoles(['ROLE_ADMIN''ROLE_SUPER_ADMIN']);
  243.         $suUser->setPassword(
  244.             $passEncoder->hashPassword($suUser$this->su_administrator_passwort)
  245.         );
  246.         $suUser->setUserdetails($suUserDetails);
  247.         $suUser->setEmail($this->su_administrator_email);
  248.         $suUser->setUuid(new UuidV1());
  249.         $this->em->persist($suUser);
  250.         $this->em->flush($suUser);
  251.         // Todo Set oAuth2
  252.         $clientSecret $helper->generate_callback_pw(128064);
  253.         $scopes = ['SUPER_USER'];
  254.         $grantTypes = ['authorization_code''refresh_token''client_credentials'];
  255.         $redirectUris = ['http://localhost:8080/callback'$uri 'intern-callback'];
  256.         $conn $this->em->getConnection();
  257.         $conn->insert('oauth2_client', [
  258.             'identifier' => $clientId,
  259.             'secret' => $clientSecret,
  260.             'name' => $suUser->getEmail(),
  261.             'redirect_uris' => implode(' ',$redirectUris),
  262.             'grants' => implode(' '$grantTypes),
  263.             'scopes' => implode(' '$scopes),
  264.             'active' => 1,
  265.             'allow_plain_text_pkce' => 0,
  266.         ]);
  267.         $uuid = new Ulid();
  268.         $id $uuid->toBase32();
  269.         $clientId $helper->generate_identifier($id);
  270.         //$clientId = $helper->generate_callback_pw(32,0,16);
  271.         //Todo set Admin
  272.         $userDetails = new UserDetails();
  273.         $userDetails->setRegisterIp($request->getClientIp());
  274.         $userDetails->setChangePw(true);
  275.         $userDetails->setAktiv(true);
  276.         $userDetails->setMustValidated(0);
  277.         $this->em->persist($userDetails);
  278.         $this->em->flush();
  279.         $uuid1 Uuid::uuid1();
  280.         $user = new User();
  281.         $user->setRoles(['ROLE_ADMIN']);
  282.         $user->setPassword(
  283.             $passEncoder->hashPassword($user$this->administrator_passwort)
  284.         );
  285.         $user->setUserdetails($userDetails);
  286.         $user->setEmail($this->administrator_email);
  287.         $user->setUuid(new UuidV1());
  288.         $this->em->persist($user);
  289.         $this->em->flush($user);
  290.         // Todo Set oAuth2
  291.         $clientSecret $helper->generate_callback_pw(128064);
  292.         $scopes = ['ADMIN'];
  293.         //$redirectUris = ['http://localhost:8080/callback', $uri . 'intern-callback'];
  294.         $conn $this->em->getConnection();
  295.         $conn->insert('oauth2_client', [
  296.             'identifier' => $clientId,
  297.             'secret' => $clientSecret,
  298.             'name' => $user->getEmail(),
  299.             'redirect_uris' => implode(' '$redirectUris),
  300.             'grants' => implode(' '$grantTypes),
  301.             'scopes' => implode(' '$scopes),
  302.             'active' => 1,
  303.             'allow_plain_text_pkce' => 0,
  304.         ]);
  305.     }
  306.     public function get_public_pages($id NULL): array
  307.     {
  308.         $public = [
  309.             '0' => [
  310.                 'id' => 1,
  311.                 'route_name' => '_public_agb',
  312.                 'route' => '/agb',
  313.                 'template' => 'agb.html.twig',
  314.                 'name' => $this->translator->trans('AGB')
  315.             ],
  316.             '1' => [
  317.                 'id' => 2,
  318.                 'route_name' => '_public_impressum',
  319.                 'route' => '/impressum',
  320.                 'template' => 'impressum.html.twig',
  321.                 'name' => $this->translator->trans('Imprint')
  322.             ],
  323.             '2' => [
  324.                 'id' => 3,
  325.                 'route_name' => '_public_datenschutz',
  326.                 'route' => '/datenschutz',
  327.                 'template' => 'datenschutz.html.twig',
  328.                 'name' => $this->translator->trans('Privacy')
  329.             ],
  330.         ];
  331.         if ($id) {
  332.             foreach ($public as $tmp) {
  333.                 if ($tmp['id'] == $id) {
  334.                     return $tmp;
  335.                 }
  336.             }
  337.         }
  338.         return $public;
  339.     }
  340. }