-
Notifications
You must be signed in to change notification settings - Fork 454
/
Factory.php
224 lines (212 loc) · 7.45 KB
/
Factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
namespace Concrete\Core\Http\Client;
use Concrete\Core\Application\Application;
use Concrete\Core\Config\Repository\Repository;
use Zend\Http\Client\Adapter\Curl;
use Zend\Http\Client\Adapter\Proxy;
class Factory
{
/**
* @var Application
*/
protected $app;
/**
* @param Application $app
*/
public function __construct(Application $app)
{
$this->app = $app;
}
/**
* Read the HTTP Client configuration.
*
* @param Repository $config
*
* @return array {
* @var bool $sslverifypeer [always]
* @var string $proxyhost [optional]
* @var int $proxyport [optional]
* @var string $proxyuser [optional]
* @var string $proxypass [optional]
* ... and all other options set in app.curl
* }
*/
protected function getOptions(Repository $config)
{
$result = $config->get('app.http_client');
if (!is_array($result)) {
$result = [];
}
$result['proxyhost'] = $config->get('concrete.proxy.host');
$result['proxyport'] = $config->get('concrete.proxy.port');
$result['proxyuser'] = $config->get('concrete.proxy.user');
$result['proxypass'] = $config->get('concrete.proxy.password');
return $result;
}
/**
* Normalize the Zend HTTP Client options.
*
* @param array $options
*
* @return array
*/
protected function normalizeOptions(array $options)
{
// Default values
$result = [
'adapter' => null,
'sslverifypeer' => true,
'sslverifypeername' => true,
'proxyhost' => '',
'proxyport' => 8080,
'proxyuser' => '',
'proxypass' => '',
'sslcafile' => null,
'sslcapath' => null,
'connecttimeout' => 5,
'timeout' => 60,
'keepalive' => false,
'maxredirects' => 5,
'rfc3986strict' => false,
'sslcert' => null,
'sslpassphrase' => null,
'storeresponse' => true,
'streamtmpdir' => null,
'strictredirects' => false,
'useragent' => 'concrete5 CMS',
'encodecookies' => true,
'httpversion' => '1.1',
'ssltransport' => 'tls',
'sslallowselfsigned' => false,
'persistent' => false,
'logger' => null,
];
// Normalize the given options
foreach ($options as $key => $value) {
// Normalize the option key (exactly like Zend Http Client)
$key = str_replace(['-', '_', ' ', '.'], '', strtolower($key));
switch ($key) {
// Not nullable boolean values
case 'sslverifypeer':
case 'sslverifypeername':
case 'keepalive':
case 'rfc3986strict':
case 'storeresponse':
case 'strictredirects':
case 'encodecookies':
case 'sslallowselfsigned':
case 'persistent':
if ($value !== null && $value !== '') {
$result[$key] = (bool) $value;
}
break;
// Not nullable integer values
case 'proxyport':
case 'connecttimeout':
case 'timeout':
case 'maxredirects':
if (is_int($value) || is_float($value) || (is_string($value) && is_numeric($value))) {
$result[$key] = (int) $value;
}
break;
// Strings thay may be empty
case 'proxyhost':
case 'proxyuser':
case 'proxypass':
case 'sslpassphrase':
case 'useragent':
$result[$key] = is_string($value) ? $value : '';
break;
// Strings thay can't be empty
case 'sslcafile':
case 'sslcapath':
case 'sslcert':
case 'httpversion':
case 'ssltransport':
case 'streamtmpdir':
if (is_string($value) && $value !== '') {
$result[$key] = $value;
}
break;
// Class names / instances
case 'adapter':
case 'logger':
if ((is_string($value) && $value !== '') || is_object($value)) {
$result[$key] = $value;
}
break;
// Pass through
default:
$result[$key] = $value;
break;
}
}
// Set the temporary directory
if ($result['streamtmpdir'] === null) {
$result['streamtmpdir'] = $this->app->make('helper/file')->getTemporaryDirectory();
}
// Fix proxy options (Zend uses some of these values even if they are null)
if ($result['proxyhost'] === '') {
unset($result['proxyhost']);
unset($result['proxyport']);
unset($result['proxyuser']);
unset($result['proxypass']);
} elseif ($result['proxyuser'] === '') {
unset($result['proxyuser']);
unset($result['proxypass']);
}
// Don't set sslpassphrase is sslcert is not set (Zend tries to set the sslpassphrase even if no sslcert has been specified)
if ($result['sslcert'] === null) {
unset($result['sslpassphrase']);
}
// Don't pass sslcafile/sslcapath if sslverifypeer is false
if ($result['sslverifypeer'] === false) {
$result['sslcafile'] = null;
$result['sslcapath'] = null;
}
return $result;
}
/**
* Create a new HTTP Client instance starting from configuration.
*
* @param Repository $config
* @param string|object|null $adapter
*
* @return Client
*/
public function createFromConfig(Repository $config, $adapter = null)
{
$options = $this->getOptions($config);
return $this->createFromOptions($options, $adapter);
}
/**
* Create a new HTTP Client instance starting from configuration.
*
* @param array $options See the app.http_client values at concrete/config/app.php, plus the concrete.proxy values at concrete/config/concrete.php.
* @param mixed $adapter The adapter to use (defaults to Curl adapter if curl extension is installed, otherwise we'll use the Socket adapter)
*
* @return Client
*/
public function createFromOptions(array $options, $adapter = null)
{
$options = $this->normalizeOptions($options);
if ((is_string($adapter) && $adapter !== '') || is_object($adapter)) {
$options['adapter'] = $adapter;
} elseif ($options['adapter'] === null) {
if (function_exists('curl_init')) {
$options['adapter'] = Curl::class;
} else {
$options['adapter'] = Proxy::class;
}
}
$client = new Client(null, $options);
if ($options['logger'] !== null) {
if (is_object($options['logger'])) {
$client->setLogger($options['logger']);
} else {
$client->setLogger($this->app->make($options['logger']));
}
}
return $client;
}
}