source: branches/version-2_12-dev/data/class/SC_Response.php @ 21441

Revision 21441, 11.7 KB checked in by Seasoft, 12 years ago (diff)

#1613 (ソース整形・ソースコメントの改善)

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