source: branches/version-2_11-dev/html/handle_error.php @ 21384

Revision 21384, 5.6 KB checked in by Seasoft, 12 years ago (diff)

#1589 (パフォーマンス改善 handle_error の無駄を省く)
#1588 (PHP の WARNING の扱いが曖昧)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2011 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// エラー捕捉用の出力バッファリング
24ob_start('_fatal_error_handler');
25
26// E_DEPRECATED 定数 (for PHP < 5.3)
27// TODO バージョン互換処理に統合したい。
28if (!defined('E_DEPRECATED')) {
29    define('E_DEPRECATED', 8192);
30}
31
32// エラーレベル設定
33// 開発時は E_ALL を推奨
34error_reporting(E_ALL & ~E_NOTICE & ~E_USER_NOTICE & ~E_DEPRECATED);
35
36// E_USER_ERROR を捕捉した場合にエラー画面を表示させるためのエラーハンドラ
37set_error_handler('handle_error', error_reporting());
38
39/**
40 * エラーを捕捉するための関数.
41 *
42 * PHP4 では, try/catch が使用できず, かつ set_error_handler で Fatal Error は
43 * 捕捉できないため, ob_start にこの関数を定義し, Fatal Error が発生した場合
44 * に出力される HTML 出力を捕捉する.
45 * この関数が実行され, エラーが捕捉されると, DEBUG_MODE が無効な場合,
46 * エラーページへリダイレクトする.
47 *
48 * @param string $buffer 出力バッファリングの内容
49 * @return string|void エラーが捕捉された場合は, エラーページへリダイレクトする;
50 *                     エラーが捕捉されない場合は, 出力バッファリングの内容を返す
51 */
52function &_fatal_error_handler(&$buffer) {
53    if (preg_match('/<b>(Fatal) error<\/b>: +(.+) in <b>(.+)<\/b> on line <b>(\d+)<\/b><br \/>/i', $buffer, $matches)) {
54        $now = date("Y/m/d H:i:s");
55        error_log($now . " [$matches[3]:$matches[4]] FATAL Error: $matches[2] from ". $_SERVER['REMOTE_ADDR'] . "\n", 3,
56                  realpath(dirname(__FILE__) . "/" . HTML2DATA_DIR . "logs/site.log"));
57        if (DEBUG_MODE !== true) {
58            $url = HTTP_URL . "error.php";
59            if (defined('ADMIN_FUNCTION') && ADMIN_FUNCTION) {
60                $url .= "?admin";
61            }
62            header("Location: $url");
63            exit;
64        }
65    }
66    return $buffer;
67}
68
69/**
70 * E_USER_ERROR を捕捉した場合にエラー画面を表示させるエラーハンドラ関数.
71 *
72 * この関数は, set_error_handler() 関数に登録するための関数である.
73 * trigger_error にて E_USER_ERROR が生成されると, エラーログを出力した後,
74 * エラー画面を表示させる.
75 * E_WARNING, E_USER_WARNING が発生した場合、ログを記録して、true を返す。
76 * (エラー画面・エラー文言は表示させない。)
77 *
78 * @param integer $errno エラーコード
79 * @param string $errstr エラーメッセージ
80 * @param string $errfile エラーが発生したファイル名
81 * @param integer $errline エラーが発生した行番号
82 * @return void|boolean E_USER_ERROR が発生した場合は, エラーページへリダイレクト;
83 *                      E_WARNING, E_USER_WARNING が発生した場合、true を返す
84 */
85function handle_error($errno, $errstr, $errfile, $errline) {
86
87    // error_reporting 設定に含まれていないエラーコードは処理しない
88    if (!(error_reporting() & $errno)) {
89        return;
90    }
91
92    $now = date("Y/m/d H:i:s");
93    switch ($errno) {
94        case E_USER_ERROR:
95            error_log($now . " [$errfile] FATAL Error($errno) $errfile:$errline $errstr from ". $_SERVER['REMOTE_ADDR'] . "\n", 3, realpath(dirname(__FILE__) . "/" . HTML2DATA_DIR . "logs/site.log"));
96
97            displaySystemError($errstr);
98            exit(1);
99            break;
100
101        case E_WARNING:
102        case E_USER_WARNING:
103            error_log($now . " [$errfile] WARNING($errno) $errfile:$errline $errstr from ". $_SERVER['REMOTE_ADDR'] . "\n", 3, realpath(dirname(__FILE__) . "/" . HTML2DATA_DIR . "logs/site.log"));
104            return true;
105            break;
106
107        default:
108    }
109}
110
111/**
112 * エラー画面を表示する
113 *
114 * @param string|null $errstr エラーメッセージ
115 * @return void
116 */
117function displaySystemError($errstr = null) {
118    if (SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE) {
119        ob_clean();
120        ob_start(array('SC_MobileEmoji', 'handler'));
121    } else {
122        // 最下層以外の出力用バッファをクリアし、出力のバッファリングを解除する
123        // FIXME #811(出力バッファリングの利用を見直し)
124        while (ob_get_level() >= 2) {
125            ob_end_clean();
126        }
127
128        // 最下層の出力バッファをクリアする
129        ob_clean();
130    }
131
132    require_once CLASS_EX_REALDIR . 'page_extends/error/LC_Page_Error_SystemError_Ex.php';
133    $objPage = new LC_Page_Error_SystemError_Ex();
134    register_shutdown_function(array($objPage, 'destroy'));
135    $objPage->init();
136    if (isset($errstr)) {
137        $objPage->arrDebugMsg[]
138            = "▼▼▼ エラーメッセージ ▼▼▼\n"
139            . $errstr
140            . "▲▲▲ エラーメッセージ ▲▲▲\n"
141        ;
142    }
143    $objPage->process();
144}
145?>
Note: See TracBrowser for help on using the repository browser.