2018-03-07 13:21:36 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* InstallController.php
|
2020-01-31 00:32:04 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-03-07 13:21:36 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-03-07 13:21:36 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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.
|
2018-03-07 13:21:36 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-03-07 13:21:36 -06:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-03-07 13:21:36 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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/>.
|
2018-03-07 13:21:36 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\System;
|
2021-03-28 04:46:23 -05:00
|
|
|
|
2018-03-07 13:21:36 -06:00
|
|
|
use Artisan;
|
2019-03-17 03:06:45 -05:00
|
|
|
use Cache;
|
2018-04-27 05:58:43 -05:00
|
|
|
use Exception;
|
2018-03-07 13:21:36 -06:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
2019-03-17 03:06:45 -05:00
|
|
|
use FireflyIII\Support\Facades\Preferences;
|
2018-08-09 10:50:30 -05:00
|
|
|
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
|
2021-03-28 04:46:23 -05:00
|
|
|
use Illuminate\Contracts\View\Factory;
|
2018-04-27 05:58:43 -05:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2019-03-30 05:03:39 -05:00
|
|
|
use Illuminate\Http\Request;
|
2021-03-28 04:46:23 -05:00
|
|
|
use Illuminate\View\View;
|
2018-03-07 13:21:36 -06:00
|
|
|
use Laravel\Passport\Passport;
|
2018-03-07 13:55:11 -06:00
|
|
|
use Log;
|
2021-04-05 05:42:34 -05:00
|
|
|
use phpseclib\Crypt\RSA as LegacyRSA;
|
2021-03-28 04:46:23 -05:00
|
|
|
use phpseclib3\Crypt\RSA;
|
|
|
|
|
2018-03-07 13:21:36 -06:00
|
|
|
/**
|
|
|
|
* Class InstallController
|
2018-08-06 12:14:30 -05:00
|
|
|
*
|
2018-07-22 01:10:16 -05:00
|
|
|
* @codeCoverageIgnore
|
2018-03-07 13:21:36 -06:00
|
|
|
*/
|
|
|
|
class InstallController extends Controller
|
|
|
|
{
|
2018-08-09 10:50:30 -05:00
|
|
|
use GetConfigurationData;
|
2019-03-30 05:03:39 -05:00
|
|
|
|
2020-10-18 09:44:34 -05:00
|
|
|
public const FORBIDDEN_ERROR = 'Internal PHP function "proc_close" is disabled for your installation. Auto-migration is not possible.';
|
|
|
|
public const BASEDIR_ERROR = 'Firefly III cannot execute the upgrade commands. It is not allowed to because of an open_basedir restriction.';
|
|
|
|
public const OTHER_ERROR = 'An unknown error prevented Firefly III from executing the upgrade commands. Sorry.';
|
|
|
|
private string $lastError;
|
2021-03-28 04:46:23 -05:00
|
|
|
private array $upgradeCommands;
|
2018-03-28 12:37:59 -05:00
|
|
|
/** @noinspection MagicMethodsValidityInspection */
|
2018-04-02 08:10:40 -05:00
|
|
|
/** @noinspection PhpMissingParentConstructorInspection */
|
2021-03-28 04:46:23 -05:00
|
|
|
|
2018-03-07 13:21:36 -06:00
|
|
|
/**
|
|
|
|
* InstallController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// empty on purpose.
|
2019-03-30 05:03:39 -05:00
|
|
|
$this->upgradeCommands = [
|
2019-06-13 00:17:31 -05:00
|
|
|
// there are 3 initial commands
|
2019-03-30 05:03:39 -05:00
|
|
|
'migrate' => ['--seed' => true, '--force' => true],
|
2021-04-30 23:46:36 -05:00
|
|
|
'firefly-iii:fix-pgsql-sequences' => [],
|
2019-06-11 13:10:59 -05:00
|
|
|
'firefly-iii:decrypt-all' => [],
|
2019-09-21 04:06:07 -05:00
|
|
|
'firefly-iii:restore-oauth-keys' => [],
|
2019-03-30 05:03:39 -05:00
|
|
|
'generate-keys' => [], // an exception :(
|
|
|
|
|
2020-07-26 08:05:48 -05:00
|
|
|
// upgrade commands
|
2019-03-30 05:03:39 -05:00
|
|
|
'firefly-iii:transaction-identifiers' => [],
|
2019-06-22 22:52:33 -05:00
|
|
|
'firefly-iii:migrate-to-groups' => [],
|
2019-03-30 05:03:39 -05:00
|
|
|
'firefly-iii:account-currencies' => [],
|
2019-06-11 13:10:59 -05:00
|
|
|
'firefly-iii:transfer-currencies' => [],
|
|
|
|
'firefly-iii:other-currencies' => [],
|
2019-03-30 05:03:39 -05:00
|
|
|
'firefly-iii:migrate-notes' => [],
|
|
|
|
'firefly-iii:migrate-attachments' => [],
|
|
|
|
'firefly-iii:bills-to-rules' => [],
|
|
|
|
'firefly-iii:bl-currency' => [],
|
|
|
|
'firefly-iii:cc-liabilities' => [],
|
|
|
|
'firefly-iii:back-to-journals' => [],
|
2019-06-22 22:52:33 -05:00
|
|
|
'firefly-iii:rename-account-meta' => [],
|
2019-08-26 11:38:23 -05:00
|
|
|
'firefly-iii:migrate-recurrence-meta' => [],
|
2019-12-30 05:13:45 -06:00
|
|
|
'firefly-iii:migrate-tag-locations' => [],
|
2021-03-11 23:30:40 -06:00
|
|
|
'firefly-iii:migrate-recurrence-type' => [],
|
2021-05-01 23:39:18 -05:00
|
|
|
'firefly-iii:upgrade-liabilities' => [],
|
2019-03-30 05:03:39 -05:00
|
|
|
|
2020-07-26 08:05:48 -05:00
|
|
|
// verify commands
|
2019-03-30 05:03:39 -05:00
|
|
|
'firefly-iii:fix-piggies' => [],
|
|
|
|
'firefly-iii:create-link-types' => [],
|
|
|
|
'firefly-iii:create-access-tokens' => [],
|
|
|
|
'firefly-iii:remove-bills' => [],
|
|
|
|
'firefly-iii:enable-currencies' => [],
|
|
|
|
'firefly-iii:fix-transfer-budgets' => [],
|
|
|
|
'firefly-iii:fix-uneven-amount' => [],
|
|
|
|
'firefly-iii:delete-zero-amount' => [],
|
|
|
|
'firefly-iii:delete-orphaned-transactions' => [],
|
|
|
|
'firefly-iii:delete-empty-journals' => [],
|
|
|
|
'firefly-iii:delete-empty-groups' => [],
|
|
|
|
'firefly-iii:fix-account-types' => [],
|
2020-07-26 08:05:48 -05:00
|
|
|
'firefly-iii:fix-account-order' => [],
|
2019-04-06 01:10:50 -05:00
|
|
|
'firefly-iii:rename-meta-fields' => [],
|
2019-08-18 06:01:38 -05:00
|
|
|
'firefly-iii:fix-ob-currencies' => [],
|
2019-09-21 04:03:00 -05:00
|
|
|
'firefly-iii:fix-long-descriptions' => [],
|
2019-12-27 23:58:49 -06:00
|
|
|
'firefly-iii:fix-recurring-transactions' => [],
|
2020-06-30 23:02:58 -05:00
|
|
|
'firefly-iii:unify-group-accounts' => [],
|
2020-07-19 06:03:18 -05:00
|
|
|
'firefly-iii:fix-transaction-types' => [],
|
2021-04-22 11:33:53 -05:00
|
|
|
'firefly-iii:fix-frontpage-accounts' => [],
|
2019-11-10 00:25:55 -06:00
|
|
|
|
|
|
|
// final command to set latest version in DB
|
2019-12-27 23:58:49 -06:00
|
|
|
'firefly-iii:set-latest-version' => ['--james-is-cool' => true],
|
2021-06-11 13:19:59 -05:00
|
|
|
'firefly-iii:verify-security-alerts' => [],
|
2019-03-30 05:03:39 -05:00
|
|
|
];
|
2020-10-18 09:44:34 -05:00
|
|
|
|
|
|
|
$this->lastError = '';
|
2018-03-07 13:21:36 -06:00
|
|
|
}
|
2019-04-06 01:10:50 -05:00
|
|
|
|
2018-03-07 13:21:36 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Show index.
|
|
|
|
*
|
2021-03-28 04:46:23 -05:00
|
|
|
* @return Factory|View
|
2018-03-07 13:21:36 -06:00
|
|
|
*/
|
2018-03-10 00:17:05 -06:00
|
|
|
public function index()
|
2018-03-07 13:21:36 -06:00
|
|
|
{
|
2019-03-17 03:06:45 -05:00
|
|
|
// index will set FF3 version.
|
2021-03-11 23:30:40 -06:00
|
|
|
app('fireflyconfig')->set('ff3_version', (string)config('firefly.version'));
|
2019-03-17 03:06:45 -05:00
|
|
|
|
2019-08-18 06:01:38 -05:00
|
|
|
// set new DB version.
|
2021-03-11 23:30:40 -06:00
|
|
|
app('fireflyconfig')->set('db_version', (int)config('firefly.db_version'));
|
2019-08-18 06:01:38 -05:00
|
|
|
|
2021-01-31 13:35:54 -06:00
|
|
|
return prefixView('install.index');
|
2018-03-10 00:17:05 -06:00
|
|
|
}
|
2018-03-07 13:21:36 -06:00
|
|
|
|
2018-03-10 00:17:05 -06:00
|
|
|
/**
|
2019-03-30 05:03:39 -05:00
|
|
|
* @param Request $request
|
2018-07-22 01:10:16 -05:00
|
|
|
*
|
2018-04-27 05:58:43 -05:00
|
|
|
* @return JsonResponse
|
2018-03-10 00:17:05 -06:00
|
|
|
*/
|
2019-03-30 05:03:39 -05:00
|
|
|
public function runCommand(Request $request): JsonResponse
|
2018-03-10 00:17:05 -06:00
|
|
|
{
|
2021-03-11 23:30:40 -06:00
|
|
|
$requestIndex = (int)$request->get('index');
|
2019-03-30 05:03:39 -05:00
|
|
|
$response = [
|
|
|
|
'hasNextCommand' => false,
|
|
|
|
'done' => true,
|
|
|
|
'next' => 0,
|
|
|
|
'previous' => null,
|
|
|
|
'error' => false,
|
|
|
|
'errorMessage' => null,
|
|
|
|
];
|
2018-03-10 00:17:05 -06:00
|
|
|
|
2019-03-30 05:03:39 -05:00
|
|
|
Log::debug(sprintf('Will now run commands. Request index is %d', $requestIndex));
|
|
|
|
$index = 0;
|
2020-10-18 09:44:34 -05:00
|
|
|
/**
|
|
|
|
* @var string $command
|
|
|
|
* @var array $args
|
|
|
|
*/
|
2019-03-30 05:03:39 -05:00
|
|
|
foreach ($this->upgradeCommands as $command => $args) {
|
|
|
|
Log::debug(sprintf('Current command is "%s", index is %d', $command, $index));
|
|
|
|
if ($index < $requestIndex) {
|
|
|
|
Log::debug('Will not execute.');
|
|
|
|
$index++;
|
|
|
|
continue;
|
2018-04-27 05:58:43 -05:00
|
|
|
}
|
2020-10-18 09:44:34 -05:00
|
|
|
$result = $this->executeCommand($command, $args);
|
|
|
|
if (false === $result) {
|
|
|
|
$response['errorMessage'] = $this->lastError;
|
|
|
|
$response['error'] = true;
|
2021-03-11 23:30:40 -06:00
|
|
|
|
2020-10-18 09:44:34 -05:00
|
|
|
return response()->json($response);
|
2018-04-27 05:58:43 -05:00
|
|
|
}
|
2020-10-18 09:44:34 -05:00
|
|
|
$index++;
|
|
|
|
$response['hasNextCommand'] = true;
|
|
|
|
$response['previous'] = $command;
|
2018-04-27 05:58:43 -05:00
|
|
|
}
|
2019-03-30 05:03:39 -05:00
|
|
|
$response['next'] = $index;
|
2019-03-17 03:06:45 -05:00
|
|
|
|
2019-03-30 05:03:39 -05:00
|
|
|
return response()->json($response);
|
2018-04-06 06:36:36 -05:00
|
|
|
}
|
2020-10-18 09:44:34 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $command
|
|
|
|
* @param array $args
|
2021-03-11 23:30:40 -06:00
|
|
|
*
|
2020-10-18 09:44:34 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function executeCommand(string $command, array $args): bool
|
|
|
|
{
|
|
|
|
Log::debug(sprintf('Will now call command %s with args.', $command), $args);
|
|
|
|
try {
|
|
|
|
if ('generate-keys' === $command) {
|
|
|
|
$this->keys();
|
|
|
|
}
|
|
|
|
if ('generate-keys' !== $command) {
|
|
|
|
Artisan::call($command, $args);
|
|
|
|
Log::debug(Artisan::output());
|
|
|
|
}
|
2021-04-07 00:28:43 -05:00
|
|
|
} catch (Exception $e) { // @phpstan-ignore-line
|
2020-10-18 09:44:34 -05:00
|
|
|
Log::error($e->getMessage());
|
|
|
|
Log::error($e->getTraceAsString());
|
|
|
|
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
|
|
|
|
$this->lastError = self::BASEDIR_ERROR;
|
2021-03-11 23:30:40 -06:00
|
|
|
|
2020-10-18 09:44:34 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->lastError = sprintf('%s %s', self::OTHER_ERROR, $e->getMessage());
|
2021-03-11 23:30:40 -06:00
|
|
|
|
2020-10-18 09:44:34 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// clear cache as well.
|
2021-04-07 00:32:54 -05:00
|
|
|
Cache::clear(); // @phpstan-ignore-line
|
2020-10-18 09:44:34 -05:00
|
|
|
Preferences::mark();
|
2021-03-11 23:30:40 -06:00
|
|
|
|
2020-10-18 09:44:34 -05:00
|
|
|
return true;
|
|
|
|
}
|
2021-03-28 04:46:23 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create specific RSA keys.
|
|
|
|
*/
|
|
|
|
public function keys(): void
|
|
|
|
{
|
|
|
|
// switch on PHP version.
|
2021-04-06 10:00:16 -05:00
|
|
|
$keys = [];
|
2021-04-05 05:42:34 -05:00
|
|
|
// switch on class existence.
|
|
|
|
Log::info(sprintf('PHP version is %s', phpversion()));
|
|
|
|
if (class_exists(LegacyRSA::class)) {
|
2021-03-28 04:46:23 -05:00
|
|
|
// PHP 7
|
2021-04-05 05:42:34 -05:00
|
|
|
Log::info('Will run PHP7 code.');
|
|
|
|
$keys = (new LegacyRSA)->createKey(4096);
|
2021-03-28 04:46:23 -05:00
|
|
|
}
|
|
|
|
|
2021-04-05 05:42:34 -05:00
|
|
|
if (!class_exists(LegacyRSA::class)) {
|
2021-03-28 04:46:23 -05:00
|
|
|
// PHP 8
|
2021-04-05 05:42:34 -05:00
|
|
|
Log::info('Will run PHP8 code.');
|
2021-03-28 04:46:23 -05:00
|
|
|
$keys = RSA::createKey(4096);
|
|
|
|
}
|
|
|
|
|
|
|
|
[$publicKey, $privateKey] = [
|
|
|
|
Passport::keyPath('oauth-public.key'),
|
|
|
|
Passport::keyPath('oauth-private.key'),
|
|
|
|
];
|
|
|
|
|
|
|
|
if (file_exists($publicKey) || file_exists($privateKey)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:00:16 -05:00
|
|
|
file_put_contents($publicKey, $keys['publickey']);
|
|
|
|
file_put_contents($privateKey, $keys['privatekey']);
|
2021-03-28 04:46:23 -05:00
|
|
|
}
|
2018-03-19 07:23:26 -05:00
|
|
|
}
|