137 lines
4.2 KiB
PHP
137 lines
4.2 KiB
PHP
<?php
|
|||
|
|
declare( strict_types=1 );
|
||
|
|
|
||
|
|
namespace StreamGoals\Update;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Pure (WordPress-free) parsing of a Gitea release payload.
|
||
|
|
*/
|
||
|
|
final class ReleaseParser {
|
||
|
|
|
||
|
|
public const ASSET_NAME = 'streamgoals-donate.zip';
|
||
|
|
|
||
|
|
private const DATE_TAG = '/^\d{4}\.\d{2}\.\d{2}-\d{4}$/';
|
||
|
|
private const SEMVER = '/^\d+\.\d+\.\d+$/';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array $release Decoded Gitea release JSON.
|
||
|
|
* @param string $host The only host a package URL may point to (e.g. repo.anhonesthost.net).
|
||
|
|
* @return array{version:string,package:string,url:string,notes:string,published:string}|null
|
||
|
|
*/
|
||
|
|
public static function parse( array $release, string $host ): ?array {
|
||
|
|
if ( ! empty( $release['draft'] ) || ! empty( $release['prerelease'] ) ) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
$version = self::normalizeVersion( $release['tag_name'] ?? null );
|
||
|
|
if ( null === $version ) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
$assets = isset( $release['assets'] ) && is_array( $release['assets'] ) ? $release['assets'] : array();
|
||
|
|
$package = self::pickPackage( $assets, $host );
|
||
|
|
if ( null === $package ) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
$url = $release['html_url'] ?? '';
|
||
|
|
return array(
|
||
|
|
'version' => $version,
|
||
|
|
'package' => $package,
|
||
|
|
'url' => is_string( $url ) && self::isAllowedUrl( $url, $host ) ? $url : '',
|
||
|
|
'notes' => is_string( $release['body'] ?? null ) ? $release['body'] : '',
|
||
|
|
'published' => is_string( $release['published_at'] ?? null ) ? $release['published_at'] : '',
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function normalizeVersion( mixed $tag ): ?string {
|
||
|
|
if ( ! is_string( $tag ) ) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
$t = trim( $tag );
|
||
|
|
if ( '' !== $t && ( 'v' === $t[0] || 'V' === $t[0] ) ) {
|
||
|
|
$t = substr( $t, 1 );
|
||
|
|
}
|
||
|
|
return 1 === preg_match( self::DATE_TAG, $t ) || 1 === preg_match( self::SEMVER, $t ) ? $t : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function isNewer( string $remote, string $current ): bool {
|
||
|
|
$r = self::normalizeVersion( $remote );
|
||
|
|
if ( null === $r ) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return version_compare( $r, ltrim( $current, 'vV' ), '>' );
|
||
|
|
}
|
||
|
|
|
||
|
|
/** https URL on exactly the configured host (no userinfo tricks). */
|
||
|
|
public static function isAllowedUrl( string $url, string $host ): bool {
|
||
|
|
$p = parse_url( $url );
|
||
|
|
if ( ! is_array( $p ) || ! isset( $p['scheme'], $p['host'] ) || isset( $p['user'] ) || isset( $p['pass'] ) ) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return 'https' === strtolower( $p['scheme'] ) && '' !== $host && strtolower( $p['host'] ) === strtolower( $host );
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function pickPackage( array $assets, string $host ): ?string {
|
||
|
|
$exact = null;
|
||
|
|
$first = null;
|
||
|
|
foreach ( $assets as $a ) {
|
||
|
|
if ( ! is_array( $a ) || ! is_string( $a['name'] ?? null ) || ! is_string( $a['browser_download_url'] ?? null ) ) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if ( ! self::isAllowedUrl( $a['browser_download_url'], $host ) ) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if ( self::ASSET_NAME === $a['name'] ) {
|
||
|
|
$exact = $exact ?? $a['browser_download_url'];
|
||
|
|
} elseif ( null === $first && str_ends_with( strtolower( $a['name'] ), '.zip' ) ) {
|
||
|
|
$first = $a['browser_download_url'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $exact ?? $first;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Minimal markdown to safe HTML: everything is escaped first; only paragraphs, headings (as <h4>) and lists. */
|
||
|
|
public static function changelogHtml( string $markdown ): string {
|
||
|
|
$lines = preg_split( '/\r\n|\r|\n/', $markdown ) ?: array();
|
||
|
|
$out = '';
|
||
|
|
$list = false;
|
||
|
|
$para = array();
|
||
|
|
$flush = static function () use ( &$out, &$para ): void {
|
||
|
|
if ( $para ) {
|
||
|
|
$out .= '<p>' . implode( ' ', $para ) . '</p>';
|
||
|
|
$para = array();
|
||
|
|
}
|
||
|
|
};
|
||
|
|
foreach ( $lines as $line ) {
|
||
|
|
$t = trim( $line );
|
||
|
|
if ( 1 === preg_match( '/^[*\-+]\s+(.*)$/', $t, $m ) ) {
|
||
|
|
$flush();
|
||
|
|
if ( ! $list ) {
|
||
|
|
$out .= '<ul>';
|
||
|
|
$list = true;
|
||
|
|
}
|
||
|
|
$out .= '<li>' . self::esc( $m[1] ) . '</li>';
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if ( $list ) {
|
||
|
|
$out .= '</ul>';
|
||
|
|
$list = false;
|
||
|
|
}
|
||
|
|
if ( '' === $t ) {
|
||
|
|
$flush();
|
||
|
|
} elseif ( 1 === preg_match( '/^#{1,6}\s+(.*)$/', $t, $m ) ) {
|
||
|
|
$flush();
|
||
|
|
$out .= '<h4>' . self::esc( $m[1] ) . '</h4>';
|
||
|
|
} else {
|
||
|
|
$para[] = self::esc( $t );
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$flush();
|
||
|
|
if ( $list ) {
|
||
|
|
$out .= '</ul>';
|
||
|
|
}
|
||
|
|
return $out;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function esc( string $s ): string {
|
||
|
|
return htmlspecialchars( $s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' );
|
||
|
|
}
|
||
|
|
}
|