vendor/shopware/core/Framework/Update/Services/UpdateHtaccess.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Update\Services;
  3. use Shopware\Core\Framework\Update\Event\UpdatePostFinishEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. class UpdateHtaccess implements EventSubscriberInterface
  6. {
  7.     private const MARKER_START = '# BEGIN Shopware';
  8.     private const MARKER_STOP = '# END Shopware';
  9.     private const INSTRUCTIONS = '# The directives (lines) between "# BEGIN Shopware" and "# END Shopware" are dynamically generated. Any changes to the directives between these markers will be overwritten.';
  10.     private const OLD_FILES = [
  11.         '9ab5be8c4bbff3490f3ae367af8a30d7', // https://github.com/shopware/production/commit/bebf9adc90bf5d7b0d53a149cc5bdba328696086
  12.         'ba812f2a64b337b032b10685ca6e2308', // https://github.com/shopware/production/commit/18ce6ffc904b8d2d237dc4ee6654c1fa9a6df719
  13.     ];
  14.     private string $htaccessPath;
  15.     /**
  16.      * @internal
  17.      */
  18.     public function __construct(string $htaccessPath)
  19.     {
  20.         $this->htaccessPath = $htaccessPath;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             UpdatePostFinishEvent::class => 'update',
  26.         ];
  27.     }
  28.     public function update(): void
  29.     {
  30.         if (!file_exists($this->htaccessPath) || !file_exists($this->htaccessPath . '.dist')) {
  31.             return;
  32.         }
  33.         if (\in_array(md5_file($this->htaccessPath), self::OLD_FILES, true)) {
  34.             $this->replaceFile($this->htaccessPath);
  35.             return;
  36.         }
  37.         $content = file_get_contents($this->htaccessPath);
  38.         // User has deleted the markers. So we will ignore the update process
  39.         if (strpos($content, self::MARKER_START) === false || strpos($content, self::MARKER_STOP) === false) {
  40.             return;
  41.         }
  42.         $this->updateByMarkers($this->htaccessPath);
  43.     }
  44.     /**
  45.      * Replace entire .htaccess from dist
  46.      */
  47.     private function replaceFile(string $path): void
  48.     {
  49.         $dist = $path . '.dist';
  50.         if (!file_exists($dist)) {
  51.             return;
  52.         }
  53.         $perms = fileperms($dist);
  54.         copy($dist, $path);
  55.         if ($perms) {
  56.             chmod($path, $perms | 0644);
  57.         }
  58.     }
  59.     private function updateByMarkers(string $path): void
  60.     {
  61.         [$pre, $_, $post] = $this->getLinesFromMarkedFile($path);
  62.         [$_, $existing, $_] = $this->getLinesFromMarkedFile($path . '.dist');
  63.         if (!\in_array(self::INSTRUCTIONS, $existing, true)) {
  64.             array_unshift($existing, self::INSTRUCTIONS);
  65.         }
  66.         array_unshift($existing, self::MARKER_START);
  67.         $existing[] = self::MARKER_STOP;
  68.         $newFile = implode("\n", array_merge($pre, $existing, $post));
  69.         $perms = fileperms($path);
  70.         file_put_contents($path, $newFile);
  71.         if ($perms) {
  72.             chmod($path, $perms | 0644);
  73.         }
  74.     }
  75.     private function getLinesFromMarkedFile(string $path): array
  76.     {
  77.         $fp = fopen($path, 'rb+');
  78.         if (!$fp) {
  79.             return [];
  80.         }
  81.         $lines = [];
  82.         while (!feof($fp)) {
  83.             if ($line = fgets($fp)) {
  84.                 $lines[] = rtrim($line, "\r\n");
  85.             }
  86.         }
  87.         $foundStart = false;
  88.         $foundStop = false;
  89.         $preLines = [];
  90.         $postLines = [];
  91.         $existingLines = [];
  92.         foreach ($lines as $line) {
  93.             if (!$foundStart && strpos($line, self::MARKER_START) === 0) {
  94.                 $foundStart = true;
  95.                 continue;
  96.             }
  97.             if (!$foundStop && strpos($line, self::MARKER_STOP) === 0) {
  98.                 $foundStop = true;
  99.                 continue;
  100.             }
  101.             if (!$foundStart) {
  102.                 $preLines[] = $line;
  103.             } elseif ($foundStop) {
  104.                 $postLines[] = $line;
  105.             } else {
  106.                 $existingLines[] = $line;
  107.             }
  108.         }
  109.         return [$preLines, $existingLines, $postLines];
  110.     }
  111. }