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

Revision 21180, 12.1 KB checked in by Seasoft, 13 years ago (diff)

#1433 (PHP Notice が発生する)

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