| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikWP - Libraries |
| 4 |
* @subpackage adapter.application |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
JLoader::import('adapter.http.response'); |
| 15 |
|
| 16 |
/** |
| 17 |
* HTTP client class. |
| 18 |
* |
| 19 |
* @since 10.1.23 |
| 20 |
*/ |
| 21 |
class JHttp |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Options for the HTTP client. |
| 25 |
* |
| 26 |
* @var JRegistry |
| 27 |
*/ |
| 28 |
protected $options; |
| 29 |
|
| 30 |
/** |
| 31 |
* Class constructor. |
| 32 |
* |
| 33 |
* @param JRegistry $options Client options object. If the registry contains any headers.* elements, |
| 34 |
* these will be added to the request headers. |
| 35 |
*/ |
| 36 |
public function __construct(JRegistry $options = null) |
| 37 |
{ |
| 38 |
$this->options = isset($options) ? $options : new JRegistry; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Gets an option from the HTTP client. |
| 43 |
* |
| 44 |
* @param string $key The name of the option to get. |
| 45 |
* |
| 46 |
* @return mixed The option value. |
| 47 |
*/ |
| 48 |
public function getOption($key) |
| 49 |
{ |
| 50 |
return $this->options->get($key); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Sets an option for the HTTP client. |
| 55 |
* |
| 56 |
* @param string $key The name of the option to set. |
| 57 |
* @param mixed $value The option value to set. |
| 58 |
* |
| 59 |
* @return self This object to support chaining. |
| 60 |
*/ |
| 61 |
public function setOption($key, $value) |
| 62 |
{ |
| 63 |
$this->options->set($key, $value); |
| 64 |
|
| 65 |
return $this; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Method to send the OPTIONS command to the server. |
| 70 |
* |
| 71 |
* @param string $url Path to the resource. |
| 72 |
* @param array $headers An array of name-value pairs to include in the header of the request. |
| 73 |
* @param integer $timeout Read timeout in seconds. |
| 74 |
* |
| 75 |
* @return Response |
| 76 |
* |
| 77 |
* @since 10.1.29 |
| 78 |
* |
| 79 |
* @uses request() |
| 80 |
*/ |
| 81 |
public function options($url, array $headers = null, $timeout = null) |
| 82 |
{ |
| 83 |
if (is_null($headers)) |
| 84 |
{ |
| 85 |
$headers = array(); |
| 86 |
} |
| 87 |
|
| 88 |
// use OPTIONS method |
| 89 |
$headers['method'] = 'OPTIONS'; |
| 90 |
|
| 91 |
return $this->request('wp_remote_request', $url, null, $headers, $timeout); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Method to send the HEAD command to the server. |
| 96 |
* |
| 97 |
* @param mixed $url Path to the resource. |
| 98 |
* @param array $headers An array of name-value pairs to include in the header of the request. |
| 99 |
* @param integer $timeout Read timeout in seconds. |
| 100 |
* |
| 101 |
* @return JHttpResponse |
| 102 |
* |
| 103 |
* @uses request() |
| 104 |
*/ |
| 105 |
public function head($url, array $headers = array(), $timeout = null) |
| 106 |
{ |
| 107 |
return $this->request('wp_remote_head', $url, null, $headers, $timeout); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Method to send the GET command to the server. |
| 112 |
* |
| 113 |
* @param mixed $url Path to the resource. |
| 114 |
* @param array $headers An array of name-value pairs to include in the header of the request. |
| 115 |
* @param integer $timeout Read timeout in seconds. |
| 116 |
* |
| 117 |
* @return JHttpResponse |
| 118 |
* |
| 119 |
* @uses request() |
| 120 |
*/ |
| 121 |
public function get($url, array $headers = array(), $timeout = null) |
| 122 |
{ |
| 123 |
return $this->request('wp_remote_get', $url, null, $headers, $timeout); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Method to send the POST command to the server. |
| 128 |
* |
| 129 |
* @param mixed $url Path to the resource. |
| 130 |
* @param mixed $data Either an associative array or a string to be sent with the request. |
| 131 |
* @param array $headers An array of name-value pairs to include in the header of the request |
| 132 |
* @param integer $timeout Read timeout in seconds. |
| 133 |
* |
| 134 |
* @return JHttpResponse |
| 135 |
* |
| 136 |
* @uses request() |
| 137 |
*/ |
| 138 |
public function post($url, $data, array $headers = array(), $timeout = null) |
| 139 |
{ |
| 140 |
return $this->request('wp_remote_post', $url, $data, $headers, $timeout); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Method to send the PUT command to the server. |
| 145 |
* |
| 146 |
* @param string $url Path to the resource. |
| 147 |
* @param mixed $data Either an associative array or a string to be sent with the request. |
| 148 |
* @param array $headers An array of name-value pairs to include in the header of the request. |
| 149 |
* @param integer $timeout Read timeout in seconds. |
| 150 |
* |
| 151 |
* @return Response |
| 152 |
* |
| 153 |
* @since 10.1.29 |
| 154 |
* |
| 155 |
* @uses request() |
| 156 |
*/ |
| 157 |
public function put($url, $data, array $headers = null, $timeout = null) |
| 158 |
{ |
| 159 |
if (is_null($headers)) |
| 160 |
{ |
| 161 |
$headers = array(); |
| 162 |
} |
| 163 |
|
| 164 |
// use PUT method |
| 165 |
$headers['method'] = 'PUT'; |
| 166 |
|
| 167 |
return $this->request('wp_remote_request', $url, $data, $headers, $timeout); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Method to send the DELETE command to the server. |
| 172 |
* |
| 173 |
* @param string $url Path to the resource. |
| 174 |
* @param array $headers An array of name-value pairs to include in the header of the request. |
| 175 |
* @param integer $timeout Read timeout in seconds. |
| 176 |
* |
| 177 |
* @return Response |
| 178 |
* |
| 179 |
* @since 10.1.29 |
| 180 |
* |
| 181 |
* @uses request() |
| 182 |
*/ |
| 183 |
public function delete($url, array $headers = null, $timeout = null) |
| 184 |
{ |
| 185 |
if (is_null($headers)) |
| 186 |
{ |
| 187 |
$headers = array(); |
| 188 |
} |
| 189 |
|
| 190 |
// use DELETE method |
| 191 |
$headers['method'] = 'DELETE'; |
| 192 |
|
| 193 |
return $this->request('wp_remote_request', $url, null, $headers, $timeout); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Method to send the TRACE command to the server. |
| 198 |
* |
| 199 |
* @param string $url Path to the resource. |
| 200 |
* @param array $headers An array of name-value pairs to include in the header of the request. |
| 201 |
* @param integer $timeout Read timeout in seconds. |
| 202 |
* |
| 203 |
* @since 10.1.29 |
| 204 |
* |
| 205 |
* @uses request() |
| 206 |
*/ |
| 207 |
public function trace($url, array $headers = null, $timeout = null) |
| 208 |
{ |
| 209 |
if (is_null($headers)) |
| 210 |
{ |
| 211 |
$headers = array(); |
| 212 |
} |
| 213 |
|
| 214 |
// use DELETE method |
| 215 |
$headers['method'] = 'TRACE'; |
| 216 |
|
| 217 |
return $this->request('wp_remote_request', $url, null, $headers, $timeout); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Method to send the PATCH command to the server. |
| 222 |
* |
| 223 |
* @param string $url Path to the resource. |
| 224 |
* @param mixed $data Either an associative array or a string to be sent with the request. |
| 225 |
* @param array $headers An array of name-value pairs to include in the header of the request. |
| 226 |
* @param integer $timeout Read timeout in seconds. |
| 227 |
* |
| 228 |
* @return Response |
| 229 |
* |
| 230 |
* @since 10.1.29 |
| 231 |
* |
| 232 |
* @uses request() |
| 233 |
*/ |
| 234 |
public function patch($url, $data, array $headers = null, $timeout = null) |
| 235 |
{ |
| 236 |
if (is_null($headers)) |
| 237 |
{ |
| 238 |
$headers = array(); |
| 239 |
} |
| 240 |
|
| 241 |
// use PATCH method |
| 242 |
$headers['method'] = 'PATCH'; |
| 243 |
|
| 244 |
return $this->request('wp_remote_request', $url, $data, $headers, $timeout); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Sends a request to the server and returns a HTTP response object. |
| 249 |
* |
| 250 |
* @param string $method The HTTP method for sending the request. |
| 251 |
* @param mixef $uri The URI to the resource to request. |
| 252 |
* @param mixed $data Either an associative array or a string to be sent with the request. |
| 253 |
* @param array $headers An array of request headers to send with the request. |
| 254 |
* @param integer $timeout Read timeout in seconds. |
| 255 |
* |
| 256 |
* @return JHttpResponse |
| 257 |
* |
| 258 |
* @throws RuntimeException |
| 259 |
* |
| 260 |
* @uses fixHeaders() |
| 261 |
*/ |
| 262 |
protected function request($method, $uri, $data = null, array $headers = array(), $timeout = null) |
| 263 |
{ |
| 264 |
// look for headers set in the options |
| 265 |
$temp = (array) $this->options->get('headers'); |
| 266 |
|
| 267 |
foreach ($temp as $key => $val) |
| 268 |
{ |
| 269 |
if (!isset($headers[$key])) |
| 270 |
{ |
| 271 |
$headers[$key] = $val; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
$args = array(); |
| 276 |
|
| 277 |
// overwrite timeout value if specified |
| 278 |
if ($timeout) |
| 279 |
{ |
| 280 |
$args['timeout'] = (int) $timeout; |
| 281 |
} |
| 282 |
else if (isset($headers['timeout'])) |
| 283 |
{ |
| 284 |
/** |
| 285 |
* Use timeout specified within the headers instead. |
| 286 |
* |
| 287 |
* @since 10.1.31 |
| 288 |
*/ |
| 289 |
$args['timeout'] = (int) $headers['timeout']; |
| 290 |
} |
| 291 |
|
| 292 |
// add body to headers |
| 293 |
if ($data) |
| 294 |
{ |
| 295 |
$args['body'] = $data; |
| 296 |
} |
| 297 |
|
| 298 |
if (!function_exists($method)) |
| 299 |
{ |
| 300 |
// throw exception as the requested method is not supported |
| 301 |
throw new RuntimeException(sprintf('HTTP method [%s] not found', $method), 404); |
| 302 |
} |
| 303 |
|
| 304 |
// adjust the headers from Joomla standards to WP |
| 305 |
$this->fixHeaders($headers); |
| 306 |
|
| 307 |
/** |
| 308 |
* Copy headers within the top-level of the array. |
| 309 |
* |
| 310 |
* @since 10.1.29 |
| 311 |
*/ |
| 312 |
$headersLookup = array( |
| 313 |
'method', |
| 314 |
'redirection', |
| 315 |
'httpversion', |
| 316 |
'blocking', |
| 317 |
'headers', |
| 318 |
'cookies', |
| 319 |
'body', |
| 320 |
'compress', |
| 321 |
'decompress', |
| 322 |
'sslverify', |
| 323 |
'sslcertificates', |
| 324 |
'stream', |
| 325 |
'filename', |
| 326 |
'limit_response_size', |
| 327 |
); |
| 328 |
|
| 329 |
// iterate directives |
| 330 |
foreach ($headersLookup as $directive) |
| 331 |
{ |
| 332 |
// check if the headers use it |
| 333 |
if (isset($headers[$directive])) |
| 334 |
{ |
| 335 |
// copy directive |
| 336 |
$args[$directive] = $headers[$directive]; |
| 337 |
unset($headers[$directive]); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Put headers within the proper key. |
| 343 |
* |
| 344 |
* @since 10.1.29 |
| 345 |
*/ |
| 346 |
$args['headers'] = $headers; |
| 347 |
|
| 348 |
// invoke method |
| 349 |
$response = $method($uri, $args); |
| 350 |
|
| 351 |
// convert response |
| 352 |
return new JHttpResponse($response); |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Adjusts the headers properties from Joomla standards for |
| 357 |
* being used with WordPress framework. |
| 358 |
* @link https://codex.wordpress.org/HTTP_API#Other_Arguments |
| 359 |
* |
| 360 |
* @param array &$headers The headers to fix. |
| 361 |
* |
| 362 |
* @return void |
| 363 |
*/ |
| 364 |
protected function fixHeaders(array &$headers) |
| 365 |
{ |
| 366 |
// lookup to adjust the headers, where the key is the |
| 367 |
// Joomla notation and the value is the correct attribute |
| 368 |
// that should be used on WordPress. |
| 369 |
$lookup = array( |
| 370 |
'userAgent' => 'user-agent', |
| 371 |
); |
| 372 |
|
| 373 |
foreach ($headers as $k => $v) |
| 374 |
{ |
| 375 |
// check if the key is set |
| 376 |
if (isset($lookup[$k])) |
| 377 |
{ |
| 378 |
// copy the value within the correct attribute |
| 379 |
$headers[$lookup[$k]] = $v; |
| 380 |
// unset the Joomla notation value |
| 381 |
unset($headers[$k]); |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
|