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

Revision 20384, 11.9 KB checked in by nanasess, 13 years ago (diff)

#812(トランザクションIDの自動生成/自動検証)

  • トークンの生成タイミングを POST ごとから, 1セッションごとに修正
  • 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    /** HTML ロード後に実行する JavaScript コード */
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        $this->doValidToken();
105        $this->setTokenTo();
106    }
107
108    /**
109     * Page のプロセス.
110     *
111     * @return void
112     */
113    function process() {}
114
115    /**
116     * Page のレスポンス送信.
117     *
118     * @return void
119     */
120    function sendResponse() {
121
122        if (isset($this->objPlugin)) { // FIXME モバイルエラー応急対応
123            // post-prosess処理(暫定的)
124            $this->objPlugin->process($this);
125        }
126
127        $this->objDisplay->prepare($this);
128        $this->objDisplay->response->write();
129    }
130
131    /**
132     * Page のレスポンス送信(ダウンロード).
133     *
134     * @return void
135     */
136    function sendResponseCSV($file_name, $data) {
137        $this->objDisplay->prepare($this);
138        $this->objDisplay->addHeader("Content-disposition", "attachment; filename=${file_name}");
139        $this->objDisplay->addHeader("Content-type", "application/octet-stream; name=${file_name}");
140        $this->objDisplay->addHeader("Cache-Control", "");
141        $this->objDisplay->addHeader("Pragma", "");
142
143        $this->objDisplay->response->body = $data;
144        $this->objDisplay->response->write();
145        exit;
146    }
147
148    /**
149     * デストラクタ.
150     *
151     * @return void
152     */
153    function destroy() {
154        // 一定時間以上かかったページの場合、ログ出力する。
155        if(defined('PAGE_DISPLAY_TIME_LOG_MODE') && PAGE_DISPLAY_TIME_LOG_MODE == true) {
156            $timeEnd = SC_Utils_Ex::sfMicrotimeFloat();;
157            $timeExecTime = $timeEnd - $this->timeStart;
158            if(defined('PAGE_DISPLAY_TIME_LOG_MIN_EXEC_TIME') && $timeExecTime >= (float)PAGE_DISPLAY_TIME_LOG_MIN_EXEC_TIME) {
159                $logMsg = sprintf("PAGE_DISPLAY_TIME_LOG [%.2fsec]", $timeExecTime);
160                GC_Utils_Ex::gfPrintLog($logMsg);
161            }
162        }
163
164    }
165
166    /**
167     * テンプレート取得
168     *
169     */
170    function getTemplate() {
171        return $this->template;
172    }
173
174    /**
175     * テンプレート設定(ポップアップなどの場合)
176     *
177     */
178    function setTemplate($template) {
179        $this->template = $template;
180    }
181
182    /**
183     * $path から URL を取得する.
184     *
185     * 以下の順序で 引数 $path から URL を取得する.
186     * 1. realpath($path) で $path の 絶対パスを取得
187     * 2. $_SERVER['DOCUMENT_ROOT'] と一致する文字列を削除
188     * 3. $useSSL の値に応じて, HTTP_URL 又は, HTTPS_URL を付与する.
189     *
190     * 返り値に, QUERY_STRING を含めたい場合は, key => value 形式
191     * の配列を $param へ渡す.
192     *
193     * @access protected
194     * @param string $path 結果を取得するためのパス
195     * @param array $param URL に付与するパラメータの配列
196     * @param mixed $useSSL 結果に HTTPS_URL を使用する場合 true,
197     *                         HTTP_URL を使用する場合 false,
198     *                         デフォルト "escape" 現在のスキーマを使用
199     * @return string $path の存在する http(s):// から始まる絶対パス
200     * @see Net_URL
201     */
202    function getLocation($path, $param = array(), $useSSL = "escape") {
203        $rootPath = $this->getRootPath($path);
204
205        // スキーマを定義
206        if ($useSSL === true) {
207            $url = HTTPS_URL . $rootPath;
208        } elseif ($useSSL === false){
209            $url = HTTP_URL . $rootPath;
210        } elseif ($useSSL == "escape") {
211            if (SC_Utils_Ex::sfIsHTTPS()) {
212                $url = HTTPS_URL . $rootPath;
213            } else {
214                $url = HTTP_URL . $rootPath;
215            }
216        } else {
217            die("[BUG] Illegal Parametor of \$useSSL ");
218        }
219
220        $netURL = new Net_URL($url);
221        // QUERY_STRING 生成
222        foreach ($param as $key => $val) {
223            $netURL->addQueryString($key, $val);
224        }
225
226        return $netURL->getURL();
227    }
228
229    /**
230     * EC-CUBE のWEBルート(/html/)を / としたパスを返す
231     *
232     * @param string $path 結果を取得するためのパス
233     * @return string EC-CUBE のWEBルート(/html/)を / としたパス
234     */
235    function getRootPath($path) {
236        // Windowsの場合は, ディレクトリの区切り文字を\から/に変換する
237        $path = str_replace('\\', '/', $path);
238        $htmlPath = str_replace('\\', '/', HTML_REALDIR);
239
240        // PHP 5.1 対策 ( http://xoops.ec-cube.net/modules/newbb/viewtopic.php?topic_id=4277&forum=9 )
241        if (strlen($path) == 0) {
242            $path = '.';
243        }
244
245        // $path が / で始まっている場合
246        if (substr($path, 0, 1) == '/') {
247            $realPath = realpath($htmlPath . substr_replace($path, '', 0, strlen(ROOT_URLPATH)));
248        // 相対パスの場合
249        } else {
250            $realPath = realpath($path);
251        }
252        $realPath = str_replace('\\', '/', $realPath);
253
254        // $path が / で終わっている場合、realpath によって削られた末尾の / を復元する。
255        if (substr($path, -1, 1) == '/' && substr($realPath, -1, 1) != '/') {
256            $realPath .= '/';
257        }
258
259        // HTML_REALDIR を削除した文字列を取得.
260        $rootPath = str_replace($htmlPath, '', $realPath);
261        $rootPath = ltrim($rootPath, '/');
262
263        return $rootPath;
264    }
265
266    /**
267     * 互換性確保用メソッド
268     *
269     * @access protected
270     * @return void
271     * @deprecated 決済モジュール互換のため
272     */
273    function allowClientCache() {
274        $this->httpCacheControl('private');
275    }
276
277    /**
278     * クライアント・プロキシのキャッシュを制御する.
279     *
280     * @access protected
281     * @param string $mode (nocache/private)
282     * @return void
283     */
284    function httpCacheControl($mode = '') {
285        switch ($mode) {
286            case 'nocache':
287                header('Pragma: no-cache');
288                header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
289                header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
290                header('Last-Modified:');
291                break;
292
293            case 'private':
294                $cache_expire = session_cache_expire() * 60;
295                header('Pragma: no-cache');                                                            // anti-proxy
296                header('Expires:');                                                                    // anti-mozilla
297                header("Cache-Control: private, max-age={$cache_expire}, pre-check={$cache_expire}");  // HTTP/1.1 client
298                header('Last-Modified:');
299                break;
300
301            default:
302                break;
303        }
304    }
305
306    /**
307     * リクエストパラメータ "mode" を取得する.
308     *
309     * 1. $_GET['mode'] の値を取得する.
310     * 2. 1 が存在しない場合は $_POST['mode'] の値を取得する.
311     * 3. どちらも存在しない場合は null を返す.
312     *
313     * mode に, 半角英数字とアンダーバー(_) 以外の文字列が検出された場合は null を
314     * 返す.
315     *
316     * @access protected
317     * @return string $_GET['mode'] 又は $_POST['mode'] の文字列
318     */
319    function getMode() {
320        $pattern = '/^[a-zA-Z0-9_]+$/';
321        $mode = null;
322        if (isset($_GET['mode']) && preg_match($pattern, $_GET['mode'])) {
323            $mode =  $_GET['mode'];
324        } elseif (isset($_POST['mode']) && preg_match($pattern, $_POST['mode'])) {
325            $mode = $_POST['mode'];
326        }
327        return $mode;
328    }
329
330    /**
331     * POST アクセスの妥当性を検証する.
332     *
333     * 生成されたトランザクショントークンの妥当性を検証し,
334     * 不正な場合はエラー画面へ遷移する.
335     *
336     * この関数は, 基本的に init() 関数で呼び出され, POST アクセスの場合は自動的に
337     * トランザクショントークンを検証する.
338     * ページによって検証タイミングなどを制御する必要がある場合は, この関数を
339     * オーバーライドし, 個別に設定を行うこと.
340     *
341     * @access protected
342     * @param boolean $is_admin 管理画面でエラー表示をする場合 true
343     * @return void
344     */
345    function doValidToken($is_admin = false) {
346        if ($_SERVER["REQUEST_METHOD"] == "POST") {
347            if (!SC_Helper_Session_Ex::isValidToken(false)) {
348                if ($is_admin) {
349                    SC_Utils_Ex::sfDispError(INVALID_MOVE_ERRORR);
350                } else {
351                    SC_Utils_Ex::sfDispSiteError(PAGE_ERROR, "", true);
352                }
353                exit;
354            }
355        }
356    }
357
358    /**
359     * トランザクショントークンを取得し, 設定する.
360     *
361     * @access protected
362     * @return void
363     */
364    function setTokenTo() {
365        $this->transactionid = SC_Helper_Session_Ex::getToken();
366    }
367
368    /**
369     * デバック出力を行う.
370     *
371     * デバック用途のみに使用すること.
372     *
373     * @access protected
374     * @param mixed $val デバックする要素
375     * @return void
376     */
377    function p($val) {
378        SC_Utils_Ex::sfPrintR($val);
379    }
380}
381?>
Note: See TracBrowser for help on using the repository browser.