|
1
|
|
|
<?php |
|
2
|
|
|
namespace Hirak\Prestissimo; |
|
3
|
|
|
|
|
4
|
|
|
use Composer\Util; |
|
5
|
|
|
use Composer\IO; |
|
6
|
|
|
|
|
7
|
|
|
class BaseRequest |
|
8
|
|
|
{ |
|
9
|
|
|
private $scheme; |
|
10
|
|
|
private $user; |
|
11
|
|
|
private $pass; |
|
12
|
|
|
private $host; |
|
13
|
|
|
private $port; |
|
14
|
|
|
private $path; |
|
15
|
|
|
private $query = array(); |
|
16
|
|
|
|
|
17
|
|
|
/** @var [string => string] */ |
|
18
|
|
|
private $headers = array(); |
|
19
|
|
|
|
|
20
|
|
|
private $capath; |
|
21
|
|
|
private $cafile; |
|
22
|
|
|
|
|
23
|
|
|
protected static $defaultCurlOptions = array(); |
|
24
|
|
|
|
|
25
|
|
|
private static $NSS_CIPHERS = array( |
|
26
|
|
|
'rsa_3des_sha', |
|
27
|
|
|
'rsa_des_sha', |
|
28
|
|
|
'rsa_null_md5', |
|
29
|
|
|
'rsa_null_sha', |
|
30
|
|
|
'rsa_rc2_40_md5', |
|
31
|
|
|
'rsa_rc4_128_md5', |
|
32
|
|
|
'rsa_rc4_128_sha', |
|
33
|
|
|
'rsa_rc4_40_md5', |
|
34
|
|
|
'fips_des_sha', |
|
35
|
|
|
'fips_3des_sha', |
|
36
|
|
|
'rsa_des_56_sha', |
|
37
|
|
|
'rsa_rc4_56_sha', |
|
38
|
|
|
'rsa_aes_128_sha', |
|
39
|
|
|
'rsa_aes_256_sha', |
|
40
|
|
|
'rsa_aes_128_gcm_sha_256', |
|
41
|
|
|
'dhe_rsa_aes_128_gcm_sha_256', |
|
42
|
|
|
'ecdh_ecdsa_null_sha', |
|
43
|
|
|
'ecdh_ecdsa_rc4_128_sha', |
|
44
|
|
|
'ecdh_ecdsa_3des_sha', |
|
45
|
|
|
'ecdh_ecdsa_aes_128_sha', |
|
46
|
|
|
'ecdh_ecdsa_aes_256_sha', |
|
47
|
|
|
'ecdhe_ecdsa_null_sha', |
|
48
|
|
|
'ecdhe_ecdsa_rc4_128_sha', |
|
49
|
|
|
'ecdhe_ecdsa_3des_sha', |
|
50
|
|
|
'ecdhe_ecdsa_aes_128_sha', |
|
51
|
|
|
'ecdhe_ecdsa_aes_256_sha', |
|
52
|
|
|
'ecdh_rsa_null_sha', |
|
53
|
|
|
'ecdh_rsa_128_sha', |
|
54
|
|
|
'ecdh_rsa_3des_sha', |
|
55
|
|
|
'ecdh_rsa_aes_128_sha', |
|
56
|
|
|
'ecdh_rsa_aes_256_sha', |
|
57
|
|
|
'echde_rsa_null', |
|
58
|
|
|
'ecdhe_rsa_rc4_128_sha', |
|
59
|
|
|
'ecdhe_rsa_3des_sha', |
|
60
|
|
|
'ecdhe_rsa_aes_128_sha', |
|
61
|
|
|
'ecdhe_rsa_aes_256_sha', |
|
62
|
|
|
'ecdhe_ecdsa_aes_128_gcm_sha_256', |
|
63
|
|
|
'ecdhe_rsa_aes_128_gcm_sha_256', |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* enable ECC cipher suites in cURL/NSS |
|
68
|
|
|
* @codeCoverageIgnore |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function nssCiphers() |
|
71
|
|
|
{ |
|
72
|
|
|
static $cache; |
|
73
|
|
|
if (isset($cache)) { |
|
74
|
|
|
return $cache; |
|
75
|
|
|
} |
|
76
|
|
|
$ver = curl_version(); |
|
77
|
|
|
if (preg_match('/^NSS.*Basic ECC$/', $ver['ssl_version'])) { |
|
78
|
|
|
return $cache = implode(',', self::$NSS_CIPHERS); |
|
79
|
|
|
} |
|
80
|
|
|
return $cache = false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function getProxy($url) |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
if (isset($_SERVER['no_proxy'])) { |
|
86
|
|
|
$pattern = new Util\NoProxyPattern($_SERVER['no_proxy']); |
|
87
|
|
|
if ($pattern->test($url)) { |
|
88
|
|
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
View Code Duplication |
if ($this->scheme === 'https') { |
|
|
|
|
|
|
93
|
|
|
if (isset($_SERVER['HTTPS_PROXY'])) { |
|
94
|
|
|
return $_SERVER['HTTPS_PROXY']; |
|
95
|
|
|
} |
|
96
|
|
|
if (isset($_SERVER['https_proxy'])) { |
|
97
|
|
|
return $_SERVER['https_proxy']; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
View Code Duplication |
if ($this->scheme === 'http') { |
|
|
|
|
|
|
102
|
|
|
if (isset($_SERVER['HTTP_PROXY'])) { |
|
103
|
|
|
return $_SERVER['HTTP_PROXY']; |
|
104
|
|
|
} |
|
105
|
|
|
if (isset($_SERVER['http_proxy'])) { |
|
106
|
|
|
return $_SERVER['http_proxy']; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
return null; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param $io |
|
114
|
|
|
* @param bool $useRedirector |
|
115
|
|
|
* @param $githubDomains |
|
116
|
|
|
* @param $gitlabDomains |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function setupAuthentication(IO\IOInterface $io, $useRedirector, array $githubDomains, array $gitlabDomains) |
|
119
|
|
|
{ |
|
120
|
|
|
if (preg_match('/\.github\.com$/', $this->host)) { |
|
121
|
|
|
$authKey = 'github.com'; |
|
122
|
|
|
if ($useRedirector) { |
|
123
|
|
|
if ($this->host === 'api.github.com' && preg_match('%^/repos(/[^/]+/[^/]+/)zipball(.+)$%', $this->path, $_)) { |
|
124
|
|
|
$this->host = 'codeload.github.com'; |
|
125
|
|
|
$this->path = $_[1] . 'legacy.zip' . $_[2]; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} else { |
|
129
|
|
|
$authKey = $this->host; |
|
130
|
|
|
} |
|
131
|
|
|
if (!$io->hasAuthentication($authKey)) { |
|
132
|
|
|
if ($this->user || $this->pass) { |
|
133
|
|
|
$io->setAuthentication($authKey, $this->user, $this->pass); |
|
134
|
|
|
} else { |
|
135
|
|
|
return; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$auth = $io->getAuthentication($authKey); |
|
140
|
|
|
|
|
141
|
|
|
// is github |
|
142
|
|
|
if (in_array($authKey, $githubDomains) && 'x-oauth-basic' === $auth['password']) { |
|
143
|
|
|
$this->addParam('access_token', $auth['username']); |
|
144
|
|
|
$this->user = $this->pass = null; |
|
145
|
|
|
return; |
|
146
|
|
|
} |
|
147
|
|
|
// is gitlab |
|
148
|
|
|
if (in_array($authKey, $gitlabDomains) && 'oauth2' === $auth['password']) { |
|
149
|
|
|
$this->addHeader('authorization', 'Bearer ' . $auth['username']); |
|
150
|
|
|
$this->user = $this->pass = null; |
|
151
|
|
|
return; |
|
152
|
|
|
} |
|
153
|
|
|
// others, includes bitbucket |
|
154
|
|
|
$this->user = $auth['username']; |
|
155
|
|
|
$this->pass = $auth['password']; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return array |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getCurlOptions() |
|
162
|
|
|
{ |
|
163
|
|
|
$headers = array(); |
|
164
|
|
|
foreach ($this->headers as $key => $val) { |
|
165
|
|
|
$headers[] = strtr(ucwords(strtr($key, '-', ' ')), ' ', '-') . ': ' . $val; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$url = $this->getURL(); |
|
169
|
|
|
|
|
170
|
|
|
$curlOpts = array( |
|
171
|
|
|
CURLOPT_URL => $url, |
|
172
|
|
|
CURLOPT_HTTPHEADER => $headers, |
|
173
|
|
|
CURLOPT_USERAGENT => ConfigFacade::getUserAgent(), |
|
174
|
|
|
//CURLOPT_VERBOSE => true, //for debug |
|
175
|
|
|
); |
|
176
|
|
|
$curlOpts += static::$defaultCurlOptions; |
|
177
|
|
|
|
|
178
|
|
|
if ($ciphers = $this->nssCiphers()) { |
|
179
|
|
|
$curlOpts[CURLOPT_SSL_CIPHER_LIST] = $ciphers; |
|
180
|
|
|
} |
|
181
|
|
|
if ($proxy = $this->getProxy($url)) { |
|
182
|
|
|
$curlOpts[CURLOPT_PROXY] = $proxy; |
|
183
|
|
|
} |
|
184
|
|
|
if ($this->capath) { |
|
185
|
|
|
$curlOpts[CURLOPT_CAPATH] = $this->capath; |
|
186
|
|
|
} |
|
187
|
|
|
if ($this->cafile) { |
|
188
|
|
|
$curlOpts[CURLOPT_CAINFO] = $this->cafile; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $curlOpts; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return string |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getURL() |
|
198
|
|
|
{ |
|
199
|
|
|
$url = self::ifOr($this->scheme, '', '://'); |
|
200
|
|
|
if ($this->user) { |
|
201
|
|
|
$user = $this->user; |
|
202
|
|
|
$user .= self::ifOr($this->pass, ':'); |
|
203
|
|
|
$url .= $user . '@'; |
|
204
|
|
|
} |
|
205
|
|
|
$url .= self::ifOr($this->host); |
|
206
|
|
|
$url .= self::ifOr($this->port, ':'); |
|
207
|
|
|
$url .= self::ifOr($this->path); |
|
208
|
|
|
$url .= self::ifOr(http_build_query($this->query), '?'); |
|
209
|
|
|
return $url; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @return string user/pass/access_token masked url |
|
214
|
|
|
*/ |
|
215
|
|
View Code Duplication |
public function getMaskedURL() |
|
|
|
|
|
|
216
|
|
|
{ |
|
217
|
|
|
$url = self::ifOr($this->scheme, '', '://'); |
|
218
|
|
|
$url .= self::ifOr($this->host); |
|
219
|
|
|
$url .= self::ifOr($this->port, ':'); |
|
220
|
|
|
$url .= self::ifOr($this->path); |
|
221
|
|
|
return $url; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @return string |
|
226
|
|
|
*/ |
|
227
|
|
View Code Duplication |
public function getOriginURL() |
|
|
|
|
|
|
228
|
|
|
{ |
|
229
|
|
|
$url = self::ifOr($this->scheme, '', '://'); |
|
230
|
|
|
$url .= self::ifOr($this->host); |
|
231
|
|
|
$url .= self::ifOr($this->port, ':'); |
|
232
|
|
|
return $url; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
private static function ifOr($str, $pre = '', $post = '') |
|
236
|
|
|
{ |
|
237
|
|
|
if ($str) { |
|
238
|
|
|
return $pre . $str . $post; |
|
239
|
|
|
} |
|
240
|
|
|
return ''; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @param string $url |
|
245
|
|
|
*/ |
|
246
|
|
|
public function setURL($url) |
|
247
|
|
|
{ |
|
248
|
|
|
$struct = parse_url($url); |
|
249
|
|
|
foreach ($struct as $key => $val) { |
|
|
|
|
|
|
250
|
|
|
if ($key === 'query') { |
|
251
|
|
|
parse_str($val, $this->query); |
|
252
|
|
|
} else { |
|
253
|
|
|
$this->$key = $val; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
public function addParam($key, $val) |
|
259
|
|
|
{ |
|
260
|
|
|
$this->query[$key] = $val; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
public function addHeader($key, $val) |
|
264
|
|
|
{ |
|
265
|
|
|
$this->headers[strtolower($key)] = $val; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
public function setCA($path = null, $file = null) |
|
269
|
|
|
{ |
|
270
|
|
|
$this->capath = $path; |
|
271
|
|
|
$this->cafile = $file; |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: