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

Revision 23388, 15.0 KB checked in by Seasoft, 10 years ago (diff)

#2534 (CSV 出力で一時ファイルが異常な状態で放置されることがある)
#2535 (CSV 生成の独自処理を減らす)
#2448 (typo修正・ソース整形・ソースコメントの改善 for 2.13.2)

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