This repository was archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmantisws.class.php
More file actions
500 lines (439 loc) · 14.8 KB
/
mantisws.class.php
File metadata and controls
500 lines (439 loc) · 14.8 KB
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
<?php
/**
* -------------------------------------------------------------------------
* Mantis plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Mantis.
*
* Mantis is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Mantis 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Mantis. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2014-2022 by Mantis plugin team.
* @license AGPLv3 https://www.gnu.org/licenses/agpl-3.0.html
* @link https://github.com/pluginsGLPI/mantis
* -------------------------------------------------------------------------
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access directly to this file");
}
class PluginMantisMantisws {
private $_host;
private $_url;
private $_login;
private $_password;
private $_client;
function __construct() {
}
function getConnexion($host, $url, $login, $pwd) {
$this->_host = $host;
$this->_url = $url;
$this->_password = $pwd;
$this->_login = $login;
$this->_client = new SoapClient($this->_host . "/" . $this->_url, self::getOptionsStreamContext());
}
/**
* function to initialize the connection to the Web service
* with the configuration settings stored in BDD
*/
function initializeConnection() {
$conf = new PluginMantisConfig();
$conf->getFromDB(1);
if (! empty($conf->fields["host"]) && ! empty($conf->fields["url"])) {
$this->_host = $conf->fields["host"];
$this->_url = $conf->fields["url"];
$this->_login = $conf->fields["login"];
$this->_password = Toolbox::sodiumDecrypt($conf->fields["pwd"]);
$this->_client = new SoapClient($this->_host . "/" . $this->_url, self::getOptionsStreamContext());
return true;
} else {
return false;
}
}
/**
* function to test the connectivity of the web service
*
* @param $host
* @param $url
* @param $login
* @param $password
* @return bool
* @throws Exception
*/
function testConnectionWS($host, $url, $login, $password) {
if (empty($host) or empty($url)) {
return false;
}
try {
$client = new SoapClient($host . "/" . $url, self::getOptionsStreamContext());
$client->mc_login($login, $password);
return true;
} catch (SoapFault $sp) {
Toolbox::logInFile('mantis', sprintf(__('Error to connect to the web service MantisBT => \'%1$s\'', 'mantis'), $sp->getMessage()) . "\n");
if ($sp->getMessage() == 'Access denied') {
return false;
} else {
throw new Exception($sp->getMessage());
}
}
}
/**
*
* @param $name
* @return array|bool
*/
public function getActorFromProjectName($name) {
$id = $this->getProjectIdWithName($name);
try {
$response = $this->_client->mc_project_get_users($this->_login, $this->_password, $id, [
90,
10,
25,
40,
55,
70
]);
$list = [];
$list[] = [
'id' => 0,
'name' => '----'
];
foreach ($response as &$actor) {
$list[] = [
'id' => $actor->id,
'name' => $actor->name
];
}
return ($list);
} catch (SoapFault $e) {
Toolbox::logInFile('mantis', sprintf(__('Error retrieving user of project id \'%1$s\' => \'%2$s\'', 'mantis'), $id, $e->getMessage()) . "\n");
return false;
}
}
public function getCustomFieldFromProjectName($name) {
$id = $this->getProjectIdWithName($name);
try {
$response = $this->_client->mc_project_get_custom_fields($this->_login, $this->_password, $id);
$list = [];
$list[] = [
'id' => 'additional_information',
'name' => 'additional_information'
];
$list[] = [
'id' => 'note',
'name' => 'note'
];
foreach ($response as $field) {
$list[] = [
'id' => $field->field->name,
'name' => $field->field->name
];
}
return ($list);
} catch (SoapFault $e) {
Toolbox::logInFile('mantis', sprintf(__('Error retrieving user of project id \'%1$s\' => \'%2$s\'', 'mantis'), $id, $e->getMessage()) . "\n");
return false;
}
}
public function getCustomFieldByNameAndProject($nameCustomField, $nameProject) {
$id = $this->getProjectIdWithName($nameProject);
try {
$response = $this->_client->mc_project_get_custom_fields($this->_login, $this->_password, $id);
$list = [];
$list[] = [
'id' => 0,
'name' => '----'
];
foreach ($response as $field) {
if ($field->field->name == $nameCustomField) {
return $field->field;
}
}
return null;
} catch (SoapFault $e) {
Toolbox::logInFile('mantis', sprintf(__('Error retrieving user of project id \'%1$s\' => \'%2$s\'', 'mantis'), $id, $e->getMessage()) . "\n");
return false;
}
}
/**
* Function to find category by name of project
*
* @param $name name of project
* @return array return categorie if find else false
*/
public function getCategoryFromProjectName($name) {
$id = $this->getProjectIdWithName($name);
try {
$response = $this->_client->mc_project_get_categories($this->_login, $this->_password, $id);
return ($response);
} catch (SoapFault $e) {
Toolbox::logInFile('mantis', sprintf(__('Error retrieving category from the project id \'%1$s\' => \'%2$s\'', 'mantis'), $id, $e->getMessage()) . "\n");
return false;
}
}
/**
* function to check if an issue exists
*
* @param $_issue_id
* @return bool
*/
public function existIssueWithId($_issue_id) {
try {
$response = $this->_client->mc_issue_exists($this->_login, $this->_password, $_issue_id);
return ($response);
} catch (SoapFault $e) {
Toolbox::logInFile('mantis', sprintf(sprintf(__('Error when checking existence of the MantisBT ticket \'%1$s\' => \'%2$s\'', 'mantis'), $_issue_id, $e->getMessage())) . "\n");
return false;
}
}
/**
* Function to delete an issue with id
*
* @param integer $_issue_id
* @return boolean
*/
public function deleteIssue($_issue_id) {
return $this->_client->mc_issue_delete($this->_login, $this->_password, $_issue_id);
}
/**
* Method to call the operation originally named mc_issue_note_add
*
* @param integer $_issue_id
* @param PluginMantisStructissuenotedata $_note
* @return integer
*/
public function addNoteToIssue($_issue_id, PluginMantisStructissuenotedata $_note) {
global $CFG_GLPI;
try {
return $this->_client->mc_issue_note_add($this->_login, $this->_password, $_issue_id, $_note);
} catch (SoapFault $e) {
Toolbox::logInFile('mantis', sprintf(__('error while creating note => \'%1$s\'', 'mantis'), $e->getMessage()) . "\n");
return false;
}
}
/**
* Function to add an attachment to an issue
*
* @param integer $_issue_id
* @param string $_name
* @param string $_file_type
* @param base64Binary $_content
* @return integer
*/
public function addAttachmentToIssue($_issue_id, $_name, $_file_type, $_content) {
global $CFG_GLPI;
try {
return $this->_client->mc_issue_attachment_add($this->_login, $this->_password, $_issue_id, $_name, $_file_type, $_content);
} catch (SoapFault $e) {
if ($e->getMessage() == "Duplicate filename.") {
Toolbox::logInFile('mantis', sprintf(__('WARNING: %1$s already exists', 'mantis'), $_name) . "\n");
return true;
} else {
Toolbox::logInFile('mantis', sprintf(__('error while creating attachment => \'%1$s\'', 'mantis'), $e->getMessage()) . "\n");
return false;
}
}
}
/**
* Function to get an attachment to an issue
*
* @param integer $_issue_id
* @param string $_name
* @param string $_file_type
* @param base64Binary $_content
* @return integer
*/
public function getAttachmentFromIssue($_issue_id) {
global $CFG_GLPI;
try {
return $this->_client->mc_issue_attachment_get($this->_login, $this->_password, $_issue_id);
} catch (SoapFault $e) {
Toolbox::logInFile('mantis', sprintf(__('error while getting attachment => \'%1$s\'', 'mantis'), $e->getMessage()) . "\n");
return false;
}
}
/**
* Function to add issue
*
* @param $issue
* @return Integer
*/
function addIssue($issue) {
global $CFG_GLPI;
try {
return $this->_client->mc_issue_add($this->_login, $this->_password, $issue);
} catch (SoapFault $e) {
Toolbox::logInFile('mantis', sprintf(__('Error creating MantisBT ticket \'%1$s\'', 'mantis'), $e->getMessage()) . "\n");
return false;
}
}
/**
* Function to find issue by id
*
* @param $idIssue
* @return bool
*/
function getIssueById($idIssue) {
global $CFG_GLPI;
try {
$response = $this->_client->mc_issue_get($this->_login, $this->_password, $idIssue);
return $response;
} catch (SoapFault $e) {
Toolbox::logInFile('mantis', sprintf(__('Error searching MantisBT ticket \'%1$s\' => \'%2$s\'', 'mantis'), $idIssue, $e->getMessage()) . "\n");
return false;
}
}
/**
* function to find id of project with name
*
* @param $name
* @return mixed
*/
public function getProjectIdWithName($name) {
global $CFG_GLPI;
try {
return $this->_client->mc_project_get_id_from_name($this->_login, $this->_password, $name);
} catch (SoapFault $e) {
Toolbox::logInFile('mantis', sprintf(__('Error retrieving the id of the project by it\'s name \'%1$s\' => \'%2$s\'', 'mantis'), $name, $e->getMessage()) . "\n");
return "ERROR -> " . $e->getMessage();
}
}
/**
* function to check if project exists (with name)
*
* @param $name
* @return bool
*/
public function existProjectWithName($name) {
global $CFG_GLPI;
try {
$response = $this->_client->mc_project_get_id_from_name($this->_login, $this->_password, $name);
if ($response == 0) {
return false;
} else {
return true;
}
} catch (SoapFault $e) {
Toolbox::logInFile('mantis', sprintf(__('Error when checking the existence of the project by his name \'%1$s\' => \'%2$s\'', 'mantis'), $name, $e->getMessage()) . "\n");
return false;
}
}
/**
* Delete the note with the specified id.
*
* @param integer $_issue_note_id
* @return boolean
*/
public function deleteNote($_issue_note_id) {
global $CFG_GLPI;
try {
return $this->_client->mc_issue_note_delete($this->_login, $this->_password, $_issue_note_id);
} catch (SoapFault $e) {
Toolbox::logInFile('mantis', sprintf(__('Error when deleting note \'%1$s\' => \'%2$s\'', 'mantis'), $_issue_note_id, $e->getMessage()) . "\n");
return false;
}
}
/**
* Delete the issue attachment with the specified id.
*
* @param integer $_issue_attachment_id
* @return boolean
*/
public function deleteAttachment($_issue_attachment_id) {
global $CFG_GLPI;
try {
return $this->_client->mc_issue_attachment_delete($this->_login, $this->_password, $_issue_attachment_id);
} catch (SoapFault $e) {
Toolbox::logInFile('mantis', sprintf(__('Error when deleting attachment \'%1$s\' => \'%2$s\'', 'mantis'), $_issue_attachment_id, $e->getMessage()) . "\n");
return false;
}
}
/**
* Get the enumeration for status.
*
* @return array
*/
public function getStateMantis() {
try {
return $this->_client->mc_enum_status($this->_login, $this->_password);
} catch (SoapFault $e) {
Toolbox::logInFile('mantis', sprintf(__('Error when getting MantisBT states => \'%1$s\'', 'mantis'), $e->getMessage()) . "\n");
return false;
}
}
public function updateIssueMantis($id, $issue) {
try {
return $this->_client->mc_issue_update($this->_login, $this->_password, $id, $issue);
} catch (SoapFault $e) {
Toolbox::logInFile('mantis', sprintf(__('Error when updating MantisBT => \'%1$s\'', 'mantis'), $e->getMessage()) . "\n");
return false;
}
}
public function setClient($client) {
$this->_client = $client;
}
public function getClient() {
return $this->_client;
}
public function setHost($host) {
$this->_host = $host;
}
public function getHost() {
return $this->_host;
}
public function setLogin($login) {
$this->_login = $login;
}
public function getLogin() {
return $this->_login;
}
public function setPassword($password) {
$this->_password = $password;
}
public function getPassword() {
return $this->_password;
}
public function setUrl($url) {
$this->_url = $url;
}
public function getUrl() {
return $this->_url;
}
/**
* Creates a stream context array for SSL option.
*
* @param nothing
* @return array
*/
static function getOptionsStreamContext() {
global $CFG_GLPI;
$conf = new PluginMantisConfig();
$conf->getFromDB(1);
$opts = ["ssl" => ["verify_peer" => $conf->fields['check_ssl'],
"verify_peer_name" => $conf->fields['check_ssl']]];
if (!empty($CFG_GLPI['proxy_name'])
&& $conf->fields['use_proxy']) {
$proxy = $CFG_GLPI['proxy_user'].
":".$CFG_GLPI['proxy_passwd'].
"@".preg_replace('#https?://#', '', $CFG_GLPI['proxy_name']).
":".$CFG_GLPI['proxy_port'];
$opts['http'] = [
'proxy' => "tcp://$proxy"
];
}
$context = stream_context_create($opts);
return ['stream_context' => $context];
}
}