You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

224 lines
4.8 KiB

<?php
namespace SamApiLib;
class HcpApi
{
private $base_url;
private $auth_token;
const cookie_name = "hcp-ns-auth";
function __construct($base_url, $login, $password)
{
$this->base_url = $base_url;
$this->auth_token = base64_encode($login) . ":" . md5($password);
}
public static function create($protocol, $repository_url, $tenant, $namespace, $login, $password)
{
$base_url = "$protocol://$namespace.$tenant.$repository_url";
return new self($base_url, $login, $password);
}
/**
* REST API query helper
* @param $ws_route
* @param $url_params
* @param $query_params
* @param $body_params
* @return array
*/
protected function query($ws_method, $ws_url, $url_params, $query_params, $body_params)
{
$query_string = http_build_query($query_params);
foreach ($url_params as $url_param => $value)
{
$ws_url = str_replace('{'.$url_param.'}', $value, $ws_url);
}
$url = $this->base_url . $ws_url . "?" . $query_string;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
$ws_method = strtoupper($ws_method);
if($ws_method !== "GET")
{
if($ws_method === "POST")
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, (json_encode($body_params)));
}
elseif ($ws_method === "PUT")
{
curl_setopt($ch, CURLOPT_PUT, true);
if(file_exists($body_params) && is_readable($body_params))
{
curl_setopt($ch, CURLOPT_INFILE, fopen($body_params, "r"));
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($body_params));
}
else
{
die("file open error");
}
}
elseif ($ws_method === "HEAD")
{
curl_setopt($ch, CURLOPT_NOBODY, true);
}
else
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $ws_method);
}
}
curl_setopt($ch, CURLOPT_COOKIE, self::cookie_name . "=" . $this->auth_token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
if ($res === false)
{
echo "curl error #" . curl_errno($ch) . " : " . curl_error($ch); die;
}
curl_close($ch);
// var_dump($res); die;
list($headers, $body) = explode("\r\n\r\n", $res); // PHP_EOL.PHP_EOL
$headers = explode("\r\n", $headers);
// sometimes headers are in the body
if(count($headers) === 1 && !empty($body))
{
$headers = explode("\r\n", $body);
$body = "";
}
else
{
$body2 = json_decode($body, true);
if(json_last_error() === JSON_ERROR_NONE)
{ // content was really json
$body = $body2; // use decoded content
}
}
$status_row = array_shift($headers);
preg_match("|HTTP/1.1 (\d+) (.+)|", $status_row, $matches);
$status = $matches[1];
$headers2 = [];
foreach ($headers as $header)
{
list($key, $value) = \explode(": ", $header);
$headers2 [$key] = $value;
}
return [$status, $headers2, $body];
}
public function put_file ($filename, $directory)
{
$basename = basename($filename);
list($status, $headers, $body) = $this->query("put", "/rest/$directory/$basename", [], [], $filename);
if($status === "201")
{
// check the hash
$keyword = "X-HCP-Hash";
$algo = "";
$hash = "";
$test = array_filter($headers, function($header) use ($keyword, &$algo, &$hash)
{
if(preg_match("|$keyword: (.+) (.+)|", $header, $matches) === 1)
{
list(,$algo, $hash) = $matches;
return true;
}
else
{
return false;
}
});
if($algo === "SHA-256")
{
$hash_calculated = strtoupper(hash("sha256", file_get_contents($filename)));
if($hash !== $hash_calculated)
{
die("hash mismatch");
}
}
}
//TODO handle 409 ...
return $status;
}
public function get_file ($filename)
{
list($status, $headers, $body) = $this->query("get", "/rest/$filename", [], [], []);
//TODO handle 404 ...
if($status == 200)
{
return $body;
}
else
{
return false;
}
}
public function delete_file ($filename)
{
list($status, $headers, $body) = $this->query("delete", "/rest/$filename", [], [], []);
//TODO handle 403 404 ...
return $status;
}
public function head_file ($filename)
{
list($status, $headers, $body) = $this->query("head", "/rest/$filename", [], [], []);
if($status == 200)
{
return $headers;
}
elseif($status == 404)
{
return false;
}
}
public function list_files ($directory)
{
list($status, $headers, $body) = $this->query("get", "/rest/$directory", [], [/*"type" => "directory"*/], []);
//TODO handle 404 ...
if($status == 200)
{
// echo $body . PHP_EOL;
$res = [];
$xml = \simplexml_load_string($body);
foreach ($xml->entry as $entry)
{
$elem = ((array)$entry->attributes())['@attributes'];
$res [$elem["urlName"]] = $elem;
}
return $res;
}
else
{
return false;
}
}
}