firefly-iii/app/Http/Controllers/System/InstallController.php

209 lines
7.5 KiB
PHP
Raw Normal View History

2018-03-07 13:21:36 -06:00
<?php
/**
* InstallController.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\System;
use Artisan;
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;
use FireflyIII\Support\Facades\Preferences;
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
2018-04-27 05:58:43 -05:00
use Illuminate\Http\JsonResponse;
2019-03-30 05:03:39 -05:00
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
2018-03-07 13:21:36 -06:00
use Laravel\Passport\Passport;
2018-03-07 13:55:11 -06:00
use Log;
use phpseclib\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
{
use GetConfigurationData;
2018-07-22 01:10:16 -05:00
/** @var string Forbidden error */
2018-04-06 06:36:36 -05:00
public const FORBIDDEN_ERROR = 'Internal PHP function "proc_close" is disabled for your installation. Auto-migration is not possible.';
2018-07-22 01:10:16 -05:00
/** @var string Basedir error */
2018-04-27 05:58:43 -05:00
public const BASEDIR_ERROR = 'Firefly III cannot execute the upgrade commands. It is not allowed to because of an open_basedir restriction.';
2018-07-22 01:10:16 -05:00
/** @var string Other errors */
2018-04-27 05:58:43 -05:00
public const OTHER_ERROR = 'An unknown error prevented Firefly III from executing the upgrade commands. Sorry.';
2019-03-30 05:03:39 -05:00
/** @var array All upgrade commands. */
private $upgradeCommands;
/** @noinspection MagicMethodsValidityInspection */
2018-04-02 08:10:40 -05:00
/** @noinspection PhpMissingParentConstructorInspection */
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],
'firefly-iii:decrypt-all' => [],
2019-03-30 05:03:39 -05:00
'generate-keys' => [], // an exception :(
2019-06-21 12:10:14 -05:00
// there are 12 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' => [],
'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-03-30 05:03:39 -05:00
// there are 14 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' => [],
'firefly-iii:rename-meta-fields' => [],
'firefly-iii:fix-ob-currencies' => [],
2019-03-30 05:03:39 -05:00
];
2018-03-07 13:21:36 -06:00
}
2018-03-07 13:21:36 -06:00
/**
2018-07-22 01:10:16 -05:00
* Show index.
*
2018-03-07 13:21:36 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
2018-03-10 00:17:05 -06:00
public function index()
2018-03-07 13:21:36 -06:00
{
// index will set FF3 version.
app('fireflyconfig')->set('ff3_version', (string)config('firefly.version'));
// set new DB version.
app('fireflyconfig')->set('db_version', (int)config('firefly.db_version'));
2018-03-10 00:17:05 -06:00
return view('install.index');
}
2018-03-07 13:21:36 -06:00
2018-03-10 00:17:05 -06:00
/**
2018-07-22 01:10:16 -05:00
* Create specific RSA keys.
2018-03-10 00:17:05 -06:00
*/
2019-03-30 05:03:39 -05:00
public function keys(): void
2018-03-10 00:17:05 -06:00
{
2018-03-07 13:21:36 -06:00
$rsa = new RSA();
$keys = $rsa->createKey(4096);
2018-04-02 08:10:40 -05:00
[$publicKey, $privateKey] = [
2018-03-07 13:21:36 -06:00
Passport::keyPath('oauth-public.key'),
Passport::keyPath('oauth-private.key'),
];
if (file_exists($publicKey) || file_exists($privateKey)) {
2019-03-30 05:03:39 -05:00
return;
2018-03-07 13:21:36 -06:00
}
file_put_contents($publicKey, Arr::get($keys, 'publickey'));
file_put_contents($privateKey, Arr::get($keys, 'privatekey'));
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
{
2019-03-30 05:03:39 -05:00
$requestIndex = (int)$request->get('index');
$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;
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
}
2019-03-30 05:03:39 -05:00
if ($index >= $requestIndex) {
Log::debug(sprintf('%d >= %d, will execute the command.', $index, $requestIndex));
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());
}
} catch (Exception $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
$response['error'] = true;
$response['errorMessage'] = self::BASEDIR_ERROR;
return response()->json($response);
}
$response['error'] = true;
$response['errorMessage'] = self::OTHER_ERROR . ' ' . $e->getMessage();
return response()->json($response);
}
// clear cache as well.
Cache::clear();
Preferences::mark();
2018-04-27 05:58:43 -05:00
2019-03-30 05:03:39 -05:00
$index++;
$response['hasNextCommand'] = true;
$response['previous'] = $command;
break;
2018-04-27 05:58:43 -05:00
}
}
2019-03-30 05:03:39 -05:00
$response['next'] = $index;
2019-03-30 05:03:39 -05:00
return response()->json($response);
2018-04-06 06:36:36 -05:00
}
}