Example #1
1
 /**
  * Downloads a package
  *
  * @param string $url    URL of file to download
  * @param string $target Download target filename [optional]
  *
  * @return mixed Path to downloaded package or boolean false on failure
  *
  * @since   11.1
  */
 public static function downloadPackage($url, $target = false)
 {
     $config = JFactory::getConfig();
     // Capture PHP errors
     $track_errors = ini_get('track_errors');
     ini_set('track_errors', true);
     // Set user agent
     $version = new JVersion();
     ini_set('user_agent', $version->getUserAgent('Installer'));
     $http = JHttpFactory::getHttp();
     $response = $http->get($url);
     if (200 != $response->code) {
         JLog::add(JText::_('JLIB_INSTALLER_ERROR_DOWNLOAD_SERVER_CONNECT'), JLog::WARNING, 'jerror');
         return false;
     }
     if ($response->headers['wrapper_data']['Content-Disposition']) {
         $contentfilename = explode("\"", $response->headers['wrapper_data']['Content-Disposition']);
         $target = $contentfilename[1];
     }
     // Set the target path if not given
     if (!$target) {
         $target = $config->get('tmp_path') . '/' . self::getFilenameFromURL($url);
     } else {
         $target = $config->get('tmp_path') . '/' . basename($target);
     }
     // Write buffer to file
     JFile::write($target, $response->body);
     // Restore error tracking to what it was before
     ini_set('track_errors', $track_errors);
     // Bump the max execution time because not using built in php zip libs are slow
     @set_time_limit(ini_get('max_execution_time'));
     // Return the name of the downloaded package
     return basename($target);
 }
 /**
  * Downloads a package
  *
  * @param   string  $url     URL of file to download
  * @param   string  $target  Download target filename [optional]
  *
  * @return  mixed  Path to downloaded package or boolean false on failure
  *
  * @since   11.1
  */
 public static 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
     $version = new JVersion();
     ini_set('user_agent', $version->getUserAgent('Installer'));
     $http = JHttpFactory::getHttp();
     // load installer plugins, and allow url and headers modification
     $headers = array();
     JPluginHelper::importPlugin('installer');
     $dispatcher = JDispatcher::getInstance();
     $results = $dispatcher->trigger('onInstallerBeforePackageDownload', array(&$url, &$headers));
     try {
         $response = $http->get($url, $headers);
     } catch (Exception $exc) {
         $response = null;
     }
     if (is_null($response)) {
         JError::raiseWarning(42, JText::_('JLIB_INSTALLER_ERROR_DOWNLOAD_SERVER_CONNECT'));
         return false;
     }
     if (302 == $response->code && isset($response->headers['Location'])) {
         return self::downloadPackage($response->headers['Location']);
     } elseif (200 != $response->code) {
         if ($response->body === '') {
             $response->body = $php_errormsg;
         }
         JError::raiseWarning(42, JText::sprintf('JLIB_INSTALLER_ERROR_DOWNLOAD_SERVER_CONNECT', $response->body));
         return false;
     }
     // Parse the Content-Disposition header to get the file name
     if (isset($response->headers['Content-Disposition']) && preg_match("/\\s*filename\\s?=\\s?(.*)/", $response->headers['Content-Disposition'], $parts)) {
         $target = trim(rtrim($parts[1], ";"), '"');
     }
     // Set the target path if not given
     if (!$target) {
         $target = $config->get('tmp_path') . '/' . self::getFilenameFromURL($url);
     } else {
         $target = $config->get('tmp_path') . '/' . basename($target);
     }
     // Write buffer to file
     JFile::write($target, $response->body);
     // Restore error tracking to what it was before
     ini_set('track_errors', $track_errors);
     // bump the max execution time because not using built in php zip libs are slow
     @set_time_limit(ini_get('max_execution_time'));
     // Return the name of the downloaded package
     return basename($target);
 }
