PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/magento/app/code/core/Mage/Downloadable/controllers/DownloadController.php

https://bitbucket.org/jit_bec/shopifine
PHP | 231 lines | 161 code | 17 blank | 53 comment | 33 complexity | 398d0817301c16a13b4e0dfc847f33fd MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Downloadable
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Download controller
  28. *
  29. * @category Mage
  30. * @package Mage_Downloadable
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Downloadable_DownloadController extends Mage_Core_Controller_Front_Action
  34. {
  35. /**
  36. * Return core session object
  37. *
  38. * @return Mage_Core_Model_Session
  39. */
  40. protected function _getSession()
  41. {
  42. return Mage::getSingleton('core/session');
  43. }
  44. /**
  45. * Return customer session object
  46. *
  47. * @return Mage_Customer_Model_Session
  48. */
  49. protected function _getCustomerSession()
  50. {
  51. return Mage::getSingleton('customer/session');
  52. }
  53. protected function _processDownload($resource, $resourceType)
  54. {
  55. $helper = Mage::helper('downloadable/download');
  56. /* @var $helper Mage_Downloadable_Helper_Download */
  57. $helper->setResource($resource, $resourceType);
  58. $fileName = $helper->getFilename();
  59. $contentType = $helper->getContentType();
  60. $this->getResponse()
  61. ->setHttpResponseCode(200)
  62. ->setHeader('Pragma', 'public', true)
  63. ->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
  64. ->setHeader('Content-type', $contentType, true);
  65. if ($fileSize = $helper->getFilesize()) {
  66. $this->getResponse()
  67. ->setHeader('Content-Length', $fileSize);
  68. }
  69. if ($contentDisposition = $helper->getContentDisposition()) {
  70. $this->getResponse()
  71. ->setHeader('Content-Disposition', $contentDisposition . '; filename='.$fileName);
  72. }
  73. $this->getResponse()
  74. ->clearBody();
  75. $this->getResponse()
  76. ->sendHeaders();
  77. $helper->output();
  78. }
  79. /**
  80. * Download sample action
  81. *
  82. */
  83. public function sampleAction()
  84. {
  85. $sampleId = $this->getRequest()->getParam('sample_id', 0);
  86. $sample = Mage::getModel('downloadable/sample')->load($sampleId);
  87. if ($sample->getId()) {
  88. $resource = '';
  89. $resourceType = '';
  90. if ($sample->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_URL) {
  91. $resource = $sample->getSampleUrl();
  92. $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_URL;
  93. } elseif ($sample->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
  94. $resource = Mage::helper('downloadable/file')->getFilePath(
  95. Mage_Downloadable_Model_Sample::getBasePath(), $sample->getSampleFile()
  96. );
  97. $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_FILE;
  98. }
  99. try {
  100. $this->_processDownload($resource, $resourceType);
  101. exit(0);
  102. } catch (Mage_Core_Exception $e) {
  103. $this->_getSession()->addError(Mage::helper('downloadable')->__('Sorry, there was an error getting requested content. Please contact the store owner.'));
  104. }
  105. }
  106. return $this->_redirectReferer();
  107. }
  108. /**
  109. * Download link's sample action
  110. *
  111. */
  112. public function linkSampleAction()
  113. {
  114. $linkId = $this->getRequest()->getParam('link_id', 0);
  115. $link = Mage::getModel('downloadable/link')->load($linkId);
  116. if ($link->getId()) {
  117. $resource = '';
  118. $resourceType = '';
  119. if ($link->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_URL) {
  120. $resource = $link->getSampleUrl();
  121. $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_URL;
  122. } elseif ($link->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
  123. $resource = Mage::helper('downloadable/file')->getFilePath(
  124. Mage_Downloadable_Model_Link::getBaseSamplePath(), $link->getSampleFile()
  125. );
  126. $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_FILE;
  127. }
  128. try {
  129. $this->_processDownload($resource, $resourceType);
  130. exit(0);
  131. } catch (Mage_Core_Exception $e) {
  132. $this->_getCustomerSession()->addError(Mage::helper('downloadable')->__('Sorry, there was an error getting requested content. Please contact the store owner.'));
  133. }
  134. }
  135. return $this->_redirectReferer();
  136. }
  137. /**
  138. * Download link action
  139. */
  140. public function linkAction()
  141. {
  142. $id = $this->getRequest()->getParam('id', 0);
  143. $linkPurchasedItem = Mage::getModel('downloadable/link_purchased_item')->load($id, 'link_hash');
  144. if (! $linkPurchasedItem->getId() ) {
  145. $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__("Requested link does not exist."));
  146. return $this->_redirect('*/customer/products');
  147. }
  148. if (!Mage::helper('downloadable')->getIsShareable($linkPurchasedItem)) {
  149. $customerId = $this->_getCustomerSession()->getCustomerId();
  150. if (!$customerId) {
  151. $product = Mage::getModel('catalog/product')->load($linkPurchasedItem->getProductId());
  152. if ($product->getId()) {
  153. $notice = Mage::helper('downloadable')->__('Please log in to download your product or purchase <a href="%s">%s</a>.', $product->getProductUrl(), $product->getName());
  154. } else {
  155. $notice = Mage::helper('downloadable')->__('Please log in to download your product.');
  156. }
  157. $this->_getCustomerSession()->addNotice($notice);
  158. $this->_getCustomerSession()->authenticate($this);
  159. $this->_getCustomerSession()->setBeforeAuthUrl(Mage::getUrl('downloadable/customer/products/'),
  160. array('_secure' => true)
  161. );
  162. return ;
  163. }
  164. $linkPurchased = Mage::getModel('downloadable/link_purchased')->load($linkPurchasedItem->getPurchasedId());
  165. if ($linkPurchased->getCustomerId() != $customerId) {
  166. $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__("Requested link does not exist."));
  167. return $this->_redirect('*/customer/products');
  168. }
  169. }
  170. $downloadsLeft = $linkPurchasedItem->getNumberOfDownloadsBought()
  171. - $linkPurchasedItem->getNumberOfDownloadsUsed();
  172. $status = $linkPurchasedItem->getStatus();
  173. if ($status == Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_AVAILABLE
  174. && ($downloadsLeft || $linkPurchasedItem->getNumberOfDownloadsBought() == 0)
  175. ) {
  176. $resource = '';
  177. $resourceType = '';
  178. if ($linkPurchasedItem->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_URL) {
  179. $resource = $linkPurchasedItem->getLinkUrl();
  180. $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_URL;
  181. } elseif ($linkPurchasedItem->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
  182. $resource = Mage::helper('downloadable/file')->getFilePath(
  183. Mage_Downloadable_Model_Link::getBasePath(), $linkPurchasedItem->getLinkFile()
  184. );
  185. $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_FILE;
  186. }
  187. try {
  188. $this->_processDownload($resource, $resourceType);
  189. $linkPurchasedItem->setNumberOfDownloadsUsed($linkPurchasedItem->getNumberOfDownloadsUsed() + 1);
  190. if ($linkPurchasedItem->getNumberOfDownloadsBought() != 0 && !($downloadsLeft - 1)) {
  191. $linkPurchasedItem->setStatus(Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_EXPIRED);
  192. }
  193. $linkPurchasedItem->save();
  194. exit(0);
  195. }
  196. catch (Exception $e) {
  197. $this->_getCustomerSession()->addError(
  198. Mage::helper('downloadable')->__('An error occurred while getting the requested content. Please contact the store owner.')
  199. );
  200. }
  201. } elseif ($status == Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_EXPIRED) {
  202. $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__('The link has expired.'));
  203. } elseif ($status == Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_PENDING
  204. || $status == Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_PAYMENT_REVIEW
  205. ) {
  206. $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__('The link is not available.'));
  207. } else {
  208. $this->_getCustomerSession()->addError(
  209. Mage::helper('downloadable')->__('An error occurred while getting the requested content. Please contact the store owner.')
  210. );
  211. }
  212. return $this->_redirect('*/customer/products');
  213. }
  214. }