source: branches/version-2_5-dev/data/class/util/GC_Utils.php @ 20484

Revision 20484, 9.4 KB checked in by shutta, 13 years ago (diff)

拡張クラス(/class_extends/)が存在するのに基底クラス(/class/)のメソッドを呼び出している部分を拡張クラスに書き換え。

  • 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-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) = preg_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_Ex::gfPrintLog("*** start Debug ***");
116            ob_start();
117            print_r($obj);
118            $buffer = ob_get_contents();
119            ob_end_clean();
120            $fp = fopen(LOG_REALFILE, "a+");
121            fwrite( $fp, $buffer."\n" );
122            fclose( $fp );
123            GC_Utils_Ex::gfPrintLog("*** end Debug ***");
124            // ログテーション
125            GC_Utils_Ex::gfLogRotation(MAX_LOG_QUANTITY, MAX_LOG_SIZE, LOG_REALFILE);
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_REALFILE;
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_Ex::gfLogRotation(MAX_LOG_QUANTITY, MAX_LOG_SIZE, $path);
158    }
159
160    /**
161     * ログローテーション機能
162     *
163     * XXX この類のローテーションは通常 0 開始だが、本実装は 1 開始である。
164     * この中でログ出力は行なわないこと。(無限ループの懸念あり)
165     * @param integer $max_log 最大ファイル数
166     * @param integer $max_size 最大サイズ
167     * @param string  $path ファイルパス
168     * @return void
169     */
170    function gfLogRotation($max_log, $max_size, $path) {
171
172        // ファイルが存在しない場合、終了
173        if (!file_exists($path)) return;
174
175        // ファイルが最大サイズを超えていない場合、終了
176        if (filesize($path) <= $max_size) return;
177
178        // Windows 版 PHP への対策として明示的に事前削除
179        $path_max = "$path.$max_log";
180        if (file_exists($path_max)) {
181            $res = unlink($path_max);
182            // 削除に失敗時した場合、ログローテーションは見送り
183            if (!$res) return;
184        }
185
186        // アーカイブのインクリメント
187        for ($i = $max_log; $i >= 2; $i--) {
188            $path_old = "$path." . ($i - 1);
189            $path_new = "$path.$i";
190            if (file_exists($path_old)) {
191                rename($path_old, $path_new);
192            }
193        }
194
195        // 現在ファイルのアーカイブ
196        rename($path, "$path.1");
197    }
198
199    /*----------------------------------------------------------------------
200     * [名称] gfMakePassword
201     * [概要] ランダムパスワード生成(英数字)
202     * [引数] パスワードの桁数
203     * [戻値] ランダム生成されたパスワード
204     * [依存] なし
205     * [注釈] -
206     *----------------------------------------------------------------------*/
207    function gfMakePassword($pwLength) {
208
209        // 乱数表のシードを決定
210        srand((double)microtime() * 54234853);
211
212        // パスワード文字列の配列を作成
213        $character = "abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345679";
214        $pw = preg_split("//", $character, 0, PREG_SPLIT_NO_EMPTY);
215
216        $password = "";
217        for($i = 0; $i<$pwLength; $i++ ) {
218            $password .= $pw[array_rand($pw, 1)];
219        }
220
221        return $password;
222    }
223
224    /*----------------------------------------------------------------------------------------------------------------------
225     * [名称] gfMailHeaderAddr
226     * [概要] 入力されたメールアドレスをメール関数用の宛先に変換
227     * [引数] 「メールアドレス」または「名前<メールアドレス>」、複数アドレス指定時はカンマ区切りで指定する。
228     * [戻値] 「メールアドレス」または「JIS_MIMEにコード変換した名前 <メールアドレス>」、複数アドレス指定時はカンマ区切りで返却する。
229     * [依存] なし
230     * [注釈] -
231     *----------------------------------------------------------------------------------------------------------------------*/
232
233    function gfMailHeaderAddr($str) {
234        $addrs = explode(",", $str); //アドレスを配列に入れる
235        foreach ($addrs as $addr) {
236            if (preg_match("/^(.+)<(.+)>$/", $addr, $matches)) {
237                //引数が「名前<メールアドレス>」の場合
238                $mailaddrs[] = mb_encode_mimeheader(trim($matches[1]))." <".trim($matches[2]).">";
239            } else {
240                //メールアドレスのみの場合
241                $mailaddrs[] =  trim($addr);
242            }
243        }
244        return implode(", ", $mailaddrs); //複数アドレスはカンマ区切りにする
245    }
246}
247?>
Note: See TracBrowser for help on using the repository browser.