vendor/pimcore/pimcore/bundles/AdminBundle/Controller/Admin/SettingsController.php line 70

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\AdminBundle\Controller\Admin;
  15. use Pimcore\Bundle\AdminBundle\Controller\AdminController;
  16. use Pimcore\Cache;
  17. use Pimcore\Cache\Core\CoreCacheHandler;
  18. use Pimcore\Cache\Symfony\CacheClearer;
  19. use Pimcore\Config;
  20. use Pimcore\Event\SystemEvents;
  21. use Pimcore\File;
  22. use Pimcore\Helper\StopMessengerWorkersTrait;
  23. use Pimcore\Localization\LocaleServiceInterface;
  24. use Pimcore\Model;
  25. use Pimcore\Model\Asset;
  26. use Pimcore\Model\Document;
  27. use Pimcore\Model\Element;
  28. use Pimcore\Model\Exception\ConfigWriteException;
  29. use Pimcore\Model\Glossary;
  30. use Pimcore\Model\Metadata;
  31. use Pimcore\Model\Property;
  32. use Pimcore\Model\Staticroute;
  33. use Pimcore\Model\Tool\SettingsStore;
  34. use Pimcore\Model\WebsiteSetting;
  35. use Pimcore\Tool;
  36. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  37. use Symfony\Component\EventDispatcher\GenericEvent;
  38. use Symfony\Component\Filesystem\Filesystem;
  39. use Symfony\Component\HttpFoundation\JsonResponse;
  40. use Symfony\Component\HttpFoundation\Request;
  41. use Symfony\Component\HttpFoundation\Response;
  42. use Symfony\Component\HttpFoundation\StreamedResponse;
  43. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  44. use Symfony\Component\HttpKernel\KernelEvents;
  45. use Symfony\Component\HttpKernel\KernelInterface;
  46. use Symfony\Component\Routing\Annotation\Route;
  47. use Symfony\Component\Yaml\Yaml;
  48. /**
  49.  * @Route("/settings")
  50.  *
  51.  * @internal
  52.  */
  53. class SettingsController extends AdminController
  54. {
  55.     use StopMessengerWorkersTrait;
  56.     private const CUSTOM_LOGO_PATH 'custom-logo.image';
  57.     /**
  58.      * @Route("/display-custom-logo", name="pimcore_settings_display_custom_logo", methods={"GET"})
  59.      *
  60.      * @param Request $request
  61.      *
  62.      * @return StreamedResponse
  63.      */
  64.     public function displayCustomLogoAction(Request $request)
  65.     {
  66.         $mime 'image/svg+xml';
  67.         if ($request->get('white')) {
  68.             $logo PIMCORE_WEB_ROOT '/bundles/pimcoreadmin/img/logo-claim-white.svg';
  69.         } else {
  70.             $logo PIMCORE_WEB_ROOT '/bundles/pimcoreadmin/img/logo-claim-gray.svg';
  71.         }
  72.         $stream fopen($logo'rb');
  73.         $storage Tool\Storage::get('admin');
  74.         if ($storage->fileExists(self::CUSTOM_LOGO_PATH)) {
  75.             try {
  76.                 $mime $storage->mimeType(self::CUSTOM_LOGO_PATH);
  77.                 $stream $storage->readStream(self::CUSTOM_LOGO_PATH);
  78.             } catch (\Exception $e) {
  79.                 // do nothing
  80.             }
  81.         }
  82.         return new StreamedResponse(function () use ($stream) {
  83.             fpassthru($stream);
  84.         }, 200, ['Content-Type' => $mime]);
  85.     }
  86.     /**
  87.      * @Route("/upload-custom-logo", name="pimcore_admin_settings_uploadcustomlogo", methods={"POST"})
  88.      *
  89.      * @param Request $request
  90.      *
  91.      * @return JsonResponse
  92.      *
  93.      * @throws \Exception
  94.      */
  95.     public function uploadCustomLogoAction(Request $request)
  96.     {
  97.         $fileExt File::getFileExtension($_FILES['Filedata']['name']);
  98.         if (!in_array($fileExt, ['svg''png''jpg'])) {
  99.             throw new \Exception('Unsupported file format');
  100.         }
  101.         if ($fileExt === 'svg' && stripos(file_get_contents($_FILES['Filedata']['tmp_name']), '<script')) {
  102.             throw new \Exception('Scripts in SVG files are not supported');
  103.         }
  104.         $storage Tool\Storage::get('admin');
  105.         $storage->writeStream(self::CUSTOM_LOGO_PATHfopen($_FILES['Filedata']['tmp_name'], 'rb'));
  106.         // set content-type to text/html, otherwise (when application/json is sent) chrome will complain in
  107.         // Ext.form.Action.Submit and mark the submission as failed
  108.         $response $this->adminJson(['success' => true]);
  109.         $response->headers->set('Content-Type''text/html');
  110.         return $response;
  111.     }
  112.     /**
  113.      * @Route("/delete-custom-logo", name="pimcore_admin_settings_deletecustomlogo", methods={"DELETE"})
  114.      *
  115.      * @param Request $request
  116.      *
  117.      * @return JsonResponse
  118.      */
  119.     public function deleteCustomLogoAction(Request $request)
  120.     {
  121.         if (Tool\Storage::get('admin')->fileExists(self::CUSTOM_LOGO_PATH)) {
  122.             Tool\Storage::get('admin')->delete(self::CUSTOM_LOGO_PATH);
  123.         }
  124.         return $this->adminJson(['success' => true]);
  125.     }
  126.     /**
  127.      * Used by the predefined metadata grid
  128.      *
  129.      * @Route("/predefined-metadata", name="pimcore_admin_settings_metadata", methods={"POST"})
  130.      *
  131.      * @param Request $request
  132.      *
  133.      * @return JsonResponse
  134.      */
  135.     public function metadataAction(Request $request)
  136.     {
  137.         $this->checkPermission('asset_metadata');
  138.         if ($request->get('data')) {
  139.             if ($request->get('xaction') == 'destroy') {
  140.                 $data $this->decodeJson($request->get('data'));
  141.                 $id $data['id'];
  142.                 $metadata Metadata\Predefined::getById($id);
  143.                 if (!$metadata->isWriteable()) {
  144.                     throw new ConfigWriteException();
  145.                 }
  146.                 $metadata->delete();
  147.                 return $this->adminJson(['success' => true'data' => []]);
  148.             } elseif ($request->get('xaction') == 'update') {
  149.                 $data $this->decodeJson($request->get('data'));
  150.                 // save type
  151.                 $metadata Metadata\Predefined::getById($data['id']);
  152.                 if (!$metadata->isWriteable()) {
  153.                     throw new ConfigWriteException();
  154.                 }
  155.                 $metadata->setValues($data);
  156.                 $existingItem Metadata\Predefined\Listing::getByKeyAndLanguage($metadata->getName(), $metadata->getLanguage(), $metadata->getTargetSubtype());
  157.                 if ($existingItem && $existingItem->getId() != $metadata->getId()) {
  158.                     return $this->adminJson(['message' => 'rule_violation''success' => false]);
  159.                 }
  160.                 $metadata->minimize();
  161.                 $metadata->save();
  162.                 $metadata->expand();
  163.                 $responseData $metadata->getObjectVars();
  164.                 $responseData['writeable'] = $metadata->isWriteable();
  165.                 return $this->adminJson(['data' => $responseData'success' => true]);
  166.             } elseif ($request->get('xaction') == 'create') {
  167.                 if (!(new Metadata\Predefined())->isWriteable()) {
  168.                     throw new ConfigWriteException();
  169.                 }
  170.                 $data $this->decodeJson($request->get('data'));
  171.                 unset($data['id']);
  172.                 // save type
  173.                 $metadata Metadata\Predefined::create();
  174.                 $metadata->setValues($data);
  175.                 $existingItem Metadata\Predefined\Listing::getByKeyAndLanguage($metadata->getName(), $metadata->getLanguage(), $metadata->getTargetSubtype());
  176.                 if ($existingItem) {
  177.                     return $this->adminJson(['message' => 'rule_violation''success' => false]);
  178.                 }
  179.                 $metadata->save();
  180.                 $responseData $metadata->getObjectVars();
  181.                 $responseData['writeable'] = $metadata->isWriteable();
  182.                 return $this->adminJson(['data' => $responseData'success' => true]);
  183.             }
  184.         } else {
  185.             // get list of types
  186.             $list = new Metadata\Predefined\Listing();
  187.             if ($filter $request->get('filter')) {
  188.                 $list->setFilter(function (Metadata\Predefined $predefined) use ($filter) {
  189.                     foreach ($predefined->getObjectVars() as $value) {
  190.                         if (stripos($value$filter) !== false) {
  191.                             return true;
  192.                         }
  193.                     }
  194.                     return false;
  195.                 });
  196.             }
  197.             $properties = [];
  198.             foreach ($list->getDefinitions() as $metadata) {
  199.                 $data $metadata->getObjectVars();
  200.                 $data['writeable'] = $metadata->isWriteable();
  201.                 $properties[] = $data;
  202.             }
  203.             return $this->adminJson(['data' => $properties'success' => true'total' => $list->getTotalCount()]);
  204.         }
  205.         return $this->adminJson(['success' => false]);
  206.     }
  207.     /**
  208.      * @Route("/get-predefined-metadata", name="pimcore_admin_settings_getpredefinedmetadata", methods={"GET"})
  209.      *
  210.      * @param Request $request
  211.      *
  212.      * @return JsonResponse
  213.      */
  214.     public function getPredefinedMetadataAction(Request $request)
  215.     {
  216.         $type $request->get('type');
  217.         $subType $request->get('subType');
  218.         $group $request->get('group');
  219.         $list Metadata\Predefined\Listing::getByTargetType($type, [$subType]);
  220.         $result = [];
  221.         foreach ($list as $item) {
  222.             $itemGroup $item->getGroup() ?? '';
  223.             if ($group === 'default' || $group === $itemGroup) {
  224.                 $item->expand();
  225.                 $data $item->getObjectVars();
  226.                 $data['writeable'] = $item->isWriteable();
  227.                 $result[] = $data;
  228.             }
  229.         }
  230.         return $this->adminJson(['data' => $result'success' => true]);
  231.     }
  232.     /**
  233.      * @Route("/properties", name="pimcore_admin_settings_properties", methods={"POST"})
  234.      *
  235.      * @param Request $request
  236.      *
  237.      * @return JsonResponse
  238.      */
  239.     public function propertiesAction(Request $request)
  240.     {
  241.         if ($request->get('data')) {
  242.             $this->checkPermission('predefined_properties');
  243.             if ($request->get('xaction') == 'destroy') {
  244.                 $data $this->decodeJson($request->get('data'));
  245.                 $id $data['id'];
  246.                 $property Property\Predefined::getById($id);
  247.                 if (!$property->isWriteable()) {
  248.                     throw new ConfigWriteException();
  249.                 }
  250.                 $property->delete();
  251.                 return $this->adminJson(['success' => true'data' => []]);
  252.             } elseif ($request->get('xaction') == 'update') {
  253.                 $data $this->decodeJson($request->get('data'));
  254.                 // save type
  255.                 $property Property\Predefined::getById($data['id']);
  256.                 if (!$property->isWriteable()) {
  257.                     throw new ConfigWriteException();
  258.                 }
  259.                 if (is_array($data['ctype'])) {
  260.                     $data['ctype'] = implode(','$data['ctype']);
  261.                 }
  262.                 $property->setValues($data);
  263.                 $property->save();
  264.                 $responseData $property->getObjectVars();
  265.                 $responseData['writeable'] = $property->isWriteable();
  266.                 return $this->adminJson(['data' => $responseData'success' => true]);
  267.             } elseif ($request->get('xaction') == 'create') {
  268.                 if (!(new Property\Predefined())->isWriteable()) {
  269.                     throw new ConfigWriteException();
  270.                 }
  271.                 $data $this->decodeJson($request->get('data'));
  272.                 unset($data['id']);
  273.                 // save type
  274.                 $property Property\Predefined::create();
  275.                 $property->setValues($data);
  276.                 $property->save();
  277.                 $responseData $property->getObjectVars();
  278.                 $responseData['writeable'] = $property->isWriteable();
  279.                 return $this->adminJson(['data' => $responseData'success' => true]);
  280.             }
  281.         } else {
  282.             // get list of types
  283.             $list = new Property\Predefined\Listing();
  284.             if ($filter $request->get('filter')) {
  285.                 $list->setFilter(function (Property\Predefined $predefined) use ($filter) {
  286.                     foreach ($predefined->getObjectVars() as $value) {
  287.                         if ($value) {
  288.                             $cellValues is_array($value) ? $value : [$value];
  289.                             foreach ($cellValues as $cellValue) {
  290.                                 if (stripos($cellValue$filter) !== false) {
  291.                                     return true;
  292.                                 }
  293.                             }
  294.                         }
  295.                     }
  296.                     return false;
  297.                 });
  298.             }
  299.             $properties = [];
  300.             foreach ($list->getProperties() as $property) {
  301.                 $data $property->getObjectVars();
  302.                 $data['writeable'] = $property->isWriteable();
  303.                 $properties[] = $data;
  304.             }
  305.             return $this->adminJson(['data' => $properties'success' => true'total' => $list->getTotalCount()]);
  306.         }
  307.         return $this->adminJson(['success' => false]);
  308.     }
  309.     /**
  310.      * @Route("/get-system", name="pimcore_admin_settings_getsystem", methods={"GET"})
  311.      *
  312.      * @param Request $request
  313.      * @param Config $config
  314.      *
  315.      * @return JsonResponse
  316.      */
  317.     public function getSystemAction(Request $requestConfig $config)
  318.     {
  319.         $this->checkPermission('system_settings');
  320.         $valueArray = [
  321.             'general' => $config['general'],
  322.             'documents' => $config['documents'],
  323.             'assets' => $config['assets'],
  324.             'objects' => $config['objects'],
  325.             'branding' => $config['branding'],
  326.             'email' => $config['email'],
  327.         ];
  328.         $locales Tool::getSupportedLocales();
  329.         $languageOptions = [];
  330.         $validLanguages = [];
  331.         foreach ($locales as $short => $translation) {
  332.             if (!empty($short)) {
  333.                 $languageOptions[] = [
  334.                     'language' => $short,
  335.                     'display' => $translation " ($short)",
  336.                 ];
  337.                 $validLanguages[] = $short;
  338.             }
  339.         }
  340.         $valueArray['general']['valid_language'] = explode(','$valueArray['general']['valid_languages']);
  341.         //for "wrong" legacy values
  342.         if (is_array($valueArray['general']['valid_language'])) {
  343.             foreach ($valueArray['general']['valid_language'] as $existingValue) {
  344.                 if (!in_array($existingValue$validLanguages)) {
  345.                     $languageOptions[] = [
  346.                         'language' => $existingValue,
  347.                         'display' => $existingValue,
  348.                     ];
  349.                 }
  350.             }
  351.         }
  352.         $response = [
  353.             'values' => $valueArray,
  354.             'config' => [
  355.                 'languages' => $languageOptions,
  356.             ],
  357.         ];
  358.         return $this->adminJson($response);
  359.     }
  360.     /**
  361.      * @Route("/set-system", name="pimcore_admin_settings_setsystem", methods={"PUT"})
  362.      *
  363.      * @param Request $request
  364.      * @param LocaleServiceInterface $localeService
  365.      *
  366.      * @return JsonResponse
  367.      */
  368.     public function setSystemAction(
  369.         LocaleServiceInterface $localeService,
  370.         Request $request,
  371.         KernelInterface $kernel,
  372.         EventDispatcherInterface $eventDispatcher,
  373.         CoreCacheHandler $cache,
  374.         Filesystem $filesystem,
  375.         CacheClearer $symfonyCacheClearer
  376.     ) {
  377.         $this->checkPermission('system_settings');
  378.         $values $this->decodeJson($request->get('data'));
  379.         $existingValues = [];
  380.         try {
  381.             $file Config::locateConfigFile('system.yml');
  382.             $existingValues Config::getConfigInstance($filetrue);
  383.         } catch (\Exception $e) {
  384.             // nothing to do
  385.         }
  386.         // localized error pages
  387.         $localizedErrorPages = [];
  388.         // fallback languages
  389.         $fallbackLanguages = [];
  390.         $existingValues['pimcore']['general']['fallback_languages'] = [];
  391.         $languages explode(','$values['general.validLanguages']);
  392.         $filteredLanguages = [];
  393.         foreach ($languages as $language) {
  394.             if (isset($values['general.fallbackLanguages.' $language])) {
  395.                 $fallbackLanguages[$language] = str_replace(' '''$values['general.fallbackLanguages.' $language]);
  396.             }
  397.             // localized error pages
  398.             if (isset($values['documents.error_pages.localized.' $language])) {
  399.                 $localizedErrorPages[$language] = $values['documents.error_pages.localized.' $language];
  400.             }
  401.             if ($localeService->isLocale($language)) {
  402.                 $filteredLanguages[] = $language;
  403.             }
  404.         }
  405.         // check if there's a fallback language endless loop
  406.         foreach ($fallbackLanguages as $sourceLang => $targetLang) {
  407.             $this->checkFallbackLanguageLoop($sourceLang$fallbackLanguages);
  408.         }
  409.         $settings['pimcore'] = [
  410.             'general' => [
  411.                 'domain' => $values['general.domain'],
  412.                 'redirect_to_maindomain' => $values['general.redirect_to_maindomain'],
  413.                 'language' => $values['general.language'],
  414.                 'valid_languages' => implode(','$filteredLanguages),
  415.                 'fallback_languages' => $fallbackLanguages,
  416.                 'default_language' => $values['general.defaultLanguage'],
  417.                 'debug_admin_translations' => $values['general.debug_admin_translations'],
  418.             ],
  419.             'documents' => [
  420.                 'versions' => [
  421.                     'days' => $values['documents.versions.days'] ?? null,
  422.                     'steps' => $values['documents.versions.steps'] ?? null,
  423.                 ],
  424.                 'error_pages' => [
  425.                     'default' => $values['documents.error_pages.default'],
  426.                     'localized' => $localizedErrorPages,
  427.                 ],
  428.             ],
  429.             'objects' => [
  430.                 'versions' => [
  431.                     'days' => $values['objects.versions.days'] ?? null,
  432.                     'steps' => $values['objects.versions.steps'] ?? null,
  433.                 ],
  434.             ],
  435.             'assets' => [
  436.                 'versions' => [
  437.                     'days' => $values['assets.versions.days'] ?? null,
  438.                     'steps' => $values['assets.versions.steps'] ?? null,
  439.                 ],
  440.                 'hide_edit_image' => $values['assets.hide_edit_image'],
  441.                 'disable_tree_preview' => $values['assets.disable_tree_preview'],
  442.             ],
  443.         ];
  444.         //branding
  445.         $settings['pimcore_admin'] = [
  446.             'branding' =>
  447.                 [
  448.                     'login_screen_invert_colors' => $values['branding.login_screen_invert_colors'],
  449.                     'color_login_screen' => $values['branding.color_login_screen'],
  450.                     'color_admin_interface' => $values['branding.color_admin_interface'],
  451.                     'login_screen_custom_image' => $values['branding.login_screen_custom_image'],
  452.                 ],
  453.         ];
  454.         if (array_key_exists('email.debug.emailAddresses'$values) && $values['email.debug.emailAddresses']) {
  455.             $settings['pimcore']['email']['debug']['email_addresses'] = $values['email.debug.emailAddresses'];
  456.         }
  457.         $settingsYml Yaml::dump($settings5);
  458.         $configFile Config::locateConfigFile('system.yml');
  459.         File::put($configFile$settingsYml);
  460.         // clear all caches
  461.         $this->clearSymfonyCache($request$kernel$eventDispatcher$symfonyCacheClearer);
  462.         $this->stopMessengerWorkers();
  463.         $eventDispatcher->addListener(KernelEvents::TERMINATE, function (TerminateEvent $event) use (
  464.             $cache$eventDispatcher$filesystem
  465.         ) {
  466.             // we need to clear the cache with a delay, because the cache is used by messenger:stop-workers
  467.             // to send the stop signal to all worker processes
  468.             sleep(2);
  469.             $this->clearPimcoreCache($cache$eventDispatcher$filesystem);
  470.         });
  471.         return $this->adminJson(['success' => true]);
  472.     }
  473.     /**
  474.      * @param string $source
  475.      * @param array $definitions
  476.      * @param array $fallbacks
  477.      *
  478.      * @throws \Exception
  479.      */
  480.     protected function checkFallbackLanguageLoop($source$definitions$fallbacks = [])
  481.     {
  482.         if (isset($definitions[$source])) {
  483.             $targets explode(','$definitions[$source]);
  484.             foreach ($targets as $l) {
  485.                 $target trim($l);
  486.                 if ($target) {
  487.                     if (in_array($target$fallbacks)) {
  488.                         throw new \Exception("Language `$source` | `$target` causes an infinte loop.");
  489.                     }
  490.                     $fallbacks[] = $target;
  491.                     $this->checkFallbackLanguageLoop($target$definitions$fallbacks);
  492.                 }
  493.             }
  494.         } else {
  495.             throw new \Exception("Language `$source` doesn't exist");
  496.         }
  497.     }
  498.     /**
  499.      * @Route("/get-web2print", name="pimcore_admin_settings_getweb2print", methods={"GET"})
  500.      *
  501.      * @param Request $request
  502.      *
  503.      * @return JsonResponse
  504.      */
  505.     public function getWeb2printAction(Request $request)
  506.     {
  507.         $this->checkPermission('web2print_settings');
  508.         $values Config::getWeb2PrintConfig();
  509.         $valueArray $values->toArray();
  510.         $optionsString = [];
  511.         if ($valueArray['wkhtml2pdfOptions'] ?? false) {
  512.             foreach ($valueArray['wkhtml2pdfOptions'] as $key => $value) {
  513.                 $tmpStr '--'.$key;
  514.                 if ($value !== null && $value !== '') {
  515.                     $tmpStr .= ' '.$value;
  516.                 }
  517.                 $optionsString[] = $tmpStr;
  518.             }
  519.         }
  520.         $valueArray['wkhtml2pdfOptions'] = implode("\n"$optionsString);
  521.         $response = [
  522.             'values' => $valueArray,
  523.         ];
  524.         return $this->adminJson($response);
  525.     }
  526.     /**
  527.      * @Route("/set-web2print", name="pimcore_admin_settings_setweb2print", methods={"PUT"})
  528.      *
  529.      * @param Request $request
  530.      *
  531.      * @return JsonResponse
  532.      */
  533.     public function setWeb2printAction(Request $request)
  534.     {
  535.         $this->checkPermission('web2print_settings');
  536.         $values $this->decodeJson($request->get('data'));
  537.         unset($values['documentation']);
  538.         unset($values['additions']);
  539.         unset($values['json_converter']);
  540.         if ($values['wkhtml2pdfOptions']) {
  541.             $optionArray = [];
  542.             $lines explode("\n"$values['wkhtml2pdfOptions']);
  543.             foreach ($lines as $line) {
  544.                 $parts explode(' 'substr($line2));
  545.                 $key trim($parts[0]);
  546.                 if ($key) {
  547.                     $value trim($parts[1] ?? '');
  548.                     $optionArray[$key] = $value;
  549.                 }
  550.             }
  551.             $values['wkhtml2pdfOptions'] = $optionArray;
  552.         }
  553.         \Pimcore\Web2Print\Config::save($values);
  554.         return $this->adminJson(['success' => true]);
  555.     }
  556.     /**
  557.      * @Route("/clear-cache", name="pimcore_admin_settings_clearcache", methods={"DELETE"})
  558.      *
  559.      * @param Request $request
  560.      * @param KernelInterface $kernel
  561.      * @param EventDispatcherInterface $eventDispatcher
  562.      * @param CoreCacheHandler $cache
  563.      * @param Filesystem $filesystem
  564.      * @param CacheClearer $symfonyCacheClearer
  565.      *
  566.      * @return JsonResponse
  567.      */
  568.     public function clearCacheAction(
  569.         Request $request,
  570.         KernelInterface $kernel,
  571.         EventDispatcherInterface $eventDispatcher,
  572.         CoreCacheHandler $cache,
  573.         Filesystem $filesystem,
  574.         CacheClearer $symfonyCacheClearer
  575.     ) {
  576.         $this->checkPermissionsHasOneOf(['clear_cache''system_settings']);
  577.         $result = [
  578.             'success' => true,
  579.         ];
  580.         $clearPimcoreCache = !(bool)$request->get('only_symfony_cache');
  581.         $clearSymfonyCache = !(bool)$request->get('only_pimcore_cache');
  582.         if ($clearPimcoreCache) {
  583.             $this->clearPimcoreCache($cache$eventDispatcher$filesystem);
  584.         }
  585.         if ($clearSymfonyCache) {
  586.             $this->clearSymfonyCache($request$kernel$eventDispatcher$symfonyCacheClearer);
  587.         }
  588.         $response = new JsonResponse($result);
  589.         if ($clearSymfonyCache) {
  590.             // we send the response directly here and exit to make sure no code depending on the stale container
  591.             // is running after this
  592.             $response->sendHeaders();
  593.             $response->sendContent();
  594.             exit;
  595.         }
  596.         return $response;
  597.     }
  598.     private function clearPimcoreCache(
  599.         CoreCacheHandler $cache,
  600.         EventDispatcherInterface $eventDispatcher,
  601.         Filesystem $filesystem,
  602.     ): void {
  603.         // empty document cache
  604.         $cache->clearAll();
  605.         if ($filesystem->exists(PIMCORE_CACHE_DIRECTORY)) {
  606.             $filesystem->remove(PIMCORE_CACHE_DIRECTORY);
  607.         }
  608.         // PIMCORE-1854 - recreate .dummy file => should remain
  609.         File::put(PIMCORE_CACHE_DIRECTORY '/.gitkeep''');
  610.         $eventDispatcher->dispatch(new GenericEvent(), SystemEvents::CACHE_CLEAR);
  611.     }
  612.     private function clearSymfonyCache(
  613.         Request $request,
  614.         KernelInterface $kernel,
  615.         EventDispatcherInterface $eventDispatcher,
  616.         CacheClearer $symfonyCacheClearer,
  617.     ): void {
  618.         // pass one or move env parameters to clear multiple envs
  619.         // if no env is passed it will use the current one
  620.         $environments $request->get('env'$kernel->getEnvironment());
  621.         if (!is_array($environments)) {
  622.             $environments trim((string)$environments);
  623.             if (empty($environments)) {
  624.                 $environments = [];
  625.             } else {
  626.                 $environments = [$environments];
  627.             }
  628.         }
  629.         if (empty($environments)) {
  630.             $environments = [$kernel->getEnvironment()];
  631.         }
  632.         $result['environments'] = $environments;
  633.         if (in_array($kernel->getEnvironment(), $environments)) {
  634.             // remove terminate and exception event listeners for the current env as they break with a
  635.             // cleared container - see #2434
  636.             foreach ($eventDispatcher->getListeners(KernelEvents::TERMINATE) as $listener) {
  637.                 $eventDispatcher->removeListener(KernelEvents::TERMINATE$listener);
  638.             }
  639.             foreach ($eventDispatcher->getListeners(KernelEvents::EXCEPTION) as $listener) {
  640.                 $eventDispatcher->removeListener(KernelEvents::EXCEPTION$listener);
  641.             }
  642.         }
  643.         foreach ($environments as $environment) {
  644.             try {
  645.                 $symfonyCacheClearer->clear($environment);
  646.             } catch (\Throwable $e) {
  647.                 $errors $result['errors'] ?? [];
  648.                 $errors[] = $e->getMessage();
  649.                 $result array_merge($result, [
  650.                     'success' => false,
  651.                     'errors' => $errors,
  652.                 ]);
  653.             }
  654.         }
  655.     }
  656.     /**
  657.      * @Route("/clear-output-cache", name="pimcore_admin_settings_clearoutputcache", methods={"DELETE"})
  658.      *
  659.      * @param EventDispatcherInterface $eventDispatcher
  660.      *
  661.      * @return JsonResponse
  662.      */
  663.     public function clearOutputCacheAction(EventDispatcherInterface $eventDispatcher)
  664.     {
  665.         $this->checkPermission('clear_fullpage_cache');
  666.         // remove "output" out of the ignored tags, if a cache lifetime is specified
  667.         Cache::removeIgnoredTagOnClear('output');
  668.         // empty document cache
  669.         Cache::clearTags(['output''output_lifetime']);
  670.         $eventDispatcher->dispatch(new GenericEvent(), SystemEvents::CACHE_CLEAR_FULLPAGE_CACHE);
  671.         return $this->adminJson(['success' => true]);
  672.     }
  673.     /**
  674.      * @Route("/clear-temporary-files", name="pimcore_admin_settings_cleartemporaryfiles", methods={"DELETE"})
  675.      *
  676.      * @param EventDispatcherInterface $eventDispatcher
  677.      *
  678.      * @return JsonResponse
  679.      */
  680.     public function clearTemporaryFilesAction(EventDispatcherInterface $eventDispatcher)
  681.     {
  682.         $this->checkPermission('clear_temp_files');
  683.         // public files
  684.         Tool\Storage::get('thumbnail')->deleteDirectory('/');
  685.         Tool\Storage::get('asset_cache')->deleteDirectory('/');
  686.         // system files
  687.         recursiveDelete(PIMCORE_SYSTEM_TEMP_DIRECTORYfalse);
  688.         $eventDispatcher->dispatch(new GenericEvent(), SystemEvents::CACHE_CLEAR_TEMPORARY_FILES);
  689.         return $this->adminJson(['success' => true]);
  690.     }
  691.     /**
  692.      * @Route("/staticroutes", name="pimcore_admin_settings_staticroutes", methods={"POST"})
  693.      *
  694.      * @param Request $request
  695.      *
  696.      * @return JsonResponse
  697.      */
  698.     public function staticroutesAction(Request $request)
  699.     {
  700.         if ($request->get('data')) {
  701.             $this->checkPermission('routes');
  702.             $data $this->decodeJson($request->get('data'));
  703.             if (is_array($data)) {
  704.                 foreach ($data as &$value) {
  705.                     if (is_string($value)) {
  706.                         $value trim($value);
  707.                     }
  708.                 }
  709.             }
  710.             if ($request->get('xaction') == 'destroy') {
  711.                 $data $this->decodeJson($request->get('data'));
  712.                 $id $data['id'];
  713.                 $route Staticroute::getById($id);
  714.                 if (!$route->isWriteable()) {
  715.                     throw new ConfigWriteException();
  716.                 }
  717.                 $route->delete();
  718.                 return $this->adminJson(['success' => true'data' => []]);
  719.             } elseif ($request->get('xaction') == 'update') {
  720.                 // save routes
  721.                 $route Staticroute::getById($data['id']);
  722.                 if (!$route->isWriteable()) {
  723.                     throw new ConfigWriteException();
  724.                 }
  725.                 $route->setValues($data);
  726.                 $route->save();
  727.                 return $this->adminJson(['data' => $route->getObjectVars(), 'success' => true]);
  728.             } elseif ($request->get('xaction') == 'create') {
  729.                 if (!(new Staticroute())->isWriteable()) {
  730.                     throw new ConfigWriteException();
  731.                 }
  732.                 unset($data['id']);
  733.                 // save route
  734.                 $route = new Staticroute();
  735.                 $route->setValues($data);
  736.                 $route->save();
  737.                 $responseData $route->getObjectVars();
  738.                 $responseData['writeable'] = $route->isWriteable();
  739.                 return $this->adminJson(['data' => $responseData'success' => true]);
  740.             }
  741.         } else {
  742.             // get list of routes
  743.             $list = new Staticroute\Listing();
  744.             if ($filter $request->get('filter')) {
  745.                 $list->setFilter(function (Staticroute $staticRoute) use ($filter) {
  746.                     foreach ($staticRoute->getObjectVars() as $value) {
  747.                         if (!is_scalar($value)) {
  748.                             continue;
  749.                         }
  750.                         if (stripos((string)$value$filter) !== false) {
  751.                             return true;
  752.                         }
  753.                     }
  754.                     return false;
  755.                 });
  756.             }
  757.             $routes = [];
  758.             foreach ($list->getRoutes() as $routeFromList) {
  759.                 $route $routeFromList->getObjectVars();
  760.                 $route['writeable'] = $routeFromList->isWriteable();
  761.                 if (is_array($routeFromList->getSiteId())) {
  762.                     $route['siteId'] = implode(','$routeFromList->getSiteId());
  763.                 }
  764.                 $routes[] = $route;
  765.             }
  766.             return $this->adminJson(['data' => $routes'success' => true'total' => $list->getTotalCount()]);
  767.         }
  768.         return $this->adminJson(['success' => false]);
  769.     }
  770.     /**
  771.      * @Route("/get-available-admin-languages", name="pimcore_admin_settings_getavailableadminlanguages", methods={"GET"})
  772.      *
  773.      * @param Request $request
  774.      *
  775.      * @return JsonResponse
  776.      */
  777.     public function getAvailableAdminLanguagesAction(Request $request)
  778.     {
  779.         $langs = [];
  780.         $availableLanguages Tool\Admin::getLanguages();
  781.         $locales Tool::getSupportedLocales();
  782.         foreach ($availableLanguages as $lang) {
  783.             if (array_key_exists($lang$locales)) {
  784.                 $langs[] = [
  785.                     'language' => $lang,
  786.                     'display' => $locales[$lang],
  787.                 ];
  788.             }
  789.         }
  790.         usort($langs, function ($a$b) {
  791.             return strcmp($a['display'], $b['display']);
  792.         });
  793.         return $this->adminJson($langs);
  794.     }
  795.     /**
  796.      * @Route("/glossary", name="pimcore_admin_settings_glossary", methods={"POST"})
  797.      *
  798.      * @param Request $request
  799.      *
  800.      * @return JsonResponse
  801.      */
  802.     public function glossaryAction(Request $request)
  803.     {
  804.         if ($request->get('data')) {
  805.             $this->checkPermission('glossary');
  806.             Cache::clearTag('glossary');
  807.             if ($request->get('xaction') == 'destroy') {
  808.                 $data $this->decodeJson($request->get('data'));
  809.                 $id $data['id'];
  810.                 $glossary Glossary::getById($id);
  811.                 $glossary->delete();
  812.                 return $this->adminJson(['success' => true'data' => []]);
  813.             } elseif ($request->get('xaction') == 'update') {
  814.                 $data $this->decodeJson($request->get('data'));
  815.                 // save glossary
  816.                 $glossary Glossary::getById($data['id']);
  817.                 if (!empty($data['link'])) {
  818.                     if ($doc Document::getByPath($data['link'])) {
  819.                         $data['link'] = $doc->getId();
  820.                     }
  821.                 }
  822.                 $glossary->setValues($data);
  823.                 $glossary->save();
  824.                 if ($link $glossary->getLink()) {
  825.                     if ((int)$link 0) {
  826.                         if ($doc Document::getById((int)$link)) {
  827.                             $glossary->setLink($doc->getRealFullPath());
  828.                         }
  829.                     }
  830.                 }
  831.                 return $this->adminJson(['data' => $glossary'success' => true]);
  832.             } elseif ($request->get('xaction') == 'create') {
  833.                 $data $this->decodeJson($request->get('data'));
  834.                 unset($data['id']);
  835.                 // save glossary
  836.                 $glossary = new Glossary();
  837.                 if (!empty($data['link'])) {
  838.                     if ($doc Document::getByPath($data['link'])) {
  839.                         $data['link'] = $doc->getId();
  840.                     }
  841.                 }
  842.                 $glossary->setValues($data);
  843.                 $glossary->save();
  844.                 if ($link $glossary->getLink()) {
  845.                     if ((int)$link 0) {
  846.                         if ($doc Document::getById((int)$link)) {
  847.                             $glossary->setLink($doc->getRealFullPath());
  848.                         }
  849.                     }
  850.                 }
  851.                 return $this->adminJson(['data' => $glossary->getObjectVars(), 'success' => true]);
  852.             }
  853.         } else {
  854.             // get list of glossaries
  855.             $list = new Glossary\Listing();
  856.             $list->setLimit($request->get('limit'));
  857.             $list->setOffset($request->get('start'));
  858.             $sortingSettings \Pimcore\Bundle\AdminBundle\Helper\QueryParams::extractSortingSettings(array_merge($request->request->all(), $request->query->all()));
  859.             if ($sortingSettings['orderKey']) {
  860.                 $list->setOrderKey($sortingSettings['orderKey']);
  861.                 $list->setOrder($sortingSettings['order']);
  862.             }
  863.             if ($request->get('filter')) {
  864.                 $list->setCondition('`text` LIKE ' $list->quote('%'.$request->get('filter').'%'));
  865.             }
  866.             $list->load();
  867.             $glossaries = [];
  868.             foreach ($list->getGlossary() as $glossary) {
  869.                 if ($link $glossary->getLink()) {
  870.                     if ((int)$link 0) {
  871.                         if ($doc Document::getById((int)$link)) {
  872.                             $glossary->setLink($doc->getRealFullPath());
  873.                         }
  874.                     }
  875.                 }
  876.                 $glossaries[] = $glossary->getObjectVars();
  877.             }
  878.             return $this->adminJson(['data' => $glossaries'success' => true'total' => $list->getTotalCount()]);
  879.         }
  880.         return $this->adminJson(['success' => false]);
  881.     }
  882.     /**
  883.      * @Route("/get-available-sites", name="pimcore_admin_settings_getavailablesites", methods={"GET"})
  884.      *
  885.      * @param Request $request
  886.      *
  887.      * @return JsonResponse
  888.      */
  889.     public function getAvailableSitesAction(Request $request)
  890.     {
  891.         $excludeMainSite $request->get('excludeMainSite');
  892.         $sitesList = new Model\Site\Listing();
  893.         $sitesObjects $sitesList->load();
  894.         $sites = [];
  895.         if (!$excludeMainSite) {
  896.             $sites[] = [
  897.                 'id' => 'default',
  898.                 'rootId' => 1,
  899.                 'domains' => '',
  900.                 'rootPath' => '/',
  901.                 'domain' => $this->trans('main_site'),
  902.             ];
  903.         }
  904.         foreach ($sitesObjects as $site) {
  905.             if ($site->getRootDocument()) {
  906.                 if ($site->getMainDomain()) {
  907.                     $sites[] = [
  908.                         'id' => $site->getId(),
  909.                         'rootId' => $site->getRootId(),
  910.                         'domains' => implode(','$site->getDomains()),
  911.                         'rootPath' => $site->getRootPath(),
  912.                         'domain' => $site->getMainDomain(),
  913.                     ];
  914.                 }
  915.             } else {
  916.                 // site is useless, parent doesn't exist anymore
  917.                 $site->delete();
  918.             }
  919.         }
  920.         return $this->adminJson($sites);
  921.     }
  922.     /**
  923.      * @Route("/get-available-countries", name="pimcore_admin_settings_getavailablecountries", methods={"GET"})
  924.      *
  925.      * @param LocaleServiceInterface $localeService
  926.      *
  927.      * @return JsonResponse
  928.      */
  929.     public function getAvailableCountriesAction(LocaleServiceInterface $localeService)
  930.     {
  931.         $countries $localeService->getDisplayRegions();
  932.         asort($countries);
  933.         $options = [];
  934.         foreach ($countries as $short => $translation) {
  935.             if (strlen($short) == 2) {
  936.                 $options[] = [
  937.                     'key' => $translation ' (' $short ')',
  938.                     'value' => $short,
  939.                 ];
  940.             }
  941.         }
  942.         $result = ['data' => $options'success' => true'total' => count($options)];
  943.         return $this->adminJson($result);
  944.     }
  945.     /**
  946.      * @Route("/thumbnail-adapter-check", name="pimcore_admin_settings_thumbnailadaptercheck", methods={"GET"})
  947.      *
  948.      * @param Request $request
  949.      *
  950.      * @return Response
  951.      */
  952.     public function thumbnailAdapterCheckAction(Request $request)
  953.     {
  954.         $content '';
  955.         $instance \Pimcore\Image::getInstance();
  956.         if ($instance instanceof \Pimcore\Image\Adapter\GD) {
  957.             $content '<span style="color: red; font-weight: bold;padding: 10px;margin:0 0 20px 0;border:1px solid red;display:block;">' .
  958.                 $this->trans('important_use_imagick_pecl_extensions_for_best_results_gd_is_just_a_fallback_with_less_quality') .
  959.                 '</span>';
  960.         }
  961.         return new Response($content);
  962.     }
  963.     /**
  964.      * @Route("/thumbnail-tree", name="pimcore_admin_settings_thumbnailtree", methods={"GET", "POST"})
  965.      *
  966.      * @return JsonResponse
  967.      */
  968.     public function thumbnailTreeAction()
  969.     {
  970.         $this->checkPermission('thumbnails');
  971.         $thumbnails = [];
  972.         $list = new Asset\Image\Thumbnail\Config\Listing();
  973.         $groups = [];
  974.         foreach ($list->getThumbnails() as $item) {
  975.             if ($item->getGroup()) {
  976.                 if (empty($groups[$item->getGroup()])) {
  977.                     $groups[$item->getGroup()] = [
  978.                         'id' => 'group_' $item->getName(),
  979.                         'text' => htmlspecialchars($item->getGroup()),
  980.                         'expandable' => true,
  981.                         'leaf' => false,
  982.                         'allowChildren' => true,
  983.                         'iconCls' => 'pimcore_icon_folder',
  984.                         'group' => $item->getGroup(),
  985.                         'children' => [],
  986.                     ];
  987.                 }
  988.                 $groups[$item->getGroup()]['children'][] =
  989.                     [
  990.                         'id' => $item->getName(),
  991.                         'text' => $item->getName(),
  992.                         'leaf' => true,
  993.                         'iconCls' => 'pimcore_icon_thumbnails',
  994.                         'cls' => 'pimcore_treenode_disabled',
  995.                         'writeable' => $item->isWriteable(),
  996.                     ];
  997.             } else {
  998.                 $thumbnails[] = [
  999.                     'id' => $item->getName(),
  1000.                     'text' => $item->getName(),
  1001.                     'leaf' => true,
  1002.                     'iconCls' => 'pimcore_icon_thumbnails',
  1003.                     'cls' => 'pimcore_treenode_disabled',
  1004.                     'writeable' => $item->isWriteable(),
  1005.                 ];
  1006.             }
  1007.         }
  1008.         foreach ($groups as $group) {
  1009.             $thumbnails[] = $group;
  1010.         }
  1011.         return $this->adminJson($thumbnails);
  1012.     }
  1013.     /**
  1014.      * @Route("/thumbnail-downloadable", name="pimcore_admin_settings_thumbnaildownloadable", methods={"GET"})
  1015.      *
  1016.      * @return JsonResponse
  1017.      */
  1018.     public function thumbnailDownloadableAction()
  1019.     {
  1020.         $thumbnails = [];
  1021.         $list = new Asset\Image\Thumbnail\Config\Listing();
  1022.         $list->setFilter(function (Asset\Image\Thumbnail\Config $config) {
  1023.             return $config->isDownloadable();
  1024.         });
  1025.         foreach ($list->getThumbnails() as $item) {
  1026.             $thumbnails[] = [
  1027.                 'id' => $item->getName(),
  1028.                 'text' => $item->getName(),
  1029.             ];
  1030.         }
  1031.         return $this->adminJson($thumbnails);
  1032.     }
  1033.     /**
  1034.      * @Route("/thumbnail-add", name="pimcore_admin_settings_thumbnailadd", methods={"POST"})
  1035.      *
  1036.      * @param Request $request
  1037.      *
  1038.      * @return JsonResponse
  1039.      */
  1040.     public function thumbnailAddAction(Request $request)
  1041.     {
  1042.         $this->checkPermission('thumbnails');
  1043.         $success false;
  1044.         $pipe Asset\Image\Thumbnail\Config::getByName($request->get('name'));
  1045.         if (!$pipe) {
  1046.             $pipe = new Asset\Image\Thumbnail\Config();
  1047.             if (!$pipe->isWriteable()) {
  1048.                 throw new ConfigWriteException();
  1049.             }
  1050.             $pipe->setName($request->get('name'));
  1051.             $pipe->save();
  1052.             $success true;
  1053.         } else {
  1054.             if (!$pipe->isWriteable()) {
  1055.                 throw new ConfigWriteException();
  1056.             }
  1057.         }
  1058.         return $this->adminJson(['success' => $success'id' => $pipe->getName()]);
  1059.     }
  1060.     /**
  1061.      * @Route("/thumbnail-delete", name="pimcore_admin_settings_thumbnaildelete", methods={"DELETE"})
  1062.      *
  1063.      * @param Request $request
  1064.      *
  1065.      * @return JsonResponse
  1066.      */
  1067.     public function thumbnailDeleteAction(Request $request)
  1068.     {
  1069.         $this->checkPermission('thumbnails');
  1070.         $pipe Asset\Image\Thumbnail\Config::getByName($request->get('name'));
  1071.         if (!$pipe->isWriteable()) {
  1072.             throw new ConfigWriteException();
  1073.         }
  1074.         $pipe->delete();
  1075.         return $this->adminJson(['success' => true]);
  1076.     }
  1077.     /**
  1078.      * @Route("/thumbnail-get", name="pimcore_admin_settings_thumbnailget", methods={"GET"})
  1079.      *
  1080.      * @param Request $request
  1081.      *
  1082.      * @return JsonResponse
  1083.      */
  1084.     public function thumbnailGetAction(Request $request)
  1085.     {
  1086.         $this->checkPermission('thumbnails');
  1087.         $pipe Asset\Image\Thumbnail\Config::getByName($request->get('name'));
  1088.         $data $pipe->getObjectVars();
  1089.         $data['writeable'] = $pipe->isWriteable();
  1090.         return $this->adminJson($data);
  1091.     }
  1092.     /**
  1093.      * @Route("/thumbnail-update", name="pimcore_admin_settings_thumbnailupdate", methods={"PUT"})
  1094.      *
  1095.      * @param Request $request
  1096.      *
  1097.      * @return JsonResponse
  1098.      */
  1099.     public function thumbnailUpdateAction(Request $request)
  1100.     {
  1101.         $this->checkPermission('thumbnails');
  1102.         $pipe Asset\Image\Thumbnail\Config::getByName($request->get('name'));
  1103.         if (!$pipe->isWriteable()) {
  1104.             throw new ConfigWriteException();
  1105.         }
  1106.         $settingsData $this->decodeJson($request->get('settings'));
  1107.         $mediaData $this->decodeJson($request->get('medias'));
  1108.         $mediaOrder $this->decodeJson($request->get('mediaOrder'));
  1109.         foreach ($settingsData as $key => $value) {
  1110.             $setter 'set' ucfirst($key);
  1111.             if (method_exists($pipe$setter)) {
  1112.                 $pipe->$setter($value);
  1113.             }
  1114.         }
  1115.         $pipe->resetItems();
  1116.         uksort($mediaData, function ($a$b) use ($mediaOrder) {
  1117.             if ($a === 'default') {
  1118.                 return -1;
  1119.             }
  1120.             return ($mediaOrder[$a] < $mediaOrder[$b]) ? -1;
  1121.         });
  1122.         foreach ($mediaData as $mediaName => $items) {
  1123.             if (preg_match('/["<>]/'$mediaName)) {
  1124.                 throw new \Exception('Invalid media query name');
  1125.             }
  1126.             foreach ($items as $item) {
  1127.                 $type $item['type'];
  1128.                 unset($item['type']);
  1129.                 $pipe->addItem($type$item$mediaName);
  1130.             }
  1131.         }
  1132.         $pipe->save();
  1133.         return $this->adminJson(['success' => true]);
  1134.     }
  1135.     /**
  1136.      * @Route("/video-thumbnail-adapter-check", name="pimcore_admin_settings_videothumbnailadaptercheck", methods={"GET"})
  1137.      *
  1138.      * @param Request $request
  1139.      *
  1140.      * @return Response
  1141.      */
  1142.     public function videoThumbnailAdapterCheckAction(Request $request)
  1143.     {
  1144.         $content '';
  1145.         if (!\Pimcore\Video::isAvailable()) {
  1146.             $content '<span style="color: red; font-weight: bold;padding: 10px;margin:0 0 20px 0;border:1px solid red;display:block;">' .
  1147.                 $this->trans('php_cli_binary_and_or_ffmpeg_binary_setting_is_missing') .
  1148.                 '</span>';
  1149.         }
  1150.         return new Response($content);
  1151.     }
  1152.     /**
  1153.      * @Route("/video-thumbnail-tree", name="pimcore_admin_settings_videothumbnailtree", methods={"GET", "POST"})
  1154.      *
  1155.      * @return JsonResponse
  1156.      */
  1157.     public function videoThumbnailTreeAction()
  1158.     {
  1159.         $this->checkPermission('thumbnails');
  1160.         $thumbnails = [];
  1161.         $list = new Asset\Video\Thumbnail\Config\Listing();
  1162.         $groups = [];
  1163.         foreach ($list->getThumbnails() as $item) {
  1164.             if ($item->getGroup()) {
  1165.                 if (empty($groups[$item->getGroup()])) {
  1166.                     $groups[$item->getGroup()] = [
  1167.                         'id' => 'group_' $item->getName(),
  1168.                         'text' => htmlspecialchars($item->getGroup()),
  1169.                         'expandable' => true,
  1170.                         'leaf' => false,
  1171.                         'allowChildren' => true,
  1172.                         'iconCls' => 'pimcore_icon_folder',
  1173.                         'group' => $item->getGroup(),
  1174.                         'children' => [],
  1175.                     ];
  1176.                 }
  1177.                 $groups[$item->getGroup()]['children'][] =
  1178.                     [
  1179.                         'id' => $item->getName(),
  1180.                         'text' => $item->getName(),
  1181.                         'leaf' => true,
  1182.                         'iconCls' => 'pimcore_icon_videothumbnails',
  1183.                         'cls' => 'pimcore_treenode_disabled',
  1184.                         'writeable' => $item->isWriteable(),
  1185.                     ];
  1186.             } else {
  1187.                 $thumbnails[] = [
  1188.                     'id' => $item->getName(),
  1189.                     'text' => $item->getName(),
  1190.                     'leaf' => true,
  1191.                     'iconCls' => 'pimcore_icon_videothumbnails',
  1192.                     'cls' => 'pimcore_treenode_disabled',
  1193.                     'writeable' => $item->isWriteable(),
  1194.                 ];
  1195.             }
  1196.         }
  1197.         foreach ($groups as $group) {
  1198.             $thumbnails[] = $group;
  1199.         }
  1200.         return $this->adminJson($thumbnails);
  1201.     }
  1202.     /**
  1203.      * @Route("/video-thumbnail-add", name="pimcore_admin_settings_videothumbnailadd", methods={"POST"})
  1204.      *
  1205.      * @param Request $request
  1206.      *
  1207.      * @return JsonResponse
  1208.      */
  1209.     public function videoThumbnailAddAction(Request $request)
  1210.     {
  1211.         $this->checkPermission('thumbnails');
  1212.         $success false;
  1213.         $pipe Asset\Video\Thumbnail\Config::getByName($request->get('name'));
  1214.         if (!$pipe) {
  1215.             $pipe = new Asset\Video\Thumbnail\Config();
  1216.             if (!$pipe->isWriteable()) {
  1217.                 throw new ConfigWriteException();
  1218.             }
  1219.             $pipe->setName($request->get('name'));
  1220.             $pipe->save();
  1221.             $success true;
  1222.         } else {
  1223.             if (!$pipe->isWriteable()) {
  1224.                 throw new ConfigWriteException();
  1225.             }
  1226.         }
  1227.         return $this->adminJson(['success' => $success'id' => $pipe->getName()]);
  1228.     }
  1229.     /**
  1230.      * @Route("/video-thumbnail-delete", name="pimcore_admin_settings_videothumbnaildelete", methods={"DELETE"})
  1231.      *
  1232.      * @param Request $request
  1233.      *
  1234.      * @return JsonResponse
  1235.      */
  1236.     public function videoThumbnailDeleteAction(Request $request)
  1237.     {
  1238.         $this->checkPermission('thumbnails');
  1239.         $pipe Asset\Video\Thumbnail\Config::getByName($request->get('name'));
  1240.         if (!$pipe->isWriteable()) {
  1241.             throw new ConfigWriteException();
  1242.         }
  1243.         $pipe->delete();
  1244.         return $this->adminJson(['success' => true]);
  1245.     }
  1246.     /**
  1247.      * @Route("/video-thumbnail-get", name="pimcore_admin_settings_videothumbnailget", methods={"GET"})
  1248.      *
  1249.      * @param Request $request
  1250.      *
  1251.      * @return JsonResponse
  1252.      */
  1253.     public function videoThumbnailGetAction(Request $request)
  1254.     {
  1255.         $this->checkPermission('thumbnails');
  1256.         $pipe Asset\Video\Thumbnail\Config::getByName($request->get('name'));
  1257.         $data $pipe->getObjectVars();
  1258.         $data['writeable'] = $pipe->isWriteable();
  1259.         return $this->adminJson($data);
  1260.     }
  1261.     /**
  1262.      * @Route("/video-thumbnail-update", name="pimcore_admin_settings_videothumbnailupdate", methods={"PUT"})
  1263.      *
  1264.      * @param Request $request
  1265.      *
  1266.      * @return JsonResponse
  1267.      */
  1268.     public function videoThumbnailUpdateAction(Request $request)
  1269.     {
  1270.         $this->checkPermission('thumbnails');
  1271.         $pipe Asset\Video\Thumbnail\Config::getByName($request->get('name'));
  1272.         if (!$pipe->isWriteable()) {
  1273.             throw new ConfigWriteException();
  1274.         }
  1275.         $settingsData $this->decodeJson($request->get('settings'));
  1276.         $mediaData $this->decodeJson($request->get('medias'));
  1277.         $mediaOrder $this->decodeJson($request->get('mediaOrder'));
  1278.         foreach ($settingsData as $key => $value) {
  1279.             $setter 'set' ucfirst($key);
  1280.             if (method_exists($pipe$setter)) {
  1281.                 $pipe->$setter($value);
  1282.             }
  1283.         }
  1284.         $pipe->resetItems();
  1285.         uksort($mediaData, function ($a$b) use ($mediaOrder) {
  1286.             if ($a === 'default') {
  1287.                 return -1;
  1288.             }
  1289.             return ($mediaOrder[$a] < $mediaOrder[$b]) ? -1;
  1290.         });
  1291.         foreach ($mediaData as $mediaName => $items) {
  1292.             foreach ($items as $item) {
  1293.                 $type $item['type'];
  1294.                 unset($item['type']);
  1295.                 $pipe->addItem($type$item$mediaName);
  1296.             }
  1297.         }
  1298.         $pipe->save();
  1299.         return $this->adminJson(['success' => true]);
  1300.     }
  1301.     /**
  1302.      * @Route("/robots-txt", name="pimcore_admin_settings_robotstxtget", methods={"GET"})
  1303.      *
  1304.      * @return JsonResponse
  1305.      */
  1306.     public function robotsTxtGetAction()
  1307.     {
  1308.         $this->checkPermission('robots.txt');
  1309.         $config Config::getRobotsConfig();
  1310.         $config $config->toArray();
  1311.         return $this->adminJson([
  1312.             'success' => true,
  1313.             'data' => $config,
  1314.             'onFileSystem' => file_exists(PIMCORE_WEB_ROOT '/robots.txt'),
  1315.         ]);
  1316.     }
  1317.     /**
  1318.      * @Route("/robots-txt", name="pimcore_admin_settings_robotstxtput", methods={"PUT"})
  1319.      *
  1320.      * @param Request $request
  1321.      *
  1322.      * @return JsonResponse
  1323.      */
  1324.     public function robotsTxtPutAction(Request $request)
  1325.     {
  1326.         $this->checkPermission('robots.txt');
  1327.         $values $request->get('data');
  1328.         if (!is_array($values)) {
  1329.             $values = [];
  1330.         }
  1331.         foreach ($values as $siteId => $robotsContent) {
  1332.             SettingsStore::set('robots.txt-' $siteId$robotsContent'string''robots.txt');
  1333.         }
  1334.         return $this->adminJson([
  1335.             'success' => true,
  1336.         ]);
  1337.     }
  1338.     /**
  1339.      * @Route("/website-settings", name="pimcore_admin_settings_websitesettings", methods={"POST"})
  1340.      *
  1341.      * @param Request $request
  1342.      *
  1343.      * @return JsonResponse
  1344.      *
  1345.      * @throws \Exception
  1346.      */
  1347.     public function websiteSettingsAction(Request $request)
  1348.     {
  1349.         $this->checkPermission('website_settings');
  1350.         if ($request->get('data')) {
  1351.             $data $this->decodeJson($request->get('data'));
  1352.             if (is_array($data)) {
  1353.                 foreach ($data as &$value) {
  1354.                     $value trim($value);
  1355.                 }
  1356.             }
  1357.             if ($request->get('xaction') == 'destroy') {
  1358.                 $id $data['id'];
  1359.                 $setting WebsiteSetting::getById($id);
  1360.                 if ($setting instanceof WebsiteSetting) {
  1361.                     $setting->delete();
  1362.                     return $this->adminJson(['success' => true'data' => []]);
  1363.                 }
  1364.             } elseif ($request->get('xaction') == 'update') {
  1365.                 // save routes
  1366.                 $setting WebsiteSetting::getById($data['id']);
  1367.                 if ($setting instanceof WebsiteSetting) {
  1368.                     switch ($setting->getType()) {
  1369.                         case 'document':
  1370.                         case 'asset':
  1371.                         case 'object':
  1372.                             if (isset($data['data'])) {
  1373.                                 $element Element\Service::getElementByPath($setting->getType(), $data['data']);
  1374.                                 $data['data'] = $element;
  1375.                             }
  1376.                             break;
  1377.                     }
  1378.                     $setting->setValues($data);
  1379.                     $setting->save();
  1380.                     $data $this->getWebsiteSettingForEditMode($setting);
  1381.                     return $this->adminJson(['data' => $data'success' => true]);
  1382.                 }
  1383.             } elseif ($request->get('xaction') == 'create') {
  1384.                 unset($data['id']);
  1385.                 // save route
  1386.                 $setting = new WebsiteSetting();
  1387.                 $setting->setValues($data);
  1388.                 $setting->save();
  1389.                 return $this->adminJson(['data' => $setting->getObjectVars(), 'success' => true]);
  1390.             }
  1391.         } else {
  1392.             $list = new WebsiteSetting\Listing();
  1393.             $list->setLimit($request->get('limit'));
  1394.             $list->setOffset($request->get('start'));
  1395.             $sortingSettings \Pimcore\Bundle\AdminBundle\Helper\QueryParams::extractSortingSettings(array_merge($request->request->all(), $request->query->all()));
  1396.             if ($sortingSettings['orderKey']) {
  1397.                 $list->setOrderKey($sortingSettings['orderKey']);
  1398.                 $list->setOrder($sortingSettings['order']);
  1399.             } else {
  1400.                 $list->setOrderKey('name');
  1401.                 $list->setOrder('asc');
  1402.             }
  1403.             if ($request->get('filter')) {
  1404.                 $list->setCondition('`name` LIKE ' $list->quote('%'.$request->get('filter').'%'));
  1405.             }
  1406.             $totalCount $list->getTotalCount();
  1407.             $list $list->load();
  1408.             $settings = [];
  1409.             foreach ($list as $item) {
  1410.                 $resultItem $this->getWebsiteSettingForEditMode($item);
  1411.                 $settings[] = $resultItem;
  1412.             }
  1413.             return $this->adminJson(['data' => $settings'success' => true'total' => $totalCount]);
  1414.         }
  1415.         return $this->adminJson(['success' => false]);
  1416.     }
  1417.     /**
  1418.      * @param WebsiteSetting $item
  1419.      *
  1420.      * @return array
  1421.      */
  1422.     private function getWebsiteSettingForEditMode($item)
  1423.     {
  1424.         $resultItem = [
  1425.             'id' => $item->getId(),
  1426.             'name' => $item->getName(),
  1427.             'language' => $item->getLanguage(),
  1428.             'type' => $item->getType(),
  1429.             'data' => null,
  1430.             'siteId' => $item->getSiteId(),
  1431.             'creationDate' => $item->getCreationDate(),
  1432.             'modificationDate' => $item->getModificationDate(),
  1433.         ];
  1434.         switch ($item->getType()) {
  1435.             case 'document':
  1436.             case 'asset':
  1437.             case 'object':
  1438.                 $element $item->getData();
  1439.                 if ($element) {
  1440.                     $resultItem['data'] = $element->getRealFullPath();
  1441.                 }
  1442.                 break;
  1443.             default:
  1444.                 $resultItem['data'] = $item->getData();
  1445.                 break;
  1446.         }
  1447.         return $resultItem;
  1448.     }
  1449.     /**
  1450.      * @Route("/get-available-algorithms", name="pimcore_admin_settings_getavailablealgorithms", methods={"GET"})
  1451.      *
  1452.      * @param Request $request
  1453.      *
  1454.      * @return JsonResponse
  1455.      */
  1456.     public function getAvailableAlgorithmsAction(Request $request)
  1457.     {
  1458.         $options = [
  1459.             [
  1460.                 'key' => 'password_hash',
  1461.                 'value' => 'password_hash',
  1462.             ],
  1463.         ];
  1464.         $algorithms hash_algos();
  1465.         foreach ($algorithms as $algorithm) {
  1466.             $options[] = [
  1467.                 'key' => $algorithm,
  1468.                 'value' => $algorithm,
  1469.             ];
  1470.         }
  1471.         $result = ['data' => $options'success' => true'total' => count($options)];
  1472.         return $this->adminJson($result);
  1473.     }
  1474.     /**
  1475.      * deleteViews
  1476.      * delete views for localized fields when languages are removed to
  1477.      * prevent mysql errors
  1478.      *
  1479.      * @param string $language
  1480.      * @param string $dbName
  1481.      */
  1482.     protected function deleteViews($language$dbName)
  1483.     {
  1484.         $db \Pimcore\Db::get();
  1485.         $views $db->fetchAll('SHOW FULL TABLES IN ' $db->quoteIdentifier($dbName) . " WHERE TABLE_TYPE LIKE 'VIEW'");
  1486.         foreach ($views as $view) {
  1487.             if (preg_match('/^object_localized_[0-9]+_' $language '$/'$view['Tables_in_' $dbName])) {
  1488.                 $sql 'DROP VIEW ' $db->quoteIdentifier($view['Tables_in_' $dbName]);
  1489.                 $db->query($sql);
  1490.             }
  1491.         }
  1492.     }
  1493.     /**
  1494.      * @Route("/test-web2print", name="pimcore_admin_settings_testweb2print", methods={"GET"})
  1495.      *
  1496.      * @param Request $request
  1497.      *
  1498.      * @return Response
  1499.      */
  1500.     public function testWeb2printAction(Request $request)
  1501.     {
  1502.         $this->checkPermission('web2print_settings');
  1503.         $response $this->render('@PimcoreAdmin/Admin/Settings/testWeb2print.html.twig');
  1504.         $html $response->getContent();
  1505.         $adapter \Pimcore\Web2Print\Processor::getInstance();
  1506.         $params = [];
  1507.         if ($adapter instanceof \Pimcore\Web2Print\Processor\WkHtmlToPdf) {
  1508.             $params['adapterConfig'] = '-O landscape';
  1509.         } elseif ($adapter instanceof \Pimcore\Web2Print\Processor\PdfReactor) {
  1510.             $params['adapterConfig'] = [
  1511.                 'javaScriptMode' => 0,
  1512.                 'addLinks' => true,
  1513.                 'appendLog' => true,
  1514.                 'enableDebugMode' => true,
  1515.             ];
  1516.         }
  1517.         $responseOptions = [
  1518.             'Content-Type' => 'application/pdf',
  1519.         ];
  1520.         $pdfData $adapter->getPdfFromString($html$params);
  1521.         return new \Symfony\Component\HttpFoundation\Response(
  1522.             $pdfData,
  1523.             200,
  1524.             $responseOptions
  1525.         );
  1526.     }
  1527. }