/libraries/joomla/client/httpclients/fopen.php
PHP | 195 lines | 167 code | 12 blank | 16 comment | 10 complexity | aef35e1fe069acbf66f73e4b5cef3532 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.1
- <?php
- /**
- * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
- * @license GNU General Public License version 2 or later; see LICENSE
- * @package Joomla.Platform
- * @subpackage Client
- */
- defined('JPATH_PLATFORM') or die;
- jimport('joomla.client.httpclient');
- jimport('joomla.base.adapterinstance');
- class JHttpClientFOpen extends JAdapterInstance implements JHTTPClient {
- function downloadFile($url, $target, &$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 JRegistry();
- }
- $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'=>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 (is_string($wrapper_data) && 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);
- }
- juimport('pasamio.pfactory');
- $stream = PFactory::getStream(true, true, 'JUpdateMan/'. getComponentVersion(), 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);
- }
- /**
- * Method to test if the adapter is capable
- *
- * @return bool True if adapter is available and capable
- *
- * @since 11.1
- */
- public function test()
- {
- return Array('downloadFile', 'proxy_supprt');
- }
-
- /**
- * Method to send the HEAD command to the server.
- *
- * @param string $url Path to the resource.
- *
- * @return JHTTPRequest Request object
- *
- * @since 11.1
- * @throws JException
- */
- public function head($url)
- {
- return false;
- }
-
- /**
- * Method to send the GET command to the server.
- *
- * @param string $url Path to the resource.
- *
- * @return JHTTPRequest Request object
- *
- * @since 11.1
- * @throws JException
- */
- public function get($url)
- {
- return false;
- }
-
- /**
- * Method to send the POST command to the server.
- *
- * @param string $url Path to the resource.
- * @param array $data Associative array of key/value pairs to send as post values.
- *
- * @return JHTTPRequest Request object
- *
- * @since 11.1
- * @throws JException
- */
- public function post($url, $data)
- {
- return false;
- }
-
- }