chore: Replace template engine

Signed-off-by: Vitor Mattos <vitor@php.rio>
This commit is contained in:
Vitor Mattos 2024-10-22 17:09:31 -03:00
parent 8517b49444
commit 6e09a96b88
No known key found for this signature in database
GPG key ID: B7AB4B76A7CA7318
7 changed files with 112 additions and 292 deletions

View file

@ -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",

311
composer.lock generated
View file

@ -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": "a2b4298bf773491ea42cc21d74d718ca",
"content-hash": "bb292b8b93451d0a54a7c3c2d008b4a3",
"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": [],

View file

@ -32,18 +32,15 @@ use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
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 @@ class FooterHandler {
}
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 @@ class FooterHandler {
}
private function getTemplate(): string {
return $this->appConfig->getAppValue('footer_template', <<<'HTML'
<table style="width:100%;border:0;font-size:8px;">
<tr>
{% if qrcode %}
<td width="{{ qrcodeSize }}px">
<img src="data:image/png;base64,{{ qrcode }}" style="width:{{ qrcodeSize }}px"/>
</td>
{% endif %}
<td style="vertical-align: bottom;padding: 0px 0px 15px 0px;line-height:1.5em;">
<a href="{{ linkToSite }}" style="text-decoration: none;color:unset;">{{ signedBy }}</a>
{% if validateIn %}
<br>
<a href="{{ validationSite }}"
style="text-decoration: none;color:unset;">
{{ validateIn|replace({'%s': validationSite}) }}
</a>
{% endif %}
</td>
</tr>
</table>
HTML
);
return $this->appConfig->getAppValue('footer_template', file_get_contents(__DIR__ . '/Templates/footer.php'));
}
private function getQrCodeImageBase64(string $text): string {

View file

@ -0,0 +1,19 @@
<table style="width:100%;border:0;font-size:8px;">
<tr>
<?php if ($qrcode) { ?>
<td width="<?= $qrcodeSize; ?>px">
<img src="data:image/png;base64,<?= $qrcode; ?>" style="width:<?= $qrcodeSize; ?>px"/>
</td>
<?php } ?>
<td style="vertical-align: bottom;padding: 0px 0px 15px 0px;line-height:1.5em;">
<a href="<?= $linkToSite; ?>" style="text-decoration: none;color:unset;"><?= $signedBy; ?></a>
<?php if ($validateIn) { ?>
<br>
<a href="<?=$validationSite; ?>"
style="text-decoration: none;color:unset;">
<?= str_replace('%s', $validationSite, $validateIn); ?>
</a>
<?php } ?>
</td>
</tr>
</table>

View file

@ -14,6 +14,7 @@
<directory name="lib" />
<ignoreFiles>
<directory name="vendor" />
<directory name="lib/Handler/Templates" />
</ignoreFiles>
</projectFiles>
<issueHandlers>

View file

@ -65,19 +65,20 @@ final class FooterHandlerTest extends \OCA\Libresign\Tests\Unit\TestCase {
'footer_validate_in' => 'Validate in %s.',
'footer_template' => <<<'HTML'
<div style="font-size:8px;">
qrcodeSize:{{ qrcodeSize }}<br />
signedBy:{{ signedBy }}<br />
validateIn:{{ validateIn }}<br />
test:{{ test }}<br />
qrcode:{{ qrcode }}
qrcodeSize:<?= $qrcodeSize ?><br />
signedBy:<?= $signedBy ?><br />
validateIn:<?= $validateIn ?><br />
test:<?= $test ?><br />
qrcode:<?= $qrcode ?>
</div>
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);

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.25.0@01a8eb06b9e9cc6cfb6a320bf9fb14331919d505">
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
<file src="lib/AppInfo/Application.php">
<InvalidArgument>
<code><![CDATA[LoadSidebarListener::class]]></code>
@ -60,9 +60,6 @@
<UndefinedClass>
<code><![CDATA[PostgreSQLPlatform]]></code>
</UndefinedClass>
<UndefinedDocblockClass>
<code><![CDATA[$qb->getConnection()->getDatabasePlatform()]]></code>
</UndefinedDocblockClass>
</file>
<file src="lib/Files/TemplateLoader.php">
<InvalidArgument>
@ -94,6 +91,9 @@
<UndefinedClass>
<code><![CDATA[PostgreSQLPlatform]]></code>
</UndefinedClass>
<UndefinedDocblockClass>
<code><![CDATA[$this->connection->getDatabasePlatform()]]></code>
</UndefinedDocblockClass>
</file>
<file src="lib/Migration/Version8000Date20231103003922.php">
<UndefinedDocblockClass>