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

212 lines
7.0 KiB
PHP
Raw Normal View History

2018-03-07 20:21:36 +01: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;
2018-04-27 12:58:43 +02:00
use Exception;
2018-03-07 20:21:36 +01:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
2018-04-27 12:58:43 +02:00
use Illuminate\Http\JsonResponse;
2018-03-07 20:21:36 +01:00
use Laravel\Passport\Passport;
2018-03-07 20:55:11 +01:00
use Log;
use phpseclib\Crypt\RSA;
2018-03-07 20:21:36 +01:00
/**
* Class InstallController
2018-08-06 19:14:30 +02:00
*
2018-07-22 08:10:16 +02:00
* @codeCoverageIgnore
2018-03-07 20:21:36 +01:00
*/
class InstallController extends Controller
{
use GetConfigurationData;
2018-07-22 08:10:16 +02:00
/** @var string Forbidden error */
2018-04-06 13:36:36 +02:00
public const FORBIDDEN_ERROR = 'Internal PHP function "proc_close" is disabled for your installation. Auto-migration is not possible.';
2018-07-22 08:10:16 +02:00
/** @var string Basedir error */
2018-04-27 12:58:43 +02: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 08:10:16 +02:00
/** @var string Other errors */
2018-04-27 12:58:43 +02:00
public const OTHER_ERROR = 'An unknown error prevented Firefly III from executing the upgrade commands. Sorry.';
/** @noinspection MagicMethodsValidityInspection */
2018-04-02 15:10:40 +02:00
/** @noinspection PhpMissingParentConstructorInspection */
2018-03-07 20:21:36 +01:00
/**
* InstallController constructor.
*/
public function __construct()
{
// empty on purpose.
}
2019-02-13 17:38:41 +01:00
/**
* Do database decrypt.
*
* @return \Illuminate\Http\JsonResponse
*/
public function decrypt(): JsonResponse
{
if ($this->hasForbiddenFunctions()) {
return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]);
}
try {
Log::debug('Am now calling decrypt database routine...');
Artisan::call('firefly:decrypt-all');
Log::debug(Artisan::output());
} catch (Exception $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]);
}
return response()->json(['error' => true, 'message' => self::OTHER_ERROR . ' ' . $e->getMessage()]);
}
return response()->json(['error' => false, 'message' => 'OK']);
}
2018-03-07 20:21:36 +01:00
/**
2018-07-22 08:10:16 +02:00
* Show index.
*
2018-03-07 20:21:36 +01:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
2018-03-10 07:17:05 +01:00
public function index()
2018-03-07 20:21:36 +01:00
{
2018-03-10 07:17:05 +01:00
return view('install.index');
}
2018-03-07 20:21:36 +01:00
2018-03-10 07:17:05 +01:00
/**
2018-07-22 08:10:16 +02:00
* Create specific RSA keys.
*
2018-03-10 07:17:05 +01:00
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function keys()
{
2018-04-06 13:36:36 +02:00
if ($this->hasForbiddenFunctions()) {
return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]);
}
2018-03-07 20:21:36 +01:00
// create keys manually because for some reason the passport namespace
// does not exist
$rsa = new RSA();
$keys = $rsa->createKey(4096);
2018-04-02 15:10:40 +02:00
[$publicKey, $privateKey] = [
2018-03-07 20:21:36 +01:00
Passport::keyPath('oauth-public.key'),
Passport::keyPath('oauth-private.key'),
];
if (file_exists($publicKey) || file_exists($privateKey)) {
2018-04-06 13:36:36 +02:00
return response()->json(['error' => false, 'message' => 'OK']);
2018-03-07 20:21:36 +01:00
}
file_put_contents($publicKey, array_get($keys, 'publickey'));
file_put_contents($privateKey, array_get($keys, 'privatekey'));
2018-04-06 13:36:36 +02:00
return response()->json(['error' => false, 'message' => 'OK']);
2018-03-10 07:17:05 +01:00
}
/**
2018-07-22 08:10:16 +02:00
* Run migration commands.
*
2018-04-27 12:58:43 +02:00
* @return JsonResponse
2018-03-10 07:17:05 +01:00
*/
2018-04-27 12:58:43 +02:00
public function migrate(): JsonResponse
2018-03-10 07:17:05 +01:00
{
2018-04-06 13:36:36 +02:00
if ($this->hasForbiddenFunctions()) {
return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]);
}
2018-04-27 12:58:43 +02:00
try {
Log::debug('Am now calling migrate routine...');
Artisan::call('migrate', ['--seed' => true, '--force' => true]);
Log::debug(Artisan::output());
} catch (Exception $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]);
}
return response()->json(['error' => true, 'message' => self::OTHER_ERROR]);
}
2018-03-10 07:17:05 +01:00
2018-04-06 13:36:36 +02:00
return response()->json(['error' => false, 'message' => 'OK']);
2018-03-10 07:17:05 +01:00
}
/**
2018-07-22 08:10:16 +02:00
* Do database upgrade.
*
2018-03-10 07:17:05 +01:00
* @return \Illuminate\Http\JsonResponse
*/
2018-04-27 12:58:43 +02:00
public function upgrade(): JsonResponse
2018-03-10 07:17:05 +01:00
{
2018-04-06 13:36:36 +02:00
if ($this->hasForbiddenFunctions()) {
return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]);
}
2018-04-27 12:58:43 +02:00
try {
Log::debug('Am now calling upgrade database routine...');
Artisan::call('firefly:upgrade-database');
2018-04-27 12:58:43 +02:00
Log::debug(Artisan::output());
} catch (Exception $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]);
}
return response()->json(['error' => true, 'message' => self::OTHER_ERROR . ' ' . $e->getMessage()]);
2018-04-27 12:58:43 +02:00
}
2018-03-10 07:17:05 +01:00
2018-04-06 13:36:36 +02:00
return response()->json(['error' => false, 'message' => 'OK']);
2018-03-07 20:21:36 +01:00
}
2018-03-11 18:47:26 +01:00
/**
2018-07-22 08:10:16 +02:00
* Do database verification.
*
2018-03-11 18:47:26 +01:00
* @return \Illuminate\Http\JsonResponse
*/
2018-07-08 12:28:42 +02:00
public function verify(): JsonResponse
2018-03-11 18:47:26 +01:00
{
2018-04-06 13:36:36 +02:00
if ($this->hasForbiddenFunctions()) {
return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]);
}
2018-04-27 12:58:43 +02:00
try {
Log::debug('Am now calling verify database routine...');
Artisan::call('firefly:verify');
2018-04-27 12:58:43 +02:00
Log::debug(Artisan::output());
} catch (Exception $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]);
}
return response()->json(['error' => true, 'message' => self::OTHER_ERROR . ' ' . $e->getMessage()]);
2018-04-27 12:58:43 +02:00
}
2018-03-11 18:47:26 +01:00
2018-04-06 13:36:36 +02:00
return response()->json(['error' => false, 'message' => 'OK']);
}
2018-03-11 18:47:26 +01:00
}