1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Utils/SystemHealth.php

323 lines
9.5 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
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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',
2023-02-27 12:43:44 +01:00
'iconv',
];
2019-07-05 00:36:40 +02:00
private static $php_version = 8.1;
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
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(),
'current_php_cli_version' => (string) self::checkPhpCli(),
'is_okay' => version_compare(phpversion(), self::$php_version, '>='),
2022-08-05 02:38:40 +02:00
'memory_limit' => ini_get('memory_limit'),
],
'env_writable' => self::checkEnvWritable(),
2022-07-18 02:26:42 +02:00
'simple_db_check' => self::simpleDbCheck(),
2020-11-26 22:05:30 +01:00
'cache_enabled' => self::checkConfigCache(),
'phantom_enabled' => (bool) config('ninja.phantomjs_pdf_generation'),
'exec' => (bool) self::checkExecWorks(),
'open_basedir' => (bool) self::checkOpenBaseDir(),
'mail_mailer' => (string) self::checkMailMailer(),
'flutter_renderer' => (string) config('ninja.flutter_canvas_kit'),
2022-05-26 04:00:53 +02:00
'jobs_pending' => (int) self::checkQueueSize(),
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(),
'file_permissions' => (string) self::checkFileSystem(),
2022-10-27 02:59:14 +02:00
'exchange_rate_api_not_configured' => (bool)self::checkCurrencySanity(),
];
}
2019-07-05 00:36:40 +02:00
2022-10-27 02:59:14 +02:00
private static function checkCurrencySanity()
{
2023-02-16 02:36:09 +01:00
if (!self::simpleDbCheck()) {
return true;
}
2022-10-27 02:59:14 +02:00
2023-02-16 02:36:09 +01:00
if (strlen(config('ninja.currency_converter_api_key')) == 0) {
try {
$cs = DB::table('clients')
->select('settings->currency_id as id')
->get();
} catch(\Exception $e) {
return true; //fresh installs, there may be no DB connection, nor migrations could have run yet.
}
$currency_count = $cs->unique('id')->filter(function ($value) {
return !is_null($value->id);
})->count();
2022-10-27 02:59:14 +02:00
2023-02-16 02:36:09 +01:00
if ($currency_count > 1) {
2022-10-27 02:59:14 +02:00
return true;
2023-02-16 02:36:09 +01:00
}
2022-10-27 02:59:14 +02:00
}
return false;
}
2022-05-26 04:00:53 +02:00
private static function checkQueueSize()
{
$count = 0;
try {
2022-05-26 04:00:53 +02:00
$count = Queue::size();
} catch (\Exception $e) {
2022-05-26 04:00:53 +02:00
}
return $count;
}
2022-04-26 06:00:42 +02:00
public static function checkFileSystem()
{
$directoryIterator = new \RecursiveDirectoryIterator(base_path(), \RecursiveDirectoryIterator::SKIP_DOTS);
foreach (new \RecursiveIteratorIterator($directoryIterator) as $file) {
if (strpos($file->getPathname(), '.git') !== false) {
2022-04-26 06:00:42 +02:00
continue;
}
2022-04-26 06:00:42 +02:00
//nlog($file->getPathname());
if ($file->isFile() && ! $file->isWritable()) {
return "{$file->getFileName()} is not writable";
}
}
return 'Ok';
}
2021-10-19 12:04:58 +02:00
public static function checkUrlState()
{
if (env('APP_URL') && substr(env('APP_URL'), -1) == '/') {
2021-10-19 12:04:58 +02:00
return true;
}
2021-10-19 12:04:58 +02:00
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 {
2021-05-13 00:13:33 +02:00
return 'SnapPDF PDF Generator';
}
2021-05-13 00:13:33 +02:00
}
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()
{
2023-02-16 02:36:09 +01:00
if (!function_exists('exec')) {
return "Unable to check CLI version";
2023-02-16 02:36:09 +01:00
}
2020-09-23 06:11:34 +02:00
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
{
2022-02-20 11:09:20 +01:00
$loaded_extensions = null;
$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')) {
2022-03-15 10:52:36 +01:00
config(['database.connections.mysql.host' => $request->input('db_host')]);
config(['database.connections.mysql.port' => $request->input('db_port')]);
config(['database.connections.mysql.database' => $request->input('db_database')]);
config(['database.connections.mysql.username' => $request->input('db_username')]);
config(['database.connections.mysql.password' => $request->input('db_password')]);
config(['database.default' => 'mysql']);
2022-03-17 22:36:29 +01:00
DB::purge('mysql');
}
2019-07-05 00:36:40 +02:00
if (! config('ninja.db.multi_db_enabled')) {
try {
$pdo = DB::connection()->getPdo();
2022-07-18 02:26:42 +02:00
$x = DB::connection()->getDatabaseName();
// nlog($pdo);
// nlog($x);
$result['success'] = true;
2020-10-28 11:10:49 +01:00
} catch (Exception $e) {
2022-07-18 02:26:42 +02:00
// $x = [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();
2022-07-18 02:26:42 +02:00
$x = DB::connection()->getDatabaseName();
$result['success'] = true;
2020-10-28 11:10:49 +01:00
} catch (Exception $e) {
2023-02-16 02:36:09 +01:00
// $x = [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;
}
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()
{
return is_writable(base_path().'/.env');
}
2019-07-05 00:36:40 +02:00
}