Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
86.67% |
26 / 30 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
file_downloader | |
86.67% |
26 / 30 |
|
33.33% |
1 / 3 |
13.40 | |
0.00% |
0 / 1 |
get | |
89.29% |
25 / 28 |
|
0.00% |
0 / 1 |
11.15 | |||
get_error_string | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
get_error_number | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
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 | |
14 | namespace phpbb; |
15 | |
16 | class file_downloader |
17 | { |
18 | /** @var string Error string */ |
19 | protected $error_string = ''; |
20 | |
21 | /** @var int Error number */ |
22 | protected $error_number = 0; |
23 | |
24 | /** |
25 | * Retrieve contents from remotely stored file |
26 | * |
27 | * @param string $host File host |
28 | * @param string $directory Directory file is in |
29 | * @param string $filename Filename of file to retrieve |
30 | * @param int $port Port to connect to; default: 80 |
31 | * @param int $timeout Connection timeout in seconds; default: 6 |
32 | * |
33 | * @return false|string File data as string if file can be read and there is no |
34 | * timeout, false if there were errors or the connection timed out |
35 | * |
36 | * @throws \phpbb\exception\runtime_exception If data can't be retrieved and no error |
37 | * message is returned |
38 | */ |
39 | public function get($host, $directory, $filename, $port = 80, $timeout = 6) |
40 | { |
41 | // Set default values for error variables |
42 | $this->error_number = 0; |
43 | $this->error_string = ''; |
44 | |
45 | if (function_exists('fsockopen') && |
46 | $socket = @fsockopen(($port == 443 ? 'ssl://' : '') . $host, $port, $this->error_number, $this->error_string, $timeout) |
47 | ) |
48 | { |
49 | @fputs($socket, "GET $directory/$filename HTTP/1.0\r\n"); |
50 | @fputs($socket, "HOST: $host\r\n"); |
51 | @fputs($socket, "Connection: close\r\n\r\n"); |
52 | |
53 | $timer_stop = time() + $timeout; |
54 | stream_set_timeout($socket, $timeout); |
55 | |
56 | $file_info = ''; |
57 | $get_info = false; |
58 | |
59 | while (!@feof($socket)) |
60 | { |
61 | if ($get_info) |
62 | { |
63 | $file_info .= @fread($socket, 1024); |
64 | } |
65 | else |
66 | { |
67 | $line = @fgets($socket, 1024); |
68 | if ($line == "\r\n") |
69 | { |
70 | $get_info = true; |
71 | } |
72 | else if (stripos($line, '404 not found') !== false) |
73 | { |
74 | throw new \phpbb\exception\runtime_exception('FILE_NOT_FOUND', array($filename)); |
75 | } |
76 | } |
77 | |
78 | $stream_meta_data = stream_get_meta_data($socket); |
79 | |
80 | if ($stream_meta_data['timed_out'] || time() >= $timer_stop) |
81 | { |
82 | throw new \phpbb\exception\runtime_exception('FSOCK_TIMEOUT'); |
83 | } |
84 | } |
85 | @fclose($socket); |
86 | } |
87 | else |
88 | { |
89 | if ($this->error_string) |
90 | { |
91 | $this->error_string = utf8_convert_message($this->error_string); |
92 | return false; |
93 | } |
94 | else |
95 | { |
96 | throw new \phpbb\exception\runtime_exception('FSOCK_DISABLED'); |
97 | } |
98 | } |
99 | |
100 | return $file_info; |
101 | } |
102 | |
103 | /** |
104 | * Get error string |
105 | * |
106 | * @return string Error string |
107 | */ |
108 | public function get_error_string() |
109 | { |
110 | return $this->error_string; |
111 | } |
112 | |
113 | /** |
114 | * Get error number |
115 | * |
116 | * @return int Error number |
117 | */ |
118 | public function get_error_number() |
119 | { |
120 | return $this->error_number; |
121 | } |
122 | } |