1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package org.openpsa.httplib |
4
|
|
|
* @author The Midgard Project, http://www.midgard-project.org |
5
|
|
|
* @copyright The Midgard Project, http://www.midgard-project.org |
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use GuzzleHttp\Psr7\Request; |
10
|
|
|
use GuzzleHttp\Client; |
11
|
|
|
use GuzzleHttp\RedirectMiddleware; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* HTTP content fetching library |
15
|
|
|
* |
16
|
|
|
* @package org.openpsa.httplib |
17
|
|
|
*/ |
18
|
|
|
class org_openpsa_httplib |
19
|
|
|
{ |
20
|
|
|
private $params = [ |
21
|
|
|
'timeout' => 30, |
22
|
|
|
'ssl_verify_peer' => false, |
23
|
|
|
'follow_redirects' => true |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
public $error = ''; |
27
|
|
|
|
28
|
|
|
public $basicauth = [ |
29
|
|
|
'user' => false, |
30
|
|
|
'password' => false, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set one of the Guzzle parameters |
35
|
|
|
*/ |
36
|
|
|
public function set_param(string $name, $value) |
37
|
|
|
{ |
38
|
|
|
$this->params[$name] = $value; |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
private function get_client() : Client |
42
|
|
|
{ |
43
|
1 |
|
$config = []; |
44
|
1 |
|
foreach ($this->params as $key => $value) { |
45
|
1 |
|
switch ($key) { |
46
|
1 |
|
case 'timeout': |
47
|
1 |
|
$config['timeout'] = $value; |
48
|
1 |
|
break; |
49
|
1 |
|
case 'ssl_verify_peer': |
50
|
1 |
|
$config['verify'] = $value; |
51
|
1 |
|
break; |
52
|
1 |
|
case 'follow_redirects': |
53
|
1 |
|
$value = ($value) ? 10 : 0; |
54
|
1 |
|
$config['allow_redirects'] = RedirectMiddleware::$defaultSettings; |
55
|
1 |
|
$config['allow_redirects']['max'] = $value; |
56
|
1 |
|
break; |
57
|
|
|
default: |
58
|
|
|
debug_add('Unsupported client parameter ' . $key, MIDCOM_LOG_WARN); |
59
|
|
|
} |
60
|
|
|
} |
61
|
1 |
|
return new Client($config); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get contents of given URL |
66
|
|
|
* |
67
|
|
|
* @param string $url Fully qualified URL |
68
|
|
|
* @return string Contents |
69
|
|
|
*/ |
70
|
|
|
public function get(string $url, array $headers = [], string $username = null, string $password = null) |
71
|
|
|
{ |
72
|
|
|
$request = new Request('GET', $url, $headers); |
73
|
|
|
|
74
|
|
|
return $this->send($request, $username, $password); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Post variables and get the resulting page |
79
|
|
|
* |
80
|
|
|
* @param string $uri Fully qualified URL |
81
|
|
|
* @param array $variables The data to POST (key => value pairs) |
82
|
|
|
* @param array $headers Additional HTTP headers |
83
|
|
|
* @return string Contents |
84
|
|
|
*/ |
85
|
1 |
|
public function post(string $uri, array $variables, array $headers = []) |
86
|
|
|
{ |
87
|
|
|
// Handle the variables to POST |
88
|
1 |
|
if (empty($variables)) { |
89
|
|
|
$this->error = '$variables is empty'; |
90
|
|
|
debug_add($this->error, MIDCOM_LOG_ERROR); |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
1 |
|
$headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
94
|
|
|
|
95
|
1 |
|
$request = new Request('POST', $uri, $headers, http_build_query($variables, '', '&')); |
96
|
|
|
|
97
|
1 |
|
return $this->send($request, $this->basicauth['user'], $this->basicauth['password']); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
private function send(Request $request, ?string $username, ?string $password) |
101
|
|
|
{ |
102
|
1 |
|
$request = $request->withHeader('User-Agent', 'Midgard/' . substr(mgd_version(), 0, 4)); |
103
|
|
|
|
104
|
|
|
// Handle basic auth |
105
|
1 |
|
if ( !empty($username) |
106
|
|
|
&& !empty($password)) { |
107
|
|
|
// Set basic auth |
108
|
|
|
$request = $request->withHeader('Authorization', 'Basic ' . base64_encode($username . ':' . $password)); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
$client = $this->get_client(); |
112
|
|
|
|
113
|
|
|
try { |
114
|
1 |
|
$response = $client->send($request); |
115
|
1 |
|
} catch (Exception $e) { |
116
|
1 |
|
$this->error = $e->getMessage(); |
117
|
1 |
|
debug_add("Got error '{$this->error}' from HTTP request", MIDCOM_LOG_INFO); |
118
|
1 |
|
return false; |
119
|
|
|
} |
120
|
|
|
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) { |
121
|
|
|
$this->error = $response->getReasonPhrase(); |
122
|
|
|
debug_add("Got error '{$this->error}' from '{$request->getUri()}'", MIDCOM_LOG_INFO); |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
return (string) $response->getBody(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|