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

Revision 21743, 13.3 KB checked in by AMUAMU, 12 years ago (diff)

#1754 (exit;を個別の処理でしない) #1692 (プラグイン機能) 各ファイルでフックポイントの呼出を書かないで、自動的にフックポイントを呼び出すように修正。

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