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