Пример #1
0
 /**
  * Set headers and send the file to the client
  *
  * @author Andreas Gohr <*****@*****.**>
  * @param string The full path to the file
  * @param string The Mime Type of the file
  */
 function sendFile($file, $mime, $overrideFileName = '')
 {
     global $vm_mainframe;
     // send headers
     header("Content-Type: {$mime}");
     list($start, $len) = vmConnector::http_rangeRequest(filesize($file));
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
     header('Pragma: public');
     header('Accept-Ranges: bytes');
     //application mime type is downloadable
     if (strtolower(substr($mime, 0, 11)) == 'application') {
         if ($overrideFileName == '') {
             $filename = basename($file);
         } else {
             $filename = $overrideFileName;
         }
         header('Content-Disposition: attachment; filename="' . $filename . '";');
     }
     $chunksize = 1 * (1024 * 1024);
     // send file contents
     $fp = @fopen($file, "rb");
     if ($fp) {
         fseek($fp, $start);
         //seek to start of range
         $chunk = $len > $chunksize ? $chunksize : $len;
         while (!feof($fp) && $chunk > 0) {
             @set_time_limit();
             // large files can take a lot of time
             print fread($fp, $chunk);
             flush();
             $len -= $chunk;
             $chunk = $len > $chunksize ? $chunksize : $len;
         }
         fclose($fp);
     } else {
         header("HTTP/1.0 500 Internal Server Error");
         print "Could not read {$file} - bad permissions?";
         $vm_mainframe->close(true);
     }
 }
Пример #2
0
 /**
  * Handles a download Request
  *
  * @param array $d
  * @return boolean
  */
 function download_request(&$d)
 {
     global $download_id, $VM_LANG, $vmLogger;
     $db = new ps_DB();
     $download_id = $db->getEscaped(vmGet($d, "download_id"));
     $q = "SELECT * FROM #__{vm}_product_download WHERE";
     $q .= " download_id = '{$download_id}'";
     $db->query($q);
     $db->next_record();
     $download_id = $db->f("download_id");
     $file_name = $db->f("file_name");
     if (strncmp($file_name, 'http', 4) !== 0) {
         $datei = DOWNLOADROOT . $file_name;
     } else {
         $datei = $file_name;
     }
     $download_max = $db->f("download_max");
     $end_date = $db->f("end_date");
     $zeit = time();
     if (!$download_id) {
         $vmLogger->err($VM_LANG->_('PHPSHOP_DOWNLOADS_ERR_INV', false));
         return false;
         //vmRedirect("index.php?option=com_virtuemart&page=shop.downloads", $d["error"]);
     } elseif ($download_max == "0") {
         $q = "DELETE FROM #__{vm}_product_download";
         $q .= " WHERE download_id = '" . $download_id . "'";
         $db->query($q);
         $db->next_record();
         $vmLogger->err($VM_LANG->_('PHPSHOP_DOWNLOADS_ERR_MAX', false));
         return false;
         //vmRedirect("index.php?option=com_virtuemart&page=shop.downloads", $d["error"]);
     } elseif ($end_date != "0" && $zeit > $end_date) {
         $q = "DELETE FROM #__{vm}_product_download";
         $q .= " WHERE download_id = '" . $download_id . "'";
         $db->query($q);
         $db->next_record();
         $vmLogger->err($VM_LANG->_('PHPSHOP_DOWNLOADS_ERR_EXP', false));
         return false;
         //vmRedirect("index.php?option=com_virtuemart&page=shop.downloads", $d["error"]);
     }
     require_once CLASSPATH . 'connectionTools.class.php';
     $download_count = true;
     if (@file_exists($datei)) {
         // Check if this is a request for a special range of the file (=Resume Download)
         $range_request = vmConnector::http_rangeRequest(filesize($datei), false);
         if ($range_request[0] == 0) {
             // this is not a request to resume a download,
             $download_count = true;
         } else {
             $download_count = false;
         }
     } else {
         $download_count = false;
     }
     // Parameter to check if the file should be removed after download, which is only true,
     // if we have a remote file, which was transferred to this server into a temporary file
     $unlink = false;
     if (strncmp($datei, 'http', 4) === 0) {
         require_once CLASSPATH . 'ps_product_files.php';
         $datei_local = ps_product_files::getRemoteFile($datei);
         if ($datei_local !== false) {
             $datei = $datei_local;
             $unlink = true;
         } else {
             $vmLogger->err($VM_LANG->_('VM_DOWNLOAD_FILE_NOTFOUND', false));
             return false;
         }
     } else {
         // Check, if file path is correct
         // and file is
         if (!@file_exists($datei)) {
             $vmLogger->err($VM_LANG->_('VM_DOWNLOAD_FILE_NOTFOUND', false));
             return false;
             //vmRedirect("index.php?option=com_virtuemart&page=shop.downloads", $d["error"]);
         }
         if (!@is_readable($datei)) {
             $vmLogger->err($VM_LANG->_('VM_DOWNLOAD_FILE_NOTREADABLE', false));
             return false;
             //vmRedirect("index.php?option=com_virtuemart&page=shop.downloads", $d["error"]);
         }
     }
     if ($download_count) {
         // decrement the download_max to limit the number of downloads
         $q = "UPDATE `#__{vm}_product_download` SET";
         $q .= " `download_max`=`download_max` - 1";
         $q .= " WHERE download_id = '" . $download_id . "'";
         $db->query($q);
         $db->next_record();
     }
     if ($end_date == "0") {
         // Set the Download Expiry Date, so the download can expire after DOWNLOAD_EXPIRE seconds
         $end_date = time('u') + DOWNLOAD_EXPIRE;
         $q = "UPDATE #__{vm}_product_download SET";
         $q .= " end_date={$end_date}";
         $q .= " WHERE download_id = '" . $download_id . "'";
         $db->query($q);
         $db->next_record();
     }
     if (ereg('Opera(/| )([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'])) {
         $UserBrowser = "Opera";
     } elseif (ereg('MSIE ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'])) {
         $UserBrowser = "IE";
     } else {
         $UserBrowser = '';
     }
     $mime_type = $UserBrowser == 'IE' || $UserBrowser == 'Opera' ? 'application/octetstream' : 'application/octet-stream';
     // dump anything in the buffer
     while (@ob_end_clean()) {
     }
     vmConnector::sendFile($datei, $mime_type, basename($file_name));
     if ($unlink) {
         // remove the temporarily downloaded remote file
         @unlink($datei);
     }
     $GLOBALS['vm_mainframe']->close(true);
 }