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
|
|
|
|
*
|
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;
|
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;
|
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
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
private static $extensions = [
|
|
|
|
'gd',
|
|
|
|
'curl',
|
|
|
|
'zip',
|
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',
|
2023-02-27 12:43:44 +01:00
|
|
|
'iconv',
|
2019-12-30 22:59:12 +01:00
|
|
|
];
|
2019-07-05 00:36:40 +02:00
|
|
|
|
2022-07-17 12:58:02 +02:00
|
|
|
private static $php_version = 8.1;
|
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
|
|
|
|
2022-06-21 11:57:17 +02: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' => [
|
2022-06-21 11:57:17 +02:00
|
|
|
'minimum_php_version' => (string) self::$php_version,
|
2020-03-30 14:30:32 +02:00
|
|
|
'current_php_version' => phpversion(),
|
2022-06-21 11:57:17 +02:00
|
|
|
'current_php_cli_version' => (string) self::checkPhpCli(),
|
2020-03-30 14:30:32 +02:00
|
|
|
'is_okay' => version_compare(phpversion(), self::$php_version, '>='),
|
2022-08-05 02:38:40 +02:00
|
|
|
'memory_limit' => ini_get('memory_limit'),
|
2020-03-30 14:30:32 +02:00
|
|
|
],
|
|
|
|
'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(),
|
2022-06-21 11:57:17 +02:00
|
|
|
'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(),
|
2021-07-19 06:17:58 +02:00
|
|
|
'queue' => (string) config('queue.default'),
|
2021-10-19 12:04:58 +02:00
|
|
|
'trailing_slash' => (bool) self::checkUrlState(),
|
2022-06-21 11:57:17 +02:00
|
|
|
'file_permissions' => (string) 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'),
|
2019-12-30 22:59:12 +01:00
|
|
|
];
|
|
|
|
}
|
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()) {
|
2022-10-31 23:30:22 +01:00
|
|
|
return true;
|
2022-11-22 00:46:42 +01:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-26 04:00:53 +02:00
|
|
|
private static function checkQueueSize()
|
|
|
|
{
|
|
|
|
$count = 0;
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
try {
|
2022-05-26 04:00:53 +02:00
|
|
|
$count = Queue::size();
|
2022-06-21 11:57:17 +02:00
|
|
|
} 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) {
|
2022-06-21 11:57:17 +02:00
|
|
|
if (strpos($file->getPathname(), '.git') !== false) {
|
2022-04-26 06:00:42 +02:00
|
|
|
continue;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
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()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (env('APP_URL') && substr(env('APP_URL'), -1) == '/') {
|
2021-10-19 12:04:58 +02:00
|
|
|
return true;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-10-19 12:04:58 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-13 00:13:33 +02:00
|
|
|
public static function getPdfEngine()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
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';
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
2021-05-13 00:13:33 +02:00
|
|
|
return 'Phantom JS Web Generator';
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-05-13 00:13:33 +02:00
|
|
|
return 'SnapPDF PDF Generator';
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
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
|
|
|
{
|
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')) {
|
2022-08-31 11:08:01 +02:00
|
|
|
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
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2022-02-20 11:09:20 +01:00
|
|
|
$loaded_extensions = null;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
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
|
|
|
|
2022-06-21 11:57:17 +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']);
|
2020-03-28 04:46:50 +01:00
|
|
|
|
2022-03-17 22:36:29 +01:00
|
|
|
DB::purge('mysql');
|
2020-03-28 04:46:50 +01:00
|
|
|
}
|
2019-07-05 00:36:40 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2020-03-28 04:46:50 +01:00
|
|
|
try {
|
|
|
|
$pdo = DB::connection()->getPdo();
|
2022-07-18 02:26:42 +02:00
|
|
|
$x = DB::connection()->getDatabaseName();
|
2023-04-06 03:07:24 +02:00
|
|
|
// nlog($pdo);
|
|
|
|
// nlog($x);
|
2020-03-28 04:46:50 +01:00
|
|
|
$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];
|
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();
|
2022-07-18 02:26:42 +02:00
|
|
|
$x = DB::connection()->getDatabaseName();
|
2020-03-28 04:46:50 +01:00
|
|
|
$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];
|
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
|
|
|
|
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')]);
|
2021-09-02 09:51:54 +02:00
|
|
|
(new \Illuminate\Mail\MailServiceProvider(app()))->register();
|
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()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
return is_writable(base_path().'/.env');
|
2020-03-18 10:40:15 +01:00
|
|
|
}
|
2019-07-05 00:36:40 +02:00
|
|
|
}
|