| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2013 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 | * このクラスはエラーハンドリング処理でも使用している。 |
|---|
| 28 | * よって、このファイルで構文エラーが発生すると、EC-CUBE はエラーを捕捉できない。 |
|---|
| 29 | * @package Util |
|---|
| 30 | * @author LOCKON CO.,LTD. |
|---|
| 31 | * @version $Id$ |
|---|
| 32 | */ |
|---|
| 33 | class GC_Utils { |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * ログファイルに変数の詳細を出力 |
|---|
| 37 | * |
|---|
| 38 | * @param mixed $obj |
|---|
| 39 | * @return void |
|---|
| 40 | */ |
|---|
| 41 | function gfDebugLog($obj) { |
|---|
| 42 | if (USE_VERBOSE_LOG === true) { |
|---|
| 43 | $msg = "DEBUG\n" |
|---|
| 44 | . print_r($obj, true); |
|---|
| 45 | GC_Utils_Ex::gfPrintLog($msg, DEBUG_LOG_REALFILE); |
|---|
| 46 | } |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * 呼び出し元関数名を返します |
|---|
| 51 | * |
|---|
| 52 | * @param int $forLogInfo ログ出力用に利用するかどうか(1:ログ出力用に利用する) |
|---|
| 53 | * @return string 呼び出し元クラス、関数名、行数の文字列表現 |
|---|
| 54 | */ |
|---|
| 55 | function gfGetCallerInfo($forLogInfo = true) { |
|---|
| 56 | // バックトレースを取得する |
|---|
| 57 | $traces = debug_backtrace(false); |
|---|
| 58 | $bklv = 1; |
|---|
| 59 | if ($forLogInfo === true) { |
|---|
| 60 | $bklv = 3; |
|---|
| 61 | if (($traces[3]['class'] === 'LC_Page' || $traces[3]['class'] === 'LC_Page_Admin') |
|---|
| 62 | && $traces[3]['function'] === 'log' |
|---|
| 63 | ) { |
|---|
| 64 | $bklv = 4; |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | $str = $traces[$bklv]['class'] . '::' . $traces[$bklv]['function'] . '(' . $traces[$bklv - 1]['line'] . ') '; |
|---|
| 68 | return $str; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * デバッグ情報として必要な範囲のバックトレースを取得する |
|---|
| 73 | * |
|---|
| 74 | * エラーハンドリングに関わる情報を切り捨てる。 |
|---|
| 75 | */ |
|---|
| 76 | function getDebugBacktrace($arrBacktrace = null) { |
|---|
| 77 | if (is_null($arrBacktrace)) { |
|---|
| 78 | $arrBacktrace = debug_backtrace(false); |
|---|
| 79 | } |
|---|
| 80 | $arrReturn = array(); |
|---|
| 81 | foreach (array_reverse($arrBacktrace) as $arrLine) { |
|---|
| 82 | // 言語レベルの致命的エラー時。発生元の情報はトレースできない。(エラーハンドリング処理のみがトレースされる) |
|---|
| 83 | // 実質的に何も返さない(空配列を返す)意図。 |
|---|
| 84 | if (strlen($arrLine['file']) === 0 |
|---|
| 85 | && ($arrLine['class'] === 'SC_Helper_HandleError' || $arrLine['class'] === 'SC_Helper_HandleError_Ex') |
|---|
| 86 | && ($arrLine['function'] === 'handle_error' || $arrLine['function'] === 'handle_warning') |
|---|
| 87 | ) { |
|---|
| 88 | break 1; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | $arrReturn[] = $arrLine; |
|---|
| 92 | |
|---|
| 93 | // エラーハンドリング処理に引き渡した以降の情報は通常不要なので含めない。 |
|---|
| 94 | if (!isset($arrLine['class']) && $arrLine['function'] === 'trigger_error') { |
|---|
| 95 | break 1; |
|---|
| 96 | } |
|---|
| 97 | if (($arrLine['class'] === 'SC_Helper_HandleError' || $arrLine['class'] === 'SC_Helper_HandleError_Ex') |
|---|
| 98 | && ($arrLine['function'] === 'handle_error' || $arrLine['function'] === 'handle_warning') |
|---|
| 99 | ) { |
|---|
| 100 | break 1; |
|---|
| 101 | } |
|---|
| 102 | if (($arrLine['class'] === 'SC_Utils' || $arrLine['class'] === 'SC_Utils_Ex') |
|---|
| 103 | && $arrLine['function'] === 'sfDispException' |
|---|
| 104 | ) { |
|---|
| 105 | break 1; |
|---|
| 106 | } |
|---|
| 107 | if (($arrLine['class'] === 'GC_Utils' || $arrLine['class'] === 'GC_Utils_Ex') |
|---|
| 108 | && ($arrLine['function'] === 'gfDebugLog' || $arrLine['function'] === 'gfPrintLog') |
|---|
| 109 | ) { |
|---|
| 110 | break 1; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | return array_reverse($arrReturn); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | /** |
|---|
| 117 | * 前方互換用 |
|---|
| 118 | * |
|---|
| 119 | * @deprecated 2.12.0 |
|---|
| 120 | */ |
|---|
| 121 | function gfGetLogStr($mess, $log_level = 'Info') { |
|---|
| 122 | trigger_error('前方互換用メソッドが使用されました。', E_USER_WARNING); |
|---|
| 123 | // メッセージの前に、ログ出力元関数名とログ出力関数呼び出し部分の行数を付与 |
|---|
| 124 | $mess = GC_Utils::gfGetCallerInfo(true) . $mess; |
|---|
| 125 | |
|---|
| 126 | // ログレベル=Debugの場合は、[Debug]を先頭に付与する |
|---|
| 127 | if ($log_level === 'Debug') { |
|---|
| 128 | $mess = '[Debug]' . $mess; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | return $mess; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * 前方互換用 |
|---|
| 136 | * |
|---|
| 137 | * @deprecated 2.12.0 GC_Utils_Ex::gfPrintLog を使用すること |
|---|
| 138 | */ |
|---|
| 139 | function gfAdminLog($mess, $log_level = 'Info') { |
|---|
| 140 | trigger_error('前方互換用メソッドが使用されました。', E_USER_WARNING); |
|---|
| 141 | // ログレベル=Debugの場合は、DEBUG_MODEがtrueの場合のみログ出力する |
|---|
| 142 | if ($log_level === 'Debug' && DEBUG_MODE === false) { |
|---|
| 143 | return; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | // ログ出力 |
|---|
| 147 | GC_Utils_Ex::gfPrintLog($mess, '', true); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | /** |
|---|
| 151 | * 前方互換用 |
|---|
| 152 | * |
|---|
| 153 | * @deprecated 2.12.0 GC_Utils_Ex::gfPrintLog を使用すること |
|---|
| 154 | */ |
|---|
| 155 | function gfFrontLog($mess, $log_level = 'Info') { |
|---|
| 156 | trigger_error('前方互換用メソッドが使用されました。', E_USER_WARNING); |
|---|
| 157 | // ログレベル=Debugの場合は、DEBUG_MODEがtrueの場合のみログ出力する |
|---|
| 158 | if ($log_level === 'Debug' && DEBUG_MODE === false) { |
|---|
| 159 | return; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | // ログ出力 |
|---|
| 163 | GC_Utils_Ex::gfPrintLog($mess, '', true); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | /** |
|---|
| 167 | * ログの出力を行う |
|---|
| 168 | * |
|---|
| 169 | * エラー・警告は trigger_error() を経由して利用すること。(補足の出力は例外。) |
|---|
| 170 | * @param string $msg |
|---|
| 171 | * @param string $path |
|---|
| 172 | * @param bool $verbose 冗長な出力を行うか |
|---|
| 173 | */ |
|---|
| 174 | function gfPrintLog($msg, $path = '', $verbose = USE_VERBOSE_LOG) { |
|---|
| 175 | // 日付の取得 |
|---|
| 176 | $today = date('Y/m/d H:i:s'); |
|---|
| 177 | // 出力パスの作成 |
|---|
| 178 | |
|---|
| 179 | if (strlen($path) === 0) { |
|---|
| 180 | $path = GC_Utils_Ex::isAdminFunction() ? ADMIN_LOG_REALFILE : LOG_REALFILE; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | $msg = "$today [{$_SERVER['SCRIPT_NAME']}] $msg from {$_SERVER['REMOTE_ADDR']}\n"; |
|---|
| 184 | if ($verbose) { |
|---|
| 185 | if (GC_Utils_Ex::isFrontFunction()) { |
|---|
| 186 | $msg .= 'customer_id = ' . $_SESSION['customer']['customer_id'] . "\n"; |
|---|
| 187 | } |
|---|
| 188 | if (GC_Utils_Ex::isAdminFunction()) { |
|---|
| 189 | $msg .= 'login_id = ' . $_SESSION['login_id'] . '(' . $_SESSION['authority'] . ')' . '[' . session_id() . ']' . "\n"; |
|---|
| 190 | } |
|---|
| 191 | $msg .= GC_Utils_Ex::toStringBacktrace(GC_Utils_Ex::getDebugBacktrace()); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | error_log($msg, 3, $path); |
|---|
| 195 | |
|---|
| 196 | // ログテーション |
|---|
| 197 | GC_Utils_Ex::gfLogRotation(MAX_LOG_QUANTITY, MAX_LOG_SIZE, $path); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | /** |
|---|
| 201 | * ログローテーション機能 |
|---|
| 202 | * |
|---|
| 203 | * XXX この類のローテーションは通常 0 開始だが、本実装は 1 開始である。 |
|---|
| 204 | * この中でログ出力は行なわないこと。(無限ループの懸念あり) |
|---|
| 205 | * @param integer $max_log 最大ファイル数 |
|---|
| 206 | * @param integer $max_size 最大サイズ |
|---|
| 207 | * @param string $path ファイルパス |
|---|
| 208 | * @return void |
|---|
| 209 | */ |
|---|
| 210 | function gfLogRotation($max_log, $max_size, $path) { |
|---|
| 211 | |
|---|
| 212 | // ファイルが存在しない場合、終了 |
|---|
| 213 | if (!file_exists($path)) return; |
|---|
| 214 | |
|---|
| 215 | // ファイルが最大サイズを超えていない場合、終了 |
|---|
| 216 | if (filesize($path) <= $max_size) return; |
|---|
| 217 | |
|---|
| 218 | // Windows 版 PHP への対策として明示的に事前削除 |
|---|
| 219 | $path_max = "$path.$max_log"; |
|---|
| 220 | if (file_exists($path_max)) { |
|---|
| 221 | $res = unlink($path_max); |
|---|
| 222 | // 削除に失敗時した場合、ログローテーションは見送り |
|---|
| 223 | if (!$res) return; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | // アーカイブのインクリメント |
|---|
| 227 | for ($i = $max_log; $i >= 2; $i--) { |
|---|
| 228 | $path_old = "$path." . ($i - 1); |
|---|
| 229 | $path_new = "$path.$i"; |
|---|
| 230 | if (file_exists($path_old)) { |
|---|
| 231 | rename($path_old, $path_new); |
|---|
| 232 | } |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | // 現在ファイルのアーカイブ |
|---|
| 236 | rename($path, "$path.1"); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | /*---------------------------------------------------------------------- |
|---|
| 240 | * [名称] gfMakePassword |
|---|
| 241 | * [概要] ランダムパスワード生成(英数字) |
|---|
| 242 | * [引数] パスワードの桁数 |
|---|
| 243 | * [戻値] ランダム生成されたパスワード |
|---|
| 244 | * [依存] なし |
|---|
| 245 | * [注釈] - |
|---|
| 246 | *----------------------------------------------------------------------*/ |
|---|
| 247 | function gfMakePassword($pwLength) { |
|---|
| 248 | |
|---|
| 249 | // 乱数表のシードを決定 |
|---|
| 250 | srand((double)microtime() * 54234853); |
|---|
| 251 | |
|---|
| 252 | // パスワード文字列の配列を作成 |
|---|
| 253 | $character = 'abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345679'; |
|---|
| 254 | $pw = preg_split('//', $character, 0, PREG_SPLIT_NO_EMPTY); |
|---|
| 255 | |
|---|
| 256 | $password = ''; |
|---|
| 257 | for ($i = 0; $i<$pwLength; $i++) { |
|---|
| 258 | $password .= $pw[array_rand($pw, 1)]; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | return $password; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | /*---------------------------------------------------------------------------------------------------------------------- |
|---|
| 265 | * [名称] gfMailHeaderAddr |
|---|
| 266 | * [概要] 入力されたメールアドレスをメール関数用の宛先に変換 |
|---|
| 267 | * [引数] 「メールアドレス」または「名前<メールアドレス>」、複数アドレス指定時はカンマ区切りで指定する。 |
|---|
| 268 | * [戻値] 「メールアドレス」または「JIS_MIMEにコード変換した名前 <メールアドレス>」、複数アドレス指定時はカンマ区切りで返却する。 |
|---|
| 269 | * [依存] なし |
|---|
| 270 | * [注釈] - |
|---|
| 271 | *----------------------------------------------------------------------------------------------------------------------*/ |
|---|
| 272 | |
|---|
| 273 | function gfMailHeaderAddr($str) { |
|---|
| 274 | $addrs = explode(',', $str); //アドレスを配列に入れる |
|---|
| 275 | $mailaddrs = array(); |
|---|
| 276 | foreach ($addrs as $addr) { |
|---|
| 277 | if (preg_match("/^(.+)<(.+)>$/", $addr, $matches)) { |
|---|
| 278 | //引数が「名前<メールアドレス>」の場合 |
|---|
| 279 | $mailaddrs[] = mb_encode_mimeheader(trim($matches[1])).' <'.trim($matches[2]).'>'; |
|---|
| 280 | } else { |
|---|
| 281 | //メールアドレスのみの場合 |
|---|
| 282 | $mailaddrs[] = trim($addr); |
|---|
| 283 | } |
|---|
| 284 | } |
|---|
| 285 | return implode(', ', $mailaddrs); //複数アドレスはカンマ区切りにする |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | /** |
|---|
| 289 | * バックトレースをテキスト形式で出力する |
|---|
| 290 | * |
|---|
| 291 | * 現状スタックトレースの形で出力している。 |
|---|
| 292 | * @param array $arrBacktrace バックトレース |
|---|
| 293 | * @return string テキストで表現したバックトレース |
|---|
| 294 | */ |
|---|
| 295 | function toStringBacktrace($arrBacktrace) { |
|---|
| 296 | $string = ''; |
|---|
| 297 | |
|---|
| 298 | foreach (array_reverse($arrBacktrace) as $backtrace) { |
|---|
| 299 | if (strlen($backtrace['class']) >= 1) { |
|---|
| 300 | $func = $backtrace['class'] . $backtrace['type'] . $backtrace['function']; |
|---|
| 301 | } else { |
|---|
| 302 | $func = $backtrace['function']; |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | $string .= $backtrace['file'] . '(' . $backtrace['line'] . '): ' . $func . "\n"; |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | return $string; |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | /** |
|---|
| 312 | * エラー型から該当する定数名を取得する |
|---|
| 313 | * |
|---|
| 314 | * 該当する定数がない場合、$error_type を返す。 |
|---|
| 315 | * @param integer $error_type エラー型 |
|---|
| 316 | * @return string|integer エラー定数名 |
|---|
| 317 | */ |
|---|
| 318 | function getErrorTypeName($error_type) { |
|---|
| 319 | $arrDefinedConstants = get_defined_constants(true); |
|---|
| 320 | |
|---|
| 321 | // PHP の歴史対応 |
|---|
| 322 | $arrDefinedCoreConstants = array(); |
|---|
| 323 | // PHP >= 5.3.1, PHP == 5.3.0 (not Windows) |
|---|
| 324 | if (isset($arrDefinedConstants['Core'])) { |
|---|
| 325 | $arrDefinedCoreConstants = $arrDefinedConstants['Core']; |
|---|
| 326 | } |
|---|
| 327 | // PHP < 5.3.0 |
|---|
| 328 | elseif (isset($arrDefinedConstants['internal'])) { |
|---|
| 329 | $arrDefinedCoreConstants = $arrDefinedConstants['internal']; |
|---|
| 330 | } |
|---|
| 331 | // PHP == 5.3.0 (Windows) |
|---|
| 332 | elseif (isset($arrDefinedConstants['mhash'])) { |
|---|
| 333 | $arrDefinedCoreConstants = $arrDefinedConstants['mhash']; |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | foreach ($arrDefinedCoreConstants as $constant_name => $constant_value) { |
|---|
| 337 | if (substr($constant_name, 0, 2) === 'E_' && $constant_value == $error_type) { |
|---|
| 338 | return $constant_name; |
|---|
| 339 | } |
|---|
| 340 | } |
|---|
| 341 | return $error_type; |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | /** |
|---|
| 345 | * 現在の URL を取得する |
|---|
| 346 | * |
|---|
| 347 | * @return string 現在のURL |
|---|
| 348 | */ |
|---|
| 349 | function getUrl() { |
|---|
| 350 | $url = ''; |
|---|
| 351 | |
|---|
| 352 | if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { |
|---|
| 353 | $url = 'https://'; |
|---|
| 354 | } else { |
|---|
| 355 | $url = 'http://'; |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
|---|
| 359 | if (strlen($_SERVER['QUERY_STRING']) >= 1) { |
|---|
| 360 | $url .= '?' . $_SERVER['QUERY_STRING']; |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | return $url; |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | /** |
|---|
| 367 | * 管理機能かを判定 |
|---|
| 368 | * |
|---|
| 369 | * @return bool 管理機能か |
|---|
| 370 | */ |
|---|
| 371 | function isAdminFunction() { |
|---|
| 372 | return defined('ADMIN_FUNCTION') && ADMIN_FUNCTION === true; |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | /** |
|---|
| 376 | * フロント機能かを判定 |
|---|
| 377 | * |
|---|
| 378 | * @return bool フロント機能か |
|---|
| 379 | */ |
|---|
| 380 | function isFrontFunction() { |
|---|
| 381 | return defined('FRONT_FUNCTION') && FRONT_FUNCTION === true; |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | /** |
|---|
| 385 | * インストール機能かを判定 |
|---|
| 386 | * |
|---|
| 387 | * @return bool インストール機能か |
|---|
| 388 | */ |
|---|
| 389 | function isInstallFunction() { |
|---|
| 390 | return defined('INSTALL_FUNCTION') && INSTALL_FUNCTION === true; |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | /** |
|---|
| 394 | * XML宣言を出力する. |
|---|
| 395 | * |
|---|
| 396 | * XML宣言があると問題が発生する UA は出力しない. |
|---|
| 397 | * |
|---|
| 398 | * @return string XML宣言の文字列 |
|---|
| 399 | */ |
|---|
| 400 | function printXMLDeclaration() { |
|---|
| 401 | $ua = $_SERVER['HTTP_USER_AGENT']; |
|---|
| 402 | if (!preg_match('/MSIE/', $ua) || preg_match('/MSIE 7/', $ua)) { |
|---|
| 403 | echo '<?xml version="1.0" encoding="' . CHAR_CODE . '"?>' . "\n"; |
|---|
| 404 | } |
|---|
| 405 | } |
|---|
| 406 | } |
|---|