-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwebhook.php
343 lines (305 loc) · 9.42 KB
/
webhook.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
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
/**
* A class designed to take care of webhooks.
*
* @author Lauge Rud Knudsen <laugerudknudsen@gmail.com>
* @version V2
*/
class Webhook {
//properties
private $type;
private $name;
private $author;
private $image;
private $id;
private $tags;
private $link;
private $description;
private $settings;
private $video = ' ';
private $dependencies = '';
private $platform = 'pc';
private $superType;
private $curl;
private $payload;
private $shouldPost = true;
//constants
const MESSAGE_SUCCESS = 'The webhook was successfully sent';
const MESSAGE_FAILURE = 'The webhook failed to send';
const MESSAGE_ERROR = 'An error has occured when trying to recieve the message';
const MESSAGE_UNSUPPORTED = 'Webhooks are not supported in this environment';
//getters
/**
* Get the message for posting.
*
* @param bool $value a boolean from the results of post.
* @return string the appropriate message.
*/
public static function getMessage($value) {
return ($value) ? self::MESSAGE_SUCCESS : self::MESSAGE_FAILURE;
}
//setters
//public methods
function __construct($id) {
$model = new Model();
$model->readSingle($id);
$this->id = $id;
foreach ($model->getTags() as $tempTag) {
if (strtolower($tempTag) === 'nsfw') {
$this->shouldPost = false;
}
}
$tags = join(', ' . $model->getTags());
$this->tags = str_replace("@", '@' . json_decode('"\u200b"'), $tags);
$this->description = $this->sanitizeWebhook($model->getDescription());
$this->name = $this->sanitizeWebhook($model->getName());
$this->link = $this->sanitizeWebhook($model->getLink());
$this->type = $model->getType();
$this->superType = getSuperType($this->type);
if (!empty($model->getDiscordId()) && $model->getDiscordId() !== -1) {
$author = new User();
$author->read($model->getDiscordId());
} else {
$author = $model->getAuthor();
}
$this->author = $author;
$this->image = $model->getImage();
//settings
$settingsPath = WEBROOT . '/files/' . $this->type . '/' . $this->id . '/settings.json';
if (file_exists($settingsPath)) {
$this->settings = json_decode(file_get_contents($settingsPath), true);
}
//description
if (isset($this->settings) && $this->settings['embed']['description']) {
$this->video .= $this->settings['embed']['description'];
}
//video
if (isset($this->settings) && $this->settings['embed']['useVideo']) {
if (!empty($this->video)) {
$this->video .= '\n\n';
}
$this->video .= $this->settings['model']['video'];
}
//dependencies
if (isset($this->settings) && !empty($this->settings['model']['dependencies'])) {
$dependencies = '';
$depTypes = ['mods', 'scripts'];
$firstRun = true;
foreach ($depTypes as $depType) {
if (!empty($this->settings['model']['dependencies'][$depType])) {
if ($firstRun) {
$firstRun = false;
} else {
$dependencies .= '\n\n';
}
$dependencies .= '__**' . ucfirst($depType) . '**__';
foreach ($this->settings['model']['dependencies'][$depType] as $depName => $depVersion) {
if (!empty($depName) && !empty($depVersion)) {
if (!empty($depName)) {
$dependencies .= '\n';
}
$dependencies .= '**' . $this->sanitizeWebhook($depName) . '**: ' . $this->sanitizeWebhook($depVersion);
}
}
}
}
$this->dependencies = $dependencies;
}
//fields
//tags
if (!empty($this->tags)) {
$fields[] = [
"name" => "Tags:",
"value" => "$this->tags"
];
}
//features
if (isset($this->settings) && !empty($this->settings['features'])) {
$maxlen = max(array_map('strlen', array_keys($this->settings['features'])));
foreach ($this->settings['features'] as $key => $checkMark) {
$padding = str_repeat(' ', $maxlen - strlen($key));
$features .= $key . ': ' . $padding . ($checkMark)? ':white_check_mark:': ':x:' . '\n';
}
$features = substr($features, 0, -2);
$fields[] = [
"name" => "Features:",
"value" => "$features"
];
}
//dependencies
if (!empty($this->dependencies)) {
$fields[] = [
"name" => "Dependencies:",
"value" => "$this->dependencies"
];
}
if (empty($fields)) {
$fields = [];
}
if (gettype($this->author) == 'object') {
$discordUsername = $this->author->getUsername();
} else {
$discordUsername = $this->author;
}
if (gettype($this->author) == 'object') {
$discordId = $this->author->getDiscordId();
} else {
$discordId = '';
}
if (gettype($this->author) == 'object') {
$discordAvatar = $this->author->getAvatar();
} else {
$discordAvatar = 'https://cdn.discordapp.com/embed/avatars/1.png';
}
$this->payload = json_encode(
[
"content" => "$this->video",
"embeds" => [
[
"title" => "$this->name",
"description" => "$this->description",
"url" => WEBROOT . "/$this->superType/?id=$this->id&$this->platform",
"color" => hexdec('#535aa2'),
"timestamp" => date('Y-m-d h:i:s', $id),
"footer" => [
"icon_url" => WEBROOT . '/resources/manifest/icon-192.png',
"text" => SITECAMEL
],
"thumbnail" => [
"url" => WEBROOT . '/resources/' . $this->type . '-192.png'
],
"image" => [
"url" => "$this->image"
],
"author" => [
"name" => $this->sanitizeWebhook($discordUsername),
"url" => WEBROOT . "/Profile/?user=" . $discordId,
"icon_url" => $discordAvatar . "?size=64"
],
"fields" => $fields,
],
[
"title" => "**Download**",
"url" => "$this->link",
"color" => hexdec('#48f442')
]
]
], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
}
/**
* Post the webhook.
*/
public function post() {
if (!$this->shouldPost) {
return;
}
$password = $this->getPassword();
if (!empty($password) && $password !== FALSE) {
$this->curl = curl_init($password);
$log = fopen(ROOT . '/webhook_queries', 'a');
fwrite($log, $this->payload);
fclose($log);
$header = [
'Content-Type: application/json',
'Content-Length: ' . strlen($this->payload)
];
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->payload);
$result = curl_exec($this->curl);
$log = fopen(ROOT . '/webhook_results', 'a');
fwrite($log, $result);
fclose($log);
$this->setMessage(!empty($result));
}
}
//private methods
/**
* Sanitizes a string.
*
* @param string $string the string to be sanitized.
* @return string the sanitized output.
*/
private function sanitizeWebhook($string) {
if (strpos($string, '@everyone') !== false) {
$string = substr(str_replace("@", '@' . json_decode('"\u200b"'), $string), 1, -1);
}
return $string;
}
/**
* Gets the appropriate password for this webhook.
*
* @return string the appropriate password.
*/
private function getPassword() {
global $helper;
$passwordKey = 'WEBHOOK_' . strtoupper($this->platform) . '_';
switch ($this->platform) {
case 'pc':
switch ($this->type) {
case 'avatar':
$passwordKey .= 'AVATARS';
break;
case 'saber':
$passwordKey .= 'SABERS';
break;
case 'platform':
$passwordKey .= 'PLATFORMS';
break;
case 'bloq':
$passwordKey .= 'NOTES';
break;
}
break;
case 'quest':
switch ($this->type) {
case 'avatar':
$passwordKey .= 'AVATARS';
break;
case 'saber':
$passwordKey .= 'SABERS';
break;
case 'platform':
$passwordKey .= 'PLATFORMS';
break;
case 'bloq':
$passwordKey .= 'NOTES';
break;
// disabled as they aren't used
// case 'trail':
// $passwordKey .= 'TRAIL';
// break;
// case 'sign':
// $passwordKey .= 'SIGN';
// break;
case 'misc':
$passwordKey .= 'MISC';
break;
}
break;
}
return $helper->setting($passwordKey);
}
/**
* Saves a message with the status of the webhook to the session.
*
* @param int $messageCode the message code.
*/
private function setMessage($messageCode) {
$output = self::MESSAGE_ERROR;
switch ($messageCode) {
case 0:
$output = self::MESSAGE_FAILURE;
break;
case 1:
$output = self::MESSAGE_SUCCESS;
break;
case 2:
$output = self::MESSAGE_UNSUPPORTED;
break;
}
$_SESSION['webhookMessage'] = $output;
}
}