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