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