source: branches/version-2_11-dev/data/class/SC_Response.php @ 20938

Revision 20938, 11.2 KB checked in by Seasoft, 13 years ago (diff)

#1302 (商品一覧からカートインで「現在のカゴの中」画面のカテゴリブロックが一部選択色)

  • 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
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2011 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 */
30class 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    function SC_Response() {
49    }
50
51    /**
52     * レスポンス出力を書き込む.
53     */
54    function write() {
55        $this->sendHeader();
56        echo $this->body;
57    }
58
59    function sendHeader() {
60        // HTTPのヘッダ
61        foreach ($this->header as $name => $head){
62            header($name.': '.$head);
63        }
64        if (strlen($this->statusCode) >= 1) {
65            $this->sendHttpStatus($this->statusCode);
66        }
67    }
68
69    function setContentType($contentType) {
70        $this->header['Content-Type'] = $contentType;
71    }
72
73    function setResposeBody($body){
74        $this->body = $body;
75    }
76
77    function addHeader($name, $value) {
78        $this->header[$name] = $value;
79    }
80
81    function containsHeader($name) {
82        return isset($this->header[$name]);
83    }
84
85    /**
86     * アプリケーション内でリダイレクトする
87     *
88     * @param string $location 「url-path」「現在のURLからのパス」「URL」のいずれか。「../」の解釈は行なわない。
89     * @return void
90     * @static
91     */
92    function sendRedirect($location, $arrQueryString = array(), $inheritQueryString = false, $useSsl = null) {
93
94        // url-path → URL 変換
95        if ($location[0] === '/') {
96            $netUrl = new Net_URL();
97            $netUrl->path = $location;
98            $location = $netUrl->getUrl();
99        }
100
101        // URL の場合
102        if (preg_match('/^https?:/', $location)) {
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        }
117        // 現在のURLからのパス
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        }
126
127        $pattern = '/^(' . preg_quote(HTTP_URL, '/') . '|' . preg_quote(HTTPS_URL, '/') . ')/';
128
129        // アプリケーション外へのリダイレクトは扱わない
130        if (preg_match($pattern, $url) === 0) {
131            SC_Utils_Ex::sfDispException();
132        }
133
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']);
141            }
142        }
143
144        foreach ($arrQueryString as $key => $val) {
145            $netUrl->addQueryString($key, $val);
146        }
147
148        $url = $netUrl->getURL();
149
150        $session = SC_SessionFactory::getInstance();
151        if (SC_MobileUserAgent_Ex::isMobile() || $session->useCookie() == false) {
152            $netUrl->addQueryString(session_name(), session_id());
153        }
154
155        $netUrl->addQueryString(TRANSACTION_ID_NAME, SC_Helper_Session_Ex::getToken());
156        $url = $netUrl->getURL();
157
158        header("Location: $url");
159        exit;
160    }
161
162    /**
163     * /html/ からのパスを指定してリダイレクトする
164     *
165     * FIXME メソッド名を分かりやすくしたい。現状だと、引数が「url-path より後」とも「url-path」とも読み取れる。(前者が意図したいところ)
166     * @param string $location /html/ からのパス。先頭に / を含むかは任意。「../」の解釈は行なわない。
167     * @return void
168     * @static
169     */
170    function sendRedirectFromUrlPath($location, $arrQueryString = array(), $inheritQueryString = false, $useSsl = null) {
171        $location = ROOT_URLPATH . ltrim($location, '/');
172        SC_Response_Ex::sendRedirect($location, $arrQueryString, $inheritQueryString, $useSsl);
173    }
174
175    /**
176     * @static
177     */
178    function reload($arrQueryString = array(), $removeQueryString = false) {
179        // 現在の URL を取得
180        $netUrl = new Net_URL($_SERVER['REQUEST_URI']);
181
182        if (!$removeQueryString) {
183            $arrQueryString = array_merge($netUrl->querystring, $arrQueryString);
184        }
185        $netUrl->querystring = array();
186
187        SC_Response_Ex::sendRedirect($netUrl->getURL(), $arrQueryString);
188    }
189
190    function setHeader($headers) {
191        $this->header = $headers;
192    }
193
194    function setStatusCode($statusCode = null) {
195        $this->statusCode = $statusCode;
196    }
197
198    /**
199     * HTTPステータスコードを送出する。
200     *
201     * @param integer $statusCode HTTPステータスコード
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     */
213    function sendHttpStatus($statusCode) {
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        );
265        if (isset($messages[$statusCode])) {
266            if ($httpVersion !== '1.1') {
267                // HTTP/1.0
268                $messages[302] = 'Moved Temporarily';
269            }
270            header("HTTP/{$httpVersion} {$statusCode} {$messages[$statusCode]}");
271            header("Status: {$statusCode} {$messages[$statusCode]}", true, $statusCode);
272        }
273    }
274}
Note: See TracBrowser for help on using the repository browser.