00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Downloadable_DownloadController extends Mage_Core_Controller_Front_Action
00035 {
00036
00037
00038
00039
00040
00041
00042 protected function _getSession()
00043 {
00044 return Mage::getSingleton('core/session');
00045 }
00046
00047
00048
00049
00050
00051
00052 protected function _getCustomerSession()
00053 {
00054 return Mage::getSingleton('customer/session');
00055 }
00056
00057 protected function _processDownload($resource, $resourceType)
00058 {
00059 $helper = Mage::helper('downloadable/download');
00060
00061
00062 $helper->setResource($resource, $resourceType);
00063
00064 $fileName = $helper->getFilename();
00065 $contentType = $helper->getContentType();
00066
00067 $this->getResponse()
00068 ->setHttpResponseCode(200)
00069 ->setHeader('Pragma', 'public', true)
00070 ->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
00071 ->setHeader('Content-type', $contentType, true);
00072
00073 if ($fileSize = $helper->getFilesize()) {
00074 $this->getResponse()
00075 ->setHeader('Content-Length', $fileSize);
00076 }
00077
00078 if ($contentDisposition = $helper->getContentDisposition()) {
00079 $this->getResponse()
00080 ->setHeader('Content-Disposition', $contentDisposition . '; filename='.$fileName);
00081 }
00082
00083 $this->getResponse()
00084 ->clearBody();
00085 $this->getResponse()
00086 ->sendHeaders();
00087
00088 $helper->output();
00089 }
00090
00091
00092
00093
00094
00095 public function sampleAction()
00096 {
00097 $sampleId = $this->getRequest()->getParam('sample_id', 0);
00098 $sample = Mage::getModel('downloadable/sample')->load($sampleId);
00099 if ($sample->getId()) {
00100 $resource = '';
00101 $resourceType = '';
00102 if ($sample->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_URL) {
00103 $resource = $sample->getSampleUrl();
00104 $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_URL;
00105 } elseif ($sample->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
00106 $resource = Mage::helper('downloadable/file')->getFilePath(
00107 Mage_Downloadable_Model_Sample::getBasePath(), $sample->getSampleFile()
00108 );
00109 $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_FILE;
00110 }
00111 try {
00112 $this->_processDownload($resource, $resourceType);
00113 exit(0);
00114 } catch (Mage_Core_Exception $e) {
00115 $this->_getSession()->addError(Mage::helper('downloadable')->__('Sorry, there was an error getting requested content. Please contact store owner.'));
00116 }
00117 }
00118 return $this->_redirectReferer();
00119 }
00120
00121
00122
00123
00124
00125 public function linkSampleAction()
00126 {
00127 $linkId = $this->getRequest()->getParam('link_id', 0);
00128 $link = Mage::getModel('downloadable/link')->load($linkId);
00129 if ($link->getId()) {
00130 $resource = '';
00131 $resourceType = '';
00132 if ($link->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_URL) {
00133 $resource = $link->getSampleUrl();
00134 $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_URL;
00135 } elseif ($link->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
00136 $resource = Mage::helper('downloadable/file')->getFilePath(
00137 Mage_Downloadable_Model_Link::getBaseSamplePath(), $link->getSampleFile()
00138 );
00139 $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_FILE;
00140 }
00141 try {
00142 $this->_processDownload($resource, $resourceType);
00143 exit(0);
00144 } catch (Mage_Core_Exception $e) {
00145 $this->_getCustomerSession()->addError(Mage::helper('downloadable')->__('Sorry, there was an error getting requested content. Please contact store owner.'));
00146 }
00147 }
00148 return $this->_redirectReferer();
00149 }
00150
00151
00152
00153
00154 public function linkAction()
00155 {
00156 $id = $this->getRequest()->getParam('id', 0);
00157 $linkPurchasedItem = Mage::getModel('downloadable/link_purchased_item')->load($id, 'link_hash');
00158 if (! $linkPurchasedItem->getId() ) {
00159 $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__("Requested link doesn't exist."));
00160 return $this->_redirect('*/customer/products');
00161 }
00162 if (!Mage::helper('downloadable')->getIsShareable($linkPurchasedItem)) {
00163 $customerId = $this->_getCustomerSession()->getCustomerId();
00164 if (!$customerId) {
00165 $product = Mage::getModel('catalog/product')->load($linkPurchasedItem->getProductId());
00166 if ($product->getId()) {
00167 $notice = Mage::helper('downloadable')->__(
00168 'Please log in to download your product or purchase <a href="%s">%s</a>.',
00169 $product->getProductUrl(), $product->getName()
00170 );
00171 } else {
00172 $notice = Mage::helper('downloadable')->__('Please log in to download your product.');
00173 }
00174 $this->_getCustomerSession()->addNotice($notice);
00175 $this->_getCustomerSession()->authenticate($this);
00176 $this->_getCustomerSession()->setBeforeAuthUrl(Mage::getUrl('downloadable/customer/products/'), array('_secure' => true));
00177 return ;
00178 }
00179 $linkPurchased = Mage::getModel('downloadable/link_purchased')->load($linkPurchasedItem->getPurchasedId());
00180 if ($linkPurchased->getCustomerId() != $customerId) {
00181 $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__("Requested link doesn't exist."));
00182 return $this->_redirect('*/customer/products');
00183 }
00184 }
00185 $downloadsLeft = $linkPurchasedItem->getNumberOfDownloadsBought() - $linkPurchasedItem->getNumberOfDownloadsUsed();
00186 if ($linkPurchasedItem->getStatus() == Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_AVAILABLE
00187 && ($downloadsLeft || $linkPurchasedItem->getNumberOfDownloadsBought() == 0)) {
00188 $resource = '';
00189 $resourceType = '';
00190 if ($linkPurchasedItem->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_URL) {
00191 $resource = $linkPurchasedItem->getLinkUrl();
00192 $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_URL;
00193 } elseif ($linkPurchasedItem->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
00194 $resource = Mage::helper('downloadable/file')->getFilePath(
00195 Mage_Downloadable_Model_Link::getBasePath(), $linkPurchasedItem->getLinkFile()
00196 );
00197 $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_FILE;
00198 }
00199 try {
00200 $this->_processDownload($resource, $resourceType);
00201 $linkPurchasedItem->setNumberOfDownloadsUsed(
00202 $linkPurchasedItem->getNumberOfDownloadsUsed()+1
00203 );
00204 if ($linkPurchasedItem->getNumberOfDownloadsBought() != 0
00205 && !($linkPurchasedItem->getNumberOfDownloadsBought() - $linkPurchasedItem->getNumberOfDownloadsUsed())) {
00206 $linkPurchasedItem->setStatus(Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_EXPIRED);
00207 }
00208 $linkPurchasedItem->save();
00209 exit(0);
00210 }
00211 catch (Exception $e) {
00212 $this->_getCustomerSession()->addError(
00213 Mage::helper('downloadable')->__('Sorry, there was an error getting requested content. Please contact store owner.')
00214 );
00215 }
00216 } elseif ($linkPurchasedItem->getStatus() == Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_EXPIRED) {
00217 $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__('Link has expired.'));
00218 } elseif ($linkPurchasedItem->getStatus() == Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_PENDING) {
00219 $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__('Link is not available.'));
00220 } else {
00221 $this->_getCustomerSession()->addError(
00222 Mage::helper('downloadable')->__('Sorry, there was an error getting requested content. Please contact store owner.')
00223 );
00224 }
00225 return $this->_redirect('*/customer/products');
00226 }
00227
00228 }