|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the puli/installer package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Puli\Installer; |
|
13
|
|
|
|
|
14
|
|
|
use RuntimeException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Downloads files. |
|
18
|
|
|
* |
|
19
|
|
|
* This file was adapted from the installer file bundled with Composer. For the |
|
20
|
|
|
* original file, authors and copyright information see |
|
21
|
|
|
* |
|
22
|
|
|
* https://github.com/composer/getcomposer.org/blob/master/web/installer |
|
23
|
|
|
* |
|
24
|
|
|
* @author Nils Adermann <[email protected]> |
|
25
|
|
|
* @author Jordi Boggiano <[email protected]> |
|
26
|
|
|
* @author Thomas Rudolph <[email protected]> |
|
27
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class HttpClient |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $headers = array( |
|
35
|
|
|
"Connection: close\r\n", |
|
36
|
|
|
"User-Agent: Puli Installer\r\n", |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Creates the HTTP client. |
|
41
|
|
|
*/ |
|
42
|
5 |
|
public function __construct() |
|
43
|
|
|
{ |
|
44
|
5 |
|
if (extension_loaded('zlib')) { |
|
45
|
5 |
|
$this->headers[] = "Accept-Encoding: gzip\r\n"; |
|
46
|
5 |
|
} |
|
47
|
5 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Downloads a file. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $url The URL of the file to download. |
|
53
|
|
|
* |
|
54
|
|
|
* @return string The downloaded file body. |
|
55
|
|
|
*/ |
|
56
|
5 |
|
public function download($url) |
|
57
|
|
|
{ |
|
58
|
5 |
|
humbug_set_headers($this->headers); |
|
59
|
5 |
|
$result = humbug_get_contents($url); |
|
60
|
|
|
|
|
61
|
5 |
|
if ($result && extension_loaded('zlib')) { |
|
62
|
5 |
|
$decode = false; |
|
63
|
|
|
|
|
64
|
5 |
|
foreach (humbug_get_headers() as $header) { |
|
65
|
5 |
|
if (preg_match('{^content-encoding: *gzip *$}i', $header)) { |
|
66
|
|
|
$decode = true; |
|
67
|
|
|
continue; |
|
68
|
5 |
|
} elseif (preg_match('{^HTTP/}i', $header)) { |
|
69
|
5 |
|
$decode = false; |
|
70
|
5 |
|
} |
|
71
|
5 |
|
} |
|
72
|
|
|
|
|
73
|
5 |
|
if ($decode) { |
|
74
|
|
|
$result = version_compare(PHP_VERSION, '5.4.0', '>=') |
|
75
|
|
|
? zlib_decode($result) |
|
76
|
|
|
// work around issue with gzuncompress & co that do not work with all gzip checksums |
|
77
|
|
|
: file_get_contents('compress.zlib://data:application/octet-stream;base64,'.base64_encode($result)); |
|
78
|
|
|
|
|
79
|
|
|
if (!$result) { |
|
80
|
|
|
throw new RuntimeException('Failed to decode zlib stream'); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
5 |
|
} |
|
84
|
|
|
|
|
85
|
5 |
|
return $result; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|