2020-03-18 10:40:15 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-03-30 14:30:32 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-03-18 10:40:15 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-03-28 04:46:50 +01:00
|
|
|
use App\Http\Requests\Setup\CheckDatabaseRequest;
|
2020-03-30 08:40:21 +02:00
|
|
|
use App\Http\Requests\Setup\CheckMailRequest;
|
2020-03-18 10:40:15 +01:00
|
|
|
use App\Http\Requests\Setup\StoreSetupRequest;
|
2020-03-25 22:34:35 +01:00
|
|
|
use App\Jobs\Account\CreateAccount;
|
2020-03-18 10:40:15 +01:00
|
|
|
use App\Models\Account;
|
|
|
|
use App\Utils\SystemHealth;
|
2020-03-26 22:43:44 +01:00
|
|
|
use Illuminate\Http\Response;
|
2020-03-18 10:40:15 +01:00
|
|
|
use Illuminate\Support\Facades\Artisan;
|
2020-06-28 13:52:45 +02:00
|
|
|
use Illuminate\Support\Facades\Schema;
|
2020-03-18 10:40:15 +01:00
|
|
|
|
|
|
|
/**
|
2020-03-30 14:30:32 +02:00
|
|
|
* Class SetupController.
|
2020-03-18 10:40:15 +01:00
|
|
|
*/
|
|
|
|
class SetupController extends Controller
|
|
|
|
{
|
2020-03-30 14:30:32 +02:00
|
|
|
/**
|
|
|
|
* Main setup view.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
|
|
|
|
*/
|
2020-03-21 06:37:30 +01:00
|
|
|
public function index()
|
|
|
|
{
|
2020-03-30 14:30:32 +02:00
|
|
|
$check = SystemHealth::check();
|
2020-03-18 10:40:15 +01:00
|
|
|
|
2020-06-28 13:52:45 +02:00
|
|
|
if($check['system_health'] == "true" && Schema::hasTable('accounts') && $account = Account::all()->first())
|
|
|
|
return redirect('/');
|
|
|
|
|
|
|
|
$check = SystemHealth::check();
|
|
|
|
|
2020-03-30 14:30:32 +02:00
|
|
|
return view('setup.index', ['check' => $check]);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-18 10:40:15 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function doSetup(StoreSetupRequest $request)
|
|
|
|
{
|
2020-03-30 14:30:32 +02:00
|
|
|
$check = SystemHealth::check();
|
|
|
|
|
2020-03-31 09:00:24 +02:00
|
|
|
if ($check['system_health'] === false) {
|
|
|
|
return back(); // This should never be reached.
|
2020-03-30 14:30:32 +02:00
|
|
|
}
|
|
|
|
|
2020-04-02 11:02:39 +02:00
|
|
|
$mail_driver = $request->input('mail_driver');
|
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
if (!$this->failsafeMailCheck($request)) {
|
2020-04-02 11:02:39 +02:00
|
|
|
$mail_driver = 'log';
|
2020-04-04 12:32:42 +02:00
|
|
|
}
|
2020-04-02 11:02:39 +02:00
|
|
|
|
2020-03-30 08:40:21 +02:00
|
|
|
$_ENV['APP_KEY'] = config('app.key');
|
2020-03-18 10:40:15 +01:00
|
|
|
$_ENV['APP_URL'] = $request->input('url');
|
|
|
|
$_ENV['APP_DEBUG'] = $request->input('debug') ? 'true' : 'false';
|
|
|
|
$_ENV['REQUIRE_HTTPS'] = $request->input('https') ? 'true' : 'false';
|
2020-03-21 06:37:30 +01:00
|
|
|
$_ENV['DB_TYPE'] = 'mysql';
|
2020-03-18 10:40:15 +01:00
|
|
|
$_ENV['DB_HOST1'] = $request->input('host');
|
2020-03-30 08:40:21 +02:00
|
|
|
$_ENV['DB_DATABASE1'] = $request->input('database');
|
|
|
|
$_ENV['DB_USERNAME1'] = $request->input('db_username');
|
2020-03-18 10:40:15 +01:00
|
|
|
$_ENV['DB_PASSWORD1'] = $request->input('db_password');
|
2020-04-02 11:02:39 +02:00
|
|
|
$_ENV['MAIL_DRIVER'] = $mail_driver;
|
2020-03-30 08:40:21 +02:00
|
|
|
$_ENV['MAIL_PORT'] = $request->input('mail_port');
|
2020-03-21 06:37:30 +01:00
|
|
|
$_ENV['MAIL_ENCRYPTION'] = $request->input('encryption');
|
|
|
|
$_ENV['MAIL_HOST'] = $request->input('mail_host');
|
|
|
|
$_ENV['MAIL_USERNAME'] = $request->input('mail_username');
|
|
|
|
$_ENV['MAIL_FROM_NAME'] = $request->input('mail_name');
|
|
|
|
$_ENV['MAIL_FROM_ADDRESS'] = $request->input('mail_address');
|
|
|
|
$_ENV['MAIL_PASSWORD'] = $request->input('mail_password');
|
2020-03-18 10:40:15 +01:00
|
|
|
$_ENV['NINJA_ENVIRONMENT'] = 'selfhost';
|
2020-03-21 06:37:30 +01:00
|
|
|
$_ENV['SELF_UPDATER_REPO_VENDOR'] = 'invoiceninja';
|
|
|
|
$_ENV['SELF_UPDATER_REPO_NAME'] = 'invoiceninja';
|
|
|
|
$_ENV['SELF_UPDATER_USE_BRANCH'] = 'v2';
|
|
|
|
$_ENV['SELF_UPDATER_MAILTO_ADDRESS'] = $request->input('mail_address');
|
|
|
|
$_ENV['SELF_UPDATER_MAILTO_NAME'] = $request->input('mail_name');
|
|
|
|
$_ENV['DB_CONNECTION'] = 'db-ninja-01';
|
2020-03-18 10:40:15 +01:00
|
|
|
|
|
|
|
$config = '';
|
|
|
|
|
2020-03-31 09:00:24 +02:00
|
|
|
try {
|
|
|
|
foreach ($_ENV as $key => $val) {
|
|
|
|
if (is_array($val)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (preg_match('/\s/', $val)) {
|
|
|
|
$val = "'{$val}'";
|
|
|
|
}
|
|
|
|
$config .= "{$key}={$val}\n";
|
2020-03-18 10:40:15 +01:00
|
|
|
}
|
|
|
|
|
2020-03-31 09:00:24 +02:00
|
|
|
/* Write the .env file */
|
|
|
|
$filePath = base_path().'/.env';
|
|
|
|
$fp = fopen($filePath, 'w');
|
|
|
|
fwrite($fp, $config);
|
|
|
|
fclose($fp);
|
2020-03-30 14:30:32 +02:00
|
|
|
|
2020-03-31 09:00:24 +02:00
|
|
|
/* We need this in some environments that do not have STDIN defined */
|
|
|
|
define('STDIN', fopen('php://stdin', 'r'));
|
2020-03-30 08:40:21 +02:00
|
|
|
|
2020-03-31 09:00:24 +02:00
|
|
|
/* Make sure no stale connections are cached */
|
|
|
|
\DB::purge('db-ninja-01');
|
2020-03-18 10:40:15 +01:00
|
|
|
|
2020-03-31 09:00:24 +02:00
|
|
|
/* Run migrations */
|
|
|
|
Artisan::call('optimize');
|
|
|
|
Artisan::call('migrate', ['--force' => true]);
|
|
|
|
Artisan::call('db:seed', ['--force' => true]);
|
2020-03-18 10:40:15 +01:00
|
|
|
|
2020-03-31 09:00:24 +02:00
|
|
|
/* Create the first account. */
|
|
|
|
if (Account::count() == 0) {
|
|
|
|
$account = CreateAccount::dispatchNow($request->all());
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect('/');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
info($e->getMessage());
|
2020-03-30 14:30:32 +02:00
|
|
|
|
2020-03-31 09:00:24 +02:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('setup_error', $e->getMessage());
|
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-26 04:23:57 +01:00
|
|
|
|
2020-03-26 22:43:44 +01:00
|
|
|
/**
|
|
|
|
* Return status based on check of database connection.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
2020-03-28 04:46:50 +01:00
|
|
|
public function checkDB(CheckDatabaseRequest $request): Response
|
2020-03-26 04:23:57 +01:00
|
|
|
{
|
2020-03-28 04:46:50 +01:00
|
|
|
$status = SystemHealth::dbCheck($request);
|
2020-03-26 22:43:44 +01:00
|
|
|
|
2020-03-28 04:46:50 +01:00
|
|
|
info($status);
|
2020-03-26 22:43:44 +01:00
|
|
|
|
2020-03-30 08:40:21 +02:00
|
|
|
if (is_array($status) && $status['success'] === true) {
|
2020-03-26 22:43:44 +01:00
|
|
|
return response([], 200);
|
2020-03-26 04:23:57 +01:00
|
|
|
}
|
2020-03-26 22:43:44 +01:00
|
|
|
|
|
|
|
return response([], 400);
|
2020-03-26 04:23:57 +01:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:43:44 +01:00
|
|
|
/**
|
|
|
|
* Return status based on check of SMTP connection.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
2020-03-30 08:40:21 +02:00
|
|
|
public function checkMail(CheckMailRequest $request)
|
2020-03-26 04:23:57 +01:00
|
|
|
{
|
2020-03-28 04:46:50 +01:00
|
|
|
try {
|
2020-03-30 08:40:21 +02:00
|
|
|
$response_array = SystemHealth::testMailServer($request);
|
2020-03-26 22:43:44 +01:00
|
|
|
|
2020-03-30 14:30:32 +02:00
|
|
|
if (count($response_array) == 0) {
|
2020-03-29 14:22:14 +02:00
|
|
|
return response([], 200);
|
2020-03-30 14:30:32 +02:00
|
|
|
} else {
|
2020-03-30 08:40:21 +02:00
|
|
|
return response()->json($response_array, 200);
|
2020-03-30 14:30:32 +02:00
|
|
|
}
|
2020-03-31 09:00:24 +02:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
info(['message' => $e->getMessage(), 'action' => 'SetupController::checkMail()']);
|
|
|
|
|
2020-03-30 08:40:21 +02:00
|
|
|
return response()->json(['message' => $e->getMessage()], 400);
|
2020-03-28 04:46:50 +01:00
|
|
|
}
|
2020-03-26 04:23:57 +01:00
|
|
|
}
|
2020-04-02 11:02:39 +02:00
|
|
|
|
|
|
|
private function failsafeMailCheck($request)
|
|
|
|
{
|
|
|
|
$response_array = SystemHealth::testMailServer($request);
|
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
if ($response_array instanceof Response) {
|
2020-04-02 11:02:39 +02:00
|
|
|
return true;
|
2020-04-04 12:32:42 +02:00
|
|
|
}
|
2020-04-02 11:02:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|