From 68b039504462a333917d62ad1b634cec631a2049 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Mon, 21 Oct 2024 11:02:20 -0300 Subject: [PATCH 1/3] fix: open settings together with cms_pico Signed-off-by: Vitor Mattos --- lib/Handler/FooterHandler.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/Handler/FooterHandler.php b/lib/Handler/FooterHandler.php index 30808493e5..5117ddceca 100644 --- a/lib/Handler/FooterHandler.php +++ b/lib/Handler/FooterHandler.php @@ -47,7 +47,6 @@ class FooterHandler { private QrCode $qrCode; - private Environment $twigEnvironment; private File $file; private FileEntity $fileEntity; private const MIN_QRCODE_SIZE = 100; @@ -61,9 +60,6 @@ public function __construct( private IL10N $l10n, private ITempManager $tempManager, ) { - $this->twigEnvironment = new Environment( - new FilesystemLoader(), - ); } public function getFooter(File $file, FileEntity $fileEntity): string { @@ -118,7 +114,10 @@ private function getMetadata(): array { private function getRenderedHtmlFooter(): string { try { - return $this->twigEnvironment + $twigEnvironment = new Environment( + new FilesystemLoader(), + ); + return $twigEnvironment ->createTemplate($this->getTemplate()) ->render($this->getTemplateVars()); } catch (SyntaxError $e) { From f627959db617261145150621c14413ecf48f3396 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Tue, 22 Oct 2024 17:09:31 -0300 Subject: [PATCH 2/3] chore: Replace template engine Signed-off-by: Vitor Mattos --- composer.json | 6 +- composer.lock | 311 +++++------------------ lib/Handler/FooterHandler.php | 42 +-- lib/Handler/Templates/footer.php | 19 ++ psalm.xml | 1 + tests/Unit/Handler/FooterHandlerTest.php | 17 +- tests/psalm-baseline.xml | 8 +- 7 files changed, 112 insertions(+), 292 deletions(-) create mode 100644 lib/Handler/Templates/footer.php diff --git a/composer.json b/composer.json index e7fc1c079e..56c0b17666 100644 --- a/composer.json +++ b/composer.json @@ -2,12 +2,12 @@ "require": { "endroid/qr-code": "^4.6", "jsignpdf/jsignpdf-php": "^1.2", + "league/plates": "^3.5", "libresign/whatosami": "^0.0.1", "mikehaertl/php-pdftk": "^0.13.0", "mpdf/mpdf": "^8.2", "pagerfanta/pagerfanta": "^3.6", "smalot/pdfparser": "^2.4", - "twig/twig": "^3.9", "wobeto/email-blur": "^1.0" }, "require-dev": { @@ -33,8 +33,8 @@ "cs:check": "php-cs-fixer fix --dry-run --diff", "cs:fix": "php-cs-fixer fix", "openapi": "generate-spec --verbose && (npm run typescript:generate || echo 'Please manually regenerate the typescript OpenAPI models')", - "psalm": "psalm --threads=1", - "psalm:update-baseline": "psalm --threads=1 --update-baseline --set-baseline=tests/psalm-baseline.xml", + "psalm": "psalm --threads=$(nproc)", + "psalm:update-baseline": "psalm --threads=$(nproc) --update-baseline --set-baseline=tests/psalm-baseline.xml", "psalm:clear": "psalm --clear-cache && psalm --clear-global-cache", "post-install-cmd": [ "@composer bin all install --ansi", diff --git a/composer.lock b/composer.lock index 44efc9d15d..44d2be68e3 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f7bef6434e144eb1f4a05e9f9ae1adec", + "content-hash": "47f1e88f3f7ef0e1a6c849ac107a1dbc", "packages": [ { "name": "bacon/bacon-qr-code", @@ -238,6 +238,70 @@ }, "time": "2024-04-24T17:19:52+00:00" }, + { + "name": "league/plates", + "version": "v3.5.0", + "source": { + "type": "git", + "url": "https://github.com/thephpleague/plates.git", + "reference": "a6a3238e46c6e19af7318fdc36bfbe49b0620231" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/thephpleague/plates/zipball/a6a3238e46c6e19af7318fdc36bfbe49b0620231", + "reference": "a6a3238e46c6e19af7318fdc36bfbe49b0620231", + "shasum": "" + }, + "require": { + "php": "^7.0|^8.0" + }, + "require-dev": { + "mikey179/vfsstream": "^1.6", + "phpunit/phpunit": "^9.5", + "squizlabs/php_codesniffer": "^3.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, + "autoload": { + "psr-4": { + "League\\Plates\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jonathan Reinink", + "email": "jonathan@reinink.ca", + "role": "Developer" + }, + { + "name": "RJ Garcia", + "email": "ragboyjr@icloud.com", + "role": "Developer" + } + ], + "description": "Plates, the native PHP template system that's fast, easy to use and easy to extend.", + "homepage": "https://platesphp.com", + "keywords": [ + "league", + "package", + "templates", + "templating", + "views" + ], + "support": { + "issues": "https://github.com/thephpleague/plates/issues", + "source": "https://github.com/thephpleague/plates/tree/v3.5.0" + }, + "time": "2023-01-16T20:25:45+00:00" + }, { "name": "libresign/whatosami", "version": "0.0.1", @@ -1109,85 +1173,6 @@ ], "time": "2023-01-24T14:02:46+00:00" }, - { - "name": "symfony/polyfill-ctype", - "version": "v1.31.0", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/a3cc8b044a6ea513310cbd48ef7333b384945638", - "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638", - "shasum": "" - }, - "require": { - "php": ">=7.2" - }, - "provide": { - "ext-ctype": "*" - }, - "suggest": { - "ext-ctype": "For best performance" - }, - "type": "library", - "extra": { - "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" - } - }, - "autoload": { - "files": [ - "bootstrap.php" - ], - "psr-4": { - "Symfony\\Polyfill\\Ctype\\": "" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Gert de Pagter", - "email": "BackEndTea@gmail.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill for ctype functions", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "ctype", - "polyfill", - "portable" - ], - "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.31.0" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2024-09-09T11:45:10+00:00" - }, { "name": "symfony/polyfill-mbstring", "version": "v1.31.0", @@ -1348,162 +1333,6 @@ ], "time": "2024-09-09T11:45:10+00:00" }, - { - "name": "symfony/polyfill-php81", - "version": "v1.31.0", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-php81.git", - "reference": "4a4cfc2d253c21a5ad0e53071df248ed48c6ce5c" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php81/zipball/4a4cfc2d253c21a5ad0e53071df248ed48c6ce5c", - "reference": "4a4cfc2d253c21a5ad0e53071df248ed48c6ce5c", - "shasum": "" - }, - "require": { - "php": ">=7.2" - }, - "type": "library", - "extra": { - "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" - } - }, - "autoload": { - "files": [ - "bootstrap.php" - ], - "psr-4": { - "Symfony\\Polyfill\\Php81\\": "" - }, - "classmap": [ - "Resources/stubs" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Nicolas Grekas", - "email": "p@tchwork.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill backporting some PHP 8.1+ features to lower PHP versions", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "polyfill", - "portable", - "shim" - ], - "support": { - "source": "https://github.com/symfony/polyfill-php81/tree/v1.31.0" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2024-09-09T11:45:10+00:00" - }, - { - "name": "twig/twig", - "version": "v3.11.1", - "source": { - "type": "git", - "url": "https://github.com/twigphp/Twig.git", - "reference": "ff063afc691e1cfda6714f1915ed766cb108d188" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/ff063afc691e1cfda6714f1915ed766cb108d188", - "reference": "ff063afc691e1cfda6714f1915ed766cb108d188", - "shasum": "" - }, - "require": { - "php": ">=7.2.5", - "symfony/deprecation-contracts": "^2.5|^3", - "symfony/polyfill-ctype": "^1.8", - "symfony/polyfill-mbstring": "^1.3", - "symfony/polyfill-php80": "^1.22", - "symfony/polyfill-php81": "^1.29" - }, - "require-dev": { - "psr/container": "^1.0|^2.0", - "symfony/phpunit-bridge": "^5.4.9|^6.4|^7.0" - }, - "type": "library", - "autoload": { - "files": [ - "src/Resources/core.php", - "src/Resources/debug.php", - "src/Resources/escaper.php", - "src/Resources/string_loader.php" - ], - "psr-4": { - "Twig\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com", - "homepage": "http://fabien.potencier.org", - "role": "Lead Developer" - }, - { - "name": "Twig Team", - "role": "Contributors" - }, - { - "name": "Armin Ronacher", - "email": "armin.ronacher@active-4.com", - "role": "Project Founder" - } - ], - "description": "Twig, the flexible, fast, and secure template language for PHP", - "homepage": "https://twig.symfony.com", - "keywords": [ - "templating" - ], - "support": { - "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.11.1" - }, - "funding": [ - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/twig/twig", - "type": "tidelift" - } - ], - "time": "2024-09-10T10:40:14+00:00" - }, { "name": "wobeto/email-blur", "version": "1.0.0", @@ -1804,12 +1633,12 @@ "source": { "type": "git", "url": "https://github.com/Roave/SecurityAdvisories.git", - "reference": "2db4b93f047a03be06ec0a890d252e1c8c635f74" + "reference": "51e3fa290bca57eca7ba6c261b8a1278eb13a98a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Roave/SecurityAdvisories/zipball/2db4b93f047a03be06ec0a890d252e1c8c635f74", - "reference": "2db4b93f047a03be06ec0a890d252e1c8c635f74", + "url": "https://api.github.com/repos/Roave/SecurityAdvisories/zipball/51e3fa290bca57eca7ba6c261b8a1278eb13a98a", + "reference": "51e3fa290bca57eca7ba6c261b8a1278eb13a98a", "shasum": "" }, "conflict": { @@ -2029,7 +1858,7 @@ "froxlor/froxlor": "<=2.2.0.0-RC3", "frozennode/administrator": "<=5.0.12", "fuel/core": "<1.8.1", - "funadmin/funadmin": "<=3.2|>=3.3.2,<=3.3.3", + "funadmin/funadmin": "<=5.0.2", "gaoming13/wechat-php-sdk": "<=1.10.2", "genix/cms": "<=1.1.11", "getformwork/formwork": "<1.13.1|==2.0.0.0-beta1", @@ -2628,7 +2457,7 @@ "type": "tidelift" } ], - "time": "2024-10-18T15:05:15+00:00" + "time": "2024-10-21T20:05:20+00:00" } ], "aliases": [], diff --git a/lib/Handler/FooterHandler.php b/lib/Handler/FooterHandler.php index 5117ddceca..9475c27d20 100644 --- a/lib/Handler/FooterHandler.php +++ b/lib/Handler/FooterHandler.php @@ -32,18 +32,15 @@ use Endroid\QrCode\QrCode; use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; use Endroid\QrCode\Writer\PngWriter; +use League\Plates\Engine; use Mpdf\Mpdf; use OCA\Libresign\Db\File as FileEntity; -use OCA\Libresign\Exception\LibresignException; use OCA\Libresign\Service\PdfParserService; use OCP\AppFramework\Services\IAppConfig; use OCP\Files\File; use OCP\IL10N; use OCP\ITempManager; use OCP\IURLGenerator; -use Twig\Environment; -use Twig\Error\SyntaxError; -use Twig\Loader\FilesystemLoader; class FooterHandler { private QrCode $qrCode; @@ -113,16 +110,10 @@ private function getMetadata(): array { } private function getRenderedHtmlFooter(): string { - try { - $twigEnvironment = new Environment( - new FilesystemLoader(), - ); - return $twigEnvironment - ->createTemplate($this->getTemplate()) - ->render($this->getTemplateVars()); - } catch (SyntaxError $e) { - throw new LibresignException($e->getMessage()); - } + $tempFile = $this->tempManager->getTemporaryFile('.php'); + file_put_contents($tempFile, $this->getTemplate()); + $templates = new Engine($this->tempManager->getTempBaseDir()); + return $templates->render(pathinfo($tempFile, PATHINFO_FILENAME), $this->getTemplateVars()); } public function setTemplateVar(string $name, mixed $value): self { @@ -159,28 +150,7 @@ private function getTemplateVars(): array { } private function getTemplate(): string { - return $this->appConfig->getAppValue('footer_template', <<<'HTML' - - - {% if qrcode %} - - {% endif %} - - -
- - - {{ signedBy }} - {% if validateIn %} -
- - {{ validateIn|replace({'%s': validationSite}) }} - - {% endif %} -
- HTML - ); + return $this->appConfig->getAppValue('footer_template', file_get_contents(__DIR__ . '/Templates/footer.php')); } private function getQrCodeImageBase64(string $text): string { diff --git a/lib/Handler/Templates/footer.php b/lib/Handler/Templates/footer.php new file mode 100644 index 0000000000..48f52b4802 --- /dev/null +++ b/lib/Handler/Templates/footer.php @@ -0,0 +1,19 @@ + + + + + + + +
+ + + + +
+ + + + +
diff --git a/psalm.xml b/psalm.xml index ddd8dae879..813e8c842d 100644 --- a/psalm.xml +++ b/psalm.xml @@ -14,6 +14,7 @@ + diff --git a/tests/Unit/Handler/FooterHandlerTest.php b/tests/Unit/Handler/FooterHandlerTest.php index 0f9aa4b9b3..6c20f46e93 100644 --- a/tests/Unit/Handler/FooterHandlerTest.php +++ b/tests/Unit/Handler/FooterHandlerTest.php @@ -65,19 +65,20 @@ public function testGetFooterWithSuccess(): void { 'footer_validate_in' => 'Validate in %s.', 'footer_template' => <<<'HTML'
- qrcodeSize:{{ qrcodeSize }}
- signedBy:{{ signedBy }}
- validateIn:{{ validateIn }}
- test:{{ test }}
- qrcode:{{ qrcode }} + qrcodeSize:
+ signedBy:
+ validateIn:
+ test:
+ qrcode:
HTML, default => '', }; }); - $this->tempManager - ->method('getTempBaseDir') - ->willReturn(sys_get_temp_dir()); + $this->tempManager->method('getTempBaseDir')->willReturn(sys_get_temp_dir()); + $tempName = sys_get_temp_dir() . '/' . mt_rand() . '.php'; + touch($tempName); + $this->tempManager->method('getTemporaryFile')->willReturn($tempName); $file = $this->createMock(\OCP\Files\File::class); $libresignFile = $this->createMock(\OCA\Libresign\Db\File::class); diff --git a/tests/psalm-baseline.xml b/tests/psalm-baseline.xml index 71964acf94..f187a4f3f6 100644 --- a/tests/psalm-baseline.xml +++ b/tests/psalm-baseline.xml @@ -1,5 +1,5 @@ - + @@ -60,9 +60,6 @@ - - getConnection()->getDatabasePlatform()]]> - @@ -94,6 +91,9 @@ + + connection->getDatabasePlatform()]]> + From 6e138c5852b7c846004bca3c7b3e8413b0082b69 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Mon, 21 Oct 2024 11:02:20 -0300 Subject: [PATCH 3/3] fix: open settings together with cms_pico Signed-off-by: Vitor Mattos