Skip to content

Commit

Permalink
Merge pull request #20 from Setasign/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
JanSlabon authored Oct 20, 2023
2 parents 6d972c8 + 41d2591 commit 9ff4df3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor
.idea
composer.phar
.DS_Store
.DS_Store
local-tests/*.pdf
18 changes: 9 additions & 9 deletions src/FpdiProtection.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,22 +218,22 @@ public function __construct($orientation = 'P', $unit = 'mm', $size = 'A4', $use
return;
}

if (OPENSSL_VERSION_NUMBER >= 0x30000000 && PHP_VERSION_ID < 80100) {
throw new \RuntimeException(
'OpenSSL 3 is not supported with PHP versions < 8.1.0. ' .
'You\'re using PHP ' . PHP_VERSION . ' with ' . OPENSSL_VERSION_TEXT . '. ' .
'Please fix your PHP installation or set $useArcfourFallback to true to use a slower fallback implementation.'
);
}

if (!(function_exists('openssl_encrypt') && in_array('rc4-40', openssl_get_cipher_methods(), true))) {
if (!function_exists('openssl_encrypt') || !in_array('rc4-40', openssl_get_cipher_methods(), true)) {
throw new \RuntimeException(
'OpenSSL with RC4 supported is required if $useArcfourFallback is false. ' .
'If using OpenSSL 3 make sure that legacy providers are loaded ' .
'(see https://wiki.openssl.org/index.php/OpenSSL_3.0#Providers).'
);
}

if (OPENSSL_VERSION_NUMBER >= 0x30000000 && PHP_VERSION_ID < 80100) {
throw new \RuntimeException(
'OpenSSL 3 is not supported with PHP versions < 8.1.0. ' .
'You\'re using PHP ' . PHP_VERSION . ' with ' . OPENSSL_VERSION_TEXT . '. ' .
'Please fix your PHP installation or set $useArcfourFallback to true to ' .
'use a slower fallback implementation.'
);
}
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/functional/FpdiProtectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,31 @@ public function testLinks()

$this->assertEquals($linkTarget, $uri);
}

public function testWithArcfourFallback()
{
$linkTarget = 'https://setasign.com';
$pdf = new FpdiProtection('P', 'mm', 'A4', true);
$pdf->setProtection([], 'user', 'owner', 3);
$pdf->AddPage();
$pdf->SetFont('Helvetica', '', 12);
$pdf->Cell(0, 10, 'Test', 0, 0, '', false, $linkTarget);

$encryptionKey = $this->getEncryptionKey($pdf);

$pdfString = $pdf->Output('S');

// let's extract the object manually and parse it into a object structre
preg_match('~5 0 obj(.*?)endobj~s', $pdfString, $match);
$parser = new PdfParser(StreamReader::createByString($match[1]));
$value = $parser->readValue();

$uri = PdfString::unescape($value->value['A']->value['URI']->value);

$uri = openssl_decrypt(
$uri, 'RC4-40', $this->calcKey($encryptionKey, 5, 3), OPENSSL_RAW_DATA
);

$this->assertEquals($linkTarget, $uri);
}
}

0 comments on commit 9ff4df3

Please sign in to comment.