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