|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Jeidison\JSignPDF\Runtime; |
| 6 | + |
| 7 | +use InvalidArgumentException; |
| 8 | +use Jeidison\JSignPDF\Sign\JSignParam; |
| 9 | +use PharData; |
| 10 | +use PharException; |
| 11 | +use RuntimeException; |
| 12 | +use UnexpectedValueException; |
| 13 | +use ZipArchive; |
| 14 | + |
| 15 | +class JSignPdfRuntimeService |
| 16 | +{ |
| 17 | + public function getPath(JSignParam $params): string |
| 18 | + { |
| 19 | + $jsignPdfPath = $params->getjSignPdfJarPath(); |
| 20 | + $downloadUrl = $params->getJSignPdfDownloadUrl(); |
| 21 | + |
| 22 | + if ($jsignPdfPath && !$downloadUrl) { |
| 23 | + if (file_exists($jsignPdfPath)) { |
| 24 | + return $jsignPdfPath; |
| 25 | + } |
| 26 | + throw new InvalidArgumentException('Jar of JSignPDF not found on path: '. $jsignPdfPath); |
| 27 | + } |
| 28 | + |
| 29 | + if ($downloadUrl && $jsignPdfPath) { |
| 30 | + $baseDir = preg_replace('/\/JSignPdf.jar$/', '', $jsignPdfPath); |
| 31 | + if (!is_dir($baseDir)) { |
| 32 | + throw new InvalidArgumentException('The JSignPdf base dir is not a real directory: '. $baseDir); |
| 33 | + } |
| 34 | + self::downloadAndExtract($params); |
| 35 | + return $jsignPdfPath; |
| 36 | + } |
| 37 | + |
| 38 | + throw new InvalidArgumentException('Java not found.'); |
| 39 | + } |
| 40 | + |
| 41 | + private function downloadAndExtract(JSignParam $params): void |
| 42 | + { |
| 43 | + $jsignPdfPath = $params->getjSignPdfJarPath(); |
| 44 | + $url = $params->getJSignPdfDownloadUrl(); |
| 45 | + |
| 46 | + $baseDir = preg_replace('/\/JSignPdf.jar$/', '', $jsignPdfPath); |
| 47 | + |
| 48 | + if (!is_dir($baseDir)) { |
| 49 | + $ok = mkdir($baseDir, 0755, true); |
| 50 | + if (!$ok) { |
| 51 | + throw new RuntimeException('Failure to create the folder: ' . $baseDir); |
| 52 | + } |
| 53 | + } |
| 54 | + if (!filter_var($url, FILTER_VALIDATE_URL)) { |
| 55 | + throw new InvalidArgumentException('The url to download Java is invalid: ' . $url); |
| 56 | + } |
| 57 | + $this->chunkDownload($url, $baseDir . '/jsignpdf.zip'); |
| 58 | + $z = new ZipArchive(); |
| 59 | + $ok = $z->open($baseDir . '/jsignpdf.zip'); |
| 60 | + if ($ok !== true) { |
| 61 | + throw new InvalidArgumentException('The file ' . $baseDir . '/jsignpdf.zip cannot be extracted'); |
| 62 | + } |
| 63 | + $ok = $z->extractTo(pathto: $baseDir, files: [$z->getNameIndex(0) . 'JSignPdf.jar']); |
| 64 | + if ($ok !== true) { |
| 65 | + throw new InvalidArgumentException('JSignPdf.jar not found inside path: ' . $z->getNameIndex(0) . 'JSignPdf.jar'); |
| 66 | + } |
| 67 | + @exec('mv ' . escapeshellarg($baseDir . '/'. $z->getNameIndex(0)) . '/JSignPdf.jar ' . escapeshellarg($baseDir)); |
| 68 | + @exec('rm -rf ' . escapeshellarg($baseDir . '/'. $z->getNameIndex(0))); |
| 69 | + unlink($baseDir . '/jsignpdf.zip'); |
| 70 | + if (!file_exists($baseDir . '/JSignPdf.jar')) { |
| 71 | + throw new RuntimeException('Java binary not found at: ' . $baseDir . '/bin/java'); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private function chunkDownload(string $url, string $destination): void |
| 76 | + { |
| 77 | + $fp = fopen($destination, 'w'); |
| 78 | + |
| 79 | + if ($fp) { |
| 80 | + $ch = curl_init($url); |
| 81 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 82 | + curl_setopt($ch, CURLOPT_FILE, $fp); |
| 83 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 84 | + $response = curl_exec($ch); |
| 85 | + if ($response === false) { |
| 86 | + throw new InvalidArgumentException('Failure to download file using the url ' . $url); |
| 87 | + } |
| 88 | + curl_close($ch); |
| 89 | + fclose($fp); |
| 90 | + } else { |
| 91 | + throw new InvalidArgumentException("Failute to download file using the url $url"); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments