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

Revision 22856, 13.6 KB checked in by Seasoft, 11 years ago (diff)

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

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