firefly-iii/app/Handlers/Events/VersionCheckEventHandler.php

122 lines
4.3 KiB
PHP
Raw Normal View History

2017-12-28 04:38:40 -06:00
<?php
/**
* VersionCheckEventHandler.php
2020-01-28 01:45:38 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2017-12-28 04:38:40 -06:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-12-28 04:38:40 -06:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-12-28 04:38:40 -06:00
*
* This program is distributed in the hope that it will be useful,
2017-12-28 04:38:40 -06:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-12-28 04:38:40 -06:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-12-28 04:38:40 -06:00
*/
2018-07-07 00:48:10 -05:00
/** @noinspection MultipleReturnStatementsInspection */
/** @noinspection NullPointerExceptionInspection */
2017-12-28 04:38:40 -06:00
declare(strict_types=1);
namespace FireflyIII\Handlers\Events;
2021-03-28 04:46:23 -05:00
2017-12-28 12:03:15 -06:00
use FireflyIII\Events\RequestedVersionCheckStatus;
2021-09-18 03:20:19 -05:00
use FireflyIII\Exceptions\FireflyException;
2018-07-08 00:59:58 -05:00
use FireflyIII\Helpers\Update\UpdateTrait;
use FireflyIII\Models\Configuration;
2018-03-30 15:40:20 -05:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2017-12-28 04:38:40 -06:00
use Log;
2021-03-28 04:46:23 -05:00
2017-12-28 04:38:40 -06:00
/**
* Class VersionCheckEventHandler
*/
class VersionCheckEventHandler
{
2018-07-08 00:59:58 -05:00
use UpdateTrait;
2017-12-28 12:03:15 -06:00
2017-12-28 04:38:40 -06:00
/**
2018-07-07 00:48:10 -05:00
* Checks with GitHub to see if there is a new version.
*
2017-12-28 12:03:15 -06:00
* @param RequestedVersionCheckStatus $event
2021-05-24 01:50:17 -05:00
*
2021-09-18 03:20:19 -05:00
* @throws FireflyException
2017-12-28 04:38:40 -06:00
*/
public function checkForUpdates(RequestedVersionCheckStatus $event): void
2017-12-28 04:38:40 -06:00
{
Log::debug('Now in checkForUpdates()');
2017-12-28 04:38:40 -06:00
// should not check for updates:
$permission = app('fireflyconfig')->get('permission_update_check', -1);
2021-03-21 03:15:40 -05:00
$value = (int)$permission->data;
if (1 !== $value) {
Log::info('Update check is not enabled.');
$this->warnToCheckForUpdates($event);
return;
}
2018-03-30 15:40:20 -05:00
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$user = $event->user;
2018-03-30 15:40:20 -05:00
if (!$repository->hasRole($user, 'owner')) {
Log::debug('User is not admin, done.');
return;
}
/** @var Configuration $lastCheckTime */
2019-06-07 23:19:21 -05:00
$lastCheckTime = app('fireflyconfig')->get('last_update_check', time());
2017-12-28 04:38:40 -06:00
$now = time();
2018-03-30 15:40:20 -05:00
$diff = $now - $lastCheckTime->data;
Log::debug(sprintf('Last check time is %d, current time is %d, difference is %d', $lastCheckTime->data, $now, $diff));
2018-03-30 15:40:20 -05:00
if ($diff < 604800) {
2018-01-21 04:09:55 -06:00
Log::debug(sprintf('Checked for updates less than a week ago (on %s).', date('Y-m-d H:i:s', $lastCheckTime->data)));
return;
2017-12-28 04:38:40 -06:00
}
// last check time was more than a week ago.
Log::debug('Have not checked for a new version in a week!');
$release = $this->getLatestRelease();
2017-12-28 04:38:40 -06:00
2020-02-02 03:39:37 -06:00
session()->flash($release['level'], $release['message']);
2019-06-07 23:19:21 -05:00
app('fireflyconfig')->set('last_update_check', time());
2018-07-07 00:48:10 -05:00
}
/**
* @param RequestedVersionCheckStatus $event
*
2021-09-18 03:20:19 -05:00
* @throws FireflyException
*/
protected function warnToCheckForUpdates(RequestedVersionCheckStatus $event): void
{
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$user = $event->user;
if (!$repository->hasRole($user, 'owner')) {
Log::debug('User is not admin, done.');
return;
}
/** @var Configuration $lastCheckTime */
$lastCheckTime = app('fireflyconfig')->get('last_update_warning', time());
$now = time();
$diff = $now - $lastCheckTime->data;
Log::debug(sprintf('Last warning time is %d, current time is %d, difference is %d', $lastCheckTime->data, $now, $diff));
if ($diff < 604800 * 4) {
Log::debug(sprintf('Warned about updates less than four weeks ago (on %s).', date('Y-m-d H:i:s', $lastCheckTime->data)));
return;
}
// last check time was more than a week ago.
Log::debug('Have warned about a new version in four weeks!');
2021-03-21 03:15:40 -05:00
session()->flash('info', (string)trans('firefly.disabled_but_check'));
app('fireflyconfig')->set('last_update_warning', time());
}
2018-03-05 12:35:58 -06:00
}