1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Utils/SystemHealth.php

257 lines
7.7 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) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-07-05 00:36:40 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-07-05 00:36:40 +02:00
*/
namespace App\Utils;
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;
2021-05-09 13:30:31 +02:00
use Illuminate\Support\Facades\Queue;
2019-07-05 00:36:40 +02:00
/**
* Class SystemHealth.
*/
class SystemHealth
{
private static $extensions = [
'gd',
'curl',
'zip',
'openssl',
'mbstring',
'xml',
'bcmath',
];
2019-07-05 00:36:40 +02:00
2021-08-18 14:26:27 +02:00
private static $php_version = 7.4;
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
*/
2021-01-04 13:36:47 +01: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
2021-01-04 13:36:47 +01: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' => [
2021-01-04 13:36:47 +01:00
'minimum_php_version' => (string)self::$php_version,
'current_php_version' => phpversion(),
2021-01-04 13:36:47 +01:00
'current_php_cli_version' => (string)self::checkPhpCli(),
'is_okay' => version_compare(phpversion(), self::$php_version, '>='),
],
'env_writable' => self::checkEnvWritable(),
//'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(),
2021-03-19 13:37:57 +01:00
'mail_mailer' => (string)self::checkMailMailer(),
2021-04-01 11:33:50 +02:00
'flutter_renderer' => (string)config('ninja.flutter_canvas_kit'),
2021-05-09 13:30:31 +02:00
'jobs_pending' => (int) Queue::size(),
2021-05-13 00:13:33 +02:00
'pdf_engine' => (string) self::getPdfEngine(),
'queue' => (string) config('queue.default'),
2021-10-19 12:04:58 +02:00
'trailing_slash' => (bool) self::checkUrlState(),
];
}
2019-07-05 00:36:40 +02:00
2021-10-19 12:04:58 +02:00
public static function checkUrlState()
{
if (env('APP_URL') && substr(env('APP_URL'), -1) == '/')
return true;
return false;
}
2021-05-13 00:13:33 +02:00
public static function getPdfEngine()
{
if(config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja')
2021-05-13 00:13:33 +02:00
return 'Invoice Ninja Hosted PDF Generator';
elseif(config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom')
2021-05-13 00:13:33 +02:00
return 'Phantom JS Web Generator';
else
return 'SnapPDF PDF Generator';
}
2021-03-19 13:37:57 +01:00
public static function checkMailMailer()
{
return config('mail.default');
}
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
{
$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 && !config('ninja.preconfigured_install')) {
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')]);
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')) {
try {
$pdo = DB::connection()->getPdo();
$result[] = [DB::connection()->getDatabaseName() => true];
$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];
$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) {
2021-01-04 13:36:47 +01:00
$result[] = [config('database.connections.' . config('database.default') . '.database') => false];
$result['success'] = false;
2020-12-08 15:04:07 +01:00
$result['message'] = $e->getMessage();
}
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 [];
}
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')]);
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')]);
2021-09-02 09:51:54 +02:00
(new \Illuminate\Mail\MailServiceProvider(app()))->register();
}
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-12-08 15:04:07 +01:00
return ['success' => true];
}
private static function checkEnvWritable()
{
2021-01-04 13:36:47 +01:00
return is_writable(base_path() . '/.env');
}
2019-07-05 00:36:40 +02:00
}