|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Frosh\Tools\Controller; |
| 4 | + |
| 5 | +use Shopware\Core\Kernel; |
| 6 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 7 | +use Symfony\Component\HttpFoundation\Request; |
| 8 | +use Symfony\Component\Routing\Annotation\Route; |
| 9 | + |
| 10 | +#[Route(path: '/api/_action/frosh-tools', defaults: ['_routeScope' => ['api'], '_acl' => ['frosh_tools:read']])] |
| 11 | +class ShopwareFilesController |
| 12 | +{ |
| 13 | + private const STATUS_OK = 0; |
| 14 | + private const STATUS_IGNORED_ALL = 1; |
| 15 | + private const STATUS_IGNORED_IN_PROJECT = 2; |
| 16 | + private bool $isPlatform; |
| 17 | + |
| 18 | + public function __construct( |
| 19 | + private readonly string $shopwareVersion, |
| 20 | + private readonly string $projectDir, |
| 21 | + private readonly array $projectIgnoredFiles |
| 22 | + ) |
| 23 | + { |
| 24 | + $this->isPlatform = !is_dir($this->projectDir . '/vendor/shopware/core') && is_dir($this->projectDir . '/src/Core'); |
| 25 | + } |
| 26 | + |
| 27 | + #[Route(path: '/shopware-files', name: 'api.frosh.tools.shopware-files', methods: ['GET'])] |
| 28 | + public function listShopwareFiles(): JsonResponse |
| 29 | + { |
| 30 | + if ($this->shopwareVersion === Kernel::SHOPWARE_FALLBACK_VERSION) { |
| 31 | + return new JsonResponse(['error' => 'Git version is not supported']); |
| 32 | + } |
| 33 | + |
| 34 | + $url = sprintf('https://swagger.docs.fos.gg/version/%s/Files.xxhsums', $this->shopwareVersion); |
| 35 | + |
| 36 | + $data = trim(@file_get_contents($url)); |
| 37 | + |
| 38 | + if (empty($data)) { |
| 39 | + return new JsonResponse(['error' => 'No file information for this Shopware version']); |
| 40 | + } |
| 41 | + |
| 42 | + $invalidFiles = []; |
| 43 | + $allFilesAreOkay = true; |
| 44 | + |
| 45 | + foreach (explode("\n", $data) as $row) { |
| 46 | + if ($this->isPlatform) { |
| 47 | + $row = preg_replace_callback('/vendor\/shopware\/(.)/', function ($matches) { |
| 48 | + return 'src/' . strtoupper($matches[1]); |
| 49 | + }, $row); |
| 50 | + } |
| 51 | + |
| 52 | + [$expectedMd5Sum, $file] = explode(' ', trim($row)); |
| 53 | + |
| 54 | + $path = $this->projectDir . '/' . $file; |
| 55 | + |
| 56 | + if (!is_file($path)) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + $xxhSum = hash_file('xxh64', $path); |
| 61 | + |
| 62 | + $ignoredState = $this->isIgnoredFileHash($file); |
| 63 | + if ($ignoredState === self::STATUS_IGNORED_ALL) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + if ($xxhSum !== $expectedMd5Sum) { |
| 68 | + if ($ignoredState === self::STATUS_OK) { |
| 69 | + $allFilesAreOkay = false; |
| 70 | + } |
| 71 | + |
| 72 | + $invalidFiles[] = [ |
| 73 | + 'name' => $file, |
| 74 | + 'shopwareUrl' => $this->getShopwareUrl($file), |
| 75 | + 'expected' => $ignoredState === self::STATUS_IGNORED_IN_PROJECT, |
| 76 | + ]; |
| 77 | + } |
| 78 | + |
| 79 | + //WE SHOULD STOP HERE, WHILE THERE MIGHT BE ANY BIG PROBLEM! |
| 80 | + if (count($invalidFiles) > 100) { |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if ($allFilesAreOkay) { |
| 86 | + return new JsonResponse(['ok' => true, 'files' => $invalidFiles]); |
| 87 | + } |
| 88 | + |
| 89 | + return new JsonResponse(['ok' => false, 'files' => $invalidFiles]); |
| 90 | + } |
| 91 | + |
| 92 | + #[Route(path: '/file-contents', name: 'api.frosh.tools.file-contents', methods: ['GET'])] |
| 93 | + public function getFileContents(Request $request): JsonResponse |
| 94 | + { |
| 95 | + if ($this->shopwareVersion === Kernel::SHOPWARE_FALLBACK_VERSION) { |
| 96 | + return new JsonResponse(['error' => 'Git version is not supported']); |
| 97 | + } |
| 98 | + |
| 99 | + $file = $request->query->get('file'); |
| 100 | + if (!$file) { |
| 101 | + return new JsonResponse(['error' => 'no file provided']); |
| 102 | + } |
| 103 | + |
| 104 | + $path = realpath($this->projectDir . '/' . $file); |
| 105 | + if ($path === false || !str_starts_with($path, $this->projectDir) || !is_file($path)) { |
| 106 | + return new JsonResponse(['error' => 'File is invalid']); |
| 107 | + } |
| 108 | + |
| 109 | + return new JsonResponse([ |
| 110 | + 'name' => $file, |
| 111 | + 'shopwareUrl' => $this->getShopwareUrl($file), |
| 112 | + 'content' => file_get_contents($path), |
| 113 | + 'originalContent' => $this->getOriginalFileContent($file), |
| 114 | + ]); |
| 115 | + } |
| 116 | + |
| 117 | + #[Route(path: '/shopware-file/restore', name: 'api.frosh.tools.shopware-file.restore', methods: ['GET'])] |
| 118 | + public function restoreShopwareFile(Request $request): JsonResponse |
| 119 | + { |
| 120 | + if ($this->shopwareVersion === Kernel::SHOPWARE_FALLBACK_VERSION) { |
| 121 | + return new JsonResponse(['error' => 'Git version is not supported']); |
| 122 | + } |
| 123 | + |
| 124 | + $file = $request->query->get('file'); |
| 125 | + if (!$file) { |
| 126 | + return new JsonResponse(['error' => 'no file provided']); |
| 127 | + } |
| 128 | + |
| 129 | + $path = realpath($this->projectDir . '/' . $file); |
| 130 | + if ($path === false || !str_starts_with($path, $this->projectDir) || !is_file($path)) { |
| 131 | + return new JsonResponse(['error' => 'File is invalid']); |
| 132 | + } |
| 133 | + |
| 134 | + $content = $this->getOriginalFileContent($file); |
| 135 | + if ($content === null || $content === '') { |
| 136 | + return new JsonResponse(['error' => 'File would be empty!']); |
| 137 | + } |
| 138 | + |
| 139 | + file_put_contents($path, $content); |
| 140 | + |
| 141 | + if (\function_exists('opcache_reset')) { |
| 142 | + opcache_reset(); |
| 143 | + } |
| 144 | + |
| 145 | + return new JsonResponse(['status' => sprintf('File at "%s" has been restored', $file)]); |
| 146 | + } |
| 147 | + |
| 148 | + private function getShopwareUrl(string $name): ?string |
| 149 | + { |
| 150 | + if ($this->isPlatform) { |
| 151 | + $name = preg_replace('/^src\//', '', $name); |
| 152 | + } else { |
| 153 | + $name = preg_replace('/^vendor\/shopware\//', '', $name); |
| 154 | + } |
| 155 | + |
| 156 | + $pathParts = \explode('/', $name); |
| 157 | + $repo = $pathParts[0]; |
| 158 | + array_shift($pathParts); |
| 159 | + |
| 160 | + return 'https://github.com/shopware/' . |
| 161 | + $repo . |
| 162 | + '/blob/v' . |
| 163 | + $this->shopwareVersion . |
| 164 | + '/' . |
| 165 | + \implode('/', $pathParts); |
| 166 | + } |
| 167 | + |
| 168 | + private function getOriginalFileContent(string $name): ?string |
| 169 | + { |
| 170 | + return @file_get_contents($this->getShopwareUrl($name) . '?raw=true') ?: null; |
| 171 | + } |
| 172 | + |
| 173 | + private function isIgnoredFileHash(string $file): int |
| 174 | + { |
| 175 | + if (in_array($file, $this->projectIgnoredFiles, true)) { |
| 176 | + return self::STATUS_IGNORED_IN_PROJECT; |
| 177 | + } |
| 178 | + |
| 179 | + return self::STATUS_OK; |
| 180 | + } |
| 181 | + |
| 182 | + private function assertNoGitVersion(): ?JsonResponse |
| 183 | + { |
| 184 | + if ($this->shopwareVersion === Kernel::SHOPWARE_FALLBACK_VERSION) { |
| 185 | + return new JsonResponse(['error' => 'Git version is not supported']); |
| 186 | + } |
| 187 | + |
| 188 | + return null; |
| 189 | + } |
| 190 | +} |
0 commit comments