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