source: branches/version-2_13_0/data/class/SC_Initial.php @ 23127

Revision 23127, 19.2 KB checked in by lockon_committer, 11 years ago (diff)

#2352 ECCUBE_VERSIONの更新(2.13.0-dev)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
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 * @author LOCKON CO.,LTD.
28 * @version $Id$
29 */
30class SC_Initial
31{
32    /**
33     * コンストラクタ.
34     */
35    public function __construct()
36    {
37        /** EC-CUBEのバージョン */
38        define('ECCUBE_VERSION', '2.13.0-dev');
39    }
40
41    /**
42     * 初期設定を行う.
43     *
44     * @access protected
45     * @return void
46     */
47    public function init()
48    {
49        $this->requireInitialConfig();
50        $this->defineDSN();                 // requireInitialConfig メソッドより後で実行
51        $this->defineDirectoryIndex();
52        $this->defineConstants();
53        $this->defineParameter();           // defineDirectoryIndex メソッドより後で実行
54        $this->complementParameter();       // defineConstants メソッドより後で実行
55        $this->phpconfigInit();             // defineConstants メソッドより後で実行
56        $this->createCacheDir();            // defineConstants メソッドより後で実行
57        $this->stripslashesDeepGpc();
58        $this->resetSuperglobalsRequest();  // stripslashesDeepGpc メソッドより後で実行
59        $this->setTimezone();               // 本当はエラーハンドラーより先に読みたい気も
60        $this->normalizeHostname();         // defineConstants メソッドより後で実行
61    }
62
63    /**
64     * 初期設定ファイルを読み込み, パスの設定を行う.
65     *
66     * @access protected
67     * @return void
68     */
69    public function requireInitialConfig()
70    {
71        define('CONFIG_REALFILE', realpath(dirname(__FILE__)) . '/../config/config.php');
72        if (file_exists(CONFIG_REALFILE)) {
73            require_once CONFIG_REALFILE;
74        }
75    }
76
77    /**
78     * DSN を定義する.
79     *
80     * @access protected
81     * @return void
82     * @deprecated 下位互換用
83     */
84    public function defineDSN()
85    {
86        if (defined('DB_TYPE') && defined('DB_USER') && defined('DB_PASSWORD')
87            && defined('DB_SERVER') && defined('DB_PORT') && defined('DB_NAME')
88        ) {
89            $dsn = DB_TYPE . '://' . DB_USER . ':' . DB_PASSWORD . '@' . DB_SERVER . ':' . DB_PORT . '/' . DB_NAME;
90            /** サイト用DB */
91            // ここで生成した DSN は使用せず, SC_Query のコンストラクタでパラメータを設定する.
92            define('DEFAULT_DSN', $dsn);
93        }
94    }
95
96    /**
97     * @deprecated
98     */
99    public function setErrorReporting()
100    {
101        error_reporting(E_ALL & ~E_NOTICE);
102        // PHP 5.3.0対応
103        if (error_reporting() > 6143) {
104            error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
105        }
106    }
107
108    /**
109     * マルチバイト文字列設定を行う.
110     *
111     * TODO SJIS-win や, eucJP-win への対応
112     *
113     * @access protected
114     * @return void
115     */
116    public function phpconfigInit()
117    {
118        ini_set('html_errors', '1');
119        ini_set('mbstring.http_input', CHAR_CODE);
120        ini_set('mbstring.http_output', CHAR_CODE);
121        ini_set('auto_detect_line_endings', 1);
122        ini_set('default_charset', CHAR_CODE);
123        ini_set('mbstring.detect_order', 'auto');
124        ini_set('mbstring.substitute_character', 'none');
125        ini_set('pcre.backtrack_limit', 1000000);
126
127        mb_language('ja'); // mb_internal_encoding() より前に
128        // TODO .htaccess の mbstring.language を削除できないか検討
129
130        mb_internal_encoding(CHAR_CODE); // mb_language() より後で
131
132        ini_set('arg_separator.output', '&');
133
134        //ロケールを明示的に設定
135        $res = setlocale(LC_ALL, LOCALE);
136        if ($res === FALSE) {
137            // TODO: Windows上のロケール設定が正常に働かない場合があることに暫定的に対応
138            // ''を指定するとApache実行環境の環境変数が使われる
139            // See also: http://php.net/manual/ja/function.setlocale.php
140            setlocale(LC_ALL, '');
141        }
142
143        // #1849 (文字エンコーディングの検出を制御する)
144        mb_detect_order(array('UTF-8', 'SJIS-win', 'eucJP-win'));
145    }
146
147    /**
148     * 定数 DIR_INDEX_PATH を設定する.
149     *
150     * @access protected
151     * @return void
152     */
153    public function defineDirectoryIndex()
154    {
155        // DirectoryIndex の実ファイル名
156        if (!defined('DIR_INDEX_FILE')) {
157            define('DIR_INDEX_FILE', 'index.php');
158        }
159
160        $useFilenameDirIndex = is_bool(USE_FILENAME_DIR_INDEX)
161            ? USE_FILENAME_DIR_INDEX
162            : (isset($_SERVER['SERVER_SOFTWARE']) ? substr($_SERVER['SERVER_SOFTWARE'], 0, 13) == 'Microsoft-IIS' : false)
163        ;
164
165        // DIR_INDEX_FILE にアクセスする時の URL のファイル名部を定義する
166        if ($useFilenameDirIndex === true) {
167            // ファイル名を使用する
168            define('DIR_INDEX_PATH', DIR_INDEX_FILE);
169        } else {
170            // ファイル名を使用しない
171            define('DIR_INDEX_PATH', '');
172        }
173    }
174
175    /**
176     * パラメータを設定する.
177     *
178     * mtb_constants.php を読み込んで定数として定義する.
179     * キャッシュディレクトリに存在しない場合は, 初期データからコピーする.
180     *
181     * @access protected
182     * @return void
183     */
184    public function defineParameter()
185    {
186        $errorMessage
187            = '<div style="color: #F00; font-weight: bold; background-color: #FEB; text-align: center">'
188            . CACHE_REALDIR
189            . ' にユーザ書込み権限(777等)を付与して下さい。</div>';
190
191        // 定数を設定
192        if (is_file(CACHE_REALDIR . 'mtb_constants.php')) {
193            require_once CACHE_REALDIR . 'mtb_constants.php';
194
195            // キャッシュが無ければ, 初期データからコピー
196        } elseif (is_file(CACHE_REALDIR . '../mtb_constants_init.php')) {
197            $mtb_constants = file_get_contents(CACHE_REALDIR . '../mtb_constants_init.php');
198            if (is_writable(CACHE_REALDIR)) {
199                $handle = fopen(CACHE_REALDIR . 'mtb_constants.php', 'w');
200                if (!$handle) {
201                    die($errorMessage);
202                }
203                if (fwrite($handle, $mtb_constants) === false) {
204                    die($errorMessage);
205                }
206                fclose($handle);
207
208                require_once CACHE_REALDIR . 'mtb_constants.php';
209            } else {
210                die($errorMessage);
211            }
212        } else {
213            die(CACHE_REALDIR . '../mtb_constants_init.php が存在しません');
214        }
215    }
216
217    /**
218     * パラメーターの補完
219     *
220     * ソースのみ差し替えたバージョンアップを考慮したもの。
221     * $this->defineIfNotDefined() で定義することを想定
222     *
223     * @access protected
224     * @return void
225     */
226    public function complementParameter()
227    {
228    }
229
230    /**
231     * 各種キャッシュディレクトリを生成する.
232     *
233     * Smarty キャッシュディレクトリを生成する.
234     *
235     * @access protected
236     * @return void
237     */
238    public function createCacheDir()
239    {
240        if (defined('HTML_REALDIR')) {
241            umask(0);
242            if (!file_exists(COMPILE_REALDIR)) {
243                mkdir(COMPILE_REALDIR);
244            }
245
246            if (!file_exists(MOBILE_COMPILE_REALDIR)) {
247                mkdir(MOBILE_COMPILE_REALDIR);
248            }
249
250            if (!file_exists(SMARTPHONE_COMPILE_REALDIR)) {
251                mkdir(SMARTPHONE_COMPILE_REALDIR);
252            }
253
254            if (!file_exists(COMPILE_ADMIN_REALDIR)) {
255                mkdir(COMPILE_ADMIN_REALDIR);
256            }
257        }
258    }
259
260    /**
261     * 定数定義
262     *
263     * @access protected
264     * @return void
265     */
266    public function defineConstants()
267    {
268        // LC_Page_Error用
269        /** 指定商品ページがない */
270        define('PRODUCT_NOT_FOUND', 1);
271        /** カート内が空 */
272        define('CART_EMPTY', 2);
273        /** ページ推移エラー */
274        define('PAGE_ERROR', 3);
275        /** 購入処理中のカート商品追加エラー */
276        define('CART_ADD_ERROR', 4);
277        /** 他にも購入手続きが行われた場合 */
278        define('CANCEL_PURCHASE', 5);
279        /** 指定カテゴリページがない */
280        define('CATEGORY_NOT_FOUND', 6);
281        /** ログインに失敗 */
282        define('SITE_LOGIN_ERROR', 7);
283        /** 会員専用ページへのアクセスエラー */
284        define('CUSTOMER_ERROR', 8);
285        /** 購入時の売り切れエラー */
286        define('SOLD_OUT', 9);
287        /** カート内商品の読込エラー */
288        define('CART_NOT_FOUND', 10);
289        /** ポイントの不足 */
290        define('LACK_POINT', 11);
291        /** 仮登録者がログインに失敗 */
292        define('TEMP_LOGIN_ERROR', 12);
293        /** URLエラー */
294        define('URL_ERROR', 13);
295        /** ファイル解凍エラー */
296        define('EXTRACT_ERROR', 14);
297        /** FTPダウンロードエラー */
298        define('FTP_DOWNLOAD_ERROR', 15);
299        /** FTPログインエラー */
300        define('FTP_LOGIN_ERROR', 16);
301        /** FTP接続エラー */
302        define('FTP_CONNECT_ERROR', 17);
303        /** DB作成エラー */
304        define('CREATE_DB_ERROR', 18);
305        /** DBインポートエラー */
306        define('DB_IMPORT_ERROR', 19);
307        /** 設定ファイル存在エラー */
308        define('FILE_NOT_FOUND', 20);
309        /** 書き込みエラー */
310        define('WRITE_FILE_ERROR', 21);
311        /** DB接続エラー */
312        define('DB_CONNECT_ERROR', 22);
313        /** ダウンロードファイル存在エラー */
314        define('DOWNFILE_NOT_FOUND', 22);
315        /** フリーメッセージ */
316        define('FREE_ERROR_MSG', 999);
317
318        // LC_Page_Error_DispError用
319        /** ログイン失敗 */
320        define('LOGIN_ERROR', 1);
321        /** アクセス失敗(タイムアウト等) */
322        define('ACCESS_ERROR', 2);
323        /** アクセス権限違反 */
324        define('AUTH_ERROR', 3);
325        /** 不正な遷移エラー */
326        define('INVALID_MOVE_ERRORR', 4);
327
328        // オーナーズストア通信関連
329        /** オーナーズストア通信ステータス */
330        define('OSTORE_STATUS_ERROR', 'ERROR');
331        /** オーナーズストア通信ステータス */
332        define('OSTORE_STATUS_SUCCESS', 'SUCCESS');
333        /** オーナーズストア通信エラーコード */
334        define('OSTORE_E_UNKNOWN', '1000');
335        /** オーナーズストア通信エラーコード */
336        define('OSTORE_E_INVALID_PARAM', '1001');
337        /** オーナーズストア通信エラーコード */
338        define('OSTORE_E_NO_CUSTOMER', '1002');
339        /** オーナーズストア通信エラーコード */
340        define('OSTORE_E_WRONG_URL_PASS', '1003');
341        /** オーナーズストア通信エラーコード */
342        define('OSTORE_E_NO_PRODUCTS', '1004');
343        /** オーナーズストア通信エラーコード */
344        define('OSTORE_E_NO_DL_DATA', '1005');
345        /** オーナーズストア通信エラーコード */
346        define('OSTORE_E_DL_DATA_OPEN', '1006');
347        /** オーナーズストア通信エラーコード */
348        define('OSTORE_E_DLLOG_AUTH', '1007');
349        /** オーナーズストア通信エラーコード */
350        define('OSTORE_E_C_ADMIN_AUTH', '2001');
351        /** オーナーズストア通信エラーコード */
352        define('OSTORE_E_C_HTTP_REQ', '2002');
353        /** オーナーズストア通信エラーコード */
354        define('OSTORE_E_C_HTTP_RESP', '2003');
355        /** オーナーズストア通信エラーコード */
356        define('OSTORE_E_C_FAILED_JSON_PARSE', '2004');
357        /** オーナーズストア通信エラーコード */
358        define('OSTORE_E_C_NO_KEY', '2005');
359        /** オーナーズストア通信エラーコード */
360        define('OSTORE_E_C_INVALID_ACCESS', '2006');
361        /** オーナーズストア通信エラーコード */
362        define('OSTORE_E_C_INVALID_PARAM', '2007');
363        /** オーナーズストア通信エラーコード */
364        define('OSTORE_E_C_AUTOUP_DISABLE', '2008');
365        /** オーナーズストア通信エラーコード */
366        define('OSTORE_E_C_PERMISSION', '2009');
367        /** オーナーズストア通信エラーコード */
368        define('OSTORE_E_C_BATCH_ERR', '2010');
369
370        // プラグイン関連
371        /** プラグインの状態:アップロード済み */
372        define('PLUGIN_STATUS_UPLOADED', '1');
373        /** プラグインの状態:インストール済み */
374        define('PLUGIN_STATUS_INSTALLED', '2');
375        /** プラグイン有効/無効:有効 */
376        define('PLUGIN_ENABLE_TRUE', '1');
377        /** プラグイン有効/無効:無効 */
378        define('PLUGIN_ENABLE_FALSE', '2');
379
380        // CSV入出力関連
381        /** CSV入出力列設定有効無効フラグ: 有効 */
382        define('CSV_COLUMN_STATUS_FLG_ENABLE', 1);
383        /** CSV入出力列設定有効無効フラグ: 無効 */
384        define('CSV_COLUMN_STATUS_FLG_DISABLE', 2);
385        /** CSV入出力列設定読み書きフラグ: 読み書き可能 */
386        define('CSV_COLUMN_RW_FLG_READ_WRITE', 1);
387        /** CSV入出力列設定読み書きフラグ: 読み込みのみ可能 */
388        define('CSV_COLUMN_RW_FLG_READ_ONLY', 2);
389        /** CSV入出力列設定読み書きフラグ: キー列 */
390        define('CSV_COLUMN_RW_FLG_KEY_FIELD', 3);
391
392        // 配置ID
393        /** 配置ID: 未使用 */
394        define('TARGET_ID_UNUSED', 0);
395        /** 配置ID: LeftNavi */
396        define('TARGET_ID_LEFT', 1);
397        /** 配置ID: MainHead */
398        define('TARGET_ID_MAIN_HEAD', 2);
399        /** 配置ID: RightNavi */
400        define('TARGET_ID_RIGHT', 3);
401        /** 配置ID: MainFoot */
402        define('TARGET_ID_MAIN_FOOT', 4);
403        /** 配置ID: TopNavi */
404        define('TARGET_ID_TOP', 5);
405        /** 配置ID: BottomNavi */
406        define('TARGET_ID_BOTTOM', 6);
407        /** 配置ID: HeadNavi */
408        define('TARGET_ID_HEAD', 7);
409        /** 配置ID: HeadTopNavi */
410        define('TARGET_ID_HEAD_TOP', 8);
411        /** 配置ID: FooterBottomNavi */
412        define('TARGET_ID_FOOTER_BOTTOM', 9);
413        /** 配置ID: HeaderInternalNavi */
414        define('TARGET_ID_HEADER_INTERNAL', 10);
415
416        // 他
417        /** アクセス成功 */
418        define('SUCCESS', 0);
419        /** 無制限フラグ: 無制限 */
420        define('UNLIMITED_FLG_UNLIMITED', '1');
421        /** 無制限フラグ: 制限有り */
422        define('UNLIMITED_FLG_LIMITED', '0');
423    }
424
425    /**
426     * クォートされた文字列のクォート部分を再帰的に取り除く.
427     *
428     * {@link http://jp2.php.net/manual/ja/function.get-magic-quotes-gpc.php PHP Manual} の記事を参考に実装。
429     * $_REQUEST は後続の処理で再構成されるため、本処理では外している。
430     * この関数は, PHP5以上を対象とし, PHP4 の場合は何もしない.
431     *
432     * @return void
433     */
434    public function stripslashesDeepGpc()
435    {
436        // Strip magic quotes from request data.
437        if (get_magic_quotes_gpc()
438            && version_compare(PHP_VERSION, '5.0.0', '>=')) {
439            // Create lamba style unescaping function (for portability)
440            $quotes_sybase = strtolower(ini_get('magic_quotes_sybase'));
441            $unescape_function = (empty($quotes_sybase) || $quotes_sybase === 'off') ? 'stripslashes($value)' : 'str_replace("\'\'","\'",$value)';
442            $stripslashes_deep = create_function('&$value, $fn', '
443                if (is_string($value)) {
444                    $value = ' . $unescape_function . ';
445                } elseif (is_array($value)) {
446                    foreach ($value as &$v) $fn($v, $fn);
447                }
448            ');
449
450            // Unescape data
451            $stripslashes_deep($_POST, $stripslashes_deep);
452            $stripslashes_deep($_GET, $stripslashes_deep);
453            $stripslashes_deep($_COOKIE, $stripslashes_deep);
454        }
455    }
456
457    /**
458     * スーパーグローバル変数「$_REQUEST」を再セット
459     *
460     * variables_order ディレクティブによる差を吸収する。
461     *
462     * @access protected
463     * @return void
464     */
465    public function resetSuperglobalsRequest()
466    {
467        $_REQUEST = array_merge($_GET, $_POST);
468    }
469
470    /**
471     * 指定された名前の定数が存在しない場合、指定された値で定義
472     *
473     * @param  string $name  定数の名前。
474     * @param  mixed  $value 定数の値。
475     * @return void
476     */
477    public function defineIfNotDefined($name, $value = null)
478    {
479        if (!defined($name)) {
480            define($name, $value);
481        }
482    }
483
484    /**
485     * タイムゾーンを設定
486     *
487     * @return void
488     */
489    public function setTimezone()
490    {
491        date_default_timezone_set('Asia/Tokyo');
492    }
493
494    /**
495     * ホスト名を正規化する
496     *
497     * @return void
498     */
499    public function normalizeHostname()
500    {
501        if (
502            // パラメーター
503            !USE_NORMALIZE_HOSTNAME
504            // コマンドライン実行の場合
505            || !isset($_SERVER['REQUEST_URI'])
506            // POSTの場合
507            || $_SERVER['REQUEST_METHOD'] === 'POST'
508        ) {
509            // 処理せず戻る
510            return;
511        }
512
513        $netUrlRequest = new Net_URL($_SERVER['REQUEST_URI']);
514        // 要求を受けたホスト名
515        $request_hostname = $netUrlRequest->host;
516
517        $netUrlCorrect = new Net_URL(SC_Utils_Ex::sfIsHTTPS() ? HTTPS_URL : HTTP_URL);
518        // 設定上のホスト名
519        $correct_hostname = $netUrlCorrect->host;
520
521        // ホスト名が不一致の場合
522        if ($request_hostname !== $correct_hostname) {
523            // ホスト名を書き換え
524            $netUrlRequest->host = $correct_hostname;
525            // 正しい URL
526            $correct_url = $netUrlRequest->getUrl();
527            // 警告
528            $msg = 'ホスト名不一致を検出。リダイレクト実行。';
529            $msg .= '要求値=' . var_export($request_hostname, true) . ' ';
530            $msg .= '設定値=' . var_export($correct_hostname, true) . ' ';
531            $msg .= 'リダイレクト先=' . var_export($correct_url, true) . ' ';
532            trigger_error($msg, E_USER_WARNING);
533            // リダイレクト(恒久的)
534            SC_Response_Ex::sendHttpStatus(301);
535            SC_Response_Ex::sendRedirect($correct_url);
536        }
537    }
538}
Note: See TracBrowser for help on using the repository browser.