source: branches/comu-ver2/data/class/pages/LC_Page.php @ 18191

Revision 18191, 16.9 KB checked in by Seasoft, 15 years ago (diff)

PHP 5.1.6 での不具合対策
・フォーラムにて shasharaku 様、withwind 様からの報告。 http://xoops.ec-cube.net/modules/newbb/viewtopic.php?topic_id=4277&forum=9

  • 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-2007 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// {{{ requires
25require_once(DATA_PATH . 'module/Net/URL.php');
26
27/**
28 * Web Page を制御する基底クラス
29 *
30 * Web Page を制御する Page クラスは必ずこのクラスを継承する.
31 * PHP4 ではこのような抽象クラスを作っても継承先で何でもできてしまうため、
32 * あまり意味がないが、アーキテクトを統一するために作っておく.
33 *
34 * @package Page
35 * @author LOCKON CO.,LTD.
36 * @version $Id:LC_Page.php 15532 2007-08-31 14:39:46Z nanasess $
37 */
38class LC_Page {
39
40    // {{{ properties
41
42    /** メインテンプレート */
43    var $tpl_mainpage;
44
45    /** テンプレートのカラム数 */
46    var $tpl_column_num = 2;
47
48    /** メインナンバー */
49    var $tpl_mainno;
50
51    /** CSS のパス */
52    var $tpl_css;
53
54    /** JavaScript */
55    var $tpl_javascript;
56
57    /** タイトル */
58    var $tpl_title;
59
60    /** カテゴリ */
61    var $tpl_page_category;
62
63    /** ログインメールアドレス */
64    var $tpl_login_email;
65
66    /** body タグの onload 属性 */
67    var $tpl_onload;
68
69    /** 送料合計 */
70    var $tpl_total_deliv_fee;
71
72    /** トランザクションID */
73    var $transactionid;
74
75    // }}}
76    // {{{ functions
77
78    /**
79     * Page を初期化する.
80     *
81     * @return void
82     */
83    function init() {
84        $this->tpl_authority = $_SESSION['authority'];
85    }
86
87    /**
88     * Page のプロセス.
89     *
90     * @return void
91     */
92    function process() {}
93
94    /**
95     * デストラクタ.
96     *
97     * @return void
98     */
99    function destroy() {}
100
101    /**
102     * 指定の URL へリダイレクトする.
103     *
104     * リダイレクト先 URL に SITE_URL 及び SSL_URL を含むかチェックし,
105     * LC_Page::getToken() の値を URLパラメータで自動的に付与する.
106     *
107     * @param string $url リダイレクト先 URL
108     * @param boolean $isMobile モバイル用にセッションIDを付与する場合 true
109     * @return void|boolean $url に SITE_URL 及び, SSL_URL を含まない場合 false,
110     *                       正常に遷移可能な場合は, $url の ロケーションヘッダを出力する.
111     * @see Net_URL
112     */
113    function sendRedirect($url, $isMobile = false) {
114
115        if (preg_match("/(" . preg_quote(SITE_URL, '/')
116                          . "|" . preg_quote(SSL_URL, '/') . ")/", $url)) {
117
118            $netURL = new Net_URL($url);
119            if (!empty($_SERVER['QUERY_STRING'])) {
120                $netURL->addRawQueryString($_SERVER['QUERY_STRING']);
121            }
122
123            $session = SC_SessionFactory::getInstance();
124            if ($isMobile || $session->useCookie() == false) {
125                $netURL->addQueryString(session_name(), session_id());
126            }
127
128            $netURL->addQueryString(TRANSACTION_ID_NAME, $this->getToken());
129            header("Location: " . $netURL->getURL());
130            //return true;
131            exit();
132        }
133        return false;
134    }
135
136    // }}}
137    // {{{ protected functions
138
139    /**
140     * トランザクショントークンを生成し, 取得する.
141     *
142     * 悪意のある不正な画面遷移を防止するため, 予測困難な文字列を生成して返す.
143     * 同時に, この文字列をセッションに保存する.
144     *
145     * この関数を使用するためには, 生成した文字列を次画面へ渡すパラメータとして
146     * 出力する必要がある.
147     *
148     * 例)
149     * <input type="hidden" name="transactionid" value="この関数の返り値" />
150     *
151     * 遷移先のページで, LC_Page::isValidToken() の返り値をチェックすることにより,
152     * 画面遷移の妥当性が確認できる.
153     *
154     * @access protected
155     * @return string トランザクショントークンの文字列
156     */
157    function getToken() {
158        if (empty($_SESSION[TRANSACTION_ID_NAME])) {
159            $_SESSION[TRANSACTION_ID_NAME] = $this->createToken();
160        }
161        return $_SESSION[TRANSACTION_ID_NAME];
162    }
163
164    /**
165     * トランザクショントークンの妥当性をチェックする.
166     *
167     * 前画面で生成されたトランザクショントークンの妥当性をチェックする.
168     * この関数を使用するためには, 前画面のページクラスで LC_Page::getToken()
169     * を呼んでおく必要がある.
170     *
171     * @access protected
172     * @return boolean トランザクショントークンが有効な場合 true
173     */
174    function isValidToken() {
175
176        $checkToken = "";
177
178        // $_POST の値を優先する
179        if (isset($_POST[TRANSACTION_ID_NAME])) {
180
181            $checkToken = $_POST[TRANSACTION_ID_NAME];
182        } elseif (isset($_GET[TRANSACTION_ID_NAME])) {
183
184            $checkToken = $_GET[TRANSACTION_ID_NAME];
185        }
186
187        $ret = false;
188        // token の妥当性チェック
189        if ($checkToken === $_SESSION[TRANSACTION_ID_NAME]) {
190
191            $ret = true;
192        }
193
194        unset($_SESSION[TRANSACTION_ID_NAME]);
195        return $ret;
196    }
197
198    /**
199     * $path から URL を取得する.
200     *
201     * 以下の順序で 引数 $path から URL を取得する.
202     * 1. realpath($path) で $path の 絶対パスを取得
203     * 2. $_SERVER['DOCUMENT_ROOT'] と一致する文字列を削除
204     * 3. $useSSL の値に応じて, SITE_URL 又は, SSL_URL を付与する.
205     *
206     * 返り値に, QUERY_STRING を含めたい場合は, key => value 形式
207     * の配列を $param へ渡す.
208     *
209     * @access protected
210     * @param string $path 結果を取得するためのパス
211     * @param array $param URL に付与するパラメータの配列
212     * @param mixed $useSSL 結果に SSL_URL を使用する場合 true,
213     *                         SITE_URL を使用する場合 false,
214     *                         デフォルト "escape" 現在のスキーマを使用
215     * @return string $path の存在する http(s):// から始まる絶対パス
216     * @see Net_URL
217     */
218    function getLocation($path, $param = array(), $useSSL = "escape") {
219        $rootPath = $this->getRootPath($path);
220
221        // スキーマを定義
222        if ($useSSL === true) {
223            $url = SSL_URL . $rootPath;
224        } elseif ($useSSL === false){
225            $url = SITE_URL . $rootPath;
226        } elseif ($useSSL == "escape") {
227            if (SC_Utils_Ex::sfIsHTTPS()) {
228                $url = SSL_URL . $rootPath;
229            } else {
230                $url = SITE_URL . $rootPath;
231            }
232        } else {
233            die("[BUG] Illegal Parametor of \$useSSL ");
234        }
235
236        $netURL = new Net_URL($url);
237        // QUERY_STRING 生成
238        foreach ($param as $key => $val) {
239            $netURL->addQueryString($key, $val);
240        }
241
242        return $netURL->getURL();
243    }
244
245    /**
246     * EC-CUBE のWEBルート(/html/)を / としたパスを返す
247     *
248     * @param string $path 結果を取得するためのパス
249     * @return string EC-CUBE のWEBルート(/html/)を / としたパス
250     */
251    function getRootPath($path) {
252        // Windowsの場合は, ディレクトリの区切り文字を\から/に変換する
253        $path = str_replace('\\', '/', $path);
254        $htmlPath = str_replace('\\', '/', HTML_PATH);
255       
256        // PHP 5.1 対策 ( http://xoops.ec-cube.net/modules/newbb/viewtopic.php?topic_id=4277&forum=9 )
257        if (strlen($path) == 0) {
258            $path = '.';
259        }
260       
261        // $path が / で始まっている場合
262        if (substr($path, 0, 1) == '/') {
263            $realPath = realpath($htmlPath . substr_replace($path, '', 0, strlen(URL_DIR)));
264        // 相対パスの場合
265        } else {
266            $realPath = realpath($path);
267        }
268        $realPath = str_replace('\\', '/', $realPath);
269       
270        // $path が / で終わっている場合、realpath によって削られた末尾の / を復元する。
271        if (substr($path, -1, 1) == '/' && substr($realPath, -1, 1) != '/') {
272            $realPath .= '/';
273        }
274       
275        // HTML_PATH を削除した文字列を取得.
276        $rootPath = str_replace($htmlPath, '', $realPath);
277        $rootPath = ltrim($rootPath, '/');
278
279        return $rootPath;
280    }
281
282    /**
283     * ページをリロードする.
284     *
285     * 引数 $queryString に, $_SERVER['QUERY_STRING'] の値を使用してはならない.
286     * この関数は, 内部で LC_Page::sendRedirect() を使用するため,
287     * $_SERVER['QUERY_STRING'] の値は自動的に付与される.
288     *
289     * @param array $queryString QueryString の配列
290     * @param bool $removeQueryString 付与されていた QueryString を削除する場合 true
291     * @return void
292     * @see Net_URL
293     */
294    function reload($queryString = array(), $removeQueryString = false) {
295
296        // 現在の URL を取得
297        $netURL = new Net_URL($_SERVER['REQUEST_URI']);
298
299        if ($removeQueryString) {
300            $netURL->querystring = array();
301            $_SERVER['QUERY_STRING'] = ''; // sendRedirect() での処理用らしい
302        }
303
304        // QueryString を付与
305        if (!empty($queryString)) {
306            foreach ($queryString as $key => $val) {
307                $netURL->addQueryString($key, $val);
308            }
309        }
310
311        $this->sendRedirect($netURL->getURL());
312    }
313
314    /**
315     * クライアント・プロキシのキャッシュを制御する.
316     *
317     * @access protected
318     * @param string $mode (nocache/private)
319     * @return void
320     */
321    function httpCacheControl($mode = '') {
322        switch ($mode) {
323            case 'nocache':
324                header('Pragma: no-cache');
325                header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
326                header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
327                header('Last-Modified:');
328                break;
329               
330            case 'private':
331                $cache_expire = session_cache_expire() * 60;
332                header('Pragma: no-cache');                                                            // anti-proxy
333                header('Expires:');                                                                    // anti-mozilla
334                header("Cache-Control: private, max-age={$cache_expire}, pre-check={$cache_expire}");  // HTTP/1.1 client
335                header('Last-Modified:');
336                break;
337               
338            default:
339                break;
340        }
341    }
342
343    /**
344     * デバック出力を行う.
345     *
346     * デバック用途のみに使用すること.
347     *
348     * @access protected
349     * @param mixed $val デバックする要素
350     * @return void
351     */
352    function p($val) {
353        SC_Utils_Ex::sfPrintR($val);
354    }
355
356    // }}}
357    // {{{ private functions
358
359    /**
360     * トランザクショントークン用の予測困難な文字列を生成して返す.
361     *
362     * @access private
363     * @return string トランザクショントークン用の文字列
364     */
365    function createToken() {
366        return sha1(uniqid(rand(), true));
367    }
368   
369    /**
370     * HTTPステータスコードを送出する。
371     *
372     * @param integer $code HTTPステータスコード
373     * @return void
374     * @author Seasoft (新規作成)
375     * @see Moony_Action::status() (オリジナル)
376     * @link http://moony.googlecode.com/ (オリジナル)
377     * @author YAMAOKA Hiroyuki (オリジナル)
378     * @copyright 2005-2008 YAMAOKA Hiroyuki (オリジナル)
379     * @license http://opensource.org/licenses/bsd-license.php New BSD License (オリジナル)
380     * @link http://ja.wikipedia.org/wiki/HTTP%E3%82%B9%E3%83%86%E3%83%BC%E3%82%BF%E3%82%B9%E3%82%B3%E3%83%BC%E3%83%89 (邦訳)
381     * @license http://www.gnu.org/licenses/fdl.html GFDL (邦訳)
382     */
383    function sendHttpStatus($code) {
384        $protocol = $_SERVER['SERVER_PROTOCOL'];
385        $httpVersion = (strpos($protocol, '1.1') !== false) ? '1.1' : '1.0';
386        $messages = array(
387            // Informational 1xx                        // 【情報】
388            100 => 'Continue',                          // 継続
389            101 => 'Switching Protocols',               // プロトコル切替え
390            // Success 2xx                              // 【成功】
391            200 => 'OK',                                // OK
392            201 => 'Created',                           // 作成
393            202 => 'Accepted',                          // 受理
394            203 => 'Non-Authoritative Information',     // 信頼できない情報
395            204 => 'No Content',                        // 内容なし
396            205 => 'Reset Content',                     // 内容のリセット
397            206 => 'Partial Content',                   // 部分的内容
398            // Redirection 3xx                          // 【リダイレクション】
399            300 => 'Multiple Choices',                  // 複数の選択
400            301 => 'Moved Permanently',                 // 恒久的に移動した
401            302 => 'Found',  // 1.1                     // 発見した (リクエストしたリソースは一時的に移動されているときに返される)
402            303 => 'See Other',                         // 他を参照せよ
403            304 => 'Not Modified',                      // 未更新
404            305 => 'Use Proxy',                         // プロキシを使用せよ
405            // 306 is no longer used but still reserved // 将来のために予約されている
406            307 => 'Temporary Redirect',                // 一時的リダイレクト
407            // Client Error 4xx                         // 【クライアントエラー】
408            400 => 'Bad Request',                       // リクエストが不正である
409            401 => 'Unauthorized',                      // 認証が必要である
410            402 => 'Payment Required',                  // 支払いが必要である
411            403 => 'Forbidden',                         // 禁止されている
412            404 => 'Not Found',                         // 未検出
413            405 => 'Method Not Allowed',                // 許可されていないメソッド
414            406 => 'Not Acceptable',                    // 受理できない
415            407 => 'Proxy Authentication Required',     // プロキシ認証が必要である
416            408 => 'Request Timeout',                   // リクエストタイムアウト
417            409 => 'Conflict',                          // 矛盾
418            410 => 'Gone',                              // 消滅した
419            411 => 'Length Required',                   // 長さが必要
420            412 => 'Precondition Failed',               // 前提条件で失敗した
421            413 => 'Request Entity Too Large',          // リクエストエンティティが大きすぎる
422            414 => 'Request-URI Too Long',              // リクエストURIが大きすぎる
423            415 => 'Unsupported Media Type',            // サポートしていないメディアタイプ
424            416 => 'Requested Range Not Satisfiable',   // リクエストしたレンジは範囲外にある
425            417 => 'Expectation Failed',                // 期待するヘッダに失敗
426            // Server Error 5xx                         // 【サーバエラー】
427            500 => 'Internal Server Error',             // サーバ内部エラー
428            501 => 'Not Implemented',                   // 実装されていない
429            502 => 'Bad Gateway',                       // 不正なゲートウェイ
430            503 => 'Service Unavailable',               // サービス利用不可
431            504 => 'Gateway Timeout',                   // ゲートウェイタイムアウト
432            505 => 'HTTP Version Not Supported',        // サポートしていないHTTPバージョン
433            509 => 'Bandwidth Limit Exceeded'           // 帯域幅制限超過
434        );
435        if (isset($messages[$code])) {
436            if ($httpVersion !== '1.1') {
437                // HTTP/1.0
438                $messages[302] = 'Moved Temporarily';
439            }
440            header("HTTP/{$httpVersion} {$code} {$messages[$code]}");
441            header("Status: {$code} {$messages[$code]}", true, $code);
442        }
443    }
444}
445?>
Note: See TracBrowser for help on using the repository browser.