Example #3
0
 /**
  * Downloads a package
  *
  * @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   11.1
  */
 public static 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::sprintf('JLIB_INSTALLER_ERROR_DOWNLOAD_SERVER_CONNECT', $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->get('tmp_path') . '/' . self::getFilenameFromURL($url);
     } else {
         $target = $config->get('tmp_path') . '/' . basename($target);
     }
     // Initialise contents buffer
     $contents = null;
     while (!feof($inputHandle)) {
         $contents .= fread($inputHandle, 4096);
         if ($contents === false) {
             JError::raiseWarning(44, JText::sprintf('JLIB_INSTALLER_ERROR_FAILED_READING_NETWORK_RESOURCES', $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);
     // bump the max execution time because not using built in php zip libs are slow
     @set_time_limit(ini_get('max_execution_time'));
     // Return the name of the downloaded package
     return basename($target);
 }
Example #4
0
 /**
  * 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);
 }
Example #5
0
 /**
  * Downloads a package
  *
  * @param   string  $url     URL of file to download
  * @param   string  $target  Download target filename [optional]
  *
  * @return  mixed  Path to downloaded package or boolean false on failure
  *
  * @since   11.1
  */
 public static 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
     $version = new JVersion();
     ini_set('user_agent', $version->getUserAgent('Installer'));
     $http = JHttpFactory::getHttp();
     try {
         $response = $http->get($url);
     } catch (Exception $exc) {
         $response = null;
     }
     if (is_null($response)) {
         JError::raiseWarning(42, JText::_('JLIB_INSTALLER_ERROR_DOWNLOAD_SERVER_CONNECT'));
         return false;
     }
     if (302 == $response->code && isset($response->headers['Location'])) {
         return self::downloadPackage($response->headers['Location']);
     } elseif (200 != $response->code) {
         if ($response->body === '') {
             $response->body = $php_errormsg;
         }
         JError::raiseWarning(42, JText::sprintf('JLIB_INSTALLER_ERROR_DOWNLOAD_SERVER_CONNECT', $response->body));
         return false;
     }
     if (isset($response->headers['Content-Disposition'])) {
         $contentfilename = explode("\"", $response->headers['Content-Disposition']);
         $target = $contentfilename[1];
     }
     // Set the target path if not given
     if (!$target) {
         $target = $config->get('tmp_path') . '/' . self::getFilenameFromURL($url);
     } else {
         $target = $config->get('tmp_path') . '/' . basename($target);
     }
     // Write buffer to file
     JFile::write($target, $response->body);
     // Restore error tracking to what it was before
     ini_set('track_errors', $track_errors);
     // bump the max execution time because not using built in php zip libs are slow
     @set_time_limit(ini_get('max_execution_time'));
     // Return the name of the downloaded package
     return basename($target);
 }
Example #6
0
 /**
  * Downloads a package
  *
  * @param   string  $url     URL of file to download
  * @param   mixed   $target  Download target filename or false to get the filename from the URL
  *
  * @return  string|boolean  Path to downloaded package or boolean false on failure
  *
  * @since   3.1
  */
 public static function downloadPackage($url, $target = false)
 {
     // Capture PHP errors
     $track_errors = ini_get('track_errors');
     ini_set('track_errors', true);
     // Set user agent
     $version = new JVersion();
     ini_set('user_agent', $version->getUserAgent('Installer'));
     // Load installer plugins, and allow url and headers modification
     $headers = array();
     JPluginHelper::importPlugin('installer');
     $dispatcher = JEventDispatcher::getInstance();
     $results = $dispatcher->trigger('onInstallerBeforePackageDownload', array(&$url, &$headers));
     try {
         $response = JHttpFactory::getHttp()->get($url, $headers);
     } catch (RuntimeException $exception) {
         JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_DOWNLOAD_SERVER_CONNECT', $exception->getMessage()), JLog::WARNING, 'jerror');
         return false;
     }
     if (302 == $response->code && isset($response->headers['Location'])) {
         return self::downloadPackage($response->headers['Location']);
     } elseif (200 != $response->code) {
         JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_DOWNLOAD_SERVER_CONNECT', $response->code), JLog::WARNING, 'jerror');
         return false;
     }
     // Parse the Content-Disposition header to get the file name
     if (isset($response->headers['Content-Disposition']) && preg_match("/\\s*filename\\s?=\\s?(.*)/", $response->headers['Content-Disposition'], $parts)) {
         $flds = explode(';', $parts[1]);
         $target = trim($flds[0], '"');
     }
     $tmpPath = JFactory::getConfig()->get('tmp_path');
     // Set the target path if not given
     if (!$target) {
         $target = $tmpPath . '/' . self::getFilenameFromUrl($url);
     } else {
         $target = $tmpPath . '/' . basename($target);
     }
     // Write buffer to file
     JFile::write($target, $response->body);
     // Restore error tracking to what it was before
     ini_set('track_errors', $track_errors);
     // Bump the max execution time because not using built in php zip libs are slow
     @set_time_limit(ini_get('max_execution_time'));
     // Return the name of the downloaded package
     return basename($target);
 }
Example #7
0
 /**
  * This checks for generation of the correct user agent string.
  *
  * @param	string	$component		default "Framework"
  * @param	bool	$mask			mask user agent as Mozilla
  * @param	bool	$addVersion		Add Version to UA String
  * @param	string	$expect			expected result
  * @param	string	$message		Test failure message
  * @param	bool	$useDefaults	use no arguments, accept function default
  *
  * @return void
  * @dataProvider casesUserAgent
  */
 public function testSettingCorrectUserAgentString($component, $mask, $addVersion, $expect, $message, $useDefaults)
 {
     if ($useDefaults) {
         $output = $this->object->getUserAgent();
     } else {
         if (is_null($mask)) {
             $output = $this->object->getUserAgent($component);
         } else {
             if (is_null($addVersion)) {
                 $output = $this->object->getUserAgent($component, $mask);
             } else {
                 $output = $this->object->getUserAgent($component, $mask, $addVersion);
             }
         }
     }
     $this->assertEquals($expect, $output, $message);
 }
Example #8
0
 /**
  * Creates a new stream object with appropriate prefix
  *
  * @param   boolean  $use_prefix   Prefix the connections for writing
  * @param   boolean  $use_network  Use network if available for writing; use false to disable (e.g. FTP, SCP)
  * @param   string   $ua           UA User agent to use
  * @param   boolean  $uamask       User agent masking (prefix Mozilla)
  *
  * @return  JStream
  *
  * @see JStream
  * @since   11.1
  */
 public static function getStream($use_prefix = true, $use_network = true, $ua = null, $uamask = false)
 {
     jimport('joomla.filesystem.stream');
     // Setup the context; Joomla! UA and overwrite
     $context = array();
     $version = new JVersion();
     // Set the UA for HTTP and overwrite for FTP
     $context['http']['user_agent'] = $version->getUserAgent($ua, $uamask);
     $context['ftp']['overwrite'] = true;
     if ($use_prefix) {
         $FTPOptions = JClientHelper::getCredentials('ftp');
         $SCPOptions = JClientHelper::getCredentials('scp');
         if ($FTPOptions['enabled'] == 1 && $use_network) {
             $prefix = 'ftp://' . $FTPOptions['user'] . ':' . $FTPOptions['pass'] . '@' . $FTPOptions['host'];
             $prefix .= $FTPOptions['port'] ? ':' . $FTPOptions['port'] : '';
             $prefix .= $FTPOptions['root'];
         } elseif ($SCPOptions['enabled'] == 1 && $use_network) {
             $prefix = 'ssh2.sftp://' . $SCPOptions['user'] . ':' . $SCPOptions['pass'] . '@' . $SCPOptions['host'];
             $prefix .= $SCPOptions['port'] ? ':' . $SCPOptions['port'] : '';
             $prefix .= $SCPOptions['root'];
         } else {
             $prefix = JPATH_ROOT . '/';
         }
         $retval = new JStream($prefix, JPATH_ROOT, $context);
     } else {
         $retval = new JStream('', '', $context);
     }
     return $retval;
 }
Example #9
0
 /**
  * Tests the getUserAgent method for a component string matching the specified option
  *
  * @return  void
  *
  * @since   3.0
  */
 public function testGetUserAgent_ComponentNotNull()
 {
     $this->assertContains('Component_test', $this->object->getUserAgent('Component_test', false, true));
 }
Example #10
0
 /**
  * Tests the getUserAgent method for a component string matching the specified option
  *
  * @return  void
  *
  * @since   3.0
  *
  * @covers  JVersion::getUserAgent
  */
 public function testGetUserAgent_ComponentNotNull()
 {
     $this->assertThat($this->object->getUserAgent('Component_test', false, true), $this->stringContains('Component_test'), 'getUserAgent should return a string with the following information:Component=Component_Test+Mask different to Mozilla 5.0+Version');
 }