Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

Commit

Permalink
Merge pull request #839 from oc666/httptransport
Browse files Browse the repository at this point in the history
add curl and socket to update process of Joomla
  • Loading branch information
realityking committed Feb 23, 2012
2 parents cdba6ce + 3caab01 commit c4c46ed
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 68 deletions.
104 changes: 104 additions & 0 deletions libraries/joomla/http/factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* @package Joomla.Platform
* @subpackage HTTP
*
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('JPATH_PLATFORM') or die();

/**
* HTTP factory class.
*
* @package Joomla.Platform
* @subpackage HTTP
* @since 12.1
*/
class JHttpFactory
{

/**
* method to recieve Http instance.
*
* @param JRegistry $options Client options object.
* @param mixed $adapters Adapter (string) or queue of adapters (array) to use for communication
*
* @return JHttp Joomla Http class
*
* @since 12.1
*/
public static function getHttp($options = null, $adapters = null)
{
if (empty($options))
{
$options = new JRegistry;
}
return new JHttp($options, self::getAvailableDriver($options, $adapters));
}

/**
* Finds an available http transport object for communication
*
* @param JRegistery $options Option for creating http transport object
* @param mixed $default Adapter (string) or queue of adapters (array) to use
*
* @return JHttpTransport Interface sub-class
*
* @since 12.1
*/
public static function getAvailableDriver(JRegistry $options, $default = null)
{
if (is_null($default))
{
$available_adapters = self::getHttpTransports();
}
else
{
settype($default, 'array');
$available_adapters = $default;
}
// Check if there is available http transport adapters
if (!count($available_adapters))
{
return false;
}
foreach ($available_adapters as $adapter)
{
$class = 'JHttpTransport' . ucfirst($adapter);
/**
* on J!2.5 (PHP 5.2) the condition should be:
* call_user_func_array(array($class, 'isSupported'), array())
*/
if ($class::isSupported())
{
return new $class($options);
}
}
return false;
}

/**
* Get the http transport handlers
*
* @return array An array of available transport handlers
*
* @since 12.1
* @todo make this function more generic cause the behaviour taken from cache (getStores)
*/
public static function getHttpTransports()
{
$basedir = __DIR__ . '/transport';
$handlers = JFolder::files($basedir, '.php');

$names = array();
foreach ($handlers as $handler)
{
$names[] = substr($handler, 0, strrpos($handler, '.'));
}

return $names;
}

}
3 changes: 2 additions & 1 deletion libraries/joomla/http/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class JHttp
public function __construct(JRegistry &$options = null, JHttpTransport $transport = null)
{
$this->options = isset($options) ? $options : new JRegistry;
$this->transport = isset($transport) ? $transport : new JHttpTransportStream($this->options);
$this->transport = isset($transport) ? $transport : JHttpFactory::getAvailableDriver($this->options);
}

/**
Expand Down Expand Up @@ -183,4 +183,5 @@ public function trace($url, array $headers = null)
{
return $this->transport->request('TRACE', new JUri($url), null, $headers);
}

}
9 changes: 9 additions & 0 deletions libraries/joomla/http/transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ public function __construct(JRegistry &$options);
* @since 11.3
*/
public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null);

/**
* method to check if http transport layer available for using
*
* @return bool true if available else false
*
* @since 12.1
*/
static public function isSupported();
}
12 changes: 12 additions & 0 deletions libraries/joomla/http/transport/curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,16 @@ protected function getResponse($content)

return $return;
}

/**
* method to check if http transport curl available for using
*
* @return bool true if available else false
*
* @since 12.1
*/
static public function isSupported()
{
return function_exists('curl_version') && curl_version();
}
}
19 changes: 18 additions & 1 deletion libraries/joomla/http/transport/socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class JHttpTransportSocket implements JHttpTransport
*/
public function __construct(JRegistry &$options)
{
if (!function_exists('fsockopen') || !is_callable('fsockopen'))
if (!self::isSupported())
{
throw new RuntimeException('Cannot use a socket transport when fsockopen() is not available.');
}
Expand Down Expand Up @@ -232,6 +232,10 @@ protected function connect(JUri $uri, $timeout = null)
}
}

if (!is_numeric($timeout))
{
$timeout = ini_get("default_socket_timeout");
}
// Attempt to connect to the server.
$connection = fsockopen($host, $port, $errno, $err, $timeout);
if (!$connection)
Expand All @@ -250,4 +254,17 @@ protected function connect(JUri $uri, $timeout = null)

return $this->connections[$key];
}

/**
* method to check if http transport socket available for using
*
* @return bool true if available else false
*
* @since 12.1
*/
static public function isSupported()
{
return function_exists('fsockopen') && is_callable('fsockopen');
}

}
14 changes: 13 additions & 1 deletion libraries/joomla/http/transport/stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class JHttpTransportStream implements JHttpTransport
public function __construct(JRegistry &$options)
{
// Verify that fopen() is available.
if (!function_exists('fopen') || !is_callable('fopen'))
if (!self::isSupported())
{
throw new RuntimeException('Cannot use a stream transport when fopen() is not available.');
}
Expand Down Expand Up @@ -177,4 +177,16 @@ protected function getResponse(array $headers, $body)

return $return;
}

