source: branches/version-2_13-dev/data/class/SC_Response.php @ 23605

Revision 23605, 15.1 KB checked in by kimoto, 10 years ago (diff)

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

Scrutinizer Auto-Fixes

This patch was automatically generated as part of the following inspection:
 https://scrutinizer-ci.com/g/nobuhiko/EC-CUBE/inspections/d8722894-69a6-4b1b-898d-43618035c60d

Enabled analysis tools:

  • PHP Analyzer
  • PHP PDepend
  • PHP Similarity Analyzer
  • PHP Change Tracking Analyzer
  • 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 * HttpResponse を扱うクラス.
26 *
27 * @author Ryuichi Tokugami
28 * @version $Id$
29 */
30class SC_Response
31{
32    /**
33     * コンテンツタイプ
34     * Enter description here ...
35     * @var unknown_type
36     */
37    public $contentType;
38    public $body;
39    public $statusCode;
40    public $header = array();
41
42    /**
43     *
44     * Enter description here ...
45     */
46    public $encoding;
47
48    /**
49     * レスポンス出力を書き込む.
50     */
51    public function write()
52    {
53        $this->sendHeader();
54        echo $this->body;
55    }
56
57    public function sendHeader()
58    {
59        // HTTPのヘッダ
60        foreach ($this->header as $name => $head) {
61            header($name.': '.$head);
62        }
63        if (strlen($this->statusCode) >= 1) {
64            $this->sendHttpStatus($this->statusCode);
65        }
66    }
67
68    /**
69     * @param string $contentType
70     */
71    public function setContentType($contentType)
72    {
73        $this->header['Content-Type'] = $contentType;
74    }
75
76    public function setResposeBody($body)
77    {
78        $this->body = $body;
79    }
80
81    public function addHeader($name, $value)
82    {
83        $this->header[$name] = $value;
84    }
85
86    public function containsHeader($name)
87    {
88        return isset($this->header[$name]);
89    }
90
91    /**
92     * アプリケーションのexit処理をする。以降の出力は基本的に停止する。
93     * 各クラス内では、exit を直接呼び出さない。
94     */
95    public function actionExit()
96    {
97        // ローカルフックポイント処理
98        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance($this->plugin_activate_flg);
99
100        if (is_object($objPlugin)) {
101            $arrBacktrace = debug_backtrace();
102            if (is_object($arrBacktrace[0]['object'])) {
103                $parent_class_name = get_parent_class($arrBacktrace[0]['object']);
104                $objPlugin->doAction($parent_class_name . '_action_' . $arrBacktrace[0]['object']->getMode(), array($arrBacktrace[0]['object']));
105                $class_name = get_class($arrBacktrace[0]['object']);
106                if ($class_name != $parent_class_name) {
107                    $objPlugin->doAction($class_name . '_action_' . $arrBacktrace[0]['object']->getMode(), array($arrBacktrace[0]['object']));
108                }
109            }
110        }
111
112        exit;
113        // デストラクタが実行される。
114    }
115
116    /**
117     * アプリケーション内でリダイレクトする
118     *
119     * 内部で生成する URL の searchpart は、下記の順で上書きしていく。(後勝ち)
120     * 1. 引数 $inheritQueryString が true の場合、$_SERVER['QUERY_STRING']
121     * 2. $location に含まれる searchpart
122     * 3. 引数 $arrQueryString
123     * @param  string    $location           「url-path」「現在のURLからのパス」「URL」のいずれか。「../」の解釈は行なわない。
124     * @param  array     $arrQueryString     URL に付加する searchpart
125     * @param  bool      $inheritQueryString 現在のリクエストの searchpart を継承するか
126     * @param  bool|null $useSsl             true:HTTPSを強制, false:HTTPを強制, null:継承
127     * @return void
128     * @static
129     */
130    public function sendRedirect($location, $arrQueryString = array(), $inheritQueryString = false, $useSsl = null)
131    {
132        // ローカルフックポイント処理
133        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance($this->plugin_activate_flg);
134
135        if (is_object($objPlugin)) {
136            $arrBacktrace = debug_backtrace();
137            if (is_object($arrBacktrace[0]['object']) && method_exists($arrBacktrace[0]['object'], 'getMode')) {
138                $parent_class_name = get_parent_class($arrBacktrace[0]['object']);
139                $objPlugin->doAction($parent_class_name . '_action_' . $arrBacktrace[0]['object']->getMode(), array($arrBacktrace[0]['object']));
140                $class_name = get_class($arrBacktrace[0]['object']);
141                if ($class_name != $parent_class_name) {
142                    $objPlugin->doAction($class_name . '_action_' . $arrBacktrace[0]['object']->getMode(), array($this));
143                }
144            } elseif (is_object($arrBacktrace[0]['object'])) {
145                $pattern = '/^[a-zA-Z0-9_]+$/';
146                $mode = null;
147                if (isset($_GET['mode']) && preg_match($pattern, $_GET['mode'])) {
148                    $mode =  $_GET['mode'];
149                } elseif (isset($_POST['mode']) && preg_match($pattern, $_POST['mode'])) {
150                    $mode = $_POST['mode'];
151                }
152                $parent_class_name = get_parent_class($arrBacktrace[0]['object']);
153                $objPlugin->doAction($parent_class_name . '_action_' . $mode, array($arrBacktrace[0]['object']));
154                $class_name = get_class($arrBacktrace[0]['object']);
155                if ($class_name != $parent_class_name) {
156                    $objPlugin->doAction($class_name . '_action_' . $mode, array($this));
157                }
158            }
159        }
160
161        // url-path → URL 変換
162        if ($location[0] === '/') {
163            $netUrl = new Net_URL($location);
164            $location = $netUrl->getUrl();
165        }
166
167        // URL の場合
168        if (preg_match('/^https?:/', $location)) {
169            $url = $location;
170            if (is_bool($useSsl)) {
171                if ($useSsl) {
172                    $pattern = '/^' . preg_quote(HTTP_URL, '/') . '(.*)/';
173                    $replacement = HTTPS_URL . '\1';
174                    $url = preg_replace($pattern, $replacement, $url);
175                } else {
176                    $pattern = '/^' . preg_quote(HTTPS_URL, '/') . '(.*)/';
177                    $replacement = HTTP_URL . '\1';
178                    $url = preg_replace($pattern, $replacement, $url);
179                }
180            }
181        // 現在のURLからのパス
182        } else {
183            if (!is_bool($useSsl)) {
184                $useSsl = SC_Utils_Ex::sfIsHTTPS();
185            }
186            $netUrl = new Net_URL($useSsl ? HTTPS_URL : HTTP_URL);
187            $netUrl->path = dirname($_SERVER['SCRIPT_NAME']) . '/' . $location;
188            $url = $netUrl->getUrl();
189        }
190
191        $pattern = '/^(' . preg_quote(HTTP_URL, '/') . '|' . preg_quote(HTTPS_URL, '/') . ')/';
192
193        // アプリケーション外へのリダイレクトは扱わない
194        if (preg_match($pattern, $url) === 0) {
195            trigger_error('', E_USER_ERROR);
196        }
197
198        $netUrl = new Net_URL($url);
199
200        if ($inheritQueryString && !empty($_SERVER['QUERY_STRING'])) {
201            $arrQueryStringBackup = $netUrl->querystring;
202            // XXX メソッド名は add で始まるが、実際には置換を行う
203            $netUrl->addRawQueryString($_SERVER['QUERY_STRING']);
204            $netUrl->querystring = array_merge($netUrl->querystring, $arrQueryStringBackup);
205        }
206
207        $netUrl->querystring = array_merge($netUrl->querystring, $arrQueryString);
208
209        $session = SC_SessionFactory_Ex::getInstance();
210        if ((SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE)
211            || ($session->useCookie() == false)
212        ) {
213            $netUrl->addQueryString(session_name(), session_id());
214        }
215
216        $netUrl->addQueryString(TRANSACTION_ID_NAME, SC_Helper_Session_Ex::getToken());
217        $url = $netUrl->getURL();
218
219        header("Location: $url");
220        exit;
221    }
222
223    /**
224     * /html/ からのパスを指定してリダイレクトする
225     *
226     * FIXME メソッド名を分かりやすくしたい。現状だと、引数が「url-path より後」とも「url-path」とも読み取れる。(前者が意図したいところ)
227     * @param  string $location /html/ からのパス。先頭に / を含むかは任意。「../」の解釈は行なわない。
228     * @return void
229     * @static
230     */
231    public function sendRedirectFromUrlPath($location, $arrQueryString = array(), $inheritQueryString = false, $useSsl = null)
232    {
233        $location = ROOT_URLPATH . ltrim($location, '/');
234        SC_Response_Ex::sendRedirect($location, $arrQueryString, $inheritQueryString, $useSsl);
235    }
236
237    /**
238     * @static
239     */
240    public function reload($arrQueryString = array(), $removeQueryString = false)
241    {
242        // 現在の URL を取得
243        $netUrl = new Net_URL($_SERVER['REQUEST_URI']);
244
245        if (!$removeQueryString) {
246            $arrQueryString = array_merge($netUrl->querystring, $arrQueryString);
247        }
248        $netUrl->querystring = array();
249
250        SC_Response_Ex::sendRedirect($netUrl->getURL(), $arrQueryString);
251    }
252
253    public function setHeader($headers)
254    {
255        $this->header = $headers;
256    }
257
258    public function setStatusCode($statusCode = null)
259    {
260        $this->statusCode = $statusCode;
261    }
262
263    /**
264     * HTTPステータスコードを送出する。
265     *
266     * @param  integer $statusCode HTTPステータスコード
267     * @return void
268     * @author Seasoft (新規作成)
269     * @see Moony_Action::status() (オリジナル)
270     * @link http://moony.googlecode.com/ (オリジナル)
271     * @author YAMAOKA Hiroyuki (オリジナル)
272     * @copyright 2005-2008 YAMAOKA Hiroyuki (オリジナル)
273     * @license http://opensource.org/licenses/bsd-license.php New BSD License (オリジナル)
274     * @link http://ja.wikipedia.org/wiki/HTTP%E3%82%B9%E3%83%86%E3%83%BC%E3%82%BF%E3%82%B9%E3%82%B3%E3%83%BC%E3%83%89 (邦訳)
275     * @license http://www.gnu.org/licenses/fdl.html GFDL (邦訳)
276     * @static
277     */
278    public function sendHttpStatus($statusCode)
279    {
280        $protocol = $_SERVER['SERVER_PROTOCOL'];
281        $httpVersion = (strpos($protocol, '1.1') !== false) ? '1.1' : '1.0';
282        $messages = array(
283            // Informational 1xx                        // 【情報】
284            100 => 'Continue',                          // 継続
285            101 => 'Switching Protocols',               // プロトコル切替え
286            // Success 2xx                              // 【成功】
287            200 => 'OK',                                // OK
288            201 => 'Created',                           // 作成
289            202 => 'Accepted',                          // 受理
290            203 => 'Non-Authoritative Information',     // 信頼できない情報
291            204 => 'No Content',                        // 内容なし
292            205 => 'Reset Content',                     // 内容のリセット
293            206 => 'Partial Content',                   // 部分的内容
294            // Redirection 3xx                          // 【リダイレクション】
295            300 => 'Multiple Choices',                  // 複数の選択
296            301 => 'Moved Permanently',                 // 恒久的に移動した
297            302 => 'Found',  // 1.1                     // 発見した (リクエストしたリソースは一時的に移動されているときに返される)
298            303 => 'See Other',                         // 他を参照せよ
299            304 => 'Not Modified',                      // 未更新
300            305 => 'Use Proxy',                         // プロキシを使用せよ
301            // 306 is no longer used but still reserved // 将来のために予約されている
302            307 => 'Temporary Redirect',                // 一時的リダイレクト
303            // Client Error 4xx                         // 【クライアントエラー】
304            400 => 'Bad Request',                       // リクエストが不正である
305            401 => 'Unauthorized',                      // 認証が必要である
306            402 => 'Payment Required',                  // 支払いが必要である
307            403 => 'Forbidden',                         // 禁止されている
308            404 => 'Not Found',                         // 未検出
309            405 => 'Method Not Allowed',                // 許可されていないメソッド
310            406 => 'Not Acceptable',                    // 受理できない
311            407 => 'Proxy Authentication Required',     // プロキシ認証が必要である
312            408 => 'Request Timeout',                   // リクエストタイムアウト
313            409 => 'Conflict',                          // 矛盾
314            410 => 'Gone',                              // 消滅した
315            411 => 'Length Required',                   // 長さが必要
316            412 => 'Precondition Failed',               // 前提条件で失敗した
317            413 => 'Request Entity Too Large',          // リクエストエンティティが大きすぎる
318            414 => 'Request-URI Too Long',              // リクエストURIが大きすぎる
319            415 => 'Unsupported Media Type',            // サポートしていないメディアタイプ
320            416 => 'Requested Range Not Satisfiable',   // リクエストしたレンジは範囲外にある
321            417 => 'Expectation Failed',                // 期待するヘッダに失敗
322            // Server Error 5xx                         // 【サーバーエラー】
323            500 => 'Internal Server Error',             // サーバー内部エラー
324            501 => 'Not Implemented',                   // 実装されていない
325            502 => 'Bad Gateway',                       // 不正なゲートウェイ
326            503 => 'Service Unavailable',               // サービス利用不可
327            504 => 'Gateway Timeout',                   // ゲートウェイタイムアウト
328            505 => 'HTTP Version Not Supported',        // サポートしていないHTTPバージョン
329            509 => 'Bandwidth Limit Exceeded'           // 帯域幅制限超過
330        );
331        if (isset($messages[$statusCode])) {
332            if ($httpVersion !== '1.1') {
333                // HTTP/1.0
334                $messages[302] = 'Moved Temporarily';
335            }
336            header("HTTP/{$httpVersion} {$statusCode} {$messages[$statusCode]}");
337            header("Status: {$statusCode} {$messages[$statusCode]}", true, $statusCode);
338        }
339    }
340
341    /**
342     * ダウンロード用の HTTP ヘッダを出力する
343     *
344     * @param string $file_name
345     * @return void
346     */
347    public static function headerForDownload($file_name) {
348        header("Content-disposition: attachment; filename={$file_name}");
349        header("Content-type: application/octet-stream; name={$file_name}");
350        header('Cache-Control: ');
351        header('Pragma: ');
352    }
353}
Note: See TracBrowser for help on using the repository browser.