1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* TokenController.php |
5
|
|
|
* Copyright (c) 2020 [email protected] |
6
|
|
|
* |
7
|
|
|
* This file is part of the Firefly III CSV importer |
8
|
|
|
* (https://github.com/firefly-iii/csv-importer). |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
21
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace App\Http\Controllers; |
25
|
|
|
|
26
|
|
|
use GrumpyDictator\FFIIIApiSupport\Exceptions\ApiHttpException; |
27
|
|
|
use GrumpyDictator\FFIIIApiSupport\Request\SystemInformationRequest; |
28
|
|
|
use GrumpyDictator\FFIIIApiSupport\Response\SystemInformationResponse; |
29
|
|
|
use Illuminate\Contracts\View\Factory; |
30
|
|
|
use Illuminate\Http\JsonResponse; |
31
|
|
|
use Illuminate\Http\RedirectResponse; |
32
|
|
|
use Illuminate\Routing\Redirector; |
33
|
|
|
use Illuminate\View\View; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Class TokenController |
37
|
|
|
*/ |
38
|
|
|
class TokenController extends Controller |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* Check if the Firefly III API responds properly. |
42
|
|
|
* |
43
|
|
|
* @return JsonResponse |
44
|
|
|
*/ |
45
|
|
|
public function doValidate(): JsonResponse |
46
|
|
|
{ |
47
|
|
|
$response = ['result' => 'OK', 'message' => null]; |
48
|
|
|
$uri = (string)config('csv_importer.uri'); |
49
|
|
|
$token = (string)config('csv_importer.access_token'); |
50
|
|
|
$request = new SystemInformationRequest($uri, $token); |
51
|
|
|
try { |
52
|
|
|
$result = $request->get(); |
53
|
|
|
} catch (ApiHttpException $e) { |
54
|
|
|
return ['result' => 'NOK', 'message' => $e->getMessage()]; |
55
|
|
|
} |
56
|
|
|
// -1 = OK (minimum is smaller) |
57
|
|
|
// 0 = OK (same version) |
58
|
|
|
// 1 = NOK (too low a version) |
59
|
|
|
|
60
|
|
|
$minimum = (string) config('csv_importer.minimum_version'); |
61
|
|
|
$compare = version_compare($minimum, $result->version); |
62
|
|
|
if (1 === $compare) { |
63
|
|
|
$errorMessage = sprintf( |
64
|
|
|
'Your Firefly III version %s is below the minimum required version %s', |
65
|
|
|
$result->version, $minimum |
66
|
|
|
); |
67
|
|
|
$response = ['result' => 'NOK', 'message' => $errorMessage]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return response()->json($response); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Same thing but not over JSON. |
75
|
|
|
* |
76
|
|
|
* @return Factory|RedirectResponse|Redirector|View |
77
|
|
|
*/ |
78
|
|
|
public function index() |
79
|
|
|
{ |
80
|
|
|
$uri = (string)config('csv_importer.uri'); |
81
|
|
|
$token = (string)config('csv_importer.access_token'); |
82
|
|
|
$request = new SystemInformationRequest($uri, $token); |
83
|
|
|
$errorMessage = 'No error message.'; |
84
|
|
|
$isError = false; |
85
|
|
|
$result = null; |
|
|
|
|
86
|
|
|
$compare = 1; |
87
|
|
|
$minimum = ''; |
88
|
|
|
try { |
89
|
|
|
/** @var SystemInformationResponse $result */ |
90
|
|
|
$result = $request->get(); |
91
|
|
|
} catch (ApiHttpException $e) { |
92
|
|
|
$errorMessage = $e->getMessage(); |
93
|
|
|
$isError = true; |
94
|
|
|
} |
95
|
|
|
// -1 = OK (minimum is smaller) |
96
|
|
|
// 0 = OK (same version) |
97
|
|
|
// 1 = NOK (too low a version) |
98
|
|
|
if (false === $isError) { |
99
|
|
|
$minimum = config('csv_importer.minimum_version'); |
100
|
|
|
$compare = version_compare($minimum, $result->version); |
101
|
|
|
} |
102
|
|
|
if (false === $isError && 1 === $compare) { |
103
|
|
|
$errorMessage = sprintf('Your Firefly III version %s is below the minimum required version %s', $result->version, $minimum); |
104
|
|
|
$isError = true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (false === $isError) { |
108
|
|
|
return redirect(route('index')); |
109
|
|
|
} |
110
|
|
|
$pageTitle = 'Token error'; |
111
|
|
|
|
112
|
|
|
return view('token.index', compact('errorMessage', 'pageTitle')); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|