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

248 lines
7.2 KiB
PHP
Raw Normal View History

2019-07-05 00:36:40 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2019-07-05 00:36:40 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @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;
use App\Http\Requests\Setup\CheckDatabaseRequest;
use App\Http\Requests\Setup\CheckMailRequest;
2019-07-05 00:36:40 +02:00
use App\Libraries\MultiDB;
use App\Mail\TestMailServer;
2020-10-28 11:10:49 +01:00
use Exception;
use Illuminate\Support\Arr;
2019-07-05 00:36:40 +02:00
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
2019-07-05 00:36:40 +02:00
/**
* Class SystemHealth.
*/
class SystemHealth
{
private static $extensions = [
'mysqli',
'gd',
'curl',
'zip',
'gmp',
'openssl',
'mbstring',
'xml',
'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-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
/**
* Check loaded extensions / PHP version / DB Connections.
*
2020-10-28 11:10:49 +01:00
* @param bool $check_database
* @return array Result set of checks
*/
2020-07-06 14:27:27 +02:00
public static function check($check_database = true) : array
{
2020-06-30 14:28:14 +02:00
$system_health = true;
2019-07-05 00:36:40 +02:00
if (in_array(false, Arr::dot(self::extensions()))) {
2020-06-30 14:28:14 +02:00
$system_health = false;
}
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-06 14:27:27 +02:00
if (! self::simpleDbCheck() && $check_database) {
info('db fails');
2020-06-30 14:28:14 +02:00
$system_health = false;
}
2019-07-05 00:36:40 +02:00
return [
2020-06-28 12:28:35 +02:00
'system_health' => $system_health,
'extensions' => self::extensions(),
'php_version' => [
'minimum_php_version' => (string) self::$php_version,
'current_php_version' => phpversion(),
2020-09-23 06:11:34 +02:00
'current_php_cli_version' => (string) self::checkPhpCli(),
'is_okay' => version_compare(phpversion(), self::$php_version, '>='),
],
'env_writable' => self::checkEnvWritable(),
//'mail' => self::testMailServer(),
2020-07-06 14:27:27 +02:00
'simple_db_check' => (bool) self::simpleDbCheck(),
2020-09-14 00:25:09 +02:00
'npm_status' => self::checkNpm(),
'node_status' => self::checkNode(),
2020-11-26 22:05:30 +01:00
'cache_enabled' => self::checkConfigCache(),
'phantom_enabled' => (bool) config('ninja.phantomjs_pdf_generation'),
];
}
2019-07-05 00:36:40 +02:00
2020-11-26 22:05:30 +01:00
public static function checkConfigCache()
{
if(env('APP_URL'))
return false;
return true;
}
2020-07-21 03:11:35 +02:00
public static function checkNode()
{
try {
exec('node -v', $foo, $exitCode);
if ($exitCode === 0) {
return empty($foo[0]) ? 'Found node, but no version information' : $foo[0];
2020-07-21 03:11:35 +02:00
}
2020-10-28 11:10:49 +01:00
} catch (Exception $e) {
2020-08-19 05:08:25 +02:00
return false;
2020-07-21 03:11:35 +02:00
}
}
public static function checkNpm()
{
try {
exec('npm -v', $foo, $exitCode);
if ($exitCode === 0) {
return empty($foo[0]) ? 'Found npm, but no version information' : $foo[0];
}
2020-10-28 11:10:49 +01:00
} catch (Exception $e) {
2020-08-19 05:08:25 +02:00
return false;
2020-07-21 03:11:35 +02:00
}
}
2020-06-28 12:28:35 +02:00
private static function simpleDbCheck() :bool
{
$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;
}
}
private static function extensions() :array
{
$loaded_extensions = [];
2019-07-05 00:36:40 +02:00
foreach (self::$extensions as $extension) {
$loaded_extensions[] = [$extension => extension_loaded($extension)];
}
2019-07-05 00:36:40 +02:00
return $loaded_extensions;
}
2019-07-05 00:36:40 +02:00
public static function dbCheck($request = null): array
{
$result = ['success' => false];
2019-07-05 00:36:40 +02: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
if (! config('ninja.db.multi_db_enabled')) {
try {
$pdo = DB::connection()->getPdo();
$result[] = [DB::connection()->getDatabaseName() => true];
$result['success'] = true;
2020-10-28 11:10:49 +01:00
} catch (Exception $e) {
$result[] = [config('database.connections.'.config('database.default').'.database') => false];
$result['success'] = false;
$result['message'] = $e->getMessage();
}
} else {
foreach (MultiDB::$dbs as $db) {
2019-07-05 00:36:40 +02:00
MultiDB::setDB($db);
try {
$pdo = DB::connection()->getPdo();
$result[] = [DB::connection()->getDatabaseName() => true];
$result['success'] = true;
2020-10-28 11:10:49 +01:00
} catch (Exception $e) {
$result[] = [config('database.connections.'.config('database.default').'.database') => false];
$result['success'] = false;
}
2019-07-05 00:36:40 +02:00
}
}
return $result;
}
private static function checkDbConnection()
{
return DB::connection()->getPdo();
}
public static function testMailServer($request = null)
{
2020-11-24 11:12:44 +01:00
if ($request->driver == 'log') {
return [];
}
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')]);
}
try {
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) {
return $e->getMessage();
}
/*
* 'message' => 'count(): Parameter must be an array or an object that implements Countable',
* 'action' => 'SetupController::checkMail()',
*
* count(Mail::failures())
*/
if (Mail::failures() > 0) {
return Mail::failures();
}
2020-11-24 11:12:44 +01:00
return response()->json(['message' => 'Success'], 200);
}
private static function checkEnvWritable()
{
return is_writable(base_path().'/.env');
}
2019-07-05 00:36:40 +02:00
}