akaunting/app/Utilities/Versions.php

235 lines
5.9 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Utilities;
use App\Traits\SiteApi;
2023-04-29 00:52:12 +03:00
use App\Utilities\Date;
2020-03-12 15:13:03 +03:00
use GrahamCampbell\Markdown\Facades\Markdown;
2020-02-21 11:45:37 +03:00
use Illuminate\Support\Arr;
2023-04-29 00:52:12 +03:00
use Illuminate\Support\Facades\Cache;
2017-09-14 22:21:00 +03:00
class Versions
{
use SiteApi;
public static function changelog()
{
$output = '';
$url = 'https://api.github.com/repos/akaunting/akaunting/releases';
$http = new \GuzzleHttp\Client(['verify' => false]);
$json = $http->get($url, ['timeout' => 30])->getBody()->getContents();
if (empty($json)) {
return $output;
}
$releases = json_decode($json);
foreach ($releases as $release) {
2017-11-23 23:28:35 +03:00
if (version_compare($release->tag_name, version('short'), '<=')) {
2017-09-14 22:21:00 +03:00
continue;
}
if ($release->prerelease == true) {
continue;
}
if (empty($release->body)) {
continue;
}
2019-11-20 13:35:43 +03:00
$output .= '<h2><span class="badge badge-pill badge-success">' . $release->tag_name . '</span></h2>';
2017-09-14 22:21:00 +03:00
2020-03-12 15:13:03 +03:00
$output .= Markdown::convertToHtml($release->body);
2017-09-14 22:21:00 +03:00
$output .= '<hr>';
}
return $output;
}
2020-02-21 11:45:37 +03:00
public static function latest($alias)
{
$versions = static::all($alias);
2023-04-25 09:46:22 +03:00
if (empty($versions[$alias])) {
return static::getVersionByAlias($alias);
2020-02-21 11:45:37 +03:00
}
2023-04-25 09:46:22 +03:00
return $versions[$alias];
2020-02-21 11:45:37 +03:00
}
public static function all($modules = null)
2017-09-14 22:21:00 +03:00
{
// Get data from cache
2019-12-17 16:27:50 +03:00
$versions = Cache::get('versions');
2017-09-14 22:21:00 +03:00
2023-04-25 09:46:22 +03:00
if (! empty($versions)) {
2019-12-17 16:27:50 +03:00
return $versions;
2017-09-14 22:21:00 +03:00
}
$info = Info::all();
2019-12-17 16:27:50 +03:00
$versions = [];
2017-09-14 22:21:00 +03:00
// Check core first
$url = 'core/version/' . $info['akaunting'] . '/' . $info['php'] . '/' . $info['mysql'] . '/' . $info['companies'];
2023-04-25 09:46:22 +03:00
# Installed modules start
2020-02-21 11:45:37 +03:00
$modules = Arr::wrap($modules);
2023-04-25 09:46:22 +03:00
$installed_modules = [];
$module_version = '?modules=';
foreach ($modules as $module) {
2023-04-25 11:44:36 +03:00
if (is_string($module)) {
$module = module($module);
}
if (! $module instanceof \Akaunting\Module\Module) {
continue;
}
2023-04-25 09:46:22 +03:00
$alias = $module->get('alias');
$installed_modules[] = $alias;
}
$module_version .= implode(',', $installed_modules);
$url .= $module_version;
# Installed modules end
$versions['core'] = static::getLatestVersion($url, $info['akaunting']);
2017-09-14 22:21:00 +03:00
// Then modules
foreach ($modules as $module) {
2020-02-21 11:45:37 +03:00
if (is_string($module)) {
$module = module($module);
}
2023-04-25 09:46:22 +03:00
if (! $module instanceof \Akaunting\Module\Module) {
2020-02-21 11:45:37 +03:00
continue;
}
2017-09-14 22:21:00 +03:00
$alias = $module->get('alias');
$version = $module->get('version');
$url = 'apps/' . $alias . '/version/' . $version . '/' . $info['akaunting'];
2017-09-14 22:21:00 +03:00
2019-12-17 16:27:50 +03:00
$versions[$alias] = static::getLatestVersion($url, $version);
2017-09-14 22:21:00 +03:00
}
2019-12-17 16:27:50 +03:00
Cache::put('versions', $versions, Date::now()->addHour(6));
2017-09-14 22:21:00 +03:00
2019-12-17 16:27:50 +03:00
return $versions;
2017-09-14 22:21:00 +03:00
}
public static function getVersionByAlias($alias)
{
$info = Info::all();
// Check core first
$url = 'core/version/' . $info['akaunting'] . '/' . $info['php'] . '/' . $info['mysql'] . '/' . $info['companies'];
$version = $info['akaunting'];
if ($alias != 'core') {
$version = module($alias)->get('version');
$url = 'apps/' . $alias . '/version/' . $version . '/' . $info['akaunting'];
}
// Get data from cache
$versions = Cache::get('versions', []);
$versions[$alias] = static::getLatestVersion($url, $version);
Cache::put('versions', $versions, Date::now()->addHour(6));
return $versions[$alias];
}
2020-12-24 02:16:00 +03:00
public static function getLatestVersion($url, $latest)
2017-09-14 22:21:00 +03:00
{
2023-04-25 09:46:22 +03:00
$version = new \stdClass();
$version->can_update = true;
$version->latest = $latest;
$version->errors = false;
$version->message = '';
if (! $body = static::getResponseBody('GET', $url, ['timeout' => 10])) {
return $version;
2017-10-27 09:38:55 +03:00
}
2017-09-14 22:21:00 +03:00
2023-04-25 09:46:22 +03:00
if (! is_object($body)) {
return $version;
2017-09-14 22:21:00 +03:00
}
2023-04-25 09:46:22 +03:00
$version->can_update = $body->success;
$version->latest = $body->data->latest;
$version->errors = $body->errors;
$version->message = $body->message;
return $version;
2017-09-14 22:21:00 +03:00
}
2020-12-24 02:16:00 +03:00
public static function getUpdates()
{
// Get data from cache
$updates = Cache::get('updates');
2023-04-25 09:46:22 +03:00
if (! empty($updates)) {
2020-12-24 02:16:00 +03:00
return $updates;
}
$updates = [];
$modules = module()->all();
$versions = static::all($modules);
foreach ($versions as $alias => $latest_version) {
if ($alias == 'core') {
$installed_version = version('short');
} else {
$module = module($alias);
if (!$module instanceof \Akaunting\Module\Module) {
continue;
}
$installed_version = $module->get('version');
}
2023-04-25 09:46:22 +03:00
if (version_compare($installed_version, $latest_version->latest, '>=')) {
2020-12-24 02:16:00 +03:00
continue;
}
$updates[$alias] = $latest_version;
}
Cache::put('updates', $updates, Date::now()->addHour(6));
return $updates;
}
2022-03-02 12:02:34 +03:00
public static function shouldUpdate($listener_version, $old_version, $new_version): bool
{
// Don't update if "listener" is same or lower than "old" version
if (version_compare($listener_version, $old_version, '<=')) {
return false;
}
// Don't update if "listener" is higher than "new" version
if (version_compare($listener_version, $new_version, '>')) {
return false;
}
return true;
}
}