PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/client/httpclients/fopen.php

https://bitbucket.org/pasamio/jhttprequest
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
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  4. * @license GNU General Public License version 2 or later; see LICENSE
  5. * @package Joomla.Platform
  6. * @subpackage Client
  7. */
  8. defined('JPATH_PLATFORM') or die;
  9. jimport('joomla.client.httpclient');
  10. jimport('joomla.base.adapterinstance');
  11. class JHttpClientFOpen extends JAdapterInstance implements JHTTPClient {
  12. function downloadFile($url, $target, &$params=null)
  13. {
  14. // this isn't intelligent some times
  15. $error_object = new stdClass();
  16. $proxy = false;
  17. $php_errormsg = ''; // Set the error message
  18. $track_errors = ini_set('track_errors',true); // Set track errors
  19. $config =& JFactory::getConfig();
  20. if(is_null($params)) {
  21. $params = new JRegistry();
  22. }
  23. $input_handle = null;
  24. // Are we on a version of PHP that supports streams?
  25. if(version_compare(PHP_VERSION, '5.0.0', '>'))
  26. {
  27. // set the ua; we could use ini_set but it might not work
  28. $http_opts = Array('user_agent'=>generateUAString());
  29. // If:
  30. // - the proxy is enabled,
  31. // - the host is set and the port are set
  32. // Set the proxy settings and create a stream context
  33. if($params->get('use_proxy', 0) && strlen($params->get('proxy_host', '')) && strlen($params->get('proxy_port', '')))
  34. {
  35. $proxy = true;
  36. // I hate eclipse sometimes
  37. // If the user has a proxy username set fill this in as well
  38. $http_opts['proxy'] = 'tcp://'. $params->get('proxy_host') . ':'. $params->get('proxy_port');
  39. $http_opts['request_fulluri'] = 'true'; // play nicely with squid
  40. if(strlen($params->get('proxy_user', '')))
  41. {
  42. $credentials = base64_encode($params->get('proxy_user', '').':'.$params->get('proxy_pass',''));
  43. $http_opts['header'] = "Proxy-Authorization: Basic $credentials\r\n";
  44. }
  45. }
  46. $context = stream_context_create(array('http'=>$http_opts));
  47. $input_handle = @fopen($url, 'r', false, $context);
  48. }
  49. else
  50. {
  51. // Open remote server
  52. ini_set('user_agent', generateUAString()); // set the ua
  53. $input_handle = @fopen($url, "r"); // or die("Remote server connection failed");
  54. }
  55. if (!$input_handle)
  56. {
  57. $error_object->number = 42;
  58. $error_object->message = 'Remote Server connection failed: ' . $php_errormsg .'; Using Proxy: '. ($proxy ? 'Yes' : 'No');
  59. ini_set('track_errors',$track_errors);
  60. return $error_object;
  61. }
  62. $meta_data = stream_get_meta_data($input_handle);
  63. foreach ($meta_data['wrapper_data'] as $wrapper_data)
  64. {
  65. if (is_string($wrapper_data) && substr($wrapper_data, 0, strlen("Content-Disposition")) == "Content-Disposition") {
  66. $contentfilename = explode ("\"", $wrapper_data);
  67. $target = $contentfilename[1];
  68. }
  69. }
  70. // Set the target path if not given
  71. if (!$target) {
  72. $target = $config->getValue('config.tmp_path').DS.Downloader::getFilenameFromURL($url);
  73. } else {
  74. $target = $config->getValue('config.tmp_path').DS.basename($target);
  75. }
  76. juimport('pasamio.pfactory');
  77. $stream = PFactory::getStream(true, true, 'JUpdateMan/'. getComponentVersion(), true);
  78. $relative_target = str_replace(JPATH_ROOT, '', $target);
  79. $output_handle = $stream->open($relative_target, 'wb');
  80. //$output_handle = fopen($target, "wb"); // or die("Local output opening failed");
  81. if (!$output_handle)
  82. {
  83. $error_object->number = 43;
  84. $error_object->message = 'Local output opening failed: ' . $stream->getError();
  85. ini_set('track_errors',$track_errors);
  86. return $error_object;
  87. }
  88. $contents = '';
  89. $downloaded = 0;
  90. while (!feof($input_handle))
  91. {
  92. $contents = fread($input_handle, 4096);
  93. if($contents === false)
  94. {
  95. $error_object->number = 44;
  96. $error_object->message = 'Failed reading network resource at '.$downloaded.' bytes: ' . $php_errormsg;
  97. ini_set('track_errors',$track_errors);
  98. return $error_object;
  99. } else if(strlen($contents))
  100. {
  101. $write_res = $stream->write($contents);
  102. if($write_res == false)
  103. {
  104. $error_object->number = 45;
  105. $error_object->message = 'Cannot write to local target: ' . $stream->getError();
  106. ini_set('track_errors',$track_errors);
  107. return $error_object;
  108. }
  109. $downloaded += 1024;
  110. }
  111. }
  112. $stream->close();
  113. fclose($input_handle);
  114. ini_set('track_errors',$track_errors);
  115. return basename($target);
  116. }
  117. /**
  118. * Method to test if the adapter is capable
  119. *
  120. * @return bool True if adapter is available and capable
  121. *
  122. * @since 11.1
  123. */
  124. public function test()
  125. {
  126. return Array('downloadFile', 'proxy_supprt');
  127. }
  128. /**
  129. * Method to send the HEAD command to the server.
  130. *
  131. * @param string $url Path to the resource.
  132. *
  133. * @return JHTTPRequest Request object
  134. *
  135. * @since 11.1
  136. * @throws JException
  137. */
  138. public function head($url)
  139. {
  140. return false;
  141. }
  142. /**
  143. * Method to send the GET command to the server.
  144. *
  145. * @param string $url Path to the resource.
  146. *
  147. * @return JHTTPRequest Request object
  148. *
  149. * @since 11.1
  150. * @throws JException
  151. */
  152. public function get($url)
  153. {
  154. return false;
  155. }
  156. /**
  157. * Method to send the POST command to the server.
  158. *
  159. * @param string $url Path to the resource.
  160. * @param array $data Associative array of key/value pairs to send as post values.
  161. *
  162. * @return JHTTPRequest Request object
  163. *
  164. * @since 11.1
  165. * @throws JException
  166. */
  167. public function post($url, $data)
  168. {
  169. return false;
  170. }
  171. }