Skip to content

Commit

Permalink
Add feature, display errors in the browser. fc2blog#255
Browse files Browse the repository at this point in the history
- set `ERROR_ON_DISPLAY` in config.php or set `FC2_ERROR_ON_DISPLAY` ENV.
- please see `config.sample.php`
  • Loading branch information
uzulla committed Mar 21, 2021
1 parent d77f920 commit 01c4546
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 42 deletions.
7 changes: 5 additions & 2 deletions app/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -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のホスト名
Expand All @@ -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');
1 change: 1 addition & 0 deletions app/config_read_from_env.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
96 changes: 57 additions & 39 deletions app/src/include/index_include.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -37,18 +38,25 @@
http_response_code(500);
}
echo <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Internal Server Error</title>
</head>
<body>
<h1>500 Internal Server Error</h1>
<p>Something went wrong.</p>
<p>(Please check error log)</p>
</body>
</html>
HTML;
<!DOCTYPE html>
<html lang="en">
<head>
<title>Internal Server Error</title>
</head>
<body>
<h1>500 Internal Server Error</h1>
<p>Something went wrong.</p>
<p>(Please check error log)</p>
HTML;
if(defined("ERROR_ON_DISPLAY") && ERROR_ON_DISPLAY==="1"){
echo "<hr><span style='color:red'>THE BLOG CONFIGURATION IS DANGER. PLEASE REMOVE `ERROR_ON_DISPLAY` in production.</span><br>";
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
</body>
</html>
HTML;
});

try {
Expand All @@ -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 <<<HTML
<!DOCTYPE html>
<html lang="ja">
<head>
<title>Does not exists config.php</title>
</head>
<body>
Does not exists config.php / config.phpが存在しておりません
<p class="ng">
Please copy app/config.sample.php to app/config.php and edit them.<br>
app/config.sample.phpをapp/config.phpにコピーしファイル内に存在するDBの接続情報とサーバーの設定情報を入力してください。
</p>
</body>
</html>
HTML;
<!DOCTYPE html>
<html lang="ja">
<head>
<title>Does not exists config.php</title>
</head>
<body>
Does not exists config.php / config.phpが存在しておりません
<p class="ng">
Please copy app/config.sample.php to app/config.php and edit them.<br>
app/config.sample.phpをapp/config.phpにコピーしファイル内に存在するDBの接続情報とサーバーの設定情報を入力してください。
</p>
</body>
</html>
HTML;
return;
}

Expand All @@ -106,6 +114,9 @@
// ob_start();
// TODO remove all `echo` in app. ex: captcha

if(defined("ERROR_ON_DISPLAY") && ERROR_ON_DISPLAY==="1") {
echo "<hr><span style='color:red'>THE BLOG CONFIGURATION IS DANGER. PLEASE REMOVE `ERROR_ON_DISPLAY` in production.</span><hr>";
}
$c->emit();
ob_end_flush();
return;
Expand All @@ -129,17 +140,24 @@
http_response_code(500);
}
echo <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Internal Server Error</title>
</head>
<body>
<h1>500 Internal Server Error</h1>
<p>Something went wrong.</p>
<p>(Please check error log)</p>
</body>
</html>
HTML;
<!DOCTYPE html>
<html lang="en">
<head>
<title>Internal Server Error</title>
</head>
<body>
<h1>500 Internal Server Error</h1>
<p>Something went wrong.</p>
<p>(Please check error log)</p>
HTML;
if(defined("ERROR_ON_DISPLAY") && ERROR_ON_DISPLAY==="1"){
echo "<hr><span style='color:red'>THE BLOG CONFIGURATION IS DANGER. PLEASE REMOVE `ERROR_ON_DISPLAY` in production.</span><br>";
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
</body>
</html>
HTML;
return;
}
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 01c4546

Please sign in to comment.