source: branches/version-2_11-dev/data/class/SC_Initial.php @ 21310

Revision 21310, 12.9 KB checked in by Seasoft, 12 years ago (diff)

#1532 (SC_Initial#init 初期化の依存関係をコメントに記述する)
#1533 (SC_Initial#setErrorReporting #phpconfigInit に処理を移動)
#1534 (SC_Initial#phpconfigInit 不要な ini_set の利用を止める)
r21307 (バージョン変更 2.11.4 -> 2.11.5-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-2011 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 SC_Initial() {
38
39        /** EC-CUBEのバージョン */
40        define('ECCUBE_VERSION', '2.11.5-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->defineErrorType();           // XXX 多分、もっと後で大丈夫
57        $this->defineConstants();           // defineDirectoryIndex メソッドより後で実行
58        $this->complementConstants();       // defineConstants メソッドより後で実行
59        $this->phpconfigInit();             // defineConstants メソッドより後で実行
60        $this->createCacheDir();            // defineConstants メソッドより後で実行
61        $this->stripslashesDeepGpc();
62        $this->resetSuperglobalsRequest();  // stripslashesDeepGpc メソッドより後で実行
63    }
64
65    /**
66     * 初期設定ファイルを読み込み, パスの設定を行う.
67     *
68     * @access protected
69     * @return void
70     */
71    function requireInitialConfig() {
72
73        define('CONFIG_REALFILE', realpath(dirname(__FILE__)) . '/../config/config.php');
74        if (file_exists(CONFIG_REALFILE)) {
75            require_once CONFIG_REALFILE;
76        }
77    }
78
79    /**
80     * DSN を定義する.
81     *
82     * @access protected
83     * @return void
84     */
85    function defineDSN() {
86        if(defined('DB_TYPE') && defined('DB_USER') && defined('DB_PASSWORD')
87           && defined('DB_SERVER') && defined('DB_PORT') && defined('DB_NAME')) {
88            /** サイト用DB */
89            define ("DEFAULT_DSN",
90                    DB_TYPE . "://" . DB_USER . ":" . DB_PASSWORD . "@"
91                    . DB_SERVER . ":" .DB_PORT . "/" . DB_NAME);
92        }
93    }
94
95    /**
96     * @deprecated
97     */
98    function setErrorReporting() {
99        error_reporting(E_ALL & ~E_NOTICE);
100        // PHP 5.3.0対応
101        if (error_reporting() > 6143) {
102            error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
103        }
104    }
105
106    /**
107     * マルチバイト文字列設定を行う.
108     *
109     * TODO SJIS-win や, eucJP-win への対応
110     *
111     * @access protected
112     * @return void
113     */
114    function phpconfigInit() {
115        // E_DEPRECATED 定数 (for PHP < 5.3)
116        // TODO バージョン互換処理に統合したい。
117        $this->defineIfNotDefined('E_DEPRECATED', 8192);
118
119        // エラーレベル設定
120        // 開発時は E_ALL を推奨
121        error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
122
123        ini_set('display_errors', '1');
124        ini_set('html_errors', '1');
125        ini_set('mbstring.http_input', CHAR_CODE);
126        ini_set('mbstring.http_output', CHAR_CODE);
127        ini_set('auto_detect_line_endings', 1);
128        ini_set('default_charset', CHAR_CODE);
129        ini_set('mbstring.detect_order', 'auto');
130        ini_set('mbstring.substitute_character', 'none');
131
132        mb_language('ja'); // mb_internal_encoding() より前に
133        // TODO 他に mb_language() している箇所の削除を検討
134        // TODO .htaccess の mbstring.language を削除できないか検討
135
136        mb_internal_encoding(CHAR_CODE); // mb_language() より後で
137
138        ini_set('arg_separator.output', '&');
139
140        //ロケールを明示的に設定
141        $res = setlocale(LC_ALL, LOCALE);
142        if($res === FALSE) {
143            // TODO: Windows上のロケール設定が正常に働かない場合があることに暫定的に対応
144            // ''を指定するとApache実行環境の環境変数が使われる
145            // See also: http://php.net/manual/ja/function.setlocale.php
146            setlocale(LC_ALL, '');
147        }
148    }
149
150    /**
151     * 定数 DIR_INDEX_PATH を設定する.
152     *
153     * @access protected
154     * @return void
155     */
156    function defineDirectoryIndex() {
157
158        // DirectoryIndex の実ファイル名
159        if (!defined('DIR_INDEX_FILE')) {
160            define('DIR_INDEX_FILE', 'index.php');
161        }
162
163        $useFilenameDirIndex = is_bool(USE_FILENAME_DIR_INDEX)
164            ? USE_FILENAME_DIR_INDEX
165            : (isset($_SERVER['SERVER_SOFTWARE']) ? substr($_SERVER['SERVER_SOFTWARE'], 0, 13) == 'Microsoft-IIS' : false)
166        ;
167
168        // DIR_INDEX_FILE にアクセスする時の URL のファイル名部を定義する
169        if ($useFilenameDirIndex === true) {
170            // ファイル名を使用する
171            define('DIR_INDEX_PATH', DIR_INDEX_FILE);
172        } else {
173            // ファイル名を使用しない
174            define('DIR_INDEX_PATH', '');
175        }
176    }
177
178    /**
179     * 定数を設定する.
180     *
181     * mtb_constants.php を読み込んで定数を設定する.
182     * キャッシュディレクトリに存在しない場合は, 初期データからコピーする.
183     *
184     * @access protected
185     * @return void
186     */
187    function defineConstants() {
188
189        $errorMessage
190            = '<div style="color: #F00; font-weight: bold; background-color: #FEB; text-align: center">'
191            . CACHE_REALDIR
192            . ' にユーザ書込み権限(777等)を付与して下さい。</div>';
193
194        // 定数を設定
195        if (is_file(CACHE_REALDIR . "mtb_constants.php")) {
196            require_once CACHE_REALDIR . 'mtb_constants.php';
197
198            // キャッシュが無ければ, 初期データからコピー
199        } elseif (is_file(CACHE_REALDIR . "../mtb_constants_init.php")) {
200
201            $mtb_constants = file_get_contents(CACHE_REALDIR . "../mtb_constants_init.php");
202            if (is_writable(CACHE_REALDIR)) {
203                $handle = fopen(CACHE_REALDIR . "mtb_constants.php", 'w');
204                if (!$handle) {
205                    die($errorMessage);
206                }
207                if (fwrite($handle, $mtb_constants) === false) {
208                    die($errorMessage);
209                }
210                fclose($handle);
211
212                require_once CACHE_REALDIR . 'mtb_constants.php';
213            } else {
214                die($errorMessage);
215            }
216        } else {
217            die(CACHE_REALDIR . "../mtb_constants_init.php が存在しません");
218        }
219    }
220
221    /**
222     * パラメーターの補完
223     *
224     * ソースのみ差し替えたバージョンアップを考慮したもの。
225     *
226     * @access protected
227     * @return void
228     */
229    function complementConstants() {
230        // 2.11.1 → 2.11.2
231        /** 郵便番号CSVのZIPアーカイブファイルの取得元 */
232        $this->defineIfNotDefined('ZIP_DOWNLOAD_URL', "http://www.post.japanpost.jp/zipcode/dl/kogaki/zip/ken_all.zip");
233    }
234
235    /**
236     * 各種キャッシュディレクトリを生成する.
237     *
238     * Smarty キャッシュディレクトリを生成する.
239     *
240     * @access protected
241     * @return void
242     */
243    function createCacheDir() {
244        if (defined("HTML_REALDIR")) {
245            umask(0);
246            if (!file_exists(COMPILE_REALDIR)) {
247                mkdir(COMPILE_REALDIR);
248            }
249
250            if (!file_exists(MOBILE_COMPILE_REALDIR)) {
251                mkdir(MOBILE_COMPILE_REALDIR);
252            }
253
254            if (!file_exists(SMARTPHONE_COMPILE_REALDIR)) {
255                mkdir(SMARTPHONE_COMPILE_REALDIR);
256            }
257
258            if (!file_exists(COMPILE_ADMIN_REALDIR)) {
259                mkdir(COMPILE_ADMIN_REALDIR);
260            }
261        }
262    }
263
264    /**
265     * エラー種別を定数定義
266     *
267     * @access protected
268     * @return void
269     */
270    function defineErrorType() {
271        // LC_Page_Error用
272        /** 指定商品ページがない */
273        define('PRODUCT_NOT_FOUND', 1);
274        /** カート内が空 */
275        define('CART_EMPTY', 2);
276        /** ページ推移エラー */
277        define('PAGE_ERROR', 3);
278        /** 購入処理中のカート商品追加エラー */
279        define('CART_ADD_ERROR', 4);
280        /** 他にも購入手続きが行われた場合 */
281        define('CANCEL_PURCHASE', 5);
282        /** 指定カテゴリページがない */
283        define('CATEGORY_NOT_FOUND', 6);
284        /** ログインに失敗 */
285        define('SITE_LOGIN_ERROR', 7);
286        /** 会員専用ページへのアクセスエラー */
287        define('CUSTOMER_ERROR', 8);
288        /** 購入時の売り切れエラー */
289        define('SOLD_OUT', 9);
290        /** カート内商品の読込エラー */
291        define('CART_NOT_FOUND', 10);
292        /** ポイントの不足 */
293        define('LACK_POINT', 11);
294        /** 仮登録者がログインに失敗 */
295        define('TEMP_LOGIN_ERROR', 12);
296        /** URLエラー */
297        define('URL_ERROR', 13);
298        /** ファイル解凍エラー */
299        define('EXTRACT_ERROR', 14);
300        /** FTPダウンロードエラー */
301        define('FTP_DOWNLOAD_ERROR', 15);
302        /** FTPログインエラー */
303        define('FTP_LOGIN_ERROR', 16);
304        /** FTP接続エラー */
305        define('FTP_CONNECT_ERROR', 17);
306        /** DB作成エラー */
307        define('CREATE_DB_ERROR', 18);
308        /** DBインポートエラー */
309        define('DB_IMPORT_ERROR', 19);
310        /** 設定ファイル存在エラー */
311        define('FILE_NOT_FOUND', 20);
312        /** 書き込みエラー */
313        define('WRITE_FILE_ERROR', 21);
314        /** DB接続エラー */
315        define('DB_CONNECT_ERROR', 22);
316        /** フリーメッセージ */
317        define('FREE_ERROR_MSG', 999);
318
319        // LC_Page_Error_DispError用
320        /** ログイン失敗 */
321        define('LOGIN_ERROR', 1);
322        /** アクセス失敗(タイムアウト等) */
323        define('ACCESS_ERROR', 2);
324        /** アクセス権限違反 */
325        define('AUTH_ERROR', 3);
326        /** 不正な遷移エラー */
327        define('INVALID_MOVE_ERRORR', 4);
328    }
329
330    /**
331     * クォートされた文字列のクォート部分を再帰的に取り除く.
332     *
333     * {@link http://jp2.php.net/manual/ja/function.get-magic-quotes-gpc.php PHP Manual} の記事を参考に実装。
334     * $_REQUEST は後続の処理で再構成されるため、本処理では外している。
335     * この関数は, PHP5以上を対象とし, PHP4 の場合は何もしない.
336     *
337     * @return void
338     */
339    function stripslashesDeepGpc() {
340        // Strip magic quotes from request data.
341        if (get_magic_quotes_gpc()
342            && version_compare(PHP_VERSION, '5.0.0', '>=')) {
343            // Create lamba style unescaping function (for portability)
344            $quotes_sybase = strtolower(ini_get('magic_quotes_sybase'));
345            $unescape_function = (empty($quotes_sybase) || $quotes_sybase === 'off') ? 'stripslashes($value)' : 'str_replace("\'\'","\'",$value)';
346            $stripslashes_deep = create_function('&$value, $fn', '
347                if (is_string($value)) {
348                    $value = ' . $unescape_function . ';
349                } else if (is_array($value)) {
350                    foreach ($value as &$v) $fn($v, $fn);
351                }
352            ');
353
354            // Unescape data
355            $stripslashes_deep($_POST, $stripslashes_deep);
356            $stripslashes_deep($_GET, $stripslashes_deep);
357            $stripslashes_deep($_COOKIE, $stripslashes_deep);
358        }
359    }
360
361    /**
362     * スーパーグローバル変数「$_REQUEST」を再セット
363     *
364     * variables_order ディレクティブによる差を吸収する。
365     *
366     * @access protected
367     * @return void
368     */
369    function resetSuperglobalsRequest() {
370        $_REQUEST = array_merge($_GET, $_POST);
371    }
372
373    /**
374     * 指定された名前の定数が存在しない場合、指定された値で定義
375     *
376     * @param string $name 定数の名前。
377     * @param mixed $value 定数の値。
378     * @return void
379     */
380    function defineIfNotDefined($name, $value = null) {
381        if (!defined($name)) {
382            define($name, $value);
383        }
384    }
385}
386?>
Note: See TracBrowser for help on using the repository browser.