diff --git a/libraries/joomla/http/factory.php b/libraries/joomla/http/factory.php new file mode 100644 index 0000000000000..a353a63794982 --- /dev/null +++ b/libraries/joomla/http/factory.php @@ -0,0 +1,104 @@ +getFilename(); + // Only load for php files. + // Note: DirectoryIterator::getExtension only available PHP >= 5.3.6 + if ($file->isFile() && substr($fileName, strrpos($fileName, '.') + 1) == 'php') + { + $names[] = substr($fileName, 0, strrpos($fileName, '.')); + } + } + + return $names; + } + +} diff --git a/libraries/joomla/http/http.php b/libraries/joomla/http/http.php index 0b08826cae5b0..89ec1152394ed 100644 --- a/libraries/joomla/http/http.php +++ b/libraries/joomla/http/http.php @@ -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); } /** @@ -183,4 +183,5 @@ public function trace($url, array $headers = null) { return $this->transport->request('TRACE', new JUri($url), null, $headers); } + } diff --git a/libraries/joomla/http/transport.php b/libraries/joomla/http/transport.php index b0b19bfab4c7a..2fce4744823eb 100644 --- a/libraries/joomla/http/transport.php +++ b/libraries/joomla/http/transport.php @@ -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(); } diff --git a/libraries/joomla/http/transport/curl.php b/libraries/joomla/http/transport/curl.php index 9dcb575956b89..35c5b22d80374 100644 --- a/libraries/joomla/http/transport/curl.php +++ b/libraries/joomla/http/transport/curl.php @@ -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(); + } } diff --git a/libraries/joomla/http/transport/socket.php b/libraries/joomla/http/transport/socket.php index 0d599f7967b1c..66a808dfe05ed 100644 --- a/libraries/joomla/http/transport/socket.php +++ b/libraries/joomla/http/transport/socket.php @@ -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.'); } @@ -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) @@ -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'); + } + } diff --git a/libraries/joomla/http/transport/stream.php b/libraries/joomla/http/transport/stream.php index fea69e072860f..5617578840040 100644 --- a/libraries/joomla/http/transport/stream.php +++ b/libraries/joomla/http/transport/stream.php @@ -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.'); } @@ -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'); + } } diff --git a/libraries/joomla/installer/helper.php b/libraries/joomla/installer/helper.php index 4da544d15332f..8a564f0ab3b22 100644 --- a/libraries/joomla/installer/helper.php +++ b/libraries/joomla/installer/helper.php @@ -38,7 +38,6 @@ 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); @@ -46,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)); + JError::raiseWarning(42, JText::sprintf('JLIB_INSTALLER_ERROR_DOWNLOAD_SERVER_CONNECT', $response->code)); 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 @@ -75,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); diff --git a/libraries/joomla/updater/adapters/collection.php b/libraries/joomla/updater/adapters/collection.php index 641242e525ba6..db3030962f55a 100644 --- a/libraries/joomla/updater/adapters/collection.php +++ b/libraries/joomla/updater/adapters/collection.php @@ -219,7 +219,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'); @@ -237,16 +239,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); diff --git a/libraries/joomla/updater/adapters/extension.php b/libraries/joomla/updater/adapters/extension.php index b47a3bc16790f..42c917604affe 100644 --- a/libraries/joomla/updater/adapters/extension.php +++ b/libraries/joomla/updater/adapters/extension.php @@ -155,7 +155,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'); @@ -175,15 +177,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, $response->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)) diff --git a/libraries/joomla/updater/update.php b/libraries/joomla/updater/update.php index 0f09eccba7655..cc61d06958e97 100644 --- a/libraries/joomla/updater/update.php +++ b/libraries/joomla/updater/update.php @@ -279,7 +279,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)); @@ -291,17 +293,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; diff --git a/libraries/joomla/updater/updater.php b/libraries/joomla/updater/updater.php index f5c784313094b..1f302afa6bb7e 100644 --- a/libraries/joomla/updater/updater.php +++ b/libraries/joomla/updater/updater.php @@ -71,13 +71,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; @@ -236,4 +229,5 @@ public function update($id) } return false; } + }