source: branches/version-2_13_0/data/class/pages/LC_Page.php @ 23126

Revision 23126, 16.3 KB checked in by m_uehara, 11 years ago (diff)

#2348 r23116 - r23125 をマージ

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