File: /home/imensosw/public_html/mpl.imenso.co/app/Services/Vuforia.php
<?php
namespace App\Services;
use Exception;
use GuzzleHttp\Client as GuzzleClient;
class Vuforia
{
protected $accessKey;
protected $secretKey;
protected $targetName;
protected $reqMethod;
protected $endPoint;
protected $reqHeaders;
protected $reqBody;
protected $hexDigest;
protected $contentType;
protected $marker;
protected $base_url;
protected $date;
public function __construct()
{
$this->accessKey = config('services.vuforia.access_key');
$this->secretKey = config('services.vuforia.secret_key');
$this->base_url = 'https://vws.vuforia.com';
}
public function createSignature()
{
$method = $this->reqMethod;
// The HTTP Header fields are used to authenticate the request
$requestHeaders = $this->reqHeaders;
$dateValue = $requestHeaders['Date'];
$requestPath = $this->endPoint;
// Not all requests will define a Content-Type
if (isset($requestHeaders['Content-Type'])) {
$this->contentType = $requestHeaders['Content-Type'];
}
if ($method == 'GET' || $method == 'DELETE') {
// Do nothing because the strings are already set correctly
$this->hexDigest = 'd41d8cd98f00b204e9800998ecf8427e';
} else {
if ($method == 'POST' || $method == 'PUT') {
// If this is a POST or PUT the request should have a request body
$this->hexDigest = md5(json_encode($this->reqBody), false);
} else {
echo 'ERROR: Invalid content type passed to Sig Builder';
}
}
$toDigest = $method."\n".$this->hexDigest."\n".$this->contentType."\n".$dateValue."\n".$requestPath;
$shaHashed = '';
try {
// the SHA1 hash needs to be transformed from hexidecimal to Base64
$shaHashed = $this->hexToBase64(hash_hmac('sha1', $toDigest, $this->secretKey));
} catch (Exception $e) {
$e->getMessage();
}
return $shaHashed;
}
private function hexToBase64($hex)
{
$return = '';
foreach (str_split($hex, 2) as $pair) {
$return .= chr(hexdec($pair));
}
return base64_encode($return);
}
private function getImageAsBase64($marker)
{
$file = file_get_contents($marker);
if ($file) {
$file = base64_encode($file);
}
return $file;
}
private function makeRequest()
{
try {
$client = new GuzzleClient([
'base_uri' => $this->base_url,
]);
$request = [
'headers' => $this->reqHeaders,
'verify' => false,
];
$request['json'] = $this->reqBody;
$res = $client->request($this->reqMethod, $this->endPoint, $request);
$response = (string) $res->getBody();
return $response;
} catch (Guzzle\Http\Exception\ClientErrorResponseException $e) {
$req = $e->getRequest();
$resp = $e->getResponse();
if (json_decode($resp->getBody(true))) {
$response = $resp->getBody(true);
} else {
$response = json_encode([
'result_code' => $resp->getReasonPhrase(),
'request' => json_decode((string) $req->getBody(true)),
]);
}
return $response;
} catch (Guzzle\Http\Exception\ServerErrorResponseException $e) {
$req = $e->getRequest();
$resp = $e->getResponse();
if (json_decode($resp->getBody(true))) {
$response = $resp->getBody(true);
} else {
$response = json_encode([
'result_code' => $resp->getReasonPhrase(),
'request' => json_decode((string) $req->getBody(true)),
]);
}
return $response;
} catch (Guzzle\Http\Exception\BadResponseException $e) {
$req = $e->getRequest();
$resp = $e->getResponse();
if (json_decode($resp->getBody(true))) {
$response = $resp->getBody(true);
} else {
$response = json_encode([
'result_code' => $resp->getReasonPhrase(),
'request' => json_decode((string) $req->getBody(true)),
]);
}
return $response;
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
$req = $e->getRequest();
$resp = $e->getResponse();
if (json_decode($resp->getBody(true))) {
$response = $resp->getBody(true);
} else {
$response = json_encode([
'result_code' => $resp->getReasonPhrase(),
'request' => json_decode((string) $req->getBody(true)),
]);
}
return $response;
} catch (\Exception $e) {
$req = $e->getRequest();
$resp = $e->getResponse();
if (json_decode($resp->getBody(true))) {
$response = $resp->getBody(true);
} else {
$response = json_encode([
'result_code' => $resp->getReasonPhrase(),
'request' => json_decode((string) $req->getBody(true)),
]);
}
return $response;
}
}
public function sendMarker($marker, $meta = [], $active_flag = 0)
{
$markerPath = pathinfo($marker);
$this->targetName = $markerPath['filename'];
$this->reqMethod = 'POST';
$metaData = json_encode($meta, JSON_UNESCAPED_SLASHES);
$this->reqBody = [
'width' => 0.2,
'name' => $this->targetName,
'image' => $this->getImageAsBase64($marker),
'application_metadata' => base64_encode($metaData),
'active_flag' => $active_flag,
];
$this->date = new \DateTime('now', new \DateTimeZone('GMT'));
$this->reqHeaders = [
'Content-Type' => 'application/json',
'Date' => $this->date->format('D, d M Y H:i:s').' GMT',
];
$this->endPoint = '/targets';
$signature = $this->createSignature();
$this->reqHeaders['Authorization'] = 'VWS '.$this->accessKey.':'.$signature;
return $this->makeRequest();
}
public function updateMarker($targetId, $meta = [], $active_flag = 1)
{
$this->reqMethod = 'PUT';
$metaData = json_encode($meta, JSON_UNESCAPED_SLASHES);
$this->reqBody = [
'application_metadata' => base64_encode($metaData),
'active_flag' => $active_flag,
];
$this->date = new \DateTime('now', new \DateTimeZone('GMT'));
$this->reqHeaders = [
'Content-Type' => 'application/json',
'Date' => $this->date->format('D, d M Y H:i:s').' GMT',
];
$this->endPoint = '/targets/'.$targetId;
$signature = $this->createSignature();
$this->reqHeaders['Authorization'] = 'VWS '.$this->accessKey.':'.$signature;
return $this->makeRequest();
}
public function getMarker($targetId)
{
$this->reqMethod = 'GET';
$this->date = new \DateTime('now', new \DateTimeZone('GMT'));
$this->reqHeaders = [
'Content-Type' => 'application/json',
'Date' => $this->date->format('D, d M Y H:i:s').' GMT',
];
$this->endPoint = '/targets/'.$targetId;
$signature = $this->createSignature();
$this->reqHeaders['Authorization'] = 'VWS '.$this->accessKey.':'.$signature;
$this->reqBody = null;
return $this->makeRequest();
}
public function removeMarker($targetId)
{
$this->reqMethod = 'DELETE';
$this->date = new \DateTime('now', new \DateTimeZone('GMT'));
$this->reqHeaders = [
'Content-Type' => 'application/json',
'Date' => $this->date->format('D, d M Y H:i:s').' GMT',
];
$this->endPoint = '/targets/'.$targetId;
$signature = $this->createSignature();
$this->reqHeaders['Authorization'] = 'VWS '.$this->accessKey.':'.$signature;
$this->reqBody = null;
return $this->makeRequest();
}
public function listMarkers()
{
$this->reqMethod = 'GET';
$this->date = new \DateTime('now', new \DateTimeZone('GMT'));
$this->reqHeaders = [
'Content-Type' => 'application/json',
'Date' => $this->date->format('D, d M Y H:i:s').' GMT',
];
$this->endPoint = '/targets';
$signature = $this->createSignature();
$this->reqHeaders['Authorization'] = 'VWS '.$this->accessKey.':'.$signature;
$this->reqBody = null;
return $this->makeRequest();
}
public function getMarkerSummaryReport($targetId)
{
$this->reqMethod = 'GET';
$this->date = new \DateTime('now', new \DateTimeZone('GMT'));
$this->reqHeaders = [
'Content-Type' => 'application/json',
'Date' => $this->date->format('D, d M Y H:i:s').' GMT',
];
$this->endPoint = '/summary/'.$targetId;
$signature = $this->createSignature();
$this->reqHeaders['Authorization'] = 'VWS '.$this->accessKey.':'.$signature;
$this->reqBody = null;
return $this->makeRequest();
}
public function checkDuplicates($targetId)
{
$this->reqMethod = 'GET';
$this->date = new \DateTime('now', new \DateTimeZone('GMT'));
$this->reqHeaders = [
'Content-Type' => 'application/json',
'Date' => $this->date->format('D, d M Y H:i:s').' GMT',
];
$this->endPoint = '/duplicates/'.$targetId;
$signature = $this->createSignature();
$this->reqHeaders['Authorization'] = 'VWS '.$this->accessKey.':'.$signature;
$this->reqBody = null;
return $this->makeRequest();
}
}