PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/mc/rt_missioncontrol_j15/lib/updater/libraries/pasamio/downloader/adapters/fopen.php

https://github.com/bhar1red/anahita
PHP | 128 lines | 107 code | 11 blank | 10 comment | 9 complexity | ebb78acb62c199e0a05023e1612e52b7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. RokUpdater::import('joomla.base.adapterinstance');
  3. class DownloaderFOpen extends JAdapterInstance {
  4. function downloadFile($url, $target = false, &$params=null)
  5. {
  6. // this isn't intelligent some times
  7. $error_object = new stdClass();
  8. $proxy = false;
  9. $php_errormsg = ''; // Set the error message
  10. $track_errors = ini_set('track_errors',true); // Set track errors
  11. $config =& JFactory::getConfig();
  12. if(is_null($params)) {
  13. $params = new JParameter();
  14. }
  15. $input_handle = null;
  16. // Are we on a version of PHP that supports streams?
  17. if(version_compare(PHP_VERSION, '5.0.0', '>'))
  18. {
  19. // set the ua; we could use ini_set but it might not work
  20. $http_opts = Array('user_agent'=>RokUpdater::generateUAString());
  21. // If:
  22. // - the proxy is enabled,
  23. // - the host is set and the port are set
  24. // Set the proxy settings and create a stream context
  25. if($params->get('use_proxy', 0) && strlen($params->get('proxy_host', '')) && strlen($params->get('proxy_port', '')))
  26. {
  27. $proxy = true;
  28. // I hate eclipse sometimes
  29. // If the user has a proxy username set fill this in as well
  30. $http_opts['proxy'] = 'tcp://'. $params->get('proxy_host') . ':'. $params->get('proxy_port');
  31. $http_opts['request_fulluri'] = 'true'; // play nicely with squid
  32. if(strlen($params->get('proxy_user', '')))
  33. {
  34. $credentials = base64_encode($params->get('proxy_user', '').':'.$params->get('proxy_pass',''));
  35. $http_opts['header'] = "Proxy-Authorization: Basic $credentials\r\n";
  36. }
  37. }
  38. $context = stream_context_create(array('http'=>$http_opts));
  39. $input_handle = @fopen($url, 'r', false, $context);
  40. }
  41. else
  42. {
  43. // Open remote server
  44. ini_set('user_agent', generateUAString()); // set the ua
  45. $input_handle = @fopen($url, "r"); // or die("Remote server connection failed");
  46. }
  47. if (!$input_handle)
  48. {
  49. $error_object->number = 42;
  50. $error_object->message = 'Remote Server connection failed: ' . $php_errormsg .'; Using Proxy: '. ($proxy ? 'Yes' : 'No');
  51. ini_set('track_errors',$track_errors);
  52. return $error_object;
  53. }
  54. $meta_data = stream_get_meta_data($input_handle);
  55. foreach ($meta_data['wrapper_data'] as $wrapper_data)
  56. {
  57. if (substr($wrapper_data, 0, strlen("Content-Disposition")) == "Content-Disposition") {
  58. $contentfilename = explode ("\"", $wrapper_data);
  59. $target = $contentfilename[1];
  60. }
  61. }
  62. // Set the target path if not given
  63. if (!$target) {
  64. $target = $config->getValue('config.tmp_path').DS.Downloader::getFilenameFromURL($url);
  65. } else {
  66. $target = $config->getValue('config.tmp_path').DS.basename($target);
  67. }
  68. RokUpdater::import('pasamio.pfactory');
  69. $stream = PFactory::getStream(true, true, 'RokUpdater/'.ROKUPDATER_VERSION, true);
  70. $relative_target = str_replace(JPATH_ROOT, '', $target);
  71. $output_handle = $stream->open($relative_target, 'wb');
  72. //$output_handle = fopen($target, "wb"); // or die("Local output opening failed");
  73. if (!$output_handle)
  74. {
  75. $error_object->number = 43;
  76. $error_object->message = 'Local output opening failed: ' . $stream->getError();
  77. ini_set('track_errors',$track_errors);
  78. return $error_object;
  79. }
  80. $contents = '';
  81. $downloaded = 0;
  82. while (!feof($input_handle))
  83. {
  84. $contents = fread($input_handle, 4096);
  85. if($contents === false)
  86. {
  87. $error_object->number = 44;
  88. $error_object->message = 'Failed reading network resource at '.$downloaded.' bytes: ' . $php_errormsg;
  89. ini_set('track_errors',$track_errors);
  90. return $error_object;
  91. } else if(strlen($contents))
  92. {
  93. $write_res = $stream->write($contents);
  94. if($write_res == false)
  95. {
  96. $error_object->number = 45;
  97. $error_object->message = 'Cannot write to local target: ' . $stream->getError();
  98. ini_set('track_errors',$track_errors);
  99. return $error_object;
  100. }
  101. $downloaded += 1024;
  102. }
  103. }
  104. $stream->close();
  105. fclose($input_handle);
  106. ini_set('track_errors',$track_errors);
  107. return basename($target);
  108. }
  109. }