source: branches/version-2_5-dev/html/handle_error.php @ 20764

Revision 20764, 4.7 KB checked in by nanasess, 13 years ago (diff)

#601 (コピーライトの更新)

  • 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_USER_ERROR を捕捉した場合にエラー画面を表示させるためのエラーハンドラ
27set_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 * この関数が実行され, エラーが捕捉されると, DEBUG_MODE が無効な場合,
36 * エラーページへリダイレクトする.
37 *
38 * @param string $buffer 出力バッファリングの内容
39 * @return string|void エラーが捕捉された場合は, エラーページへリダイレクトする;
40 *                     エラーが捕捉されない場合は, 出力バッファリングの内容を返す
41 */
42function &_fatal_error_handler(&$buffer) {
43    if (preg_match('/<b>(Fatal) error<\/b>: +(.+) in <b>(.+)<\/b> on line <b>(\d+)<\/b><br \/>/i', $buffer, $matches)) {
44        error_log("FATAL Error: $matches[3]:$matches[4] $matches[2]\n", 3,
45                  realpath(dirname(__FILE__) . "/" . HTML2DATA_DIR . "logs/site.log"));
46        if (DEBUG_MODE !== true) {
47            $url = HTTP_URL . "error.php";
48            if (defined('ADMIN_FUNCTION') && ADMIN_FUNCTION) {
49                $url .= "?admin";
50            }
51            header("Location: $url");
52            exit;
53        }
54    }
55    return $buffer;
56}
57
58/**
59 * E_USER_ERROR を捕捉した場合にエラー画面を表示させるエラーハンドラ関数.
60 *
61 * この関数は, set_error_handler() 関数に登録するための関数である.
62 * trigger_error にて E_USER_ERROR が生成されると, エラーログを出力した後,
63 * エラー画面を表示させる.
64 *
65 * E_USER_ERROR 以外のエラーが生成された場合, この関数は true を返す.
66 *
67 * @param integer $errno エラーコード
68 * @param string $errstr エラーメッセージ
69 * @param string $errfile エラーが発生したファイル名
70 * @param integer $errline エラーが発生した行番号
71 * @return void|boolean E_USER_ERROR が発生した場合は, エラーページへリダイレクト;
72 *                      E_USER_ERROR 以外の場合は true
73 */
74function handle_error($errno, $errstr, $errfile, $errline) {
75    switch ($errno) {
76    case E_USER_ERROR:
77        error_log("FATAL Error($errno) $errfile:$errline $errstr", 3, realpath(dirname(__FILE__) . "/" . HTML2DATA_DIR . "logs/site.log"));
78
79        displaySystemError($errstr);
80        exit(1);
81        break;
82
83    case E_USER_WARNING:
84    case E_USER_NOTICE:
85    default:
86    }
87    return true;
88}
89
90/**
91 * エラー画面を表示する
92 *
93 * @param string|null $errstr エラーメッセージ
94 * @return void
95 */
96function displaySystemError($errstr = null) {
97    if (SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE) {
98        ob_clean();
99        ob_start(array('SC_MobileEmoji', 'handler'));
100    } else {
101        // 最下層以外の出力用バッファをクリアし、出力のバッファリングを解除する
102        // FIXME #811(出力バッファリングの利用を見直し)
103        while (ob_get_level() >= 2) {
104            ob_end_clean();
105        }
106
107        // 最下層の出力バッファをクリアする
108        ob_clean();
109    }
110
111    require_once CLASS_EX_REALDIR . 'page_extends/error/LC_Page_Error_SystemError_Ex.php';
112    $objPage = new LC_Page_Error_SystemError_Ex();
113    register_shutdown_function(array($objPage, 'destroy'));
114    $objPage->init();
115    if (isset($errstr)) {
116        $objPage->arrDebugMsg[]
117            = "▼▼▼ エラーメッセージ ▼▼▼\n"
118            . $errstr
119            . "▲▲▲ エラーメッセージ ▲▲▲\n"
120        ;
121    }
122    $objPage->process();
123}
124?>
Note: See TracBrowser for help on using the repository browser.