| 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 | // エラー画面を表示させるためのエラーハンドラ |
|---|
| 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 | * エラー画面を表示させるための関数. |
|---|
| 58 | * |
|---|
| 59 | * この関数は, set_error_handler() 関数に登録するための関数である. |
|---|
| 60 | * trigger_error にて E_USER_ERROR が生成されると, ob_end_clean() 関数によって |
|---|
| 61 | * 出力バッファリングが無効にされ, エラーログを出力した後, エラーページへ |
|---|
| 62 | * リダイレクトする. |
|---|
| 63 | * |
|---|
| 64 | * E_USER_ERROR 以外のエラーが生成された場合, この関数は true を返す. |
|---|
| 65 | * |
|---|
| 66 | * @param integer $errno エラーコード |
|---|
| 67 | * @param string $errstr エラーメッセージ |
|---|
| 68 | * @param string $errfile エラーが発生したファイル名 |
|---|
| 69 | * @param integer $errline エラーが発生した行番号 |
|---|
| 70 | * @return void|boolean E_USER_ERROR が発生した場合は, エラーページへリダイレクト; |
|---|
| 71 | * E_USER_ERROR 以外の場合は true |
|---|
| 72 | */ |
|---|
| 73 | function handle_error($errno, $errstr, $errfile, $errline) { |
|---|
| 74 | switch ($errno) { |
|---|
| 75 | case E_USER_ERROR: |
|---|
| 76 | ob_end_clean(); |
|---|
| 77 | error_log("FATAL Error($errno) $errfile:$errline $errstr", 3, realpath(dirname(__FILE__) . "/" . HTML2DATA_DIR . "logs/site.log")); |
|---|
| 78 | |
|---|
| 79 | $admin = ""; |
|---|
| 80 | if (defined('ADMIN_FUNCTION') && ADMIN_FUNCTION) { |
|---|
| 81 | $admin = "?admin"; |
|---|
| 82 | } |
|---|
| 83 | header("Location: " . SITE_URL . "error.php" . $admin); |
|---|
| 84 | exit(1); |
|---|
| 85 | break; |
|---|
| 86 | |
|---|
| 87 | case E_USER_WARNING: |
|---|
| 88 | case E_USER_NOTICE: |
|---|
| 89 | default: |
|---|
| 90 | } |
|---|
| 91 | return true; |
|---|
| 92 | } |
|---|
| 93 | ?> |
|---|