source: branches/version-2_12-dev/data/class/util/GC_Utils.php @ 21589

Revision 21589, 14.3 KB checked in by Seasoft, 12 years ago (diff)

#1633 (エラーハンドリングの改善)
#1613 (typo修正・ソース整形・ソースコメントの改善)

  • 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/**
25 * 各種ユーティリティクラス.
26 *
27 * このクラスはエラーハンドリング処理でも使用している。
28 * よって、このファイルで構文エラーが発生すると、EC-CUBE はエラーを捕捉できない。
29 * @package Util
30 * @author LOCKON CO.,LTD.
31 * @version $Id$
32 */
33class GC_Utils {
34
35    /**
36     * ログファイルに変数の詳細を出力
37     *
38     * @param mixed $obj
39     * @return void
40     */
41    function gfDebugLog($obj) {
42        if (USE_VERBOSE_LOG === true) {
43            $msg = "DEBUG\n"
44                 . print_r($obj, true);
45            GC_Utils_Ex::gfPrintLog($msg, DEBUG_LOG_REALFILE);
46        }
47    }
48
49    /**
50     * 呼び出し元関数名を返します
51     *
52     * @param int $forLogInfo ログ出力用に利用するかどうか(1:ログ出力用に利用する)
53     * @return string 呼び出し元クラス、関数名、行数の文字列表現
54     */
55    function gfGetCallerInfo($forLogInfo = true) {
56        // バックトレースを取得する
57        $traces = debug_backtrace(false);
58        $bklv = 1;
59        if ($forLogInfo === true) {
60            $bklv = 3;
61            if (($traces[3]['class'] === 'LC_Page' || $traces[3]['class'] === 'LC_Page_Admin')
62                && $traces[3]['function'] === 'log'
63            ) {
64                $bklv = 4;
65            }
66        }
67        $str = $traces[$bklv]['class'] . '::' . $traces[$bklv]['function'] . '(' . $traces[$bklv - 1]['line'] . ') ';
68        return $str;
69    }
70
71    /**
72     * デバッグ情報として必要な範囲のバックトレースを取得する
73     *
74     * エラーハンドリングに関わる情報を切り捨てる。
75     */
76    function getDebugBacktrace($arrBacktrace = null) {
77        if (is_null($arrBacktrace)) {
78            $arrBacktrace = debug_backtrace(false);
79        }
80        $arrReturn = array();
81        foreach (array_reverse($arrBacktrace) as $arrLine) {
82            // 言語レベルの致命的エラー時。発生元の情報はトレースできない。(エラーハンドリング処理のみがトレースされる)
83            // 実質的に何も返さない(空配列を返す)意図。
84            if (strlen($arrLine['file']) === 0
85                && ($arrLine['class'] === 'SC_Helper_HandleError' || $arrLine['class'] === 'SC_Helper_HandleError_Ex')
86                && ($arrLine['function'] === 'handle_error' || $arrLine['function'] === 'handle_warning')
87            ) {
88                break 1;
89            }
90
91            $arrReturn[] = $arrLine;
92
93            // エラーハンドリング処理に引き渡した以降の情報は通常不要なので含めない。
94            if (!isset($arrLine['class']) && $arrLine['function'] === 'trigger_error') {
95                break 1;
96            }
97            if (($arrLine['class'] === 'SC_Helper_HandleError' || $arrLine['class'] === 'SC_Helper_HandleError_Ex')
98                && ($arrLine['function'] === 'handle_error' || $arrLine['function'] === 'handle_warning')
99            ) {
100                break 1;
101            }
102            if (($arrLine['class'] === 'SC_Utils' || $arrLine['class'] === 'SC_Utils_Ex')
103                && $arrLine['function'] === 'sfDispException'
104            ) {
105                break 1;
106            }
107            if (($arrLine['class'] === 'GC_Utils' || $arrLine['class'] === 'GC_Utils_Ex')
108                && ($arrLine['function'] === 'gfDebugLog' || $arrLine['function'] === 'gfPrintLog')
109            ) {
110                break 1;
111            }
112        }
113        return array_reverse($arrReturn);
114    }
115
116    /**
117     * 前方互換用
118     *
119     * @deprecated 2.12.0
120     */
121    function gfGetLogStr($mess, $log_level = 'Info') {
122        trigger_error('前方互換用メソッドが使用されました。', E_USER_WARNING);
123        // メッセージの前に、ログ出力元関数名とログ出力関数呼び出し部分の行数を付与
124        $mess = GC_Utils::gfGetCallerInfo(true) . $mess;
125
126        // ログレベル=Debugの場合は、[Debug]を先頭に付与する
127        if ($log_level === 'Debug') {
128            $mess = '[Debug]' . $mess;
129        }
130
131        return $mess;
132    }
133
134    /**
135     * 前方互換用
136     *
137     * @deprecated 2.12.0 GC_Utils_Ex::gfPrintLog を使用すること
138     */
139    function gfAdminLog($mess, $log_level = 'Info') {
140        trigger_error('前方互換用メソッドが使用されました。', E_USER_WARNING);
141        // ログレベル=Debugの場合は、DEBUG_MODEがtrueの場合のみログ出力する
142        if ($log_level === 'Debug' && DEBUG_MODE === false) {
143            return;
144        }
145
146        // ログ出力
147        GC_Utils_Ex::gfPrintLog($mess, '', true);
148    }
149
150    /**
151     * 前方互換用
152     *
153     * @deprecated 2.12.0 GC_Utils_Ex::gfPrintLog を使用すること
154     */
155    function gfFrontLog($mess, $log_level = 'Info') {
156        trigger_error('前方互換用メソッドが使用されました。', E_USER_WARNING);
157        // ログレベル=Debugの場合は、DEBUG_MODEがtrueの場合のみログ出力する
158        if ($log_level === 'Debug' && DEBUG_MODE === false) {
159            return;
160        }
161
162        // ログ出力
163        GC_Utils_Ex::gfPrintLog($mess, '', true);
164    }
165
166    /**
167     * ログの出力を行う
168     *
169     * エラー・警告は trigger_error() を経由して利用すること。(補足の出力は例外。)
170     * @param string $msg
171     * @param string $path
172     * @param bool $verbose 冗長な出力を行うか
173     */
174    function gfPrintLog($msg, $path = '', $verbose = USE_VERBOSE_LOG) {
175        // 日付の取得
176        $today = date('Y/m/d H:i:s');
177        // 出力パスの作成
178
179        if (strlen($path) === 0) {
180            $path = GC_Utils_Ex::isAdminFunction() ? ADMIN_LOG_REALFILE : LOG_REALFILE;
181        }
182
183        $msg = "$today [{$_SERVER['PHP_SELF']}] $msg from {$_SERVER['REMOTE_ADDR']}\n";
184        if ($verbose) {
185            if (GC_Utils_Ex::isFrontFunction()) {
186                $msg .= 'customer_id = ' . $_SESSION['customer']['customer_id'] . "\n";
187            }
188            if (GC_Utils_Ex::isAdminFunction()) {
189                $msg .= 'login_id = ' . $_SESSION['login_id'] . '(' . $_SESSION['authority'] . ')' . '[' . session_id() . ']' . "\n";
190            }
191            $msg .= GC_Utils_Ex::toStringBacktrace(GC_Utils_Ex::getDebugBacktrace());
192        }
193
194        error_log($msg, 3, $path);
195
196        // ログテーション
197        GC_Utils_Ex::gfLogRotation(MAX_LOG_QUANTITY, MAX_LOG_SIZE, $path);
198    }
199
200    /**
201     * ログローテーション機能
202     *
203     * XXX この類のローテーションは通常 0 開始だが、本実装は 1 開始である。
204     * この中でログ出力は行なわないこと。(無限ループの懸念あり)
205     * @param integer $max_log 最大ファイル数
206     * @param integer $max_size 最大サイズ
207     * @param string  $path ファイルパス
208     * @return void
209     */
210    function gfLogRotation($max_log, $max_size, $path) {
211
212        // ファイルが存在しない場合、終了
213        if (!file_exists($path)) return;
214
215        // ファイルが最大サイズを超えていない場合、終了
216        if (filesize($path) <= $max_size) return;
217
218        // Windows 版 PHP への対策として明示的に事前削除
219        $path_max = "$path.$max_log";
220        if (file_exists($path_max)) {
221            $res = unlink($path_max);
222            // 削除に失敗時した場合、ログローテーションは見送り
223            if (!$res) return;
224        }
225
226        // アーカイブのインクリメント
227        for ($i = $max_log; $i >= 2; $i--) {
228            $path_old = "$path." . ($i - 1);
229            $path_new = "$path.$i";
230            if (file_exists($path_old)) {
231                rename($path_old, $path_new);
232            }
233        }
234
235        // 現在ファイルのアーカイブ
236        rename($path, "$path.1");
237    }
238
239    /*----------------------------------------------------------------------
240     * [名称] gfMakePassword
241     * [概要] ランダムパスワード生成(英数字)
242     * [引数] パスワードの桁数
243     * [戻値] ランダム生成されたパスワード
244     * [依存] なし
245     * [注釈] -
246     *----------------------------------------------------------------------*/
247    function gfMakePassword($pwLength) {
248
249        // 乱数表のシードを決定
250        srand((double)microtime() * 54234853);
251
252        // パスワード文字列の配列を作成
253        $character = 'abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345679';
254        $pw = preg_split('//', $character, 0, PREG_SPLIT_NO_EMPTY);
255
256        $password = '';
257        for ($i = 0; $i<$pwLength; $i++) {
258            $password .= $pw[array_rand($pw, 1)];
259        }
260
261        return $password;
262    }
263
264    /*----------------------------------------------------------------------------------------------------------------------
265     * [名称] gfMailHeaderAddr
266     * [概要] 入力されたメールアドレスをメール関数用の宛先に変換
267     * [引数] 「メールアドレス」または「名前<メールアドレス>」、複数アドレス指定時はカンマ区切りで指定する。
268     * [戻値] 「メールアドレス」または「JIS_MIMEにコード変換した名前 <メールアドレス>」、複数アドレス指定時はカンマ区切りで返却する。
269     * [依存] なし
270     * [注釈] -
271     *----------------------------------------------------------------------------------------------------------------------*/
272
273    function gfMailHeaderAddr($str) {
274        $addrs = explode(',', $str); //アドレスを配列に入れる
275        foreach ($addrs as $addr) {
276            if (preg_match("/^(.+)<(.+)>$/", $addr, $matches)) {
277                //引数が「名前<メールアドレス>」の場合
278                $mailaddrs[] = mb_encode_mimeheader(trim($matches[1])).' <'.trim($matches[2]).'>';
279            } else {
280                //メールアドレスのみの場合
281                $mailaddrs[] =  trim($addr);
282            }
283        }
284        return implode(', ', $mailaddrs); //複数アドレスはカンマ区切りにする
285    }
286
287    /**
288     * バックトレースをテキスト形式で出力する
289     *
290     * 現状スタックトレースの形で出力している。
291     * @param array $arrBacktrace バックトレース
292     * @return string テキストで表現したバックトレース
293     */
294    function toStringBacktrace($arrBacktrace) {
295        $string = '';
296
297        foreach (array_reverse($arrBacktrace) as $backtrace) {
298            if (strlen($backtrace['class']) >= 1) {
299                $func = $backtrace['class'] . $backtrace['type'] . $backtrace['function'];
300            } else {
301                $func = $backtrace['function'];
302            }
303
304            $string .= $backtrace['file'] . '(' . $backtrace['line'] . '): ' . $func . "\n";
305        }
306
307        return $string;
308    }
309
310    /**
311     * エラー型から該当する定数名を取得する
312     *
313     * 該当する定数がない場合、$error_type を返す。
314     * @param integer $error_type エラー型
315     * @return string|integer エラー定数名
316     */
317    function getErrorTypeName($error_type) {
318        $arrDefinedConstants = get_defined_constants(true);
319
320        // PHP の歴史対応
321        $arrDefinedCoreConstants = array();
322        // PHP >= 5.3.1, PHP == 5.3.0 (not Windows)
323        if (isset($arrDefinedConstants['Core'])) {
324            $arrDefinedCoreConstants = $arrDefinedConstants['Core'];
325        }
326        // PHP < 5.3.0
327        elseif (isset($arrDefinedConstants['internal'])) {
328            $arrDefinedCoreConstants = $arrDefinedConstants['internal'];
329        }
330        // PHP == 5.3.0 (Windows)
331        elseif (isset($arrDefinedConstants['mhash'])) {
332            $arrDefinedCoreConstants = $arrDefinedConstants['mhash'];
333        }
334
335        foreach ($arrDefinedCoreConstants as $constant_name => $constant_value) {
336            if (substr($constant_name, 0, 2) === 'E_' && $constant_value == $error_type) {
337                return $constant_name;
338            }
339        }
340        return $error_type;
341    }
342
343    /**
344     * 現在の URL を取得する
345     *
346     * @return string 現在のURL
347     */
348    function getUrl() {
349        $url = '';
350
351        if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
352            $url = 'https://';
353        } else {
354            $url = 'http://';
355        }
356
357        $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
358        if (strlen($_SERVER['QUERY_STRING']) >= 1) {
359            $url .= '?' . $_SERVER['QUERY_STRING'];
360        }
361
362        return $url;
363    }
364
365    /**
366     * 管理機能かを判定
367     *
368     * @return bool 管理機能か
369     */
370    function isAdminFunction() {
371        return defined('ADMIN_FUNCTION') && ADMIN_FUNCTION === true;
372    }
373
374    /**
375     * フロント機能かを判定
376     *
377     * @return bool フロント機能か
378     */
379    function isFrontFunction() {
380        return defined('FRONT_FUNCTION') && FRONT_FUNCTION === true;
381    }
382
383    /**
384     * インストール機能かを判定
385     *
386     * @return bool インストール機能か
387     */
388    function isInstallFunction() {
389        return defined('INSTALL_FUNCTION') && INSTALL_FUNCTION === true;
390    }
391
392    /**
393     * XML宣言を出力する.
394     *
395     * XML宣言があると問題が発生する UA は出力しない.
396     *
397     * @return string XML宣言の文字列
398     */
399    function printXMLDeclaration() {
400        $ua = $_SERVER['HTTP_USER_AGENT'];
401        if (!preg_match('/MSIE/', $ua) || preg_match('/MSIE 7/', $ua)) {
402            echo '<?xml version="1.0" encoding="' . CHAR_CODE . '"?>' . "\n";
403        }
404    }
405}
Note: See TracBrowser for help on using the repository browser.