All checks were successful
Test and publish WordPress plugin / release (push) Successful in 23s
146 lines
9.1 KiB
PHP
146 lines
9.1 KiB
PHP
<?php
|
|
defined('ABSPATH') || exit;
|
|
|
|
/** WordPress-managed updates from the owner's published Gitea release assets. */
|
|
final class Jenhome_Gitea_Updater {
|
|
const REPO = 'https://git.labs.casa/josh/jenhomeforms';
|
|
const API = 'https://git.labs.casa/api/v1/repos/josh/jenhomeforms/releases/latest';
|
|
const SLUG = 'jenhome-contact-exports';
|
|
const CACHE = 'jenhome_gitea_release_v1';
|
|
private static $plugin = '';
|
|
|
|
public static function boot($file) {
|
|
self::$plugin = plugin_basename($file);
|
|
add_filter('update_plugins_git.labs.casa', [__CLASS__, 'update'], 10, 4);
|
|
add_filter('plugins_api', [__CLASS__, 'information'], 10, 3);
|
|
add_filter('upgrader_pre_download', [__CLASS__, 'download'], 10, 4);
|
|
add_action('load-update-core.php', [__CLASS__, 'clear']);
|
|
add_action('upgrader_process_complete', [__CLASS__, 'completed'], 10, 2);
|
|
}
|
|
|
|
public static function clear() { delete_transient(self::CACHE); }
|
|
|
|
public static function completed($upgrader, $extra) {
|
|
if (in_array(self::$plugin, $extra['plugins'] ?? [], true) || ($extra['plugin'] ?? '') === self::$plugin) self::clear();
|
|
}
|
|
|
|
/** No credentials are needed: the owner created this repository as public. */
|
|
public static function allowed_url($url) {
|
|
$p = wp_parse_url($url);
|
|
if (!$p || ($p['scheme'] ?? '') !== 'https' || ($p['host'] ?? '') !== 'git.labs.casa') return false;
|
|
foreach (['user', 'pass', 'port', 'query', 'fragment'] as $key) if (isset($p[$key])) return false;
|
|
if ($url === self::API) return true;
|
|
return (bool)preg_match('~^/(?:josh/jenhomeforms/releases/download/v\d+\.\d+\.\d+/jenhome-contact-exports-\d+\.\d+\.\d+\.zip(?:\.sha256)?|attachments/[a-f0-9-]{36})$~D', $p['path'] ?? '');
|
|
}
|
|
|
|
/** Follow only bounded HTTPS redirects on the same, explicitly allowed host/path. */
|
|
private static function request($url, $args = []) {
|
|
for ($hop = 0; $hop < 4; ++$hop) {
|
|
if (!self::allowed_url($url)) return new WP_Error('jenhome_update_url', 'The plugin update URL is not allowed.');
|
|
$response = wp_safe_remote_get($url, $args + [
|
|
'timeout' => 20, 'redirection' => 0, 'sslverify' => true,
|
|
'limit_response_size' => 1048576,
|
|
'headers' => ['Accept' => 'application/json, application/octet-stream'],
|
|
]);
|
|
if (is_wp_error($response)) return $response;
|
|
$status = wp_remote_retrieve_response_code($response);
|
|
if ($status === 200) return $response;
|
|
if (!in_array($status, [301, 302, 303, 307, 308], true)) return new WP_Error('jenhome_update_http', 'The Gitea update service returned HTTP ' . (int)$status . '.');
|
|
$next = wp_remote_retrieve_header($response, 'location');
|
|
if (!is_string($next) || $next === '') break;
|
|
$url = str_starts_with($next, '/') && !str_starts_with($next, '//') ? 'https://git.labs.casa' . $next : $next;
|
|
}
|
|
return new WP_Error('jenhome_update_redirect', 'The plugin update redirect could not be verified.');
|
|
}
|
|
|
|
public static function parse_release($release) {
|
|
if (!is_array($release) || !empty($release['draft']) || !empty($release['prerelease'])
|
|
|| !is_string($release['tag_name'] ?? null)
|
|
|| !preg_match('/^v(\d+\.\d+\.\d+)$/D', $release['tag_name'] ?? '', $matches)) return false;
|
|
$version = $matches[1];
|
|
$filename = self::SLUG . '-' . $version . '.zip';
|
|
$names = array_column(is_array($release['assets'] ?? null) ? $release['assets'] : [], 'name');
|
|
if (!in_array($filename, $names, true) || !in_array($filename . '.sha256', $names, true)) return false;
|
|
$package = self::REPO . '/releases/download/v' . $version . '/' . $filename;
|
|
return ['version' => $version, 'package' => $package, 'checksum_url' => $package . '.sha256',
|
|
'url' => self::REPO . '/releases/tag/v' . $version,
|
|
'body' => is_string($release['body'] ?? null) ? $release['body'] : '',
|
|
'published_at' => is_string($release['published_at'] ?? null) ? $release['published_at'] : ''];
|
|
}
|
|
|
|
public static function latest() {
|
|
$cached = get_transient(self::CACHE);
|
|
if ($cached !== false) return $cached ?: false;
|
|
$response = self::request(self::API);
|
|
$release = !is_wp_error($response) ? self::parse_release(json_decode(wp_remote_retrieve_body($response), true)) : false;
|
|
if ($release) {
|
|
$checksum = self::request($release['checksum_url'], ['limit_response_size' => 1024]);
|
|
$text = is_wp_error($checksum) ? '' : trim(wp_remote_retrieve_body($checksum));
|
|
$filename = self::SLUG . '-' . $release['version'] . '.zip';
|
|
if (!preg_match('/^([a-f0-9]{64})\s+\*?' . preg_quote($filename, '/') . '$/Di', $text, $match)) $release = false;
|
|
else $release['sha256'] = strtolower($match[1]);
|
|
}
|
|
set_transient(self::CACHE, $release ?: [], $release ? 3600 : 300);
|
|
return $release;
|
|
}
|
|
|
|
public static function update($update, $data, $file, $locales) {
|
|
if ($file !== self::$plugin || ($data['UpdateURI'] ?? '') !== self::REPO) return $update;
|
|
$release = self::latest();
|
|
if (!$release) return false;
|
|
return ['id' => self::REPO, 'slug' => self::SLUG, 'version' => $release['version'],
|
|
'url' => $release['url'], 'package' => $release['package'], 'requires' => '6.2',
|
|
'requires_php' => '8.1', 'tested' => '7.1.3'];
|
|
}
|
|
|
|
public static function information($result, $action, $args) {
|
|
if ($action !== 'plugin_information' || ($args->slug ?? '') !== self::SLUG) return $result;
|
|
$release = self::latest();
|
|
if (!$release) return $result;
|
|
return (object)[
|
|
'name' => 'Jen Home Contact Fields and Exports', 'slug' => self::SLUG,
|
|
'version' => $release['version'], 'author' => 'Josh Stevens Sr', 'author_profile' => 'https://hostlabs.pro/',
|
|
'homepage' => 'https://hostlabs.pro/', 'requires' => '6.2', 'requires_php' => '8.1', 'tested' => '7.1.3',
|
|
'download_link' => $release['package'], 'last_updated' => $release['published_at'],
|
|
'sections' => ['description' => 'Site-specific Houzez contact fields, account 7 inquiry ownership and CSV exports for Jen Home staging and production.',
|
|
'changelog' => wpautop(esc_html($release['body']))],
|
|
];
|
|
}
|
|
|
|
public static function validate_package($path, $release) {
|
|
if (!hash_equals($release['sha256'], hash_file('sha256', $path))) return new WP_Error('jenhome_update_checksum', 'The plugin update checksum does not match its release.');
|
|
if (!class_exists('ZipArchive')) return new WP_Error('jenhome_update_zip', 'PHP ZIP support is required to verify this update.');
|
|
$zip = new ZipArchive();
|
|
if ($zip->open($path) !== true) return new WP_Error('jenhome_update_zip', 'The plugin update is not a valid ZIP file.');
|
|
try {
|
|
$required = ['jenhome-contact-exports.php', 'forms.js', 'crm-display.php', 'gitea-updater.php'];
|
|
$seen = []; $total = 0;
|
|
for ($i = 0; $i < $zip->numFiles; ++$i) {
|
|
$entry = $zip->statIndex($i); $name = $entry['name']; $total += $entry['size'];
|
|
if (!in_array($name, array_map(function($f) { return self::SLUG . '/' . $f; }, $required), true) || isset($seen[$name]) || $total > 10485760) {
|
|
return new WP_Error('jenhome_update_contents', 'The plugin ZIP contains unexpected files.');
|
|
}
|
|
$opsys = $attributes = 0;
|
|
if ($zip->getExternalAttributesIndex($i, $opsys, $attributes) && (($attributes >> 16) & 0170000) === 0120000) return new WP_Error('jenhome_update_contents', 'The plugin ZIP contains a symbolic link.');
|
|
$seen[$name] = true;
|
|
}
|
|
if (count($seen) !== count($required)) return new WP_Error('jenhome_update_contents', 'The plugin ZIP is incomplete.');
|
|
$main = $zip->getFromName(self::SLUG . '/jenhome-contact-exports.php');
|
|
if (!preg_match('/^\s*\*\s*Version:\s*([^\s]+)\s*$/m', $main, $m) || $m[1] !== $release['version']) return new WP_Error('jenhome_update_version', 'The plugin ZIP version does not match the release.');
|
|
return true;
|
|
} finally { $zip->close(); }
|
|
}
|
|
|
|
public static function download($reply, $package, $upgrader, $extra) {
|
|
if ($reply !== false || ($extra['plugin'] ?? '') !== self::$plugin) return $reply;
|
|
$release = self::latest();
|
|
if (!$release || $package !== $release['package']) return new WP_Error('jenhome_update_release', 'The plugin package is not the verified published Gitea release.');
|
|
$path = wp_tempnam(self::SLUG . '.zip');
|
|
if (!$path) return new WP_Error('jenhome_update_temp', 'A temporary update file could not be created.');
|
|
$response = self::request($package, ['timeout' => 60, 'stream' => true, 'filename' => $path, 'limit_response_size' => 5242880]);
|
|
$check = is_wp_error($response) ? $response : self::validate_package($path, $release);
|
|
if (is_wp_error($check)) { wp_delete_file($path); return $check; }
|
|
return $path;
|
|
}
|
|
}
|