Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Downloadable_Adminhtml_Downloadable_FileController
  • Mage_Downloadable_Adminhtml_Downloadable_Product_EditController
  • Mage_Downloadable_Block_Adminhtml_Catalog_Product_Edit_Tab_Downloadable
  • Mage_Downloadable_Block_Adminhtml_Catalog_Product_Edit_Tab_Downloadable_Links
  • Mage_Downloadable_Block_Adminhtml_Catalog_Product_Edit_Tab_Downloadable_Samples
  • Mage_Downloadable_Block_Adminhtml_Sales_Items_Column_Downloadable_Name
  • Mage_Downloadable_Block_Catalog_Product_Links
  • Mage_Downloadable_Block_Catalog_Product_Samples
  • Mage_Downloadable_Block_Catalog_Product_View_Type
  • Mage_Downloadable_Block_Checkout_Cart_Item_Renderer
  • Mage_Downloadable_Block_Checkout_Success
  • Mage_Downloadable_Block_Customer_Products_List
  • Mage_Downloadable_Block_Sales_Order_Email_Items_Downloadable
  • Mage_Downloadable_Block_Sales_Order_Email_Items_Order_Downloadable
  • Mage_Downloadable_Block_Sales_Order_Item_Renderer_Downloadable
  • Mage_Downloadable_CustomerController
  • Mage_Downloadable_DownloadController
  • Mage_Downloadable_Helper_Catalog_Product_Configuration
  • Mage_Downloadable_Helper_Data
  • Mage_Downloadable_Helper_Download
  • Mage_Downloadable_Helper_File
  • Mage_Downloadable_Model_CatalogIndex_Data_Downloadable
  • Mage_Downloadable_Model_Link
  • Mage_Downloadable_Model_Link_Api
  • Mage_Downloadable_Model_Link_Api_Uploader
  • Mage_Downloadable_Model_Link_Api_V2
  • Mage_Downloadable_Model_Link_Api_Validator
  • Mage_Downloadable_Model_Link_Purchased
  • Mage_Downloadable_Model_Link_Purchased_Item
  • Mage_Downloadable_Model_Mysql4_Indexer_Price
  • Mage_Downloadable_Model_Mysql4_Link
  • Mage_Downloadable_Model_Mysql4_Link_Collection
  • Mage_Downloadable_Model_Mysql4_Link_Purchased
  • Mage_Downloadable_Model_Mysql4_Link_Purchased_Collection
  • Mage_Downloadable_Model_Mysql4_Link_Purchased_Item
  • Mage_Downloadable_Model_Mysql4_Link_Purchased_Item_Collection
  • Mage_Downloadable_Model_Mysql4_Sample
  • Mage_Downloadable_Model_Mysql4_Sample_Collection
  • Mage_Downloadable_Model_Observer
  • Mage_Downloadable_Model_Product_Price
  • Mage_Downloadable_Model_Product_Type
  • Mage_Downloadable_Model_Resource_Indexer_Price
  • Mage_Downloadable_Model_Resource_Link
  • Mage_Downloadable_Model_Resource_Link_Collection
  • Mage_Downloadable_Model_Resource_Link_Purchased
  • Mage_Downloadable_Model_Resource_Link_Purchased_Collection
  • Mage_Downloadable_Model_Resource_Link_Purchased_Item
  • Mage_Downloadable_Model_Resource_Link_Purchased_Item_Collection
  • Mage_Downloadable_Model_Resource_Sample
  • Mage_Downloadable_Model_Resource_Sample_Collection
  • Mage_Downloadable_Model_Sales_Order_Pdf_Items_Abstract
  • Mage_Downloadable_Model_Sales_Order_Pdf_Items_Creditmemo
  • Mage_Downloadable_Model_Sales_Order_Pdf_Items_Invoice
  • Mage_Downloadable_Model_Sample
  • Mage_Downloadable_Model_System_Config_Source_Contentdisposition
  • Mage_Downloadable_Model_System_Config_Source_Orderitemstatus
  • Overview
  • Package
  • Class
  • Tree
  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: /**
 28:  * Downloadable Products Download Helper
 29:  *
 30:  * @category    Mage
 31:  * @package     Mage_Downloadable
 32:  * @author      Magento Core Team <core@magentocommerce.com>
 33:  */
 34: class Mage_Downloadable_Helper_Download extends Mage_Core_Helper_Abstract
 35: {
 36:     const LINK_TYPE_URL         = 'url';
 37:     const LINK_TYPE_FILE        = 'file';
 38: 
 39:     const XML_PATH_CONTENT_DISPOSITION  = 'catalog/downloadable/content_disposition';
 40: 
 41:     /**
 42:      * Type of link
 43:      *
 44:      * @var string
 45:      */
 46:     protected $_linkType        = self::LINK_TYPE_FILE;
 47: 
 48:     /**
 49:      * Resource file
 50:      *
 51:      * @var string
 52:      */
 53:     protected $_resourceFile    = null;
 54: 
 55:     /**
 56:      * Resource open handle
 57:      *
 58:      * @var resource
 59:      */
 60:     protected $_handle          = null;
 61: 
 62:     /**
 63:      * Remote server headers
 64:      *
 65:      * @var array
 66:      */
 67:     protected $_urlHeaders      = array();
 68: 
 69:     /**
 70:      * MIME Content-type for a file
 71:      *
 72:      * @var string
 73:      */
 74:     protected $_contentType     = 'application/octet-stream';
 75: 
 76:     /**
 77:      * File name
 78:      *
 79:      * @var string
 80:      */
 81:     protected $_fileName        = 'download';
 82: 
 83:     /**
 84:      * Retrieve Resource file handle (socket, file pointer etc)
 85:      *
 86:      * @return resource
 87:      */
 88:     protected function _getHandle()
 89:     {
 90:         if (!$this->_resourceFile) {
 91:             Mage::throwException(Mage::helper('downloadable')->__('Please set resource file and link type.'));
 92:         }
 93: 
 94:         if (is_null($this->_handle)) {
 95:             if ($this->_linkType == self::LINK_TYPE_URL) {
 96:                 $port = 80;
 97: 
 98:                 /**
 99:                  * Validate URL
100:                  */
101:                 $urlProp = parse_url($this->_resourceFile);
102:                 if (!isset($urlProp['scheme']) || strtolower($urlProp['scheme'] != 'http')) {
103:                     Mage::throwException(Mage::helper('downloadable')->__('Invalid download URL scheme.'));
104:                 }
105:                 if (!isset($urlProp['host'])) {
106:                     Mage::throwException(Mage::helper('downloadable')->__('Invalid download URL host.'));
107:                 }
108:                 $hostname = $urlProp['host'];
109: 
110:                 if (isset($urlProp['port'])) {
111:                     $port = (int)$urlProp['port'];
112:                 }
113: 
114:                 $path = '/';
115:                 if (isset($urlProp['path'])) {
116:                     $path = $urlProp['path'];
117:                 }
118:                 $query = '';
119:                 if (isset($urlProp['query'])) {
120:                     $query = '?' . $urlProp['query'];
121:                 }
122: 
123:                 try {
124:                     $this->_handle = fsockopen($hostname, $port, $errno, $errstr);
125:                 }
126:                 catch (Exception $e) {
127:                     throw $e;
128:                 }
129: 
130:                 if ($this->_handle === false) {
131:                     Mage::throwException(Mage::helper('downloadable')->__('Cannot connect to remote host, error: %s.', $errstr));
132:                 }
133: 
134:                 $headers = 'GET ' . $path . $query . ' HTTP/1.0' . "\r\n"
135:                     . 'Host: ' . $hostname . "\r\n"
136:                     . 'User-Agent: Magento ver/' . Mage::getVersion() . "\r\n"
137:                     . 'Connection: close' . "\r\n"
138:                     . "\r\n";
139:                 fwrite($this->_handle, $headers);
140: 
141:                 while (!feof($this->_handle)) {
142:                     $str = fgets($this->_handle, 1024);
143:                     if ($str == "\r\n") {
144:                         break;
145:                     }
146:                     $match = array();
147:                     if (preg_match('#^([^:]+): (.*)\s+$#', $str, $match)) {
148:                         $k = strtolower($match[1]);
149:                         if ($k == 'set-cookie') {
150:                             continue;
151:                         }
152:                         else {
153:                             $this->_urlHeaders[$k] = trim($match[2]);
154:                         }
155:                     }
156:                     elseif (preg_match('#^HTTP/[0-9\.]+ (\d+) (.*)\s$#', $str, $match)) {
157:                         $this->_urlHeaders['code'] = $match[1];
158:                         $this->_urlHeaders['code-string'] = trim($match[2]);
159:                     }
160:                 }
161: 
162:                 if (!isset($this->_urlHeaders['code']) || $this->_urlHeaders['code'] != 200) {
163:                     Mage::throwException(Mage::helper('downloadable')->__('An error occurred while getting the requested content. Please contact the store owner.'));
164:                 }
165:             }
166:             elseif ($this->_linkType == self::LINK_TYPE_FILE) {
167:                 $this->_handle = new Varien_Io_File();
168:                 if (!is_file($this->_resourceFile)) {
169:                     Mage::helper('core/file_storage_database')->saveFileToFilesystem($this->_resourceFile);
170:                 }
171:                 $this->_handle->open(array('path'=>Mage::getBaseDir('var')));
172:                 if (!$this->_handle->fileExists($this->_resourceFile, true)) {
173:                     Mage::throwException(Mage::helper('downloadable')->__('The file does not exist.'));
174:                 }
175:                 $this->_handle->streamOpen($this->_resourceFile, 'r');
176:             }
177:             else {
178:                 Mage::throwException(Mage::helper('downloadable')->__('Invalid download link type.'));
179:             }
180:         }
181:         return $this->_handle;
182:     }
183: 
184:     /**
185:      * Retrieve file size in bytes
186:      */
187:     public function getFilesize()
188:     {
189:         $handle = $this->_getHandle();
190:         if ($this->_linkType == self::LINK_TYPE_FILE) {
191:             return $handle->streamStat('size');
192:         }
193:         elseif ($this->_linkType == self::LINK_TYPE_URL) {
194:             if (isset($this->_urlHeaders['content-length'])) {
195:                 return $this->_urlHeaders['content-length'];
196:             }
197:         }
198:         return null;
199:     }
200: 
201:     public function getContentType()
202:     {
203:         $handle = $this->_getHandle();
204:         if ($this->_linkType == self::LINK_TYPE_FILE) {
205:             if (function_exists('mime_content_type') && ($contentType = mime_content_type($this->_resourceFile))) {
206:                 return $contentType;
207:             } else {
208:                 return Mage::helper('downloadable/file')->getFileType($this->_resourceFile);
209:             }
210:         }
211:         elseif ($this->_linkType == self::LINK_TYPE_URL) {
212:             if (isset($this->_urlHeaders['content-type'])) {
213:                 $contentType = explode('; ', $this->_urlHeaders['content-type']);
214:                 return $contentType[0];
215:             }
216:         }
217:         return $this->_contentType;
218:     }
219: 
220:     public function getFilename()
221:     {
222:         $handle = $this->_getHandle();
223:         if ($this->_linkType == self::LINK_TYPE_FILE) {
224:             return pathinfo($this->_resourceFile, PATHINFO_BASENAME);
225:         }
226:         elseif ($this->_linkType == self::LINK_TYPE_URL) {
227:             if (isset($this->_urlHeaders['content-disposition'])) {
228:                 $contentDisposition = explode('; ', $this->_urlHeaders['content-disposition']);
229:                 if (!empty($contentDisposition[1]) && strpos($contentDisposition[1], 'filename=') !== false) {
230:                     return substr($contentDisposition[1], 9);
231:                 }
232:             }
233:             if ($fileName = @pathinfo($this->_resourceFile, PATHINFO_BASENAME)) {
234:                 return $fileName;
235:             }
236:         }
237:         return $this->_fileName;
238:     }
239: 
240:     /**
241:      * Set resource file for download
242:      *
243:      * @param string $resourceFile
244:      * @param string $linkType
245:      * @return Mage_Downloadable_Helper_Download
246:      */
247:     public function setResource($resourceFile, $linkType = self::LINK_TYPE_FILE)
248:     {
249:         if (self::LINK_TYPE_FILE == $linkType) {
250:             //check LFI protection
251:             /** @var $helper Mage_Core_Helper_Data */
252:             $helper = Mage::helper('core');
253:             $helper->checkLfiProtection($resourceFile);
254:         }
255: 
256:         $this->_resourceFile    = $resourceFile;
257:         $this->_linkType        = $linkType;
258: 
259:         return $this;
260:     }
261: 
262:     /**
263:      * Retrieve Http Request Object
264:      *
265:      * @return Mage_Core_Controller_Request_Http
266:      */
267:     public function getHttpRequest()
268:     {
269:         return Mage::app()->getFrontController()->getRequest();
270:     }
271: 
272:     /**
273:      * Retrieve Http Response Object
274:      *
275:      * @return Mage_Core_Controller_Response_Http
276:      */
277:     public function getHttpResponse()
278:     {
279:         return Mage::app()->getFrontController()->getResponse();
280:     }
281: 
282:     public function output()
283:     {
284:         $handle = $this->_getHandle();
285:         if ($this->_linkType == self::LINK_TYPE_FILE) {
286:             while ($buffer = $handle->streamRead()) {
287:                 print $buffer;
288:             }
289:         }
290:         elseif ($this->_linkType == self::LINK_TYPE_URL) {
291:             while (!feof($handle)) {
292:                 print fgets($handle, 1024);
293:             }
294:         }
295:     }
296: 
297:     /**
298:      * Use Content-Disposition: attachment
299:      *
300:      * @param mixed $store
301:      * @return bool
302:      */
303:     public function getContentDisposition($store = null)
304:     {
305:         return Mage::getStoreConfig(self::XML_PATH_CONTENT_DISPOSITION, $store);
306:     }
307: }
308: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0