source: branches/version-2_13-dev/data/class/pages/mypage/LC_Page_Mypage_DownLoad.php @ 23606

Revision 23606, 13.4 KB checked in by kimoto, 10 years ago (diff)

#2448 typo修正・ソース整形・ソースコメントの改善 for 2.13.3

 https://scrutinizer-ci.com/g/nobuhiko/EC-CUBE/inspections/e0f27994-b3c7-4fc6-ad70-55d3cd63769b/patches

  • 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 CUORE
4 *
5 * Copyright(c) 2009 CUORE CO.,LTD. All Rights Reserved.
6 *
7 * http://ec.cuore.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 */
23require_once CLASS_EX_REALDIR . 'page_extends/LC_Page_Ex.php';
24
25/**
26 * ダウンロード商品ダウンロード のページクラス.
27 *
28 * @package Page
29 * @author CUORE CO.,LTD.
30 * @version $Id$
31 */
32class LC_Page_Mypage_DownLoad extends LC_Page_Ex
33{
34    /** フォームパラメーターの配列 */
35    public $objFormParam;
36
37    /** 基本Content-Type */
38    public $defaultContentType = 'Application/octet-stream';
39
40    /** 拡張Content-Type配列
41     * Application/octet-streamで対応出来ないファイルタイプのみ拡張子をキーに記述する
42     * 拡張子が本配列に存在しない場合は $defaultContentTypeを利用する */
43    public $arrContentType = array('apk' => 'application/vnd.android.package-archive',
44                                'pdf' => 'application/pdf'
45        );
46
47    /**
48     * Page を初期化する.
49     *
50     * @return void
51     */
52    public function init()
53    {
54        $this->skip_load_page_layout = true;
55        parent::init();
56        $this->allowClientCache();
57    }
58
59    /**
60     * Page のプロセス.
61     *
62     * @return void
63     */
64    public function process()
65    {
66        ob_end_clean();
67        parent::process();
68        $this->action();
69        $this->sendResponse();
70    }
71
72    /**
73     * Page のAction.
74     *
75     * @return void
76     */
77    public function action()
78    {
79        // ログインチェック
80        $objCustomer = new SC_Customer_Ex();
81        if (!$objCustomer->isLoginSuccess(true)) {
82            SC_Utils_Ex::sfDispSiteError(DOWNFILE_NOT_FOUND, '', true);
83        }
84
85        // パラメーターチェック
86        $objFormParam = new SC_FormParam_Ex();
87        $this->lfInitParam($objFormParam);
88        // GET、SESSION['customer']値の取得
89        $objFormParam->setParam($_SESSION['customer']);
90        $objFormParam->setParam($_GET);
91        $this->arrErr = $this->lfCheckError($objFormParam);
92        if (count($this->arrErr)!=0) {
93            SC_Utils_Ex::sfDispSiteError(DOWNFILE_NOT_FOUND, '', true);
94        }
95
96    }
97
98    /**
99     * Page のResponse.
100     *
101     * todo たいした処理でないのに異常に処理が重い
102     * @return void
103     */
104    public function sendResponse()
105    {
106        // TODO sendResponseをオーバーライドしている為、afterフックポイントが実行されない.直接実行する.(#1790)
107        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance($this->plugin_activate_flg);
108        $objPlugin->doAction('LC_Page_Mypage_DownLoad_action_after', array($this));
109
110        $this->objDisplay->noAction();
111
112        // パラメーター取得
113        $customer_id = $_SESSION['customer']['customer_id'];
114        $order_id = $_GET['order_id'];
115        $product_class_id = $_GET['product_class_id'];
116
117        //DBから商品情報の読込
118        $arrForm = $this->lfGetRealFileName($customer_id, $order_id, $product_class_id);
119
120        //ファイル情報が無い場合はNG
121        if ($arrForm['down_realfilename'] == '') {
122            SC_Utils_Ex::sfDispSiteError(DOWNFILE_NOT_FOUND, '', true);
123        }
124        //ファイルそのものが無い場合もとりあえずNG
125        $realpath = DOWN_SAVE_REALDIR . $arrForm['down_realfilename'];
126        if (!file_exists($realpath)) {
127            SC_Utils_Ex::sfDispSiteError(DOWNFILE_NOT_FOUND, '', true);
128        }
129        //ファイル名をエンコードする Safariの対策はUTF-8で様子を見る
130        $encoding = 'Shift_JIS';
131        if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Safari')) {
132            $encoding = 'UTF-8';
133        }
134        $sdown_filename = mb_convert_encoding($arrForm['down_filename'], $encoding, 'auto');
135
136        // flushなどを利用しているので、現行のSC_Displayは利用できません。
137        // SC_DisplayやSC_Responseに大容量ファイルレスポンスが実装されたら移行可能だと思います。
138
139        // ダウンロード実行 モバイル端末はダウンロード方法が異なる
140        if (SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE) {
141            // キャリアがAUのモバイル端末はさらにダウンロード方法が異なる
142            if (SC_MobileUserAgent::getCarrier() == 'ezweb') {
143                // AUモバイル
144                $this->lfMobileAuDownload($realpath, $sdown_filename);
145            } else {
146                // AU以外のモバイル
147                $this->lfMobileDownload($realpath, $sdown_filename);
148            }
149        } else {
150            // PC、スマフォ
151            $this->lfDownload($realpath, $sdown_filename);
152        }
153    }
154
155    /**
156     * 商品情報の読み込みを行う.
157     *
158     * @param  integer $customer_id      会員ID
159     * @param  integer $order_id         受注ID
160     * @param  integer $product_class_id 商品規格ID
161     * @return string   商品情報の配列
162     */
163    public function lfGetRealFileName($customer_id, $order_id, $product_class_id)
164    {
165        $objQuery =& SC_Query_Ex::getSingletonInstance();
166        $col = <<< __EOS__
167            pc.down_realfilename AS down_realfilename,
168            pc.down_filename AS down_filename
169__EOS__;
170
171        $table = <<< __EOS__
172            dtb_order AS o
173            JOIN dtb_order_detail AS od USING(order_id)
174            JOIN dtb_products_class AS pc USING(product_id, product_class_id)
175__EOS__;
176
177        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
178        $where = 'o.customer_id = ? AND o.order_id = ? AND od.product_class_id = ?';
179        $where .= ' AND ' . $dbFactory->getDownloadableDaysWhereSql('o');
180        $where .= ' = 1';
181        $arrWhereVal = array($customer_id, $order_id, $product_class_id);
182        $arrRet = $objQuery->select($col, $table, $where, $arrWhereVal);
183
184        return $arrRet[0];
185    }
186
187    /* パラメーター情報の初期化 */
188
189    /**
190     * @param SC_FormParam_Ex $objFormParam
191     */
192    public function lfInitParam(&$objFormParam)
193    {
194        $objFormParam->addParam('customer_id', 'customer_id', INT_LEN, 'n', array('EXIST_CHECK', 'NUM_CHECK'));
195        $objFormParam->addParam('order_id', 'order_id', INT_LEN, 'n', array('EXIST_CHECK', 'NUM_CHECK'));
196        $objFormParam->addParam('product_class_id', 'product_class_id', INT_LEN, 'n', array('EXIST_CHECK', 'NUM_CHECK'));
197    }
198
199    /* 入力内容のチェック */
200
201    /**
202     * @param SC_FormParam_Ex $objFormParam
203     */
204    public function lfCheckError(&$objFormParam)
205    {
206        $objErr = new SC_CheckError_Ex($objFormParam->getHashArray());
207        $objErr->arrErr = $objFormParam->checkError();
208
209        return $objErr->arrErr;
210    }
211
212    /**
213     * モバイル端末用ヘッダー出力処理
214     *
215     * @param string $realpath       ダウンロードファイルパス
216     * @param string $sdown_filename ダウンロード時の指定ファイル名
217     */
218    public function lfMobileHeader($realpath, $sdown_filename)
219    {
220        $objHelperMobile = new SC_Helper_Mobile_Ex();
221        //ファイルの拡張子からコンテンツタイプを取得する
222        $mime_type = $objHelperMobile->getMIMEType($realpath);
223        header('Content-Type: ' . $mime_type);
224        header('Content-Disposition: attachment; filename=' . $sdown_filename);
225        header('Accept-Ranges: bytes');
226        header('Last-Modified: ' . gmdate('D,d M Y H:i:s') . ' GMT');
227        header('Cache-Control: public');
228    }
229
230    /**
231     * モバイル端末(AU)ダウンロード処理
232     *
233     * @param string $realpath       ダウンロードファイルパス
234     * @param string $sdown_filename ダウンロード時の指定ファイル名
235     */
236    public function lfMobileAuDownload($realpath, $sdown_filename)
237    {
238        //モバイル用ヘッダー出力
239        $this->lfMobileHeader($realpath, $sdown_filename);
240        //ファイルサイズを取得する
241        $file_size = filesize($realpath);
242        //読み込み
243        $fp = fopen($realpath, 'rb');
244        if (isset($_SERVER['HTTP_RANGE'])) {
245            // 二回目以降のリクエスト
246            list($range_offset, $range_limit) = sscanf($_SERVER['HTTP_RANGE'], 'bytes=%d-%d');
247            $content_range = sprintf('bytes %d-%d/%d', $range_offset, $range_limit, $file_size);
248            $content_length = $range_limit - $range_offset + 1;
249            fseek($fp, $range_offset, SEEK_SET);
250            header('HTTP/1.1 206 Partial Content');
251            header('Content-Lenth: ' . $content_length);
252            header('Content-Range: ' . $content_range);
253        } else {
254            // 一回目のリクエスト
255            $content_length = $file_size;
256            header('Content-Length: ' . $content_length);
257        }
258        echo fread($fp, $content_length);
259        SC_Utils_Ex::sfFlush();
260    }
261
262    /**
263     * モバイル端末(AU以外)ダウンロード処理
264     *
265     * @param string $realpath       ダウンロードファイルパス
266     * @param string $sdown_filename ダウンロード時の指定ファイル名
267     */
268    public function lfMobileDownload($realpath, $sdown_filename)
269    {
270        //モバイル用ヘッダー出力
271        $this->lfMobileHeader($realpath, $sdown_filename);
272        //ファイルサイズを取得する
273        $file_size = filesize($realpath);
274
275        //出力用バッファをクリアする
276        @ob_end_clean();
277
278        //HTTP_RANGEがセットされていた場合
279        if (isset($_SERVER['HTTP_RANGE'])) {
280            // 二回目以降のリクエスト
281            list($a, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
282            list($range) = explode(',', $range, 2);
283            list($range, $range_end) = explode('-', $range);
284            $range=intval($range);
285
286            if (!$range_end) {
287                $range_end=$file_size-1;
288            } else {
289                $range_end=intval($range_end);
290            }
291
292            $new_length = $range_end-$range+1;
293            header('HTTP/1.1 206 Partial Content');
294            header("Content-Length: $new_length");
295            header("Content-Range: bytes $range-$range_end/$file_size");
296        } else {
297            // 一回目のリクエスト
298            $new_length=$file_size;
299            header('Content-Length: '.$file_size);
300        }
301
302        //ファイル読み込み
303        $chunksize = 1*(DOWNLOAD_BLOCK*1024);
304        $bytes_send = 0;
305        if ($realpath = fopen($realpath, 'r')) {
306            // 二回目以降のリクエスト
307            if (isset($_SERVER['HTTP_RANGE'])) fseek($realpath, $range);
308
309            while (!feof($realpath) && (!connection_aborted()) && ($bytes_send<$new_length)) {
310                $buffer = fread($realpath, $chunksize);
311                print($buffer);
312                SC_Utils_Ex::sfFlush();
313                SC_Utils_Ex::extendTimeOut();
314                $bytes_send += strlen($buffer);
315            }
316            fclose($realpath);
317        }
318        die();
319    }
320
321    /**
322     * モバイル端末以外ダウンロード処理
323     *
324     * @param string $realpath       ダウンロードファイルパス
325     * @param string $sdown_filename ダウンロード時の指定ファイル名
326     */
327    public function lfDownload($realpath, $sdown_filename)
328    {
329        // 拡張子を取得
330        $extension = pathinfo($realpath, PATHINFO_EXTENSION);
331        $contentType = $this->defaultContentType;
332        // 拡張ContentType判定(拡張子をキーに拡張ContentType対象か判断)
333        if (isset($this->arrContentType[$extension])) {
334            // 拡張ContentType対象の場合は、ContentTypeを変更
335            $contentType = $this->arrContentType[$extension];
336        }
337        header('Content-Type: '.$contentType);
338        //ファイル名指定
339        header('Content-Disposition: attachment; filename="' . $sdown_filename . '"');
340        header('Content-Transfer-Encoding: binary');
341        //キャッシュ無効化
342        header('Expires: Mon, 26 Nov 1962 00:00:00 GMT');
343        header('Last-Modified: ' . gmdate('D,d M Y H:i:s') . ' GMT');
344        //IE6+SSL環境下は、キャッシュ無しでダウンロードできない
345        header('Cache-Control: private');
346        header('Pragma: private');
347        //ファイルサイズ指定
348        $zv_filesize = filesize($realpath);
349        header('Content-Length: ' . $zv_filesize);
350        //ファイル読み込み
351        $handle = fopen($realpath, 'rb');
352        if ($handle === false) {
353            SC_Utils_Ex::sfDispSiteError(DOWNFILE_NOT_FOUND, '', true);
354            SC_Response_Ex::actionExit();
355        }
356        while (!feof($handle)) {
357            echo fread($handle, DOWNLOAD_BLOCK*1024);
358            SC_Utils_Ex::sfFlush();
359            SC_Utils_Ex::extendTimeOut();
360        }
361        fclose($handle);
362    }
363}
Note: See TracBrowser for help on using the repository browser.