/**
* method to check if http transport stream available for using
*
* @return bool true if available else false
*
* @since 12.1
*/
static public function isSupported()
{
return function_exists('fopen') && is_callable('fopen');
}
}
35 changes: 7 additions & 28 deletions libraries/joomla/installer/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,18 @@ public static function downloadPackage($url, $target = false)
$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)
$http = JHttpFactory::getHttp();
$response = $http->get($url);
if (200 != $response->code)
{
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 ($response->headers['wrapper_data']['Content-Disposition'])
{
if (substr($wrapper_data, 0, strlen("Content-Disposition")) == "Content-Disposition")
{
$contentfilename = explode("\"", $wrapper_data);
$target = $contentfilename[1];
}
$contentfilename = explode("\"", $response->headers['wrapper_data']['Content-Disposition']);
$target = $contentfilename[1];
}

// Set the target path if not given
Expand All @@ -74,24 +69,8 @@ public static function downloadPackage($url, $target = false)
$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);
JFile::write($target, $response->body);

// Restore error tracking to what it was before
ini_set('track_errors', $track_errors);
Expand Down
18 changes: 8 additions & 10 deletions libraries/joomla/updater/adapters/collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ public function findUpdate($options)
$this->updates = array();
$dbo = $this->parent->getDBO();

if (!($fp = @fopen($url, "r")))
$http = JHttpFactory::getHttp();
$response = $http->get($url);
if (200 != $response->code)
{
$query = $dbo->getQuery(true);
$query->update('#__update_sites');
Expand All @@ -244,16 +246,12 @@ public function findUpdate($options)
$this->xml_parser = xml_parser_create('');
xml_set_object($this->xml_parser, $this);
xml_set_element_handler($this->xml_parser, '_startElement', '_endElement');

while ($data = fread($fp, 8192))
if (!xml_parse($this->xml_parser, $response->body))
{
if (!xml_parse($this->xml_parser, $data, feof($fp)))
{
JLog::add("Error parsing url: " . $url, JLog::WARNING, 'updater');
$app = JFactory::getApplication();
$app->enqueueMessage(JText::sprintf('JLIB_UPDATER_ERROR_COLLECTION_PARSE_URL', $url), 'warning');
return false;
}
JLog::add("Error parsing url: " . $url, JLog::WARNING, 'updater');
$app = JFactory::getApplication();
$app->enqueueMessage(JText::sprintf('JLIB_UPDATER_ERROR_COLLECTION_PARSE_URL', $url), 'warning');
return false;
}
// TODO: Decrement the bad counter if non-zero
return array('update_sites' => $this->update_sites, 'updates' => $this->updates);
Expand Down
17 changes: 8 additions & 9 deletions libraries/joomla/updater/adapters/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ public function findUpdate($options)

$dbo = $this->parent->getDBO();

if (!($fp = @fopen($url, "r")))
$http = JHttpFactory::getHttp();
$response = $http->get($url);
if (!empty($response->code) && 200 != $response->code)
{
$query = $dbo->getQuery(true);
$query->update('#__update_sites');
Expand All @@ -184,15 +186,12 @@ public function findUpdate($options)
xml_set_element_handler($this->xml_parser, '_startElement', '_endElement');
xml_set_character_data_handler($this->xml_parser, '_characterData');

while ($data = fread($fp, 8192))
if (!xml_parse($this->xml_parser, $data))
{
if (!xml_parse($this->xml_parser, $data, feof($fp)))
{
JLog::add("Error parsing url: " . $url, JLog::WARNING, 'updater');
$app = JFactory::getApplication();
$app->enqueueMessage(JText::sprintf('JLIB_UPDATER_ERROR_EXTENSION_PARSE_URL', $url), 'warning');
return false;
}
JLog::add("Error parsing url: " . $url, JLog::WARNING, 'updater');
$app = JFactory::getApplication();
$app->enqueueMessage(JText::sprintf('JLIB_UPDATER_ERROR_EXTENSION_PARSE_URL', $url), 'warning');
return false;
}
xml_parser_free($this->xml_parser);
if (isset($this->latest))
Expand Down
21 changes: 10 additions & 11 deletions libraries/joomla/updater/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ public function _characterData($parser, $data)
*/
public function loadFromXML($url)
{
if (!($fp = @fopen($url, 'r')))
$http = JHttpFactory::getHttp();
$response = $http->get($url);
if (200 != $response->code)
{
// TODO: Add a 'mark bad' setting here somehow
JError::raiseWarning('101', JText::sprintf('JLIB_UPDATER_ERROR_EXTENSION_OPEN_URL', $url));
Expand All @@ -296,17 +298,14 @@ public function loadFromXML($url)
xml_set_element_handler($this->xml_parser, '_startElement', '_endElement');
xml_set_character_data_handler($this->xml_parser, '_characterData');

while ($data = fread($fp, 8192))
if (!xml_parse($this->xml_parser, $response->data))
{
if (!xml_parse($this->xml_parser, $data, feof($fp)))
{
die(
sprintf(
"XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml_parser)),
xml_get_current_line_number($this->xml_parser)
)
);
}
die(
sprintf(
"XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml_parser)),
xml_get_current_line_number($this->xml_parser)
)
);
}
xml_parser_free($this->xml_parser);
return true;
Expand Down
8 changes: 1 addition & 7 deletions libraries/joomla/updater/updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ public static function &getInstance()
*/
public function findUpdates($eid = 0, $cacheTimeout = 0)
{
// Check if fopen is allowed
$result = ini_get('allow_url_fopen');
if (empty($result))
{
JError::raiseWarning('101', JText::_('JLIB_UPDATER_ERROR_COLLECTION_FOPEN'));
return false;
}

$dbo = $this->getDBO();
$retval = false;
Expand Down Expand Up @@ -217,4 +210,5 @@ public function update($id)
}
return false;
}

}

0 comments on commit c4c46ed

Please sign in to comment.