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