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