/** * Extract a ZIP compressed file to a given path * * @access public * @param string $archive Path to ZIP archive to extract * @param string $destination Path to extract archive into * @param array $options Extraction options [unused] * @return boolean True if successful * @since 1.5 */ function extract($archive, $destination, $options = array()) { // Initialize variables $this->_data = null; $this->_metadata = null; $stream =& PFactory::getStream(); if (!$stream->open($archive, 'rb')) { $this->set('error.message', 'Unable to read archive'); return JError::raiseWarning(100, $this->get('error.message')); } $position = 0; $return_array = array(); $i = 0; $chunksize = 512; // tar has items in 512 byte packets while ($entry = $stream->read($chunksize)) { //$entry =& $this->_data[$i]; $info = @unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/Ctypeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $entry); if (!$info) { $this->set('error.message', 'Unable to decompress data'); return JError::raiseWarning(100, $this->get('error.message')); } $size = octdec($info['size']); $bsize = ceil($size / $chunksize) * $chunksize; $contents = ''; if ($size) { //$contents = fread($this->_fh, $size); $contents = substr($stream->read($bsize), 0, octdec($info['size'])); } if ($info['filename']) { $file = array('attr' => null, 'data' => null, 'date' => octdec($info['mtime']), 'name' => trim($info['filename']), 'size' => octdec($info['size']), 'type' => isset($this->_types[$info['typeflag']]) ? $this->_types[$info['typeflag']] : null); if ($info['typeflag'] == 0 || $info['typeflag'] == 0x30 || $info['typeflag'] == 0x35) { /* File or folder. */ $file['data'] = $contents; $mode = hexdec(substr($info['mode'], 4, 3)); $file['attr'] = ($info['typeflag'] == 0x35 ? 'd' : '-') . ($mode & 0x400 ? 'r' : '-') . ($mode & 0x200 ? 'w' : '-') . ($mode & 0x100 ? 'x' : '-') . ($mode & 0x40 ? 'r' : '-') . ($mode & 0x20 ? 'w' : '-') . ($mode & 0x10 ? 'x' : '-') . ($mode & 0x4 ? 'r' : '-') . ($mode & 0x2 ? 'w' : '-') . ($mode & 0x1 ? 'x' : '-'); } else { /* Some other type. */ } $type = strtolower($file['type']); if ($type == 'file' || $type == 'unix file') { $path = JPath::clean($destination . DS . $file['name']); // Make sure the destination folder exists if (!JFolder::create(dirname($path))) { $this->set('error.message', 'Unable to create destination'); return JError::raiseWarning(100, $this->get('error.message')); } if (JFile::write($path, $contents, true) === false) { $this->set('error.message', 'Unable to write entry'); return JError::raiseWarning(100, $this->get('error.message')); } $contents = ''; // reclaim some memory } } } $stream->close(); return true; }
/** * Extract a Gzip compressed file to a given path * * @access public * @param string $archive Path to ZIP archive to extract * @param string $destination Path to extract archive to * @param array $options Extraction options [unused] * @return boolean True if successful * @since 1.5 */ function extract($archive, $destination, $options = array()) { // Initialize variables $this->_data = null; if (!extension_loaded('zlib')) { $this->set('error.message', 'Zlib Not Supported'); return JError::raiseWarning(100, $this->get('error.message')); } /* if (!$this->_data = JFile::read($archive)) { $this->set('error.message', 'Unable to read archive'); return JError::raiseWarning(100, $this->get('error.message')); } $position = $this->_getFilePosition(); $buffer = gzinflate(substr($this->_data, $position, strlen($this->_data) - $position)); if (empty ($buffer)) { $this->set('error.message', 'Unable to decompress data'); return JError::raiseWarning(100, $this->get('error.message')); } if (JFile::write($destination, $buffer) === false) { $this->set('error.message', 'Unable to write archive'); return JError::raiseWarning(100, $this->get('error.message')); } return true; */ // New style! streams! $input =& PFactory::getStream(); $input->set('processingmethod', 'gz'); // use gz if (!$input->open($archive)) { $this->set('error.message', 'Unable to read archive (gz)'); return JError::raiseWarning(100, $this->get('error.message')); } $output =& PFactory::getStream(); if (!$output->open($destination, 'w')) { $this->set('error.message', 'Unable to write archive (gz)'); $input->close(); // close the previous file return JError::raiseWarning(100, $this->get('error.message')); } $written = 0; do { $this->_data = $input->read($input->get('chunksize', 8196)); if ($this->_data) { if (!$output->write($this->_data)) { $this->set('error.message', 'Unable to write file (gz)'); return JError::raiseWarning(100, $this->get('error.message')); } } } while ($this->_data); $output->close(); $input->close(); return true; }
/** * Extract a Bzip2 compressed file to a given path * * @access public * @param string $archive Path to Bzip2 archive to extract * @param string $destination Path to extract archive to * @param array $options Extraction options [unused] * @return boolean True if successful * @since 1.5 */ function extract($archive, $destination, $options = array()) { // Initialize variables $this->_data = null; if (!extension_loaded('bz2')) { $this->set('error.message', 'BZip2 Not Supported'); return JError::raiseWarning(100, $this->get('error.message')); } /* // old style: read the whole file and then parse it if (!$this->_data = JFile::read($archive)) { $this->set('error.message', 'Unable to read archive'); return JError::raiseWarning(100, $this->get('error.message')); } $buffer = bzdecompress($this->_data); unset($this->_data); if (empty ($buffer)) { $this->set('error.message', 'Unable to decompress data'); return JError::raiseWarning(100, $this->get('error.message')); } if (JFile::write($destination, $buffer) === false) { $this->set('error.message', 'Unable to write archive'); return JError::raiseWarning(100, $this->get('error.message')); } //*/ // New style! streams! $input =& PFactory::getStream(); $input->set('processingmethod', 'bz'); // use bzip if (!$input->open($archive)) { $this->set('error.message', 'Unable to read archive (bz2)'); return JError::raiseWarning(100, $this->get('error.message')); } $output =& PFactory::getStream(); if (!$output->open($destination, 'w')) { $this->set('error.message', 'Unable to write archive (bz2)'); $input->close(); // close the previous file return JError::raiseWarning(100, $this->get('error.message')); } $written = 0; do { $this->_data = $input->read($input->get('chunksize', 8196)); if ($this->_data) { if (!$output->write($this->_data)) { $this->set('error.message', 'Unable to write file (bz2)'); return JError::raiseWarning(100, $this->get('error.message')); } } } while ($this->_data); $output->close(); $input->close(); return true; }
function downloadFile($url, $target = false, &$params = null) { // this isn't intelligent some times $error_object = new stdClass(); $proxy = false; $php_errormsg = ''; // Set the error message $track_errors = ini_set('track_errors', true); // Set track errors $config =& JFactory::getConfig(); if (is_null($params)) { $params = new JParameter(); } $input_handle = null; // Are we on a version of PHP that supports streams? if (version_compare(PHP_VERSION, '5.0.0', '>')) { // set the ua; we could use ini_set but it might not work $http_opts = array('user_agent' => RokUpdater::generateUAString()); // If: // - the proxy is enabled, // - the host is set and the port are set // Set the proxy settings and create a stream context if ($params->get('use_proxy', 0) && strlen($params->get('proxy_host', '')) && strlen($params->get('proxy_port', ''))) { $proxy = true; // I hate eclipse sometimes // If the user has a proxy username set fill this in as well $http_opts['proxy'] = 'tcp://' . $params->get('proxy_host') . ':' . $params->get('proxy_port'); $http_opts['request_fulluri'] = 'true'; // play nicely with squid if (strlen($params->get('proxy_user', ''))) { $credentials = base64_encode($params->get('proxy_user', '') . ':' . $params->get('proxy_pass', '')); $http_opts['header'] = "Proxy-Authorization: Basic {$credentials}\r\n"; } } $context = stream_context_create(array('http' => $http_opts)); $input_handle = @fopen($url, 'r', false, $context); } else { // Open remote server ini_set('user_agent', generateUAString()); // set the ua $input_handle = @fopen($url, "r"); // or die("Remote server connection failed"); } if (!$input_handle) { $error_object->number = 42; $error_object->message = 'Remote Server connection failed: ' . $php_errormsg . '; Using Proxy: ' . ($proxy ? 'Yes' : 'No'); ini_set('track_errors', $track_errors); return $error_object; } $meta_data = stream_get_meta_data($input_handle); foreach ($meta_data['wrapper_data'] as $wrapper_data) { if (substr($wrapper_data, 0, strlen("Content-Disposition")) == "Content-Disposition") { $contentfilename = explode("\"", $wrapper_data); $target = $contentfilename[1]; } } // Set the target path if not given if (!$target) { $target = $config->getValue('config.tmp_path') . DS . Downloader::getFilenameFromURL($url); } else { $target = $config->getValue('config.tmp_path') . DS . basename($target); } RokUpdater::import('pasamio.pfactory'); $stream = PFactory::getStream(true, true, 'RokUpdater/' . ROKUPDATER_VERSION, true); $relative_target = str_replace(JPATH_ROOT, '', $target); $output_handle = $stream->open($relative_target, 'wb'); //$output_handle = fopen($target, "wb"); // or die("Local output opening failed"); if (!$output_handle) { $error_object->number = 43; $error_object->message = 'Local output opening failed: ' . $stream->getError(); ini_set('track_errors', $track_errors); return $error_object; } $contents = ''; $downloaded = 0; while (!feof($input_handle)) { $contents = fread($input_handle, 4096); if ($contents === false) { $error_object->number = 44; $error_object->message = 'Failed reading network resource at ' . $downloaded . ' bytes: ' . $php_errormsg; ini_set('track_errors', $track_errors); return $error_object; } else { if (strlen($contents)) { $write_res = $stream->write($contents); if ($write_res == false) { $error_object->number = 45; $error_object->message = 'Cannot write to local target: ' . $stream->getError(); ini_set('track_errors', $track_errors); return $error_object; } $downloaded += 1024; } } } $stream->close(); fclose($input_handle); ini_set('track_errors', $track_errors); return basename($target); }
function downloadFile($url, $target = false, &$params = null) { if (!function_exists('curl_init')) { $error_object = new stdClass(); $error_object->number = 40; $error_object->message = 'cURL support not available on this host. Use fopen instead.'; return $error_object; } $config =& JFactory::getConfig(); if (is_null($params)) { $params = new JParameter(); } $php_errormsg = ''; // Set the error message $track_errors = ini_set('track_errors', true); // Set track errors // create a new cURL resource $ch = curl_init(); // set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, $url); // Set the download URL curl_setopt($ch, CURLOPT_HEADER, false); // Don't include the header in the output curl_setopt($ch, CURLOPT_USERAGENT, generateUAString()); // set the user agent curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects (required for Joomla!) curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // 10 maximum redirects // curl_setopt($ch, CURLOPT_HEADERFUNCTION, // set a custom header callback // array($this, 'header_callback')); // use this object and function 'header_callback' if (!$target) { $target = $config->getValue('config.tmp_path') . DS . Downloader::getFilenameFromURL($url); } juimport('pasamio.pfactory'); $output_stream = PFactory::getStream(true, true, 'JUpdateMan/' . getComponentVersion(), true); $relative_target = str_replace(JPATH_ROOT, '', $target); $output_handle = $output_stream->open($relative_target, 'wb'); //$output_handle = @fopen($target, "wb"); // Open a location if (!$output_handle) { $error_object->number = 43; $error_object->message = 'Local output opening failed: ' . $output_stream->getError(); ini_set('track_errors', $track_errors); return $error_object; } // since we're using streams we should be able to get everything up and running fine here curl_setopt($ch, CURLOPT_FILE, $output_stream->getFileHandle()); if ($params->get('use_proxy', 0) && strlen($params->get('proxy_host', '')) && strlen($params->get('proxy_port', ''))) { curl_setopt($ch, CURLOPT_PROXY, $params->get('proxy_host') . ':' . $params->get('proxy_port')); if (strlen($params->get('proxy_user', ''))) { curl_setopt($ch, CURLOPT_PROXYUSERPWD, $params->get('proxy_user') . ':' . $params->get('proxy_pass', '')); } } // grab URL and pass it to the browser if (curl_exec($ch) === false) { $error_object->number = 46; $error_object->message = 'cURL transfer failed(' . curl_errno($ch) . '): ' . curl_error($ch); ini_set('track_errors', $track_errors); return $error_object; } // close cURL resource, and free up system resources curl_close($ch); $output_stream->close(); return basename($target); }