-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.php
executable file
·134 lines (114 loc) · 4.44 KB
/
config.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
<?php
require_once INCLUDE_DIR . 'class.forms.php';
class SevenPluginConfig extends PluginConfig {
public const KEY_API_KEY = 'apiKey';
public const KEY_ON_NEW_TICKET = 'onNewTicket';
public const KEY_EVENT_MAIL_GROUP_ID = 'eventTemplateId';
function translate(): array {
if (!method_exists('Plugin', 'translate')) { // Provide translation compatibility osTicket < v1.9.4
return [
function ($x) {
return $x;
},
function ($x, $y, $n) {
return $n != 1 ? $y : $x;
},
];
}
return Plugin::translate('seven');
}
public function getApiKey(): ?string {
return $this->get(self::KEY_API_KEY);
}
public function getEventMailGroupId(): ?int {
return $this->get(self::KEY_EVENT_MAIL_GROUP_ID);
}
public function getOnNewTicket(): bool {
return (bool)$this->get(self::KEY_ON_NEW_TICKET);
}
function getOptions(): array {
[$__] = self::translate();
$cfg = new OsticketConfig;
$primaryLanguage = $cfg->getPrimaryLanguage();
$sql=sprintf('SELECT tpl.*,count(dept.tpl_id) as depts '.
'FROM '.EMAIL_TEMPLATE_GRP_TABLE.' tpl '.
'LEFT JOIN '.DEPT_TABLE.' dept USING(tpl_id) '.
'WHERE isactive=1 AND lang=\'%s\' GROUP BY tpl.tpl_id', $primaryLanguage);
$res=db_query($sql);
$tplChoices = [];
if($res) {
while ($row = db_fetch_array($res)) {
$tplChoices[$row['tpl_id']] = $row['name'];
}
}
return [
'credentials' => new SectionBreakField([
'label' => $__('seven Credentials'),
]),
self::KEY_API_KEY => new PasswordField([
'configuration' => [
'autofocus' => !$this->getApiKey(),
'length' => 90,
'size' => 90,
],
'hint' => $__('Can be created in your seven dashboard.'),
'label' => $__('API Key'),
'required' => !$this->getApiKey(),
'validator' => 'noop',
]),
'events' => new SectionBreakField([
'label' => $__('Events'),
]),
self::KEY_EVENT_MAIL_GROUP_ID => new ChoiceField([
'choices' => $tplChoices,
'hint' => $__('Mail template used for event-based SMS dispatch.'),
'label' => $__('Select Mail Template'),
]),
self::KEY_ON_NEW_TICKET => new BooleanField([
'hint' => $__('Sends an SMS to the associated agent after a new ticket got submitted.'),
'label' => $__('Send SMS on new Ticket?'),
]),
];
}
public function request(string $endpoint, array $body, string $apiKey = null): mixed {
$apiKey = $apiKey ?? $this->getApiKey();
$headers = [
'Accept: application/json',
'Content-type: application/json',
'X-Api-Key: ' . $apiKey,
];
$ch = curl_init('https://gateway.seven.io/api/' . $endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (!empty($body)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
}
$json = curl_exec($ch);
curl_close($ch);
try {
$json = json_decode($json, null, 512, JSON_THROW_ON_ERROR);
if (is_int($json)) return null;
} catch (JsonException) {
return null;
}
return $json;
}
public function sms(array $body): mixed {
return $this->request('sms', $body);
}
function pre_save(&$config, &$errors): bool {
global $msg;
[$__] = self::translate();
$apiKey = &$config[self::KEY_API_KEY];
$json = $this->request('balance', [], $apiKey);
$invalidKeyErrorMessage = $__('The API key seems to be invalid.');
if ($json === null) {
$apiKey = '';
$errors['err'] = $invalidKeyErrorMessage;
return false;
}
if (!$errors) $msg = $__('Configuration updated successfully');
return true;
}
}