vendor/store.shopware.com/swagplatformsecurity/src/SwagPlatformSecurity.php line 19

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swag\Security;
  3. use Shopware\Core\Framework\Plugin;
  4. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  5. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  6. use Shopware\Core\Kernel;
  7. use Swag\Security\Components\RemoveDisabledServicesCompilerPass;
  8. use Swag\Security\Components\State;
  9. use Swag\Security\Components\UpdateHtaccess;
  10. use Symfony\Component\Config\FileLocator;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  13. require_once __DIR__ '/Fixes/NEXT15858/init.php';
  14. require_once __DIR__ '/Fixes/NEXT21077/init.php';
  15. class SwagPlatformSecurity extends Plugin
  16. {
  17.     public const PLUGIN_NAME 'SwagPlatformSecurity';
  18.     public function build(ContainerBuilder $container): void
  19.     {
  20.         $loader = new PhpFileLoader($container, new FileLocator([__DIR__ '/Resources/config']));
  21.         $loader->load('services.php');
  22.         $this->fetchPluginConfig($container);
  23.         $container->addCompilerPass(new RemoveDisabledServicesCompilerPass());
  24.     }
  25.     public function boot(): void
  26.     {
  27.         parent::boot();
  28.         foreach ($this->container->getParameter('SwagPlatformSecurity.activeFixes') as $securityFix) {
  29.             $securityFix::boot($this->container);
  30.         }
  31.     }
  32.     public function install(InstallContext $installContext): void
  33.     {
  34.         $this->copyHtaccess();
  35.     }
  36.     public function update(UpdateContext $updateContext): void
  37.     {
  38.         $this->copyHtaccess();
  39.     }
  40.     private function fetchPluginConfig(ContainerBuilder $container): void
  41.     {
  42.         try {
  43.             $qb Kernel::getConnection()->createQueryBuilder();
  44.             $config $qb
  45.                 ->select(['ticket''active'])
  46.                 ->from('swag_security_config''config')
  47.                 ->execute()
  48.                 ->fetchAll(\PDO::FETCH_KEY_PAIR);
  49.         } catch (\Throwable $e) {
  50.             $config = [];
  51.         }
  52.         foreach ($config as &$item) {
  53.             $item = (bool) $item;
  54.         }
  55.         unset($item);
  56.         $shopwareVersion $_SERVER['SHOPWARE_FAKE_VERSION'] ?? $container->getParameter('kernel.shopware_version');
  57.         $availableFixes = [];
  58.         $activeFixes = [];
  59.         foreach (State::KNOWN_ISSUES as $knownIssue) {
  60.             if (!$knownIssue::isValidForVersion($shopwareVersion)) {
  61.                 continue;
  62.             }
  63.             $availableFixes[] = $knownIssue;
  64.             if (array_key_exists($knownIssue::getTicket(), $config) && !$config[$knownIssue::getTicket()]) {
  65.                 continue;
  66.             }
  67.             $knownIssue::buildContainer($container);
  68.             $activeFixes[] = $knownIssue;
  69.         }
  70.         $container->setParameter('SwagPlatformSecurity.activeFixes'$activeFixes);
  71.         $container->setParameter('SwagPlatformSecurity.availableFixes'$availableFixes);
  72.     }
  73.     private function copyHtaccess(): void
  74.     {
  75.         $projectDirHtaccess $this->container->getParameter('kernel.project_dir'). '/.htaccess';
  76.         copy(__DIR__ '/../root_htaccess.dist'$projectDirHtaccess);
  77.         $knownFolders = [
  78.             $this->container->getParameter('kernel.project_dir'). '/bin',
  79.             $this->container->getParameter('kernel.project_dir'). '/config',
  80.             $this->container->getParameter('kernel.project_dir'). '/custom',
  81.             $this->container->getParameter('kernel.project_dir'). '/files',
  82.             $this->container->getParameter('kernel.project_dir'). '/src',
  83.             $this->container->getParameter('kernel.project_dir'). '/var',
  84.             $this->container->getParameter('kernel.project_dir'). '/vendor',
  85.         ];
  86.         foreach ($knownFolders as $knownFolder) {
  87.             if (file_exists($knownFolder)) {
  88.                 copy(__DIR__ '/../block_directory_access_htaccess.dist'$knownFolder '/.htaccess');
  89.             }
  90.         }
  91.         $shopwareVersion $_SERVER['SHOPWARE_FAKE_VERSION'] ?? $this->container->getParameter('kernel.shopware_version');
  92.         $updateService = new UpdateHtaccess();
  93.         // Only update the .htaccess if we are on a older shopware version
  94.         if (version_compare($shopwareVersion$updateService->getMaxVersion(), '<')) {
  95.             $publicHtaccess $this->container->getParameter('kernel.project_dir'). '/public/.htaccess';
  96.             $updateService->updateHtaccess($publicHtaccess__DIR__ '/../current_public_htaccess.dist');
  97.         }
  98.     }
  99. }