2020-03-18 10:40:15 +01:00
|
|
|
<?php
|
2020-09-06 04:23:04 +02:00
|
|
|
|
2020-03-18 10:40:15 +01:00
|
|
|
/**
|
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
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-03-18 10:40:15 +01:00
|
|
|
*
|
|
|
|
* @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-10-26 20:10:04 +01:00
|
|
|
use App\Jobs\Util\VersionCheck;
|
2020-03-18 10:40:15 +01:00
|
|
|
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;
|
2020-03-18 10:40:15 +01:00
|
|
|
use App\Utils\SystemHealth;
|
2020-11-01 23:51:56 +01:00
|
|
|
use App\Utils\Traits\AppSetup;
|
2020-12-21 16:32:04 +01:00
|
|
|
use Beganovich\Snappdf\Snappdf;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Exception;
|
|
|
|
use Illuminate\Contracts\Foundation\Application;
|
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
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;
|
2021-04-08 12:01:00 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\File;
|
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;
|
2021-04-08 12:01:00 +02:00
|
|
|
use \Illuminate\Support\Facades\DB;
|
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-11-01 23:51:56 +01:00
|
|
|
use AppSetup;
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function index()
|
|
|
|
{
|
2020-07-06 14:27:27 +02:00
|
|
|
$check = SystemHealth::check(false);
|
2020-03-18 10:40:15 +01:00
|
|
|
|
2021-04-22 05:12:48 +02:00
|
|
|
if ($check['system_health'] == true && $check['simple_db_check'] && Schema::hasTable('accounts') && $account = Account::all()->first()) {
|
2020-06-28 13:52:45 +02:00
|
|
|
return redirect('/');
|
2021-04-22 05:12:48 +02:00
|
|
|
}
|
2021-02-24 01:48:32 +01:00
|
|
|
|
|
|
|
// not sure if we really need this.
|
|
|
|
// if(File::exists(base_path('.env')))
|
|
|
|
// abort(400, '.env file already exists, delete file to start Setup again.');
|
2020-06-28 13:52:45 +02:00
|
|
|
|
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-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);
|
|
|
|
}
|
2020-03-30 14:30:32 +02:00
|
|
|
|
2020-03-31 09:00:24 +02:00
|
|
|
if ($check['system_health'] === false) {
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog($check);
|
2020-06-30 14:41:46 +02:00
|
|
|
|
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. */
|
2020-03-30 14:30:32 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 15:04:07 +01:00
|
|
|
try {
|
|
|
|
$db = SystemHealth::dbCheck($request);
|
2020-12-09 13:38:22 +01:00
|
|
|
|
2020-12-08 15:04:07 +01:00
|
|
|
if ($db['success'] == false) {
|
2021-01-04 13:36:47 +01:00
|
|
|
throw new Exception($db['message']);
|
2020-12-08 15:04:07 +01:00
|
|
|
}
|
2021-01-04 13:36:47 +01:00
|
|
|
} catch (Exception $e) {
|
2020-12-08 15:04:07 +01:00
|
|
|
return response([
|
|
|
|
'message' => 'Oops, connection to database was not successful.',
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-12-09 13:38:22 +01:00
|
|
|
if ($request->mail_driver != 'log') {
|
|
|
|
$smtp = SystemHealth::testMailServer($request);
|
2020-12-08 15:04:07 +01:00
|
|
|
|
2020-12-09 13:38:22 +01:00
|
|
|
if ($smtp['success'] == false) {
|
2021-01-04 13:36:47 +01:00
|
|
|
throw new Exception($smtp['message']);
|
2020-12-09 13:38:22 +01:00
|
|
|
}
|
2020-12-08 15:04:07 +01:00
|
|
|
}
|
2021-01-04 13:36:47 +01:00
|
|
|
} catch (Exception $e) {
|
2020-12-08 15:04:07 +01:00
|
|
|
return response([
|
|
|
|
'message' => 'Oops, connection to mail server was not successful.',
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:02:39 +02:00
|
|
|
$mail_driver = $request->input('mail_driver');
|
|
|
|
|
2020-11-24 11:45:27 +01:00
|
|
|
$env_values = [
|
2020-12-17 11:10:47 +01:00
|
|
|
'APP_URL' => $request->input('url'),
|
2020-11-24 11:45:27 +01:00
|
|
|
'REQUIRE_HTTPS' => $request->input('https') ? 'true' : 'false',
|
2021-01-04 13:43:13 +01:00
|
|
|
'APP_DEBUG' => 'false',
|
2020-11-24 11:45:27 +01:00
|
|
|
|
2020-12-08 14:29:15 +01:00
|
|
|
'DB_HOST1' => $request->input('db_host'),
|
2021-01-04 13:36:47 +01:00
|
|
|
'DB_PORT1' => $request->input('db_port'),
|
2020-12-08 14:29:15 +01:00
|
|
|
'DB_DATABASE1' => $request->input('db_database'),
|
2020-11-24 11:45:27 +01:00
|
|
|
'DB_USERNAME1' => $request->input('db_username'),
|
|
|
|
'DB_PASSWORD1' => $request->input('db_password'),
|
|
|
|
|
|
|
|
'MAIL_MAILER' => $mail_driver,
|
|
|
|
'MAIL_PORT' => $request->input('mail_port'),
|
|
|
|
'MAIL_ENCRYPTION' => $request->input('encryption'),
|
|
|
|
'MAIL_HOST' => $request->input('mail_host'),
|
|
|
|
'MAIL_USERNAME' => $request->input('mail_username'),
|
|
|
|
'MAIL_FROM_NAME' => $request->input('mail_name'),
|
|
|
|
'MAIL_FROM_ADDRESS' => $request->input('mail_address'),
|
|
|
|
'MAIL_PASSWORD' => $request->input('mail_password'),
|
|
|
|
|
|
|
|
'NINJA_ENVIRONMENT' => 'selfhost',
|
|
|
|
];
|
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-03-12 01:48:41 +01:00
|
|
|
|
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.
|
|
|
|
unset($env_values['DB_HOST1']);
|
|
|
|
unset($env_values['DB_PORT1']);
|
|
|
|
unset($env_values['DB_DATABASE1']);
|
|
|
|
unset($env_values['DB_USERNAME1']);
|
|
|
|
unset($env_values['DB_PASSWORD1']);
|
|
|
|
}
|
2021-02-02 14:52:21 +01:00
|
|
|
|
2021-04-22 05:12:48 +02:00
|
|
|
try {
|
2020-11-24 11:45:27 +01:00
|
|
|
foreach ($env_values as $property => $value) {
|
|
|
|
$this->updateEnvironmentProperty($property, $value);
|
2020-03-18 10:40:15 +01: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 */
|
2020-10-28 11:10:49 +01:00
|
|
|
DB::purge('db-ninja-01');
|
2021-03-13 07:45:41 +01:00
|
|
|
|
2020-03-31 09:00:24 +02:00
|
|
|
/* Run migrations */
|
2021-04-22 05:12:48 +02:00
|
|
|
if (!config('ninja.disable_auto_update')) {
|
2021-04-08 00:42:23 +02:00
|
|
|
Artisan::call('optimize');
|
2021-04-22 05:12:48 +02:00
|
|
|
}
|
2021-04-08 00:42:23 +02:00
|
|
|
|
2020-03-31 09:00:24 +02:00
|
|
|
Artisan::call('migrate', ['--force' => true]);
|
|
|
|
Artisan::call('db:seed', ['--force' => true]);
|
2021-03-13 07:45:41 +01:00
|
|
|
|
2020-08-04 15:30:27 +02:00
|
|
|
Storage::disk('local')->delete('test.pdf');
|
|
|
|
|
2020-03-31 09:00:24 +02:00
|
|
|
/* Create the first account. */
|
|
|
|
if (Account::count() == 0) {
|
2020-10-09 15:12:50 +02:00
|
|
|
CreateAccount::dispatchNow($request->all());
|
2020-03-31 09:00:24 +02:00
|
|
|
}
|
|
|
|
|
2020-10-26 20:10:04 +01:00
|
|
|
VersionCheck::dispatchNow();
|
2020-11-24 11:52:01 +01:00
|
|
|
|
2020-11-02 01:23:02 +01:00
|
|
|
$this->buildCache(true);
|
2020-10-26 20:10:04 +01:00
|
|
|
|
2020-03-31 09:00:24 +02: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());
|
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
|
|
|
/**
|
2020-12-17 11:10:47 +01:00
|
|
|
* Return status based on database check.
|
2020-03-26 22:43:44 +01:00
|
|
|
*
|
2020-10-14 13:40:00 +02:00
|
|
|
* @param CheckDatabaseRequest $request
|
2020-12-17 11:10:47 +01:00
|
|
|
* @return Application|ResponseFactory|JsonResponse|Response
|
2020-03-26 22:43:44 +01:00
|
|
|
*/
|
2020-12-17 11:10:47 +01:00
|
|
|
public function checkDB(CheckDatabaseRequest $request)
|
2020-03-26 04:23:57 +01:00
|
|
|
{
|
2020-11-24 11:52:01 +01:00
|
|
|
try {
|
|
|
|
$status = SystemHealth::dbCheck($request);
|
2020-03-26 22:43:44 +01:00
|
|
|
|
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) {
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog(['message' => $e->getMessage(), 'action' => 'SetupController::checkDB()']);
|
2020-03-26 22:43:44 +01:00
|
|
|
|
2020-11-24 11:52:01 +01:00
|
|
|
return response()->json(['message' => $e->getMessage()], 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.
|
|
|
|
*
|
2020-10-09 15:12:50 +02:00
|
|
|
* @param CheckMailRequest $request
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Application|ResponseFactory|JsonResponse|Response
|
2020-03-26 22:43:44 +01:00
|
|
|
*/
|
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-12-08 15:04:07 +01:00
|
|
|
$response = SystemHealth::testMailServer($request);
|
2020-03-26 22:43:44 +01:00
|
|
|
|
2020-12-08 15:04:07 +01:00
|
|
|
if ($response['success']) {
|
2020-03-29 14:22:14 +02:00
|
|
|
return response([], 200);
|
2020-03-30 14:30:32 +02:00
|
|
|
} else {
|
2020-12-08 15:04:07 +01:00
|
|
|
return response()->json(['message' => $response['message']], 400);
|
2020-03-30 14:30:32 +02:00
|
|
|
}
|
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()']);
|
2020-03-31 09:00:24 +02:00
|
|
|
|
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
|
|
|
|
2020-07-02 13:58:39 +02:00
|
|
|
public function checkPdf(Request $request)
|
|
|
|
{
|
|
|
|
try {
|
2021-03-07 07:27:44 +01:00
|
|
|
if (config('ninja.phantomjs_pdf_generation')) {
|
2020-08-04 15:30:27 +02:00
|
|
|
return $this->testPhantom();
|
|
|
|
}
|
|
|
|
|
2020-12-25 14:15:57 +01:00
|
|
|
$pdf = new Snappdf();
|
2020-07-02 13:58:39 +02:00
|
|
|
|
2020-12-25 14:15:57 +01:00
|
|
|
if (config('ninja.snappdf_chromium_path')) {
|
|
|
|
$pdf->setChromiumPath(config('ninja.snappdf_chromium_path'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$pdf = $pdf
|
2020-12-21 16:32:04 +01:00
|
|
|
->setHtml('GENERATING PDFs WORKS! Thank you for using Invoice Ninja!')
|
|
|
|
->generate();
|
2020-12-16 12:51:41 +01:00
|
|
|
|
2021-01-04 13:36:47 +01:00
|
|
|
Storage::disk(config('filesystems.default'))->put('test.pdf', $pdf);
|
|
|
|
Storage::disk('local')->put('test.pdf', $pdf);
|
2020-12-16 12:51:41 +01:00
|
|
|
|
2021-01-04 13:36:47 +01:00
|
|
|
return response(['url' => Storage::disk('local')->url('test.pdf')], 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
|
|
|
|
2020-08-04 15:30:27 +02:00
|
|
|
private function testPhantom()
|
|
|
|
{
|
|
|
|
try {
|
2021-03-07 07:27:44 +01:00
|
|
|
$key = config('ninja.phantomjs_pdf_generation');
|
2020-08-04 15:30:27 +02:00
|
|
|
$url = 'https://www.invoiceninja.org/';
|
|
|
|
|
|
|
|
$phantom_url = "https://phantomjscloud.com/api/browser/v2/{$key}/?request=%7Burl:%22{$url}%22,renderType:%22pdf%22%7D";
|
2020-10-28 11:10:49 +01:00
|
|
|
$pdf = CurlUtils::get($phantom_url);
|
2020-08-04 15:30:27 +02:00
|
|
|
|
|
|
|
Storage::disk(config('filesystems.default'))->put('test.pdf', $pdf);
|
|
|
|
Storage::disk('local')->put('test.pdf', $pdf);
|
|
|
|
|
|
|
|
return response(['url' => Storage::disk('local')->url('test.pdf')], 200);
|
2020-10-28 11:10:49 +01:00
|
|
|
} catch (Exception $e) {
|
2020-09-06 04:23:04 +02:00
|
|
|
return response([], 500);
|
2020-08-04 15:30:27 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 12:01:00 +02:00
|
|
|
|
|
|
|
public function update()
|
|
|
|
{
|
|
|
|
|
2021-04-22 16:03:21 +02:00
|
|
|
// if( Ninja::isNinja() || !request()->has('secret') || (request()->input('secret') != config('ninja.update_secret')) )
|
|
|
|
if(!request()->has('secret') || (request()->input('secret') != config('ninja.update_secret')) )
|
2021-04-08 12:01:00 +02:00
|
|
|
return redirect('/');
|
|
|
|
|
|
|
|
$cacheCompiled = base_path('bootstrap/cache/compiled.php');
|
2021-04-22 05:12:48 +02:00
|
|
|
if (file_exists($cacheCompiled)) {
|
|
|
|
unlink ($cacheCompiled);
|
|
|
|
}
|
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-08 12:01:00 +02:00
|
|
|
|
|
|
|
Artisan::call('clear-compiled');
|
|
|
|
Artisan::call('cache:clear');
|
|
|
|
Artisan::call('debugbar:clear');
|
|
|
|
Artisan::call('route:clear');
|
|
|
|
Artisan::call('view:clear');
|
|
|
|
Artisan::call('config:clear');
|
|
|
|
Cache::flush();
|
|
|
|
Artisan::call('migrate', ['--force' => true]);
|
|
|
|
Artisan::call('db:seed', ['--force' => true]);
|
|
|
|
|
2021-04-22 15:32:34 +02:00
|
|
|
$this->buildCache(true);
|
|
|
|
|
|
|
|
return redirect('/');
|
2021-04-08 12:01:00 +02:00
|
|
|
|
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|