source: branches/version-2_12-dev/html/handle_error.php @ 21420

Revision 21420, 5.7 KB checked in by Seasoft, 12 years ago (diff)

#1613 (ソース整形・ソースコメントの改善)

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