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

Revision 23602, 16.5 KB checked in by kimoto, 10 years ago (diff)

#2448 typo修正・ソース整形・ソースコメントの改善 for 2.13.3

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