-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglpiws.class.php
509 lines (466 loc) · 14.1 KB
/
glpiws.class.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
<?php
/**
* GlpiWebService
*
* A class to handle basic integration with GLPI WebServices plugin.
*
* @author diego.pessanha
* @since 2014-10-09
*
* @var string $url
* @var string $ws_user
* @var string $ws_pass
* @var string $glpi_user
* @var string $glpi_pass
* @var string $error
* @var string $session
* @var SoapClient $client
*/
class GlpiWebService
{
public $url = "http://YOUR-URL/plugins/webservices/soap.php";
public $ws_user = '';
public $ws_pass = '';
public $glpi_user = '';
public $glpi_pass = '';
public $client = null;
public $session = null;
protected $errors = array();
/**
* getErrors
*
* Treat the erros array and return a formatted string
*
* @author diego.pessanha
* @since 2014-10-09
* @return string A string with all errors or null if empty
*/
public function getErrors()
{
if (!empty($this->errors))
return "ERROR: " . implode("\n[ERROR]", $this->errors);
else
return null;
}
/**
* connect
*
* Connect to the WebService and call the doLogin methos if specified
*
* @author diego.pessanha
* @since 2014-10-09
* @param boolean $autoLogin default true
* @return boolean
*/
public function connect($autoLogin = true)
{
// Try to connect to GLPI soap server
try
{
$this->client = new SoapClient(null,array(
'uri' => $this->url,
'location' => $this->url
));
}
catch (Exception $e)
{
$this->errors[] = 'Could not connect';
return false;
}
if ($autoLogin)
{
return $this->login();
}
return true;
}
/**
* login
*
* Authentication method
*
* @author diego.pessanha
* @since 2014-10-09
* @return boolean
*/
public function login()
{
if ($this->client)
{
// Authenticate
$params = array(
'login_name' => $this->glpi_user,
'login_password' => $this->glpi_pass,
'username' => $this->ws_user,
'password' => $this->ws_pass,
'method' => 'glpi.doLogin'
);
try
{
$response = $this->client->__soapCall('genericExecute', array(new SoapParam($params,'params')));
}
catch (Exception $e)
{
$this->errors[] = $e->getMessage();
return false;
}
$this->session = $response['session'];
return true;
}
else
{
$this->errors[] = 'SOAP client not connected! Use GlpiWebService->connect() method.';
return false;
}
}
/**
* createTicket
*
* Create an openned ticket (cannot create an already closed or resolved ticket)
* If a solution is set, it will call a method to set a solution to the ticket
* If a user or group is set, it will call a method to assign the ticket to this entity
*
* @author diego.pessanha
* @since 2014-10-09
*
* @param array $params params of the ticket tah will be sent to GLPI
* @param integer $solutionType ID of the solution in GLPI
* @param string $solutionDesc
* @param integer $userId ID of the user in GLPI
* @param integer $groupId ID of the group in GLPI
*
* @return integer|boolean The ID of the ticket or FALSE in case of error
*
*/
public function createTicket($params, $userId = null, $groupId = null, $solutionType = null, $solutionDesc = null, $closeMessage = null)
{
if ($params == null)
{
$this->errors[] = 'Params are required!';
return false;
}
if ($this->client)
{
$params['method'] = 'glpi.createTicket';
try
{
$response = $this->client->__soapCall("genericExecute", array(new SoapParam($params,'params')));
}
catch (Exception $e)
{
$this->errors[] = $e->getMessage();
return false;
}
if (empty($response) || !isset($response['id']))
{
$this->errors[] = 'No response from GLPI WebService.';
return false;
}
$id = $response['id'];
if ($userId || $groupId)
{
if (!$this->assignTicket($id, $userId, $groupId))
{
return false;
}
}
if ($solutionType)
{
if (!$this->resolveTicket($id, $solutionType, $solutionDesc))
{
return false;
}
}
if ($closeMessage)
{
if (!$this->closeTicket($id, $closeMessage))
{
return false;
}
}
// Return the ticket ID
return $id;
}
else
{
$this->errors[] = 'SOAP client not connected! Use GlpiWebService->connect() method.';
return false;
}
}
/**
* resolveTicket
*
* Set the status of an existing ticket to "Resolved"
*
* @author diego.pessanha
* @since 2014-10-09
*
* @param integer $id ID of the previously created tcket GLPI
* @param integer $solutionType ID of the solution in GLPI
* @param string $solutionDesc
* @return boolean
*
*/
public function resolveTicket($id, $solutionType, $solutionDesc)
{
if ($id == null || $solutionType == null || $solutionDesc == null)
{
$this->errors[] = "ID, solution type and message are required! Ticket $id was created but could not be resolved.";
return false;
}
if ($this->client)
{
// Set solution
$params['method'] = 'glpi.setTicketSolution';
$params['ticket'] = $id;
$params['type'] = $solutionType;
$params['solution'] = $solutionDesc;
// Invoke webservice method with your parameters
try
{
$response = $this->client->__soapCall("genericExecute", array(new SoapParam($params,'params')));
}
catch (Exception $e)
{
$this->errors[] = $e->getMessage();
return false;
}
// Return webservice response object
return true;
}
else
{
$this->errors[] = 'SOAP client not connected! Use GlpiWebService->connect() method.';
return false;
}
}
/**
* assignTicket
*
* Assign an existing ticket to a user or group
*
* @author diego.pessanha
* @since 2014-10-09
*
* @param integer $id ID of the previously created tcket GLPI
* @param integer $userId ID of the user in GLPI
* @param integer $groupId ID of the group in GLPI
* @return boolean
*
*/
public function assignTicket($id, $userId, $groupId)
{
if ($id == null || ($userId == null && $groupId == null))
{
$this->errors[] = "Please, select a valid ticket and a user or a group! Ticket $id was created but could not assig the user/group.";
return false;
}
if ($this->client)
{
$params['method'] = 'glpi.setTicketAssign';
$params['ticket'] = $id;
$params['user'] = $userId;
$params['group'] = $groupId;
try
{
$response = $this->client->__soapCall("genericExecute", array(new SoapParam($params,'params')));
}
catch (Exception $e)
{
$this->errors[] = $e->getMessage();
return false;
}
return true;
}
else
{
$this->errors[] = 'SOAP client not connected! Use GlpiWebService->connect() method.';
return false;
}
}
/**
* closeTicket
*
* Set the status of an existing ticket to "Resolved"
*
* @author diego.pessanha
* @since 2014-10-09
*
* @param integer $id ID of the previously created tcket GLPI
* @param string $message A followup message, mandatory
* @return boolean
*
*/
public function closeTicket($id, $message)
{
if ($id == null || $message == null)
{
$this->errors[] = "ID and a followup message are required! Ticket $id was created but could not be closed.";
return false;
}
if ($this->client)
{
// Set solution
$params['method'] = 'glpi.addTicketFollowup';
$params['ticket'] = $id;
$params['content'] = $message;
$params['close'] = true;
// Invoke webservice method with your parameters
try
{
$response = $this->client->__soapCall("genericExecute", array(new SoapParam($params,'params')));
}
catch (Exception $e)
{
$this->errors[] = $e->getMessage();
return false;
}
// Return webservice response object
return true;
}
else
{
$this->errors[] = 'SOAP client not connected! Use GlpiWebService->connect() method.';
return false;
}
}
/**
* getCategory
*
* Get the name of an existing category
*
* @author diego.pessanha
* @since 2014-10-15
*
* @param integer $id ID of the previously created tcket GLPI
* @param string $id The id of the category
* @return boolean
*
*/
public function getCategory($id)
{
if ($id == null)
{
$this->errors[] = "ID of the category are required!";
return false;
}
if ($this->client)
{
$result = $this->getListItems('itilcategories',$id);
if (!empty($result))
{
return $result[0];
}
else
{
return false;
}
}
else
{
$this->errors[] = 'SOAP client not connected! Use GlpiWebService->connect() method.';
return false;
}
}
/**
* getListItem
*
* Get the list of the items in an existing category
*
* @author diego.pessanha
* @since 2014-10-15
*
* @param integer $id ID of the previously created tcket GLPI
* @param string $listName The name of the class of special list in GLPI
* @param string $id The id of the category
* @return boolean
*
*/
public function getListItems($listName, $id = null, $name = null)
{
if ($listName == null)
{
$this->errors[] = "Name of the list is required!";
return false;
}
if ($this->client)
{
// Set solution
$params['method'] = 'glpi.listDropdownValues';
$params['dropdown'] = $listName;
if ($id != null) $params['id'] = $id;
if ($name != null) $params['name'] = $name;
// Invoke webservice method with your parameters
try
{
$response = $this->client->__soapCall("genericExecute", array(new SoapParam($params,'params')));
}
catch (Exception $e)
{
$this->errors[] = $e->getMessage();
return false;
}
// Return webservice response object
return $response;
}
else
{
$this->errors[] = 'SOAP client not connected! Use GlpiWebService->connect() method.';
return false;
}
}
/**
* uploadDocument
*
* Attach a file to an existing ticket. Verify allowed extensions on GLPI.
*
* @author diego.pessanha
* @since 2014-10-15
*
* @param integer $id ID of the previously created tcket GLPI
* @param string $name The name of the document (no spaces and with extension)
* @param string $docPath The local path to the document
* @param string $content Follow-up text
* @return boolean
*
*/
public function uploadDocument($id, $name, $docPath, $content = null)
{
if ($id == null || ($name == null && $docPath == null))
{
$this->errors[] = "ID of the ticket, name of the document and the path to it are required!";
return false;
}
if (!file_exists($docPath))
{
$this->errors[] = "The file does not exists!";
return false;
}
if ($this->client)
{
$base64 = base64_encode(file_get_contents($docPath));
// Set solution
$params['method'] = 'glpi.addTicketDocument';
$params['ticket'] = $id;
$params['name'] = $name;
$params['base64'] = $base64;
if ($content != null) $params['content'] = $content;
// Invoke webservice method with your parameters
try
{
$response = $this->client->__soapCall("genericExecute", array(new SoapParam($params,'params')));
}
catch (Exception $e)
{
$this->errors[] = $e->getMessage();
return false;
}
// Return webservice response object
return true;
}
else
{
$this->errors[] = 'SOAP client not connected! Use GlpiWebService->connect() method.';
return false;
}
}
}