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

Revision 23256, 19.8 KB checked in by Seasoft, 10 years ago (diff)

#2427 (SC_SessionFactory_UseRequest#initSession URLパスでリダイレクトしている)
#2445 (TOPページのURLに揺らぎがある)
#2446 (HTTP へ復帰すべき画面遷移でも HTTPS が維持される)

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