2015-09-14 16:23:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Core;
|
|
|
|
|
2015-11-09 18:31:26 +01:00
|
|
|
use Icinga\Application\Benchmark;
|
2015-09-29 19:01:25 +02:00
|
|
|
use Exception;
|
|
|
|
|
2015-09-14 16:23:43 +02:00
|
|
|
class RestApiClient
|
|
|
|
{
|
|
|
|
protected $version = 'v1';
|
|
|
|
|
|
|
|
protected $peer;
|
|
|
|
|
|
|
|
protected $port;
|
|
|
|
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
protected $pass;
|
|
|
|
|
2015-12-23 17:12:53 +01:00
|
|
|
protected $curl;
|
|
|
|
|
2015-09-14 16:23:43 +02:00
|
|
|
public function __construct($peer, $port = 5665, $cn = null)
|
|
|
|
{
|
|
|
|
$this->peer = $peer;
|
|
|
|
$this->port = $port;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: replace with Web2 CA trust resource plus cert and get rid
|
|
|
|
// of user/pass or at least strongly advise against using it
|
|
|
|
public function setCredentials($user, $pass)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
$this->pass = $pass;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPeerIdentity()
|
|
|
|
{
|
|
|
|
return $this->peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function url($url)
|
|
|
|
{
|
|
|
|
return sprintf('https://%s:%d/%s/%s', $this->peer, $this->port, $this->version, $url);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function request($method, $url, $body = null, $raw = false)
|
2015-12-23 17:12:53 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
// TODO: Re-enabled once problems with 5.4 are fixed:
|
|
|
|
|
|
|
|
if (function_exists('curl_version')) {
|
|
|
|
return $this->curlRequest($method, $url, $body, $raw);
|
|
|
|
} elseif (version_compare(PHP_VERSION, '5.4.0') >= 0) {
|
|
|
|
return $this->phpRequest($method, $url, $body, $raw);
|
|
|
|
*/
|
|
|
|
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
|
|
|
|
return $this->phpRequest($method, $url, $body, $raw);
|
|
|
|
} elseif (function_exists('curl_version')) {
|
|
|
|
return $this->curlRequest($method, $url, $body, $raw);
|
|
|
|
} else {
|
|
|
|
throw new Exception('No CURL extension detected, this is required for PHP < 5.4');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function phpRequest($method, $url, $body = null, $raw = false)
|
2015-09-14 16:23:43 +02:00
|
|
|
{
|
|
|
|
$auth = base64_encode(sprintf('%s:%s', $this->user, $this->pass));
|
|
|
|
$headers = array(
|
|
|
|
'Host: ' . $this->getPeerIdentity(),
|
|
|
|
'Authorization: Basic ' . $auth,
|
|
|
|
'Connection: close'
|
|
|
|
);
|
2015-11-06 09:31:40 +01:00
|
|
|
|
|
|
|
if (! $raw) {
|
|
|
|
$headers[] = 'Accept: application/json';
|
|
|
|
}
|
|
|
|
|
2015-09-14 16:23:43 +02:00
|
|
|
if ($body !== null) {
|
|
|
|
$body = json_encode($body);
|
|
|
|
$headers[] = 'Content-Type: application/json';
|
|
|
|
}
|
|
|
|
|
|
|
|
$opts = array(
|
|
|
|
'http' => array(
|
|
|
|
'protocol_version' => '1.1',
|
|
|
|
'user_agent' => 'Icinga Web 2.0 - Director',
|
|
|
|
'method' => strtoupper($method),
|
|
|
|
'content' => $body,
|
2015-09-29 19:01:25 +02:00
|
|
|
'header' => $headers,
|
|
|
|
'ignore_errors' => true
|
2015-09-14 16:23:43 +02:00
|
|
|
),
|
|
|
|
'ssl' => array(
|
2015-12-23 17:12:53 +01:00
|
|
|
// TODO: Fix this!
|
2015-09-14 16:23:43 +02:00
|
|
|
'verify_peer' => false,
|
|
|
|
// 'cafile' => $dir . 'cacert.pem',
|
|
|
|
// 'verify_depth' => 5,
|
|
|
|
// 'CN_match' => $peerName // != peer
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$context = stream_context_create($opts);
|
|
|
|
|
2015-11-09 18:31:26 +01:00
|
|
|
Benchmark::measure('Rest Api, sending ' . $url);
|
2015-09-29 19:01:25 +02:00
|
|
|
$res = file_get_contents($this->url($url), false, $context);
|
|
|
|
if (substr(array_shift($http_response_header), 0, 10) !== 'HTTP/1.1 2') {
|
|
|
|
throw new Exception($res);
|
|
|
|
}
|
2015-11-09 18:31:26 +01:00
|
|
|
Benchmark::measure('Rest Api, got response');
|
2015-09-14 16:23:43 +02:00
|
|
|
if ($raw) {
|
2015-09-29 19:01:25 +02:00
|
|
|
return $res;
|
2015-09-14 16:23:43 +02:00
|
|
|
} else {
|
2015-09-29 19:01:25 +02:00
|
|
|
return RestApiResponse::fromJsonResult($res);
|
2015-09-14 16:23:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-23 17:12:53 +01:00
|
|
|
protected function curlRequest($method, $url, $body = null, $raw = false)
|
|
|
|
{
|
|
|
|
$auth = sprintf('%s:%s', $this->user, $this->pass);
|
|
|
|
$headers = array(
|
|
|
|
'Host: ' . $this->getPeerIdentity(),
|
|
|
|
'Authorization: Basic ' . $auth,
|
|
|
|
'Connection: close'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (! $raw) {
|
|
|
|
$headers[] = 'Accept: application/json';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($body !== null) {
|
|
|
|
$body = json_encode($body);
|
|
|
|
$headers[] = 'Content-Type: application/json';
|
|
|
|
}
|
|
|
|
|
|
|
|
$curl = $this->curl();
|
|
|
|
$opts = array(
|
|
|
|
CURLOPT_URL => $this->url($url),
|
|
|
|
CURLOPT_HTTPHEADER => $headers,
|
|
|
|
CURLOPT_USERPWD => base64_decode($auth),
|
|
|
|
CURLOPT_CUSTOMREQUEST => strtoupper($method),
|
|
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
|
|
|
|
|
|
// TODO: Fix this!
|
|
|
|
CURLOPT_SSL_VERIFYHOST => false,
|
|
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($body !== null) {
|
|
|
|
$opts[CURLOPT_POSTFIELDS] = $body;
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_setopt_array($curl, $opts);
|
|
|
|
|
|
|
|
Benchmark::measure('Rest Api, sending ' . $url);
|
|
|
|
$res = curl_exec($curl);
|
|
|
|
if ($res === false) {
|
|
|
|
throw new Exception('CURL ERROR: ' . curl_error($curl));
|
|
|
|
}
|
|
|
|
if ($code == 200) {
|
|
|
|
$response = json_decode($response, true);
|
|
|
|
print_r($response);
|
|
|
|
}
|
|
|
|
Benchmark::measure('Rest Api, got response');
|
|
|
|
if ($raw) {
|
|
|
|
return $res;
|
|
|
|
} else {
|
|
|
|
return RestApiResponse::fromJsonResult($res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 16:23:43 +02:00
|
|
|
public function get($url, $body = null)
|
|
|
|
{
|
|
|
|
return $this->request('get', $url, $body);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRaw($url, $body = null)
|
|
|
|
{
|
|
|
|
return $this->request('get', $url, $body, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function post($url, $body = null)
|
|
|
|
{
|
|
|
|
return $this->request('post', $url, $body);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function put($url, $body = null)
|
|
|
|
{
|
|
|
|
return $this->request('put', $url, $body);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($url, $body = null)
|
|
|
|
{
|
|
|
|
return $this->request('delete', $url, $body);
|
|
|
|
}
|
2015-12-23 17:12:53 +01:00
|
|
|
|
|
|
|
protected function curl()
|
|
|
|
{
|
|
|
|
if ($this->curl === null) {
|
|
|
|
$this->curl = curl_init(sprintf('https://%s:%d', $this->peer, $this->port));
|
|
|
|
if (! $this->curl) {
|
|
|
|
throw new Exception('CURL INIT ERROR: ' . curl_error($this->curl));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->curl;
|
|
|
|
}
|
2015-09-14 16:23:43 +02:00
|
|
|
}
|