Skip to content

Commit bab0911

Browse files
committed
fix: update index.php with real example and fix the code
Signed-off-by: Vitor Mattos <[email protected]>
1 parent d74135c commit bab0911

3 files changed

Lines changed: 78 additions & 46 deletions

File tree

example/index.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,38 @@
33
/**
44
* @author Jeidison Farias <[email protected]>
55
*/
6-
require_once "../src/JSignPDF.php";
6+
require_once __DIR__ . '/../vendor/autoload.php';
77

88
use Jeidison\JSignPDF\JSignPDF;
99
use Jeidison\JSignPDF\Sign\JSignParam;
1010

11+
$password = '123';
12+
13+
$privateKey = openssl_pkey_new([
14+
'private_key_bits' => 2048,
15+
'private_key_type' => OPENSSL_KEYTYPE_RSA,
16+
]);
17+
18+
$csr = openssl_csr_new(['commonName' => 'John Doe'], $privateKey, ['digest_alg' => 'sha256']);
19+
$x509 = openssl_csr_sign($csr, null, $privateKey, 365);
20+
21+
openssl_pkcs12_export(
22+
$x509,
23+
$pfxCertificateContent,
24+
$privateKey,
25+
$password,
26+
);
27+
1128
$param = JSignParam::instance();
12-
$param->setCertificate(file_get_contents('../tests/resources/certificado.pfx'));
13-
$param->setPdf(file_get_contents('../tests/resources/pdf-test.pdf'));
14-
$param->setPassword('123');
29+
30+
$param->setJavaVersion('openjdk version "21.0.7" 2025-04-15 LTS');
31+
$param->setJavaDownloadUrl('https://github.com/adoptium/temurin21-binaries/releases/download/jdk-21.0.8%2B9/OpenJDK21U-jre_x64_linux_hotspot_21.0.8_9.tar.gz');
32+
$param->setJavaPath(__DIR__ . '/../tmp/java');
33+
34+
$param->setCertificate($pfxCertificateContent);
35+
$param->setPdf(file_get_contents(__DIR__ . '/../tests/resources/pdf-test.pdf'));
36+
$param->setPassword($password);
1537

1638
$jSignPdf = new JSignPDF($param);
1739
$fileSigned = $jSignPdf->sign();
18-
file_put_contents('../tmp/file_signed.pdf', $fileSigned);
40+
file_put_contents(__DIR__ . '/../tmp/file_signed.pdf', $fileSigned);

src/Runtime/JavaRuntimeService.php

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use InvalidArgumentException;
88
use Jeidison\JSignPDF\Sign\JSignParam;
99
use PharData;
10+
use PharException;
1011
use RuntimeException;
12+
use UnexpectedValueException;
1113

