-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathRequestTracker.php
More file actions
761 lines (667 loc) · 24.2 KB
/
Copy pathRequestTracker.php
File metadata and controls
761 lines (667 loc) · 24.2 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
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
<?php
/**
* RTPHPLib v1.2.0
* Copyright (C) 2012 Samuel Schmidt
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*
* Requires curl
*
* Standard request fields are documented at http://requesttracker.wikia.com/wiki/REST
* Depending on your request type, this will determine how you create your array of values.
* See the example script for a demonstration.
*
*/
class RequestTracker
{
/**
* The location of the REST api
* @var string
*/
protected $url;
/**
* The location of the next request
* @var string
*/
protected $requestUrl;
/**
* Username with which to authenticate
* @var string
*/
protected $user;
/**
* Password to use
* @var string
*/
protected $pass;
/**
* Current set of fields to post to RT
* @var array
*/
protected $postFields;
/**
* If false, will disable verification of SSL certificates.
* This is not recommended for production use. If SSL is not
* working and the RT host's cert is valid, you should verify that
* your curl installation has a CA cert bundle installed.
* @var bool
*/
protected $enableSslVerification = true;
/**
* Create a new instance for API requests
* @param string $rootUrl
* The base URL to your request tracker installation. For example,
* if your RT is located at "http://rt.example.com", your rootUrl
* would be "http://rt.example.com". There should be no trailing slash.
* @param string $user The username to authenticate with.
* @param string $pass The password to authenticate with.
*/
public function __construct($rootUrl, $user, $pass)
{
$this->url = $rootUrl."/REST/1.0/";
$this->user = $user;
$this->pass = $pass;
}
/**
* Sends a request to your RT.
*
* In general, this function should not be called directly- should only
* be used by a subclass if there is custom functionality not covered
* by the general API functions provided.
*
* @param boolean $doNotUseContentField - the normal behavior of this
* function is to take the postFields and push them into a single
* content field for the POST. If this is set to true, the postFields
* will be used as the fields for the form instead of getting pushed
* into the content field.
*
* @param object[] $attachments Attachments array to add to ticket keyed by
* filenames. The array values should be CURLFile objects for PHP > 5.5 or
* an array of strings containing the file info prepended with "@" for
* PHP < 5.5 eg:
* @/tmp/phpK5TNJc
* or
* @/tmp/phpK5TNJc;type=text/plain;filename=2.txt
*
* From original Request Tracker API, to add attachment to ticket while doing a comment
* we must add another attachment_1 param with raw file
* http://requesttracker.wikia.com/wiki/REST#Ticket_History_Comment
* After testings, one right way is to create CurlObject with file and to put into attachment_1
* because normal POST field fails
* More info: http://php.net/manual/en/class.curlfile.php
*/
protected function send($doNotUseContentField = false, $attachments = false)
{
if (!empty($this->postFields) && $doNotUseContentField == true) {
$fields = $this->postFields;
$fields['user'] = $this->user;
$fields['pass'] = $this->pass;
} elseif (!empty($this->postFields)) {
$fields = array('user'=>$this->user, 'pass'=>$this->pass, 'content'=>$this->parseArray($this->postFields));
} else {
$fields = array('user'=>$this->user, 'pass'=>$this->pass);
}
// If we've received attachment param, we have to add to POST params apart from 'content' and send Content-Type
if (!empty($attachments)) {
$i = 1;
foreach ($attachments as $key => $attachment) {
$fields['attachment_'.$i++] = $attachment;
}
}
$response = $this->post($fields);
$this->setPostFields('');
return $response;
}
/**
* Create a ticket
* @param array $content the ticket fields as fieldname=>fieldvalue array
* @return array key=>value response pair array
*/
public function createTicket($content, array $attachments = array())
{
$content['id'] = 'ticket/new';
$url = $this->url."ticket/new";
if (isset($content['Text'])) {
$content['Text'] = str_replace("\n", "\n ", $content['Text']);
}
$this->setRequestUrl($url);
$this->setPostFields($content);
if (!empty($attachments)) {
$content['Attachment'] = implode("\n ", array_keys($attachments));
$this->setPostFields($content);
$response = $this->send(false, $attachments);
} else {
$this->setPostFields($content);
$response = $this->send();
}
return $this->parseResponse($response);
}
/**
* Edit ticket
* @param int $ticketId
* @param array $content the ticket fields as fieldname=>fieldvalue array
* @return array key=>value response pair array
*/
public function editTicket($ticketId, $content)
{
$url = $this->url."ticket/$ticketId/edit";
$this->setRequestUrl($url);
$this->setPostFields($content);
$response = $this->send();
return $this->parseResponse($response);
}
/**
* Reply to a ticket
* @param int $ticketId
* @param array $content the ticket fields as fieldname=>fieldvalue array
* @param array $attachments ticket attachments array keyed by filename.
* For the array value see self::send() documentation.
* @return array key=>value response pair array
*/
public function doTicketReply($ticketId, $content, array $attachments = array())
{
$content['Action'] = 'correspond';
$content['id'] = $ticketId;
$url = $this->url."ticket/$ticketId/comment";
if (isset($content['Text'])) {
$content['Text'] = str_replace("\n", "\n ", $content['Text']);
}
$this->setRequestUrl($url);
if (!empty($attachments)) {
$content['Attachment'] = implode("\n ", array_keys($attachments));
$this->setPostFields($content);
$response = $this->send(false, $attachments);
} else {
$this->setPostFields($content);
$response = $this->send();
}
return $this->parseResponse($response);
}
/**
* Merge a ticket into another
* @param int $ticketId
* @param int $intoId
* @return array key=>value response pair array
*/
public function doTicketMerge($ticketId, $intoId)
{
$url = $this->url."ticket/$ticketId/merge/$intoId";
$this->setRequestUrl($url);
$response = $this->send();
return $this->parseResponse($response);
}
/**
* Comment on a ticket
* @param int $ticketId
* @param array $content
* @param array $attachments ticket attachments array keyed by filename.
* For the array value see self::send() documentation.
* @return array key=>value response pair array
*/
public function doTicketComment($ticketId, $content, array $attachments = array())
{
$content['Action'] = 'comment';
$url = $this->url."ticket/$ticketId/comment";
if (isset($content['Text'])) {
$content['Text'] = str_replace("\n", "\n ", $content['Text']);
}
$this->setRequestUrl($url);
if (!empty($attachments)) {
$content['Attachment'] = implode("\n ", array_keys($attachments));
$this->setPostFields($content);
$response = $this->send(false, $attachments);
} else {
$this->setPostFields($content);
$response = $this->send();
}
return $this->parseResponse($response);
}
/**
* Get ticket metadata
* @param int $ticketId
*/
public function getTicketProperties($ticketId)
{
$url = $this->url."ticket/$ticketId/show";
$this->setRequestUrl($url);
$response = $this->send();
return $this->parseResponse($response);
}
/**
* Get ticket links
* @param int $ticketId
* @return array key=>value response pair array
*/
public function getTicketLinks($ticketId)
{
$url = $this->url."ticket/$ticketId/links/show";
$this->setRequestUrl($url);
$response = $this->send();
return $this->parseResponse($response);
}
/**
* Add link from one ticket to another without worrying about existing links
* @param int $ticket1
* @param string $relationship - RefersTo, ReferredToBy, MemberOf, HasMember, DependsOn, DependedOnBy
* @param int $ticket2
* @return array key=>value response pair array
*/
public function addTicketLink($ticket1, $relationship, $ticket2)
{
/* Note that this URL does not contain a ticket number. */
$url = $this->url."ticket/link";
$this->setRequestUrl($url);
$content = array(
'id' => $ticket1,
'rel' => $relationship,
'to' => $ticket2
);
$this->setPostFields($content);
/* Use $doNotUseContentField = true for the send($doNotUseContentField)
function so that the fields won't get pushed into the content field. */
$response = $this->send(true);
return $this->parseResponse($response);
}
/**
* Modify links on a ticket
* @param int $ticketId
* @param array $content
* @return array key=>value response pair array
*/
public function editTicketLinks($ticketId, $content)
{
$url = $this->url."ticket/$ticketId/links";
$this->setRequestUrl($url);
$this->setPostFields($content);
$response = $this->send();
return $this->parseResponse($response);
}
/**
* Get a list of attachments on a ticket
* @param int $ticketId
* @return array key=>value response pair array
*/
public function getTicketAttachments($ticketId)
{
$url = $this->url."ticket/$ticketId/attachments";
$this->setRequestUrl($url);
$response = $this->send();
$response = $this->parseResponse($response);
if (!empty($response['Attachments'])) {
// Turn Attachments to an arrray keyed by attachment id:
$attachments = explode(chr(10), $response['Attachments']);
$response = $this->parseResponseBody($attachments);
}
return $response;
}
/**
* Get a specific attachment's metadata on a ticket
* @param int $ticketId
* @param int $attachmentId
* @return array key=>value response pair array
*/
public function getAttachment($ticketId, $attachmentId)
{
$url = $this->url."ticket/$ticketId/attachments/$attachmentId";
$this->setRequestUrl($url);
$response = $this->send();
return $this->parseResponse($response);
}
/**
* Get the content of an attachment
* @param int $ticketId
* @param int $attachmentId
* @param bool $raw @todo: Default this to true? I dont see a usecase for false tbh.
* @return array key=>value response pair array
*/
public function getAttachmentContent($ticketId, $attachmentId, $raw = false)
{
$url = $this->url."ticket/$ticketId/attachments/$attachmentId/content";
$this->setRequestUrl($url);
$response = $this->send();
if (! $raw) {
return $this->parseResponse($response);
} else {
// Remove unneeded newlines from response body and return it as string.
$body = explode(chr(10), $response['body']);
// getAttachmentContent even returns 3 newlines at the end, and cleanResponseBody only strips off 1
// We therefor use array_splice($body, 0, -2) as an extra
return implode(chr(10), $this->cleanResponseBody(array_splice($body, 0, -2)));
}
}
/**
* Get the history of a ticket
* @param int $ticketId
* @param boolean $longFormat Whether to return all data of each history node
* @return array key=>value response pair array
*/
public function getTicketHistory($ticketId, $longFormat = true)
{
if ($longFormat) {
$url = $this->url."ticket/$ticketId/history?format=l";
} else {
$url = $this->url."ticket/$ticketId/history";
}
$this->setRequestUrl($url);
$response = $this->send();
return $longFormat ? $this->parseLongTicketHistoryResponse($response) : $this->parseResponse($response);
}
/**
* Get the long form data of a specific ticket history node
* @param int $ticketId
* @param int $historyId
* @return array key=>value response pair array
*/
public function getTicketHistoryNode($ticketId, $historyId)
{
$url = $this->url."ticket/$ticketId/history/id/$historyId";
$this->setRequestUrl($url);
$response = $this->send();
return $this->parseResponse($response);
}
/**
* Convenience wrapper to search for tickets.
*
* @see self::search()
*/
public function searchTickets($query, $orderby, $format = 's')
{
return $this->search($query, $orderby, $format);
}
/**
* General search based on a query
*
* Extend the Request Tracker class and implement custom search functions there
* by passing $query and $orderBy to this function
* @param string $query the query to run
* @param string $orderBy how to order the query
* @param string $format the format type (i,s,l)
* @param string $type search for: 'ticket', 'queue', 'group' or 'user'?
* @param array $fields fields to return
*
* @return array
* 's' = ticket-id=>ticket-subject
* 'i' = not implemented
* 'l' = not implemented
*/
public function search($query, $orderBy, $format = 's', $type = 'ticket', $fields = array())
{
$url = $this->url."search/$type?query=".urlencode($query);
if (!empty($fields)) {
$url .= '&fields=' . urlencode(implode(',', $fields));
}
$url .= "&orderby=$orderBy&format=$format";
$this->setRequestUrl($url);
$response = $this->send();
$responseArray = array();
if ($format=='s') {
$responseArray = $this->parseResponse($response);
} elseif ($format=='i') {
return $response['body'];
} elseif ($format=='l') {
$responseArray = $this->parseLongFormatSearchResponse($response);
}
return $responseArray;
}
protected function parseResponse($response, $delimiter = ':')
{
$response = explode(chr(10), $response['body']);
$response = $this->cleanResponseBody($response);
return $this->parseResponseBody($response);
}
private function parseLongFormatSearchResponse($response, $delimiter = ':')
{
$resultNodes = array();
$resultStrings = preg_split('/(?=^id: )/m', $response['body'], null);
// First item contains RT version and newline, remove it.
unset($resultStrings[0]);
foreach ($resultStrings as $resultString) {
$node = explode(chr(10), $resultString);
// remove empty line in the end.
array_pop($node);
$resultNodes[] = $this->parseResponseBody($node, $delimiter);
}
return $resultNodes;
}
private function parseLongTicketHistoryResponse($response, $delimiter = ':')
{
$historyNodes = array();
$historyNodeStrings = preg_split('/\# ([0-9]*)\/([0-9]*) \(id\/([0-9]*)\/total\)/', $response['body']);
// First item contains RT version and newline, remove it.
unset($historyNodeStrings[0]);
foreach ($historyNodeStrings as $historyNodeString) {
$node = explode(chr(10), $historyNodeString);
$node = $this->cleanResponseBody($node);
$node = $this->parseResponseBody($node, $delimiter);
if (!empty($node['Attachments'])) {
$node['Attachments'] = $this->parseResponseBody(explode(chr(10), $node['Attachments']), $delimiter);
}
else {
// Normalize to an array.
$node['Attachments'] = array();
}
$historyNodes[] = $node;
}
return $historyNodes;
}
private function cleanResponseBody(array $response)
{
array_shift($response); //skip RT status response
array_shift($response); //skip blank line
array_pop($response); //remove empty blank line in the end
return $response;
}
private function parseResponseBody(array $response, $delimiter = ':')
{
$responseArray = array();
$lastkey = null;
foreach ($response as $line) {
//RT will always preface a multiline with the length of the last key + length of $delimiter + one space)
if(! is_null($lastkey) && preg_match('/^\s{' . ( strlen($lastkey) + strlen($delimiter) + 1 ) . '}(.*)$/', $line, $matches)) {
$responseArray[$lastkey] .= "\n" . $matches[1];
}
elseif(! is_null($lastkey) && strlen($line) == 0) {
$lastkey = null;
}
elseif(preg_match('/^#/', $line, $matches)) {
$responseArray[$line] = '';
}
elseif(preg_match('/^([a-zA-Z0-9]+|CF\.{[^}]+})' . $delimiter . '\s(.*)$/', $line, $matches)) {
$lastkey = $matches[1];
$responseArray[$lastkey] = $matches[2];
}
}
return $responseArray;
}
private function parseArray($contentArray)
{
$content = "";
foreach ($contentArray as $key => $value) {
$content .= "$key: $value".chr(10);
}
return $content;
}
/**
* Get metadata for a user
* @param int|string $userId either the user id or the user login
* @return array key=>value response pair array
*/
public function getUserProperties($userId)
{
$url = $this->url."user/$userId";
$this->setRequestUrl($url);
$response = $this->send();
return $this->parseResponse($response);
}
/**
* Create a user
* @param array $content the user fields as fieldname=>fieldvalue array
* @return array key=>value response pair array
*/
public function createUser($content)
{
$content['id'] = 'user/new';
$url = $this->url."user/new";
$this->setRequestUrl($url);
$this->setPostFields($content);
$response = $this->send();
return $this->parseResponse($response);
}
/**
* Edit user
* @param int $userId
* @param array $content the user fields as fieldname=>fieldvalue array
* @return array key=>value response pair array
*/
public function editUser($userId, $content)
{
$url = $this->url."user/$userId/edit";
$this->setRequestUrl($url);
$this->setPostFields($content);
$response = $this->send();
return $this->parseResponse($response);
}
/**
* Convenience wrapper to search for users.
*
* @see self::search()
*/
public function searchUsers($query = '', $orderby = '', $format = 's')
{
return $this->search($query, $orderby, $format, 'user');
}
/**
* Get metadata of a queue
* @param int $queueId
* @return array key=>value response pair array
*/
public function getQueueProperties($queueId)
{
$url = $this->url."queue/$queueId";
$this->setRequestUrl($url);
$response = $this->send();
return $this->parseResponse($response);
}
/**
* Convenience wrapper to search for queues.
*
* @see self::search()
*/
public function searchQueues($query = '', $orderby = '', $format = 's')
{
return $this->search($query, $orderby, $format, 'queue');
}
/**
* Get metadata of a group
* @param int $groupId
* @param array $fields
* Ask for specific fields to retrieve. (eg Members or Name)
* @return array key=>value response pair array
*/
public function getGroupProperties($groupId, array $fields = array())
{
$url = $this->url."group/$groupId";
if ($fields) {
$url .= '?fields=' . implode(',', $fields);
}
$this->setRequestUrl($url);
$response = $this->send();
return $this->parseResponse($response);
}
/**
* Convenience wrapper to search for groups.
*
* @see self::search()
*/
public function searchGroups($query = '', $orderby = '', $format = 's')
{
return $this->search($query, $orderby, $format, 'group');
}
/**
* Toggles SSL certificate verification.
* @param $verify boolean false to turn off verification, true to enable
*/
public function verifySslCertificates($verify)
{
$this->enableSslVerification = $verify;
}
protected function setRequestUrl($url)
{
$this->requestUrl = $url;
}
protected function setPostFields($data)
{
$this->postFields = $data;
}
private function post($data, $contentType = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->requestUrl);
curl_setopt($ch, CURLOPT_POST, 1);
if (!empty($contentType)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: $contentType"));
}
if (!$this->enableSslVerification) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
}
array_unshift($data, "");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = "";
if ($response===false) {
$error = curl_error($ch);
}
curl_close($ch);
if ($response === false) {
throw new RequestTrackerException("A fatal error occurred when communicating with RT :: ".$error);
}
# Fix for situations in which RT replies like this:
# RT/4.4.1 401 Credentials required
#
# Your username or password is incorrect
if($code == 200 && preg_match('!^RT/\d+\.\d+\.\d+[^\s]*\s+(\d{3})\s+.+?\n\n(.*)\n$!s', $response, $matches)) {
$code = $matches[1];
//$response = $matches[2];
}
if ($code == 401) {
throw new AuthenticationException("The user credentials were refused.");
}
if ($code != 200) {
throw new HttpException("An error occurred : [$code] :: $response");
}
$response = array('code'=>$code, 'body'=>$response);
return $response;
}
/**
* Dont save any stateful information when serializing.
*/
public function __sleep()
{
return array('url', 'user', 'pass', 'enableSslVerification');
}
}
class RequestTrackerException extends Exception
{
}
class AuthenticationException extends RequestTrackerException
{
}
class HttpException extends RequestTrackerException
{
}