src/Controller/MainController.php line 69

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