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

Revision 22567, 13.7 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

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