| 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 | */ |
|---|
| 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 | $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 | * |
|---|
| 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 | $dirname = dirname($path); |
|---|
| 172 | // ファイル名を取得 |
|---|
| 173 | $basename = basename($path); |
|---|
| 174 | |
|---|
| 175 | // ファイルが最大サイズを超えていないかチェック |
|---|
| 176 | if(filesize($path) > $max_size) { |
|---|
| 177 | if ($dh = opendir($dirname)) { |
|---|
| 178 | while (($file = readdir($dh)) !== false) { |
|---|
| 179 | // ログローテーションにて作成されたファイルを取得 |
|---|
| 180 | if(ereg("^". $basename . "\." , $file)) { |
|---|
| 181 | $arrFile[] = $file; |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | // ローテーションにて作成されたログファイルが存在しない場合は実行しない |
|---|
| 186 | if(is_array($arrFile)) { |
|---|
| 187 | // ソートを行う |
|---|
| 188 | $arrLog = natcasesort($arrFile); |
|---|
| 189 | |
|---|
| 190 | // ファイルログが最大個数なら以上なら古いファイルから削除する |
|---|
| 191 | $count = count($arrLog); |
|---|
| 192 | if($count >= $max_log) { |
|---|
| 193 | $diff = $count - $max_log; |
|---|
| 194 | for($i = 0; $diff >= $i ; $i++) { |
|---|
| 195 | unlink($dirname. "/" .array_pop($arrLog)); |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | // ログファイルの添え字をずらす |
|---|
| 200 | $count = count($arrLog); |
|---|
| 201 | for($i = $count; 1 <= $i; $i--) { |
|---|
| 202 | $move_number = $i + 1; |
|---|
| 203 | if(file_exists("$path.$move_number")) unlink("$path.$move_number"); |
|---|
| 204 | copy("$dirname/" . $arrLog[$i - 1], "$path.$move_number"); |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | $ret = copy($path, "$path.1"); |
|---|
| 208 | |
|---|
| 209 | // 新規ログファイルを作成 |
|---|
| 210 | if($ret) { |
|---|
| 211 | unlink($path); |
|---|
| 212 | touch($path); |
|---|
| 213 | chmod($path, 0666); |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | /*---------------------------------------------------------------------- |
|---|
| 220 | * [名称] gfMakePassword |
|---|
| 221 | * [概要] ランダムパスワード生成(英数字) |
|---|
| 222 | * [引数] パスワードの桁数 |
|---|
| 223 | * [戻値] ランダム生成されたパスワード |
|---|
| 224 | * [依存] なし |
|---|
| 225 | * [注釈] - |
|---|
| 226 | *----------------------------------------------------------------------*/ |
|---|
| 227 | function gfMakePassword($pwLength) { |
|---|
| 228 | |
|---|
| 229 | // 乱数表のシードを決定 |
|---|
| 230 | srand((double)microtime() * 54234853); |
|---|
| 231 | |
|---|
| 232 | // パスワード文字列の配列を作成 |
|---|
| 233 | $character = "abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345679"; |
|---|
| 234 | $pw = preg_split("//", $character, 0, PREG_SPLIT_NO_EMPTY); |
|---|
| 235 | |
|---|
| 236 | $password = ""; |
|---|
| 237 | for($i = 0; $i<$pwLength; $i++ ) { |
|---|
| 238 | $password .= $pw[array_rand($pw, 1)]; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | return $password; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | /*---------------------------------------------------------------------- |
|---|
| 245 | * [名称] sf_explodeExt |
|---|
| 246 | * [概要] ファイルの拡張子取得 |
|---|
| 247 | * [引数] ファイル名 |
|---|
| 248 | * [戻値] 拡張子 |
|---|
| 249 | * [依存] なし |
|---|
| 250 | * [注釈] - |
|---|
| 251 | *----------------------------------------------------------------------*/ |
|---|
| 252 | function gf_explodeExt($fileName) { |
|---|
| 253 | $ext1 = explode(".", $fileName); |
|---|
| 254 | $ext2 = $ext1[count($ext1) - 1]; |
|---|
| 255 | $ext2 = strtolower($ext2); |
|---|
| 256 | return $ext2; |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | /*---------------------------------------------------------------------------------------------------------------------- |
|---|
| 261 | * [名称] gfMailHeaderAddr |
|---|
| 262 | * [概要] 入力されたメールアドレスをメール関数用の宛先に変換 |
|---|
| 263 | * [引数] 「メールアドレス」または「名前<メールアドレス>」、複数アドレス指定時はカンマ区切りで指定する。 |
|---|
| 264 | * [戻値] 「メールアドレス」または「JIS_MIMEにコード変換した名前 <メールアドレス>」、複数アドレス指定時はカンマ区切りで返却する。 |
|---|
| 265 | * [依存] なし |
|---|
| 266 | * [注釈] - |
|---|
| 267 | *----------------------------------------------------------------------------------------------------------------------*/ |
|---|
| 268 | |
|---|
| 269 | function gfMailHeaderAddr($str) { |
|---|
| 270 | $addrs = explode(",", $str); //アドレスを配列に入れる |
|---|
| 271 | foreach ($addrs as $addr) { |
|---|
| 272 | if (preg_match("/^(.+)<(.+)>$/", $addr, $matches)) { |
|---|
| 273 | //引数が「名前<メールアドレス>」の場合 |
|---|
| 274 | $mailaddrs[] = mb_encode_mimeheader(trim($matches[1]))." <".trim($matches[2]).">"; |
|---|
| 275 | } else { |
|---|
| 276 | //メールアドレスのみの場合 |
|---|
| 277 | $mailaddrs[] = trim($addr); |
|---|
| 278 | } |
|---|
| 279 | } |
|---|
| 280 | return implode(", ", $mailaddrs); //複数アドレスはカンマ区切りにする |
|---|
| 281 | } |
|---|
| 282 | } |
|---|
| 283 | ?> |
|---|