Mage_Downloadable_Helper_Download Class Reference

Inheritance diagram for Mage_Downloadable_Helper_Download:

Mage_Core_Helper_Abstract

List of all members.

Public Member Functions

 getFilesize ()
 getContentType ()
 getFilename ()
 setResource ($resourceFile, $linkType=self::LINK_TYPE_FILE)
 getHttpRequest ()
 getHttpResponse ()
 output ()
 getContentDisposition ($store=null)

Public Attributes

const LINK_TYPE_URL = 'url'
const LINK_TYPE_FILE = 'file'
const XML_PATH_CONTENT_DISPOSITION = 'catalog/downloadable/content_disposition'

Protected Member Functions

 _getHandle ()

Protected Attributes

 $_linkType = self::LINK_TYPE_FILE
 $_resourceFile = null
 $_handle = null
 $_urlHeaders = array()
 $_contentType = 'application/octet-stream'
 $_fileName = 'download'


Detailed Description

Definition at line 34 of file Download.php.


Member Function Documentation

_getHandle (  )  [protected]

Retrieve Resource file handle (socket, file pointer etc)

Returns:
resource

Validate URL

Definition at line 88 of file Download.php.

00089     {
00090         if (!$this->_resourceFile) {
00091             Mage::throwException(Mage::helper('downloadable')->__('Please set resource file and link type'));
00092         }
00093 
00094         if (is_null($this->_handle)) {
00095             if ($this->_linkType == self::LINK_TYPE_URL) {
00096                 $port = 80;
00097 
00098                 /**
00099                  * Validate URL
00100                  */
00101                 $urlProp = parse_url($this->_resourceFile);
00102                 if (!isset($urlProp['scheme']) || strtolower($urlProp['scheme'] != 'http')) {
00103                     Mage::throwException(Mage::helper('downloadable')->__('Invalid download URL scheme'));
00104                 }
00105                 if (!isset($urlProp['host'])) {
00106                     Mage::throwException(Mage::helper('downloadable')->__('Invalid download URL host'));
00107                 }
00108                 $hostname = $urlProp['host'];
00109 
00110                 if (isset($urlProp['port'])) {
00111                     $port = (int)$urlProp['port'];
00112                 }
00113 
00114                 $path = '/';
00115                 if (isset($urlProp['path'])) {
00116                     $path = $urlProp['path'];
00117                 }
00118                 $query = '';
00119                 if (isset($urlProp['query'])) {
00120                     $query = '?' . $urlProp['query'];
00121                 }
00122 
00123                 try {
00124                     $this->_handle = fsockopen($hostname, $port, $errno, $errstr);
00125                 }
00126                 catch (Exception $e) {
00127                     throw $e;
00128                 }
00129 
00130                 if ($this->_handle === false) {
00131                     Mage::throwException(Mage::helper('downloadable')->__('Can\'t connect to remote host, error: %s', $errstr));
00132                 }
00133 
00134                 $headers = 'GET ' . $path . $query . ' HTTP/1.0' . "\r\n"
00135                     . 'Host: ' . $hostname . "\r\n"
00136                     . 'User-Agent: Magento ver/' . Mage::getVersion() . "\r\n"
00137                     . 'Connection: close' . "\r\n"
00138                     . "\r\n";
00139                 fwrite($this->_handle, $headers);
00140 
00141                 while (!feof($this->_handle)) {
00142                     $str = fgets($this->_handle, 1024);
00143                     if ($str == "\r\n") {
00144                         break;
00145                     }
00146                     $match = array();
00147                     if (preg_match('#^([^:]+): (.*)\s+$#', $str, $match)) {
00148                         $k = strtolower($match[1]);
00149                         if ($k == 'set-cookie') {
00150                             continue;
00151                         }
00152                         else {
00153                             $this->_urlHeaders[$k] = trim($match[2]);
00154                         }
00155                     }
00156                     elseif (preg_match('#^HTTP/[0-9\.]+ (\d+) (.*)\s$#', $str, $match)) {
00157                         $this->_urlHeaders['code'] = $match[1];
00158                         $this->_urlHeaders['code-string'] = trim($match[2]);
00159                     }
00160                 }
00161 
00162                 if (!isset($this->_urlHeaders['code']) || $this->_urlHeaders['code'] != 200) {
00163                     Mage::throwException(Mage::helper('downloadable')->__('Sorry, the was an error getting requested content. Please contact store owner.'));
00164                 }
00165             }
00166             elseif ($this->_linkType == self::LINK_TYPE_FILE) {
00167                 $this->_handle = new Varien_Io_File();
00168                 $this->_handle->open(array('path'=>Mage::getBaseDir('var')));
00169                 if (!$this->_handle->fileExists($this->_resourceFile, true)) {
00170                     Mage::throwException(Mage::helper('downloadable')->__('File does not exists'));
00171                 }
00172                 $this->_handle->streamOpen($this->_resourceFile, 'r');
00173             }
00174             else {
00175                 Mage::throwException(Mage::helper('downloadable')->__('Invalid download link type'));
00176             }
00177         }
00178         return $this->_handle;
00179     }

