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

Revision 21693, 13.7 KB checked in by h_yoshimoto, 12 years ago (diff)

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