firefly-iii/app/Http/Controllers/DebugController.php

275 lines
9.6 KiB
PHP
Raw Normal View History

2017-12-31 04:38:21 -06:00
<?php
/**
* DebugController.php
2020-01-31 00:32:04 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2017-12-31 04:38:21 -06:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-12-31 04:38:21 -06:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-12-31 04:38:21 -06:00
*
* This program is distributed in the hope that it will be useful,
2017-12-31 04:38:21 -06:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-12-31 04:38:21 -06:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-12-31 04:38:21 -06:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers;
use Artisan;
2017-12-31 04:38:21 -06:00
use Carbon\Carbon;
use DB;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Middleware\IsDemoUser;
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
use Illuminate\Http\RedirectResponse;
2017-12-31 04:38:21 -06:00
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Routing\Route;
2017-12-31 04:38:21 -06:00
use Log;
use Monolog\Handler\RotatingFileHandler;
use Route as RouteFacade;
2017-12-31 04:38:21 -06:00
/**
* Class DebugController
2018-07-17 15:21:03 -05:00
*
2017-12-31 04:38:21 -06:00
*/
class DebugController extends Controller
{
use GetConfigurationData;
/**
2019-07-21 10:15:06 -05:00
* DebugController constructor.
*
2019-07-21 10:15:06 -05:00
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
2020-02-06 15:01:59 -06:00
$this->middleware(IsDemoUser::class)->except(['displayError']);
}
/**
2018-07-22 01:10:16 -05:00
* Show all possible errors.
*
* @throws FireflyException
*/
2018-07-08 05:28:42 -05:00
public function displayError(): void
{
Log::debug('This is a test message at the DEBUG level.');
Log::info('This is a test message at the INFO level.');
Log::notice('This is a test message at the NOTICE level.');
Log::warning('This is a test message at the WARNING level.');
Log::error('This is a test message at the ERROR level.');
Log::critical('This is a test message at the CRITICAL level.');
Log::alert('This is a test message at the ALERT level.');
Log::emergency('This is a test message at the EMERGENCY level.');
throw new FireflyException('A very simple test error.');
}
/**
2018-07-22 01:10:16 -05:00
* Clear log and session.
*
* @param Request $request
*
* @return RedirectResponse|Redirector
*/
public function flush(Request $request)
{
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
$request->session()->forget(['start', 'end', '_previous', 'viewRange', 'range', 'is_custom_range']);
Log::debug('Call cache:clear...');
Artisan::call('cache:clear');
Log::debug('Call config:clear...');
Artisan::call('config:clear');
Log::debug('Call route:clear...');
Artisan::call('route:clear');
Log::debug('Call twig:clean...');
try {
Artisan::call('twig:clean');
// @codeCoverageIgnoreStart
} catch (Exception $e) {
// don't care
2018-07-08 00:59:58 -05:00
Log::debug(sprintf('Called twig:clean: %s', $e->getMessage()));
}
// @codeCoverageIgnoreEnd
Log::debug('Call view:clear...');
Artisan::call('view:clear');
Log::debug('Done! Redirecting...');
return redirect(route('index'));
}
2017-12-31 04:38:21 -06:00
/**
2018-07-22 01:10:16 -05:00
* Show debug info.
*
2017-12-31 04:38:21 -06:00
* @param Request $request
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2018-07-17 15:21:03 -05:00
*
2017-12-31 04:38:21 -06:00
*/
public function index(Request $request)
{
$search = ['~', '#'];
$replace = ['\~', '# '];
2020-05-25 23:28:42 -05:00
$now = Carbon::now()->format('Y-m-d H:i:s e');
2020-02-22 23:36:58 -06:00
$installationId = app('fireflyconfig')->get('installation_id', '')->data;
2017-12-31 04:38:21 -06:00
$phpVersion = str_replace($search, $replace, PHP_VERSION);
2018-03-27 12:29:58 -05:00
$phpOs = str_replace($search, $replace, PHP_OS);
2017-12-31 04:38:21 -06:00
$interface = PHP_SAPI;
2018-03-27 12:29:58 -05:00
$drivers = implode(', ', DB::availableDrivers());
2017-12-31 04:38:21 -06:00
$currentDriver = DB::getDriverName();
$userAgent = $request->header('user-agent');
$trustedProxies = config('firefly.trusted_proxies');
2017-12-31 04:38:21 -06:00
$displayErrors = ini_get('display_errors');
$errorReporting = $this->errorReporting((int) ini_get('error_reporting'));
$appEnv = config('app.env');
$appDebug = var_export(config('app.debug'), true);
$logChannel = config('logging.default');
$appLogLevel = config('logging.level');
$cacheDriver = config('cache.default');
2020-05-25 23:28:42 -05:00
$loginProvider = config('auth.providers.users.driver');
// some new vars.
$telemetry = true === config('firefly.send_telemetry') && true === config('firefly.feature_flags.telemetry');
$defaultLanguage = (string) config('firefly.default_language');
$defaultLocale = (string) config('firefly.default_locale');
$userLanguage = app('steam')->getLanguage();
$userLocale = app('steam')->getLocale();
$isDocker = env('IS_DOCKER', false);
2017-12-31 04:38:21 -06:00
// set languages, see what happens:
$original = setlocale(LC_ALL, 0);
$localeAttempts = [];
2020-04-22 02:28:20 -05:00
$parts = app('steam')->getLocaleArray(app('steam')->getLocale());
foreach ($parts as $code) {
2020-05-25 23:28:42 -05:00
$code = trim($code);
Log::debug(sprintf('Trying to set %s', $code));
$localeAttempts[$code] = var_export(setlocale(LC_ALL, $code), true);
}
setlocale(LC_ALL, $original);
2017-12-31 04:38:21 -06:00
// get latest log file:
2018-03-08 14:08:26 -06:00
$logger = Log::driver();
2017-12-31 04:38:21 -06:00
$handlers = $logger->getHandlers();
$logContent = '';
foreach ($handlers as $handler) {
if ($handler instanceof RotatingFileHandler) {
$logFile = $handler->getUrl();
if (null !== $logFile) {
try {
2018-01-28 13:59:26 -06:00
$logContent = file_get_contents($logFile);
2018-06-01 15:04:52 -05:00
// @codeCoverageIgnoreStart
2018-01-28 13:59:26 -06:00
} catch (Exception $e) {
// don't care
2018-03-27 12:29:58 -05:00
Log::debug(sprintf('Could not read log file. %s', $e->getMessage()));
}
2018-06-01 15:04:52 -05:00
// @codeCoverageIgnoreEnd
2017-12-31 04:38:21 -06:00
}
}
}
2019-02-13 10:38:41 -06:00
if ('' !== $logContent) {
2018-11-02 15:17:07 -05:00
// last few lines
$logContent = 'Truncated from this point <----|' . substr($logContent, -8192);
}
2017-12-31 04:38:21 -06:00
return view(
'debug',
compact(
'phpVersion',
'localeAttempts',
'appEnv',
'appDebug',
'logChannel',
'appLogLevel',
'now',
'drivers',
'currentDriver',
'loginProvider',
'userAgent',
'displayErrors',
'installationId',
'errorReporting',
'phpOs',
'interface',
'logContent',
'cacheDriver',
2020-05-25 23:28:42 -05:00
'trustedProxies',
'telemetry',
'userLanguage',
'userLocale',
'defaultLanguage',
'defaultLocale',
'isDocker'
)
2017-12-31 04:38:21 -06:00
);
}
/**
2018-07-22 01:10:16 -05:00
* Return all possible routes.
*
* @return string
*/
public function routes(): string
{
$set = RouteFacade::getRoutes();
$ignore = ['chart.', 'javascript.', 'json.', 'report-data.', 'popup.', 'debugbar.', 'attachments.download', 'attachments.preview',
2020-06-06 14:23:26 -05:00
'bills.rescan', 'budgets.income', 'currencies.def', 'error', 'flush', 'help.show',
'login', 'logout', 'password.reset', 'profile.confirm-email-change', 'profile.undo-email-change',
'register', 'report.options', 'routes', 'rule-groups.down', 'rule-groups.up', 'rules.up', 'rules.down',
'rules.select', 'search.search', 'test-flash', 'transactions.link.delete', 'transactions.link.switch',
2020-06-06 14:23:26 -05:00
'two-factor.lost', 'reports.options', 'debug',
'preferences.delete-code', 'rules.test-triggers', 'piggy-banks.remove-money', 'piggy-banks.add-money',
2019-06-07 11:19:24 -05:00
'accounts.reconcile.transactions', 'accounts.reconcile.overview',
2020-06-06 14:23:26 -05:00
'transactions.clone', 'two-factor.index', 'api.v1', 'installer.', 'attachments.view', 'recurring.events',
2018-06-21 11:58:27 -05:00
'recurring.suggest',
];
$return = '&nbsp;';
/** @var Route $route */
foreach ($set as $route) {
$name = (string) $route->getName();
2019-06-21 12:10:02 -05:00
if (in_array('GET', $route->methods(), true)) {
$found = false;
foreach ($ignore as $string) {
if (!(false === stripos($name, $string))) {
$found = true;
break;
}
}
2018-07-08 00:59:58 -05:00
if (false === $found) {
$return .= 'touch ' . $route->getName() . '.md;';
}
}
}
return $return;
}
/**
2018-07-22 01:10:16 -05:00
* Flash all types of messages.
*
* @param Request $request
*
* @return RedirectResponse|Redirector
*/
public function testFlash(Request $request)
{
$request->session()->flash('success', 'This is a success message.');
$request->session()->flash('info', 'This is an info message.');
$request->session()->flash('warning', 'This is a warning.');
$request->session()->flash('error', 'This is an error!');
return redirect(route('home'));
}
2018-03-05 12:35:58 -06:00
}