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
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-07-05 00:36:40 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Utils;
|
|
|
|
|
|
|
|
use App\Libraries\MultiDB;
|
2020-03-18 10:40:15 +01:00
|
|
|
use App\Mail\TestMailServer;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Exception;
|
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-12-18 22:29:32 +01:00
|
|
|
// 'gmp',
|
2020-03-24 10:15:30 +01:00
|
|
|
'openssl',
|
|
|
|
'mbstring',
|
2020-03-30 14:30:32 +02:00
|
|
|
'xml',
|
2020-09-06 11:38:10 +02:00
|
|
|
'bcmath',
|
2020-10-04 23:37:12 +02:00
|
|
|
'mysqlnd',
|
2020-10-12 23:27:42 +02:00
|
|
|
//'intl', //todo double check whether we need this for email dns validation
|
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
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param bool $check_database
|
2019-12-30 22:59:12 +01:00
|
|
|
* @return array Result set of checks
|
|
|
|
*/
|
2021-01-04 13:36:47 +01:00
|
|
|
public static function check($check_database = true): array
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2020-06-30 14:28:14 +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-30 14:28:14 +02:00
|
|
|
$system_health = false;
|
2020-07-27 16:55:26 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 14:27:27 +02:00
|
|
|
if (phpversion() < self::$php_version) {
|
2020-06-30 14:28:14 +02:00
|
|
|
$system_health = false;
|
2020-07-27 16:55:26 +02:00
|
|
|
}
|
2020-07-06 14:27:27 +02:00
|
|
|
|
2021-01-04 13:36:47 +01:00
|
|
|
if (!self::simpleDbCheck() && $check_database) {
|
2020-09-06 11:38:10 +02:00
|
|
|
info('db fails');
|
2020-06-30 14:28:14 +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' => [
|
2021-01-04 13:36:47 +01:00
|
|
|
'minimum_php_version' => (string)self::$php_version,
|
2020-03-30 14:30:32 +02:00
|
|
|
'current_php_version' => phpversion(),
|
2021-01-04 13:36:47 +01:00
|
|
|
'current_php_cli_version' => (string)self::checkPhpCli(),
|
2020-03-30 14:30:32 +02:00
|
|
|
'is_okay' => version_compare(phpversion(), self::$php_version, '>='),
|
|
|
|
],
|
|
|
|
'env_writable' => self::checkEnvWritable(),
|
2020-03-26 04:23:57 +01:00
|
|
|
//'mail' => self::testMailServer(),
|
2021-01-04 13:36:47 +01:00
|
|
|
'simple_db_check' => (bool)self::simpleDbCheck(),
|
2020-11-26 22:05:30 +01:00
|
|
|
'cache_enabled' => self::checkConfigCache(),
|
2021-01-04 13:36:47 +01:00
|
|
|
'phantom_enabled' => (bool)config('ninja.phantomjs_pdf_generation'),
|
|
|
|
'exec' => (bool)self::checkExecWorks(),
|
|
|
|
'open_basedir' => (bool)self::checkOpenBaseDir(),
|
2019-12-30 22:59:12 +01:00
|
|
|
];
|
|
|
|
}
|
2019-07-05 00:36:40 +02:00
|
|
|
|
2020-12-12 22:06:47 +01:00
|
|
|
public static function checkOpenBaseDir()
|
|
|
|
{
|
2020-12-16 12:52:40 +01:00
|
|
|
if (strlen(ini_get('open_basedir') == 0)) {
|
2020-12-12 22:06:47 +01:00
|
|
|
return true;
|
2020-12-16 12:52:40 +01:00
|
|
|
}
|
2020-12-12 22:06:47 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function checkExecWorks()
|
|
|
|
{
|
2020-12-16 12:52:40 +01:00
|
|
|
if (function_exists('exec')) {
|
2020-12-12 22:06:47 +01:00
|
|
|
return true;
|
2020-12-16 12:52:40 +01:00
|
|
|
}
|
2020-12-12 22:06:47 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-26 22:05:30 +01:00
|
|
|
public static function checkConfigCache()
|
|
|
|
{
|
2020-11-27 12:08:42 +01:00
|
|
|
if (env('APP_URL')) {
|
2020-11-26 22:05:30 +01:00
|
|
|
return false;
|
2020-11-27 12:08:42 +01:00
|
|
|
}
|
2020-11-26 22:05:30 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:36:47 +01:00
|
|
|
private static function simpleDbCheck(): bool
|
2020-06-28 12:28:35 +02:00
|
|
|
{
|
|
|
|
$result = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$pdo = DB::connection()->getPdo();
|
|
|
|
$result = true;
|
2020-10-28 11:10:49 +01:00
|
|
|
} catch (Exception $e) {
|
2020-06-28 12:28:35 +02:00
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2020-09-23 06:11:34 +02:00
|
|
|
private static function checkPhpCli()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
exec('php -v', $foo, $exitCode);
|
|
|
|
|
|
|
|
if ($exitCode === 0) {
|
|
|
|
return empty($foo[0]) ? 'Found php cli, but no version information' : $foo[0];
|
|
|
|
}
|
2020-10-28 11:10:49 +01:00
|
|
|
} catch (Exception $e) {
|
2020-09-23 06:11:34 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:36:47 +01:00
|
|
|
private static function extensions(): array
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
|
|
|
$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-12-08 15:04:07 +01:00
|
|
|
if ($request) {
|
2021-01-04 13:36:47 +01:00
|
|
|
config(['database.connections.db-ninja-01.host' => $request->input('db_host')]);
|
|
|
|
config(['database.connections.db-ninja-01.port' => $request->input('db_port')]);
|
|
|
|
config(['database.connections.db-ninja-01.database' => $request->input('db_database')]);
|
|
|
|
config(['database.connections.db-ninja-01.username' => $request->input('db_username')]);
|
|
|
|
config(['database.connections.db-ninja-01.password' => $request->input('db_password')]);
|
2020-03-28 04:46:50 +01:00
|
|
|
config(['database.default' => 'db-ninja-01']);
|
|
|
|
|
|
|
|
DB::purge('db-ninja-01');
|
|
|
|
}
|
2019-07-05 00:36:40 +02:00
|
|
|
|
2021-01-04 13:36:47 +01:00
|
|
|
if (!config('ninja.db.multi_db_enabled')) {
|
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;
|
2020-10-28 11:10:49 +01:00
|
|
|
} catch (Exception $e) {
|
2021-01-04 13:36:47 +01:00
|
|
|
$result[] = [config('database.connections.' . config('database.default') . '.database') => false];
|
2020-03-28 04:46:50 +01:00
|
|
|
$result['success'] = false;
|
2020-10-14 13:40:00 +02:00
|
|
|
$result['message'] = $e->getMessage();
|
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;
|
2020-10-28 11:10:49 +01:00
|
|
|
} catch (Exception $e) {
|
2021-01-04 13:36:47 +01:00
|
|
|
$result[] = [config('database.connections.' . config('database.default') . '.database') => false];
|
2020-03-28 04:46:50 +01:00
|
|
|
$result['success'] = false;
|
2020-12-08 15:04:07 +01:00
|
|
|
$result['message'] = $e->getMessage();
|
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-11-24 11:12:44 +01:00
|
|
|
if ($request->driver == 'log') {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-12-08 15:04:07 +01:00
|
|
|
if ($request) {
|
2020-12-08 14:29:15 +01:00
|
|
|
config(['mail.driver' => $request->input('mail_driver')]);
|
|
|
|
config(['mail.host' => $request->input('mail_host')]);
|
|
|
|
config(['mail.port' => $request->input('mail_port')]);
|
|
|
|
config(['mail.from.address' => $request->input('mail_address')]);
|
|
|
|
config(['mail.from.name' => $request->input('mail_name')]);
|
2020-03-28 04:46:50 +01:00
|
|
|
config(['mail.encryption' => $request->input('encryption')]);
|
2020-12-08 14:29:15 +01:00
|
|
|
config(['mail.username' => $request->input('mail_username')]);
|
|
|
|
config(['mail.password' => $request->input('mail_password')]);
|
2020-03-28 04:46:50 +01:00
|
|
|
}
|
|
|
|
|
2020-03-18 10:40:15 +01:00
|
|
|
try {
|
2020-12-03 14:10:40 +01:00
|
|
|
Mail::to(config('mail.from.address'))->send(new TestMailServer('Email Server Works!', config('mail.from.address')));
|
2020-10-28 11:10:49 +01:00
|
|
|
} catch (Exception $e) {
|
2020-12-08 15:04:07 +01:00
|
|
|
return ['success' => false, 'message' => $e->getMessage()];
|
2020-03-18 10:40:15 +01:00
|
|
|
}
|
|
|
|
|
2020-12-08 15:04:07 +01:00
|
|
|
return ['success' => true];
|
2020-03-18 10:40:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static function checkEnvWritable()
|
|
|
|
{
|
2021-01-04 13:36:47 +01:00
|
|
|
return is_writable(base_path() . '/.env');
|
2020-03-18 10:40:15 +01:00
|
|
|
}
|
2019-07-05 00:36:40 +02:00
|
|
|
}
|