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_Helper_Download extends Mage_Core_Helper_Abstract
00035 {
00036 const LINK_TYPE_URL = 'url';
00037 const LINK_TYPE_FILE = 'file';
00038
00039 const XML_PATH_CONTENT_DISPOSITION = 'catalog/downloadable/content_disposition';
00040
00041
00042
00043
00044
00045
00046 protected $_linkType = self::LINK_TYPE_FILE;
00047
00048
00049
00050
00051
00052
00053 protected $_resourceFile = null;
00054
00055
00056
00057
00058
00059
00060 protected $_handle = null;
00061
00062
00063
00064
00065
00066
00067 protected $_urlHeaders = array();
00068
00069
00070
00071
00072
00073
00074 protected $_contentType = 'application/octet-stream';
00075
00076
00077
00078
00079
00080
00081 protected $_fileName = 'download';
00082
00083
00084
00085
00086
00087
00088 protected function _getHandle()
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
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 }
00180
00181
00182
00183
00184 public function getFilesize()
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 }
00197
00198 public function getContentType()
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 }
00216
00217 public function getFilename()
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 }
00236
00237
00238
00239
00240
00241
00242
00243
00244 public function setResource($resourceFile, $linkType = self::LINK_TYPE_FILE)
00245 {
00246 $this->_resourceFile = $resourceFile;
00247 $this->_linkType = $linkType;
00248
00249 return $this;
00250 }
00251
00252
00253
00254
00255
00256
00257 public function getHttpRequest()
00258 {
00259 return Mage::app()->getFrontController()->getRequest();
00260 }
00261
00262
00263
00264
00265
00266
00267 public function getHttpResponse()
00268 {
00269 return Mage::app()->getFrontController()->getResponse();
00270 }
00271
00272 public function output()
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 }
00286
00287
00288
00289
00290
00291
00292
00293 public function getContentDisposition($store = null)
00294 {
00295 return Mage::getStoreConfig(self::XML_PATH_CONTENT_DISPOSITION, $store);
00296 }
00297 }