src/Controller/DownloadController.php line 232

  1. <?php
  2. namespace App\Controller;
  3. use App\AppHelper\Helper;
  4. use App\AppHelper\Settings;
  5. use App\Entity\ApiLog;
  6. use App\Entity\DatabaseBackups;
  7. use App\Entity\Patch;
  8. use App\Entity\Product;
  9. use App\Entity\SystemSettings;
  10. use App\Entity\UpdateChecker;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  22. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  23. use Symfony\Component\Serializer\Serializer;
  24. use Symfony\Contracts\Translation\TranslatorInterface;
  25. #[Route('/download'name'app_download')]
  26. class DownloadController extends AbstractController
  27. {
  28.     use Settings;
  29.     public function __construct(
  30.         private readonly TranslatorInterface    $translator,
  31.         private readonly EntityManagerInterface $em,
  32.     )
  33.     {
  34.     }
  35.     /**
  36.      * @param DatabaseBackups $databaseBackups
  37.      * @return BinaryFileResponse|void
  38.      */
  39.     #[IsGranted('ROLE_ADMIN')]
  40.     #[Route('/{id}/sql-dump'name'_sql_dump')]
  41.     public function download_sql_dump(DatabaseBackups $databaseBackups)
  42.     {
  43.         $file $this->getParameter('sql_backup_dir') . '/' $databaseBackups->getDescription();
  44.         if (is_file($file)) {
  45.             return new BinaryFileResponse($file);
  46.         }
  47.     }
  48.     /**
  49.      * @param $type
  50.      * @param $dezimal
  51.      * @return BinaryFileResponse|void
  52.      */
  53.     #[IsGranted('ROLE_ADMIN')]
  54.     #[Route('/icons/{type}/{dezimal}'name'_icon_download')]
  55.     public function icon_download($type$dezimal)
  56.     {
  57.         $helper Helper::instance();
  58.         $filename '';
  59.         switch ($type) {
  60.             case 'fa47':
  61.                 $fileDir $this->getParameter('fa47_icon_dir') . DIRECTORY_SEPARATOR;
  62.                 $svgDir $fileDir 'icons' DIRECTORY_SEPARATOR;
  63.                 if (is_file($fileDir 'fa-icons.json')) {
  64.                     $faJson json_decode(file_get_contents($fileDir 'fa-icons.json'), true);
  65.                     foreach ($faJson as $tmp) {
  66.                         if (dechex((int)$dezimal) === $tmp['code']) {
  67.                             $filename $tmp['title'] . '.svg';
  68.                             break;
  69.                         }
  70.                     }
  71.                 }
  72.                 if ($filename) {
  73.                     $file $svgDir $filename;
  74.                     if (is_file($file)) {
  75.                         $response = new BinaryFileResponse($file);
  76.                         $response->setContentDisposition(
  77.                             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  78.                             basename($file)
  79.                         );
  80.                         return $response;
  81.                     }
  82.                 }
  83.                 break;
  84.             case'fa640':
  85.                 $raw '';
  86.                 $fileDir $this->getParameter('fa640_icon_dir') . DIRECTORY_SEPARATOR;
  87.                 if (is_dir($fileDir 'icon')) {
  88.                     $helper->recursive_destroy_dir($fileDir 'icon');
  89.                 }
  90.                 if (is_file($fileDir 'fa640.json')) {
  91.                     $faJson json_decode(file_get_contents($fileDir 'fa640.json'), true);
  92.                     foreach ($faJson as $tmp) {
  93.                         if (dechex((int)$dezimal) === $tmp['hex']) {
  94.                             $filename $tmp['icon'] . '.svg';
  95.                             $raw $tmp['raw'];
  96.                             break;
  97.                         }
  98.                     }
  99.                 }
  100.                 if ($filename && $raw) {
  101.                     if (!is_dir($fileDir 'icon')) {
  102.                         mkdir($fileDir 'icon');
  103.                     }
  104.                     $file $fileDir 'icon' DIRECTORY_SEPARATOR $filename;
  105.                     file_put_contents($file$raw);
  106.                     if (is_file($file)) {
  107.                         $response = new BinaryFileResponse($file);
  108.                         $response->setContentDisposition(
  109.                             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  110.                             basename($file)
  111.                         );
  112.                         return $response;
  113.                     }
  114.                 }
  115.                 break;
  116.             case 'bs':
  117.                 $fileDir $this->getParameter('bs_icon_dir') . DIRECTORY_SEPARATOR 'font' DIRECTORY_SEPARATOR;
  118.                 $svgDir $this->getParameter('bs_icon_dir') . DIRECTORY_SEPARATOR 'icons' DIRECTORY_SEPARATOR;
  119.                 if (is_file($fileDir 'bootstrap-icons.json')) {
  120.                     $bsJson json_decode(file_get_contents($fileDir 'bootstrap-icons.json'), true);
  121.                     foreach ($bsJson as $key => $val) {
  122.                         if ((int)$val === (int)$dezimal) {
  123.                             $filename $key '.svg';
  124.                             break;
  125.                         }
  126.                     }
  127.                 }
  128.                 if ($filename) {
  129.                     $file $svgDir $filename;
  130.                     if (is_file($file)) {
  131.                         $response = new BinaryFileResponse($file);
  132.                         $response->setContentDisposition(
  133.                             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  134.                             basename($file)
  135.                         );
  136.                         return $response;
  137.                     }
  138.                 }
  139.                 break;
  140.             case'md':
  141.                 $url '';
  142.                 $name '';
  143.                 $fileDir $this->getParameter('material_design_icon_dir') . DIRECTORY_SEPARATOR;
  144.                 $helper->recursive_destroy_dir($fileDir 'icon');
  145.                 if (is_file($fileDir 'material-design.json')) {
  146.                     $mdJson json_decode(file_get_contents($fileDir 'material-design.json'), true);
  147.                     foreach ($mdJson as $tmp) {
  148.                         if ($tmp['dezimal'] === (int)$dezimal) {
  149.                             $name $tmp['name'];
  150.                             $url sprintf('https://raw.githubusercontent.com/google/material-design-icons/master/src/%s/%s/materialicons/24px.svg'$tmp['category'], $tmp['name']);
  151.                         }
  152.                     }
  153.                 }
  154.                 if ($url && $helper->url_check($url)) {
  155.                     $icon file_get_contents($url);
  156.                     if ($icon) {
  157.                         if (!is_dir($fileDir 'icon')) {
  158.                             mkdir($fileDir 'icon');
  159.                         }
  160.                         $newFile $fileDir 'icon' DIRECTORY_SEPARATOR $name '.svg';
  161.                         file_put_contents($newFile$icon);
  162.                         $response = new BinaryFileResponse($newFile);
  163.                         $response->setContentDisposition(
  164.                             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  165.                             basename($newFile)
  166.                         );
  167.                         return $response;
  168.                     }
  169.                 }
  170.                 break;
  171.         }
  172.     }
  173.     /**
  174.      * @param Patch $patch
  175.      * @return BinaryFileResponse|void
  176.      */
  177.     #[IsGranted('ROLE_ADMIN')]
  178.     #[Route('/{id}/patch'name'_patch')]
  179.     public function download_patch(Patch $patch)
  180.     {
  181.         $file $this->getParameter('patch_dir') . DIRECTORY_SEPARATOR $patch->getPatchBezeichnung() . '.zip';
  182.         if (is_file($file)) {
  183.             $response = new BinaryFileResponse($file);
  184.             $response->setContentDisposition(
  185.                 ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  186.                 $patch->getPatchBezeichnung() . '.zip'
  187.             );
  188.             return $response;
  189.         }
  190.     }
  191.     /**
  192.      * @param Product $product
  193.      * @return BinaryFileResponse|void
  194.      */
  195.    // #[IsGranted('ROLE_ADMIN')]
  196.    // #[IsGranted('ROLE_OAUTH2_PRODUCT')]
  197.    // /**
  198.    //  * @Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_OAUTH2_PRODUCT')")
  199.    //  */
  200.     #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_OAUTH2_PRODUCT')")]
  201.     #[Route('/{id}/product'name'_download_product')]
  202.     public function download_product(Product $product)
  203.     {
  204.             $file $this->getParameter('product_dir') . '/' $product->getSlug() . '/' $product->getSlug() . '.zip';
  205.             if (is_file($file)) {
  206.                 $response = new BinaryFileResponse($file);
  207.                 $response->setContentDisposition(
  208.                     ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  209.                     $product->getSlug() . '.zip'
  210.                 );
  211.                 return $response;
  212.             }
  213.     }
  214.     /**
  215.      * @param Request $request
  216.      * @return BinaryFileResponse|Response
  217.      */
  218.     #[Route('/{slug}/update'name'_download_update')]
  219.     public function download_update(Request $request)
  220.     {
  221.         $file $this->getParameter('product_dir') . '/' $request->get('slug') . '/' $request->get('slug') . '.zip';
  222.         if (is_file($file)) {
  223.             $response = new BinaryFileResponse($file);
  224.             $response->setContentDisposition(
  225.                 ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  226.                 $request->get('slug') . '.zip'
  227.             );
  228.             return $response;
  229.         } else {
  230.             return $this->render('main/404.html.twig', [
  231.             ]);
  232.         }
  233.     }
  234.     #[Route('/{slug}/{logId}/{pt}/update-json'name'_update_json')]
  235.     public function product_update_json(Request $request): JsonResponse
  236.     {
  237.         $product $this->em->getRepository(Product::class)->findOneBy(['slug' => $request->get('slug')]);
  238.         if (!$product->getUpdateChecker()) {
  239.             return new JsonResponse(
  240.                 (object)[],
  241.                 403);
  242.         }
  243.         $apiLog $this->em->getRepository(ApiLog::class)->find($request->get('logId'));
  244.         if (!$apiLog) {
  245.             $updateAktiv 0;
  246.         } else {
  247.             $updateAktiv $apiLog->getUpdateAktiv();
  248.         }
  249.         $update_config json_decode($product->getUpdateChecker()->getUpdateData(), true);
  250.         if ($product->getUpdateChecker()->getType() == 'plugin') {
  251.             if (!$product->getUpdateChecker()->isRatingAktiv()) {
  252.                 unset($update_config['rating']);
  253.                 unset($update_config['num_ratings']);
  254.                 unset($update_config['downloaded']);
  255.                 unset($update_config['active_installs']);
  256.             }
  257.             if (!$product->getUpdateChecker()->isLanguageAktiv()) {
  258.                 unset($update_config['translations']);
  259.             }
  260.         }
  261.         $selfUrl $request->getSchemeAndHttpHost();
  262.         if ($request->get('pt') == 1) {
  263.             $data = [
  264.                 'is_git' => (int)$product->isIsGit(),
  265.                 'update_aktiv' => $updateAktiv,
  266.                 'self' => $selfUrl,
  267.                 'git_config' => json_decode($product->getUpdateChecker()->getGitData()),
  268.                 'update_config' => $update_config
  269.             ];
  270.         } else {
  271.             $data $update_config;
  272.         }
  273.         return new JsonResponse(
  274.             $data,
  275.             200);
  276.     }
  277. }