source: branches/version-2_12-dev/data/class/pages/LC_Page.php @ 21743

Revision 21743, 14.1 KB checked in by AMUAMU, 12 years ago (diff)

#1754 (exit;を個別の処理でしない) #1692 (プラグイン機能) 各ファイルでフックポイントの呼出を書かないで、自動的にフックポイントを呼び出すように修正。

  • 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 * Web Page を制御する基底クラス
26 *
27 * Web Page を制御する Page クラスは必ずこのクラスを継承する.
28 * PHP4 ではこのような抽象クラスを作っても継承先で何でもできてしまうため、
29 * あまり意味がないが、アーキテクトを統一するために作っておく.
30 *
31 * @package Page
32 * @author LOCKON CO.,LTD.
33 * @version $Id:LC_Page.php 15532 2007-08-31 14:39:46Z nanasess $
34 */
35class LC_Page {
36
37    // {{{ properties
38
39    /** メインテンプレート */
40    var $tpl_mainpage;
41
42    /** テンプレートのカラム数 */
43    var $tpl_column_num;
44
45    /** メインナンバー */
46    var $tpl_mainno;
47
48    /** CSS のパス */
49    var $tpl_css;
50
51    /** JavaScript */
52    var $tpl_javascript;
53
54    /** タイトル */
55    var $tpl_title;
56
57    /** カテゴリ */
58    var $tpl_page_category;
59
60    /** ログインメールアドレス */
61    var $tpl_login_email;
62
63    /** HTML ロード後に実行する JavaScript コード */
64    var $tpl_onload;
65
66    /** トランザクションID */
67    var $transactionid;
68
69    /** メインテンプレート名 */
70    var $template = SITE_FRAME;
71
72    /** 店舗基本情報 */
73    var $arrSiteInfo;
74
75    /** プラグインを実行フラグ */
76    var $plugin_activate_flg = true;
77
78    // }}}
79    // {{{ functions
80
81    /**
82     * Page を初期化する.
83     *
84     * @return void
85     */
86    function init() {
87        // 開始時刻を設定する。
88        $this->timeStart = microtime(true);
89
90        $this->tpl_authority = $_SESSION['authority'];
91
92        // ディスプレイクラス生成
93        $this->objDisplay = new SC_Display_Ex();
94
95        $layout = new SC_Helper_PageLayout_Ex();
96        $layout->sfGetPageLayout($this, false, $_SERVER['SCRIPT_NAME'],
97                                 $this->objDisplay->detectDevice());
98
99        // スーパーフックポイントを実行.
100        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance($this->plugin_activate_flg);
101        $objPlugin->doAction('LC_Page_preProcess', array($this));
102
103        // 店舗基本情報取得
104        $this->arrSiteInfo = SC_Helper_DB_Ex::sfGetBasisData();
105
106        // トランザクショントークンの検証と生成
107        $this->doValidToken();
108        $this->setTokenTo();
109
110        // ローカルフックポイントを実行
111        $parent_class_name = get_parent_class($this);
112        if ($parent_class_name != 'LC_Page') {
113            $objPlugin->doAction($parent_class_name . '_action_before', array($this));
114        }
115        $class_name = get_class($this);
116        if ($parent_class_name != 'LC_Page' && $class_name != $parent_class_name) {
117            $objPlugin->doAction($class_name . '_action_before', array($this));
118        }
119    }
120
121    /**
122     * Page のプロセス.
123     *
124     * @return void
125     */
126    function process() {}
127
128    /**
129     * Page のレスポンス送信.
130     *
131     * @return void
132     */
133    function sendResponse() {
134        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance($this->plugin_activate_flg);
135        // ローカルフックポイントを実行
136        $parent_class_name = get_parent_class($this);
137        if ($parent_class_name != 'LC_Page') {
138            $objPlugin->doAction($parent_class_name . '_action_after', array($this));
139        }
140        $class_name = get_class($this);
141        if ($parent_class_name != 'LC_Page' && $class_name != $parent_class_name) {
142            $objPlugin->doAction($class_name . '_action_after', array($this));
143        }
144
145        // HeadNaviにpluginテンプレートを追加する.
146        $objPlugin->setHeadNaviBlocs($this->arrPageLayout['HeadNavi']);
147
148        // スーパーフックポイントを実行.
149        $objPlugin->doAction('LC_Page_process', array($this));
150
151        // ページクラス名をテンプレートに渡す
152        $arrBacktrace = debug_backtrace();
153        if (strlen($this->tpl_page_class_name) === 0) {
154            $this->tpl_page_class_name = $arrBacktrace[1]['class'];
155            $this->tpl_page_class_name = preg_replace('/_Ex$/', '', $this->tpl_page_class_name);
156        }
157
158        $this->objDisplay->prepare($this);
159        $this->objDisplay->response->write();
160    }
161
162    /**
163     * Page のレスポンス送信(ダウンロード).
164     *
165     * @return void
166     */
167    function sendResponseCSV($file_name, $data) {
168        $this->objDisplay->prepare($this);
169        $this->objDisplay->addHeader('Content-disposition', "attachment; filename=${file_name}");
170        $this->objDisplay->addHeader('Content-type', "application/octet-stream; name=${file_name}");
171        $this->objDisplay->addHeader('Cache-Control', '');
172        $this->objDisplay->addHeader('Pragma', '');
173
174        $this->objDisplay->response->body = $data;
175        $this->objDisplay->response->write();
176        SC_Response_Ex::actionExit();
177    }
178
179    /**
180     * デストラクタ.
181     *
182     * @return void
183     */
184    function destroy() {
185        // 一定時間以上かかったページの場合、ログ出力する。
186        // エラー画面の表示では $this->timeStart が出力されない
187        if (defined('PAGE_DISPLAY_TIME_LOG_MODE') && PAGE_DISPLAY_TIME_LOG_MODE == true && isset($this->timeStart)) {
188            $timeEnd = microtime(true);
189            $timeExecTime = $timeEnd - $this->timeStart;
190            if (defined('PAGE_DISPLAY_TIME_LOG_MIN_EXEC_TIME') && $timeExecTime >= (float)PAGE_DISPLAY_TIME_LOG_MIN_EXEC_TIME) {
191                $logMsg = sprintf('PAGE_DISPLAY_TIME_LOG [%.2fsec]', $timeExecTime);
192                GC_Utils_Ex::gfPrintLog($logMsg);
193            }
194        }
195
196    }
197
198    /**
199     * テンプレート取得
200     *
201     */
202    function getTemplate() {
203        return $this->template;
204    }
205
206    /**
207     * テンプレート設定(ポップアップなどの場合)
208     *
209     */
210    function setTemplate($template) {
211        $this->template = $template;
212    }
213
214    /**
215     * $path から URL を取得する.
216     *
217     * 以下の順序で 引数 $path から URL を取得する.
218     * 1. realpath($path) で $path の 絶対パスを取得
219     * 2. $_SERVER['DOCUMENT_ROOT'] と一致する文字列を削除
220     * 3. $useSSL の値に応じて, HTTP_URL 又は, HTTPS_URL を付与する.
221     *
222     * 返り値に, QUERY_STRING を含めたい場合は, key => value 形式
223     * の配列を $param へ渡す.
224     *
225     * @access protected
226     * @param string $path 結果を取得するためのパス
227     * @param array $param URL に付与するパラメーターの配列
228     * @param mixed $useSSL 結果に HTTPS_URL を使用する場合 true,
229     *                         HTTP_URL を使用する場合 false,
230     *                         デフォルト 'escape' 現在のスキーマを使用
231     * @return string $path の存在する http(s):// から始まる絶対パス
232     * @see Net_URL
233     */
234    function getLocation($path, $param = array(), $useSSL = 'escape') {
235        $rootPath = $this->getRootPath($path);
236
237        // スキーマを定義
238        if ($useSSL === true) {
239            $url = HTTPS_URL . $rootPath;
240        } elseif ($useSSL === false) {
241            $url = HTTP_URL . $rootPath;
242        } elseif ($useSSL == 'escape') {
243            if (SC_Utils_Ex::sfIsHTTPS()) {
244                $url = HTTPS_URL . $rootPath;
245            } else {
246                $url = HTTP_URL . $rootPath;
247            }
248        } else {
249            die("[BUG] Illegal Parametor of \$useSSL ");
250        }
251
252        $netURL = new Net_URL($url);
253        // QUERY_STRING 生成
254        foreach ($param as $key => $val) {
255            $netURL->addQueryString($key, $val);
256        }
257
258        return $netURL->getURL();
259    }
260
261    /**
262     * EC-CUBE のWEBルート(/html/)を / としたパスを返す
263     *
264     * @param string $path 結果を取得するためのパス
265     * @return string EC-CUBE のWEBルート(/html/)を / としたパス
266     */
267    function getRootPath($path) {
268        // Windowsの場合は, ディレクトリの区切り文字を\から/に変換する
269        $path = str_replace('\\', '/', $path);
270        $htmlPath = str_replace('\\', '/', HTML_REALDIR);
271
272        // PHP 5.1 対策 ( http://xoops.ec-cube.net/modules/newbb/viewtopic.php?topic_id=4277&forum=9)
273        if (strlen($path) == 0) {
274            $path = '.';
275        }
276
277        // $path が / で始まっている場合
278        if (substr($path, 0, 1) == '/') {
279            $realPath = realpath($htmlPath . substr_replace($path, '', 0, strlen(ROOT_URLPATH)));
280        // 相対パスの場合
281        } else {
282            $realPath = realpath($path);
283        }
284        $realPath = str_replace('\\', '/', $realPath);
285
286        // $path が / で終わっている場合、realpath によって削られた末尾の / を復元する。
287        if (substr($path, -1, 1) == '/' && substr($realPath, -1, 1) != '/') {
288            $realPath .= '/';
289        }
290
291        // HTML_REALDIR を削除した文字列を取得.
292        $rootPath = str_replace($htmlPath, '', $realPath);
293        $rootPath = ltrim($rootPath, '/');
294
295        return $rootPath;
296    }
297
298    /**
299     * 互換性確保用メソッド
300     *
301     * @access protected
302     * @return void
303     * @deprecated 決済モジュール互換のため
304     */
305    function allowClientCache() {
306        $this->httpCacheControl('private');
307    }
308
309    /**
310     * クライアント・プロキシのキャッシュを制御する.
311     *
312     * @access protected
313     * @param string $mode (nocache/private)
314     * @return void
315     */
316    function httpCacheControl($mode = '') {
317        switch ($mode) {
318            case 'nocache':
319                header('Pragma: no-cache');
320                header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
321                header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
322                header('Last-Modified:');
323                break;
324
325            case 'private':
326                $cache_expire = session_cache_expire() * 60;
327                header('Pragma: no-cache');                                                            // anti-proxy
328                header('Expires:');                                                                    // anti-mozilla
329                header("Cache-Control: private, max-age={$cache_expire}, pre-check={$cache_expire}");  // HTTP/1.1 client
330                header('Last-Modified:');
331                break;
332
333            default:
334                break;
335        }
336    }
337
338    /**
339     * リクエストパラメーター 'mode' を取得する.
340     *
341     * 1. $_GET['mode'] の値を取得する.
342     * 2. 1 が存在しない場合は $_POST['mode'] の値を取得する.
343     * 3. どちらも存在しない場合は null を返す.
344     *
345     * mode に, 半角英数字とアンダーバー(_) 以外の文字列が検出された場合は null を
346     * 返す.
347     *
348     * @access protected
349     * @return string $_GET['mode'] 又は $_POST['mode'] の文字列
350     */
351    function getMode() {
352        $pattern = '/^[a-zA-Z0-9_]+$/';
353        $mode = null;
354        if (isset($_GET['mode']) && preg_match($pattern, $_GET['mode'])) {
355            $mode =  $_GET['mode'];
356        } elseif (isset($_POST['mode']) && preg_match($pattern, $_POST['mode'])) {
357            $mode = $_POST['mode'];
358        }
359        return $mode;
360    }
361
362    /**
363     * POST アクセスの妥当性を検証する.
364     *
365     * 生成されたトランザクショントークンの妥当性を検証し,
366     * 不正な場合はエラー画面へ遷移する.
367     *
368     * この関数は, 基本的に init() 関数で呼び出され, POST アクセスの場合は自動的に
369     * トランザクショントークンを検証する.
370     * ページによって検証タイミングなどを制御する必要がある場合は, この関数を
371     * オーバーライドし, 個別に設定を行うこと.
372     *
373     * @access protected
374     * @param boolean $is_admin 管理画面でエラー表示をする場合 true
375     * @return void
376     */
377    function doValidToken($is_admin = false) {
378        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
379            if (!SC_Helper_Session_Ex::isValidToken(false)) {
380                if ($is_admin) {
381                    SC_Utils_Ex::sfDispError(INVALID_MOVE_ERRORR);
382                } else {
383                    SC_Utils_Ex::sfDispSiteError(PAGE_ERROR, '', true);
384                }
385                SC_Response_Ex::actionExit();
386            }
387        }
388    }
389
390    /**
391     * トランザクショントークンを取得し, 設定する.
392     *
393     * @access protected
394     * @return void
395     */
396    function setTokenTo() {
397        $this->transactionid = SC_Helper_Session_Ex::getToken();
398    }
399
400    /**
401     * 前方互換用
402     *
403     * @deprecated 2.12.0 GC_Utils_Ex::gfPrintLog を使用すること
404     */
405    function log($mess, $log_level) {
406        trigger_error('前方互換用メソッドが使用されました。', E_USER_WARNING);
407        // ログレベル=Debugの場合は、DEBUG_MODEがtrueの場合のみログ出力する
408        if ($log_level === 'Debug' && DEBUG_MODE === false) {
409            return;
410        }
411
412        // ログ出力
413        GC_Utils_Ex::gfPrintLog($mess, '', true);
414    }
415
416    /**
417     * デバック出力を行う.
418     *
419     * デバック用途のみに使用すること.
420     *
421     * @access protected
422     * @param mixed $val デバックする要素
423     * @return void
424     */
425    function p($val) {
426        SC_Utils_Ex::sfPrintR($val);
427    }
428}
Note: See TracBrowser for help on using the repository browser.