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

Revision 22021, 13.6 KB checked in by Seasoft, 12 years ago (diff)

#1620 (タイムアウトしそうなループ処理では、タイムアウトを延長する)
#1934 (ブラウザ強制送出は SC_Utils#sfFlush を使う)

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