1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Downloadable_DownloadController extends Mage_Core_Controller_Front_Action
35: {
36:
37: 38: 39: 40: 41:
42: protected function _getSession()
43: {
44: return Mage::getSingleton('core/session');
45: }
46:
47: 48: 49: 50: 51:
52: protected function _getCustomerSession()
53: {
54: return Mage::getSingleton('customer/session');
55: }
56:
57: protected function _processDownload($resource, $resourceType)
58: {
59: $helper = Mage::helper('downloadable/download');
60:
61:
62: $helper->setResource($resource, $resourceType);
63:
64: $fileName = $helper->getFilename();
65: $contentType = $helper->getContentType();
66:
67: $this->getResponse()
68: ->setHttpResponseCode(200)
69: ->setHeader('Pragma', 'public', true)
70: ->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
71: ->setHeader('Content-type', $contentType, true);
72:
73: if ($fileSize = $helper->getFilesize()) {
74: $this->getResponse()
75: ->setHeader('Content-Length', $fileSize);
76: }
77:
78: if ($contentDisposition = $helper->getContentDisposition()) {
79: $this->getResponse()
80: ->setHeader('Content-Disposition', $contentDisposition . '; filename='.$fileName);
81: }
82:
83: $this->getResponse()
84: ->clearBody();
85: $this->getResponse()
86: ->sendHeaders();
87:
88: $helper->output();
89: }
90:
91: 92: 93: 94:
95: public function sampleAction()
96: {
97: $sampleId = $this->getRequest()->getParam('sample_id', 0);
98: $sample = Mage::getModel('downloadable/sample')->load($sampleId);
99: if ($sample->getId()) {
100: $resource = '';
101: $resourceType = '';
102: if ($sample->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_URL) {
103: $resource = $sample->getSampleUrl();
104: $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_URL;
105: } elseif ($sample->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
106: $resource = Mage::helper('downloadable/file')->getFilePath(
107: Mage_Downloadable_Model_Sample::getBasePath(), $sample->getSampleFile()
108: );
109: $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_FILE;
110: }
111: try {
112: $this->_processDownload($resource, $resourceType);
113: exit(0);
114: } catch (Mage_Core_Exception $e) {
115: $this->_getSession()->addError(Mage::helper('downloadable')->__('Sorry, there was an error getting requested content. Please contact the store owner.'));
116: }
117: }
118: return $this->_redirectReferer();
119: }
120:
121: 122: 123: 124:
125: public function linkSampleAction()
126: {
127: $linkId = $this->getRequest()->getParam('link_id', 0);
128: $link = Mage::getModel('downloadable/link')->load($linkId);
129: if ($link->getId()) {
130: $resource = '';
131: $resourceType = '';
132: if ($link->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_URL) {
133: $resource = $link->getSampleUrl();
134: $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_URL;
135: } elseif ($link->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
136: $resource = Mage::helper('downloadable/file')->getFilePath(
137: Mage_Downloadable_Model_Link::getBaseSamplePath(), $link->getSampleFile()
138: );
139: $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_FILE;
140: }
141: try {
142: $this->_processDownload($resource, $resourceType);
143: exit(0);
144: } catch (Mage_Core_Exception $e) {
145: $this->_getCustomerSession()->addError(Mage::helper('downloadable')->__('Sorry, there was an error getting requested content. Please contact the store owner.'));
146: }
147: }
148: return $this->_redirectReferer();
149: }
150:
151: 152: 153:
154: public function linkAction()
155: {
156: $id = $this->getRequest()->getParam('id', 0);
157: $linkPurchasedItem = Mage::getModel('downloadable/link_purchased_item')->load($id, 'link_hash');
158: if (! $linkPurchasedItem->getId() ) {
159: $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__("Requested link does not exist."));
160: return $this->_redirect('*/customer/products');
161: }
162: if (!Mage::helper('downloadable')->getIsShareable($linkPurchasedItem)) {
163: $customerId = $this->_getCustomerSession()->getCustomerId();
164: if (!$customerId) {
165: $product = Mage::getModel('catalog/product')->load($linkPurchasedItem->getProductId());
166: if ($product->getId()) {
167: $notice = Mage::helper('downloadable')->__('Please log in to download your product or purchase <a href="%s">%s</a>.', $product->getProductUrl(), $product->getName());
168: } else {
169: $notice = Mage::helper('downloadable')->__('Please log in to download your product.');
170: }
171: $this->_getCustomerSession()->addNotice($notice);
172: $this->_getCustomerSession()->authenticate($this);
173: $this->_getCustomerSession()->setBeforeAuthUrl(Mage::getUrl('downloadable/customer/products/'),
174: array('_secure' => true)
175: );
176: return ;
177: }
178: $linkPurchased = Mage::getModel('downloadable/link_purchased')->load($linkPurchasedItem->getPurchasedId());
179: if ($linkPurchased->getCustomerId() != $customerId) {
180: $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__("Requested link does not exist."));
181: return $this->_redirect('*/customer/products');
182: }
183: }
184: $downloadsLeft = $linkPurchasedItem->getNumberOfDownloadsBought()
185: - $linkPurchasedItem->getNumberOfDownloadsUsed();
186:
187: $status = $linkPurchasedItem->getStatus();
188: if ($status == Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_AVAILABLE
189: && ($downloadsLeft || $linkPurchasedItem->getNumberOfDownloadsBought() == 0)
190: ) {
191: $resource = '';
192: $resourceType = '';
193: if ($linkPurchasedItem->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_URL) {
194: $resource = $linkPurchasedItem->getLinkUrl();
195: $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_URL;
196: } elseif ($linkPurchasedItem->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
197: $resource = Mage::helper('downloadable/file')->getFilePath(
198: Mage_Downloadable_Model_Link::getBasePath(), $linkPurchasedItem->getLinkFile()
199: );
200: $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_FILE;
201: }
202: try {
203: $this->_processDownload($resource, $resourceType);
204: $linkPurchasedItem->setNumberOfDownloadsUsed($linkPurchasedItem->getNumberOfDownloadsUsed() + 1);
205:
206: if ($linkPurchasedItem->getNumberOfDownloadsBought() != 0 && !($downloadsLeft - 1)) {
207: $linkPurchasedItem->setStatus(Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_EXPIRED);
208: }
209: $linkPurchasedItem->save();
210: exit(0);
211: }
212: catch (Exception $e) {
213: $this->_getCustomerSession()->addError(
214: Mage::helper('downloadable')->__('An error occurred while getting the requested content. Please contact the store owner.')
215: );
216: }
217: } elseif ($status == Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_EXPIRED) {
218: $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__('The link has expired.'));
219: } elseif ($status == Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_PENDING
220: || $status == Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_PAYMENT_REVIEW
221: ) {
222: $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__('The link is not available.'));
223: } else {
224: $this->_getCustomerSession()->addError(
225: Mage::helper('downloadable')->__('An error occurred while getting the requested content. Please contact the store owner.')
226: );
227: }
228: return $this->_redirect('*/customer/products');
229: }
230:
231: }
232: