2021-02-21 13:17:07 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-22 18:03:04 +03:00
|
|
|
class DockerHubBridge extends BridgeAbstract
|
|
|
|
{
|
|
|
|
const NAME = 'Docker Hub Bridge';
|
2021-02-21 13:17:07 +03:00
|
|
|
const URI = 'https://hub.docker.com';
|
|
|
|
const DESCRIPTION = 'Returns new images for a container';
|
|
|
|
const MAINTAINER = 'VerifiedJoseph';
|
2021-02-28 16:26:24 +03:00
|
|
|
const PARAMETERS = [
|
|
|
|
'User Submitted Image' => [
|
2021-02-21 13:17:07 +03:00
|
|
|
'user' => [
|
|
|
|
'name' => 'User',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'rssbridge',
|
|
|
|
],
|
|
|
|
'repo' => [
|
|
|
|
'name' => 'Repository',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'rss-bridge',
|
2023-02-15 22:15:38 +03:00
|
|
|
],
|
|
|
|
'filter' => [
|
|
|
|
'name' => 'Filter tag',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => false,
|
|
|
|
'exampleValue' => 'latest',
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2021-02-28 16:26:24 +03:00
|
|
|
],
|
|
|
|
'Official Image' => [
|
|
|
|
'repo' => [
|
|
|
|
'name' => 'Repository',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'postgres',
|
2023-02-15 22:15:38 +03:00
|
|
|
],
|
|
|
|
'filter' => [
|
|
|
|
'name' => 'Filter tag',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => false,
|
|
|
|
'exampleValue' => 'alpine3.17',
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2023-02-15 22:15:38 +03:00
|
|
|
]
|
2021-02-21 13:17:07 +03:00
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
const CACHE_TIMEOUT = 3600; // 1 hour
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
private $apiURL = 'https://hub.docker.com/v2/repositories/';
|
2021-03-15 19:54:26 +03:00
|
|
|
private $imageUrlRegex = '/hub\.docker\.com\/r\/([\w]+)\/([\w-]+)\/?/';
|
|
|
|
private $officialImageUrlRegex = '/hub\.docker\.com\/_\/([\w-]+)\/?/';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-03-15 19:54:26 +03:00
|
|
|
public function detectParameters($url)
|
|
|
|
{
|
|
|
|
$params = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-03-15 19:54:26 +03:00
|
|
|
// user submitted image
|
|
|
|
if (preg_match($this->imageUrlRegex, $url, $matches)) {
|
|
|
|
$params['context'] = 'User Submitted Image';
|
|
|
|
$params['user'] = $matches[1];
|
|
|
|
$params['repo'] = $matches[2];
|
|
|
|
return $params;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-03-15 19:54:26 +03:00
|
|
|
// official image
|
|
|
|
if (preg_match($this->officialImageUrlRegex, $url, $matches)) {
|
|
|
|
$params['context'] = 'Official Image';
|
|
|
|
$params['repo'] = $matches[1];
|
|
|
|
return $params;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-03-15 19:54:26 +03:00
|
|
|
return null;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
2022-01-02 12:36:09 +03:00
|
|
|
$json = getContents($this->getApiUrl());
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
$data = json_decode($json, false);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
foreach ($data->results as $result) {
|
|
|
|
$item = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
$lastPushed = date('Y-m-d H:i:s', strtotime($result->tag_last_pushed));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
$item['title'] = $result->name;
|
|
|
|
$item['uid'] = $result->id;
|
|
|
|
$item['uri'] = $this->getTagUrl($result->name);
|
|
|
|
$item['author'] = $result->last_updater_username;
|
|
|
|
$item['timestamp'] = $result->tag_last_pushed;
|
|
|
|
$item['content'] = <<<EOD
|
|
|
|
<Strong>Tag</strong><br>
|
|
|
|
<p>{$result->name}</p>
|
|
|
|
<Strong>Last pushed</strong><br>
|
|
|
|
<p>{$lastPushed}</p>
|
|
|
|
<Strong>Images</strong><br>
|
2023-03-04 19:33:28 +03:00
|
|
|
{$this->getImagesTable($result)}
|
2021-02-21 13:17:07 +03:00
|
|
|
EOD;
|
|
|
|
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
public function getURI()
|
|
|
|
{
|
2023-02-15 22:15:38 +03:00
|
|
|
$uri = parent::getURI();
|
|
|
|
|
2021-02-28 16:26:24 +03:00
|
|
|
if ($this->queriedContext === 'Official Image') {
|
2023-02-15 22:15:38 +03:00
|
|
|
$uri = self::URI . '/_/' . $this->getRepo();
|
2021-02-28 16:26:24 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-02-15 22:15:38 +03:00
|
|
|
if ($this->queriedContext === 'User Submitted Image') {
|
|
|
|
$uri = '/r/' . $this->getRepo();
|
2021-02-21 13:17:07 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-02-15 22:15:38 +03:00
|
|
|
if ($this->getInput('filter')) {
|
|
|
|
$uri .= '/tags/?&page=1&name=' . $this->getInput('filter');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $uri;
|
2021-02-21 13:17:07 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
public function getName()
|
|
|
|
{
|
2021-02-28 16:26:24 +03:00
|
|
|
if ($this->getInput('repo')) {
|
2023-02-15 22:15:38 +03:00
|
|
|
$name = $this->getRepo();
|
|
|
|
|
|
|
|
if ($this->getInput('filter')) {
|
|
|
|
$name .= ':' . $this->getInput('filter');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $name . ' - Docker Hub';
|
2021-02-21 13:17:07 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
return parent::getName();
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
private function getRepo()
|
|
|
|
{
|
2021-02-28 16:26:24 +03:00
|
|
|
if ($this->queriedContext === 'Official Image') {
|
|
|
|
return $this->getInput('repo');
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
return $this->getInput('user') . '/' . $this->getInput('repo');
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
private function getApiUrl()
|
|
|
|
{
|
2023-02-15 22:15:38 +03:00
|
|
|
$url = '';
|
|
|
|
|
2021-02-28 16:26:24 +03:00
|
|
|
if ($this->queriedContext === 'Official Image') {
|
2023-02-15 22:15:38 +03:00
|
|
|
$url = $this->apiURL . 'library/' . $this->getRepo() . '/tags/?page_size=25&page=1';
|
2021-02-28 16:26:24 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-02-15 22:15:38 +03:00
|
|
|
if ($this->queriedContext === 'User Submitted Image') {
|
|
|
|
$url = $this->apiURL . $this->getRepo() . '/tags/?page_size=25&page=1';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->getInput('filter')) {
|
|
|
|
$url .= '&name=' . $this->getInput('filter');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
2021-02-21 13:17:07 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
private function getLayerUrl($name, $digest)
|
|
|
|
{
|
2021-02-28 16:26:24 +03:00
|
|
|
if ($this->queriedContext === 'Official Image') {
|
|
|
|
return self::URI . '/layers/' . $this->getRepo() . '/library/' .
|
|
|
|
$this->getRepo() . '/' . $name . '/images/' . $digest;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
return self::URI . '/layers/' . $this->getRepo() . '/' . $name . '/images/' . $digest;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-21 13:17:07 +03:00
|
|
|
private function getTagUrl($name)
|
|
|
|
{
|
2023-02-15 22:15:38 +03:00
|
|
|
$url = '';
|
|
|
|
|
2021-02-28 16:26:24 +03:00
|
|
|
if ($this->queriedContext === 'Official Image') {
|
2023-02-15 22:15:38 +03:00
|
|
|
$url = self::URI . '/_/' . $this->getRepo();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->queriedContext === 'User Submitted Image') {
|
|
|
|
$url = self::URI . '/r/' . $this->getRepo();
|
2021-02-28 16:26:24 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-02-15 22:15:38 +03:00
|
|
|
return $url . '/tags/?&name=' . $name;
|
2021-02-21 13:17:07 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-03-04 19:33:28 +03:00
|
|
|
private function getImagesTable($result)
|
2021-02-21 13:17:07 +03:00
|
|
|
{
|
2023-03-04 19:33:28 +03:00
|
|
|
$data = '';
|
2021-02-21 13:17:07 +03:00
|
|
|
|
|
|
|
foreach ($result->images as $image) {
|
|
|
|
$layersUrl = $this->getLayerUrl($result->name, $image->digest);
|
|
|
|
$id = $this->getShortDigestId($image->digest);
|
2023-03-04 19:33:28 +03:00
|
|
|
$size = format_bytes($image->size);
|
|
|
|
$data .= <<<EOD
|
|
|
|
<tr>
|
|
|
|
<td><a href="{$layersUrl}">{$id}</a></td>
|
|
|
|
<td>{$image->os}/{$image->architecture}</td>
|
|
|
|
<td>{$size}</td>
|
|
|
|
</tr>
|
2021-02-21 13:17:07 +03:00
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
|
2023-03-04 19:33:28 +03:00
|
|
|
return <<<EOD
|
|
|
|
<table style="width:400px;">
|
|
|
|
<thead>
|
|
|
|
<tr style="text-align: left;">
|
|
|
|
<th>Digest</th>
|
|
|
|
<th>OS/architecture</th>
|
|
|
|
<th>Compressed Size</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
</tbody>
|
|
|
|
{$data}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
EOD;
|
2021-02-21 13:17:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getShortDigestId($digest)
|
|
|
|
{
|
|
|
|
$parts = explode(':', $digest);
|
|
|
|
return substr($parts[1], 0, 12);
|
|
|
|
}
|
|
|
|
}
|