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

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