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