forked from itomig-de/itomig-webhook-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.itomig-webhook-integration.php
568 lines (518 loc) · 16.4 KB
/
main.itomig-webhook-integration.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
<?php
// Copyright (C) 2017 ITOMIG GmbH
//
// iTop is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// iTop is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with iTop. If not, see <http://www.gnu.org/licenses/>
/**
* Persistent classes (internal): user defined actions
*
* @copyright Copyright (C) 2017 ITOMIG GmbH
* @license http://opensource.org/licenses/AGPL-3.0
*/
require_once (APPROOT . '/core/asynctask.class.inc.php');
require_once (APPROOT . '/core/action.class.inc.php');
define ('WEBREQUEST_SEND_OK', 0);
define ('WEBREQUEST_SEND_PENDING', 1);
define ('WEBREQUEST_SEND_ERROR', 2);
/**
* Web request
* sending web requests to $sURL with specific options
* via curl
*
*/
class WebRequest{
protected $sURL;
protected $aOptions;
/**
* Create a new objects of this class
* @param string $sURL Url which should called with this request
* @return WebRequest new Object of this class
*/
public function __construct($sURL){
$this->sURL = $sURL;
$this->aOptions = array();
}
/**
* Sets Opt Option for curl
* @param string $option name of curl option to be set
* @param string $value value to be set
* @return void
*/
public function setOpt($option, $value){
$this->$aOptions[$option] = $value;
}
/**
* Sets mulltiple opt options for curl at once
* @param string $aOption array with key values for options
* @return void
*/
public function setOptArray($aOptions){
foreach ($aOptions as $option => $value) {
$this->aOptions[$option] = $value;
}
}
/**
* Send the reuqest synchronous
* @param Array $aIssues reference to an array for holding errors or other feeback information
* @param EventNotificationWebrequestNotification $oLog Object where status information can be written in
* @return Intenger status auf webrequest
*/
public function SendSynchronous(&$aIssues, $oLog = null){
//Execute the request
try{
$ch = curl_init($this->sURL);
curl_setopt_array($ch, $this->aOptions);
$sContent = curl_exec($ch);
$sHttpStatus = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
$sErrMessage = '';
// Check for errors and display the error message
if ($iErrno = curl_errno ( $ch )) {
$sErrMessage = curl_error ( $ch );
$aResult->Errno = $iErrno;
$aResult->ErrMessage = $sErrMessage;
}
curl_close ( $ch );
$aIssues = array(
'httpResponse' => array(
'status' => $sHttpStatus,
'content' => $sContent,
'errno' => $iErrno,
'errmsg' => $sErrMessage
)
);
if (($iErrno == 0) && ($sHttpStatus == 200))
{
return WEBREQUEST_SEND_OK;
}
else {
return WEBREQUEST_SEND_ERROR;
}
}
catch(Exception $e) {
IssueLog::Error('ITOMIG Webhook Integration - ' . $e->getMessage());
$aIssues = array($e->getMessage());
return WEBREQUEST_SEND_ERROR;
}
}
/**
* Send the reuqest asynchronous, add request to a queue of async tasks
* @param Array $aIssues reference to an array for holding errors or other feeback information
* @param EventNotificationWebrequestNotification $oLog Object where status information can be written in
* @return Intenger status auf webrequest
*/
protected function SendAsynchronous(&$aIssues, $oLog = null)
{
try{
AsyncSendRequest::AddToQueue($this, $oLog);
}
catch(Exception $e)
{
$aIssues = array($e->GetMessage(),"Exception thrown after tried to add Reuqest to queue");
return WEBREQUEST_SEND_ERROR;
}
$aIssues = array();
return WEBREQUEST_SEND_PENDING;
}
/**
* Send the reuqest
* @param Array $aIssues reference to an array for holding errors or other feeback information
* @param boolean $bForceSynchronous if set to true request will send synch anyway regardless of the configuration
* @param EventNotificationWebrequestNotification $oLog Object where status information can be written in
* @return Intenger status auf webrequest
*/
public function Send(&$aIssues, $bForceSynchronous = false, $oLog = null)
{
if ($bForceSynchronous)
{
return $this->SendSynchronous($aIssues, $oLog);
}
else{
$bConfigASYNC = MetaModel::GetModuleSetting('itomig-webhook-integration', 'asynchronous', false);
if ($bConfigASYNC)
{
return $this->SendAsynchronous($aIssues, $oLog);
}
else
{
return $this->SendSynchronous($aIssues, $oLog);
}
}
}
}
/**
* Configure web request calls
* in objectes of these class
*
*/
abstract class ActionWebRequest extends ActionNotification {
public static function Init() {
$aParams = array (
"category" => "core/cmdb,application",
"key_type" => "autoincrement",
"name_attcode" => "name",
"state_attcode" => "",
"reconc_keys" => array (
'name'
),
"db_table" => "priv_webrequest_notify",
"db_key_field" => "id",
"db_finalclass_field" => "",
"display_template" => ""
);
MetaModel::Init_Params ( $aParams );
MetaModel::Init_InheritAttributes ();
//Init Attributes
MetaModel::Init_AddAttribute ( new AttributeURL ( "webrequest_url", array (
"allowed_values" => null,
"sql" => "webrequest_url",
"default_value" => null,
"target" => "_blank",
"is_null_allowed" => false,
"depends_on" => array ()
) ) );
// Init displays
// Attributes to be displayed for a list view
MetaModel::Init_SetZListItems ( 'list', array (
'name',
'finalclass',
'status',
'webrequest_url',
) );
// Attributes used as criteriaa of the std search form
MetaModel::Init_SetZListItems ( 'standard_search', array (
'name',
'finalclass',
'description',
'status',
'webrequest_url'
) );
// MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
}
// array of strings explaining the issue
protected $m_aWebrequestErrors;
/**
* Execute when Action is called, Get fields, apply params and execute post request
* @param oTrigger object TriggerObject which called the action
* @param aContextArgs array Contect Arguments
*/
public function DoExecute($oTrigger, $aContextArgs) {
// Create NotificationObject to Log Status information if enabled
if (MetaModel::IsLogEnabledNotification ()) {
$oLog = new EventNotificationWebrequestNotification ();
if ($this->IsBeingTested ()) {
$oLog->Set ( 'message', 'TEST - Notifcation pending ' );
} else {
$oLog->Set ( 'message', 'Notification pending' );
}
$oLog->Set ( 'userinfo', UserRights::GetUser () );
$oLog->Set ( 'webrequest_finalclass', $this->Get ('finalclass') );
$oLog->Set ( 'trigger_id', $oTrigger->GetKey () );
$oLog->Set ( 'action_id', $this->GetKey () );
$oLog->Set ( 'object_id', $aContextArgs ['this->object()']->GetKey () );
$oLog->DBInsertNoReload ();
} else {
$oLog = null;
}
try {
//Execute Request
$sRes = $this->_DoExecute ($oTrigger, $aContextArgs, $oLog);
$sPrefix = ($this->IsBeingTested()) ? 'TEST - ' : '';
// Logging Feedback
if ($oLog)
{
$oLog->Set('message', $sPrefix . $sRes);
}
} catch ( Exception $e ) {
if ($oLog) {
$oLog->Set ( 'message', 'Error: ' . $e->getMessage () );
}
}
if ($oLog) {
$oLog->DBUpdate ();
}
}
/**
* Helper function for DoExecute
* @param oTrigger object TriggerObject which called the action
* @param aContextArgs array Contect Arguments
* @param oLog object reference to the Log Object for store information in EventNotification
* @return String result
*/
private function _DoExecute($oTrigger, $aContextArgs, &$oLog) {
$sPreviousUrlMaker = ApplicationContext::SetUrlMakerClass ();
try{
$this->m_aWebrequestErrors = array();
// Get URL
$sWebrequestURL = $this->Get("webrequest_url");
if (!is_null($oLog)){
$oLog->Set('webrequest_url',$sWebrequestURL);
}
// pepare post data depending on finalclass
$aPostParams = $this->preparePostData($aContextArgs, $oLog);
}
catch (Exception $e){
ApplicationContext::SetUrlMakerClass ( $sPreviousUrlMaker );
throw $e;
}
ApplicationContext::SetUrlMakerClass ( $sPreviousUrlMaker );
if(empty($this->m_aWebrequestErrors))
{ // If no error occurred until now
if ($this->IsBeingTested ()) {
// No Execution in Test mode
return "WebRequest Notification Action";
}
else {
$oWebReq = $this->prepareWebRequest($sWebrequestURL,$aPostParams);
$iRes = $oWebReq->Send($aIssues, false, $oLog);
switch ($iRes)
{
case WEBREQUEST_SEND_OK:
return "Sent";
case WEBREQUEST_SEND_PENDING:
return "Pending";
case WEBREQUEST_SEND_ERROR:
if(is_array($aIssues['httpResponse'])){
return "Error - HTTP Response: ".implode(', ', $aIssues['httpResponse']);
}
return "Errors: ".implode(', ', $aIssues);
}
}
}
else
{
return "Errors: " . implode(', ', $this->m_aWebrequestErrors);
}
}
/**
* Helper function for prepareing curl request
* Could be overridden by child classes
* @param url String url for request
* @param aPostParam array post parameters
* @return WebRequest Webrequest with Params and Options
*/
protected function prepareWebRequest($url, $aPostParam){
$oWebReq = new WebRequest($url);
$aCurlOptions = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
CURLOPT_CONNECTTIMEOUT => MetaModel::GetModuleSetting('itomig-webhook-integration', 'timeout', 5),
CURLOPT_POSTFIELDS => json_encode($aPostParam)
);
if (MetaModel::GetModuleSetting('itomig-webhook-integration', 'certificate_check', true)) {
$aCurlOptions[CURLOPT_SSL_VERIFYPEER] = true;
$aCurlOptions[CURLOPT_SSL_VERIFYHOST] = 2;
} else {
// no certificate checks
$aCurlOptions[CURLOPT_SSL_VERIFYPEER] = false;
$aCurlOptions[CURLOPT_SSL_VERIFYHOST] = 0;
}
if (($sCertFile = MetaModel::GetModuleSetting( 'itomig-webhook-integration', 'ca_certificate_file', '')) != '') {
$aCurlOptions[CURLOPT_SSLCERT] = $sCertFile; // The name of a file containing a PEM formatted certificate.
}
$oWebReq->setOptArray($aCurlOptions);
return $oWebReq;
}
abstract protected function preparePostData($aContextArgs, &$oLog);
}
/**
* Configure webhook calls (i.e. Slack)
* in objectes of these class
*
*/
abstract class _ActionWebhookNotification extends ActionWebRequest {
/**
* returns a link to the first found object by oql
* @param sOqlAttCode String code of attribute which contains oql
* @param aArgs array arguments for oql
* @return String link to first result
*/
protected function GetObjectLink($sOqlAttCode, $aArgs){
$sOQL = $this->Get($sOqlAttCode);
if (!isset($sOQL) || strlen($sOQL) == '') return '';
try{
$oSearch = DBObjectSearch::FromOQL($sOQL);
$oSearch->AllowAllData();
}
catch (OQLException $e)
{
$this->m_aWebrequestErrors[] = "query syntax error for recipient '$sOqlAttCode'";
return $e->getMessage();
}
$oSet = new DBObjectSet($oSearch, array() /* order */, $aArgs);
if($oSet->Count() > 1){
IssueLog::Warning("ITOMIG Webhook Integration - Multiple results for OQL '$sOqlAttCode'. Just link the first object.");
//Just Take first
$sClass = $oSet->GetClass();
$oObj = $oSet->Fetch();
return ApplicationContext::MakeObjectUrl($sClass, $oObj->GetKey(), null, true);
}
else if ($oSet->Count() > 0){
$sClass = $oSet->GetClass();
$oObj = $oSet->Fetch();
return ApplicationContext::MakeObjectUrl($sClass, $oObj->GetKey(), null, true);
}
else{
$this->m_aWebrequestErrors[] = "No result for OQL '$sOqlAttCode'";
return null;
}
}
/**
* prepares data for the request
* @param oLog object reference to the Log Object for store information in EventNotification
* @return Array contains data for request
*/
protected function preparePostData($aContextArgs, &$oLog){
$aPostParams_raw = array(
'sWebhookChannel' => $this->Get('channel'),
'sBotAlias' => $this->Get('bot_alias'),
'sSendAttachment' => $this->Get('attachment'),
'sText' => MetaModel::ApplyParams($this->Get("text"), $aContextArgs),
'sAttTitle' => MetaModel::ApplyParams($this->Get("att_title"), $aContextArgs),
'sAttTitleLink' => $this->GetObjectLink('att_title_link', $aContextArgs),
'sAttColor' => $this->Get('att_color'),
'sAttText' => MetaModel::ApplyParams($this->Get("att_text"), $aContextArgs),
'sAttFallback' => MetaModel::ApplyParams($this->Get("att_fallback"), $aContextArgs),
);
return $aPostParams_raw;
}
}
/**
* Logging status and content of web requests
* in objectes of these class
*
*/
class EventNotificationWebrequestNotification extends EventNotification {
public static function Init() {
$aParams = array (
"category" => "core/cmdb,view_in_gui",
"key_type" => "autoincrement",
"name_attcode" => "",
"state_attcode" => "",
"reconc_keys" => array (),
"db_table" => "priv_event_webrequest_notify",
"db_key_field" => "id",
"db_finalclass_field" => "",
"display_template" => "",
"order_by_default" => array (
'date' => false
)
);
MetaModel::Init_Params ( $aParams );
MetaModel::Init_InheritAttributes ();
MetaModel::Init_AddAttribute ( new AttributeText ( "webrequest_url", array (
"allowed_values" => null,
"sql" => "webrequest_url",
"default_value" => null,
"is_null_allowed" => true,
"depends_on" => array ()
) ) );
MetaModel::Init_AddAttribute ( new AttributeText ( "content", array (
"allowed_values" => null,
"sql" => "content",
"default_value" => null,
"is_null_allowed" => true,
"depends_on" => array ()
) ) );
MetaModel::Init_AddAttribute ( new AttributeText ( "webrequest_finalclass", array (
"allowed_values" => null,
"sql" => "webrequest_finalclass",
"default_value" => null,
"is_null_allowed" => true,
"depends_on" => array ()
) ) );
MetaModel::Init_AddAttribute ( new AttributeText ( "response", array (
"allowed_values" => null,
"sql" => "response",
"default_value" => null,
"is_null_allowed" => true,
"depends_on" => array ()
) ) );
// Display lists
MetaModel::Init_SetZListItems ( 'details', array (
'webrequest_finalclass',
'date',
'userinfo',
'message',
'trigger_id',
'action_id',
'object_id',
'webrequest_url',
'content',
'response'
) ); // Attributes to be displayed for the complete details
MetaModel::Init_SetZListItems ( 'list', array (
'date',
'webrequest_finalclass',
'message',
) ); // Attributes to be displayed for a list
// Search criteria
// MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
// MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
}
}
/**
* Sending WebRequests Async
*
*/
class AsyncSendRequest extends AsyncTask
{
public static function Init()
{
$aParams = array
(
"category" => "core/cmdb",
"key_type" => "autoincrement",
"name_attcode" => "created",
"state_attcode" => "",
"reconc_keys" => array(),
"db_table" => "priv_async_send_request",
"db_key_field" => "id",
"db_finalclass_field" => "",
"display_template" => "",
);
MetaModel::Init_Params($aParams);
MetaModel::Init_InheritAttributes();
MetaModel::Init_AddAttribute(new AttributeLongText("request", array("allowed_values"=>null, "sql"=>"request", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
}
static public function AddToQueue($oWebRequest, $oLog)
{
$oNew = MetaModel::NewObject(__class__);
if ($oLog)
{
$oNew->Set('event_id', $oLog->GetKey());
}
$oNew->Set('request', serialize($oWebRequest));
$oNew->DBInsert();
}
public function DoProcess()
{
$oWebRequest = unserialize($this->Get('request'));
$iRes = $oWebRequest->Send($aIssues, true);
switch ($iRes)
{
case WEBREQUEST_SEND_OK:
return "Sent";
case WEBREQUEST_SEND_PENDING:
return "Bug - the request should be sent in synchronous mode";
case WEBREQUEST_SEND_ERROR:
if(is_array($aIssues['httpResponse'])){
return "Failed - HTTP Response: ".implode(', ', $aIssues['httpResponse']);
}
return "Failed: ".implode(', ', $aIssues);
}
}
}
?>