2019-07-05 00:36:40 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-03-30 14:30:32 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-07-05 00:36:40 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-07-05 00:36:40 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Utils;
|
|
|
|
|
2020-03-28 04:46:50 +01:00
|
|
|
use App\Http\Requests\Setup\CheckDatabaseRequest;
|
|
|
|
use App\Http\Requests\Setup\CheckMailRequest;
|
2019-07-05 00:36:40 +02:00
|
|
|
use App\Libraries\MultiDB;
|
2020-03-18 10:40:15 +01:00
|
|
|
use App\Mail\TestMailServer;
|
2020-03-30 14:30:32 +02:00
|
|
|
use Illuminate\Support\Arr;
|
2019-07-05 00:36:40 +02:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-03-18 10:40:15 +01:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2019-07-05 00:36:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class SystemHealth.
|
|
|
|
*/
|
|
|
|
class SystemHealth
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
private static $extensions = [
|
|
|
|
'mysqli',
|
|
|
|
'gd',
|
|
|
|
'curl',
|
|
|
|
'zip',
|
2020-03-24 10:15:30 +01:00
|
|
|
'gmp',
|
|
|
|
'openssl',
|
|
|
|
'mbstring',
|
2020-03-30 14:30:32 +02:00
|
|
|
'xml',
|
2020-04-06 14:32:27 +02:00
|
|
|
'bcmath'
|
2019-12-30 22:59:12 +01:00
|
|
|
];
|
2019-07-05 00:36:40 +02:00
|
|
|
|
2019-07-09 11:13:33 +02:00
|
|
|
private static $php_version = 7.3;
|
2019-07-05 00:36:40 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-03-30 14:30:32 +02:00
|
|
|
* Check loaded extensions / PHP version / DB Connections.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
|
|
|
* @return array Result set of checks
|
|
|
|
*/
|
|
|
|
public static function check() : array
|
|
|
|
{
|
2020-06-28 12:28:35 +02:00
|
|
|
$system_health = "true";
|
2019-07-05 00:36:40 +02:00
|
|
|
|
2020-03-30 14:30:32 +02:00
|
|
|
if (in_array(false, Arr::dot(self::extensions()))) {
|
2020-06-28 12:28:35 +02:00
|
|
|
$system_health = "false";
|
2019-12-30 22:59:12 +01:00
|
|
|
} elseif (phpversion() < self::$php_version) {
|
2020-06-28 12:28:35 +02:00
|
|
|
$system_health = "false";
|
|
|
|
} elseif(!self::simpleDbCheck()) {
|
2020-06-28 12:32:18 +02:00
|
|
|
info("db fails");
|
2020-06-28 12:28:35 +02:00
|
|
|
$system_health = "false";
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-07-05 00:36:40 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return [
|
2020-06-28 12:28:35 +02:00
|
|
|
'system_health' => $system_health,
|
2019-12-30 22:59:12 +01:00
|
|
|
'extensions' => self::extensions(),
|
2020-03-30 14:30:32 +02:00
|
|
|
'php_version' => [
|
|
|
|
'minimum_php_version' => self::$php_version,
|
|
|
|
'current_php_version' => phpversion(),
|
|
|
|
'is_okay' => version_compare(phpversion(), self::$php_version, '>='),
|
|
|
|
],
|
|
|
|
'env_writable' => self::checkEnvWritable(),
|
2020-03-26 04:23:57 +01:00
|
|
|
//'mail' => self::testMailServer(),
|
2019-12-30 22:59:12 +01:00
|
|
|
];
|
|
|
|
}
|
2019-07-05 00:36:40 +02:00
|
|
|
|
2020-06-28 12:28:35 +02:00
|
|
|
private static function simpleDbCheck() :bool
|
|
|
|
{
|
|
|
|
$result = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$pdo = DB::connection()->getPdo();
|
|
|
|
$result = true;
|
|
|
|
}
|
|
|
|
catch (\Exception $e) {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
private static function extensions() :array
|
|
|
|
{
|
|
|
|
$loaded_extensions = [];
|
2019-07-05 00:36:40 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$extensions as $extension) {
|
|
|
|
$loaded_extensions[] = [$extension => extension_loaded($extension)];
|
|
|
|
}
|
2019-07-05 00:36:40 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $loaded_extensions;
|
|
|
|
}
|
2019-07-05 00:36:40 +02:00
|
|
|
|
2020-03-28 04:46:50 +01:00
|
|
|
public static function dbCheck($request = null): array
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2020-03-28 04:46:50 +01:00
|
|
|
$result = ['success' => false];
|
2019-07-05 00:36:40 +02:00
|
|
|
|
2020-03-28 04:46:50 +01:00
|
|
|
if ($request && $request instanceof CheckDatabaseRequest) {
|
|
|
|
config(['database.connections.db-ninja-01.host'=> $request->input('host')]);
|
|
|
|
config(['database.connections.db-ninja-01.database'=> $request->input('database')]);
|
|
|
|
config(['database.connections.db-ninja-01.username'=> $request->input('username')]);
|
|
|
|
config(['database.connections.db-ninja-01.password'=> $request->input('password')]);
|
|
|
|
config(['database.default' => 'db-ninja-01']);
|
|
|
|
|
|
|
|
DB::purge('db-ninja-01');
|
|
|
|
}
|
2019-07-05 00:36:40 +02:00
|
|
|
|
2020-03-28 04:46:50 +01:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
|
|
|
try {
|
|
|
|
$pdo = DB::connection()->getPdo();
|
2020-03-30 14:30:32 +02:00
|
|
|
$result[] = [DB::connection()->getDatabaseName() => true];
|
2020-03-28 04:46:50 +01:00
|
|
|
$result['success'] = true;
|
|
|
|
} catch (\Exception $e) {
|
2020-03-30 14:30:32 +02:00
|
|
|
$result[] = [config('database.connections.'.config('database.default').'.database') => false];
|
2020-03-28 04:46:50 +01:00
|
|
|
$result['success'] = false;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach (MultiDB::$dbs as $db) {
|
2019-07-05 00:36:40 +02:00
|
|
|
MultiDB::setDB($db);
|
|
|
|
|
2020-03-28 04:46:50 +01:00
|
|
|
try {
|
|
|
|
$pdo = DB::connection()->getPdo();
|
2020-03-30 14:30:32 +02:00
|
|
|
$result[] = [DB::connection()->getDatabaseName() => true];
|
2020-03-28 04:46:50 +01:00
|
|
|
$result['success'] = true;
|
|
|
|
} catch (\Exception $e) {
|
2020-03-30 14:30:32 +02:00
|
|
|
$result[] = [config('database.connections.'.config('database.default').'.database') => false];
|
2020-03-28 04:46:50 +01:00
|
|
|
$result['success'] = false;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-07-05 00:36:40 +02:00
|
|
|
}
|
|
|
|
}
|
2020-01-23 21:35:00 +01:00
|
|
|
|
|
|
|
return $result;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2020-03-18 10:40:15 +01:00
|
|
|
|
|
|
|
private static function checkDbConnection()
|
|
|
|
{
|
|
|
|
return DB::connection()->getPdo();
|
|
|
|
}
|
|
|
|
|
2020-03-28 04:46:50 +01:00
|
|
|
public static function testMailServer($request = null)
|
2020-03-21 06:37:30 +01:00
|
|
|
{
|
2020-03-28 04:46:50 +01:00
|
|
|
if ($request && $request instanceof CheckMailRequest) {
|
|
|
|
config(['mail.driver' => $request->input('driver')]);
|
|
|
|
config(['mail.host' => $request->input('host')]);
|
|
|
|
config(['mail.port' => $request->input('port')]);
|
|
|
|
config(['mail.from.address' => $request->input('from_address')]);
|
|
|
|
config(['mail.from.name' => $request->input('from_name')]);
|
|
|
|
config(['mail.encryption' => $request->input('encryption')]);
|
|
|
|
config(['mail.username' => $request->input('username')]);
|
|
|
|
config(['mail.password' => $request->input('password')]);
|
|
|
|
}
|
|
|
|
|
2020-03-18 10:40:15 +01:00
|
|
|
try {
|
|
|
|
Mail::to(config('mail.from.address'))
|
|
|
|
->send(new TestMailServer('Email Server Works!', config('mail.from.address')));
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (\Exception $e) {
|
2020-03-18 10:40:15 +01:00
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
|
2020-03-31 09:00:24 +02:00
|
|
|
/*
|
|
|
|
* 'message' => 'count(): Parameter must be an array or an object that implements Countable',
|
|
|
|
* 'action' => 'SetupController::checkMail()',
|
|
|
|
*
|
|
|
|
* count(Mail::failures())
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (Mail::failures() > 0) {
|
2020-03-18 10:40:15 +01:00
|
|
|
return Mail::failures();
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-18 10:40:15 +01:00
|
|
|
|
2020-03-30 14:30:32 +02:00
|
|
|
return response()->json(['message'=>'Success'], 200);
|
2020-03-18 10:40:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static function checkEnvWritable()
|
|
|
|
{
|
2020-03-22 21:45:16 +01:00
|
|
|
return is_writable(base_path().'/.env');
|
2020-03-18 10:40:15 +01:00
|
|
|
}
|
2019-07-05 00:36:40 +02:00
|
|
|
}
|