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

Revision 22567, 14.4 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

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