2017-12-28 11:38:40 +01:00
< ? php
2022-12-29 19:41:57 +01:00
2017-12-28 11:38:40 +01:00
/**
* VersionCheckEventHandler . php
2020-01-28 08:45:38 +01:00
* Copyright ( c ) 2019 james @ firefly - iii . org
2017-12-28 11:38:40 +01:00
*
2019-10-02 06:37:26 +02:00
* This file is part of Firefly III ( https :// github . com / firefly - iii ) .
2017-12-28 11:38:40 +01:00
*
2019-10-02 06:37:26 +02: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 11:38:40 +01:00
*
2019-10-02 06:37:26 +02:00
* This program is distributed in the hope that it will be useful ,
2017-12-28 11:38:40 +01:00
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
2019-10-02 06:37:26 +02:00
* GNU Affero General Public License for more details .
2017-12-28 11:38:40 +01:00
*
2019-10-02 06:37:26 +02: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 11:38:40 +01:00
*/
declare ( strict_types = 1 );
namespace FireflyIII\Handlers\Events ;
2021-03-28 11:46:23 +02:00
2025-06-14 10:18:58 +02:00
use Deprecated ;
2025-05-27 17:06:15 +02:00
use Carbon\Carbon ;
2017-12-28 19:03:15 +01:00
use FireflyIII\Events\RequestedVersionCheckStatus ;
2021-09-18 10:20:19 +02:00
use FireflyIII\Exceptions\FireflyException ;
2018-07-08 07:59:58 +02:00
use FireflyIII\Helpers\Update\UpdateTrait ;
2018-08-17 21:51:15 +02:00
use FireflyIII\Models\Configuration ;
2018-03-30 22:40:20 +02:00
use FireflyIII\Repositories\User\UserRepositoryInterface ;
2025-02-15 06:12:02 +01:00
use Illuminate\Support\Facades\Log ;
2021-03-28 11:46:23 +02:00
2017-12-28 11:38:40 +01:00
/**
* Class VersionCheckEventHandler
*/
class VersionCheckEventHandler
{
2018-07-08 07:59:58 +02:00
use UpdateTrait ;
2017-12-28 19:03:15 +01:00
2017-12-28 11:38:40 +01:00
/**
2018-07-07 07:48:10 +02:00
* Checks with GitHub to see if there is a new version .
*
2021-09-18 10:20:19 +02:00
* @ throws FireflyException
2017-12-28 11:38:40 +01:00
*/
2025-06-14 10:18:58 +02:00
#[Deprecated(message: '?')]
2018-06-09 06:07:40 +02:00
public function checkForUpdates ( RequestedVersionCheckStatus $event ) : void
2017-12-28 11:38:40 +01:00
{
2025-02-15 06:12:02 +01:00
Log :: debug ( 'Now in checkForUpdates()' );
2017-12-28 11:38:40 +01:00
2019-12-27 08:07:07 +01:00
// should not check for updates:
2024-01-01 14:43:56 +01:00
$permission = app ( 'fireflyconfig' ) -> get ( 'permission_update_check' , - 1 );
2024-12-22 08:43:12 +01:00
$value = ( int ) $permission -> data ;
2019-12-27 08:07:07 +01:00
if ( 1 !== $value ) {
2025-02-15 06:12:02 +01:00
Log :: debug ( 'Update check is not enabled.' );
2020-05-20 06:40:18 +02:00
$this -> warnToCheckForUpdates ( $event );
2020-03-17 15:01:00 +01:00
2019-12-27 08:07:07 +01:00
return ;
}
2018-03-30 22:40:20 +02:00
/** @var UserRepositoryInterface $repository */
2024-01-01 14:43:56 +01:00
$repository = app ( UserRepositoryInterface :: class );
$user = $event -> user ;
2018-03-30 22:40:20 +02:00
if ( ! $repository -> hasRole ( $user , 'owner' )) {
2025-02-15 06:12:02 +01:00
Log :: debug ( 'User is not admin, done.' );
2020-03-17 15:01:00 +01:00
2017-12-28 11:57:38 +01:00
return ;
}
2018-08-17 21:51:15 +02:00
/** @var Configuration $lastCheckTime */
2025-05-27 17:06:15 +02:00
$lastCheckTime = app ( 'fireflyconfig' ) -> get ( 'last_update_check' , Carbon :: now () -> getTimestamp ());
$now = Carbon :: now () -> getTimestamp ();
2018-03-30 22:40:20 +02:00
$diff = $now - $lastCheckTime -> data ;
2025-02-15 06:12:02 +01:00
Log :: debug ( sprintf ( 'Last check time is %d, current time is %d, difference is %d' , $lastCheckTime -> data , $now , $diff ));
2018-03-30 22:40:20 +02:00
if ( $diff < 604800 ) {
2025-05-27 17:06:15 +02:00
Log :: debug ( sprintf ( 'Checked for updates less than a week ago (on %s).' , Carbon :: createFromTimestamp ( $lastCheckTime -> data ) -> format ( 'Y-m-d H:i:s' )));
2020-03-17 15:01:00 +01:00
2018-08-17 21:51:15 +02:00
return ;
2017-12-28 11:38:40 +01:00
}
// last check time was more than a week ago.
2025-02-15 06:12:02 +01:00
Log :: debug ( 'Have not checked for a new version in a week!' );
2024-01-01 14:43:56 +01:00
$release = $this -> getLatestRelease ();
2017-12-28 11:38:40 +01:00
2020-02-02 10:39:37 +01:00
session () -> flash ( $release [ 'level' ], $release [ 'message' ]);
2025-05-27 17:06:15 +02:00
app ( 'fireflyconfig' ) -> set ( 'last_update_check' , Carbon :: now () -> getTimestamp ());
2018-07-07 07:48:10 +02:00
}
2020-05-20 06:40:18 +02:00
/**
2021-09-18 10:20:19 +02:00
* @ throws FireflyException
2020-05-20 06:40:18 +02:00
*/
protected function warnToCheckForUpdates ( RequestedVersionCheckStatus $event ) : void
{
/** @var UserRepositoryInterface $repository */
2024-01-01 14:43:56 +01:00
$repository = app ( UserRepositoryInterface :: class );
$user = $event -> user ;
2020-05-20 06:40:18 +02:00
if ( ! $repository -> hasRole ( $user , 'owner' )) {
2025-02-15 06:12:02 +01:00
Log :: debug ( 'User is not admin, done.' );
2020-05-20 06:40:18 +02:00
return ;
}
/** @var Configuration $lastCheckTime */
2025-05-27 17:06:15 +02:00
$lastCheckTime = app ( 'fireflyconfig' ) -> get ( 'last_update_warning' , Carbon :: now () -> getTimestamp ());
$now = Carbon :: now () -> getTimestamp ();
2020-05-20 06:40:18 +02:00
$diff = $now - $lastCheckTime -> data ;
2025-02-15 06:12:02 +01:00
Log :: debug ( sprintf ( 'Last warning time is %d, current time is %d, difference is %d' , $lastCheckTime -> data , $now , $diff ));
2020-05-20 06:40:18 +02:00
if ( $diff < 604800 * 4 ) {
2025-05-27 17:06:15 +02:00
Log :: debug ( sprintf ( 'Warned about updates less than four weeks ago (on %s).' , Carbon :: createFromTimestamp ( $lastCheckTime -> data ) -> format ( 'Y-m-d H:i:s' )));
2020-05-20 06:40:18 +02:00
return ;
}
// last check time was more than a week ago.
2025-02-15 06:12:02 +01:00
Log :: debug ( 'Have warned about a new version in four weeks!' );
2020-05-20 06:40:18 +02:00
2024-12-22 08:43:12 +01:00
session () -> flash ( 'info' , ( string ) trans ( 'firefly.disabled_but_check' ));
2025-05-27 17:06:15 +02:00
app ( 'fireflyconfig' ) -> set ( 'last_update_warning' , Carbon :: now () -> getTimestamp ());
2020-05-20 06:40:18 +02:00
}
2018-03-05 19:35:58 +01:00
}