| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2023-04-01 07:04:42 +02:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  | use Psr\Container\ContainerExceptionInterface; | 
					
						
							|  |  |  | use Psr\Container\NotFoundExceptionInterface; | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param RequestedVersionCheckStatus $event | 
					
						
							| 
									
										
										
										
											2021-05-24 08:50:17 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |      * @throws ContainerExceptionInterface | 
					
						
							|  |  |  |      * @throws NotFoundExceptionInterface | 
					
						
							| 
									
										
										
										
											2022-09-18 12:36:35 +02:00
										 |  |  |      * @deprecated ? | 
					
						
							| 
									
										
										
										
											2017-12-28 11:38:40 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-06-09 06:07:40 +02:00
										 |  |  |     public function checkForUpdates(RequestedVersionCheckStatus $event): void | 
					
						
							| 
									
										
										
										
											2017-12-28 11:38:40 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-08-17 21:51:15 +02: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:
 | 
					
						
							|  |  |  |         $permission = app('fireflyconfig')->get('permission_update_check', -1); | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |         $value      = (int)$permission->data; | 
					
						
							| 
									
										
										
										
											2019-12-27 08:07:07 +01:00
										 |  |  |         if (1 !== $value) { | 
					
						
							| 
									
										
										
										
											2023-08-30 15:38:15 +02: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 */ | 
					
						
							|  |  |  |         $repository = app(UserRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |         $user       = $event->user; | 
					
						
							| 
									
										
										
										
											2018-03-30 22:40:20 +02:00
										 |  |  |         if (!$repository->hasRole($user, 'owner')) { | 
					
						
							| 
									
										
										
										
											2018-08-17 21:51:15 +02: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 */ | 
					
						
							| 
									
										
										
										
											2019-06-08 06:19:21 +02:00
										 |  |  |         $lastCheckTime = app('fireflyconfig')->get('last_update_check', time()); | 
					
						
							| 
									
										
										
										
											2017-12-28 11:38:40 +01:00
										 |  |  |         $now           = time(); | 
					
						
							| 
									
										
										
										
											2018-03-30 22:40:20 +02:00
										 |  |  |         $diff          = $now - $lastCheckTime->data; | 
					
						
							| 
									
										
										
										
											2018-08-17 21:51:15 +02: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) { | 
					
						
							| 
									
										
										
										
											2018-01-21 11:09:55 +01:00
										 |  |  |             Log::debug(sprintf('Checked for updates less than a week ago (on %s).', date('Y-m-d H:i:s', $lastCheckTime->data))); | 
					
						
							| 
									
										
										
										
											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.
 | 
					
						
							|  |  |  |         Log::debug('Have not checked for a new version in a week!'); | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +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']); | 
					
						
							| 
									
										
										
										
											2019-06-08 06:19:21 +02:00
										 |  |  |         app('fireflyconfig')->set('last_update_check', time()); | 
					
						
							| 
									
										
										
										
											2018-07-07 07:48:10 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-20 06:40:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param RequestedVersionCheckStatus $event | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |      * @throws ContainerExceptionInterface | 
					
						
							|  |  |  |      * @throws NotFoundExceptionInterface | 
					
						
							| 
									
										
										
										
											2020-05-20 06:40:18 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function warnToCheckForUpdates(RequestedVersionCheckStatus $event): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var UserRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(UserRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |         $user       = $event->user; | 
					
						
							| 
									
										
										
										
											2020-05-20 06:40:18 +02:00
										 |  |  |         if (!$repository->hasRole($user, 'owner')) { | 
					
						
							| 
									
										
										
										
											2023-08-30 18:03:47 +02:00
										 |  |  |             Log::debug('User is not admin, done.'); | 
					
						
							| 
									
										
										
										
											2020-05-20 06:40:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             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!'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |         session()->flash('info', (string)trans('firefly.disabled_but_check')); | 
					
						
							| 
									
										
										
										
											2020-05-20 06:40:18 +02:00
										 |  |  |         app('fireflyconfig')->set('last_update_warning', time()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |