source: branches/version-2_5-dev/data/class/SC_Response.php @ 20764

Revision 20764, 11.2 KB checked in by nanasess, 15 years ago (diff)

#601 (コピーライトの更新)

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