source: branches/version-2_4/data/class/util/GC_Utils.php @ 18734

Revision 18734, 9.4 KB checked in by nanasess, 14 years ago (diff)

Copyright の更新(#601)

  • 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-2010 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        if(DEBUG_MODE === true) {
115            GC_Utils::gfPrintLog("*** start Debug ***");
116            ob_start();
117            print_r($obj);
118            $buffer = ob_get_contents();
119            ob_end_clean();
120            $fp = fopen(LOG_PATH, "a+");
121            fwrite( $fp, $buffer."\n" );
122            fclose( $fp );
123            GC_Utils::gfPrintLog("*** end Debug ***");
124            // ログテーション
125            GC_Utils::gfLogRotation(MAX_LOG_QUANTITY, MAX_LOG_SIZE, LOG_PATH);
126        }
127    }
128
129    /*----------------------------------------------------------------------
130     * [名称] gfPrintLog
131     * [概要] ログファイルに日時、処理ファイル名、メッセージを出力
132     * [引数] 表示したいメッセージ
133     * [戻値] なし
134     * [依存] なし
135     * [注釈] -
136     *----------------------------------------------------------------------*/
137    function gfPrintLog($mess, $path = '') {
138        // 日付の取得
139        $today = date("Y/m/d H:i:s");
140        // 出力パスの作成
141        if ($path == "") {
142            $path = LOG_PATH;
143        }
144
145        // エスケープされている文字をもとに戻す
146        $trans_tbl = get_html_translation_table (HTML_ENTITIES);
147        $trans_tbl = array_flip ($trans_tbl);
148        $mess = strtr($mess, $trans_tbl);
149
150        $fp = fopen($path, "a+");
151        if($fp) {
152            fwrite( $fp, $today." [".$_SERVER['PHP_SELF']."] ".$mess." from ". $_SERVER['REMOTE_ADDR']. "\n" );
153            fclose( $fp );
154        }
155
156        // ログテーション
157        GC_Utils::gfLogRotation(MAX_LOG_QUANTITY, MAX_LOG_SIZE, $path);
158    }
159
160    /**
161     * ログローテーション機能
162     * XXX この類のローテーションは通常 0 開始だが、本実装は 1 開始である。
163     * @param integer $max_log 最大ファイル数
164     * @param integer $max_size 最大サイズ
165     * @param string  $path ファイルパス
166     * @return void なし
167     */
168    function gfLogRotation($max_log, $max_size, $path) {
169
170        // ファイルが最大サイズを超えていない場合、終了
171        if (filesize($path) <= $max_size) return;
172
173        // アーカイブのインクリメント(削除を兼ねる)
174        for ($i = $max_log; $i >= 2; $i--) {
175            $path_old = "$path." . ($i - 1);
176            $path_new = "$path.$i";
177            if (file_exists($path_old)) {
178                rename($path_old, $path_new);
179            }
180        }
181
182        // 現在ファイルのアーカイブ
183        rename($path, "$path.1");
184    }
185
186    /*----------------------------------------------------------------------
187     * [名称] gfMakePassword
188     * [概要] ランダムパスワード生成(英数字)
189     * [引数] パスワードの桁数
190     * [戻値] ランダム生成されたパスワード
191     * [依存] なし
192     * [注釈] -
193     *----------------------------------------------------------------------*/
194    function gfMakePassword($pwLength) {
195
196        // 乱数表のシードを決定
197        srand((double)microtime() * 54234853);
198
199        // パスワード文字列の配列を作成
200        $character = "abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345679";
201        $pw = preg_split("//", $character, 0, PREG_SPLIT_NO_EMPTY);
202
203        $password = "";
204        for($i = 0; $i<$pwLength; $i++ ) {
205            $password .= $pw[array_rand($pw, 1)];
206        }
207
208        return $password;
209    }
210
211    /*----------------------------------------------------------------------
212     * [名称] sf_explodeExt
213     * [概要] ファイルの拡張子取得
214     * [引数] ファイル名
215     * [戻値] 拡張子
216     * [依存] なし
217     * [注釈] -
218     *----------------------------------------------------------------------*/
219    function gf_explodeExt($fileName) {
220        $ext1 = explode(".", $fileName);
221        $ext2 = $ext1[count($ext1) - 1];
222        $ext2 = strtolower($ext2);
223        return $ext2;
224    }
225
226
227    /*----------------------------------------------------------------------------------------------------------------------
228     * [名称] gfMailHeaderAddr
229     * [概要] 入力されたメールアドレスをメール関数用の宛先に変換
230     * [引数] 「メールアドレス」または「名前<メールアドレス>」、複数アドレス指定時はカンマ区切りで指定する。
231     * [戻値] 「メールアドレス」または「JIS_MIMEにコード変換した名前 <メールアドレス>」、複数アドレス指定時はカンマ区切りで返却する。
232     * [依存] なし
233     * [注釈] -
234     *----------------------------------------------------------------------------------------------------------------------*/
235
236    function gfMailHeaderAddr($str) {
237        $addrs = explode(",", $str); //アドレスを配列に入れる
238        foreach ($addrs as $addr) {
239            if (preg_match("/^(.+)<(.+)>$/", $addr, $matches)) {
240                //引数が「名前<メールアドレス>」の場合
241                $mailaddrs[] = mb_encode_mimeheader(trim($matches[1]))." <".trim($matches[2]).">";
242            } else {
243                //メールアドレスのみの場合
244                $mailaddrs[] =  trim($addr);
245            }
246        }
247        return implode(", ", $mailaddrs); //複数アドレスはカンマ区切りにする
248    }
249}
250?>
Note: See TracBrowser for help on using the repository browser.