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