1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Utils/SystemHealth.php
David Bomba ba75a44eb8
Laravel 7.x Shift (#40)
* Adopt Laravel coding style

The Laravel framework adopts the PSR-2 coding style with some additions.
Laravel apps *should* adopt this coding style as well.

However, Shift allows you to customize the adopted coding style by
adding your own [PHP CS Fixer][1] `.php_cs` config to your project.

You may use [Shift's .php_cs][2] file as a base.

[1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer
[2]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200

* Shift bindings

PHP 5.5.9+ adds the new static `class` property which provides the fully qualified class name. This is preferred over using class name strings as these references are checked by the parser.

* Shift core files

* Shift to Throwable

* Add laravel/ui dependency

* Unindent vendor mail templates

* Shift config files

* Default config files

In an effort to make upgrading the constantly changing config files
easier, Shift defaulted them so you can review the commit diff for
changes. Moving forward, you should use ENV variables or create a
separate config file to allow the core config files to remain
automatically upgradeable.

* Shift Laravel dependencies

* Shift cleanup

* Upgrade to Laravel 7

Co-authored-by: Laravel Shift <shift@laravelshift.com>
2020-09-06 19:38:10 +10:00

215 lines
6.3 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Utils;
use App\Http\Requests\Setup\CheckDatabaseRequest;
use App\Http\Requests\Setup\CheckMailRequest;
use App\Libraries\MultiDB;
use App\Mail\TestMailServer;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
/**
* Class SystemHealth.
*/
class SystemHealth
{
private static $extensions = [
'mysqli',
'gd',
'curl',
'zip',
'gmp',
'openssl',
'mbstring',
'xml',
'bcmath',
];
private static $php_version = 7.3;
/**
* Check loaded extensions / PHP version / DB Connections.
*
* @return array Result set of checks
*/
public static function check($check_database = true) : array
{
$system_health = true;
if (in_array(false, Arr::dot(self::extensions()))) {
$system_health = false;
}
if (phpversion() < self::$php_version) {
$system_health = false;
}
if (! self::simpleDbCheck() && $check_database) {
info('db fails');
$system_health = false;
}
return [
'system_health' => $system_health,
'extensions' => self::extensions(),
'php_version' => [
'minimum_php_version' => (string) self::$php_version,
'current_php_version' => phpversion(),
'is_okay' => version_compare(phpversion(), self::$php_version, '>='),
],
'env_writable' => self::checkEnvWritable(),
//'mail' => self::testMailServer(),
'simple_db_check' => (bool) self::simpleDbCheck(),
//'npm_status' => self::checkNpm(),
//'node_status' => self::checkNode(),
];
}
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];
}
} catch (\Exception $e) {
return false;
}
}
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];
}
} catch (\Exception $e) {
return false;
}
}
private static function simpleDbCheck() :bool
{
$result = true;
try {
$pdo = DB::connection()->getPdo();
$result = true;
} catch (\Exception $e) {
$result = false;
}
return $result;
}
private static function extensions() :array
{
$loaded_extensions = [];
foreach (self::$extensions as $extension) {
$loaded_extensions[] = [$extension => extension_loaded($extension)];
}
return $loaded_extensions;
}
public static function dbCheck($request = null): array
{
$result = ['success' => false];
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');
}
if (! config('ninja.db.multi_db_enabled')) {
try {
$pdo = DB::connection()->getPdo();
$result[] = [DB::connection()->getDatabaseName() => true];
$result['success'] = true;
} catch (\Exception $e) {
$result[] = [config('database.connections.'.config('database.default').'.database') => false];
$result['success'] = false;
}
} else {
foreach (MultiDB::$dbs as $db) {
MultiDB::setDB($db);
try {
$pdo = DB::connection()->getPdo();
$result[] = [DB::connection()->getDatabaseName() => true];
$result['success'] = true;
} catch (\Exception $e) {
$result[] = [config('database.connections.'.config('database.default').'.database') => false];
$result['success'] = false;
}
}
}
return $result;
}
private static function checkDbConnection()
{
return DB::connection()->getPdo();
}
public static function testMailServer($request = null)
{
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')));
} 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();
}
return response()->json(['message'=>'Success'], 200);
}
private static function checkEnvWritable()
{
return is_writable(base_path().'/.env');
}
}