[19661] | 1 | <?php |
---|
[19705] | 2 | /* |
---|
| 3 | * This file is part of EC-CUBE |
---|
| 4 | * |
---|
[20764] | 5 | * Copyright(c) 2000-2011 LOCKON CO.,LTD. All Rights Reserved. |
---|
[19705] | 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 | */ |
---|
[19661] | 30 | class SC_Response{ |
---|
| 31 | |
---|
| 32 | /** |
---|
[19705] | 33 | * コンテンツタイプ |
---|
[19661] | 34 | * Enter description here ... |
---|
| 35 | * @var unknown_type |
---|
| 36 | */ |
---|
| 37 | var $contentType; |
---|
| 38 | var $body; |
---|
[19924] | 39 | var $statusCode; |
---|
[19661] | 40 | var $header = array(); |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * |
---|
| 44 | * Enter description here ... |
---|
| 45 | */ |
---|
| 46 | var $encoding; |
---|
| 47 | |
---|
[19705] | 48 | function SC_Response() { |
---|
[19661] | 49 | } |
---|
| 50 | |
---|
[19705] | 51 | /** |
---|
| 52 | * レスポンス出力を書き込む. |
---|
| 53 | */ |
---|
| 54 | function write() { |
---|
[19661] | 55 | $this->sendHeader(); |
---|
| 56 | echo $this->body; |
---|
| 57 | } |
---|
| 58 | |
---|
[19705] | 59 | function sendHeader() { |
---|
| 60 | // HTTPのヘッダ |
---|
[21441] | 61 | foreach ($this->header as $name => $head) { |
---|
[19661] | 62 | header($name.': '.$head); |
---|
| 63 | } |
---|
[19924] | 64 | if (strlen($this->statusCode) >= 1) { |
---|
| 65 | $this->sendHttpStatus($this->statusCode); |
---|
| 66 | } |
---|
[19661] | 67 | } |
---|
| 68 | |
---|
[19705] | 69 | function setContentType($contentType) { |
---|
[19661] | 70 | $this->header['Content-Type'] = $contentType; |
---|
| 71 | } |
---|
| 72 | |
---|
[21479] | 73 | function setResposeBody($body) { |
---|
[19661] | 74 | $this->body = $body; |
---|
| 75 | } |
---|
| 76 | |
---|
[19705] | 77 | function addHeader($name, $value) { |
---|
[19661] | 78 | $this->header[$name] = $value; |
---|
| 79 | } |
---|
| 80 | |
---|
[19705] | 81 | function containsHeader($name) { |
---|
[19661] | 82 | return isset($this->header[$name]); |
---|
| 83 | } |
---|
| 84 | |
---|
[19832] | 85 | /** |
---|
[19908] | 86 | * アプリケーション内でリダイレクトする |
---|
| 87 | * |
---|
[20939] | 88 | * 内部で生成する URL の searchpart は、下記の順で上書きしていく。(後勝ち) |
---|
| 89 | * 1. 引数 $inheritQueryString が true の場合、$_SERVER['QUERY_STRING'] |
---|
| 90 | * 2. $location に含まれる searchpart |
---|
| 91 | * 3. 引数 $arrQueryString |
---|
[19908] | 92 | * @param string $location 「url-path」「現在のURLからのパス」「URL」のいずれか。「../」の解釈は行なわない。 |
---|
[20939] | 93 | * @param array $arrQueryString URL に付加する searchpart |
---|
| 94 | * @param bool $inheritQueryString 現在のリクエストの searchpart を継承するか |
---|
| 95 | * @param bool|null $useSsl true:HTTPSを強制, false:HTTPを強制, null:継承 |
---|
[19832] | 96 | * @return void |
---|
| 97 | * @static |
---|
| 98 | */ |
---|
| 99 | function sendRedirect($location, $arrQueryString = array(), $inheritQueryString = false, $useSsl = null) { |
---|
[19661] | 100 | |
---|
[19834] | 101 | // url-path → URL 変換 |
---|
[19832] | 102 | if ($location[0] === '/') { |
---|
[20939] | 103 | $netUrl = new Net_URL($location); |
---|
[19834] | 104 | $location = $netUrl->getUrl(); |
---|
[19832] | 105 | } |
---|
[19834] | 106 | |
---|
[19832] | 107 | // URL の場合 |
---|
[19834] | 108 | if (preg_match('/^https?:/', $location)) { |
---|
[19832] | 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); |
---|
[21441] | 115 | } else { |
---|
[19832] | 116 | $pattern = '/^' . preg_quote(HTTPS_URL, '/') . '(.*)/'; |
---|
| 117 | $replacement = HTTP_URL . '\1'; |
---|
| 118 | $url = preg_replace($pattern, $replacement, $url); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | } |
---|
[19834] | 122 | // 現在のURLからのパス |
---|
[19832] | 123 | else { |
---|
| 124 | if (!is_bool($useSsl)) { |
---|
| 125 | $useSsl = SC_Utils_Ex::sfIsHTTPS(); |
---|
| 126 | } |
---|
| 127 | $netUrl = new Net_URL($useSsl ? HTTPS_URL : HTTP_URL); |
---|
[21704] | 128 | $netUrl->path = dirname($_SERVER['SCRIPT_NAME']) . '/' . $location; |
---|
[19832] | 129 | $url = $netUrl->getUrl(); |
---|
| 130 | } |
---|
[19765] | 131 | |
---|
[19834] | 132 | $pattern = '/^(' . preg_quote(HTTP_URL, '/') . '|' . preg_quote(HTTPS_URL, '/') . ')/'; |
---|
[19832] | 133 | |
---|
[19908] | 134 | // アプリケーション外へのリダイレクトは扱わない |
---|
| 135 | if (preg_match($pattern, $url) === 0) { |
---|
[21582] | 136 | trigger_error('', E_USER_ERROR); |
---|
[19908] | 137 | } |
---|
[19661] | 138 | |
---|
[19908] | 139 | $netUrl = new Net_URL($url); |
---|
[20938] | 140 | |
---|
[20939] | 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); |
---|
[19908] | 146 | } |
---|
[19765] | 147 | |
---|
[20939] | 148 | $netUrl->querystring = array_merge($netUrl->querystring, $arrQueryString); |
---|
[20938] | 149 | |
---|
[19908] | 150 | $session = SC_SessionFactory::getInstance(); |
---|
[21302] | 151 | if ((SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE) |
---|
| 152 | || ($session->useCookie() == false) |
---|
| 153 | ) { |
---|
[19908] | 154 | $netUrl->addQueryString(session_name(), session_id()); |
---|
[19661] | 155 | } |
---|
[19832] | 156 | |
---|
[19908] | 157 | $netUrl->addQueryString(TRANSACTION_ID_NAME, SC_Helper_Session_Ex::getToken()); |
---|
| 158 | $url = $netUrl->getURL(); |
---|
| 159 | |
---|
[19832] | 160 | header("Location: $url"); |
---|
| 161 | exit; |
---|
[19661] | 162 | } |
---|
| 163 | |
---|
[19832] | 164 | /** |
---|
[19913] | 165 | * /html/ からのパスを指定してリダイレクトする |
---|
[19834] | 166 | * |
---|
[19913] | 167 | * FIXME メソッド名を分かりやすくしたい。現状だと、引数が「url-path より後」とも「url-path」とも読み取れる。(前者が意図したいところ) |
---|
| 168 | * @param string $location /html/ からのパス。先頭に / を含むかは任意。「../」の解釈は行なわない。 |
---|
[19834] | 169 | * @return void |
---|
[19832] | 170 | * @static |
---|
| 171 | */ |
---|
[19834] | 172 | function sendRedirectFromUrlPath($location, $arrQueryString = array(), $inheritQueryString = false, $useSsl = null) { |
---|
[19972] | 173 | $location = ROOT_URLPATH . ltrim($location, '/'); |
---|
[19834] | 174 | SC_Response_Ex::sendRedirect($location, $arrQueryString, $inheritQueryString, $useSsl); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | * @static |
---|
| 179 | */ |
---|
[19832] | 180 | function reload($arrQueryString = array(), $removeQueryString = false) { |
---|
[19661] | 181 | // 現在の URL を取得 |
---|
[19832] | 182 | $netUrl = new Net_URL($_SERVER['REQUEST_URI']); |
---|
[19661] | 183 | |
---|
[19832] | 184 | if (!$removeQueryString) { |
---|
| 185 | $arrQueryString = array_merge($netUrl->querystring, $arrQueryString); |
---|
[19661] | 186 | } |
---|
[19832] | 187 | $netUrl->querystring = array(); |
---|
[19661] | 188 | |
---|
[19834] | 189 | SC_Response_Ex::sendRedirect($netUrl->getURL(), $arrQueryString); |
---|
[19661] | 190 | } |
---|
| 191 | |
---|
[19705] | 192 | function setHeader($headers) { |
---|
[19661] | 193 | $this->header = $headers; |
---|
| 194 | } |
---|
| 195 | |
---|
[19924] | 196 | function setStatusCode($statusCode = null) { |
---|
| 197 | $this->statusCode = $statusCode; |
---|
[19661] | 198 | } |
---|
| 199 | |
---|
[19817] | 200 | /** |
---|
| 201 | * HTTPステータスコードを送出する。 |
---|
| 202 | * |
---|
[19924] | 203 | * @param integer $statusCode HTTPステータスコード |
---|
[19817] | 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 | */ |
---|
[19924] | 215 | function sendHttpStatus($statusCode) { |
---|
[19817] | 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', // 期待するヘッダに失敗 |
---|
[20970] | 258 | // Server Error 5xx // 【サーバーエラー】 |
---|
| 259 | 500 => 'Internal Server Error', // サーバー内部エラー |
---|
[19817] | 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 | ); |
---|
[19924] | 267 | if (isset($messages[$statusCode])) { |
---|
[19817] | 268 | if ($httpVersion !== '1.1') { |
---|
| 269 | // HTTP/1.0 |
---|
| 270 | $messages[302] = 'Moved Temporarily'; |
---|
| 271 | } |
---|
[19924] | 272 | header("HTTP/{$httpVersion} {$statusCode} {$messages[$statusCode]}"); |
---|
| 273 | header("Status: {$statusCode} {$messages[$statusCode]}", true, $statusCode); |
---|
[19817] | 274 | } |
---|
| 275 | } |
---|
[19661] | 276 | } |
---|