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

Revision 23051, 16.0 KB checked in by poego, 11 years ago (diff)

#1861 Googlebotのスマホ、Mobile対応

Google bot等のクローラーにUAごとにページ表示が変わることを通知するヘッダーを追加

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