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

123 lines
4.3 KiB
PHP
Raw Normal View History

2017-12-28 04:38:40 -06:00
<?php
/**
* VersionCheckEventHandler.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Handlers\Events;
use FireflyConfig;
2017-12-28 12:03:15 -06:00
use FireflyIII\Events\RequestedVersionCheckStatus;
2018-01-14 12:56:18 -06:00
use FireflyIII\Exceptions\FireflyException;
2018-03-30 15:40:20 -05:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2018-01-14 12:56:18 -06:00
use FireflyIII\Services\Github\Object\Release;
use FireflyIII\Services\Github\Request\UpdateRequest;
2017-12-28 04:38:40 -06:00
use FireflyIII\User;
use Log;
/**
* Class VersionCheckEventHandler
*/
class VersionCheckEventHandler
{
2017-12-28 12:03:15 -06:00
2017-12-28 04:38:40 -06:00
/**
2017-12-28 12:03:15 -06:00
* @param RequestedVersionCheckStatus $event
2017-12-28 04:38:40 -06:00
*/
2017-12-28 12:03:15 -06:00
public function checkForUpdates(RequestedVersionCheckStatus $event)
2017-12-28 04:38:40 -06:00
{
// in Sandstorm, cannot check for updates:
2018-03-30 15:40:20 -05:00
$sandstorm = 1 === (int)getenv('SANDSTORM');
2017-12-28 04:38:40 -06:00
if ($sandstorm === true) {
2018-03-30 15:40:20 -05:00
return; // @codeCoverageIgnore
2017-12-28 04:38:40 -06:00
}
2018-03-30 15:40:20 -05:00
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
2018-01-14 12:56:18 -06:00
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')) {
return;
}
$permission = FireflyConfig::get('permission_update_check', -1);
$lastCheckTime = 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('Difference is %d seconds.', $diff));
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;
}
// last check time was more than a week ago.
Log::debug('Have not checked for a new version in a week!');
// have actual permission?
if ($permission->data === -1) {
// never asked before.
2018-03-30 15:40:20 -05:00
session()->flash('info', (string)trans('firefly.check_for_updates_permission', ['link' => route('admin.update-check')]));
2017-12-28 04:38:40 -06:00
return;
}
2018-01-14 12:56:18 -06:00
$current = config('firefly.version');
/** @var UpdateRequest $request */
$request = app(UpdateRequest::class);
$check = -2;
2018-03-30 15:40:20 -05:00
$first = new Release(['id' => '0', 'title' => '0.2', 'updated' => '2017-01-01', 'content' => '']);
2018-01-14 12:56:18 -06:00
try {
$request->call();
$releases = $request->getReleases();
// first entry should be the latest entry:
/** @var Release $first */
$first = reset($releases);
$check = version_compare($current, $first->getTitle());
2018-03-30 15:40:20 -05:00
Log::debug(sprintf('Comparing %s with %s, result is %s', $current, $first->getTitle(), $check));
2018-01-14 12:56:18 -06:00
FireflyConfig::set('last_update_check', time());
} catch (FireflyException $e) {
Log::error(sprintf('Could not check for updates: %s', $e->getMessage()));
}
2018-01-21 04:09:55 -06:00
$string = 'no result: ' . $check;
2018-01-14 12:56:18 -06:00
if ($check === -2) {
$string = strval(trans('firefly.update_check_error'));
}
if ($check === -1) {
// there is a new FF version!
$monthAndDayFormat = (string)trans('config.month_and_day');
2018-03-30 15:40:20 -05:00
$string = (string)trans(
'firefly.update_new_version_alert',
[
'your_version' => $current,
'new_version' => $first->getTitle(),
'date' => $first->getUpdated()->formatLocalized($monthAndDayFormat),
]
2018-01-14 12:56:18 -06:00
);
2018-01-21 04:09:55 -06:00
}
if ($check !== 0) {
// flash info
2018-01-14 12:56:18 -06:00
session()->flash('info', $string);
}
2017-12-28 04:38:40 -06:00
}
2018-03-05 12:35:58 -06:00
}