getContentDisposition ( store = null  ) 

Use Content-Disposition: attachment

Parameters:
mixed $store
Returns:
bool

Definition at line 293 of file Download.php.

00294     {
00295         return Mage::getStoreConfig(self::XML_PATH_CONTENT_DISPOSITION, $store);
00296     }

getContentType (  ) 

Definition at line 198 of file Download.php.

00199     {
00200         $handle = $this->_getHandle();
00201         if ($this->_linkType == self::LINK_TYPE_FILE) {
00202             if (function_exists('mime_content_type')) {
00203                 return mime_content_type($this->_resourceFile);
00204             } else {
00205                 return Mage::helper('downloadable/file')->getFileType($this->_resourceFile);
00206             }
00207         }
00208         elseif ($this->_linkType == self::LINK_TYPE_URL) {
00209             if (isset($this->_urlHeaders['content-type'])) {
00210                 $contentType = split('; ', $this->_urlHeaders['content-type']);
00211                 return $contentType[0];
00212             }
00213         }
00214         return $this->_contentType;
00215     }

getFilename (  ) 

Definition at line 217 of file Download.php.

00218     {
00219         $handle = $this->_getHandle();
00220         if ($this->_linkType == self::LINK_TYPE_FILE) {
00221             return pathinfo($this->_resourceFile, PATHINFO_BASENAME);
00222         }
00223         elseif ($this->_linkType == self::LINK_TYPE_URL) {
00224             if (isset($this->_urlHeaders['content-disposition'])) {
00225                 $contentDisposition = split('; ', $this->_urlHeaders['content-disposition']);
00226                 if (!empty($contentDisposition[1]) && strpos($contentDisposition[1], 'filename=') !== false) {
00227                     return substr($contentDisposition[1], 9);
00228                 }
00229             }
00230             if ($fileName = @pathinfo($this->_resourceFile, PATHINFO_BASENAME)) {
00231                 return $fileName;
00232             }
00233         }
00234         return $this->_fileName;
00235     }

getFilesize (  ) 

Retrieve file size in bytes

Definition at line 184 of file Download.php.

00185     {
00186         $handle = $this->_getHandle();
00187         if ($this->_linkType == self::LINK_TYPE_FILE) {
00188             return $handle->streamStat('size');
00189         }
00190         elseif ($this->_linkType == self::LINK_TYPE_URL) {
00191             if (isset($this->_urlHeaders['content-length'])) {
00192                 return $this->_urlHeaders['content-length'];
00193             }
00194         }
00195         return null;
00196     }

getHttpRequest (  ) 

Retrieve Http Request Object

Returns:
Mage_Core_Controller_Request_Http

Definition at line 257 of file Download.php.

00258     {
00259         return Mage::app()->getFrontController()->getRequest();
00260     }

getHttpResponse (  ) 

Retrieve Http Response Object

Returns:
Mage_Core_Controller_Response_Http

Definition at line 267 of file Download.php.

00268     {
00269         return Mage::app()->getFrontController()->getResponse();
00270     }

output (  ) 

Definition at line 272 of file Download.php.

00273     {
00274         $handle = $this->_getHandle();
00275         if ($this->_linkType == self::LINK_TYPE_FILE) {
00276             while ($buffer = $handle->streamRead()) {
00277                 print $buffer;
00278             }
00279         }
00280         elseif ($this->_linkType == self::LINK_TYPE_URL) {
00281             while (!feof($handle)) {
00282                 print fgets($handle, 1024);
00283             }
00284         }
00285     }

setResource ( resourceFile,
linkType = self::LINK_TYPE_FILE 
)

Set resource file for download

Parameters:
string $resourceFile
string $linkType
Returns:
Mage_Downloadable_Helper_Download

Definition at line 244 of file Download.php.

00245     {
00246         $this->_resourceFile    = $resourceFile;
00247         $this->_linkType        = $linkType;
00248 
00249         return $this;
00250     }


Member Data Documentation

$_contentType = 'application/octet-stream' [protected]

Definition at line 74 of file Download.php.

$_fileName = 'download' [protected]

Definition at line 81 of file Download.php.

$_handle = null [protected]

Definition at line 60 of file Download.php.

$_linkType = self::LINK_TYPE_FILE [protected]

Definition at line 46 of file Download.php.

$_resourceFile = null [protected]

Definition at line 53 of file Download.php.

$_urlHeaders = array() [protected]

Definition at line 67 of file Download.php.

const LINK_TYPE_FILE = 'file'

Definition at line 37 of file Download.php.

const LINK_TYPE_URL = 'url'

Definition at line 36 of file Download.php.

const XML_PATH_CONTENT_DISPOSITION = 'catalog/downloadable/content_disposition'

Definition at line 39 of file Download.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:08 2009 for Magento by  doxygen 1.5.8