<?php
namespace O2k\Diadoc\Integration;
class Api
{
private const API_KEY = '';
private const SERVICE_URL = 'https://diadoc-api.kontur.ru';
private const RESOURCE_AUTHENTICATE = '/V3/Authenticate';
private const RESOURCE_GET_DOCFLOWS_EVENTS_V2 = '/V2/GetDocflowEvents';
private const RESOURCE_GET_DOCFLOWS_EVENTS_V3 = '/V3/GetDocflowEvents';
private const RESOURCE_GET_DOCUMENT = '/V3/GetDocument';
private const RESOURCE_GET_DOCUMENT_TYPES = '/V2/GetDocumentTypes';
private const RESOURCE_GET_BOX = '/GetBox';
private const RESOURCE_PARSE_TITLE_XML = '/ParseTitleXml';
private const RESOURCE_GET_ENTITY_CONTENT = '/V4/GetEntityContent';
private const RESOURCE_GENERATE_PRINT_FORM_FROM_ATTACHMENT = '/GeneratePrintFormFromAttachment';
private const RESOURCE_GET_GENERATED_PRINT_FORM = '/GetGeneratedPrintForm';
private const RESOURCE_GET_MESSAGE = '/V5/GetMessage';
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
private $token;
public function getDocflowEventsV3(
$boxId,
$startTimestamp,
$endTimestamp,
$sortDirection = 1,
$afterIndexKey = null,
$populateDocuments = false,
$injectEntityContent = false,
$populatePreviousDocumentStates = false
)
{
if (!$boxId) {
return false;
}
$uriParameters = [
'boxId' => $boxId,
];
$startProtoTimestamp = new \Diadoc\Api\Proto\Timestamp(['Ticks' => \O2k\DateTime\Helper::convertTimestampToTicks($startTimestamp)]);
$endProtoTimestamp = new \Diadoc\Api\Proto\Timestamp(['Ticks' => \O2k\DateTime\Helper::convertTimestampToTicks($endTimestamp)]);
$protoFilter = new \Diadoc\Api\Proto\TimeBasedFilter();
$protoFilter->setFromTimestamp($startProtoTimestamp);
$protoFilter->setToTimestamp($endProtoTimestamp);
$protoFilter->setSortDirection($sortDirection);
$getDocflowEventsRequest = new \Diadoc\Api\Proto\Docflow\GetDocflowEventsRequest([
'Filter' => $protoFilter,
'AfterIndexKey' => $afterIndexKey,
'PopulateDocuments' => $populateDocuments,
'InjectEntityContent' => $injectEntityContent,
'PopulatePreviousDocumentStates' => $populatePreviousDocumentStates
]);
$serializedProtoData = $getDocflowEventsRequest->serializeToString();
$response = $this->doRequest(
self::RESOURCE_GET_DOCFLOWS_EVENTS_V3,
[
'boxId' => $boxId,
],
'POST',
$serializedProtoData
);
$docflowEvents = new \Diadoc\Api\Proto\Docflow\GetDocflowEventsResponseV3;
$docflowEvents->mergeFromString($response);
return $docflowEvents;
}
public function getDocflowEventsV2(
$boxId,
$startTimestamp,
$endTimestamp,
$sortDirection = 1,
$afterIndexKey = null,
$populateDocuments = false,
$injectEntityContent = false,
$populatePreviousDocumentStates = false
)
{
if (!$boxId) {
return false;
}
$uriParameters = [
'boxId' => $boxId,
];
$startProtoTimestamp = new \Diadoc\Api\Proto\Timestamp(['Ticks' => \O2k\DateTime\Helper::convertTimestampToTicks($startTimestamp)]);
$endProtoTimestamp = new \Diadoc\Api\Proto\Timestamp(['Ticks' => \O2k\DateTime\Helper::convertTimestampToTicks($endTimestamp)]);
$protoFilter = new \Diadoc\Api\Proto\TimeBasedFilter();
$protoFilter->setFromTimestamp($startProtoTimestamp);
$protoFilter->setToTimestamp($endProtoTimestamp);
$protoFilter->setSortDirection($sortDirection);
$getDocflowEventsRequest = new \Diadoc\Api\Proto\Docflow\GetDocflowEventsRequest([
'Filter' => $protoFilter,
'AfterIndexKey' => $afterIndexKey,
'PopulateDocuments' => $populateDocuments,
'InjectEntityContent' => $injectEntityContent,
'PopulatePreviousDocumentStates' => $populatePreviousDocumentStates
]);
$serializedProtoData = $getDocflowEventsRequest->serializeToString();
$response = $this->doRequest(
self::RESOURCE_GET_DOCFLOWS_EVENTS_V2,
[
'boxId' => $boxId,
],
'POST',
$serializedProtoData
);
$docflowEvents = new \Diadoc\Api\Proto\Docflow\GetDocflowEventsResponse;
$docflowEvents->mergeFromString($response);
return $docflowEvents;
}
public function getMessage($boxId, $messageId, $entityId, $originalSignature = false, $injectEntityContent = false)
{
$requestData = [
'boxId' => $boxId,
'messageId' => $messageId,
'entityId' => $entityId
];
if ($originalSignature) {
$requestData['originalSignature'] = $originalSignature;
}
if ($injectEntityContent) {
$requestData['injectEntityContent'] = $injectEntityContent;
}
$response = $this->doRequest(
self::RESOURCE_GET_MESSAGE,
$requestData
);
$message = new \Diadoc\Api\Proto\Events\Message;
$message->mergeFromString($response);
return $message;
}
public function getEntityContent($boxId, $messageId, $entityId)
{
$response = $this->doRequest(
self::RESOURCE_GET_ENTITY_CONTENT,
[
'boxId' => $boxId,
'messageId' => $messageId,
'entityId' => $entityId
]
);
return $response;
}
public function generatePrintFormFromAttachment($documentType, $fromBoxId, $documentContent)
{
$response = $this->doRequest(
self::RESOURCE_GENERATE_PRINT_FORM_FROM_ATTACHMENT,
[
'documentType' => $documentType,
'fromBoxId' => $fromBoxId
],
'POST',
$documentContent
);
return $response;
}
public function getGeneratedPrintForm($printFormId)
{
$response = $this->doRequest(
self::RESOURCE_GET_GENERATED_PRINT_FORM,
[
'printFormId' => $printFormId,
]
);
return $response;
}
public function parseTitleXml($boxId, $documentTypeNamedId, $documentFunction, $documentVersion, $titleIndex, $xmlFileContent)
{
$response = $this->doRequest(
self::RESOURCE_PARSE_TITLE_XML,
[
'boxId' => $boxId,
'documentTypeNamedId' => $documentTypeNamedId,
'documentFunction' => $documentFunction,
'documentVersion' => $documentVersion,
'titleIndex' => $titleIndex
],
'POST',
$xmlFileContent
);
return $response;
}
public function getDocumentTypes($boxId)
{
$response = $this->doRequest(
self::RESOURCE_GET_DOCUMENT_TYPES,
[
'boxId' => $boxId,
]
);
$documentTypes = new \Diadoc\Api\Proto\Documents\Types\GetDocumentTypesResponseV2();
$documentTypes->mergeFromString($response);
return $documentTypes;
}
public function getBox($boxId)
{
$response = $this->doRequest(
self::RESOURCE_GET_BOX,
[
'boxId' => $boxId
]
);
$box = new \Diadoc\Api\Proto\Box();
$box->mergeFromString($response);
return $box;
}
public function authenticateByPassword($login, $password)
{
$uriParameters = [
'type' => 'password',
];
$protoData = new \Diadoc\Api\Proto\LoginPassword(['Login' => $login, 'Password' => $password]);
$serializedProtoData = $protoData->serializeToString();
$response = $this->doRequest(
self::RESOURCE_AUTHENTICATE,
$uriParameters,
'POST',
$serializedProtoData
);
$this->setToken($response);
return $response;
}
protected function getUri($action, $params = [])
{
$uri = self::SERVICE_URL.$action;
if ($params) {
$uri .= '?'.http_build_query($params);
}
return $uri;
}
protected function doRequest($resource, $params = [], $method = self::METHOD_GET, $data = array())
{
$uri = sprintf(
'%s%s?%s',
self::SERVICE_URL,
$resource,
http_build_query($params)
);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 180);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->buildRequestHeaders());
if ($method == self::METHOD_POST) {
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data);
}
elseif ($method == self::METHOD_GET) {
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
}
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
protected function getToken()
{
return $this->token;
}
public function setToken($token)
{
$this->token = $token;
}
protected function buildRequestHeaders()
{
$header = 'DiadocAuth ddauth_api_client_id='.self::API_KEY;
if ($token = $this->getToken()) {
$header .= ', ddauth_token='.$token;
}
return ['Authorization: ' . $header];
}
}