source: branches/feature-module-update/data/class/util/GC_Utils.php @ 16582

Revision 16582, 10.4 KB checked in by nanasess, 16 years ago (diff)

ライセンス表記変更

  • 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-2007 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            $return = mb_convert_encoding($return,'SJIS',CHAR_CODE);
52            $return = str_replace( array( "\r\n", "\r" ), "\n", $return);   // 改行方法の統一
53        }
54        echo $return;
55    }
56
57    /*----------------------------------------------------------------------
58     * [名称] gfSetCsv
59     * [概要] 引数の配列をCSV形式に変換する
60     * [引数] 1:CSVにする配列 2:引数1が連想配列時の添え字を指定した配列
61     * [戻値] CSVデータ
62     * [依存] -
63     * [注釈] -
64     *----------------------------------------------------------------------*/
65    function gfSetCsv( $array, $arrayIndex = "" ){
66        //引数$arrayIndexは、$arrayが連想配列のときに添え字を指定してやるために使用する
67
68        $return = "";
69        for ($i=0; $i<count($array); $i++){
70
71            for ($j=0; $j<count($array[$i]); $j++ ){
72                if ( $j > 0 ) $return .= ",";
73                $return .= "\"";
74                if ( $arrayIndex ){
75                    $return .= mb_ereg_replace("<","<",mb_ereg_replace( "\"","\"\"",$array[$i][$arrayIndex[$j]] )) ."\"";
76                } else {
77                    $return .= mb_ereg_replace("<","<",mb_ereg_replace( "\"","\"\"",$array[$i][$j] )) ."\"";
78                }
79            }
80            $return .= "\n";
81        }
82
83        return $return;
84    }
85
86    /*----------------------------------------------------------------------
87     * [名称] gfGetAge
88     * [概要] 日付より年齢を計算する。
89     * [引数] 1:日付文字列(yyyy/mm/dd、yyyy-mm-dd hh:mm:ss等)
90     * [戻値] 年齢の数値
91     * [依存] -
92     * [注釈] -
93     *----------------------------------------------------------------------*/
94    function gfGetAge($dbdate)
95    {
96        $ty = date("Y");
97        $tm = date("m");
98        $td = date("d");
99        list($by, $bm, $bd) = split("[-/ ]", $dbdate);
100        $age = $ty - $by;
101        if($tm * 100 + $td < $bm * 100 + $bd) $age--;
102        return $age;
103    }
104
105    /*----------------------------------------------------------------------
106     * [名称] gfDebugLog
107     * [概要] ログファイルに変数の詳細を出力する。
108     * [引数] 対象となる変数
109     * [戻値] なし
110     * [依存] gfPrintLog
111     * [注釈] -
112     *----------------------------------------------------------------------*/
113    function gfDebugLog($obj){
114            GC_Utils::gfPrintLog("*** start Debug ***");
115            ob_start();
116            print_r($obj);
117            $buffer = ob_get_contents();
118            ob_end_clean();
119            $fp = fopen(LOG_PATH, "a+");
120            fwrite( $fp, $buffer."\n" );
121            fclose( $fp );
122            GC_Utils::gfPrintLog("*** end Debug ***");
123
124            // ログテーション
125            GC_Utils::gfLogRotation(MAX_LOG_QUANTITY, MAX_LOG_SIZE, LOG_PATH);
126    }
127
128    /*----------------------------------------------------------------------
129     * [名称] gfPrintLog
130     * [概要] ログファイルに日時、処理ファイル名、メッセージを出力
131     * [引数] 表示したいメッセージ
132     * [戻値] なし
133     * [依存] なし
134     * [注釈] -
135     *----------------------------------------------------------------------*/
136    function gfPrintLog($mess, $path = '') {
137        // 日付の取得
138        $today = date("Y/m/d H:i:s");
139        // 出力パスの作成
140        if ($path == "") {
141            $path = LOG_PATH;
142        }
143
144        // エスケープされている文字をもとに戻す
145        $trans_tbl = get_html_translation_table (HTML_ENTITIES);
146        $trans_tbl = array_flip ($trans_tbl);
147        $mess = strtr($mess, $trans_tbl);
148
149        $fp = fopen($path, "a+");
150        if($fp) {
151            fwrite( $fp, $today." [".$_SERVER['PHP_SELF']."] ".$mess." from ". $_SERVER['REMOTE_ADDR']. "\n" );
152            fclose( $fp );
153        }
154
155        // ログテーション
156        GC_Utils::gfLogRotation(MAX_LOG_QUANTITY, MAX_LOG_SIZE, $path);
157    }
158
159    /**
160     * ログローテーション機能
161     *
162     * @param integer $max_log 最大ファイル数
163     * @param integer $max_size 最大サイズ
164     * @param string  $path ファイルパス
165     * @return void なし
166     */
167    function gfLogRotation($max_log, $max_size, $path) {
168
169        // ディレクトリ名を取得
170        $dirname = dirname($path);
171        // ファイル名を取得
172        $basename = basename($path);
173
174        // ファイルが最大サイズを超えていないかチェック
175        if(filesize($path) > $max_size) {
176            if ($dh = opendir($dirname)) {
177                while (($file = readdir($dh)) !== false) {
178                    // ログローテーションにて作成されたファイルを取得
179                    if(ereg("^". $basename . "\." , $file)) {
180                        $arrLog[] = $file;
181                    }
182                }
183
184                // ファイルログが最大個数なら以上なら古いファイルから削除する
185                $count = count($arrLog);
186                if($count >= $max_log) {
187                    $diff = $count - $max_log;
188                    for($i = 0; $diff >= $i ; $i++) {
189                        unlink($dirname. "/" .array_pop($arrLog));
190                    }
191                }
192
193                // ログファイルの添え字をずらす
194                $count = count($arrLog);
195                for($i = $count; 1 <= $i; $i--) {
196                    $move_number = $i + 1;
197
198                    if(file_exists("$path.$move_number")) unlink("$path.$move_number");
199                    copy("$dirname/" . $arrLog[$i - 1], "$path.$move_number");
200
201                }
202                $ret = copy($path, "$path.1");
203
204                // 新規ログファイルを作成
205                if($ret) {
206                    unlink($path);
207                    touch($path);
208                }
209            }
210        }
211    }
212
213    /*----------------------------------------------------------------------
214     * [名称] gfMakePassword
215     * [概要] ランダムパスワード生成(英数字)
216     * [引数] パスワードの桁数
217     * [戻値] ランダム生成されたパスワード
218     * [依存] なし
219     * [注釈] -
220     *----------------------------------------------------------------------*/
221    function gfMakePassword($pwLength) {
222
223        // 乱数表のシードを決定
224        srand((double)microtime() * 54234853);
225
226        // パスワード文字列の配列を作成
227        $character = "abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345679";
228        $pw = preg_split("//", $character, 0, PREG_SPLIT_NO_EMPTY);
229
230        $password = "";
231        for($i = 0; $i<$pwLength; $i++ ) {
232            $password .= $pw[array_rand($pw, 1)];
233        }
234
235        return $password;
236    }
237
238    /*----------------------------------------------------------------------
239     * [名称] sf_explodeExt
240     * [概要] ファイルの拡張子取得
241     * [引数] ファイル名
242     * [戻値] 拡張子
243     * [依存] なし
244     * [注釈] -
245     *----------------------------------------------------------------------*/
246    function gf_explodeExt($fileName) {
247        $ext1 = explode(".", $fileName);
248        $ext2 = $ext1[count($ext1) - 1];
249        $ext2 = strtolower($ext2);
250        return $ext2;
251    }
252
253
254    /*----------------------------------------------------------------------------------------------------------------------
255     * [名称] gfMailHeaderAddr
256     * [概要] 入力されたメールアドレスをメール関数用の宛先に変換
257     * [引数] 「メールアドレス」または「名前<メールアドレス>」、複数アドレス指定時はカンマ区切りで指定する。
258     * [戻値] 「メールアドレス」または「JIS_MIMEにコード変換した名前 <メールアドレス>」、複数アドレス指定時はカンマ区切りで返却する。
259     * [依存] なし
260     * [注釈] -
261     *----------------------------------------------------------------------------------------------------------------------*/
262
263    function gfMailHeaderAddr($str) {
264        $addrs = explode(",", $str); //アドレスを配列に入れる
265        foreach ($addrs as $addr) {
266            if (preg_match("/^(.+)<(.+)>$/", $addr, $matches)) {
267                //引数が「名前<メールアドレス>」の場合
268                $mailaddrs[] = mb_encode_mimeheader(trim($matches[1]))." <".trim($matches[2]).">";
269            } else {
270                //メールアドレスのみの場合
271                $mailaddrs[] =  trim($addr);
272            }
273        }
274        return implode(", ", $mailaddrs); //複数アドレスはカンマ区切りにする
275    }
276}
277?>
Note: See TracBrowser for help on using the repository browser.