| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved. |
|---|
| 6 | * |
|---|
| 7 | * http://www.lockon.co.jp/ |
|---|
| 8 | * |
|---|
| 9 | * This program is free software; you can redistribute it and/or |
|---|
| 10 | * modify it under the terms of the GNU General Public License |
|---|
| 11 | * as published by the Free Software Foundation; either version 2 |
|---|
| 12 | * of the License, or (at your option) any later version. |
|---|
| 13 | * |
|---|
| 14 | * This program is distributed in the hope that it will be useful, |
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | * GNU General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU General Public License |
|---|
| 20 | * along with this program; if not, write to the Free Software |
|---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | // エラー捕捉用の出力バッファリング |
|---|
| 24 | ob_start('_fatal_error_handler'); |
|---|
| 25 | |
|---|
| 26 | // E_USER_ERROR を捕捉した場合にエラー画面を表示させるためのエラーハンドラ |
|---|
| 27 | set_error_handler('handle_error'); |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * エラーを捕捉するための関数. |
|---|
| 31 | * |
|---|
| 32 | * PHP4 では, try/catch が使用できず, かつ set_error_handler で Fatal Error は |
|---|
| 33 | * 捕捉できないため, ob_start にこの関数を定義し, Fatal Error が発生した場合 |
|---|
| 34 | * に出力される HTML 出力を捕捉する. |
|---|
| 35 | * この関数が実行され, エラーが捕捉されると, エラーページへリダイレクトする. |
|---|
| 36 | * |
|---|
| 37 | * @param string $buffer 出力バッファリングの内容 |
|---|
| 38 | * @return string|void エラーが捕捉された場合は, エラーページへリダイレクトする; |
|---|
| 39 | * エラーが捕捉されない場合は, 出力バッファリングの内容を返す |
|---|
| 40 | */ |
|---|
| 41 | function &_fatal_error_handler(&$buffer) { |
|---|
| 42 | if (preg_match('/<b>(Fatal) error<\/b>: +(.+) in <b>(.+)<\/b> on line <b>(\d+)<\/b><br \/>/i', $buffer, $matches)) { |
|---|
| 43 | |
|---|
| 44 | $admin = ""; |
|---|
| 45 | if (defined('ADMIN_FUNCTION') && ADMIN_FUNCTION) { |
|---|
| 46 | $admin = "?admin"; |
|---|
| 47 | } |
|---|
| 48 | error_log("FATAL Error: $matches[3]:$matches[4] $matches[2]\n", 3, |
|---|
| 49 | realpath(dirname(__FILE__) . "/" . HTML2DATA_DIR . "logs/site.log")); |
|---|
| 50 | header("Location: " . SITE_URL . "error.php" . $admin); |
|---|
| 51 | exit; |
|---|
| 52 | } |
|---|
| 53 | return $buffer; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * E_USER_ERROR を捕捉した場合にエラー画面を表示させるエラーハンドラ関数. |
|---|
| 58 | * |
|---|
| 59 | * この関数は, set_error_handler() 関数に登録するための関数である. |
|---|
| 60 | * trigger_error にて E_USER_ERROR が生成されると, エラーログを出力した後, |
|---|
| 61 | * エラー画面を表示させる. |
|---|
| 62 | * |
|---|
| 63 | * E_USER_ERROR 以外のエラーが生成された場合, この関数は true を返す. |
|---|
| 64 | * |
|---|
| 65 | * @param integer $errno エラーコード |
|---|
| 66 | * @param string $errstr エラーメッセージ |
|---|
| 67 | * @param string $errfile エラーが発生したファイル名 |
|---|
| 68 | * @param integer $errline エラーが発生した行番号 |
|---|
| 69 | * @return void|boolean E_USER_ERROR が発生した場合は, エラーページへリダイレクト; |
|---|
| 70 | * E_USER_ERROR 以外の場合は true |
|---|
| 71 | */ |
|---|
| 72 | function handle_error($errno, $errstr, $errfile, $errline) { |
|---|
| 73 | switch ($errno) { |
|---|
| 74 | case E_USER_ERROR: |
|---|
| 75 | error_log("FATAL Error($errno) $errfile:$errline $errstr", 3, realpath(dirname(__FILE__) . "/" . HTML2DATA_DIR . "logs/site.log")); |
|---|
| 76 | |
|---|
| 77 | displaySystemError($errstr); |
|---|
| 78 | exit(1); |
|---|
| 79 | break; |
|---|
| 80 | |
|---|
| 81 | case E_USER_WARNING: |
|---|
| 82 | case E_USER_NOTICE: |
|---|
| 83 | default: |
|---|
| 84 | } |
|---|
| 85 | return true; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | /** |
|---|
| 89 | * エラー画面を表示する |
|---|
| 90 | * |
|---|
| 91 | * @param string|null $errstr エラーメッセージ |
|---|
| 92 | * @return void |
|---|
| 93 | */ |
|---|
| 94 | function displaySystemError($errstr = null) { |
|---|
| 95 | if (SC_Utils_Ex::sfIsMobileSite()) { |
|---|
| 96 | ob_clean(); |
|---|
| 97 | } else { |
|---|
| 98 | // 最下層以外の出力用バッファをクリアし、出力のバッファリングを解除する |
|---|
| 99 | // FIXME #811(出力バッファリングの利用を見直し) |
|---|
| 100 | while (ob_get_level() >= 2) { |
|---|
| 101 | ob_end_clean(); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | // 最下層の出力バッファをクリアする |
|---|
| 105 | ob_clean(); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | require_once CLASS_EX_PATH . 'page_extends/error/LC_Page_Error_SystemError_Ex.php'; |
|---|
| 109 | $objPage = new LC_Page_Error_SystemError_Ex(); |
|---|
| 110 | register_shutdown_function(array($objPage, 'destroy')); |
|---|
| 111 | $objPage->init(); |
|---|
| 112 | if (isset($errstr)) { |
|---|
| 113 | $objPage->arrDebugMsg[] |
|---|
| 114 | = "▼▼▼ エラーメッセージ ▼▼▼\n" |
|---|
| 115 | . $errstr |
|---|
| 116 | . "▲▲▲ エラーメッセージ ▲▲▲\n" |
|---|
| 117 | ; |
|---|
| 118 | } |
|---|
| 119 | $objPage->process(); |
|---|
| 120 | } |
|---|
| 121 | ?> |
|---|