1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/SetupController.php

295 lines
9.2 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers;
use App\Http\Requests\Setup\CheckDatabaseRequest;
use App\Http\Requests\Setup\CheckMailRequest;
use App\Http\Requests\Setup\StoreSetupRequest;
use App\Jobs\Account\CreateAccount;
2021-08-13 00:02:51 +02:00
use App\Jobs\Util\SchedulerCheck;
2020-10-26 20:10:04 +01:00
use App\Jobs\Util\VersionCheck;
use App\Models\Account;
2020-10-28 11:10:49 +01:00
use App\Utils\CurlUtils;
2021-04-08 12:01:00 +02:00
use App\Utils\Ninja;
use App\Utils\SystemHealth;
2020-11-01 23:51:56 +01:00
use App\Utils\Traits\AppSetup;
2020-10-28 11:10:49 +01:00
use Exception;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
2020-07-02 13:58:39 +02:00
use Illuminate\Support\Facades\Request;
2020-06-28 13:52:45 +02:00
use Illuminate\Support\Facades\Schema;
2020-07-02 13:58:39 +02:00
use Illuminate\Support\Facades\Storage;
/**
* Class SetupController.
*/
class SetupController extends Controller
{
2020-11-01 23:51:56 +01:00
use AppSetup;
public function index()
{
2020-07-06 14:27:27 +02:00
$check = SystemHealth::check(false);
2022-05-26 04:00:53 +02:00
if ($check['system_health'] == true && $check['simple_db_check'] && Schema::hasTable('accounts') && $account = Account::first()) {
2020-06-28 13:52:45 +02:00
return redirect('/');
2021-04-22 05:12:48 +02:00
}
if (Ninja::isHosted()) {
return redirect('/');
}
return view('setup.index', ['check' => $check]);
}
public function doSetup(StoreSetupRequest $request)
{
2020-11-24 11:52:01 +01:00
try {
$check = SystemHealth::check(false);
2021-01-04 13:36:47 +01:00
} catch (Exception $e) {
2020-12-29 22:10:03 +01:00
nlog(['message' => $e->getMessage(), 'action' => 'SetupController::doSetup()']);
2020-11-24 11:52:01 +01:00
return response()->json(['message' => $e->getMessage()], 400);
}
if ($check['system_health'] === false) {
2020-11-24 11:12:44 +01:00
return response('Oops, something went wrong. Check your logs.'); /* We should never reach this block, but just in case. */
}
$mail_driver = $request->input('mail_driver');
2021-09-02 09:51:54 +02:00
$url = $request->input('url');
$db_host = $request->input('db_host');
$db_port = $request->input('db_port');
$db_database = $request->input('db_database');
$db_username = $request->input('db_username');
$db_password = $request->input('db_password');
$mail_port = $request->input('mail_port');
$encryption = $request->input('encryption');
$mail_host = $request->input('mail_host');
$mail_username = $request->input('mail_username');
$mail_name = $request->input('mail_name');
$mail_address = $request->input('mail_address');
$mail_password = $request->input('mail_password');
$env_values = [
2021-09-02 09:51:54 +02:00
'APP_URL' => $url,
'REQUIRE_HTTPS' => $request->input('https') ? 'true' : 'false',
2021-01-04 13:43:13 +01:00
'APP_DEBUG' => 'false',
2021-09-02 09:51:54 +02:00
'DB_HOST' => $db_host,
'DB_PORT' => $db_port,
'DB_DATABASE' => $db_database,
'DB_USERNAME' => $db_username,
'DB_PASSWORD' => $db_password,
'MAIL_MAILER' => $mail_driver,
2021-09-02 09:51:54 +02:00
'MAIL_PORT' => $mail_port,
'MAIL_ENCRYPTION' => $encryption,
'MAIL_HOST' => $mail_host,
'MAIL_USERNAME' => $mail_username,
'MAIL_FROM_NAME' => $mail_name,
'MAIL_FROM_ADDRESS' => $mail_address,
'MAIL_PASSWORD' => $mail_password,
'NINJA_ENVIRONMENT' => 'selfhost',
2021-07-02 02:43:32 +02:00
'DB_CONNECTION' => 'mysql',
];
2020-11-24 11:52:01 +01:00
2021-04-22 05:12:48 +02:00
if (config('ninja.db.multi_db_enabled')) {
$env_values['DB_CONNECTION'] = 'db-ninja-01';
}
2021-04-22 05:12:48 +02:00
if (config('ninja.preconfigured_install')) {
// Database connection was already configured. Don't let the user override it.
2021-07-02 02:43:32 +02:00
unset($env_values['DB_HOST']);
unset($env_values['DB_PORT']);
unset($env_values['DB_DATABASE']);
unset($env_values['DB_USERNAME']);
unset($env_values['DB_PASSWORD']);
2023-10-26 04:57:44 +02:00
} else {
2024-01-14 05:05:00 +01:00
config(['database.connections.mysql.host' => $request->input('db_host')]);
config(['database.connections.mysql.port' => $request->input('db_port')]);
config(['database.connections.mysql.database' => $request->input('db_database')]);
config(['database.connections.mysql.username' => $request->input('db_username')]);
config(['database.connections.mysql.password' => $request->input('db_password')]);
config(['database.default' => 'mysql']);
DB::purge('mysql');
/* Make sure no stale connections are cached */
}
2021-02-02 14:52:21 +01:00
2021-04-22 05:12:48 +02:00
try {
foreach ($env_values as $property => $value) {
$this->updateEnvironmentProperty($property, $value);
}
/* We need this in some environments that do not have STDIN defined */
define('STDIN', fopen('php://stdin', 'r'));
Artisan::call('config:clear');
Artisan::call('migrate', ['--force' => true]);
Artisan::call('db:seed', ['--force' => true]);
Artisan::call('config:clear');
2020-08-04 15:30:27 +02:00
Storage::disk('local')->delete('test.pdf');
/* Create the first account. */
if (Account::count() == 0) {
(new CreateAccount($request->all(), $request->getClientIp()))->handle();
}
(new VersionCheck())->handle();
2020-11-24 11:52:01 +01:00
return redirect('/');
2020-10-28 11:10:49 +01:00
} catch (Exception $e) {
2020-12-29 22:10:03 +01:00
nlog($e->getMessage());
2021-03-13 07:45:41 +01:00
info($e->getMessage());
echo $e->getMessage();
return redirect()
->back()
->with('setup_error', $e->getMessage());
}
}
/**
* Return status based on database check.
*
* @param CheckDatabaseRequest $request
* @return Application|ResponseFactory|JsonResponse|Response
*/
public function checkDB(CheckDatabaseRequest $request)
{
2024-01-14 05:05:00 +01:00
2020-11-24 11:52:01 +01:00
try {
$status = SystemHealth::dbCheck($request);
2020-11-24 11:52:01 +01:00
if (is_array($status) && $status['success'] === true) {
return response([], 200);
}
return response($status, 400);
2021-01-04 13:36:47 +01:00
} catch (Exception $e) {
nlog("failed?");
nlog($e->getMessage());
2020-12-29 22:10:03 +01:00
nlog(['message' => $e->getMessage(), 'action' => 'SetupController::checkDB()']);
2020-11-24 11:52:01 +01:00
return response()->json(['message' => $e->getMessage()], 400);
}
}
/**
* Return status based on check of SMTP connection.
*
2020-10-09 15:12:50 +02:00
* @param CheckMailRequest $request
2020-10-28 11:10:49 +01:00
* @return Application|ResponseFactory|JsonResponse|Response
*/
public function checkMail(CheckMailRequest $request)
{
try {
2020-12-08 15:04:07 +01:00
$response = SystemHealth::testMailServer($request);
2020-12-08 15:04:07 +01:00
if ($response['success']) {
return response([], 200);
} else {
2020-12-08 15:04:07 +01:00
return response()->json(['message' => $response['message']], 400);
}
2020-10-28 11:10:49 +01:00
} catch (Exception $e) {
2020-12-29 22:10:03 +01:00
nlog(['message' => $e->getMessage(), 'action' => 'SetupController::checkMail()']);
return response()->json(['message' => $e->getMessage()], 400);
}
}
2020-07-02 13:58:39 +02:00
public function checkPdf(Request $request)
{
try {
2022-05-26 04:00:53 +02:00
return response(['url' => ''], 200);
2020-10-28 11:10:49 +01:00
} catch (Exception $e) {
2020-12-29 22:10:03 +01:00
nlog($e->getMessage());
2020-07-02 13:58:39 +02:00
return response([], 500);
}
}
2020-07-21 03:11:35 +02:00
2022-03-18 00:33:26 +01:00
public function clearCompiledCache()
{
$cacheCompiled = base_path('bootstrap/cache/compiled.php');
2022-03-18 00:33:26 +01:00
if (file_exists($cacheCompiled)) {
unlink($cacheCompiled);
2022-03-18 00:33:26 +01:00
}
$cacheServices = base_path('bootstrap/cache/packages.php');
if (file_exists($cacheServices)) {
unlink($cacheServices);
2022-03-18 00:33:26 +01:00
}
$cacheServices = base_path('bootstrap/cache/services.php');
if (file_exists($cacheServices)) {
unlink($cacheServices);
2022-03-18 00:33:26 +01:00
}
$cacheRoute = base_path('bootstrap/cache/routes-v7.php');
if (file_exists($cacheRoute)) {
unlink($cacheRoute);
}
2022-03-18 00:33:26 +01:00
}
2021-04-08 12:01:00 +02:00
public function update()
{
if (! request()->has('secret') || (request()->input('secret') != config('ninja.update_secret'))) {
2021-04-08 12:01:00 +02:00
return redirect('/');
}
2021-04-08 12:01:00 +02:00
$cacheCompiled = base_path('bootstrap/cache/compiled.php');
2021-04-22 05:12:48 +02:00
if (file_exists($cacheCompiled)) {
unlink($cacheCompiled);
2021-04-22 05:12:48 +02:00
}
2021-04-30 06:29:27 +02:00
2021-04-08 12:01:00 +02:00
$cacheServices = base_path('bootstrap/cache/services.php');
2021-04-22 05:12:48 +02:00
if (file_exists($cacheServices)) {
unlink($cacheServices);
2021-04-22 05:12:48 +02:00
}
2021-04-08 12:01:00 +02:00
2021-05-18 09:28:13 +02:00
$cacheRoute = base_path('bootstrap/cache/routes-v7.php');
if (file_exists($cacheRoute)) {
unlink($cacheRoute);
2021-05-18 09:28:13 +02:00
}
2021-04-08 12:01:00 +02:00
Artisan::call('clear-compiled');
Artisan::call('route:clear');
Artisan::call('view:clear');
Artisan::call('config:clear');
// Artisan::call('optimize');
2021-04-30 06:29:27 +02:00
2021-04-08 12:01:00 +02:00
Artisan::call('migrate', ['--force' => true]);
Artisan::call('db:seed', ['--force' => true]);
2024-06-21 02:29:38 +02:00
Artisan::call('cache:clear');
2021-04-22 15:32:34 +02:00
(new SchedulerCheck())->handle();
2021-04-08 12:01:00 +02:00
return redirect('/');
2021-04-08 12:01:00 +02:00
}
}