source: branches/version-2_5-dev/data/class/pages/LC_Page.php @ 19817

Revision 19817, 14.9 KB checked in by Seasoft, 13 years ago (diff)

#887(LC_Page#sendHttpStatus を SC_Response へ移植)

  • 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-2010 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_REALDIR . '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;
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    var $template = SITE_FRAME;
77
78    // }}}
79    // {{{ functions
80
81    /**
82     * Page を初期化する.
83     *
84     * @return void
85     */
86    function init() {
87        // 開始時刻を設定する。
88        $this->timeStart = SC_Utils_Ex::sfMicrotimeFloat();
89
90        $this->tpl_authority = $_SESSION['authority'];
91
92        // ディスプレイクラス生成
93        $this->objDisplay = new SC_Display();
94
95        $layout = new SC_Helper_PageLayout_Ex();
96        $layout->sfGetPageLayout($this, false, $_SERVER['PHP_SELF'],
97                                 $this->objDisplay->detectDevice());
98
99        // プラグインクラス生成
100        $this->objPlugin = new SC_Helper_Plugin_Ex();
101        $this->objPlugin->preProcess($this);
102    }
103
104    /**
105     * Page のプロセス.
106     *
107     * @return void
108     */
109    function process() {}
110
111    /**
112     * Page のプロセス.(モバイル)
113     *
114     * @return void
115     */
116    function mobileProcess() {}
117
118    /**
119     * Page のレスポンス送信.
120     *
121     * @return void
122     */
123    function sendResponse() {
124
125        if (isset($this->objPlugin)) { // FIXME モバイルエラー応急対応
126            // post-prosess処理(暫定的)
127            $this->objPlugin->process($this);
128        }
129
130        $this->objDisplay->prepare($this);
131        $this->objDisplay->response->write();
132    }
133
134    /**
135     * Page のレスポンス送信(ダウンロード).
136     *
137     * @return void
138     */
139    function sendResponseCSV($file_name, $data) {
140        $this->objDisplay->prepare($this);
141        $this->objDisplay->addHeader("Content-disposition", "attachment; filename=${file_name}");
142        $this->objDisplay->addHeader("Content-type", "application/octet-stream; name=${file_name}");
143        $this->objDisplay->addHeader("Cache-Control", "");
144        $this->objDisplay->addHeader("Pragma", "");
145
146        $this->objDisplay->response->body = $data;
147        $this->objDisplay->response->write();
148        exit;
149    }
150
151    /**
152     * デストラクタ.
153     *
154     * @return void
155     */
156    function destroy() {
157        // 一定時間以上かかったページの場合、ログ出力する。
158        if(defined('PAGE_DISPLAY_TIME_LOG_MODE') && PAGE_DISPLAY_TIME_LOG_MODE == true) {
159            $timeEnd = SC_Utils_Ex::sfMicrotimeFloat();;
160            $timeExecTime = $timeEnd - $this->timeStart;
161            if(defined('PAGE_DISPLAY_TIME_LOG_MIN_EXEC_TIME') && $timeExecTime >= (float)PAGE_DISPLAY_TIME_LOG_MIN_EXEC_TIME) {
162                $logMsg = sprintf("PAGE_DISPLAY_TIME_LOG [%.2fsec]", $timeExecTime);
163                GC_Utils_Ex::gfPrintLog($logMsg);
164            }
165        }
166
167    }
168
169    /**
170     * テンプレート取得
171     *
172     */
173    function getTemplate() {
174        return $this->template;
175    }
176
177    /**
178     * テンプレート設定(ポップアップなどの場合)
179     *
180     */
181    function setTemplate($template) {
182        $this->template = $template;
183    }
184
185    /**
186     * 指定の URL へリダイレクトする.
187     *
188     * リダイレクト先 URL に HTTP_URL 及び HTTPS_URL を含むかチェックし,
189     * LC_Page::getToken() の値を URLパラメータで自動的に付与する.
190     *
191     * @param string $url リダイレクト先 URL
192     * @param boolean $isMobile モバイル用にセッションIDを付与する場合 true
193     * @return void|boolean $url に HTTP_URL 及び, HTTPS_URL を含まない場合 false,
194     *                       正常に遷移可能な場合は, $url の ロケーションヘッダを出力する.
195     * @see Net_URL
196     */
197    function sendRedirect($url, $isMobile = false) {
198echo "SC_Response.php::sendRedirect()に移行してね。";
199exit;
200
201        if (preg_match("/(" . preg_quote(HTTP_URL, '/')
202                          . "|" . preg_quote(HTTPS_URL, '/') . ")/", $url)) {
203
204            $netURL = new Net_URL($url);
205            if (!empty($_SERVER['QUERY_STRING'])) {
206                $netURL->addRawQueryString($_SERVER['QUERY_STRING']);
207            }
208
209            $session = SC_SessionFactory::getInstance();
210            if ($isMobile || $session->useCookie() == false) {
211                $netURL->addQueryString(session_name(), session_id());
212            }
213
214            $netURL->addQueryString(TRANSACTION_ID_NAME, $this->getToken());
215            header("Location: " . $netURL->getURL());
216            //return true;
217            exit();
218        }
219        return false;
220    }
221
222    // }}}
223    // {{{ protected functions
224
225    /**
226     * トランザクショントークンを生成し, 取得する.
227     *
228     * 悪意のある不正な画面遷移を防止するため, 予測困難な文字列を生成して返す.
229     * 同時に, この文字列をセッションに保存する.
230     *
231     * この関数を使用するためには, 生成した文字列を次画面へ渡すパラメータとして
232     * 出力する必要がある.
233     *
234     * 例)
235     * <input type="hidden" name="transactionid" value="この関数の返り値" />
236     *
237     * 遷移先のページで, LC_Page::isValidToken() の返り値をチェックすることにより,
238     * 画面遷移の妥当性が確認できる.
239     *
240     * @access protected
241     * @return string トランザクショントークンの文字列
242     */
243    function getToken() {
244echo "SC_Helper_Session.php::getToken()に移行してね。";
245exit;
246        if (empty($_SESSION[TRANSACTION_ID_NAME])) {
247            $_SESSION[TRANSACTION_ID_NAME] = $this->createToken();
248        }
249        return $_SESSION[TRANSACTION_ID_NAME];
250    }
251
252    /**
253     * トランザクショントークンの妥当性をチェックする.
254     *
255     * 前画面で生成されたトランザクショントークンの妥当性をチェックする.
256     * この関数を使用するためには, 前画面のページクラスで LC_Page::getToken()
257     * を呼んでおく必要がある.
258     *
259     * @access protected
260     * @return boolean トランザクショントークンが有効な場合 true
261     */
262    function isValidToken() {
263echo "SC_Helper_Session.php::isValidToken()に移行してね。";
264exit;
265
266        $checkToken = "";
267
268        // $_POST の値を優先する
269        if (isset($_POST[TRANSACTION_ID_NAME])) {
270
271            $checkToken = $_POST[TRANSACTION_ID_NAME];
272        } elseif (isset($_GET[TRANSACTION_ID_NAME])) {
273
274            $checkToken = $_GET[TRANSACTION_ID_NAME];
275        }
276
277        $ret = false;
278        // token の妥当性チェック
279        if ($checkToken === $_SESSION[TRANSACTION_ID_NAME]) {
280
281            $ret = true;
282        }
283
284        unset($_SESSION[TRANSACTION_ID_NAME]);
285        return $ret;
286    }
287
288    /**
289     * $path から URL を取得する.
290     *
291     * 以下の順序で 引数 $path から URL を取得する.
292     * 1. realpath($path) で $path の 絶対パスを取得
293     * 2. $_SERVER['DOCUMENT_ROOT'] と一致する文字列を削除
294     * 3. $useSSL の値に応じて, HTTP_URL 又は, HTTPS_URL を付与する.
295     *
296     * 返り値に, QUERY_STRING を含めたい場合は, key => value 形式
297     * の配列を $param へ渡す.
298     *
299     * @access protected
300     * @param string $path 結果を取得するためのパス
301     * @param array $param URL に付与するパラメータの配列
302     * @param mixed $useSSL 結果に HTTPS_URL を使用する場合 true,
303     *                         HTTP_URL を使用する場合 false,
304     *                         デフォルト "escape" 現在のスキーマを使用
305     * @return string $path の存在する http(s):// から始まる絶対パス
306     * @see Net_URL
307     */
308    function getLocation($path, $param = array(), $useSSL = "escape") {
309        $rootPath = $this->getRootPath($path);
310
311        // スキーマを定義
312        if ($useSSL === true) {
313            $url = HTTPS_URL . $rootPath;
314        } elseif ($useSSL === false){
315            $url = HTTP_URL . $rootPath;
316        } elseif ($useSSL == "escape") {
317            if (SC_Utils_Ex::sfIsHTTPS()) {
318                $url = HTTPS_URL . $rootPath;
319            } else {
320                $url = HTTP_URL . $rootPath;
321            }
322        } else {
323            die("[BUG] Illegal Parametor of \$useSSL ");
324        }
325
326        $netURL = new Net_URL($url);
327        // QUERY_STRING 生成
328        foreach ($param as $key => $val) {
329            $netURL->addQueryString($key, $val);
330        }
331
332        return $netURL->getURL();
333    }
334
335    /**
336     * EC-CUBE のWEBルート(/html/)を / としたパスを返す
337     *
338     * @param string $path 結果を取得するためのパス
339     * @return string EC-CUBE のWEBルート(/html/)を / としたパス
340     */
341    function getRootPath($path) {
342        // Windowsの場合は, ディレクトリの区切り文字を\から/に変換する
343        $path = str_replace('\\', '/', $path);
344        $htmlPath = str_replace('\\', '/', HTML_REALDIR);
345       
346        // PHP 5.1 対策 ( http://xoops.ec-cube.net/modules/newbb/viewtopic.php?topic_id=4277&forum=9 )
347        if (strlen($path) == 0) {
348            $path = '.';
349        }
350       
351        // $path が / で始まっている場合
352        if (substr($path, 0, 1) == '/') {
353            $realPath = realpath($htmlPath . substr_replace($path, '', 0, strlen(URL_PATH)));
354        // 相対パスの場合
355        } else {
356            $realPath = realpath($path);
357        }
358        $realPath = str_replace('\\', '/', $realPath);
359       
360        // $path が / で終わっている場合、realpath によって削られた末尾の / を復元する。
361        if (substr($path, -1, 1) == '/' && substr($realPath, -1, 1) != '/') {
362            $realPath .= '/';
363        }
364       
365        // HTML_REALDIR を削除した文字列を取得.
366        $rootPath = str_replace($htmlPath, '', $realPath);
367        $rootPath = ltrim($rootPath, '/');
368
369        return $rootPath;
370    }
371
372    /**
373     * ページをリロードする.
374     *
375     * 引数 $queryString に, $_SERVER['QUERY_STRING'] の値を使用してはならない.
376     * この関数は, 内部で LC_Page::sendRedirect() を使用するため,
377     * $_SERVER['QUERY_STRING'] の値は自動的に付与される.
378     *
379     * @param array $queryString QueryString の配列
380     * @param bool $removeQueryString 付与されていた QueryString を削除する場合 true
381     * @return void
382     * @see Net_URL
383     */
384    function reload($queryString = array(), $removeQueryString = false) {
385echo "SC_Display.php::reload()に移行してね。";
386exit;
387
388        // 現在の URL を取得
389        $netURL = new Net_URL($_SERVER['REQUEST_URI']);
390
391        if ($removeQueryString) {
392            $netURL->querystring = array();
393            $_SERVER['QUERY_STRING'] = ''; // sendRedirect() での処理用らしい
394        }
395
396        // QueryString を付与
397        if (!empty($queryString)) {
398            foreach ($queryString as $key => $val) {
399                $netURL->addQueryString($key, $val);
400            }
401        }
402
403        $this->sendRedirect($netURL->getURL());
404    }
405
406    /**
407     * 互換性確保用メソッド
408     *
409     * @access protected
410     * @return void
411     * @deprecated 決済モジュール互換のため
412     */
413    function allowClientCache() {
414        LC_Page::httpCacheControl('private');
415    }
416
417    /**
418     * クライアント・プロキシのキャッシュを制御する.
419     *
420     * @access protected
421     * @param string $mode (nocache/private)
422     * @return void
423     */
424    function httpCacheControl($mode = '') {
425        switch ($mode) {
426            case 'nocache':
427                header('Pragma: no-cache');
428                header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
429                header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
430                header('Last-Modified:');
431                break;
432               
433            case 'private':
434                $cache_expire = session_cache_expire() * 60;
435                header('Pragma: no-cache');                                                            // anti-proxy
436                header('Expires:');                                                                    // anti-mozilla
437                header("Cache-Control: private, max-age={$cache_expire}, pre-check={$cache_expire}");  // HTTP/1.1 client
438                header('Last-Modified:');
439                break;
440               
441            default:
442                break;
443        }
444    }
445
446    /**
447     * デバック出力を行う.
448     *
449     * デバック用途のみに使用すること.
450     *
451     * @access protected
452     * @param mixed $val デバックする要素
453     * @return void
454     */
455    function p($val) {
456        SC_Utils_Ex::sfPrintR($val);
457    }
458
459    // }}}
460    // {{{ private functions
461
462    /**
463     * トランザクショントークン用の予測困難な文字列を生成して返す.
464     *
465     * @access private
466     * @return string トランザクショントークン用の文字列
467     */
468    function createToken() {
469echo "SC_Helper_Session::createToken()に移行してね。";
470exit;
471        return sha1(uniqid(rand(), true));
472    }
473}
474?>
Note: See TracBrowser for help on using the repository browser.