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