$TheBody"); } return false; } /** * @param string $ImageName image name including repo address */ public function getImage(string $ImageNameComplete, $imageTag='latest') { $RepoURL = "https://".$ImageNameComplete; if (preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/", $RepoURL) ) { //Good URL, now lets work it $URLbits = parse_url($RepoURL); if ($URLbits) { $imageName = ltrim($URLbits['path'], "/"); $registry = $URLbits['host'] .":".$URLbits['port']; } } $tagsURL = "https://".$registry. "/v2/".$imageName."/tags/list"; try { $TagsRaw = $this->doRequest($tagsURL, null, null, true); } catch (\Exception $e) { throw new \Exception("Exception getting tags:".$e->getMessage()); } if ($TagsRaw and isset($TagsRaw->tags)) { //echo "
tags
".print_r($TagsRaw,true)."
"; $ImagesReturn = array(); foreach($TagsRaw->tags as $tag) { $ImageManifestURL = "https://".$registry."/v2/".$imageName."/manifests/".$tag; // echo $ImageManifestURL; try { $ImageRaw = $this->doRequest($ImageManifestURL, null, array('Accept: application/vnd.oci.image.manifest.v1+json'),true); // echo "
image maniest
".print_r($ImageRaw,true)."
"; } catch (\Exception $e) { throw new \Exception("Exception getting image manifest:".$e->getMessage()); } if (isset($ImageRaw->config->digest)) { $ImageConfigURL = "https://".$registry."/v2/".$imageName."/blobs/".$ImageRaw->config->digest; // echo $ImageConfigURL; $ImageConfigRaw = $this->doRequest($ImageConfigURL, null, null,true ); //echo "
image config
".print_r($ImageConfigRaw,true)."
"; try { $Image = new image( array('name'=>$ImageNameComplete, 'imageBasic'=>$ImageRaw, 'imageConfig'=>$ImageConfigRaw)); if ($Image) { if ($imageTag === $tag ) return $Image; } else throw new \Exception("Image object failed to create while fetching"); } catch (\Exception $e) { throw new \Exception("Exception creating image object:".$e->getMessage()); } //echo "
image
".print_r($Image,true)."
"; } else throw new \Exception("Could not find image config digest"); } // tags loop } // if tags exist else throw new \Exception("Could not get tags for image"); return false; } /** * @param string $repo * @return image[] * @throws \Exception */ public function getImages(string $repo ) { //podman.registry.generic.host:5000/httpdphp7 $cache = $this->getCache($repo); if ($cache) { return $cache; } // Not cached, continue ; $ImagesReturn = array(); $URL = 'https://'.$repo.'/v2/_catalog'; if (preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/", $URL) ) { $Images = $this->doRequest( $URL, null, null, true ); if (isset($Images->repositories) ) { foreach($Images->repositories as $imageName) { $Image = $this->getImage($repo ."/". $imageName); if ($Image) $ImagesReturn[$imageName] = $Image; } //Images loop } else throw new \Exception("Images not valid"); } else { throw new \Exception("Repo not valid"); } $cache = $this->setCache($repo, $ImagesReturn); return $ImagesReturn; } private function getCache($name) { $CacheDIr = __DIR__ . "/cache"; $name = str_replace(array('.',":"),'-',$name); if (file_exists( $CacheDIr . "/".$name ) and is_readable($CacheDIr . "/".$name)) { if ( (mktime() - filectime($CacheDIr . "/".$name) <= 60) ) { $data = file_get_contents($CacheDIr . "/".$name); return unserialize( $data ); } else { return false; } } return false; } private function setCache($name, $Object) { $CacheDIr = __DIR__ . "/cache"; if (! file_exists($CacheDIr)) mkdir($CacheDIr); if (file_exists($CacheDIr)) { $name = str_replace(array('.',":"),'-',$name); return file_put_contents( $CacheDIr . "/".$name, serialize( $Object ) ); } else return false; //TODO lets make cache work via like a central storage folder or something per user } }