Skip to content

Commit

Permalink
Merge branch 'enhancement/handle_disabled_url_fopen' of https://githu…
Browse files Browse the repository at this point in the history
…b.com/kohlerdominik/php-swiss-qr-bill into kohlerdominik-enhancement/handle_disabled_url_fopen
  • Loading branch information
sprain committed Nov 20, 2022
2 parents f2628ea + e55a9a3 commit ba155f9
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 14 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ jobs:
- name: Install dependencies
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress

- name: Execute tests
- name: Execute tests in default environment
run: vendor/bin/phpunit --verbose

- name: Execute tests in hardened environment
run: php -d allow_url_fopen=0 -d memory_limit=128M -d register_globals=0 vendor/bin/phpunit --verbose

- name: Check coding standard
run: vendor/bin/php-cs-fixer --no-interaction --dry-run --diff -v fix src/

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"phpunit/phpunit": "^9.0",
"dg/bypass-finals": "^1.3",
"dms/phpunit-arraysubset-asserts": "^0.2",
"fpdf/fpdf": "^1.82",
"fpdf/fpdf": "^1.85",
"friendsofphp/php-cs-fixer": "^3.4",
"khanamiryan/qrcode-detector-decoder": "^1.0.3",
"phpstan/phpstan": "^1.2",
Expand Down
21 changes: 14 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion src/PaymentPart/Output/FpdfOutput/FpdfOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,28 @@ private function addSwissQrCodeImage(): void
$yPosQrCode = 209.5 + $this->offsetY;
$xPosQrCode = 67 + $this->offsetX;

$this->fpdf->Image($qrCode->getDataUri($this->getQrCodeImageFormat()), $xPosQrCode, $yPosQrCode, 46, 46, 'png');
if (ini_get('allow_url_fopen') === "1") {
$this->fpdf->Image(
$qrCode->getDataUri($this->getQrCodeImageFormat()),
$xPosQrCode,
$yPosQrCode,
46,
46,
'png'
);
} elseif (method_exists($this->fpdf, 'MemImage')) {
$this->fpdf->MemImage(
$qrCode->getAsString($this->getQrCodeImageFormat()),
$xPosQrCode,
$yPosQrCode,
46,
46
);
} else {
throw new UnsupportedEnvironmentException(
'If directive "allow_url_fopen" is disabled, FPDFs "Memory Image Support" script is required'
);
}
}

private function addInformationContentReceipt(): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php


namespace Sprain\SwissQrBill\PaymentPart\Output\FpdfOutput;

class UnsupportedEnvironmentException extends \RuntimeException
{
}
46 changes: 44 additions & 2 deletions tests/PaymentPart/Output/FpdfOutput/FpdfOutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Sprain\Tests\SwissQrBill\PaymentPart\Output\FpdfOutput;

use Fpdf\Fpdf;
use Fpdf\Traits\MemoryImageSupport\MemImageTrait;
use PHPUnit\Framework\TestCase;
use Sprain\SwissQrBill\Exception\InvalidFpdfImageFormat;
use Sprain\SwissQrBill\PaymentPart\Output\FpdfOutput\FpdfOutput;
use Sprain\SwissQrBill\PaymentPart\Output\FpdfOutput\UnsupportedEnvironmentException;
use Sprain\SwissQrBill\QrBill;
use Sprain\SwissQrBill\QrCode\QrCode;
use Sprain\Tests\SwissQrBill\TestQrBillCreatorTrait;
Expand Down Expand Up @@ -35,7 +37,7 @@ public function testValidQrBills(string $name, QrBill $qrBill): void
foreach ($variations as $variation) {
$file = $variation['file'];

$fpdf = new Fpdf('P', 'mm', 'A4');
$fpdf = $this->instantiateFpdf();
$fpdf->AddPage();

$output = new FpdfOutput($qrBill, 'en', $fpdf);
Expand Down Expand Up @@ -67,7 +69,7 @@ public function testItThrowsSvgNotSupportedException(): void
'paymentReferenceQr'
]);

$fpdf = new Fpdf('P', 'mm', 'A4');
$fpdf = $this->instantiateFpdf();
$fpdf->AddPage();

$output = new FpdfOutput($qrBill, 'en', $fpdf);
Expand All @@ -76,6 +78,31 @@ public function testItThrowsSvgNotSupportedException(): void
->getPaymentPart();
}

public function testItThrowsUnsupportedEnvironmentException(): void
{
if (ini_get('allow_url_fopen') === "1") {
$this->markTestSkipped("This test only works in hardened environment.");
}

$this->expectException(UnsupportedEnvironmentException::class);

$qrBill = $this->createQrBill([
'header',
'creditorInformationQrIban',
'creditor',
'paymentAmountInformation',
'paymentReferenceQr'
]);

$fpdf = $this->instantiateFpdf(false);
$fpdf->AddPage();

$output = new FpdfOutput($qrBill, 'en', $fpdf);
$output
->setQrCodeImageFormat(QrCode::FILE_FORMAT_PNG)
->getPaymentPart();
}

private function getActualPdfContents(string $fileContents): ?string
{
// Extract actual pdf content and ignore all meta data which may differ in different versions of Fpdf
Expand All @@ -87,4 +114,19 @@ private function getActualPdfContents(string $fileContents): ?string
}
return null;
}

private function instantiateFpdf($withMemImageSupport = null): Fpdf
{
if ($withMemImageSupport === null) {
$withMemImageSupport = !ini_get('allow_url_fopen');
}

if ($withMemImageSupport) {
return new class extends Fpdf {
use MemImageTrait;
};
} else {
return new Fpdf('P', 'mm', 'A4');
}
}
}
21 changes: 19 additions & 2 deletions tests/PaymentPart/Output/FpdfOutput/FpdiOutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sprain\Tests\SwissQrBill\PaymentPart\Output\FpdfOutput;

use Fpdf\Traits\MemoryImageSupport\MemImageTrait;
use PHPUnit\Framework\TestCase;
use setasign\Fpdi\Fpdi;
use Sprain\SwissQrBill\Exception\InvalidFpdfImageFormat;
Expand Down Expand Up @@ -35,7 +36,7 @@ public function testValidQrBills(string $name, QrBill $qrBill): void
foreach ($variations as $variation) {
$file = $variation['file'];

$fpdf = new Fpdi('P', 'mm', 'A4');
$fpdf = $this->instantiateFpdi();
$fpdf->AddPage();

$output = new FpdfOutput($qrBill, 'en', $fpdf);
Expand Down Expand Up @@ -67,7 +68,7 @@ public function testItThrowsSvgNotSupportedException(): void
'paymentReferenceQr'
]);

$fpdf = new Fpdi('P', 'mm', 'A4');
$fpdf = $this->instantiateFpdi();
$fpdf->AddPage();

$output = new FpdfOutput($qrBill, 'en', $fpdf);
Expand All @@ -87,4 +88,20 @@ private function getActualPdfContents(string $fileContents): ?string
}
return null;
}

private function instantiateFpdi($withMemImageSupport = null): Fpdi
{
if ($withMemImageSupport === null) {
$withMemImageSupport = !ini_get('allow_url_fopen');
}

if ($withMemImageSupport) {
return new class extends Fpdi {
// this uses the MemImageTrait from Fpdf/Fpdf
use MemImageTrait;
};
} else {
return new Fpdi('P', 'mm', 'A4');
}
}
}

0 comments on commit ba155f9

Please sign in to comment.