/** * Downloads a package * * @static * @param string URL of file to download * @param string Download target filename [optional] * @return mixed Path to downloaded package or boolean false on failure * @since 1.5 */ function downloadPackage($url, $target = false) { $config =& JFactory::getConfig(); // Capture PHP errors $php_errormsg = 'Error Unknown'; $track_errors = ini_get('track_errors'); ini_set('track_errors', true); // Set user agent jimport('joomla.version'); $version = new JVersion(); ini_set('user_agent', $version->getUserAgent('Installer')); // Open the remote server socket for reading $inputHandle = @fopen($url, "r"); $error = strstr($php_errormsg, 'failed to open stream:'); if (!$inputHandle) { JError::raiseWarning(42, JText::_('SERVER_CONNECT_FAILED') . ', ' . $error); return false; } $meta_data = stream_get_meta_data($inputHandle); 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 . JInstallerHelper::getFilenameFromURL($url); } else { $target = $config->getValue('config.tmp_path') . DS . basename($target); } // Initialise contents buffer $contents = null; while (!feof($inputHandle)) { $contents .= fread($inputHandle, 4096); if ($contents == false) { JError::raiseWarning(44, 'Failed reading network resource: ' . $php_errormsg); return false; } } // Write buffer to file JFile::write($target, $contents); // Close file pointer resource fclose($inputHandle); // restore error tracking to what it was before ini_set('track_errors', $track_errors); // Return the name of the downloaded package return basename($target); }
/** * Tests the getFilenameFromURL method * * @since 3.1 * * @return void */ public function testGetFilenameFromURL() { $this->assertThat(JInstallerHelper::getFilenameFromURL('http://update.joomla.org/core/list.xml'), $this->equalTo('list.xml'), 'JInstallerHelper::getFilenameFromURL should return the last portion of the URL'); }
/** * Download a specific package using the MageBridge Proxy (CURL-based) * * @param string $url * @param string $target * @return string */ public static function downloadPackage($url, $target = false) { $app = JFactory::getApplication(); // Open the remote server socket for reading $proxy = MageBridgeModelProxy::getInstance(); $data = $proxy->getRemote($url, null, 'get', false); if (empty($data)) { JError::raiseWarning(42, JText::_('REMOTE_DOWNLOAD_FAILED') . ', ' . $error); return false; } // Set the target path if not given $config = JFactory::getConfig(); if (!$target) { $target = $config->get('tmp_path') . '/' . JInstallerHelper::getFilenameFromURL($url); } else { $target = $config->get('tmp_path') . '/' . basename($target); } // Write received data to file file_put_contents($target, $data); // Return the name of the downloaded package return basename($target); }
public static function downloadPackage($url, $file = null) { // System variables $app = JFactory::getApplication(); // Use fopen() instead if (ini_get('allow_url_fopen') == 1) { return JInstallerHelper::downloadPackage($url, $file); } // Set the target path if not given if (empty($file)) { $file = $app->getCfg('tmp_path') . '/' . JInstallerHelper::getFilenameFromURL($url); } else { $file = $app->getCfg('tmp_path') . '/' . basename($file); } // Open the remote server socket for reading $ch = curl_init($url); curl_setopt_array($ch, array(CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false, CURLOPT_MAXREDIRS => 2, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_FRESH_CONNECT => false, CURLOPT_FORBID_REUSE => false, CURLOPT_BUFFERSIZE => 8192)); $data = curl_exec($ch); curl_close($ch); if (empty($data)) { JError::raiseWarning(42, JText::_('LIB_YIREO_HELPER_INSTALL_REMOTE_DOWNLOAD_FAILED') . ', ' . $error); return false; } // Write received data to file JFile::write($file, $data); // Return the name of the downloaded package return basename($file); }