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

373 lines
11 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
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. 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 LimitIterator;
use SplFileObject;
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;
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
*/
2024-07-10 10:09:42 +02:00
public static function check($check_database = true, $check_file_system = 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'),
2021-05-13 00:13:33 +02:00
'pdf_engine' => (string) self::getPdfEngine(),
'queue' => (string) config('queue.default'),
'queue_data' => self::checkQueueData(),
'jobs_pending' => 0, // TODO for backwards compatibility, remove once Flutter AP is updated
2021-10-19 12:04:58 +02:00
'trailing_slash' => (bool) self::checkUrlState(),
2024-07-10 10:09:42 +02:00
'file_permissions' => (string) ($check_file_system ? self::checkFileSystem() : ''),
2022-10-27 02:59:14 +02:00
'exchange_rate_api_not_configured' => (bool)self::checkCurrencySanity(),
2024-02-28 11:15:43 +01:00
'api_version' => (string) config('ninja.app_version'),
2024-06-28 23:42:45 +02:00
'is_docker' => (bool) config('ninja.is_docker'),
'pending_migrations' => (bool) ($check_file_system ? self::checkPendingMigrations() : false),
];
}
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.
}
2024-02-13 05:25:18 +01:00
$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;
}
private static function checkQueueData()
2022-05-26 04:00:53 +02:00
{
$pending = 0;
$failed = 0;
$last_error = '';
2022-05-26 04:00:53 +02:00
try {
$pending = DB::table('jobs')->count();
$failed = DB::table('failed_jobs')->count();
if ($failed > 0) {
$failed_job = DB::table('failed_jobs')->latest('failed_at')->first();
$last_error = $failed_job->exception;
}
} catch (\Exception $e) {
2022-05-26 04:00:53 +02:00
}
return [
'failed' => $failed,
'pending' => $pending,
'last_error' => $last_error,
];
2022-05-26 04:00:53 +02:00
}
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;
}
public static function checkPendingMigrations()
{
$run_count = DB::table('migrations')->count();
$directory = base_path('database/migrations');
$iterator = new \FilesystemIterator($directory);
$total_count = iterator_count($iterator) - 1;
return $run_count != $total_count;
}
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()
{
2024-07-14 13:28:54 +02: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
{
2024-04-08 00:51:33 +02:00
$result = false;
2020-06-28 12:28:35 +02:00
try {
2024-04-08 00:51:33 +02:00
$result = DB::connection()->getPdo();
2020-06-28 12:28:35 +02:00
$result = true;
2024-04-08 00:51:33 +02:00
$result = DB::connection()->getDatabaseName() && strlen(DB::connection()->getDatabaseName()) > 1;
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
}
2024-01-14 05:05:00 +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');
}
public static function lastError()
{
$log_file = new SplFileObject(sprintf('%s/laravel.log', base_path('storage/logs')));
$log_file->seek(PHP_INT_MAX);
$last_line = $log_file->key();
$lines = new LimitIterator($log_file, max(0, $last_line - 500), $last_line);
$log_lines = iterator_to_array($lines);
$last_error = '';
foreach ($lines as $line) {
// Match the main error, ie. [2024-07-10 12:23:07] production.ERROR: ...
if (substr($line, 0, 2) === '[2') {
$last_error = $line;
}
}
return $last_error;
}
2019-07-05 00:36:40 +02:00
}