diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..6e7ef90 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor +phpstan.neon diff --git a/README.md b/README.md new file mode 100644 index 0000000..8b95f8b --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# Thumbnail Processor Addon for Shopware 6 to use secure ImgProxy + +[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) + +This plugins allows you to use your self-hosted secured ImgProxy. You will need Plugin `Thumbnail Processor`. + +## Install + +Download the plugin from the release page and enable it in shopware. + +## Usage +Adjust the config. [See docs of ImgProxy.](https://docs.imgproxy.net/#/configuration?id=url-signature) + +## License + +The MIT License (MIT). Please see [License File](LICENSE) for more information. diff --git a/bin/fix-cs.sh b/bin/fix-cs.sh new file mode 100755 index 0000000..29180ae --- /dev/null +++ b/bin/fix-cs.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +echo "Fix php files" +php ../../../dev-ops/analyze/vendor/bin/php-cs-fixer fix --config=../../../vendor/shopware/platform/.php_cs.dist -vv . diff --git a/bin/static-analyze.sh b/bin/static-analyze.sh new file mode 100755 index 0000000..bc32553 --- /dev/null +++ b/bin/static-analyze.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +php "`dirname \"$0\"`"/phpstan-config-generator.php +composer dump-autoload +php ../../../dev-ops/analyze/vendor/bin/phpstan analyze --configuration phpstan.neon --autoload-file=../../../vendor/autoload.php src +php ../../../dev-ops/analyze/vendor/bin/psalm --config=psalm.xml --show-info=false diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..903a923 --- /dev/null +++ b/composer.json @@ -0,0 +1,43 @@ +{ + "name": "frosh/platform-thumbnail-processor-secure-imgproxy", + "keywords": [ + "shopware", + "backend", + "admin", + "media", + "media manager", + "cdn", + "storage", + "thumbnail" + ], + "description": "This plugins allows you to use secured imgproxy in thumbnail processor", + "version": "v1.0.0", + "type": "shopware-platform-plugin", + "license": "MIT", + "authors": [ + { + "name": "FriendsOfShopware", + "homepage": "https://friendsofshopware.de" + } + ], + "require": { + "shopware/core": "*", + "frosh/platform-thumbnail-processor": "*" + }, + "extra": { + "shopware-plugin-class": "Frosh\\ThumbnailProcessorImgProxy\\FroshPlatformThumbnailProcessorImgProxy", + "label": { + "de-DE": "ThumbnailProcessorImgProxy Addon", + "en-GB": "ThumbnailProcessorImgProxy Addon" + }, + "description": { + "de-DE": "Deutsche Beschreibung des Plugins", + "en-GB": "English description of the plugin" + } + }, + "autoload": { + "psr-4": { + "Frosh\\ThumbnailProcessorImgProxy\\": "src/" + } + } +} diff --git a/src/FroshPlatformThumbnailProcessorImgProxy.php b/src/FroshPlatformThumbnailProcessorImgProxy.php new file mode 100644 index 0000000..a8e62ac --- /dev/null +++ b/src/FroshPlatformThumbnailProcessorImgProxy.php @@ -0,0 +1,9 @@ + + + + + Configuration + + + Domain + + https://imgproxy.example.com/ + + + + imgproxykey + + + + + imgproxysalt + + + + + resizingType + + fit + fit + + + + gravity + + sm + sm + + + + enlarge + + 0 + 0 + + + + signatureSize + + 32 + 32 + + + + diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml new file mode 100644 index 0000000..90c1342 --- /dev/null +++ b/src/Resources/config/services.xml @@ -0,0 +1,12 @@ + + + + + + + + + + diff --git a/src/Service/ThumbnailUrlTemplate.php b/src/Service/ThumbnailUrlTemplate.php new file mode 100644 index 0000000..50ec6cd --- /dev/null +++ b/src/Service/ThumbnailUrlTemplate.php @@ -0,0 +1,77 @@ +domain = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.Domain'); + $this->key = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.imgproxykey'); + $this->salt = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.imgproxysalt'); + $this->resizingType = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.resizingType') ?: 'fit'; + $this->gravity = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.gravity') ?: 'sm'; + $this->enlarge = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.enlarge') ?: 0; + $this->signatureSize = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.signatureSize') ?: 32; + $this->parent = $parent; + } + + /** + * @param string $mediaUrl + * @param string $mediaPath + * @param string $width + * @param string $height + */ + public function getUrl($mediaUrl, $mediaPath, $width, $height): string + { + $keyBin = pack('H*', $this->key); + $saltBin = pack('H*', $this->salt); + + if (empty($keyBin) || empty($saltBin)) { + return $this->parent->getUrl($mediaUrl, $mediaPath, $width, $height); + } + + $extension = pathinfo($mediaPath, PATHINFO_EXTENSION); + $encodedUrl = rtrim(strtr(base64_encode($mediaUrl . '/' . $mediaPath), '+/', '-_'), '='); + + $path = "/{$this->resizingType}/{$width}/{$height}/{$this->gravity}/{$this->enlarge}/{$encodedUrl}.{$extension}"; + $signature = hash_hmac('sha256', $saltBin . $path, $keyBin, true); + + if ($this->signatureSize !== 32) { + $signature = pack('A' . $this->signatureSize, $signature); + } + + $signature = rtrim(strtr(base64_encode($signature), '+/', '-_'), '='); + + return $this->domain . '/' . $signature . $path; + } +}