/** * Retrieve Resource file handle (socket, file pointer etc) * * @return resource */ protected function _getHandle() { if (!$this->_resourceFile) { AO::throwException(AO::helper('downloadable')->__('Please set resource file and link type')); } if (is_null($this->_handle)) { if ($this->_linkType == self::LINK_TYPE_URL) { $port = 80; /** * Validate URL */ $urlProp = parse_url($this->_resourceFile); if (!isset($urlProp['scheme']) || strtolower($urlProp['scheme'] != 'http')) { AO::throwException(AO::helper('downloadable')->__('Invalid download URL scheme')); } if (!isset($urlProp['host'])) { AO::throwException(AO::helper('downloadable')->__('Invalid download URL host')); } $hostname = $urlProp['host']; if (isset($urlProp['port'])) { $port = (int) $urlProp['port']; } $path = '/'; if (isset($urlProp['path'])) { $path = $urlProp['path']; } $query = ''; if (isset($urlProp['query'])) { $query = '?' . $urlProp['query']; } try { $this->_handle = fsockopen($hostname, $port, $errno, $errstr); } catch (Exception $e) { throw $e; } if ($this->_handle === false) { AO::throwException(AO::helper('downloadable')->__('Can\'t connect to remote host, error: %s', $errstr)); } $headers = 'GET ' . $path . $query . ' HTTP/1.0' . "\r\n" . 'Host: ' . $hostname . "\r\n" . 'User-Agent: Magento ver/' . AO::getVersion() . "\r\n" . 'Connection: close' . "\r\n" . "\r\n"; fwrite($this->_handle, $headers); while (!feof($this->_handle)) { $str = fgets($this->_handle, 1024); if ($str == "\r\n") { break; } $match = array(); if (preg_match('#^([^:]+): (.*)\\s+$#', $str, $match)) { $k = strtolower($match[1]); if ($k == 'set-cookie') { continue; } else { $this->_urlHeaders[$k] = trim($match[2]); } } elseif (preg_match('#^HTTP/[0-9\\.]+ (\\d+) (.*)\\s$#', $str, $match)) { $this->_urlHeaders['code'] = $match[1]; $this->_urlHeaders['code-string'] = trim($match[2]); } } if (!isset($this->_urlHeaders['code']) || $this->_urlHeaders['code'] != 200) { AO::throwException(AO::helper('downloadable')->__('Sorry, the was an error getting requested content. Please contact store owner.')); } } elseif ($this->_linkType == self::LINK_TYPE_FILE) { $this->_handle = new Varien_Io_File(); $this->_handle->open(array('path' => AO::getBaseDir('var'))); if (!$this->_handle->fileExists($this->_resourceFile, true)) { AO::throwException(AO::helper('downloadable')->__('File does not exists')); } $this->_handle->streamOpen($this->_resourceFile, 'r'); } else { AO::throwException(AO::helper('downloadable')->__('Invalid download link type')); } } return $this->_handle; }