source: branches/version-2_13-dev/data/class/pages/LC_Page.php @ 23142

Revision 23142, 16.4 KB checked in by nanasess, 11 years ago (diff)

#2342 (JavaScript?のグローバルな宣言を減らす)

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