/forum/phpbb/file_downloader.php

https://github.com/AJenbo/ubuntudanmark.dk · PHP · 120 lines · 68 code · 13 blank · 39 comment · 11 complexity · 2a0a037d5fbcac25195ac24cf034ca42 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. namespace phpbb;
  14. class file_downloader
  15. {
  16. /** @var string Error string */
  17. protected $error_string = '';
  18. /** @var int Error number */
  19. protected $error_number = 0;
  20. /**
  21. * Retrieve contents from remotely stored file
  22. *
  23. * @param string $host File host
  24. * @param string $directory Directory file is in
  25. * @param string $filename Filename of file to retrieve
  26. * @param int $port Port to connect to; default: 80
  27. * @param int $timeout Connection timeout in seconds; default: 6
  28. *
  29. * @return mixed File data as string if file can be read and there is no
  30. * timeout, false if there were errors or the connection timed out
  31. *
  32. * @throws \phpbb\exception\runtime_exception If data can't be retrieved and no error
  33. * message is returned
  34. */
  35. public function get($host, $directory, $filename, $port = 80, $timeout = 6)
  36. {
  37. // Set default values for error variables
  38. $this->error_number = 0;
  39. $this->error_string = '';
  40. if ($socket = @fsockopen(($port == 443 ? 'tls://' : '') . $host, $port, $this->error_number, $this->error_string, $timeout))
  41. {
  42. @fputs($socket, "GET $directory/$filename HTTP/1.0\r\n");
  43. @fputs($socket, "HOST: $host\r\n");
  44. @fputs($socket, "Connection: close\r\n\r\n");
  45. $timer_stop = time() + $timeout;
  46. stream_set_timeout($socket, $timeout);
  47. $file_info = '';
  48. $get_info = false;
  49. while (!@feof($socket))
  50. {
  51. if ($get_info)
  52. {
  53. $file_info .= @fread($socket, 1024);
  54. }
  55. else
  56. {
  57. $line = @fgets($socket, 1024);
  58. if ($line == "\r\n")
  59. {
  60. $get_info = true;
  61. }
  62. else if (stripos($line, '404 not found') !== false)
  63. {
  64. throw new \phpbb\exception\runtime_exception('FILE_NOT_FOUND', array($filename));
  65. }
  66. }
  67. $stream_meta_data = stream_get_meta_data($socket);
  68. if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop)
  69. {
  70. throw new \phpbb\exception\runtime_exception('FSOCK_TIMEOUT');
  71. }
  72. }
  73. @fclose($socket);
  74. }
  75. else
  76. {
  77. if ($this->error_string)
  78. {
  79. $this->error_string = utf8_convert_message($this->error_string);
  80. return false;
  81. }
  82. else
  83. {
  84. throw new \phpbb\exception\runtime_exception('FSOCK_DISABLED');
  85. }
  86. }
  87. return $file_info;
  88. }
  89. /**
  90. * Get error string
  91. *
  92. * @return string Error string
  93. */
  94. public function get_error_string()
  95. {
  96. return $this->error_string;
  97. }
  98. /**
  99. * Get error number
  100. *
  101. * @return int Error number
  102. */
  103. public function get_error_number()
  104. {
  105. return $this->error_number;
  106. }
  107. }