private function updateInfoXML()

in pp3/module/Application/src/Application/Pp/Catalog.php [267:343]


    private function updateInfoXML($pluginVersion) {
        $sha1Reference = false;
        $sha256Reference = false;

        foreach ($pluginVersion->getDigests() as $digest) {
            if ($digest->getAlgorithm() == 'SHA-1') {
                $sha1Reference = $digest->getValue();
            } else if ($digest->getAlgorithm() == 'SHA-256') {
                $sha256Reference = $digest->getValue();
            }
        }

        // Skip update if:
        // - info.xml is present (PP3 assumes immutable sources)
        // - SHA-256 checksum is present
        // - artifact size is present
        if($pluginVersion->getInfoXml() != null && $pluginVersion->getArtifactSize() && $sha256Reference) {
            return;
        }
        // Fetch NBM
        $client = new Client($pluginVersion->getUrl(), array(
            'maxredirects' => 0,
            'timeout' => 30
        ));
        $archiveFile = tempnam(sys_get_temp_dir(), "mvn-download");
        $client->setStream("$archiveFile");
        $response = $client->send();
        if ($response->isSuccess()) {
            $filesize = filesize($archiveFile);
            $sha1 = hash_file("sha1", $archiveFile);
            $sha256 = hash_file("sha256", $archiveFile);

            if($sha1Reference && strtolower($sha1) != $sha1Reference) {
                error_log(sprintf('PluginVersion(id: %d) SHA-1 message digest does not match artifact. Expected: %s, Got: %s', $pluginVersion->getId(), $sha1Reference, $sha1));
                return;
            }

            if(! $sha1Reference) {
                $pluginVersion->addDigest("SHA-1", $sha1);
            }

            if(! $sha256Reference) {
                $pluginVersion->addDigest('SHA-256', $sha256);
            }

            if(substr(strtolower($pluginVersion->getUrl()), -4) == '.jar') {
               $execution = self::OSGI_JAR_PARSER . " " . escapeshellarg($archiveFile);
               $return_var = -1;
               $output = [];
               exec($execution, $output, $return_var);
               $outputString = implode("\n", $output);
               if(intval($return_var) != 0) {
                   error_log(sprintf('PluginVersion(id: %d) Failed to extract info from JAR, Got: %s', $pluginVersion->getId(), $outputString));
                   return;
               }
               $infoXML = $outputString;
            } else {
                // Extract Info/info.xml from archive
                $archive = new ZipArchive();
                $archive->open($archiveFile);
                $infoXML = $archive->getFromName("Info/info.xml");
                $archive->close();
            }

            unlink($archiveFile);

            if ($infoXML) {
                // Update persistent data to fetch info.xml only once
                $stream = fopen('php://memory', 'r+');
                fwrite($stream, $infoXML);
                rewind($stream);
                $pluginVersion->setArtifactSize($filesize);
                $pluginVersion->setInfoXml($stream);
                $this->_pluginVersionRepository->merge($pluginVersion);
            }
        }
    }