-
Notifications
You must be signed in to change notification settings - Fork 9
/
DataBoxSimpleApi.php
390 lines (330 loc) · 10.4 KB
/
DataBoxSimpleApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
namespace Defr\CzechDataBox;
use DateInterval;
use DateTime;
use Defr\CzechDataBox\Api\tDbOwnerInfo;
use Defr\CzechDataBox\Api\tDbOwnersArray;
use Defr\CzechDataBox\Api\tDbUserInfo;
use Defr\CzechDataBox\Api\tDummyInput;
use Defr\CzechDataBox\Api\tFilesArray;
use Defr\CzechDataBox\Api\tFindDBInput;
use Defr\CzechDataBox\Api\tIDMessInput;
use Defr\CzechDataBox\Api\tListOfFReceivedInput;
use Defr\CzechDataBox\Api\tListOfSentInput;
use Defr\CzechDataBox\Api\tMessageCreateInput;
use Defr\CzechDataBox\Api\tMessageCreateOutput;
use Defr\CzechDataBox\Api\tMessageEnvelopeSub;
use Defr\CzechDataBox\Api\tNumOfMessagesInput;
use Defr\CzechDataBox\Api\tRecord;
use Defr\CzechDataBox\ApiExtensions\dmFile;
use function array_filter;
use function array_map;
use function basename;
use function file_get_contents;
use function is_array;
use function is_string;
use function md5;
use function mime_content_type;
use function strtoupper;
use function substr;
/**
* Class DataBoxSimpleApi
*/
class DataBoxSimpleApi
{
public function __construct(private DataBox $dataBox)
{
}
/**
* Vrátí informace o aktuální datové schránce.
*
* @return tDbOwnerInfo
*/
public function getDataBoxInfo()
{
return $this->dataBox->DataBoxAccess()->GetOwnerInfoFromLogin(new tDummyInput(null))->getDbOwnerInfo();
}
/**
* Vratí datum expirace hesla.
*
* @return \DateTime
*/
public function getPasswordExpires()
{
return $this->dataBox->DataBoxAccess()->GetPasswordInfo(new tDummyInput(null))->getPswExpDate();
}
/**
* Vrátí informace o aktuálním uživateli.
*
* @return tDbUserInfo
*/
public function getUserInfo()
{
return $this->dataBox->DataBoxAccess()->GetUserInfoFromLogin(new tDummyInput(null))->getDbUserInfo();
}
/**
* Stahne seznam prijatych zprav v DS.
*
* @param int $days
* @param int $limit
*
* @return tRecord[]|null
*
* @throws DataBoxException
*/
public function getListOfReceivedMessages($days = 90, $limit = 1000)
{
if ($days < 0 || $limit < 1) {
throw new DataBoxException();
}
$params = new tListOfFReceivedInput();
$params->setDmToTime(new DateTime())
->setDmFromTime((new DateTime())->sub(new DateInterval('P'.$days.'D')))
->setDmLimit($limit)
->setDmOffset(0)
->setDmRecipientOrgUnitNum(null)
->setDmStatusFilter(-1);
$result = $this->dataBox->DmInfoWebService()->GetListOfReceivedMessages($params)->getDmRecords()->getDmRecord();
return $this->returnAsArray($result);
}
/**
* Vrati zmeny odeslanych DZ.
*
* @param int $days
* @param int $limit
*
* @return tRecord[]|null
*
* @throws DataBoxException
*/
public function getListOfSentMessages($days = 90, $limit = 1000)
{
if ($days < 0 || $limit < 1) {
throw new DataBoxException();
}
$params = new tListOfSentInput();
$params->setDmToTime(new DateTime())
->setDmFromTime((new DateTime())->sub(new DateInterval('P'.$days.'D')))
->setDmLimit($limit)
->setDmOffset(0)
->setDmSenderOrgUnitNum(null)
->setDmStatusFilter(-1);
$result = $this->dataBox->DmInfoWebService()->GetListOfSentMessages($params)->getDmRecords()->getDmRecord();
return $this->returnAsArray($result);
}
/**
* Vyhleda DS na zaklade ID.
*
* @param $dataBoxId
*
* @return tDbOwnersArray
*/
public function findDataBoxById($dataBoxId)
{
$params = new tFindDBInput();
$owner = new tDbOwnerInfo();
$owner->setDbID($dataBoxId);
$params->setDbOwnerInfo($owner);
return $this->dataBox->DataBoxSearch()->FindDataBox($params)->getDbResults();
}
/**
* Stáhne přijatou DZ ve formátu ZFO.
*
* @param $dataMessageId
*
* @return DataBoxMessageFile
*
* @throws DataBoxException
*/
public function downloadSignedReceivedMessage($dataMessageId)
{
$file = $this->getLocationForMessage($dataMessageId, 'r');
if (!$file->getIsExist()) {
$content = $this->dataBox->DmOperationsWebService()->SignedMessageDownload(
(new tIDMessInput())->setDmID($dataMessageId),
)->getDmSignature();
$file->save($content);
}
return $file;
}
/**
* Stáhne odeslanou DZ ve formátu ZFO.
*
* @param $dataMessageId
*
* @return DataBoxMessageFile
*
* @throws DataBoxException
*/
public function downloadSignedSentMessage($dataMessageId)
{
$file = $this->getLocationForMessage($dataMessageId, 's');
if (!$file->getIsExist()) {
$content = $this->dataBox->DmOperationsWebService()->SignedSentMessageDownload(
(new tIDMessInput())->setDmID($dataMessageId),
)->getDmSignature();
$file->save($content);
}
return $file;
}
/**
* Stáhne doručenku odeslané zprávy.
*
* @param $dataMessageId
*
* @return DataBoxMessageFile
*
* @throws DataBoxException
*/
public function downloadDeliveryInfo($dataMessageId)
{
$file = $this->getLocationForMessage($dataMessageId, 'di');
if (!$file->getIsExist()) {
$content = $this->dataBox->DmInfoWebService()->GetSignedDeliveryInfo(
(new tIDMessInput())->setDmID($dataMessageId),
)->getDmSignature();
$file->save($content);
}
return $file;
}
/**
* Stáhne všechny přílohy datové zprávy.
*
* @param $dataMessageId
*
* @return DataBoxMessageAttachment[]|array
*
* @throws DataBoxException
*/
public function getReceivedDataMessageAttachments($dataMessageId)
{
/** @var dmFile[] $attachments */
$attachments = $this->dataBox->DmOperationsWebService()->MessageDownload(
new tIDMessInput($dataMessageId),
)->getDmReturnedMessage()->getDmDm()->getDmFiles()->getDmFile();
$files = [];
foreach ($attachments as $attachment) {
$file = $this->getLocationForAttachment($dataMessageId, $attachment->getDmFileDescr());
if (!$file->getIsExist()) {
$content = $attachment->getDmEncodedContent();
$file->save($content);
}
$files[] = $file;
}
return $files;
}
/**
* Vytvoří základní zprávu k odeslání
*
* @param string $recipient Recipient's DataBox ID
* @param string $subject Message annotation
* @param array $attachments Paths to files attached to the message
*
* @return tMessageCreateInput
* @throws DataBoxException
*/
public function createBasicDataMessage($recipient, $subject, array $attachments = [])
{
if (!is_string($recipient)) {
throw new DataBoxException("Invalid recipient data type provided");
} elseif (!is_string($subject)) {
throw new DataBoxException("Invalid subject data type provided");
}
// Create an envelope, for some reason the dmEnvelope is not processed
// properly and returns an internal error
$envelope = new tMessageEnvelopeSub(
null,
null,
$recipient,
null,
null,
null,
$subject,
);
// Make sure the attachments exist. If you have files stored in the memory
// just do not pass any attachments and create the dmFile object yourself,
// then set it as an array to dmFiles
$attachments = array_filter($attachments, '\file_exists');
// Creates the dmFile object for each of the attachments, gets mime
// type and file name from the file
$attachments = array_map(
static function ($filePath) {
$file = new dmFile();
$file->setDmMimeType(mime_content_type($filePath));
$file->setDmFileDescr(basename($filePath));
$file->setDmEncodedContent(file_get_contents($filePath));
return $file;
},
$attachments,
);
$dmFiles = new tFilesArray();
if ($attachments !== []) {
// tFilesArray is not properly generated, it should accept an array of dmFile objects
$dmFiles->setDmFile($attachments);
}
return new tMessageCreateInput($envelope, $dmFiles);
}
/**
* Pošle datovou zprávu.
*
*
* @return tMessageCreateOutput
* @throws DataBoxException
*/
public function sendDataMessage(tMessageCreateInput $messageToSent)
{
return $this->dataBox->DmOperationsWebService()->CreateMessage($messageToSent);
}
/**
* Počet zpráv odeslaných v celém systému ISDS.
*
* @return int
*/
public function getStats()
{
$input = new tNumOfMessagesInput();
return $this->dataBox->IsdsStat()->NumOfMessages($input)->getStatResult();
}
/**
* @param $input
*
* @return array
*/
private function returnAsArray($input)
{
if (is_array($input)) {
return $input;
} else {
return [$input];
}
}
/**
* @param $messageId
* @param null $suffix
*
* @return DataBoxMessageFile
*/
private function getLocationForMessage($messageId, $suffix = null)
{
if ($suffix !== null && substr($suffix, 1, 1) != '_') {
$suffix = '_'.$suffix;
}
$hash = md5($messageId);
$subFolder = substr($hash, 0, 2).'/'.substr($hash, 2, 2).'/'.substr($hash, 4, 2);
$location = $this->dataBox->getDirectory().'/'.$subFolder.'/'.$messageId.strtoupper($suffix).'.zfo';
return new DataBoxMessageFile($location);
}
/**
* @param $messageId
* @param $fileName
*
* @return DataBoxMessageAttachment
*/
private function getLocationForAttachment($messageId, $fileName)
{
$hash = md5($messageId);
$subFolder = substr($hash, 0, 2).'/'.substr($hash, 2, 2).'/'.substr($hash, 4, 2);
$location = $this->dataBox->getDirectory().'/'.$subFolder.'/'.$messageId.'/'.$fileName;
return new DataBoxMessageAttachment($location);
}
}