1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* InstallController.php |
4
|
|
|
* Copyright (c) 2018 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III. |
7
|
|
|
* |
8
|
|
|
* Firefly III is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* Firefly III 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 General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
declare(strict_types=1); |
23
|
|
|
|
24
|
|
|
namespace FireflyIII\Http\Controllers\System; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
use Artisan; |
28
|
|
|
use Cache; |
29
|
|
|
use Exception; |
30
|
|
|
use FireflyIII\Http\Controllers\Controller; |
31
|
|
|
use FireflyIII\Support\Facades\Preferences; |
32
|
|
|
use FireflyIII\Support\Http\Controllers\GetConfigurationData; |
33
|
|
|
use Illuminate\Http\JsonResponse; |
34
|
|
|
use Illuminate\Support\Arr; |
35
|
|
|
use Laravel\Passport\Passport; |
36
|
|
|
use Log; |
37
|
|
|
use phpseclib\Crypt\RSA; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Class InstallController |
41
|
|
|
* |
42
|
|
|
* @codeCoverageIgnore |
43
|
|
|
*/ |
44
|
|
|
class InstallController extends Controller |
45
|
|
|
{ |
46
|
|
|
use GetConfigurationData; |
|
|
|
|
47
|
|
|
/** @var string Forbidden error */ |
48
|
|
|
public const FORBIDDEN_ERROR = 'Internal PHP function "proc_close" is disabled for your installation. Auto-migration is not possible.'; |
49
|
|
|
/** @var string Basedir error */ |
50
|
|
|
public const BASEDIR_ERROR = 'Firefly III cannot execute the upgrade commands. It is not allowed to because of an open_basedir restriction.'; |
51
|
|
|
/** @var string Other errors */ |
52
|
|
|
public const OTHER_ERROR = 'An unknown error prevented Firefly III from executing the upgrade commands. Sorry.'; |
53
|
|
|
/** @noinspection MagicMethodsValidityInspection */ |
54
|
|
|
/** @noinspection PhpMissingParentConstructorInspection */ |
55
|
|
|
/** |
56
|
|
|
* InstallController constructor. |
57
|
|
|
*/ |
58
|
|
|
public function __construct() |
59
|
|
|
{ |
60
|
|
|
// empty on purpose. |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Do database decrypt. |
65
|
|
|
* |
66
|
|
|
* @return \Illuminate\Http\JsonResponse |
67
|
|
|
*/ |
68
|
|
|
public function decrypt(): JsonResponse |
69
|
|
|
{ |
70
|
|
|
if ($this->hasForbiddenFunctions()) { |
71
|
|
|
return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]); |
72
|
|
|
} |
73
|
|
|
try { |
74
|
|
|
Log::debug('Am now calling decrypt database routine...'); |
75
|
|
|
Artisan::call('firefly:decrypt-all'); |
76
|
|
|
Log::debug(Artisan::output()); |
77
|
|
|
} catch (Exception $e) { |
78
|
|
|
Log::error($e->getMessage()); |
79
|
|
|
Log::error($e->getTraceAsString()); |
80
|
|
|
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) { |
81
|
|
|
Cache::clear(); |
82
|
|
|
|
83
|
|
|
return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return response()->json(['error' => true, 'message' => self::OTHER_ERROR . ' ' . $e->getMessage()]); |
87
|
|
|
} |
88
|
|
|
// clear cache as well. |
89
|
|
|
Cache::clear(); |
90
|
|
|
Preferences::mark(); |
|
|
|
|
91
|
|
|
|
92
|
|
|
|
93
|
|
|
return response()->json(['error' => false, 'message' => 'OK']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Show index. |
98
|
|
|
* |
99
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
100
|
|
|
*/ |
101
|
|
|
public function index() |
102
|
|
|
{ |
103
|
|
|
// index will set FF3 version. |
104
|
|
|
app('fireflyconfig')->set('ff3_version', (string)config('firefly.version')); |
105
|
|
|
|
106
|
|
|
return view('install.index'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Create specific RSA keys. |
111
|
|
|
* |
112
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
113
|
|
|
*/ |
114
|
|
|
public function keys() |
115
|
|
|
{ |
116
|
|
|
if ($this->hasForbiddenFunctions()) { |
117
|
|
|
return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]); |
118
|
|
|
} |
119
|
|
|
// create keys manually because for some reason the passport namespace |
120
|
|
|
// does not exist |
121
|
|
|
$rsa = new RSA(); |
122
|
|
|
$keys = $rsa->createKey(4096); |
123
|
|
|
|
124
|
|
|
[$publicKey, $privateKey] = [ |
125
|
|
|
Passport::keyPath('oauth-public.key'), |
126
|
|
|
Passport::keyPath('oauth-private.key'), |
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
if (file_exists($publicKey) || file_exists($privateKey)) { |
130
|
|
|
return response()->json(['error' => false, 'message' => 'OK']); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
file_put_contents($publicKey, Arr::get($keys, 'publickey')); |
134
|
|
|
file_put_contents($privateKey, Arr::get($keys, 'privatekey')); |
135
|
|
|
|
136
|
|
|
// clear cache as well. |
137
|
|
|
Cache::clear(); |
138
|
|
|
Preferences::mark(); |
|
|
|
|
139
|
|
|
|
140
|
|
|
return response()->json(['error' => false, 'message' => 'OK']); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Run migration commands. |
145
|
|
|
* |
146
|
|
|
* @return JsonResponse |
147
|
|
|
*/ |
148
|
|
|
public function migrate(): JsonResponse |
149
|
|
|
{ |
150
|
|
|
if ($this->hasForbiddenFunctions()) { |
151
|
|
|
return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
try { |
155
|
|
|
Log::debug('Am now calling migrate routine...'); |
156
|
|
|
Artisan::call('migrate', ['--seed' => true, '--force' => true]); |
157
|
|
|
Log::debug(Artisan::output()); |
158
|
|
|
} catch (Exception $e) { |
159
|
|
|
Log::error($e->getMessage()); |
160
|
|
|
Log::error($e->getTraceAsString()); |
161
|
|
|
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) { |
162
|
|
|
return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return response()->json(['error' => true, 'message' => self::OTHER_ERROR]); |
166
|
|
|
} |
167
|
|
|
// clear cache as well. |
168
|
|
|
Cache::clear(); |
169
|
|
|
Preferences::mark(); |
|
|
|
|
170
|
|
|
|
171
|
|
|
|
172
|
|
|
return response()->json(['error' => false, 'message' => 'OK']); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Do database upgrade. |
177
|
|
|
* |
178
|
|
|
* @return \Illuminate\Http\JsonResponse |
179
|
|
|
*/ |
180
|
|
|
public function upgrade(): JsonResponse |
181
|
|
|
{ |
182
|
|
|
if ($this->hasForbiddenFunctions()) { |
183
|
|
|
return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]); |
184
|
|
|
} |
185
|
|
|
try { |
186
|
|
|
Log::debug('Am now calling upgrade database routine...'); |
187
|
|
|
Artisan::call('firefly:upgrade-database'); |
188
|
|
|
Log::debug(Artisan::output()); |
189
|
|
|
} catch (Exception $e) { |
190
|
|
|
Log::error($e->getMessage()); |
191
|
|
|
Log::error($e->getTraceAsString()); |
192
|
|
|
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) { |
193
|
|
|
return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return response()->json(['error' => true, 'message' => self::OTHER_ERROR . ' ' . $e->getMessage()]); |
197
|
|
|
} |
198
|
|
|
// clear cache as well. |
199
|
|
|
Cache::clear(); |
200
|
|
|
Preferences::mark(); |
|
|
|
|
201
|
|
|
|
202
|
|
|
|
203
|
|
|
return response()->json(['error' => false, 'message' => 'OK']); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Do database verification. |
208
|
|
|
* |
209
|
|
|
* @return \Illuminate\Http\JsonResponse |
210
|
|
|
*/ |
211
|
|
|
public function verify(): JsonResponse |
212
|
|
|
{ |
213
|
|
|
if ($this->hasForbiddenFunctions()) { |
214
|
|
|
return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]); |
215
|
|
|
} |
216
|
|
|
try { |
217
|
|
|
Log::debug('Am now calling verify database routine...'); |
218
|
|
|
Artisan::call('firefly:verify'); |
219
|
|
|
Log::debug(Artisan::output()); |
220
|
|
|
} catch (Exception $e) { |
221
|
|
|
Log::error($e->getMessage()); |
222
|
|
|
Log::error($e->getTraceAsString()); |
223
|
|
|
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) { |
224
|
|
|
return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return response()->json(['error' => true, 'message' => self::OTHER_ERROR . ' ' . $e->getMessage()]); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
// clear cache as well. |
232
|
|
|
Cache::clear(); |
233
|
|
|
Preferences::mark(); |
|
|
|
|
234
|
|
|
|
235
|
|
|
return response()->json(['error' => false, 'message' => 'OK']); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
} |
240
|
|
|
|