source: tmp/version-2_5-test/data/class/pages/LC_Page.php @ 18609

Revision 18609, 17.3 KB checked in by kajiwara, 14 years ago (diff)

正式版にナイトリービルド版をマージしてみるテスト

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