/** * 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); }
/** * Sends the requested file to the browser * and assures that the requested file is no payable product download file * @author soeren * @param int $file_id * @param int $product_id * @return mixed */ function send_file($file_id, $product_id) { global $VM_LANG, $vmLogger, $mosConfig_absolute_path; $dbf = new ps_DB(); $html = ""; $sql = 'SELECT attribute_value FROM #__{vm}_product_attribute WHERE `product_id` = ' . intval($product_id) . ' AND attribute_name=\'download\''; $dbf->query($sql); $dbf->next_record(); $exclude_filename = $GLOBALS['vmInputFilter']->safeSQL($dbf->f("attribute_value")); $sql = 'SELECT file_mimetype, file_name' . ' FROM `#__{vm}_product_files` WHERE '; if ($exclude_filename) { $sql .= ' file_title != \'' . $exclude_filename . '\' AND '; } $sql .= ' file_product_id = \'' . $product_id . '\' AND file_published = \'1\' AND file_id = \'' . $file_id . '\' AND file_is_image = \'0\''; $dbf->setQuery($sql); $dbf->query(); if (!$dbf->next_record()) { $vmLogger->err($VM_LANG->_('PHPSHOP_FILES_NOT_FOUND', false)); return false; } $filename = $mosConfig_absolute_path . str_replace($mosConfig_absolute_path, '', $dbf->f("file_name")); // dump anything in the buffer while (@ob_end_clean()) { } if (strtolower(substr($filename, 0, 4)) == 'http') { vmRedirect($filename); } if ($filename) { require_once CLASSPATH . 'connectionTools.class.php'; vmConnector::sendFile($filename, $dbf->f("file_mimetype")); $GLOBALS['vm_mainframe']->close(true); } else { $vmLogger->err($VM_LANG->_('PHPSHOP_FILES_NOT_FOUND', false)); } return true; }