1 | <?php |
||
2 | /** |
||
3 | * InstallController.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\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\Http\RedirectResponse; |
||
35 | use Illuminate\Http\Request; |
||
36 | use Illuminate\Routing\Redirector; |
||
37 | use Illuminate\Support\Arr; |
||
38 | use Laravel\Passport\Passport; |
||
39 | use Log; |
||
40 | use phpseclib\Crypt\RSA; |
||
41 | |||
42 | /** |
||
43 | * Class InstallController |
||
44 | * |
||
45 | * @codeCoverageIgnore |
||
46 | */ |
||
47 | class InstallController extends Controller |
||
48 | { |
||
49 | use GetConfigurationData; |
||
50 | /** @var string Forbidden error */ |
||
51 | public const FORBIDDEN_ERROR = 'Internal PHP function "proc_close" is disabled for your installation. Auto-migration is not possible.'; |
||
52 | /** @var string Basedir error */ |
||
53 | public const BASEDIR_ERROR = 'Firefly III cannot execute the upgrade commands. It is not allowed to because of an open_basedir restriction.'; |
||
54 | /** @var string Other errors */ |
||
55 | public const OTHER_ERROR = 'An unknown error prevented Firefly III from executing the upgrade commands. Sorry.'; |
||
56 | |||
57 | /** @var array All upgrade commands. */ |
||
58 | private $upgradeCommands; |
||
59 | |||
60 | |||
61 | /** @noinspection MagicMethodsValidityInspection */ |
||
62 | /** @noinspection PhpMissingParentConstructorInspection */ |
||
63 | /** |
||
64 | * InstallController constructor. |
||
65 | */ |
||
66 | public function __construct() |
||
67 | { |
||
68 | // empty on purpose. |
||
69 | $this->upgradeCommands = [ |
||
70 | // there are 3 initial commands |
||
71 | 'migrate' => ['--seed' => true, '--force' => true], |
||
72 | 'firefly-iii:decrypt-all' => [], |
||
73 | 'firefly-iii:restore-oauth-keys' => [], |
||
74 | 'generate-keys' => [], // an exception :( |
||
75 | |||
76 | // there are 14 upgrade commands. |
||
77 | 'firefly-iii:transaction-identifiers' => [], |
||
78 | 'firefly-iii:migrate-to-groups' => [], |
||
79 | 'firefly-iii:account-currencies' => [], |
||
80 | 'firefly-iii:transfer-currencies' => [], |
||
81 | 'firefly-iii:other-currencies' => [], |
||
82 | 'firefly-iii:migrate-notes' => [], |
||
83 | 'firefly-iii:migrate-attachments' => [], |
||
84 | 'firefly-iii:bills-to-rules' => [], |
||
85 | 'firefly-iii:bl-currency' => [], |
||
86 | 'firefly-iii:cc-liabilities' => [], |
||
87 | 'firefly-iii:back-to-journals' => [], |
||
88 | 'firefly-iii:rename-account-meta' => [], |
||
89 | 'firefly-iii:migrate-recurrence-meta' => [], |
||
90 | 'firefly-iii:migrate-tag-locations' => [], |
||
91 | |||
92 | // there are 16 verify commands. |
||
93 | 'firefly-iii:fix-piggies' => [], |
||
94 | 'firefly-iii:create-link-types' => [], |
||
95 | 'firefly-iii:create-access-tokens' => [], |
||
96 | 'firefly-iii:remove-bills' => [], |
||
97 | 'firefly-iii:enable-currencies' => [], |
||
98 | 'firefly-iii:fix-transfer-budgets' => [], |
||
99 | 'firefly-iii:fix-uneven-amount' => [], |
||
100 | 'firefly-iii:delete-zero-amount' => [], |
||
101 | 'firefly-iii:delete-orphaned-transactions' => [], |
||
102 | 'firefly-iii:delete-empty-journals' => [], |
||
103 | 'firefly-iii:delete-empty-groups' => [], |
||
104 | 'firefly-iii:fix-account-types' => [], |
||
105 | 'firefly-iii:rename-meta-fields' => [], |
||
106 | 'firefly-iii:fix-ob-currencies' => [], |
||
107 | 'firefly-iii:fix-long-descriptions' => [], |
||
108 | 'firefly-iii:fix-recurring-transactions' => [], |
||
109 | |||
110 | // final command to set latest version in DB |
||
111 | 'firefly-iii:set-latest-version' => ['--james-is-cool' => true], |
||
112 | ]; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Show index. |
||
117 | * |
||
118 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
119 | */ |
||
120 | public function index() |
||
121 | { |
||
122 | // index will set FF3 version. |
||
123 | app('fireflyconfig')->set('ff3_version', (string) config('firefly.version')); |
||
124 | |||
125 | // set new DB version. |
||
126 | app('fireflyconfig')->set('db_version', (int) config('firefly.db_version')); |
||
127 | |||
128 | return view('install.index'); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Create specific RSA keys. |
||
133 | */ |
||
134 | public function keys(): void |
||
135 | { |
||
136 | $rsa = new RSA(); |
||
137 | $keys = $rsa->createKey(4096); |
||
138 | |||
139 | [$publicKey, $privateKey] = [ |
||
140 | Passport::keyPath('oauth-public.key'), |
||
141 | Passport::keyPath('oauth-private.key'), |
||
142 | ]; |
||
143 | |||
144 | if (file_exists($publicKey) || file_exists($privateKey)) { |
||
145 | return; |
||
146 | } |
||
147 | |||
148 | file_put_contents($publicKey, Arr::get($keys, 'publickey')); |
||
149 | file_put_contents($privateKey, Arr::get($keys, 'privatekey')); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param Request $request |
||
154 | * |
||
155 | * @return JsonResponse |
||
156 | */ |
||
157 | public function runCommand(Request $request): JsonResponse |
||
158 | { |
||
159 | $requestIndex = (int) $request->get('index'); |
||
160 | $response = [ |
||
161 | 'hasNextCommand' => false, |
||
162 | 'done' => true, |
||
163 | 'next' => 0, |
||
164 | 'previous' => null, |
||
165 | 'error' => false, |
||
166 | 'errorMessage' => null, |
||
167 | ]; |
||
168 | |||
169 | Log::debug(sprintf('Will now run commands. Request index is %d', $requestIndex)); |
||
170 | $index = 0; |
||
171 | foreach ($this->upgradeCommands as $command => $args) { |
||
172 | Log::debug(sprintf('Current command is "%s", index is %d', $command, $index)); |
||
173 | if ($index < $requestIndex) { |
||
174 | Log::debug('Will not execute.'); |
||
175 | $index++; |
||
176 | continue; |
||
177 | } |
||
178 | if ($index >= $requestIndex) { |
||
179 | Log::debug(sprintf('%d >= %d, will execute the command.', $index, $requestIndex)); |
||
180 | Log::debug(sprintf('Will now call command %s with args.', $command), $args); |
||
181 | try { |
||
182 | if ('generate-keys' === $command) { |
||
183 | $this->keys(); |
||
184 | } |
||
185 | if ('generate-keys' !== $command) { |
||
186 | Artisan::call($command, $args); |
||
187 | Log::debug(Artisan::output()); |
||
188 | } |
||
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 | $response['error'] = true; |
||
194 | $response['errorMessage'] = self::BASEDIR_ERROR; |
||
195 | |||
196 | return response()->json($response); |
||
197 | } |
||
198 | |||
199 | $response['error'] = true; |
||
200 | $response['errorMessage'] = self::OTHER_ERROR . ' ' . $e->getMessage(); |
||
201 | |||
202 | return response()->json($response); |
||
203 | } |
||
204 | // clear cache as well. |
||
205 | Cache::clear(); |
||
206 | Preferences::mark(); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
207 | |||
208 | $index++; |
||
209 | $response['hasNextCommand'] = true; |
||
210 | $response['previous'] = $command; |
||
211 | break; |
||
212 | } |
||
213 | } |
||
214 | $response['next'] = $index; |
||
215 | |||
216 | return response()->json($response); |
||
217 | } |
||
218 | } |
||
219 |