1214
class JavaRuntimeService
1315
{
@@ -74,24 +76,57 @@ private function downloadAndExtract(string $url, string $baseDir): void
7476
if (!filter_var($url, FILTER_VALIDATE_URL)) {
7577
throw new InvalidArgumentException('The url to download Java is invalid: ' . $url);
7678
}
77-
$content = @file_get_contents($url);
78-
if ($content === false) {
79-
throw new InvalidArgumentException("Failute to download file using the url $url, error: " . print_r(error_get_last(), true));
79+
$this->chunkDownload($url, $baseDir . '/java.tar.gz');
80+
try {
81+
$tar = new PharData($baseDir . '/java.tar.gz');
82+
} catch (PharException|UnexpectedValueException $e) {
83+
throw new InvalidArgumentException('The file ' . $baseDir . '/java.tar.gz cannot be extracted');
8084
}
81-
if (!$content) {
82-
throw new InvalidArgumentException('The url returned empty content: ' . $url);
85+
$rootDirInsideTar = $this->findRootDir($tar, $baseDir . '/java.tar.gz');
86+
if (!$rootDirInsideTar) {
87+
throw new InvalidArgumentException('Invalid tar content.');
8388
}
84-
$decompressedContent = @gzdecode($content);
85-
if ($decompressedContent === false) {
86-
throw new InvalidArgumentException('The file downloaded from follow URL cannot be gzdecoded: ' . $url);
87-
}
88-
file_put_contents($baseDir . '/java.tar.gz', $decompressedContent);
89-
$tar = new PharData($baseDir . '/java.tar.gz');
90-
$tar->extractTo($baseDir, null, true);
89+
$tar->extractTo(directory: $baseDir, overwrite: true);
90+
@exec('mv ' . escapeshellarg($baseDir . '/'. $rootDirInsideTar) . '/* ' . escapeshellarg($baseDir));
91+
@exec('rm -rf ' . escapeshellarg($baseDir . '/'. $rootDirInsideTar));
9192
unlink($baseDir . '/java.tar.gz');
9293
if (!file_exists($baseDir . '/bin/java')) {
9394
throw new RuntimeException('Java binary not found at: ' . $baseDir . '/bin/java');
9495
}
9596
chmod($baseDir . '/bin/java', 0700);
9697
}
98+
99+
private function findRootDir(PharData $phar, $rootDir) {
100+
$files = new \RecursiveIteratorIterator($phar, \RecursiveIteratorIterator::CHILD_FIRST);
101+
102+
foreach ($files as $file) {
103+
$pathName = $file->getPathname();
104+
if (str_contains($pathName, '/bin/') || str_contains($pathName, '/bin/')) {
105+
$parts = explode($rootDir, $pathName);
106+
$internalFullPath = end($parts);
107+
$parts = explode('/bin/', $internalFullPath);
108+
return trim($parts[0], '/');
109+
}
110+
}
111+
}
112+
113+
private function chunkDownload(string $url, string $destination): void
114+
{
115+
$fp = fopen($destination, 'w');
116+
117+
if ($fp) {
118+
$ch = curl_init($url);
119+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
120+
curl_setopt($ch, CURLOPT_FILE, $fp);
121+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
122+
$response = curl_exec($ch);
123+
if ($response === false) {
124+
throw new InvalidArgumentException('Failure to download file using the url ' . $url);
125+
}
126+
curl_close($ch);
127+
fclose($fp);
128+
} else {
129+
throw new InvalidArgumentException("Failute to download file using the url $url");
130+
}
131+
}
97132
}

tests/Runtime/JavaRuntimeServiceTest.php

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function testGetPathWithDownloadUrlWith4xxError(): void {
9191
$jsignParam->setJavaDownloadUrl('https://404.domain');
9292
$jsignParam->setJavaVersion('21.0.0');
9393
$this->expectException(InvalidArgumentException::class);
94-
$this->expectExceptionMessageMatches('/Failute to download/');
94+
$this->expectExceptionMessageMatches('/Failure to download/');
9595
$service->getPath($jsignParam);
9696
}
9797

@@ -118,32 +118,7 @@ public function testGetPathWithDownloadUrlWithInvalidGzipedFile(): void {
118118

119119
$jsignParam->setJavaVersion('21.0.0');
120120
$this->expectException(InvalidArgumentException::class);
121-
$this->expectExceptionMessageMatches('/cannot be gzdecoded/');
122-
$service->getPath($jsignParam);
123-
}
124-
125-
public function testGetPathWithDownloadUrlWithCorruptFile(): void {
126-
mkdir($this->testTmpDir . '/bin', 0755, true);
127-
touch($this->testTmpDir . '/bin/java');
128-
129-
$jsignParam = new JSignParam();
130-
$service = new JavaRuntimeService();
131-
$jsignParam->setJavaPath($this->testTmpDir . '/bin/java');
132-
133-
$server = new MockWebServer();
134-
$server->start();
135-
$server->setResponseOfPath(
136-
'/',
137-
new Response(
138-
gzencode('invalid tar content'),
139-
)
140-
);
141-
$url = $server->getServerRoot();
142-
$jsignParam->setJavaDownloadUrl($url);
143-
144-
$jsignParam->setJavaVersion('21.0.0');
145-
$this->expectException(UnexpectedValueException::class);
146-
$this->expectExceptionMessageMatches('/internal corruption/');
121+
$this->expectExceptionMessageMatches('/cannot be extracted/');
147122
$service->getPath($jsignParam);
148123
}
149124

@@ -170,8 +145,8 @@ public function testGetPathWithDownloadUrlWithInvalidJavaPackage(): void {
170145
$jsignParam->setJavaDownloadUrl($url);
171146

172147
$jsignParam->setJavaVersion('21.0.0');
173-
$this->expectException(RuntimeException::class);
174-
$this->expectExceptionMessageMatches('/Java binary not found/');
148+
$this->expectException(InvalidArgumentException::class);
149+
$this->expectExceptionMessageMatches('/Invalid tar content/');
175150
$service->getPath($jsignParam);
176151
}
177152

0 commit comments

Comments
 (0)