-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcode.php
More file actions
541 lines (460 loc) · 19.6 KB
/
code.php
File metadata and controls
541 lines (460 loc) · 19.6 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
<?php
/**
* Copyright notice
*
* (c) 2019 Harald Holzmann <harald@varioous.at>, varioous OG
*
* All rights reserved
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script 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.
*
* This copyright notice MUST APPEAR in all copies of the script!
*/
class OpenProject
{
//role id for new users that are created with this script
const SUPPORT_ROLE_ID = 0;
//role id of admin
const ADMIN_ROLE_ID = 0;
//user id, every ticket is assigned to this user
const SUPPORT_DEFAULT_ASSIGN_USER = 0;
//default user for job tickets
const JOBS_DEFAULT_ASSIGN_USER = 0;
//project id for job tickets
const JOBS_DEFAULT_PROJECT = 0;
//open project api key
const OPEN_PROJECT_API_KEY = "xxxxxxxxx";
//api version to use
const OPEN_PROJECT_API_VERSION = 3;
//url of the open project api
const OPEN_PROJECT_API_URL = "https://projects.varioous.at/api/v" . self::OPEN_PROJECT_API_VERSION . "/";
//slack tocken to send message
const SLACK_TOKEN = "xxxxxxxxx";
//slack api url
const SLACK_API_URL = "https://slack.com/api/chat.postMessage";
//rocket chat api url
const ROCKET_CHAT_API_URL = "xxxxxxxxxxxx";
//rocket chat room id
const ROCKET_CHAT_ROOM_ID = "xxxx";
//rocket chat auth token
const ROCKET_CHAT_AUTH_TOKEN = "sxxxxxxxxxxxxx";
//rocket chat user id
const ROCKET_CHAT_USER_ID = "xxxxxxxxxx";
public static function createJobTicket($mail, $mailInfo)
{
//data for ticket
$ticketData = array();
//type
$ticketData['_type'] = 'WorkPackage';
$subject = 'Jobanfrage';
if (!is_null($mailInfo->subject) && strlen($mailInfo->subject) > 0) {
//subject
$subject = $mailInfo->subject;
}
$ticketData['subject'] = $subject;
//description
$descriptionArray = array();
$descriptionArray['format'] = "textile";
$descriptionArray['raw'] = "Absender-Mail-Adresse: " . $mailInfo->fromAddress . "\n";
$descriptionArray['raw'] .= "Absender-Name: " . $mailInfo->fromName . "\n";
$descriptionArray['raw'] .= " ............................. " . "\n\n";
$descriptionArray['raw'] .= $mail->textPlain;
$ticketData['description'] = $descriptionArray;
//assignee
$assigneeArray = array();
$assigneeArray['href'] =
"/api/v" . self::OPEN_PROJECT_API_VERSION . "/users/" . intval(self::JOBS_DEFAULT_ASSIGN_USER);
$ticketData['assignee'] = $assigneeArray;
//assign project data
$projectArray = array();
$projectArray['href'] = "/api/v" . self::OPEN_PROJECT_API_VERSION . "/projects/" . self::JOBS_DEFAULT_PROJECT;
$ticketData['project'] = $projectArray;
//send request
$ch = curl_init(self::OPEN_PROJECT_API_URL . 'work_packages');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_USERPWD, 'apikey:' . self::OPEN_PROJECT_API_KEY);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($ticketData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_TIMEOUT, 200);
$response = curl_exec($ch);
curl_close($ch);
$respArray = json_decode($response);
if (is_null($respArray->id)) {
//send mail that creation failed
return -1;
}
$workPackageId = $respArray->id;
//attachments
foreach ($mail->getAttachments() as $attachment) {
self::createAttachement($attachment, $workPackageId);
}
return $respArray->id;
}
public static function createTicket($mail, $mailInfo)
{
//data for ticket
$ticketData = array();
//type
$ticketData['_type'] = 'WorkPackage';
$subject = 'Supportanfrage';
if (!is_null($mailInfo->subject) && strlen($mailInfo->subject) > 0) {
//subject
$subject = $mailInfo->subject;
}
$ticketData['subject'] = $subject;
//description
$descriptionArray = array();
$descriptionArray['format'] = "textile";
$descriptionArray['raw'] = "Ersteller-Mail-Adresse: " . $mailInfo->fromAddress . "\n";
$descriptionArray['raw'] .= "Ersteller-Name: " . $mailInfo->fromName . "\n";
$descriptionArray['raw'] .= " ............................. " . "\n\n";
$descriptionArray['raw'] .= $mail->textPlain;
$ticketData['description'] = $descriptionArray;
//assignee
$assigneeArray = array();
$assigneeArray['href'] =
"/api/v" . self::OPEN_PROJECT_API_VERSION . "/users/" . intval(self::SUPPORT_DEFAULT_ASSIGN_USER);
$ticketData['assignee'] = $assigneeArray;
//sender, first check if user or email already exists
$user = self::getUser($mailInfo->fromAddress);
if (intval($user->count) == 0) {
//create user
$user = self::createUser($mailInfo);
$userId = $user->id;
} else {
$userId = $user->_embedded->elements[0]->id;
}
//check if user already has a support project
$supportProjectId = -1;
$projects = self::getAllProjects();
foreach ($projects->_embedded->elements as $project) {
$availableAssignees = self::getProjectAvailableAssignees($project->id);
foreach ($availableAssignees->_embedded->elements as $possibleAssignees) {
if (intval($possibleAssignees->id) == intval($userId)) {
if (strpos($project->identifier, 'support') !== false) {
$supportProjectId = $project->id;
}
}
}
}
//check if support project found
if ($supportProjectId == -1) {
//create new project
$projectName = explode("@", $mailInfo->fromAddress, 2);
$projectIdentifier =
strtolower('created-support-' . preg_replace('/[^a-z]/i', '', $projectName[0]) . '-' . rand(1, 99));
self::createProject($projectIdentifier, 'Support - ' . ucfirst($projectName[0]));
//load project id
$projectsNew = self::getAllProjects();
foreach ($projectsNew->_embedded->elements as $projectVal) {
if (stripos($projectVal->identifier, $projectIdentifier) !== false) {
$supportProjectId = $projectVal->id;
}
}
//add user to project as member
self::addProjectMembership($supportProjectId, $userId, self::SUPPORT_ROLE_ID);
//also add default user to project as member
self::addProjectMembership($supportProjectId, self::SUPPORT_DEFAULT_ASSIGN_USER, self::ADMIN_ROLE_ID);
}
//assign project data
$projectArray = array();
$projectArray['href'] = "/api/v" . self::OPEN_PROJECT_API_VERSION . "/projects/" . $supportProjectId;
$ticketData['project'] = $projectArray;
//send request
$ch = curl_init(self::OPEN_PROJECT_API_URL . 'work_packages?notify=false');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_USERPWD, 'apikey:' . self::OPEN_PROJECT_API_KEY);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($ticketData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_TIMEOUT, 200);
$response = curl_exec($ch);
curl_close($ch);
$respArray = json_decode($response);
if (is_null($respArray->id)) {
//send mail that creation failed
return -1;
}
$workPackageId = $respArray->id;
//attachments
foreach ($mail->getAttachments() as $attachment) {
self::createAttachement($attachment, $workPackageId);
}
//set user as observer
self::addUserToProjectAsWatcher($workPackageId, $userId);
return $respArray->id;
}
public static function addProjectMembership($projectId, $userId, $roleId)
{
//project data
$projectArray = array();
$projectArray['href'] = '/api/v' . self::OPEN_PROJECT_API_VERSION . '/projects/' . intval($projectId);
//user data
$userArray = array();
$userArray['href'] = '/api/v' . self::OPEN_PROJECT_API_VERSION . '/users/' . intval($userId);
//roles data
$rolesArray = array();
$roleArray = array();
$roleArray['href'] = '/api/v' . self::OPEN_PROJECT_API_VERSION . '/roles/' . intval($roleId);
array_push($rolesArray, $roleArray);
//membership data
$membershipData = array();
$membershipData['project'] = $projectArray;
$membershipData['principal'] = $userArray;
$membershipData['roles'] = $rolesArray;
//send request
$ch = curl_init(self::OPEN_PROJECT_API_URL . 'memberships');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_USERPWD, 'apikey:' . self::OPEN_PROJECT_API_KEY);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($membershipData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_TIMEOUT, 200);
curl_exec($ch);
curl_close($ch);
}
public static function createUser($mailInfo)
{
//data for user
$userData = array();
$userData['login'] = $mailInfo->fromAddress;
$userData['email'] = $mailInfo->fromAddress;
$userData['status'] = 'invited';
$name = explode(' ', $mailInfo->fromName);
$userData['firstName'] = $name[0];
if (count($name) >= 2) {
$userData['lastName'] = $name[1];
}
//send request
$ch = curl_init(self::OPEN_PROJECT_API_URL . 'users');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_USERPWD, 'apikey:' . self::OPEN_PROJECT_API_KEY);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_TIMEOUT, 200);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response);
}
public static function addUserToProjectAsWatcher($workPackageId, $userId)
{
//description
$userData = array();
$userArray = array();
$userArray['href'] = '/api/v' . self::OPEN_PROJECT_API_VERSION . '/users/' . intval($userId);
$userData['user'] = $userArray;
//send request
$ch = curl_init(self::OPEN_PROJECT_API_URL . 'work_packages/' . intval($workPackageId)
. '/watchers?notify=false');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_USERPWD, 'apikey:' . self::OPEN_PROJECT_API_KEY);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_TIMEOUT, 200);
curl_exec($ch);
curl_close($ch);
}
public static function addCommentToWorkPackage($workPackageId, $mail, $mailInfo)
{
$commentBody = "Ersteller-Mail-Adresse: " . $mailInfo->fromAddress . "\n";
$commentBody .= "Ersteller-Name: " . $mailInfo->fromName . "\n";
$commentBody .= " ............................. " . "\n";
$commentBody .= $mail->textPlain;
//note
$commentData = array();
$commentBodyDataArray = array();
$commentBodyDataArray['raw'] = $commentBody;
$commentData['comment'] = $commentBodyDataArray;
//send request
$ch = curl_init(self::OPEN_PROJECT_API_URL . 'work_packages/' . intval($workPackageId) . '/activities');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_USERPWD, 'apikey:' . self::OPEN_PROJECT_API_KEY);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($commentData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_TIMEOUT, 200);
curl_exec($ch);
curl_close($ch);
}
public static function createProject($identifier, $name)
{
//data for project
$projectData = array();
$projectData['identifier'] = $identifier;
$projectData['name'] = $name;
//send request
$ch = curl_init(self::OPEN_PROJECT_API_URL . 'projects');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_USERPWD, 'apikey:' . self::OPEN_PROJECT_API_KEY_BOT);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($projectData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_TIMEOUT, 200);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response);
}
public static function getUser($mail)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => 'apikey:' . self::OPEN_PROJECT_API_KEY,
CURLOPT_URL => self::OPEN_PROJECT_API_URL
. "users?filters=[{%22login%22:{%22operator%22:%20%22=%22,%22values%22:[%22" . $mail . "%22]}}]"
));
$resp = curl_exec($curl);
curl_close($curl);
return json_decode($resp);
}
public static function getWorkPackage($id)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => 'apikey:' . self::OPEN_PROJECT_API_KEY,
CURLOPT_URL => self::OPEN_PROJECT_API_URL . "work_packages/" . intval($id)
));
$resp = curl_exec($curl);
curl_close($curl);
return json_decode($resp);
}
public static function getAllProjects()
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_USERPWD => 'apikey:' . self::OPEN_PROJECT_API_KEY,
CURLOPT_URL => self::OPEN_PROJECT_API_URL . "projects"
));
$resp = curl_exec($curl);
curl_close($curl);
return json_decode($resp);
}
public static function getProjectAvailableAssignees($projectId)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => 'apikey:' . self::OPEN_PROJECT_API_KEY,
CURLOPT_URL => self::OPEN_PROJECT_API_URL . "projects/" . $projectId . "/available_assignees"
));
$resp = curl_exec($curl);
curl_close($curl);
return json_decode($resp);
}
public static function sendMessageToRocketChat($message)
{
$data = array("message" => ["rid" => self::ROCKET_CHAT_ROOM_ID, "msg" => $message]);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => self::ROCKET_CHAT_API_URL,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => array(
"X-Auth-Token: ".self::ROCKET_CHAT_AUTH_TOKEN,
"X-User-Id: ".self::ROCKET_CHAT_USER_ID,
"Content-type: application/json"
),
));
curl_exec($curl);
curl_close($curl);
}
public static function sendMessageToSlack($message)
{
$ch = curl_init(self::SLACK_API_URL);
$data = http_build_query([
"token" => self::SLACK_TOKEN,
"channel" => "#varioous_support",
"text" => $message,
"username" => "Open-Project-Bot",
]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
curl_close($ch);
}
public static function createAttachement($attachement, $ticketId)
{
$curl = curl_init(self::OPEN_PROJECT_API_URL . 'work_packages/' . $ticketId . '/attachments');
$boundary = '----WebKitFormBoundary' . uniqid();
$post_data = self::build_data_files($attachement, $boundary);
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => 'apikey:' . self::OPEN_PROJECT_API_KEY,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_TIMEOUT => 200,
CURLOPT_HTTPHEADER => array(
"Content-Type: multipart/form-data; boundary=" . $boundary
),
));
curl_exec($curl);
curl_close($curl);
}
private static function build_data_files($attachment, $delimeter)
{
$attachmentData = array();
$attachmentData['fileName'] = $attachment->name;
$data = '';
$eol = "\r\n";
$data .= "--" . $delimeter . $eol;
$data .= 'Content-Disposition: form-data; name="metadata"' . $eol . $eol;
$data .= json_encode($attachmentData) . $eol;
$data .= "--" . $delimeter . $eol . 'Content-Disposition: form-data; name="file"; filename="'
. $attachment->name . '"' . $eol . 'Content-Type: ' . mime_content_type($attachment->filePath) . $eol;
$data .= $eol;
$data .= file_get_contents($attachment->filePath) . $eol;
$data .= "--" . $delimeter . "--";
return $data;
}
}