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 | */ |
---|
31 | class 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 | * 呼び出し元関数名を返します |
---|
125 | * |
---|
126 | * @param int $forLogInfo ログ出力用に利用するかどうか(1:ログ出力用に利用する) |
---|
127 | * @return string 呼び出し元クラス、関数名、行数の文字列表現 |
---|
128 | */ |
---|
129 | function gfGetCallerInfo($forLogInfo=true) { |
---|
130 | // バックトレースを取得する |
---|
131 | $traces = debug_backtrace(false); |
---|
132 | $bklv = 1; |
---|
133 | if ($forLogInfo === true) { |
---|
134 | $bklv = 3; |
---|
135 | if( ($traces[3]['class'] === 'LC_Page' |
---|
136 | || $traces[3]['class'] === 'LC_Page_Admin') |
---|
137 | && $traces[3]['function'] === 'log') |
---|
138 | { |
---|
139 | $bklv = 4; |
---|
140 | } |
---|
141 | } |
---|
142 | $str = $traces[$bklv]['class'] . "::" . $traces[$bklv]['function'] . "(" . $traces[$bklv-1]['line'] . ") "; |
---|
143 | return $str; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * ログメッセージに、呼び出し元関数名等の情報を付加して返します |
---|
148 | * |
---|
149 | * @param string $mess ログメッセージ |
---|
150 | * @param string $log_level ログレベル("Info" or "Debug") |
---|
151 | * @return string ログメッセージに呼び出し元関数名等の情報を付加した文字列 |
---|
152 | */ |
---|
153 | function gfGetLogStr($mess, $log_level='Info') { |
---|
154 | // メッセージの前に、ログ出力元関数名とログ出力関数呼び出し部分の行数を付与 |
---|
155 | $mess = GC_Utils::gfGetCallerInfo(true) . $mess; |
---|
156 | |
---|
157 | // ログレベル=Debugの場合は、[Debug]を先頭に付与する |
---|
158 | if ($log_level === 'Debug') { |
---|
159 | $mess = "[Debug]" . $mess; |
---|
160 | } |
---|
161 | |
---|
162 | return $mess; |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * 管理画面用ログ出力 |
---|
167 | * |
---|
168 | * 管理画面用ログ出力を行ないます |
---|
169 | * @param string $mess ログメッセージ |
---|
170 | * @param string $log_level ログレベル("Info" or "Debug") |
---|
171 | * @return void |
---|
172 | */ |
---|
173 | function gfAdminLog($mess, $log_level='Info') { |
---|
174 | // ログレベル=Debugの場合は、DEBUG_MODEがtrueの場合のみログ出力する |
---|
175 | if ($log_level === 'Debug'&& DEBUG_MODE === false) { |
---|
176 | return; |
---|
177 | } |
---|
178 | |
---|
179 | // ログメッセージに、呼び出し元関数名等の情報を付加する |
---|
180 | $mess = GC_Utils::gfGetLogStr($mess, $log_level); |
---|
181 | |
---|
182 | // ログ出力 |
---|
183 | // ※現在は管理画面用・フロント用のログ出力とも、同じファイル(site.log)に出力します。 |
---|
184 | // 分けたい場合は、以下の関数呼び出しの第2引数にファイルパスを指定してください |
---|
185 | GC_Utils_Ex::gfPrintLog($mess); |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * フロント用ログ出力 |
---|
190 | * |
---|
191 | * フロント用ログ出力を行ないます |
---|
192 | * @param string $mess ログメッセージ |
---|
193 | * @param string $log_level ログレベル("Info" or "Debug") |
---|
194 | * @return void |
---|
195 | */ |
---|
196 | function gfFrontLog($mess, $log_level='Info') { |
---|
197 | // ログレベル=Debugの場合は、DEBUG_MODEがtrueの場合のみログ出力する |
---|
198 | if ($log_level === 'Debug'&& DEBUG_MODE === false) { |
---|
199 | return; |
---|
200 | } |
---|
201 | |
---|
202 | // ログメッセージに、呼び出し元関数名等の情報を付加する |
---|
203 | $mess = GC_Utils::gfGetLogStr($mess, $log_level); |
---|
204 | |
---|
205 | // ログ出力 |
---|
206 | // ※現在は管理画面用・フロント用のログ出力とも、同じファイル(site.log)に出力します。 |
---|
207 | // 分けたい場合は、以下の関数呼び出しの第2引数にファイルパスを指定してください |
---|
208 | GC_Utils_Ex::gfPrintLog($mess); |
---|
209 | } |
---|
210 | |
---|
211 | /*---------------------------------------------------------------------- |
---|
212 | * [名称] gfPrintLog |
---|
213 | * [概要] ログファイルに日時、処理ファイル名、メッセージを出力 |
---|
214 | * [引数] 表示したいメッセージ |
---|
215 | * [戻値] なし |
---|
216 | * [依存] なし |
---|
217 | * [注釈] - |
---|
218 | *----------------------------------------------------------------------*/ |
---|
219 | function gfPrintLog($mess, $path = '') { |
---|
220 | // 日付の取得 |
---|
221 | $today = date("Y/m/d H:i:s"); |
---|
222 | // 出力パスの作成 |
---|
223 | if ($path == "") { |
---|
224 | $path = LOG_REALFILE; |
---|
225 | } |
---|
226 | |
---|
227 | // エスケープされている文字をもとに戻す |
---|
228 | $trans_tbl = get_html_translation_table (HTML_ENTITIES); |
---|
229 | $trans_tbl = array_flip ($trans_tbl); |
---|
230 | $mess = strtr($mess, $trans_tbl); |
---|
231 | |
---|
232 | $fp = fopen($path, "a+"); |
---|
233 | if ($fp) { |
---|
234 | fwrite($fp, $today." [".$_SERVER['PHP_SELF']."] ".$mess." from ". $_SERVER['REMOTE_ADDR']. "\n"); |
---|
235 | fclose($fp); |
---|
236 | } |
---|
237 | |
---|
238 | // ログテーション |
---|
239 | GC_Utils_Ex::gfLogRotation(MAX_LOG_QUANTITY, MAX_LOG_SIZE, $path); |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * ログローテーション機能 |
---|
244 | * |
---|
245 | * XXX この類のローテーションは通常 0 開始だが、本実装は 1 開始である。 |
---|
246 | * この中でログ出力は行なわないこと。(無限ループの懸念あり) |
---|
247 | * @param integer $max_log 最大ファイル数 |
---|
248 | * @param integer $max_size 最大サイズ |
---|
249 | * @param string $path ファイルパス |
---|
250 | * @return void |
---|
251 | */ |
---|
252 | function gfLogRotation($max_log, $max_size, $path) { |
---|
253 | |
---|
254 | // ファイルが存在しない場合、終了 |
---|
255 | if (!file_exists($path)) return; |
---|
256 | |
---|
257 | // ファイルが最大サイズを超えていない場合、終了 |
---|
258 | if (filesize($path) <= $max_size) return; |
---|
259 | |
---|
260 | // Windows 版 PHP への対策として明示的に事前削除 |
---|
261 | $path_max = "$path.$max_log"; |
---|
262 | if (file_exists($path_max)) { |
---|
263 | $res = unlink($path_max); |
---|
264 | // 削除に失敗時した場合、ログローテーションは見送り |
---|
265 | if (!$res) return; |
---|
266 | } |
---|
267 | |
---|
268 | // アーカイブのインクリメント |
---|
269 | for ($i = $max_log; $i >= 2; $i--) { |
---|
270 | $path_old = "$path." . ($i - 1); |
---|
271 | $path_new = "$path.$i"; |
---|
272 | if (file_exists($path_old)) { |
---|
273 | rename($path_old, $path_new); |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | // 現在ファイルのアーカイブ |
---|
278 | rename($path, "$path.1"); |
---|
279 | } |
---|
280 | |
---|
281 | /*---------------------------------------------------------------------- |
---|
282 | * [名称] gfMakePassword |
---|
283 | * [概要] ランダムパスワード生成(英数字) |
---|
284 | * [引数] パスワードの桁数 |
---|
285 | * [戻値] ランダム生成されたパスワード |
---|
286 | * [依存] なし |
---|
287 | * [注釈] - |
---|
288 | *----------------------------------------------------------------------*/ |
---|
289 | function gfMakePassword($pwLength) { |
---|
290 | |
---|
291 | // 乱数表のシードを決定 |
---|
292 | srand((double)microtime() * 54234853); |
---|
293 | |
---|
294 | // パスワード文字列の配列を作成 |
---|
295 | $character = "abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345679"; |
---|
296 | $pw = preg_split("//", $character, 0, PREG_SPLIT_NO_EMPTY); |
---|
297 | |
---|
298 | $password = ""; |
---|
299 | for ($i = 0; $i<$pwLength; $i++) { |
---|
300 | $password .= $pw[array_rand($pw, 1)]; |
---|
301 | } |
---|
302 | |
---|
303 | return $password; |
---|
304 | } |
---|
305 | |
---|
306 | /*---------------------------------------------------------------------------------------------------------------------- |
---|
307 | * [名称] gfMailHeaderAddr |
---|
308 | * [概要] 入力されたメールアドレスをメール関数用の宛先に変換 |
---|
309 | * [引数] 「メールアドレス」または「名前<メールアドレス>」、複数アドレス指定時はカンマ区切りで指定する。 |
---|
310 | * [戻値] 「メールアドレス」または「JIS_MIMEにコード変換した名前 <メールアドレス>」、複数アドレス指定時はカンマ区切りで返却する。 |
---|
311 | * [依存] なし |
---|
312 | * [注釈] - |
---|
313 | *----------------------------------------------------------------------------------------------------------------------*/ |
---|
314 | |
---|
315 | function gfMailHeaderAddr($str) { |
---|
316 | $addrs = explode(",", $str); //アドレスを配列に入れる |
---|
317 | foreach ($addrs as $addr) { |
---|
318 | if (preg_match("/^(.+)<(.+)>$/", $addr, $matches)) { |
---|
319 | //引数が「名前<メールアドレス>」の場合 |
---|
320 | $mailaddrs[] = mb_encode_mimeheader(trim($matches[1]))." <".trim($matches[2]).">"; |
---|
321 | } else { |
---|
322 | //メールアドレスのみの場合 |
---|
323 | $mailaddrs[] = trim($addr); |
---|
324 | } |
---|
325 | } |
---|
326 | return implode(", ", $mailaddrs); //複数アドレスはカンマ区切りにする |
---|
327 | } |
---|
328 | } |
---|