-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJSignPdfRuntimeService.php
More file actions
113 lines (101 loc) · 4.35 KB
/
JSignPdfRuntimeService.php
File metadata and controls
113 lines (101 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
namespace Jeidison\JSignPDF\Runtime;
use InvalidArgumentException;
use Jeidison\JSignPDF\Sign\JSignParam;
use RuntimeException;
use ZipArchive;
class JSignPdfRuntimeService
{
public function getPath(JSignParam $params): string
{
$jsignPdfPath = $params->getjSignPdfJarPath();
$downloadUrl = $params->getJSignPdfDownloadUrl();
if ($jsignPdfPath && !$downloadUrl) {
if (file_exists($jsignPdfPath)) {
return $jsignPdfPath;
}
throw new InvalidArgumentException('Jar of JSignPDF not found on path: '. $jsignPdfPath);
}
if ($downloadUrl && $jsignPdfPath) {
$baseDir = preg_replace('/\/JSignPdf.jar$/', '', $jsignPdfPath);
if (!is_string($baseDir)) {
throw new InvalidArgumentException('Invalid JsignParamPath');
}
if (!is_dir($baseDir)) {
$ok = mkdir($baseDir, 0755, true);
if ($ok === false) {
throw new InvalidArgumentException('The JSignPdf base dir cannot be created: '. $baseDir);
}
}
if (!file_exists($jsignPdfPath) || !self::validateVersion($params)) {
self::downloadAndExtract($params);
}
return $jsignPdfPath;
}
throw new InvalidArgumentException('Java not found.');
}
private function validateVersion(JSignParam $params): bool
{
$jsignPdfPath = $params->getjSignPdfJarPath();
$versionCacheFile = $jsignPdfPath . '/.jsignpdf_version_' . basename($params->getJSignPdfDownloadUrl());
return file_exists($versionCacheFile);
}
private function downloadAndExtract(JSignParam $params): void
{
$jsignPdfPath = $params->getjSignPdfJarPath();
$url = $params->getJSignPdfDownloadUrl();
$baseDir = preg_replace('/\/JSignPdf.jar$/', '', $jsignPdfPath);
if (!is_string($baseDir)) {
throw new InvalidArgumentException('Invalid JsignParamPath');
}
if (!is_dir($baseDir)) {
$ok = mkdir($baseDir, 0755, true);
if (!$ok) {
throw new RuntimeException('Failure to create the folder: ' . $baseDir);
}
}
if (!filter_var($url, FILTER_VALIDATE_URL)) {
throw new InvalidArgumentException('The url to download Java is invalid: ' . $url);
}
$this->chunkDownload($url, $baseDir . '/jsignpdf.zip');
$z = new ZipArchive();
$ok = $z->open($baseDir . '/jsignpdf.zip');
if ($ok !== true) {
throw new InvalidArgumentException('The file ' . $baseDir . '/jsignpdf.zip cannot be extracted');
}
$ok = $z->extractTo(pathto: $baseDir, files: [$z->getNameIndex(0) . 'JSignPdf.jar']);
if ($ok !== true) {
throw new InvalidArgumentException('JSignPdf.jar not found inside path: ' . $z->getNameIndex(0) . 'JSignPdf.jar');
}
@exec('mv ' . escapeshellarg($baseDir . '/'. $z->getNameIndex(0)) . '/JSignPdf.jar ' . escapeshellarg($baseDir));
@exec('rm -rf ' . escapeshellarg($baseDir . '/'. $z->getNameIndex(0)));
@exec('rm -f ' . escapeshellarg($baseDir) . '/.jsignpdf_version_*');
unlink($baseDir . '/jsignpdf.zip');
if (!file_exists($baseDir . '/JSignPdf.jar')) {
throw new RuntimeException('Java binary not found at: ' . $baseDir . '/bin/java');
}
touch($baseDir . '/.jsignpdf_version_' . basename($url));
}
private function chunkDownload(string $url, string $destination): void
{
$fp = fopen($destination, 'w');
if ($fp) {
$ch = curl_init($url);
if ($ch === false) {
throw new InvalidArgumentException('Failure to download file using the url ' . $url);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
if ($response === false) {
throw new InvalidArgumentException('Failure to download file using the url ' . $url);
}
curl_close($ch);
fclose($fp);
} else {
throw new InvalidArgumentException("Failute to download file using the url $url");
}
}
}