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

Revision 23124, 13.8 KB checked in by kimoto, 11 years ago (diff)

#2043 typo修正・ソース整形・ソースコメントの改善 for 2.13.0
PHP4的な書き方の修正

  • 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    public $objFormParam;
36
37    /** 基本Content-Type */
38    public $defaultContentType = 'Application/octet-stream';
39
40    /** 拡張Content-Type配列
41     * Application/octet-streamで対応出来ないファイルタイプのみ拡張子をキーに記述する
42     * 拡張子が本配列に存在しない場合は $defaultContentTypeを利用する */
43    public $arrContentType = array('apk' => 'application/vnd.android.package-archive',
44                                'pdf' => 'application/pdf'
45        );
46
47    /**
48     * Page を初期化する.
49     *
50     * @return void
51     */
52    public function init()
53    {
54        $this->skip_load_page_layout = true;
55        parent::init();
56        $this->allowClientCache();
57    }
58
59    /**
60     * Page のプロセス.
61     *
62     * @return void
63     */
64    public 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    public 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    public 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    public 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 od.product_id = ? AND od.product_class_id = ?';
188        $where .= ' AND ' . $dbFactory->getDownloadableDaysWhereSql('o');
189        $where .= ' = 1';
190        $arrRet = $objQuery->select($col, $table, $where,
191                                    array($customer_id, $order_id, $product_id, $product_class_id, $product_id, $product_class_id));
192
193        return $arrRet[0];
194    }
195
196    /* パラメーター情報の初期化 */
197    public function lfInitParam(&$objFormParam)
198    {
199        $objFormParam->addParam('customer_id', 'customer_id', INT_LEN, 'n', array('EXIST_CHECK','NUM_CHECK'));
200        $objFormParam->addParam('order_id', 'order_id', INT_LEN, 'n', array('EXIST_CHECK', 'NUM_CHECK'));
201        $objFormParam->addParam('product_id', 'product_id', INT_LEN, 'n', array('EXIST_CHECK','NUM_CHECK'));
202        $objFormParam->addParam('product_class_id', 'product_class_id', INT_LEN, 'n', array('EXIST_CHECK','NUM_CHECK'));
203    }
204
205    /* 入力内容のチェック */
206    public function lfCheckError(&$objFormParam)
207    {
208        $objErr = new SC_CheckError_Ex($objFormParam->getHashArray());
209        $objErr->arrErr = $objFormParam->checkError();
210
211        return $objErr->arrErr;
212    }
213
214    /**
215     * モバイル端末用ヘッダー出力処理
216     *
217     * @param string $realpath       ダウンロードファイルパス
218     * @param string $sdown_filename ダウンロード時の指定ファイル名
219     */
220    public function lfMobileHeader($realpath,$sdown_filename)
221    {
222        $objHelperMobile = new SC_Helper_Mobile_Ex();
223        //ファイルの拡張子からコンテンツタイプを取得する
224        $mime_type = $objHelperMobile->getMIMEType($realpath);
225        header('Content-Type: ' . $mime_type);
226        header('Content-Disposition: attachment; filename=' . $sdown_filename);
227        header('Accept-Ranges: bytes');
228        header('Last-Modified: ' . gmdate('D,d M Y H:i:s') . ' GMT');
229        header('Cache-Control: public');
230    }
231
232    /**
233     * モバイル端末(AU)ダウンロード処理
234     *
235     * @param string $realpath       ダウンロードファイルパス
236     * @param string $sdown_filename ダウンロード時の指定ファイル名
237     */
238    public function lfMobileAuDownload($realpath,$sdown_filename)
239    {
240        //モバイル用ヘッダー出力
241        $this->lfMobileHeader($realpath,$sdown_filename);
242        //ファイルサイズを取得する
243        $file_size = filesize($realpath);
244        //読み込み
245        $fp = fopen($realpath, 'rb');
246        if (isset($_SERVER['HTTP_RANGE'])) {
247            // 二回目以降のリクエスト
248            list($range_offset, $range_limit) = sscanf($_SERVER['HTTP_RANGE'], 'bytes=%d-%d');
249            $content_range = sprintf('bytes %d-%d/%d', $range_offset, $range_limit, $file_size);
250            $content_length = $range_limit - $range_offset + 1;
251            fseek($fp, $range_offset, SEEK_SET);
252            header('HTTP/1.1 206 Partial Content');
253            header('Content-Lenth: ' . $content_length);
254            header('Content-Range: ' . $content_range);
255        } else {
256            // 一回目のリクエスト
257            $content_length = $file_size;
258            header('Content-Length: ' . $content_length);
259        }
260        echo fread($fp, $content_length) ;
261        SC_Utils_Ex::sfFlush();
262    }
263
264    /**
265     * モバイル端末(AU以外)ダウンロード処理
266     *
267     * @param string $realpath       ダウンロードファイルパス
268     * @param string $sdown_filename ダウンロード時の指定ファイル名
269     */
270    public function lfMobileDownload($realpath,$sdown_filename)
271    {
272        //モバイル用ヘッダー出力
273        $this->lfMobileHeader($realpath,$sdown_filename);
274        //ファイルサイズを取得する
275        $file_size = filesize($realpath);
276
277        //出力用バッファをクリアする
278        @ob_end_clean();
279
280        //HTTP_RANGEがセットされていた場合
281        if (isset($_SERVER['HTTP_RANGE'])) {
282            // 二回目以降のリクエスト
283            list($a, $range) = explode('=',$_SERVER['HTTP_RANGE'],2);
284            list($range) = explode(',',$range,2);
285            list($range, $range_end) = explode('-', $range);
286            $range=intval($range);
287
288            if (!$range_end) {
289                $range_end=$file_size-1;
290            } else {
291                $range_end=intval($range_end);
292            }
293
294            $new_length = $range_end-$range+1;
295            header('HTTP/1.1 206 Partial Content');
296            header("Content-Length: $new_length");
297            header("Content-Range: bytes $range-$range_end/$file_size");
298        } else {
299            // 一回目のリクエスト
300            $new_length=$file_size;
301            header('Content-Length: '.$file_size);
302        }
303
304        //ファイル読み込み
305        $chunksize = 1*(DOWNLOAD_BLOCK*1024);
306        $bytes_send = 0;
307        if ($realpath = fopen($realpath, 'r')) {
308            // 二回目以降のリクエスト
309            if (isset($_SERVER['HTTP_RANGE'])) fseek($realpath, $range);
310
311            while (!feof($realpath) && (!connection_aborted()) && ($bytes_send<$new_length)) {
312                $buffer = fread($realpath, $chunksize);
313                print($buffer);
314                SC_Utils_Ex::sfFlush();
315                SC_Utils_Ex::extendTimeOut();
316                $bytes_send += strlen($buffer);
317            }
318            fclose($realpath);
319        }
320        die();
321    }
322
323    /**
324     * モバイル端末以外ダウンロード処理
325     *
326     * @param string $realpath       ダウンロードファイルパス
327     * @param string $sdown_filename ダウンロード時の指定ファイル名
328     */
329    public function lfDownload($realpath,$sdown_filename)
330    {
331        // 拡張子を取得
332        $extension = pathinfo($realpath, PATHINFO_EXTENSION);
333        $contentType = $this->defaultContentType;
334        // 拡張ContentType判定(拡張子をキーに拡張ContentType対象か判断)
335        if (isset($this->arrContentType[$extension])) {
336            // 拡張ContentType対象の場合は、ContentTypeを変更
337            $contentType = $this->arrContentType[$extension];
338        }
339        header('Content-Type: '.$contentType);
340        //ファイル名指定
341        header('Content-Disposition: attachment; filename="' . $sdown_filename . '"');
342        header('Content-Transfer-Encoding: binary');
343        //キャッシュ無効化
344        header('Expires: Mon, 26 Nov 1962 00:00:00 GMT');
345        header('Last-Modified: ' . gmdate('D,d M Y H:i:s') . ' GMT');
346        //IE6+SSL環境下は、キャッシュ無しでダウンロードできない
347        header('Cache-Control: private');
348        header('Pragma: private');
349        //ファイルサイズ指定
350        $zv_filesize = filesize($realpath);
351        header('Content-Length: ' . $zv_filesize);
352        //ファイル読み込み
353        $handle = fopen($realpath, 'rb');
354        if ($handle === false) {
355            SC_Utils_Ex::sfDispSiteError(DOWNFILE_NOT_FOUND,'',true);
356            SC_Response_Ex::actionExit();
357        }
358        while (!feof($handle)) {
359            echo fread($handle, DOWNLOAD_BLOCK*1024);
360            SC_Utils_Ex::sfFlush();
361            SC_Utils_Ex::extendTimeOut();
362        }
363        fclose($handle);
364    }
365}
Note: See TracBrowser for help on using the repository browser.