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

102 lines
3.4 KiB
PHP
Raw Normal View History

2017-12-28 04:38:40 -06:00
<?php
/**
* VersionCheckEventHandler.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
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;
2018-07-25 23:10:17 -05:00
2017-12-28 12:03:15 -06:00
use FireflyIII\Events\RequestedVersionCheckStatus;
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 FireflyIII\User;
use Log;
2018-08-06 12:14:30 -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
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);
$value = (int)$permission->data;
if (1 !== $value) {
Log::info('Update check is not enabled.');
return;
}
2018-03-30 15:40:20 -05:00
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
2017-12-28 04:38:40 -06:00
/** @var User $user */
$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)));
2017-12-28 04:38:40 -06:00
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!');
try {
$latestRelease = $this->getLatestRelease();
} catch (FireflyException $e) {
Log::error($e);
session()->flash('error', (string)trans('firefly.update_check_error'));
2017-12-28 04:38:40 -06:00
// softfail.
return;
}
2018-07-07 00:48:10 -05:00
$versionCheck = $this->versionCheck($latestRelease);
2018-08-06 12:14:30 -05:00
$resultString = $this->parseResult($versionCheck, $latestRelease);
2018-07-08 00:59:58 -05:00
if (0 !== $versionCheck && '' !== $resultString) {
2018-07-07 00:48:10 -05:00
// flash info
2018-07-08 00:59:58 -05:00
session()->flash('info', $resultString);
2018-07-07 00:48:10 -05:00
}
2019-06-07 23:19:21 -05:00
app('fireflyconfig')->set('last_update_check', time());
2018-07-07 00:48:10 -05:00
}
2018-03-05 12:35:58 -06:00
}