|
| 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 | + |
| 14 | +class JavaRuntimeService |
| 15 | +{ |
| 16 | + public function getPath(JSignParam $params): string |
| 17 | + { |
| 18 | + if ($params->isUseJavaInstalled()) { |
| 19 | + return 'java'; |
| 20 | + } |
| 21 | + |
| 22 | + $javaPath = $params->getJavaPath(); |
| 23 | + $downloadUrl = $params->getJavaDownloadUrl(); |
| 24 | + |
| 25 | + if ($javaPath && !$downloadUrl) { |
| 26 | + if (is_file($javaPath) && is_executable($javaPath)) { |
| 27 | + return $javaPath; |
| 28 | + } |
| 29 | + throw new InvalidArgumentException('Java path defined is not executable: ' . $javaPath); |
| 30 | + } |
| 31 | + |
| 32 | + if ($downloadUrl && $javaPath) { |
| 33 | + $baseDir = preg_replace('/\/bin\/java$/', '', $javaPath); |
| 34 | + if (!is_dir($baseDir)) { |
| 35 | + $ok = mkdir($baseDir, 0755, true); |
| 36 | + if ($ok === false) { |
| 37 | + throw new InvalidArgumentException('The java base dir is not a real directory. Create this directory first: '. $baseDir); |
| 38 | + } |
| 39 | + } |
| 40 | + if (!self::validateVersion($params)) { |
| 41 | + self::downloadAndExtract($downloadUrl, $javaPath); |
| 42 | + } |
| 43 | + $params->setJavaDownloadUrl(''); |
| 44 | + $params->setJavaPath($javaPath); |
| 45 | + return $javaPath; |
| 46 | + } |
| 47 | + |
| 48 | + throw new InvalidArgumentException('Java not found.'); |
| 49 | + } |
| 50 | + |
| 51 | + private function validateVersion(JSignParam $params): bool |
| 52 | + { |
| 53 | + $javaPath = $params->getJavaPath(); |
| 54 | + $baseDir = preg_replace('/\/bin\/java$/', '', $javaPath); |
| 55 | + $lastVersion = $baseDir . '/.java_version_' . basename($params->getJavaDownloadUrl()); |
| 56 | + return file_exists($lastVersion); |
| 57 | + } |
| 58 | + |
| 59 | + private function downloadAndExtract(string $url, string $baseDir): void |
| 60 | + { |
| 61 | + $baseDir = preg_replace('/\/bin\/java$/', '', $baseDir); |
| 62 | + |
| 63 | + if (!is_dir($baseDir)) { |
| 64 | + $ok = mkdir($baseDir, 0755, true); |
| 65 | + if (!$ok) { |
| 66 | + throw new RuntimeException('Failure to create the folder: ' . $baseDir); |
| 67 | + } |
| 68 | + } |
| 69 | + if (!filter_var($url, FILTER_VALIDATE_URL)) { |
| 70 | + throw new InvalidArgumentException('The url to download Java is invalid: ' . $url); |
| 71 | + } |
| 72 | + $this->chunkDownload($url, $baseDir . '/java.tar.gz'); |
| 73 | + try { |
| 74 | + $tar = new PharData($baseDir . '/java.tar.gz'); |
| 75 | + } catch (PharException|UnexpectedValueException $e) { |
| 76 | + throw new InvalidArgumentException('The file ' . $baseDir . '/java.tar.gz cannot be extracted'); |
| 77 | + } |
| 78 | + $rootDirInsideTar = $this->findRootDir($tar, $baseDir . '/java.tar.gz'); |
| 79 | + if (!$rootDirInsideTar) { |
| 80 | + throw new InvalidArgumentException('Invalid tar content.'); |
| 81 | + } |
| 82 | + $tar->extractTo(directory: $baseDir, overwrite: true); |
| 83 | + @exec('mv ' . escapeshellarg($baseDir . '/'. $rootDirInsideTar) . '/* ' . escapeshellarg($baseDir)); |
| 84 | + @exec('rm -rf ' . escapeshellarg($baseDir . '/'. $rootDirInsideTar)); |
| 85 | + @exec('rm -f ' . escapeshellarg($baseDir) . '/.java_version_*'); |
| 86 | + unlink($baseDir . '/java.tar.gz'); |
| 87 | + touch($baseDir . '/.java_version_' . basename($url)); |
| 88 | + if (!file_exists($baseDir . '/bin/java')) { |
| 89 | + throw new RuntimeException('Java binary not found at: ' . $baseDir . '/bin/java'); |
| 90 | + } |
| 91 | + chmod($baseDir . '/bin/java', 0700); |
| 92 | + } |
| 93 | + |
| 94 | + private function findRootDir(PharData $phar, $rootDir) { |
| 95 | + $files = new \RecursiveIteratorIterator($phar, \RecursiveIteratorIterator::CHILD_FIRST); |
| 96 | + $rootDir = realpath($rootDir); |
| 97 | + |
| 98 | + foreach ($files as $file) { |
| 99 | + $pathName = $file->getPathname(); |
| 100 | + if (str_contains($pathName, '/bin/') || str_contains($pathName, '/bin/')) { |
| 101 | + $parts = explode($rootDir, $pathName); |
| 102 | + $internalFullPath = end($parts); |
| 103 | + $parts = explode('/bin/', $internalFullPath); |
| 104 | + return trim($parts[0], '/'); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private function chunkDownload(string $url, string $destination): void |
| 110 | + { |
| 111 | + $fp = fopen($destination, 'w'); |
| 112 | + |
| 113 | + if ($fp) { |
| 114 | + $ch = curl_init($url); |
| 115 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 116 | + curl_setopt($ch, CURLOPT_FILE, $fp); |
| 117 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 118 | + $response = curl_exec($ch); |
| 119 | + if ($response === false) { |
| 120 | + throw new InvalidArgumentException('Failure to download file using the url ' . $url); |
| 121 | + } |
| 122 | + curl_close($ch); |
| 123 | + fclose($fp); |
| 124 | + } else { |
| 125 | + throw new InvalidArgumentException("Failute to download file using the url $url"); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments