source: branches/version-2_12-dev/data/class/SC_Initial.php @ 21948

Revision 21948, 17.5 KB checked in by h_yoshimoto, 12 years ago (diff)

#1849 eucJP-winに変更

  • 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-2012 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    // {{{ cunstructor
33
34    /**
35     * コンストラクタ.
36     */
37    function __construct() {
38
39        /** EC-CUBEのバージョン */
40        define('ECCUBE_VERSION', '2.12.1-dev');
41    }
42
43    // }}}
44    // {{{ functions
45
46    /**
47     * 初期設定を行う.
48     *
49     * @access protected
50     * @return void
51     */
52    function init() {
53        $this->requireInitialConfig();
54        $this->defineDSN();                 // requireInitialConfig メソッドより後で実行
55        $this->defineDirectoryIndex();
56        $this->defineConstants();
57        $this->defineParameter();           // defineDirectoryIndex メソッドより後で実行
58        $this->complementParameter();       // defineConstants メソッドより後で実行
59        $this->phpconfigInit();             // defineConstants メソッドより後で実行
60        $this->createCacheDir();            // defineConstants メソッドより後で実行
61        $this->stripslashesDeepGpc();
62        $this->resetSuperglobalsRequest();  // stripslashesDeepGpc メソッドより後で実行
63        $this->setTimezone();               // 本当はエラーハンドラーより先に読みたい気も
64    }
65
66    /**
67     * 初期設定ファイルを読み込み, パスの設定を行う.
68     *
69     * @access protected
70     * @return void
71     */
72    function requireInitialConfig() {
73
74        define('CONFIG_REALFILE', realpath(dirname(__FILE__)) . '/../config/config.php');
75        if (file_exists(CONFIG_REALFILE)) {
76            require_once CONFIG_REALFILE;
77        }
78    }
79
80    /**
81     * DSN を定義する.
82     *
83     * @access protected
84     * @return void
85     * @deprecated 下位互換用
86     */
87    function defineDSN() {
88        if (defined('DB_TYPE') && defined('DB_USER') && defined('DB_PASSWORD')
89            && defined('DB_SERVER') && defined('DB_PORT') && defined('DB_NAME')
90        ) {
91            $dsn = DB_TYPE . '://' . DB_USER . ':' . DB_PASSWORD . '@' . DB_SERVER . ':' . DB_PORT . '/' . DB_NAME;
92            /** サイト用DB */
93            // ここで生成した DSN は使用せず, SC_Query のコンストラクタでパラメータを設定する.
94            define('DEFAULT_DSN', $dsn);
95        }
96    }
97
98    /**
99     * @deprecated
100     */
101    function setErrorReporting() {
102        error_reporting(E_ALL & ~E_NOTICE);
103        // PHP 5.3.0対応
104        if (error_reporting() > 6143) {
105            error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
106        }
107    }
108
109    /**
110     * マルチバイト文字列設定を行う.
111     *
112     * TODO SJIS-win や, eucJP-win への対応
113     *
114     * @access protected
115     * @return void
116     */
117    function phpconfigInit() {
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
126        mb_language('ja'); // mb_internal_encoding() より前に
127        // TODO 他に mb_language() している箇所の削除を検討
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    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    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
198            $mtb_constants = file_get_contents(CACHE_REALDIR . '../mtb_constants_init.php');
199            if (is_writable(CACHE_REALDIR)) {
200                $handle = fopen(CACHE_REALDIR . 'mtb_constants.php', 'w');
201                if (!$handle) {
202                    die($errorMessage);
203                }
204                if (fwrite($handle, $mtb_constants) === false) {
205                    die($errorMessage);
206                }
207                fclose($handle);
208
209                require_once CACHE_REALDIR . 'mtb_constants.php';
210            } else {
211                die($errorMessage);
212            }
213        } else {
214            die(CACHE_REALDIR . '../mtb_constants_init.php が存在しません');
215        }
216    }
217
218    /**
219     * パラメーターの補完
220     *
221     * ソースのみ差し替えたバージョンアップを考慮したもの。
222     * $this->defineIfNotDefined() で定義することを想定
223     *
224     * @access protected
225     * @return void
226     */
227    function complementParameter() {
228    }
229
230    /**
231     * 各種キャッシュディレクトリを生成する.
232     *
233     * Smarty キャッシュディレクトリを生成する.
234     *
235     * @access protected
236     * @return void
237     */
238    function createCacheDir() {
239        if (defined('HTML_REALDIR')) {
240            umask(0);
241            if (!file_exists(COMPILE_REALDIR)) {
242                mkdir(COMPILE_REALDIR);
243            }
244
245            if (!file_exists(MOBILE_COMPILE_REALDIR)) {
246                mkdir(MOBILE_COMPILE_REALDIR);
247            }
248
249            if (!file_exists(SMARTPHONE_COMPILE_REALDIR)) {
250                mkdir(SMARTPHONE_COMPILE_REALDIR);
251            }
252
253            if (!file_exists(COMPILE_ADMIN_REALDIR)) {
254                mkdir(COMPILE_ADMIN_REALDIR);
255            }
256        }
257    }
258
259    /**
260     * 定数定義
261     *
262     * @access protected
263     * @return void
264     */
265    function defineConstants() {
266        // LC_Page_Error用
267        /** 指定商品ページがない */
268        define('PRODUCT_NOT_FOUND', 1);
269        /** カート内が空 */
270        define('CART_EMPTY', 2);
271        /** ページ推移エラー */
272        define('PAGE_ERROR', 3);
273        /** 購入処理中のカート商品追加エラー */
274        define('CART_ADD_ERROR', 4);
275        /** 他にも購入手続きが行われた場合 */
276        define('CANCEL_PURCHASE', 5);
277        /** 指定カテゴリページがない */
278        define('CATEGORY_NOT_FOUND', 6);
279        /** ログインに失敗 */
280        define('SITE_LOGIN_ERROR', 7);
281        /** 会員専用ページへのアクセスエラー */
282        define('CUSTOMER_ERROR', 8);
283        /** 購入時の売り切れエラー */
284        define('SOLD_OUT', 9);
285        /** カート内商品の読込エラー */
286        define('CART_NOT_FOUND', 10);
287        /** ポイントの不足 */
288        define('LACK_POINT', 11);
289        /** 仮登録者がログインに失敗 */
290        define('TEMP_LOGIN_ERROR', 12);
291        /** URLエラー */
292        define('URL_ERROR', 13);
293        /** ファイル解凍エラー */
294        define('EXTRACT_ERROR', 14);
295        /** FTPダウンロードエラー */
296        define('FTP_DOWNLOAD_ERROR', 15);
297        /** FTPログインエラー */
298        define('FTP_LOGIN_ERROR', 16);
299        /** FTP接続エラー */
300        define('FTP_CONNECT_ERROR', 17);
301        /** DB作成エラー */
302        define('CREATE_DB_ERROR', 18);
303        /** DBインポートエラー */
304        define('DB_IMPORT_ERROR', 19);
305        /** 設定ファイル存在エラー */
306        define('FILE_NOT_FOUND', 20);
307        /** 書き込みエラー */
308        define('WRITE_FILE_ERROR', 21);
309        /** DB接続エラー */
310        define('DB_CONNECT_ERROR', 22);
311        /** ダウンロードファイル存在エラー */
312        define('DOWNFILE_NOT_FOUND', 22);
313        /** フリーメッセージ */
314        define('FREE_ERROR_MSG', 999);
315
316        // LC_Page_Error_DispError用
317        /** ログイン失敗 */
318        define('LOGIN_ERROR', 1);
319        /** アクセス失敗(タイムアウト等) */
320        define('ACCESS_ERROR', 2);
321        /** アクセス権限違反 */
322        define('AUTH_ERROR', 3);
323        /** 不正な遷移エラー */
324        define('INVALID_MOVE_ERRORR', 4);
325
326        // オーナーズストア通信関連
327        /** オーナーズストア通信ステータス */
328        define('OSTORE_STATUS_ERROR', 'ERROR');
329        /** オーナーズストア通信ステータス */
330        define('OSTORE_STATUS_SUCCESS', 'SUCCESS');
331        /** オーナーズストア通信エラーコード */
332        define('OSTORE_E_UNKNOWN', '1000');
333        /** オーナーズストア通信エラーコード */
334        define('OSTORE_E_INVALID_PARAM', '1001');
335        /** オーナーズストア通信エラーコード */
336        define('OSTORE_E_NO_CUSTOMER', '1002');
337        /** オーナーズストア通信エラーコード */
338        define('OSTORE_E_WRONG_URL_PASS', '1003');
339        /** オーナーズストア通信エラーコード */
340        define('OSTORE_E_NO_PRODUCTS', '1004');
341        /** オーナーズストア通信エラーコード */
342        define('OSTORE_E_NO_DL_DATA', '1005');
343        /** オーナーズストア通信エラーコード */
344        define('OSTORE_E_DL_DATA_OPEN', '1006');
345        /** オーナーズストア通信エラーコード */
346        define('OSTORE_E_DLLOG_AUTH', '1007');
347        /** オーナーズストア通信エラーコード */
348        define('OSTORE_E_C_ADMIN_AUTH', '2001');
349        /** オーナーズストア通信エラーコード */
350        define('OSTORE_E_C_HTTP_REQ', '2002');
351        /** オーナーズストア通信エラーコード */
352        define('OSTORE_E_C_HTTP_RESP', '2003');
353        /** オーナーズストア通信エラーコード */
354        define('OSTORE_E_C_FAILED_JSON_PARSE', '2004');
355        /** オーナーズストア通信エラーコード */
356        define('OSTORE_E_C_NO_KEY', '2005');
357        /** オーナーズストア通信エラーコード */
358        define('OSTORE_E_C_INVALID_ACCESS', '2006');
359        /** オーナーズストア通信エラーコード */
360        define('OSTORE_E_C_INVALID_PARAM', '2007');
361        /** オーナーズストア通信エラーコード */
362        define('OSTORE_E_C_AUTOUP_DISABLE', '2008');
363        /** オーナーズストア通信エラーコード */
364        define('OSTORE_E_C_PERMISSION', '2009');
365        /** オーナーズストア通信エラーコード */
366        define('OSTORE_E_C_BATCH_ERR', '2010');
367
368        // プラグイン関連
369        /** プラグインの状態:アップロード済み */
370        define('PLUGIN_STATUS_UPLOADED', '1');
371        /** プラグインの状態:インストール済み */
372        define('PLUGIN_STATUS_INSTALLED', '2');
373        /** プラグイン有効/無効:有効 */
374        define('PLUGIN_ENABLE_TRUE', '1');
375        /** プラグイン有効/無効:無効 */
376        define('PLUGIN_ENABLE_FALSE', '2');
377
378        // CSV入出力関連
379        /** CSV入出力列設定有効無効フラグ: 有効 */
380        define('CSV_COLUMN_STATUS_FLG_ENABLE', 1);
381        /** CSV入出力列設定有効無効フラグ: 無効 */
382        define('CSV_COLUMN_STATUS_FLG_DISABLE', 2);
383        /** CSV入出力列設定読み書きフラグ: 読み書き可能 */
384        define('CSV_COLUMN_RW_FLG_READ_WRITE', 1);
385        /** CSV入出力列設定読み書きフラグ: 読み込みのみ可能 */
386        define('CSV_COLUMN_RW_FLG_READ_ONLY', 2);
387        /** CSV入出力列設定読み書きフラグ: キー列 */
388        define('CSV_COLUMN_RW_FLG_KEY_FIELD', 3);
389
390        // 配置ID
391        /** 配置ID: 未使用 */
392        define('TARGET_ID_UNUSED', 0);
393        /** 配置ID: LeftNavi */
394        define('TARGET_ID_LEFT', 1);
395        /** 配置ID: MainHead */
396        define('TARGET_ID_MAIN_HEAD', 2);
397        /** 配置ID: RightNavi */
398        define('TARGET_ID_RIGHT', 3);
399        /** 配置ID: MainFoot */
400        define('TARGET_ID_MAIN_FOOT', 4);
401        /** 配置ID: TopNavi */
402        define('TARGET_ID_TOP', 5);
403        /** 配置ID: BottomNavi */
404        define('TARGET_ID_BOTTOM', 6);
405        /** 配置ID: HeadNavi */
406        define('TARGET_ID_HEAD', 7);
407        /** 配置ID: HeadTopNavi */
408        define('TARGET_ID_HEAD_TOP', 8);
409        /** 配置ID: FooterBottomNavi */
410        define('TARGET_ID_FOOTER_BOTTOM', 9);
411        /** 配置ID: HeaderInternalNavi */
412        define('TARGET_ID_HEADER_INTERNAL', 10);
413
414        // 他
415        /** アクセス成功 */
416        define('SUCCESS', 0);
417        /** 無制限フラグ: 無制限 */
418        define('UNLIMITED_FLG_UNLIMITED', '1');
419        /** 無制限フラグ: 制限有り */
420        define('UNLIMITED_FLG_LIMITED', '0');
421    }
422
423    /**
424     * クォートされた文字列のクォート部分を再帰的に取り除く.
425     *
426     * {@link http://jp2.php.net/manual/ja/function.get-magic-quotes-gpc.php PHP Manual} の記事を参考に実装。
427     * $_REQUEST は後続の処理で再構成されるため、本処理では外している。
428     * この関数は, PHP5以上を対象とし, PHP4 の場合は何もしない.
429     *
430     * @return void
431     */
432    function stripslashesDeepGpc() {
433        // Strip magic quotes from request data.
434        if (get_magic_quotes_gpc()
435            && version_compare(PHP_VERSION, '5.0.0', '>=')) {
436            // Create lamba style unescaping function (for portability)
437            $quotes_sybase = strtolower(ini_get('magic_quotes_sybase'));
438            $unescape_function = (empty($quotes_sybase) || $quotes_sybase === 'off') ? 'stripslashes($value)' : 'str_replace("\'\'","\'",$value)';
439            $stripslashes_deep = create_function('&$value, $fn', '
440                if (is_string($value)) {
441                    $value = ' . $unescape_function . ';
442                } else if (is_array($value)) {
443                    foreach ($value as &$v) $fn($v, $fn);
444                }
445            ');
446
447            // Unescape data
448            $stripslashes_deep($_POST, $stripslashes_deep);
449            $stripslashes_deep($_GET, $stripslashes_deep);
450            $stripslashes_deep($_COOKIE, $stripslashes_deep);
451        }
452    }
453
454    /**
455     * スーパーグローバル変数「$_REQUEST」を再セット
456     *
457     * variables_order ディレクティブによる差を吸収する。
458     *
459     * @access protected
460     * @return void
461     */
462    function resetSuperglobalsRequest() {
463        $_REQUEST = array_merge($_GET, $_POST);
464    }
465
466    /**
467     * 指定された名前の定数が存在しない場合、指定された値で定義
468     *
469     * @param string $name 定数の名前。
470     * @param mixed $value 定数の値。
471     * @return void
472     */
473    function defineIfNotDefined($name, $value = null) {
474        if (!defined($name)) {
475            define($name, $value);
476        }
477    }
478
479    /**
480     * タイムゾーンを設定
481     *
482     * @return void
483     */
484    function setTimezone() {
485        date_default_timezone_set('Asia/Tokyo');
486    }
487}
Note: See TracBrowser for help on using the repository browser.