From 01c4546064ae32b3073a474887824518373b605c Mon Sep 17 00:00:00 2001 From: uzulla Date: Sun, 21 Mar 2021 13:57:46 +0900 Subject: [PATCH] Add feature, display errors in the browser. #255 - set `ERROR_ON_DISPLAY` in config.php or set `FC2_ERROR_ON_DISPLAY` ENV. - please see `config.sample.php` --- app/config.sample.php | 7 ++- app/config_read_from_env.php | 1 + app/src/include/index_include.php | 96 ++++++++++++++++++------------- docker-compose.yml | 2 +- 4 files changed, 64 insertions(+), 42 deletions(-) diff --git a/app/config.sample.php b/app/config.sample.php index 2af6451f..b1a4ed41 100644 --- a/app/config.sample.php +++ b/app/config.sample.php @@ -2,8 +2,7 @@ // Sample Config file // Please copy `config.sample.php` to `config.php` and edit them. -//error_reporting(-1); -error_reporting(0); +error_reporting(-1); // DBの接続情報 define('DB_HOST', 'localhost'); // dbのホスト名 @@ -22,5 +21,9 @@ // Please edit the path when change `app` and `public` relative path condition. define('WWW_DIR', __DIR__ . '/../public/'); // this path need finish with slash. +// If you want get error log on display. +// define('ERROR_ON_DISPLAY', "1"); +// ini_set('display_errors', '1'); + // 別のGitHub repoを追従する場合に設定してください // define('GITHUB_REPO', '/uzulla/fc2blog'); diff --git a/app/config_read_from_env.php b/app/config_read_from_env.php index 80dfdda3..a32a1168 100644 --- a/app/config_read_from_env.php +++ b/app/config_read_from_env.php @@ -13,6 +13,7 @@ } if ((string)getenv("FC2_ERROR_ON_DISPLAY") === "1") { + define("ERROR_ON_DISPLAY", "1"); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); ini_set('html_errors', '1'); diff --git a/app/src/include/index_include.php b/app/src/include/index_include.php index acc36206..f06b7303 100644 --- a/app/src/include/index_include.php +++ b/app/src/include/index_include.php @@ -2,10 +2,11 @@ declare(strict_types=1); +// For security reasons. +// if you want set enable, please see FC2_ERROR_ON_DISPLAY in config.sample.php ini_set("display_errors", "0"); ini_set("display_startup_errors", "0"); ini_set('html_errors', "0"); -error_reporting(-1); // Check all errors when shutdown applications. register_shutdown_function(function () { @@ -37,18 +38,25 @@ http_response_code(500); } echo << - - - Internal Server Error - - -

500 Internal Server Error

-

Something went wrong.

-

(Please check error log)

- - - HTML; + + + + Internal Server Error + + +

500 Internal Server Error

+

Something went wrong.

+

(Please check error log)

+ HTML; + if(defined("ERROR_ON_DISPLAY") && ERROR_ON_DISPLAY==="1"){ + echo "
THE BLOG CONFIGURATION IS DANGER. PLEASE REMOVE `ERROR_ON_DISPLAY` in production.
"; + echo nl2br(htmlspecialchars($something.PHP_EOL, ENT_QUOTES)); + echo nl2br(htmlspecialchars("Uncaught Fatal Error: {$error['type']}:{$error['message']} in {$error['file']}:{$error['line']}")); + } + echo << + + HTML; }); try { @@ -66,20 +74,20 @@ if (!file_exists(__DIR__ . '/../../config.php') && (string)getenv("FC2_CONFIG_FROM_ENV") !== "1") { header("Content-Type: text/html; charset=UTF-8"); echo << - - - Does not exists config.php - - - Does not exists config.php / config.phpが存在しておりません -

- Please copy app/config.sample.php to app/config.php and edit them.
- app/config.sample.phpをapp/config.phpにコピーしファイル内に存在するDBの接続情報とサーバーの設定情報を入力してください。 -

- - - HTML; + + + + Does not exists config.php + + + Does not exists config.php / config.phpが存在しておりません +

+ Please copy app/config.sample.php to app/config.php and edit them.
+ app/config.sample.phpをapp/config.phpにコピーしファイル内に存在するDBの接続情報とサーバーの設定情報を入力してください。 +

+ + + HTML; return; } @@ -106,6 +114,9 @@ // ob_start(); // TODO remove all `echo` in app. ex: captcha + if(defined("ERROR_ON_DISPLAY") && ERROR_ON_DISPLAY==="1") { + echo "
THE BLOG CONFIGURATION IS DANGER. PLEASE REMOVE `ERROR_ON_DISPLAY` in production.
"; + } $c->emit(); ob_end_flush(); return; @@ -129,17 +140,24 @@ http_response_code(500); } echo << - - - Internal Server Error - - -

500 Internal Server Error

-

Something went wrong.

-

(Please check error log)

- - - HTML; + + + + Internal Server Error + + +

500 Internal Server Error

+

Something went wrong.

+

(Please check error log)

+ HTML; + if(defined("ERROR_ON_DISPLAY") && ERROR_ON_DISPLAY==="1"){ + echo "
THE BLOG CONFIGURATION IS DANGER. PLEASE REMOVE `ERROR_ON_DISPLAY` in production.
"; + echo nl2br(htmlspecialchars($something.PHP_EOL, ENT_QUOTES)); + echo nl2br(htmlspecialchars("Uncaught Exception {$error_class_name}: {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}\n{$e->getTraceAsString()}")); + } + echo << + + HTML; return; } diff --git a/docker-compose.yml b/docker-compose.yml index ad7bb835..ac587e63 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,7 +29,7 @@ services: FC2_ERROR_LOG_PATH: "php://stderr" FC2_APP_LOG_PATH: "php://stderr" FC2_LOG_LEVEL: 100 - FC2_ERROR_ON_DISPLAY: 1 + FC2_ERROR_ON_DISPLAY: 0 FC2_DB_HOST: "db" FC2_DB_PORT: "3306" FC2_DB_USER: "docker"