source: branches/version-2_11-dev/data/class/util/GC_Utils.php @ 20955

Revision 20955, 8.9 KB checked in by Seasoft, 13 years ago (diff)

#1323 (デバッグログ処理の無駄を削減)

  • 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 * @package Util
28 * @author LOCKON CO.,LTD.
29 * @version $Id$
30 */
31class GC_Utils {
32
33    /*----------------------------------------------------------------------
34     * [名称] gfDownloadCsv
35     * [概要] 引数データをCSVとして、クライアントにダウンロードさせる
36     * [引数] 1:ヘッダ文字列 2:CSVデータ
37     * [戻値] -
38     * [依存] -
39     * [注釈] 引数は1,2ともカンマ区切りになっていること
40     *----------------------------------------------------------------------*/
41    function gfDownloadCsv($header, $contents){
42
43        $fiest_name = date('YmdHis') .".csv";
44
45        /* HTTPヘッダの出力 */
46        Header("Content-disposition: attachment; filename=${fiest_name}");
47        Header("Content-type: application/octet-stream; name=${fiest_name}");
48
49        $return = $header.$contents;
50        if (mb_detect_encoding($return) == CHAR_CODE){
51            // 文字コード変換
52            $return = mb_convert_encoding($return,'SJIS',CHAR_CODE);
53            // 改行方法の統一
54            $return = str_replace( array( "\r\n", "\r" ), "\n", $return);
55        }
56        echo $return;
57    }
58
59    /*----------------------------------------------------------------------
60     * [名称] gfSetCsv
61     * [概要] 引数の配列をCSV形式に変換する
62     * [引数] 1:CSVにする配列 2:引数1が連想配列時の添え字を指定した配列
63     * [戻値] CSVデータ
64     * [依存] -
65     * [注釈] -
66     *----------------------------------------------------------------------*/
67    function gfSetCsv( $array, $arrayIndex = "" ){
68        //引数$arrayIndexは、$arrayが連想配列のときに添え字を指定してやるために使用する
69
70        $return = "";
71        for ($i=0; $i<count($array); $i++){
72
73            for ($j=0; $j<count($array[$i]); $j++ ){
74                if ( $j > 0 ) $return .= ",";
75                $return .= "\"";
76                if ( $arrayIndex ){
77                    $return .= mb_ereg_replace("<","<",mb_ereg_replace( "\"","\"\"",$array[$i][$arrayIndex[$j]] )) ."\"";
78                } else {
79                    $return .= mb_ereg_replace("<","<",mb_ereg_replace( "\"","\"\"",$array[$i][$j] )) ."\"";
80                }
81            }
82            $return .= "\n";
83        }
84
85        return $return;
86    }
87
88    /*----------------------------------------------------------------------
89     * [名称] gfGetAge
90     * [概要] 日付より年齢を計算する。
91     * [引数] 1:日付文字列(yyyy/mm/dd、yyyy-mm-dd hh:mm:ss等)
92     * [戻値] 年齢の数値
93     * [依存] -
94     * [注釈] -
95     *----------------------------------------------------------------------*/
96    function gfGetAge($dbdate)
97    {
98        $ty = date('Y');
99        $tm = date('m');
100        $td = date('d');
101        list($by, $bm, $bd) = preg_split("/[-/ ]/", $dbdate);
102        $age = $ty - $by;
103        if($tm * 100 + $td < $bm * 100 + $bd) $age--;
104        return $age;
105    }
106
107    /**
108     * ログファイルに変数の詳細を出力
109     *
110     * @param mixed $obj
111     * @return void
112     */
113    function gfDebugLog($obj) {
114        if (DEBUG_MODE === true) {
115            GC_Utils_Ex::gfPrintLog(
116                "*** start Debug ***\n" .
117                print_r($obj, true) .
118                "*** end Debug ***"
119            );
120        }
121    }
122
123    /*----------------------------------------------------------------------
124     * [名称] gfPrintLog
125     * [概要] ログファイルに日時、処理ファイル名、メッセージを出力
126     * [引数] 表示したいメッセージ
127     * [戻値] なし
128     * [依存] なし
129     * [注釈] -
130     *----------------------------------------------------------------------*/
131    function gfPrintLog($mess, $path = '') {
132        // 日付の取得
133        $today = date("Y/m/d H:i:s");
134        // 出力パスの作成
135        if ($path == "") {
136            $path = LOG_REALFILE;
137        }
138
139        // エスケープされている文字をもとに戻す
140        $trans_tbl = get_html_translation_table (HTML_ENTITIES);
141        $trans_tbl = array_flip ($trans_tbl);
142        $mess = strtr($mess, $trans_tbl);
143
144        $fp = fopen($path, "a+");
145        if($fp) {
146            fwrite( $fp, $today." [".$_SERVER['PHP_SELF']."] ".$mess." from ". $_SERVER['REMOTE_ADDR']. "\n" );
147            fclose( $fp );
148        }
149
150        // ログテーション
151        GC_Utils_Ex::gfLogRotation(MAX_LOG_QUANTITY, MAX_LOG_SIZE, $path);
152    }
153
154    /**
155     * ログローテーション機能
156     *
157     * XXX この類のローテーションは通常 0 開始だが、本実装は 1 開始である。
158     * この中でログ出力は行なわないこと。(無限ループの懸念あり)
159     * @param integer $max_log 最大ファイル数
160     * @param integer $max_size 最大サイズ
161     * @param string  $path ファイルパス
162     * @return void
163     */
164    function gfLogRotation($max_log, $max_size, $path) {
165
166        // ファイルが存在しない場合、終了
167        if (!file_exists($path)) return;
168
169        // ファイルが最大サイズを超えていない場合、終了
170        if (filesize($path) <= $max_size) return;
171
172        // Windows 版 PHP への対策として明示的に事前削除
173        $path_max = "$path.$max_log";
174        if (file_exists($path_max)) {
175            $res = unlink($path_max);
176            // 削除に失敗時した場合、ログローテーションは見送り
177            if (!$res) return;
178        }
179
180        // アーカイブのインクリメント
181        for ($i = $max_log; $i >= 2; $i--) {
182            $path_old = "$path." . ($i - 1);
183            $path_new = "$path.$i";
184            if (file_exists($path_old)) {
185                rename($path_old, $path_new);
186            }
187        }
188
189        // 現在ファイルのアーカイブ
190        rename($path, "$path.1");
191    }
192
193    /*----------------------------------------------------------------------
194     * [名称] gfMakePassword
195     * [概要] ランダムパスワード生成(英数字)
196     * [引数] パスワードの桁数
197     * [戻値] ランダム生成されたパスワード
198     * [依存] なし
199     * [注釈] -
200     *----------------------------------------------------------------------*/
201    function gfMakePassword($pwLength) {
202
203        // 乱数表のシードを決定
204        srand((double)microtime() * 54234853);
205
206        // パスワード文字列の配列を作成
207        $character = "abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345679";
208        $pw = preg_split("//", $character, 0, PREG_SPLIT_NO_EMPTY);
209
210        $password = "";
211        for($i = 0; $i<$pwLength; $i++ ) {
212            $password .= $pw[array_rand($pw, 1)];
213        }
214
215        return $password;
216    }
217
218    /*----------------------------------------------------------------------------------------------------------------------
219     * [名称] gfMailHeaderAddr
220     * [概要] 入力されたメールアドレスをメール関数用の宛先に変換
221     * [引数] 「メールアドレス」または「名前<メールアドレス>」、複数アドレス指定時はカンマ区切りで指定する。
222     * [戻値] 「メールアドレス」または「JIS_MIMEにコード変換した名前 <メールアドレス>」、複数アドレス指定時はカンマ区切りで返却する。
223     * [依存] なし
224     * [注釈] -
225     *----------------------------------------------------------------------------------------------------------------------*/
226
227    function gfMailHeaderAddr($str) {
228        $addrs = explode(",", $str); //アドレスを配列に入れる
229        foreach ($addrs as $addr) {
230            if (preg_match("/^(.+)<(.+)>$/", $addr, $matches)) {
231                //引数が「名前<メールアドレス>」の場合
232                $mailaddrs[] = mb_encode_mimeheader(trim($matches[1]))." <".trim($matches[2]).">";
233            } else {
234                //メールアドレスのみの場合
235                $mailaddrs[] =  trim($addr);
236            }
237        }
238        return implode(", ", $mailaddrs); //複数アドレスはカンマ区切りにする
239    }
240}
241?>
Note: See TracBrowser for help on using the repository browser.