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

Revision 21514, 13.3 KB checked in by Seasoft, 12 years ago (diff)

#1625 (typo修正・ソース整形・ソースコメントの改善)

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