source: branches/version-2_13-dev/data/class/SC_Initial.php @ 22922

Revision 22922, 19.1 KB checked in by Seasoft, 11 years ago (diff)

#2284 (ホスト名が異なるとリダイレクトのタイミングでエラーとなる)

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