-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdk.php
1113 lines (992 loc) · 40.3 KB
/
sdk.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Docxpresso SERVER SDK
*
* @copyright Copyright (c) 2017 No-nonsense Labs (http://www.nononsenselabs.com)
* @license MIT
* @link https://opensource.org/licenses/MIT
* @version 1.0
* @since 1.0
*/
namespace SDK_Docxpresso;
/**
* A collection of methods that simplify the data exchange and communication
* with the Docxpresso SERVER package
*/
class Utils
{
/**
* Construct
*
* @param array $options with the following keys and values
* 'pKey' => (string) the private key of your Docxpresso SERVER
* installation
* 'docxpressoInstallation' => (string) the URL of your Docxpresso
* SERVER installation
*
* @access public
*/
public function __construct($options = array())
{
$this->_options = $options;
}
/**
* Checks the validity of an API key
*
* @param string $key the key you wish to validate
* @param string $data the string that was used to generate the key
* @param string $pKey the private key used to generate the hash
* @return boolean
* @access public
*/
public function apikey_control($key, $data, $pKey)
{
$resultbin = self::sha1_hmac($pKey , $data);
$result = bin2hex($resultbin);
if ($key == $result) {
return true;
} else {
return false;
}
}
/**
* Encodes in base64 url safe
*
* @param string $str
* @return string
* @access public
*/
public function base64_encode_url_safe($str)
{
return strtr(base64_encode($str), '+/=', '-_,');
}
/**
* Decodes base64 url safe
*
* @param string $str
* @return string
* @access public
*/
public function base64_decode_url_safe($str)
{
return base64_decode(strtr($str, '-_,', '+/='));
}
/**
* Generates a one time link to preview a document in the associated
* Docxpresso SERVER interface
*
* @param array $data with the following keys and values
* 'template' => (int) the id of the requested document template.
* This value is compulsory and must correspond to a valid template
* id.
* 'token' => (string) a unique identifier of a previous use. If given
* the requestDataURI option will be ignored and the data associated
* with the token will be preloaded into the document.
* 'identifier' => (string) optional var name that we may pass to help
* identify that particular usage. Default value is an empty string
* 'reference' => (string) an optional string we may pass to help
* identify that particular usage. Default value is an empty string
* 'custom' => (string) an optional string we may pass to add external
* additional info to the template
* 'form' => (boolean) if true Docxpresso will serve a web form rather
* than an interactive document. Default value is false.
* 'format' => (string) the requested document output format.
* Default value is "odt"· (Open Document Text).
* 'enduserid' => (string) a value that will help us later to identify
* the user that requested the document. Default value is an empty
* string.
* 'requestConfigURI' => (string) the URL where Docxpresso should fetch
* external configuration adjustments.
* 'requestDataURI' => (string) the URL where Docxpresso should fetch
* external data. Default value is an empty string.
* 'responseExternalCSS' => (string) the URL where Docxpresso should
* fetch for some external CSS file.
* 'responseExternalJS' => (string) the URL where Docxpresso should
* fetch for some external JS file.
* 'responseDataURI' => (string) the URL where Docxpresso should
* forward the user data. Default value is an empty string.
* 'responseURL' => (string) the URL where Docxpresso should redirect
* the end user after saving the data. Default value is an empty
* string.
* 'documentName' => (string) the name we want to give to the generated
* document. Default value is an empty and in that case Docxpresso
* will use the default template name.
* string.
* 'domain' => (string) the URL doing the request. Default value is an
* empty string.
* 'prefix' => (string) a prefix that will limit enduser edition to
* only the field variables that start by that prefix. You can use
* a comma separated list to select mor than one prefix. Default value
* is an empty string.
* 'editableVars' => (string) a comma separated list of var names
* to restrict the edition to that set. Default value is an empty
* string.
* 'enforceValidation' => (boolean) if true the user will not be able
* to send data until all variable fields are validated. Default value
* is false.
* @return string
* @access public
*/
public function previewDocument($data)
{
if(!empty($data['form'])){
$url = $this->_options['docxpressoInstallation'] . '/documents/previewForm/' . $data['template'];
}else {
$url = $this->_options['docxpressoInstallation'] . '/documents/preview/' . $data['template'];
}
$options = new \stdClass();
if (isset($data['token'])) {
$options->token = $data['token'];
}
if (isset($data['format'])) {
$options->format = $data['format'];
} else {
$options->format = 'odt';
}
if (isset($data['enduserid'])) {
$options->enduserid = $data['enduserid'];
}
if (isset($data['identifier'])) {
$options->identifier = $data['identifier'];
}
if (isset($data['reference'])) {
$options->reference = $data['reference'];
}
if (isset($data['custom'])) {
$options->custom = $data['custom'];
}
if (!empty($data['requestConfigURI'])) {
$dURI = new \stdClass();
$dURI->URL = $data['requestConfigURI'];
$options->requestDataURI = json_encode($dURI);
}
if (!empty($data['requestDataURI'])) {
$dURI = new \stdClass();
$dURI->URL = $data['requestDataURI'];
$dURI->requestData = 'preview';
$options->requestDataURI = json_encode($dURI);
}
if (isset($data['requestExternalJS'])) {
$options->requestExternalJS = $data['requestExternalJS'];
}
if (isset($data['requestExternalCSS'])) {
$options->requestExternalCSS = $data['requestExternalCSS'];
}
if (!empty($data['responseDataURI'])) {
$options->responseDataURI = $data['responseDataURI'];
}
if (!empty($data['responseURL'])) {
$options->responseURL = $data['responseURL'];
}
if (!empty($data['documentName'])) {
$options->documentName = $data['documentName'];
}
if (!empty($data['domain'])) {
$options->domain = $data['domain'];
}
if (!empty($data['prefix'])) {
$options->prefix = $data['prefix'];
}
if (!empty($data['editableVars'])) {
$options->editableVars = $data['editableVars'];
}
if (!empty($data['enforceValidation'])) {
$options->enforceValidation = true;
}
$opt = $this->base64_encode_url_safe(json_encode($options));
return $this->_returnLink($url, $data['template'], $opt);
}
/**
* Generates a one time link to generate a document in the associated
* Docxpresso SERVER interface
*
* @param array $data with the following keys and values
* 'template' => (int) the id of the requested document template.
* This value is compulsory and must correspond to a valid template
* id.
* 'requestDataURI' => (string) the URL where Docxpresso should fetch
* external data. Default value is an empty string.
* 'documentName' => (string) the name we want to give to the generated
* document (it should include the extensions: .odt, .pdf, .doc,
* .doc(legacy), .docx or .rtf). The default values is document.odt
* 'varData' => the JSON data we would like to use to generate the
* document. This will only be used if the RequestDataURI parameter is
* empty
* 'display' => (string) it can be 'document' (default) or 'form'.
* This is only used for the generation of continue links
* 'response' => (string) it can be 'download'(default) if the document
* is to be directly downloadable from the browser or json if we want
* to get the document as base64 encoded together with the usage id
* and token
* 'callback' => it only spplies to json responses and sets the name
* of the callback function for JSONP responses.
* @return string
* @access public
*/
public function requestDocument($data)
{
$url = $this->_options['docxpressoInstallation'] . '/documents/requestDocument/' . $data['template'];
$options = new \stdClass();
if (isset($data['documentName'])) {
$options->name = $data['documentName'];
} else {
$options->name = 'document.odt';
}
if (isset($data['varData'])) {
$options->data = $data['varData'];
} else {
$options->data = '{}';
}
if (isset($data['display'])) {
$options->display = $data['display'];
} else {
$options->display = 'document';
}
if (!empty($data['requestDataURI'])) {
$dURI = new \stdClass();
$dURI->URL = $data['requestDataURI'];
$options->requestDataURI = json_encode($dURI);
}
$opt = $this->base64_encode_url_safe(json_encode($options));
return $this->_returnLink($url, $data['template'], $opt);
}
/**
* Generates a one time link to download an attachment from the associated
* Docxpresso SERVER installation
*
* @param array $data $data with the following keys and values
* 'usageId' => (int) the id of the corresponding usage.
* This value is compulsory and must correspond to a valid template
* id.
* 'name' => (string) the name of the attachment file we want to
* download. It should correspond to the name given in the Docxpresso
* SERVER processing interface.
* 'token' => (string) the token of the given usage for further
* security.
* @return string
* @access public
*/
public function downloadAttachment($data)
{
$url = $this->_options['docxpressoInstallation']. '/documents/getAttachment/' . $data['usageId'];
$uniqid = uniqid() . rand(99999, 9999999);
$timestamp = time();
$options = new \stdClass();
$options->name = $data['name'];
$options->token = $data['token'];
$opt = $this->base64_encode_url_safe(json_encode($options));
return $this->_returnLink($url, $data['usageId'], $opt);
}
/**
* Generates a one time link to download a "full· document package" in zip
* format (document + attachments) from the associated
* Docxpresso SERVER installation
*
* @param array $data $data with the following keys and values
* 'id' => (int) the id of the corresponding template.
* This value is compulsory and must correspond to a valid template
* id.
* 'token' => (string) the token of the requested usage.
* @return string
* @access public
*/
public function downloadDocument($data)
{
$url = $this->_options['docxpressoInstallation']. '/documents/getFullDocumentation/' . $data['id'];
$options = new \stdClass();
$options->token = $data['token'];
$opt = $this->base64_encode_url_safe(json_encode($options));
return $this->_returnLink($url, $data['id'], $opt);
}
/**
* Generates a one time link to get all annex document data: thumbnail,
* base64 encoded template odt file, etcetera.
*
* @param integer $token the token of the requested annex
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @return string
* @access public
*/
public function getAnnexData($token, $callback = '')
{
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/get_annex/' . $token;
if (!empty($callback)) {
$url .= '/' . $callback;
}
return $this->_returnLink($url);
}
/**
* Generates a one time link to get all document template data: Docxpresso
* data, thumbnail, base64 encoded template odt file, etcetera.
*
* @param integer $id the id of the required template
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @return string
* @access public
*/
public function getTemplateData($id, $callback = '')
{
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/get_template/' . $id;
if (!empty($callback)) {
$url .= '/' . $callback;
}
return $this->_returnLink($url, $id);
}
/**
* Generates a one time link to get just a template thumbnail
*
* @param integer $id the id of the required template
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @return string
* @access public
*/
public function getTemplateThumbnail($id, $callback = '')
{
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/get_thumbnail/' . $id;
if (!empty($callback)) {
$url .= '/' . $callback;
}
return $this->_returnLink($url, $id);
}
/**
* Get usage history by year/month
*
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @return string
* @access public
*/
public function getUsageHistory($callback = '')
{
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/get_usage_history';
if (!empty($callback)) {
$url .= '/' . $callback;
}
return $this->_returnLink($url);
}
/**
* Allows to remotely authenticate from any other application into the
* associated Docxpresso SERVER installation
*
* @param array $data $data with the following keys and values
* 'email' => (string) the email of the user we want to log in.
* This value is compulsory and must correspond to a valid registered
* user email.
* 'url' => (string) target url where the user should be redirected
* after being authenticated
* @return string
* @access public
*/
public function accessByTokenAction($data)
{
$url = $this->_options['docxpressoInstallation']. '/users/accessByToken';
$options = new \stdClass();
$options->email = $data['email'];
$options->url = $data['url'];
$opt = $this->base64_encode_url_safe(json_encode($options));
return $this->_returnLink($url, NULL, $opt);
}
/**
* Returns a link to list of categories in JSON(P) format from the associated
* Docxpresso SERVER installation
*
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @return string
* @access public
*/
public function listCategories($callback = '')
{
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/categories';
if (!empty($callback)) {
$url .= '/' . $callback;
}
return $this->_returnLink($url);
}
/**
* Returns a link to list of documents in a given category in JSON(P) format
* from the associated Docxpresso SERVER installation
*
* @param integer $category the corresponding category id.
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @param boolean $published if true only "published" templates will be
* available through the request.
* @param string $access it can be "all" (deafult value), "public" for only
* documents declared like public or a "username" to filter by permissions
* @return string
* @access public
*/
public function documentsByCategory($category, $callback = '', $published = 0, $access = 'all')
{
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/documents_by_category/' . $category;
if (!empty($callback)) {
$url .= '/' . $callback;
} else {
$url .= '/NULL';
}
if (!empty($published)) {
$url .= '/1';
} else {
$url .= '/0';
}
if (!empty($access)) {
$url .= '/' . rawurlencode($access);
}
return $this->_returnLink($url);
}
/**
* Allows to change the password associated with a user email.
*
* @param string $email user unique email identifier.
* @param string $password new password. It should be, at least 8 chars long
* and contain at least an uppercase letter, a lowercase letter, a number
* and a non-standard char: !,%,&,@,#,$,^,*,?,_,~
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @return string
* @access public
*/
public function modifyPassword($email, $password, $callback = '')
{
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/modify_password';
if (!empty($callback)) {
$url .= '/' . $callback;
}
$options = new \stdClass();
$options->email = $email;
$options->password = $password;
$opt = $this->base64_encode_url_safe(json_encode($options));
return $this->_returnLink($url, NULL, $opt);
}
/**
* Allows to modify the configuration of Signature Providers. If the
* requested signature provider does not exist and it belongs to one of
* the current available ones the corresponding entry will be cerated.
*
* @param string $provider the name of the signature provider. Currently
* the only available one is vidSigner
* @param string $config base64 encoded config JSON.
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @return string
* @access public
*/
public function modifySignatureProvider($provider, $config, $callback = '')
{
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/modify_signature_providers/' . $provider;
if (!empty($callback)) {
$url .= '/' . $callback;
}
$options = new \stdClass();
$options->custom = $config;
$opt = $this->base64_encode_url_safe(json_encode($options));
return $this->_returnLink($url, NULL, $opt);
}
/**
* Allows to delete a current signature provider.
*
* @param string $provider the name of the signature provider. Currently
* the only available one is vidSigner
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @param boolean $published if true only "published" templates will be
* available through the request.
* @return string
* @access public
*/
public function deleteSignatureProvider($provider, $callback = '')
{
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/delete_signature_providers/' . $provider;
return $this->_returnLink($url);
}
/**
* List all Signature Providers
*
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @param boolean $published if true only "published" templates will be
* available through the request.
* @return string
* @access public
*/
public function listSignatureProviders($callback = '')
{
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/list_signature_providers';
if (!empty($callback)) {
$url .= '/' . $callback;
}
return $this->_returnLink($url);
}
/**
* Returns a link to download the whole document (sub)tree in JSON(P) format
* from the associated Docxpresso SERVER installation
*
* @param mixed $rootCategory the corresponding category id from which
* we want to build the document tree. Default value is 'root' that corresponds
* with the "root category"
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @param boolean $published if true only "published" templates will be
* available through the request.
* @param string $access it can be "all" (deafult value), "public" for only
* documents declared like public or a "username" to filter by permissions
* @return string
* @access public
*/
public function documentTree($rootCategory= 'root', $callback = '', $published = 0, $access = 'all')
{
if ($rootCategory == 'root') {
$rootCategory = 1;
}
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/document_tree/' . $rootCategory;
if (!empty($callback)) {
$url .= '/' . $callback;
} else {
$url .= '/NULL';
}
if (!empty($published)) {
$url .= '/1';
} else {
$url .= '/0';
}
if (!empty($access)) {
$url .= '/' . rawurlencode($access);
}
return $this->_returnLink($url);
}
/**
* Returns a link to download all documents with a given name in JSON(P)
* format from the associated Docxpresso SERVER installation
*
* @param string $name the name of the template we are looking for. This
* method launches a "LIKE" SQL query.
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @param boolean $published if true only "published" templates will be
* available through the request.
* @param string $access it can be "all" (deafult value), "public" for only
* documents declared like public or a "username" to filter by permissions
* @return string
* @access public
*/
public function templatesByName($name, $callback = '', $published = 0, $access = "all")
{
$url = $this->_options['docxpressoInstallation'];
$url .= '/RESTservices/predefined/documents_by_name/' . rawurlencode($name);
if (!empty($callback)) {
$url .= '/' . $callback;
} else {
$url .= '/NULL';
}
if (!empty($published)) {
$url .= '/1';
} else {
$url .= '/0';
}
if (!empty($access)) {
$url .= '/' . rawurlencode($access);
}
return $this->_returnLink($url);
}
/**
* Returns a link to download all template usage data in JSON(P)
* format for a given template id from the associated Docxpresso
* SERVER installation
*
* @param array $data with the following keys and values
* 'id' => (int) the id of the template.
* This value is compulsory and must correspond to a valid template
* id.
* 'identifier' => (string) the identifier field of an usage. The
* default value is an empty string
* 'reference' => (string) the reference field of an usage. The
* default value is an empty string
* 'enduserid' => (string) the end user id of a particular usage.
* Default value is an empty string.
* 'startDate' => (string) a date in the format yyyy-mm-dd that will
* select usages that happened after it. Default value is an empty
* string.
* 'endDate' => (string) a date in the format yyyy-mm-dd that will
* select usages that happened before it. Default value is an empty
* string.
* 'locked' => (integer) it can be zero for all usages (default), 1 if
* we only want usages that have been set as completed or 2 for the
* opposite.
* 'firstResult' => (int) query offset. Default value is 0;
* 'maxResults' => (int) maximum number of results. Beware that
* each installation may have upper limits to this number.
* Default value is an empty and in that case Docxpresso
* 'sort' => (string) teh field used to sort the results.
* 'order' => (string) possible values are DESC (default) or ASC.
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @return string
* @access public
*/
public function dataByTemplate($data, $callback = '')
{
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/data_by_template/' . $data['id'];
if (!empty($callback)) {
$url .= '/' . $callback;
}
//we build and options object with the search filters
$options = new \stdClass();
if (!empty($data['identifier'])) {
$options->identifier = $data['identifier'];
}
if (!empty($data['reference'])) {
$options->reference = $data['reference'];
}
if (!empty($data['enduserid'])) {
$options->enduserid = $data['enduserid'];
}
if (!empty($data['locked'])) {
$options->locked = $data['locked'];
}
//dates must be in the format 2016-01-30
if (!empty($data['startDate'])) {
$options->startDate = $data['startDate'];
}
if (!empty($data['endDate'])) {
$options->endDate = $data['endDate'];
}
if (!empty($data['firstResult'])) {
$options->firstResult = $data['firstResult'];
}
if (!empty($data['maxResults'])) {
$options->maxResults = $data['maxResults'];
}
if (!empty($data['sort'])) {
$options->sort = $data['sort'];
}
if (!empty($data->order)) {
$options->order = $data['order'];
}
$opt = $this->base64_encode_url_safe(json_encode($options));
return $this->_returnLink($url, $data['id'], $opt);
}
/**
* Returns a link to download the data of a given single usage JSON(P)
* format from the associated Docxpresso SERVER installation
*
* @param integer $usageId the id of a particular usage
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @return string
* @access public
*/
public function dataByUsage($usageId, $callback = '')
{
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/data_by_usage/' . $usageId;
if (!empty($callback)) {
$url .= '/' . $callback;
}
return $this->_returnLink($url, $usageId);
}
/**
* Returns a link to generate a HTML or CSV file for all the data usage
* for a given template
*
* @param array $data with the following keys and values
* 'id' => (int) the id of the template. This value is compulsory and
* must correspond to a valid template id.
* 'format' => (string) it may be html (default) or csv
* 'identifier' => (string) the identifier field of an usage. The
* default value is an empty string
* 'reference' => (string) the reference field of an usage. The
* default value is an empty string
* 'enduserid' => (string) the end user id of a particular usage.
* Default value is an empty string.
* 'after' => (string) a date in the format yyyy-mm-dd that will
* select usages that happened after it. Default value is an empty
* string.
* 'before' => (string) a date in the format yyyy-mm-dd that will
* select usages that happened before it. Default value is an empty
* string.
* 'firstResult' => (int) query offset. Default value is 0;
* 'maxResults' => (int) maximum number of results. Beware that
* each installation may have upper limits to this number.
* Default value is an empty and in that case Docxpresso
* 'sort' => (string) teh field used to sort the results.
* 'order' => (string) possible values are DESC (default) or ASC.
* @return string
* @access public
*/
public function dataDigestByUsage($data)
{
$url = $this->_options['docxpressoInstallation']. '/data/digest/' . $data['id'] ;
$url = $this->_returnLink($url, $data['id'], NULL) . '&';
//we build the URL with the search filters and output foemat
if (!empty($data['format'])) {
$url .= 'format=' . $data['format'] . '&';
}
if (!empty($data['identifier'])) {
$url .= 'identifier=' . $data['identifier'] . '&';
}
if (!empty($data['reference'])) {
$url .= 'reference=' . $data['reference'] . '&';
}
if (!empty($data['enduserid'])) {
$url .= 'enduserid=' . $data['enduserid'] . '&';
}
//dates like before and after must be in the format 2016-01-30
if (!empty($data['before'])) {
$url .= 'before=' . $data['before'] . '&';
}
if (!empty($data['after'])) {
$url .= 'after=' . $data['after'] . '&';
}
if (!empty($data['domain'])) {
$url .= 'domain=' . $data['domain'] . '&';
}
if (!empty($data['maxResults'])) {
$url .= 'maxResults=' . $data['maxResults'] . '&';
}
if (!empty($data['sort'])) {
$url .= 'sort=' . $data['sort'] . '&';
}
if (!empty($data->order)) {
$url .= 'order=' . $data['order'];
}
return $url;
}
/**
* Returns a link to get a JSON with both the document base64 encoded
* together with the other usage data
*
* @param integer $usageId the id of a particular usage
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @return string
* @access public
*/
public function documentAndDataByUsage($usageId, $callback = '')
{
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/document_and_data_by_usage/' . $usageId;
if (!empty($callback)) {
$url .= '/' . $callback;
}
return $this->_returnLink($url, $usageId);
}
/**
* Returns a link to download basic statistical data like number of uses
* and last usage
*
* @param mixed $id template id. If set to 'all' the data
* for all available templates will be provided
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty or NULL plain JSON will be returned.
* @param boolean $published if true only "published" templates will be
* available through the request.
* @return string
* @access public
*/
public function dataStatistics($id = 'all', $callback = '', $published = 0)
{
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/data_statistics';
if (!empty($id)) {
$url .= '/' . $id;
} else {
$url .= '/all';
}
if (!empty($callback)) {
$url .= '/' . $callback;
} else if (!empty($published)) {
$url .= '/NULL';
}
if (!empty($published)) {
$url .= '/1';
}
return $this->_returnLink($url, $id);
}
/**
* Returns a link to download the total usage count group by day
*
* @param array $data with the following keys and values
* 'id' => (mixed) the id of the template. If set to 'all' the data
* for all available templates will be provided.
* 'after' => (string) a date in the format yyyy-mm-dd that will
* select usages that happened after it. Default value is an empty
* string.
* 'before' => (string) a date in the format yyyy-mm-dd that will
* select usages that happened before it. Default value is an empty
* string.
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty or NULL plain JSON will be returned.
* @return string
* @access public
*/
public function usageCount($data = array(), $callback = '')
{
if (!isset($data['id'])) {
$data['id'] = 'all';
}
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/usage_count/' . $data['id'];
if (!empty($callback)) {
$url .= '/' . $callback;
}
//we build and options object with the search filters
$options = new \stdClass();
if (!empty($data['before'])) {
$options->before = $data['before'];
}
if (!empty($data['after'])) {
$options->after = $data['after'];
}
$opt = $this->base64_encode_url_safe(json_encode($options));
return $this->_returnLink($url, $data['id'], $opt);
}
/**
* Returns a link to list of users in JSON(P) format from the associated
* Docxpresso SERVER installation
*
* @param string $callback the callback name that we want to use for padded
* JSON responses. If empty plain JSON will be returned.
* @return string
* @access public
*/
public function userList($callback = '')
{
$url = $this->_options['docxpressoInstallation']. '/RESTservices/predefined/users';
if (!empty($callback)) {
$url .= '/' . $callback;
}
return $this->_returnLink($url);
}
/**
* Creates the link requested by all other methods
*
* @param string $url
* @param mixed $id
* @param mixed $opt
* @return string
* @access private
*/
private function _returnLink($url, $id = NULL, $opt = NULL)
{
$uniqid = uniqid() . rand(99999, 9999999);
$timestamp = time();
$control = '';
if (!empty($id)){
$control .= $id . '-';
}
$control .= $timestamp . '-' . $uniqid;
if (!empty($opt)){
$control .= '-' . $opt;
}
$dataKey = sha1($control, true);
$masterKey = $this->_options['pKey'];
$APIKEY = bin2hex($this->sha1_hmac($masterKey, $dataKey));
//we should now redirect to Docxpresso
$addr = $url . '?';
$addr .= 'uniqid=' . $uniqid .'&';
$addr .= 'timestamp=' . $timestamp . '&';
$addr .= 'APIKEY=' . $APIKEY;
if(!empty($opt)){
$addr.= '&options=' . $opt;
}
return $addr;
}