forked from Combodo/combodo-webhook-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatamodel.combodo-webhook-integration.xml
2633 lines (2539 loc) · 76.1 KB
/
datamodel.combodo-webhook-integration.xml
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
<?xml version="1.0" encoding="UTF-8"?>
<itop_design xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.7">
<classes>
<class id="RemoteApplicationType" _delta="define">
<parent>Typology</parent>
<properties>
<category>grant_by_profile,bizmodel,searchable</category>
<abstract>false</abstract>
<key_type>autoincrement</key_type>
<db_table>remoteapplicationtype</db_table>
<db_key_field>id</db_key_field>
<db_final_class_field/>
<naming>
<attributes>
<attribute id="name"/>
</attributes>
</naming>
<display_template/>
<icon>asset/img/icon-remote-application-type-48px.png</icon>
<reconciliation>
<attributes>
<attribute id="name"/>
</attributes>
</reconciliation>
</properties>
<fields>
<field id="remoteapplicationconnections_list" xsi:type="AttributeLinkedSet">
<linked_class>RemoteApplicationConnection</linked_class>
<ext_key_to_me>remoteapplicationtype_id</ext_key_to_me>
</field>
</fields>
<methods/>
<presentation>
<details>
<items>
<item id="name">
<rank>10</rank>
</item>
<item id="remoteapplicationconnections_list">
<rank>100</rank>
</item>
</items>
</details>
</presentation>
</class>
<class id="RemoteApplicationConnection" _delta="define">
<parent>Typology</parent>
<properties>
<category>grant_by_profile,bizmodel,searchable</category>
<abstract>false</abstract>
<key_type>autoincrement</key_type>
<db_table>remoteapplicationconnection</db_table>
<db_key_field>id</db_key_field>
<db_final_class_field/>
<naming>
<attributes>
<attribute id="name"/>
</attributes>
</naming>
<display_template/>
<icon>asset/img/icon-remote-application-connection-48px.png</icon>
<reconciliation>
<attributes>
<attribute id="name"/>
<attribute id="remoteapplicationtype_id"/>
<attribute id="environment"/>
</attributes>
</reconciliation>
</properties>
<fields>
<field id="remoteapplicationtype_id" xsi:type="AttributeExternalKey">
<sql>remoteapplicationtype_id</sql>
<on_target_delete>DEL_AUTO</on_target_delete>
<target_class>RemoteApplicationType</target_class>
<is_null_allowed>false</is_null_allowed>
</field>
<field id="environment" xsi:type="AttributeEnum">
<values>
<value id="development">1-development</value>
<value id="test">2-test</value>
<value id="production">3-production</value>
</values>config
<sql>environment</sql>
<default_value>2-test</default_value>
<is_null_allowed>false</is_null_allowed>
</field>
<field id="url" xsi:type="AttributeString">
<sql>url</sql>
<default_value/>
<is_null_allowed>false</is_null_allowed>
</field>
<field id="actions_list" xsi:type="AttributeLinkedSet">
<linked_class>ActionWebhook</linked_class>
<ext_key_to_me>remoteapplicationconnection_id</ext_key_to_me>
</field>
</fields>
<methods>
<method id="PrepareHeaders">
<static>false</static>
<access>public</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* Prepare connection specific headers (e.g. authentication)
* @return string[]
*/]]></comment>
<code><![CDATA[ public function PrepareHeaders(array $aContextArgs, \EventNotification &$oLog)
{
return [];
}
]]>
</code>
</method>
</methods>
<presentation>
<details>
<items>
<item id="col:col0">
<rank>10</rank>
<items>
<item id="fieldset:RemoteApplicationConnection:baseinfo">
<rank>10</rank>
<items>
<item id="name">
<rank>10</rank>
</item>
<item id="remoteapplicationtype_id">
<rank>20</rank>
</item>
<item id="environment">
<rank>30</rank>
</item>
<item id="url">
<rank>40</rank>
</item>
</items>
</item>
</items>
</item>
<item id="actions_list">
<rank>100</rank>
</item>
</items>
</details>
<search>
<items>
<item id="name">
<rank>10</rank>
</item>
<item id="remoteapplicationtype_id">
<rank>20</rank>
</item>
<item id="environment">
<rank>30</rank>
</item>
<item id="url">
<rank>40</rank>
</item>
</items>
</search>
<list>
<items>
<item id="remoteapplicationtype_id">
<rank>20</rank>
</item>
<item id="environment">
<rank>30</rank>
</item>
<item id="url">
<rank>40</rank>
</item>
</items>
</list>
</presentation>
</class>
<class id="RemoteiTopConnection" _delta="define">
<parent>RemoteApplicationConnection</parent>
<properties>
<category>grant_by_profile,bizmodel,searchable</category>
<abstract>false</abstract>
<key_type>autoincrement</key_type>
<db_table>remoteitopconnection</db_table>
<db_key_field>id</db_key_field>
<db_final_class_field/>
<naming>
<attributes>
<attribute id="name"/>
</attributes>
</naming>
<display_template/>
<icon/>
<reconciliation>
<attributes>
<attribute id="name"/>
<attribute id="remoteapplicationtype_id"/>
<attribute id="environment"/>
</attributes>
</reconciliation>
</properties>
<fields>
<field id="auth_user" xsi:type="AttributeString">
<sql>auth_user</sql>
<default_value/>
<is_null_allowed>false</is_null_allowed>
</field>
<field id="auth_pwd" xsi:type="AttributePassword">
<sql>auth_pwd</sql>
<default_value/>
<is_null_allowed>false</is_null_allowed>
</field>
<field id="version" xsi:type="AttributeString">
<sql>version</sql>
<default_value>1.3</default_value>
<is_null_allowed>false</is_null_allowed>
</field>
</fields>
<methods>
<method id="PrepareHeaders">
<static>false</static>
<access>public</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* Prepare connection specific headers (e.g. authentication)
* @return string[]
*/]]></comment>
<code><![CDATA[ public function PrepareHeaders(array $aContextArgs, \EventNotification &$oLog)
{
$aHeaders = parent::PrepareHeaders($aContextArgs, $oLog);
// Prepare Basic Auth header
$sAuthUser = $this->Get('auth_user');
$sAuthPwd = $this->Get('auth_pwd');
$sEncryptedCredentials = base64_encode("$sAuthUser:$sAuthPwd");
$aHeaders[] = "Authorization: Basic $sEncryptedCredentials";
return $aHeaders;
}
]]>
</code>
</method>
</methods>
<presentation>
<details>
<items>
<item id="col:col0">
<rank>10</rank>
<items>
<item id="fieldset:RemoteApplicationConnection:baseinfo">
<rank>10</rank>
<items>
<item id="name">
<rank>10</rank>
</item>
<item id="remoteapplicationtype_id">
<rank>20</rank>
</item>
<item id="environment">
<rank>30</rank>
</item>
<item id="url">
<rank>40</rank>
</item>
<item id="version">
<rank>50</rank>
</item>
</items>
</item>
<item id="fieldset:RemoteApplicationConnection:authinfo">
<rank>20</rank>
<items>
<item id="auth_user">
<rank>10</rank>
</item>
<item id="auth_pwd">
<rank>20</rank>
</item>
</items>
</item>
</items>
</item>
<item id="actions_list">
<rank>100</rank>
</item>
</items>
</details>
</presentation>
</class>
<class id="RemoteiTopConnectionToken" _delta="define">
<parent>RemoteApplicationConnection</parent>
<properties>
<category>grant_by_profile,bizmodel,searchable</category>
<abstract>false</abstract>
<key_type>autoincrement</key_type>
<db_table>remoteitopconnectiontoken</db_table>
<db_key_field>id</db_key_field>
<db_final_class_field/>
<naming>
<attributes>
<attribute id="name"/>
</attributes>
</naming>
<display_template/>
<icon/>
<reconciliation>
<attributes>
<attribute id="name"/>
<attribute id="remoteapplicationtype_id"/>
<attribute id="environment"/>
</attributes>
</reconciliation>
</properties>
<fields>
<field id="version" xsi:type="AttributeString">
<sql>version</sql>
<default_value>1.3</default_value>
<is_null_allowed>false</is_null_allowed>
</field>
<field id="token" xsi:type="AttributeEncryptedString">
<sql>token</sql>
<default_value/>
<is_null_allowed>false</is_null_allowed>
</field>
</fields>
<methods>
<method id="PrepareHeaders">
<static>false</static>
<access>public</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* Prepare connection specific headers (e.g. authentication)
* @return string[]
*/]]></comment>
<code><![CDATA[ public function PrepareHeaders(array $aContextArgs, \EventNotification &$oLog)
{
$aHeaders = parent::PrepareHeaders($aContextArgs, $oLog);
// Prepare Basic Auth header
$sToken = $this->Get('token');
$aHeaders[] = "Auth-Token: $sToken";
return $aHeaders;
}
]]>
</code>
</method>
</methods>
<presentation>
<details>
<items>
<item id="col:col0">
<rank>10</rank>
<items>
<item id="fieldset:RemoteApplicationConnection:baseinfo">
<rank>10</rank>
<items>
<item id="name">
<rank>10</rank>
</item>
<item id="remoteapplicationtype_id">
<rank>20</rank>
</item>
<item id="environment">
<rank>30</rank>
</item>
<item id="url">
<rank>40</rank>
</item>
<item id="version">
<rank>50</rank>
</item>
</items>
</item>
<item id="fieldset:RemoteApplicationConnection:authinfo">
<rank>20</rank>
<items>
<item id="token">
<rank>10</rank>
</item>
</items>
</item>
</items>
</item>
<item id="actions_list">
<rank>100</rank>
</item>
</items>
</details>
</presentation>
</class>
<class id="ActionWebhook" _delta="define">
<parent>cmdbAbstractObject</parent>
<php_parent>
<name><![CDATA[Combodo\iTop\Core\Notification\Action\_ActionWebhook]]></name>
</php_parent>
<properties>
<category>grant_by_profile,core/cmdb,application</category>
<abstract>false</abstract>
<key_type>autoincrement</key_type>
<db_table>priv_action_webhook</db_table>
<db_key_field>id</db_key_field>
<naming>
<attributes>
<attribute id="name"/>
</attributes>
</naming>
<display_template/>
<icon>asset/img/icon-webhook-48px.png</icon>
<reconciliation>
<attributes>
<attribute id="name"/>
</attributes>
</reconciliation>
</properties>
<fields>
<field id="language" xsi:type="AttributeApplicationLanguage">
<sql>language</sql>
<is_null_allowed>false</is_null_allowed>
<default_value>EN US</default_value>
</field>
<field id="remoteapplicationconnection_id" xsi:type="AttributeExternalKey">
<sql>remoteapplicationconnection_id</sql>
<on_target_delete>DEL_AUTO</on_target_delete>
<target_class>RemoteApplicationConnection</target_class>
<is_null_allowed>false</is_null_allowed>
</field>
<field id="test_remoteapplicationconnection_id" xsi:type="AttributeExternalKey">
<sql>test_remoteapplicationconnection_id</sql>
<on_target_delete>DEL_AUTO</on_target_delete>
<target_class>RemoteApplicationConnection</target_class>
<is_null_allowed>true</is_null_allowed>
</field>
<field id="method" xsi:type="AttributeEnum">
<values>
<value id="get">get</value>
<value id="post">post</value>
<value id="put">put</value>
<value id="patch">patch</value>
<value id="delete">delete</value>
<value id="head">head</value>
</values>
<sql>method</sql>
<default_value>post</default_value>
<is_null_allowed>false</is_null_allowed>
</field>
<field id="path" xsi:type="AttributeString">
<sql>path</sql>
<default_value/>
<is_null_allowed>true</is_null_allowed>
</field>
<field id="headers" xsi:type="AttributeText">
<sql>headers</sql>
<default_value/>
<is_null_allowed>true</is_null_allowed>
</field>
<field id="payload" xsi:type="AttributeText">
<sql>payload</sql>
<default_value/>
<is_null_allowed>true</is_null_allowed>
</field>
<field id="prepare_payload_callback" xsi:type="AttributeString">
<sql>prepare_payload_callback</sql>
<default_value/>
<is_null_allowed>true</is_null_allowed>
</field>
<field id="process_response_callback" xsi:type="AttributeString">
<sql>process_response_callback</sql>
<default_value/>
<is_null_allowed>true</is_null_allowed>
</field>
</fields>
<methods>
<method id="GetRemoteApplicationConnectionFromActionWebhook">
<static>true</static>
<access>public</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* Helper to retrieve the \RemoteApplicationConnection used by $oAction.
* Can be used in response processing methods to know where the response comes from.
*
* @param \ActionWebhook $oAction
*
* @return \RemoteApplicationConnection {@see static::GetRemoteApplicationConnection}
*/]]></comment>
<code><![CDATA[ static public function GetRemoteApplicationConnectionFromActionWebhook($oAction)
{
return $oAction->GetRemoteApplicationConnection();
}
]]>
</code>
</method>
<method id="PrefillCreationForm">
<static>false</static>
<access>public</access>
<type>Overload-DBObject</type>
<code><![CDATA[ public function PrefillCreationForm(&$aContextParam)
{
// Set default content-type
$this->Set('headers', 'Content-type: application/json');
}
]]>
</code>
</method>
<method id="PrepareWebRequest">
<static>false</static>
<access>protected</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* @inheritDoc
*/]]></comment>
<code><![CDATA[ protected function PrepareWebRequest(array $aContextArgs, \EventNotification &$oLog)
{
// Switch to action language in order to have dictionary entries in the right language
// Note: We save current user language so we can switch back to it afterwards
$sUserLang = Dict::GetUserLanguage();
Dict::SetUserLanguage($this->Get('language'));
// Surround with try/catch to make sure to restore user language in case of crash and don't contaminate the rest of the app.
try {
// Get web request parameters
// - URL
$sURL = $this->PrepareURL($aContextArgs, $oLog);
if (!is_null($oLog))
{
$oLog->Set('webhook_url', ($sURL !== null) ? $sURL : 'No test URL provided');
}
// - Method
$sMethod = $this->PrepareMethod($aContextArgs, $oLog);
// - Headers
$aHeaders = $this->PrepareHeaders($aContextArgs, $oLog);
if (!is_null($oLog))
{
$this->LogHeaders($this->Get('headers'), $aHeaders, $oLog);
$this->TruncateLogAttributeForDB($oLog, 'headers');
}
// - Payload
// ... Force payload to be prepared from callback in the method of this class (avoid polymorphic inheritance)
if ($this->HasPreparePayloadCallback()) {
$payload = self::PreparePayload($aContextArgs, $oLog);
}
// ... Payload can be prepared using the polymorphic inheritance
else {
$payload = $this->PreparePayload($aContextArgs, $oLog);
}
if (!is_null($oLog))
{
$this->LogPayload($this->Get('payload'), $payload, $oLog);
$this->TruncateLogAttributeForDB($oLog, 'payload');
}
} catch (Exception $oException) {
throw $oException;
} finally {
// Switch back to current user language
Dict::SetUserLanguage($sUserLang);
}
// Get module parameters
$sConnectionTimeoutInSeconds = MetaModel::GetModuleSetting('combodo-webhook-integration', 'timeout', \Combodo\iTop\Service\WebRequestSender::DEFAULT_CONNECTION_TIMEOUT_IN_SECONDS);
// Prepare cURL options
$aCURLOptions = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => strtoupper($sMethod),
// Note: We pass headers as a cURL option to avoid bug N°3267 in \utils::DoPostRequest()
CURLOPT_HTTPHEADER => $aHeaders,
CURLOPT_CONNECTTIMEOUT => $sConnectionTimeoutInSeconds,
CURLOPT_POSTFIELDS => $payload,
);
// Actually send payload for POST and PUT requests
switch ($sMethod) {
case 'post':
case 'put':
$aCURLOptions[CURLOPT_POST] = true;
break;
}
// Certificate options
// - Should we check its validity?
if (MetaModel::GetModuleSetting('combodo-webhook-integration', 'certificate_check', true))
{
$aCURLOptions[CURLOPT_SSL_VERIFYPEER] = true;
$aCURLOptions[CURLOPT_SSL_VERIFYHOST] = 2;
}
else
{
$aCURLOptions[CURLOPT_SSL_VERIFYPEER] = false;
$aCURLOptions[CURLOPT_SSL_VERIFYHOST] = 0;
}
// - Location of CA certificate file
$sCACertFile = MetaModel::GetModuleSetting('combodo-webhook-integration', 'ca_certificate_file', '');
if (!empty($sCACertFile))
{
// The name of a file containing a PEM formatted certificate.
$aCURLOptions[CURLOPT_SSLCERT] = $sCACertFile;
}
$oWebRequest = new \Combodo\iTop\Core\WebRequest($sURL);
$oWebRequest->SetOptions($aCURLOptions);
return $oWebRequest;
}
]]>
</code>
</method>
<method id="PrepareURL">
<static>false</static>
<access>protected</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* Return the URL for the web request
*
* @param array $aContextArgs
* @param \EventNotification $oLog
*
* @return string
* @throws \ArchivedObjectException
* @throws \CoreException
* @throws \Exception
*/]]></comment>
<code><![CDATA[ protected function PrepareURL(array $aContextArgs, \EventNotification &$oLog)
{
$oRemoteApplicationConnection = $this->GetRemoteApplicationConnection();
// Get URL
if($oRemoteApplicationConnection !== null)
{
$sURL = MetaModel::ApplyParams($oRemoteApplicationConnection->Get('url').$this->Get('path'), $aContextArgs);
}
elseif($this->IsBeingTested())
{
$sURL = null;
}
else
{
throw new Exception('Action should have at least one webhook URL set!');
}
return $sURL;
}
]]>
</code>
</method>
<method id="PrepareMethod">
<static>false</static>
<access>protected</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* Return the web request method (get or post)
*
* @param array $aContextArgs
* @param \EventNotification $oLog
*
* @return string
* @throws \ArchivedObjectException
* @throws \CoreException
*/]]></comment>
<code><![CDATA[ protected function PrepareMethod(array $aContextArgs, \EventNotification &$oLog)
{
return $this->Get('method');
}
]]>
</code>
</method>
<method id="PrepareHeaders">
<static>false</static>
<access>protected</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* Return HTTP headers of the action as an array (eg. ['Key 1: Value 1', 'Key 2: Value 2']).
* If no headers specified, 'Content-type: application/json' will be returned as it is the standard for most webhook apps.
*
* IMPORTANT: The array must be NON associative, otherwise the cURL lib. won't use it.
*
* @param array $aContextArgs
* @param \EventNotification $oLog
*
* @return array
* @throws \ArchivedObjectException
* @throws \CoreException
*/]]></comment>
<code><![CDATA[ protected function PrepareHeaders(array $aContextArgs, \EventNotification &$oLog)
{
$aHeaders = [];
$sHeadersAsText = MetaModel::ApplyParams($this->Get('headers'), $aContextArgs);
$aLines = preg_split('/\r\n|\r|\n/', $sHeadersAsText);
foreach ($aLines as $sLine) {
$sLine = trim($sLine);
if (empty($sLine)) {
continue;
}
$aHeaders[] = $sLine;
}
if (count($aHeaders) === 0) {
$aHeaders[] = 'Content-type: application/json';
}
// Retrieve connection, to let it add some extra headers (e.g. authentication)
$oRemoteApplicationconnection = $this->GetRemoteApplicationConnection();
$aExtraHeaders = $oRemoteApplicationconnection->PrepareHeaders($aContextArgs, $oLog);
foreach($aExtraHeaders as $sExtraHeader)
{
$aHeaders[] = $sExtraHeader;
}
return $aHeaders;
}
]]>
</code>
</method>
<method id="PreparePayload">
<static>false</static>
<access>protected</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* Prepare the payload by formatting it, applying parameters and return it either as:
* - a string (eg. JSON encoded)
* - an array of parameters
*
* @param array $aContextArgs
* @param \EventNotification $oLog
*
* @return string|array
* @throws \ArchivedObjectException
* @throws \CoreException
* @throws \Exception
*/]]></comment>
<code><![CDATA[ protected function PreparePayload(array $aContextArgs, \EventNotification &$oLog)
{
$sCallbackFQCN = $this->Get('prepare_payload_callback');
if (empty($sCallbackFQCN))
{
// Convert / encode payload depending on the content-type header
$sContentType = null;
$aHeaders = $this->PrepareHeaders($aContextArgs, $oLog);
foreach ($aHeaders as $sHeader) {
preg_match('/(Content-type)[\s]*:[\s]*(.*)/', $sHeader, $aMatches);
if (strlen($aMatches[2]) > 0){
$sContentType = $aMatches[2];
break;
}
}
switch ($sContentType) {
case 'application/json':
// Apply params to payload recursively, to send properly encoded JSON
$payload = $this->ApplyParamsToJson($aContextArgs, $this->Get('payload'));
break;
default:
$payload = MetaModel::ApplyParams($this->Get('payload'), $aContextArgs);
break;
}
}
else
{
/** @var \DBObject $oTriggeringObject */
$oTriggeringObject = $aContextArgs['this->object()'];
// Check if callback is on the object itself
if(stripos($sCallbackFQCN, '$this->') !== false)
{
$sMethodName = str_ireplace('$this->', '', $sCallbackFQCN);
$payload = $oTriggeringObject->$sMethodName($aContextArgs, $oLog, $this);
}
// Otherwise, check if callback is callable as a static method
elseif(is_callable($sCallbackFQCN))
{
$payload = call_user_func($sCallbackFQCN, $oTriggeringObject, $aContextArgs, $oLog, $this);
}
// Otherwise, there is a problem
else
{
throw new Exception('Prepare payload callback is not callable ('.$sCallbackFQCN.')');
}
}
return $payload;
}
]]>
</code>
</method>
<method id="LogHeaders">
<static>false</static>
<access>protected</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* Log raw and prepared headers in the event notification.
*
* "Authorization" header will obfuscated automatically.
* This method can be overloaded if you want to obfuscate other parts of what is logged (eg. credentials).
*
* @param string $sRawHeaders
* @param array $aPreparedHeaders
* @param \EventNotification $oLog
*
* @return void
*/]]></comment>
<code><![CDATA[ protected function LogHeaders($sRawHeaders, array $aPreparedHeaders, \EventNotification &$oLog)
{
// Obfuscate authorization header
$sRegExp = '/^Authorization:(.+)$/m';
$sAuthHeaderObfuscated = 'Authorization: ******';
$sObfuscatedRawHeaders = preg_replace($sRegExp, $sAuthHeaderObfuscated, $sRawHeaders);
$sObfuscatedPreparedHeaders = preg_replace($sRegExp, $sAuthHeaderObfuscated, implode("\n", $aPreparedHeaders));
$oLog->Set('headers', "Raw:\n".$sObfuscatedRawHeaders."\n\nAfter preparation:\n".$sObfuscatedPreparedHeaders);
}
]]>
</code>
</method>
<method id="LogPayload">
<static>false</static>
<access>protected</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* Log raw and prepared payload in the event notification.
* This method can be overloaded if you want to obfuscate parts of what is logged (eg. credentials).
*
* @param string $sRawPayload
* @param string $sPreparedPayload
* @param \EventNotification $oLog
*
* @return void
*/]]></comment>
<code><![CDATA[ protected function LogPayload($sRawPayload, $sPreparedPayload, \EventNotification &$oLog)
{
$oLog->Set('payload', "Raw:\n".$sRawPayload."\n\nAfter preparation:\n".$sPreparedPayload);
}
]]>
</code>
</method>
<method id="ApplyParamsToArray">
<static>false</static>
<access>protected</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* Apply $aContextParams recursively to $aInputData
* @param array $aContextArgs
* @param array $aInputData Data to apply params to
*
* @see \MetaModel::ApplyParams
* @return array Same structure as $aInputData but with applied params
*/]]></comment>
<code><![CDATA[ protected function ApplyParamsToArray(array $aContextArgs, array $aInputData)
{
$aPreparedData = [];
foreach ($aInputData as $sKey => $value) {
$sPreparedKey = MetaModel::ApplyParams($sKey, $aContextArgs);
// N°5367 - Fix non-string values (boolean, null) converted into empty string
// Some APIs explicitly require booleans such as 'false'; or null values.
// Do not convert those into a string by calling MetaModel::ApplyParams().
$preparedValue = is_array($value) ? $this->ApplyParamsToArray($aContextArgs, $value) : (is_string($value) ? MetaModel::ApplyParams($value, $aContextArgs) : $value);
$aPreparedData[$sPreparedKey] = $preparedValue;
}
return $aPreparedData;
}
]]>
</code>
</method>
<method id="ApplyParamsToJson">
<static>false</static>
<access>protected</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* Apply $aContextParams to $sInputAsJson
* @param array $aContextArgs
* @param array $sInputAsJson JSON string to apply params to
*
* @see \MetaModel::ApplyParams
* @return string Same string as $sInputAsJson but with applied params
*
* @throw InvalidJsonValueException if payload has an invalid format
*
* @uses json_decode
* @uses json_encode
* @uses ApplyParamsToArray
*
* @since 1.1.2 N°5473 in case invalid JSON : replace Exception by WebhookInvalidJsonValueException, and more data logged (IssueLog)
*/]]></comment>
<code><![CDATA[
protected function ApplyParamsToJson(array $aContextArgs, string $sInputAsJson)
{
$aInputAsArray = json_decode($sInputAsJson, true);
if (false === is_array($aInputAsArray)) {
// LogChannels class and NOTIFICATIONS constant were introduced recently, we can't use it with a 2.7.0 compatibility
$sLogChannel = 'notifications';
$aLogContext = [
'action_class' => get_class($this),
'action_id' => $this->GetKey(),
'action_name' => $this->Get('name'),
'payload' => $sInputAsJson,
'json_last_error' => json_last_error(),
'json_last_error_msg' => json_last_error_msg(),
];
IssueLog::Error('Webhook Action failed: Wrong JSON format for input', $sLogChannel, $aLogContext);
throw new Combodo\iTop\Core\Notification\Action\Webhook\Exception\WebhookInvalidJsonValueException('Wrong JSON format for input, see error log for more information.');
}
$aPreparedDataAsArray = $this->ApplyParamsToArray($aContextArgs, $aInputAsArray);
$sPreparedDataAsJson = json_encode($aPreparedDataAsArray);
return $sPreparedDataAsJson;
}
]]>
</code>
</method>
<method id="GetRemoteApplicationConnection">
<static>false</static>
<access>public</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* Return the RemoteApplicationConnection to use depending on the current object status
*
* @return \RemoteApplicationConnection
*/]]></comment>
<code><![CDATA[ public function GetRemoteApplicationConnection()
{
// Get RemoteApplicationConnection object
$sAttCode = $this->IsBeingTested() ? 'test_remoteapplicationconnection_id' : 'remoteapplicationconnection_id';
$sID = $this->Get($sAttCode);
/** @var \AttributeExternalKey $oAttDef */
$oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
$sClass = $oAttDef->GetTargetClass();
return MetaModel::GetObject($sClass, $sID, false, true);
}
]]>
</code>
</method>
<method id="HasPreparePayloadCallback">
<static>false</static>
<access>public</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* @return bool True if a callback is defined for preparing the payload
*/]]></comment>
<code><![CDATA[ public function HasPreparePayloadCallback()
{
return strlen($this->Get('prepare_payload_callback')) > 0;
}
]]>
</code>
</method>
<method id="HasProcessResponseCallback">
<static>false</static>
<access>public</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* @return bool True if a callback is defined to process the response
*/]]></comment>
<code><![CDATA[ public function HasProcessResponseCallback()
{
return strlen($this->Get('process_response_callback')) > 0;
}
]]>
</code>
</method>
<method id="TruncateLogAttributeForDB">
<static>false</static>
<access>protected</access>
<type>Overload-DBObject</type>
<comment><![CDATA[/**
* Truncate the $sAttCode (based on its att. def.) to ensure that it fits in the DB
*
* @param \EventWebhook $oLog
* @param string $sAttCode
* @return void
* @since 1.0.1
*/]]></comment>
<code><![CDATA[ public function TruncateLogAttributeForDB(\EventWebhook &$oLog, $sAttCode)
{
$oAttDef = MetaModel::GetAttributeDef(get_class($oLog), $sAttCode);
$iMaxSize = $oAttDef->GetMaxSize();
$sFitText = $oLog->Get($sAttCode);
$sEndingText = '[TRUNCATED]';
// Note: We don't use mb_* functions as we don't want to cut after a whole character but after a certain byte length
if ($iMaxSize && (strlen($sFitText) > $iMaxSize))
{
$sFitText = substr($sFitText, 0, $iMaxSize - strlen($sEndingText)) . $sEndingText;
$oLog->Set($sAttCode, $sFitText);
}
}
]]>
</code>
</method>
</methods>
<presentation>
<details>
<items>