1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* UpdateTrait.php |
4
|
|
|
* Copyright (c) 2019 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii). |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License as |
10
|
|
|
* published by the Free Software Foundation, either version 3 of the |
11
|
|
|
* License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
declare(strict_types=1); |
23
|
|
|
|
24
|
|
|
namespace FireflyIII\Helpers\Update; |
25
|
|
|
|
26
|
|
|
use Carbon\Carbon; |
27
|
|
|
use FireflyIII\Exceptions\FireflyException; |
28
|
|
|
use FireflyIII\Services\FireflyIIIOrg\Update\UpdateRequestInterface; |
29
|
|
|
use Log; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Trait UpdateTrait |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
trait UpdateTrait |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Get object for the latest release from GitHub. |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
* @throws FireflyException |
42
|
|
|
*/ |
43
|
|
|
public function getLatestRelease(): array |
44
|
|
|
{ |
45
|
|
|
Log::debug('Now in getLatestRelease()'); |
46
|
|
|
/** @var UpdateRequestInterface $checker */ |
47
|
|
|
$checker = app(UpdateRequestInterface::class); |
48
|
|
|
$channel = app('fireflyconfig')->get('update_channel', 'stable')->data; |
49
|
|
|
|
50
|
|
|
return $checker->getVersion($channel); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Parses the version check result in a human readable sentence. |
55
|
|
|
* |
56
|
|
|
* @param int $versionCheck |
57
|
|
|
* @param array $information |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function parseResult(int $versionCheck, array $information): string |
62
|
|
|
{ |
63
|
|
|
Log::debug(sprintf('Now in parseResult(%d)', $versionCheck)); |
64
|
|
|
$current = (string)config('firefly.version'); |
65
|
|
|
$return = ''; |
66
|
|
|
$triggered = false; |
67
|
|
|
if (-1 === $versionCheck) { |
68
|
|
|
$triggered = true; |
69
|
|
|
$monthAndDayFormat = (string)trans('config.month_and_day'); |
70
|
|
|
$carbon = Carbon::createFromFormat('Y-m-d', $information['date']); |
71
|
|
|
$return = (string)trans( |
72
|
|
|
'firefly.update_new_version_alert', |
73
|
|
|
[ |
74
|
|
|
'your_version' => $current, |
75
|
|
|
'new_version' => $information['version'], |
76
|
|
|
'date' => $carbon->formatLocalized($monthAndDayFormat), |
77
|
|
|
] |
78
|
|
|
); |
79
|
|
|
// append warning if beta or alpha. |
80
|
|
|
$isBeta = $information['is_beta'] ?? false; |
81
|
|
|
if (true === $isBeta) { |
82
|
|
|
$return = sprintf('%s %s', $return, trans('firefly.update_version_beta')); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$isAlpha = $information['is_alpha'] ?? false; |
86
|
|
|
if (true === $isAlpha) { |
87
|
|
|
$return = sprintf('%s %s', $return, trans('firefly.update_version_alpha')); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (0 === $versionCheck) { |
92
|
|
|
$triggered = true; |
93
|
|
|
Log::debug('User is running current version.'); |
94
|
|
|
// you are running the current version! |
95
|
|
|
$return = (string)trans('firefly.update_current_version_alert', ['version' => $current]); |
96
|
|
|
} |
97
|
|
|
if (1 === $versionCheck) { |
98
|
|
|
$triggered = true; |
99
|
|
|
Log::debug('User is running NEWER version.'); |
100
|
|
|
// you are running a newer version! |
101
|
|
|
$return = (string)trans('firefly.update_newer_version_alert', ['your_version' => $current, 'new_version' => $information['version']]); |
102
|
|
|
} |
103
|
|
|
if (false === $triggered) { |
104
|
|
|
Log::debug('No option was triggered.'); |
105
|
|
|
$return = (string)trans('firefly.update_check_error'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Compare version and store result. |
113
|
|
|
* |
114
|
|
|
* @param array $information |
115
|
|
|
* |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
|
|
public function versionCheck(array $information): int |
119
|
|
|
{ |
120
|
|
|
Log::debug('Now in versionCheck()'); |
121
|
|
|
$current = (string)config('firefly.version'); |
122
|
|
|
$check = version_compare($current, $information['version']); |
123
|
|
|
Log::debug(sprintf('Comparing %s with %s, result is %s', $current, $information['version'], $check), $information); |
124
|
|
|
|
125
|
|
|
return $check; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|