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