forked from rootslinux/phpbb-discord-notifications
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnotification_service.php
412 lines (365 loc) · 13.2 KB
/
notification_service.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
<?php
/**
* Discord Notifications. An extension for the phpBB Forum Software package.
*
* @copyright (c) 2018, Tyler Olsen, https://github.com/rootslinux
* @license GNU General Public License, version 2 (GPL-2.0)
*/
namespace mober\discordnotifications;
/**
* Contains the core logic for formatting and sending notification message to Discord.
* This includes common utilities, such as verifying notification configuration settings
* and a few DB queries.
*/
class notification_service
{
// Maximum number of characters allowed by Discord in a message description.
// Reference: https://discordapp.com/developers/docs/resources/channel#embed-limits
const MAX_MESSAGE_SIZE = 2048;
// Maximum number of characters (not bytes) allowed by Discord in a message footer.
const MAX_FOOTER_SIZE = 2048;
// The notification color (gray) to use as a default if a missing or invalid color value is received.
const DEFAULT_COLOR = 11777212;
const ELLIPSIS = '…';
/** @var \phpbb\config\config */
protected $config;
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var \phpbb\log\log $log */
protected $log;
/**
* Constructor
*
* @param \phpbb\config\config $config
* @param \phpbb\db\driver\driver_interface $db
*/
public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\log\log $log)
{
$this->config = $config;
$this->db = $db;
$this->log = $log;
}
/**
* Check whether notifications are enabled for a certain type
*
* @param string $notification_type The name of the notification type to check
* @return bool if the global notification setting is disabled or this notification type is disabled
*/
public function is_notification_type_enabled($notification_type)
{
return $this->config['discord_notifications_enabled'] == 1 && $this->config[$notification_type] == 1;
}
/**
* Check whether notifications that occur on a specific forum should be generated
*
* @param int $forum_id The ID of the forum to check
* @return string|false
*/
public function get_forum_notification_url($forum_id)
{
global $table_prefix;
if (!is_numeric($forum_id))
{
return false;
}
if ($this->config['discord_notifications_enabled'] == 0)
{
return false;
}
// Query the forum table where forum notification settings are stored
$sql = "SELECT discord_notifications FROM " . FORUMS_TABLE . " WHERE forum_id = " . (int) $forum_id;
$result = $this->db->sql_query($sql);
$data = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if ($data['discord_notifications'])
{
$sql = "SELECT url FROM {$table_prefix}discord_webhooks WHERE alias = '" . $this->db->sql_escape($data['discord_notifications']) . "'";
$result = $this->db->sql_query($sql);
$data2 = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
return $data2['url'];
}
return false;
}
/**
* Retrieve the value for the ACP settings configuration related to post preview length
*
* @return int The number of characters to display in the post preview. A zero value indicates that no preview
* should be displayed
*/
public function get_post_preview_length()
{
return $this->config['discord_notifications_post_preview_length'];
}
/**
* Retrieves the name of a forum from the database when given an ID
*
* @param int $forum_id The ID of the forum to query
* @return string|false The name of the forum, or false if not found
*/
public function query_forum_name($forum_id)
{
if (!is_numeric($forum_id))
{
return null;
}
$sql = "SELECT forum_name from " . FORUMS_TABLE . " WHERE forum_id = " . (int) $forum_id;
$result = $this->db->sql_query($sql);
$data = $this->db->sql_fetchfield('forum_name');
$this->db->sql_freeresult($result);
return $data;
}
/**
* Retrieves the subject of a post from the database when given an ID
*
* @param int $post_id The ID of the post to query
* @return null|string The subject of the post, or NULL if not found
*/
public function query_post_subject($post_id)
{
if (!is_numeric($post_id))
{
return null;
}
$sql = "SELECT post_subject from " . POSTS_TABLE . " WHERE post_id = " . (int) $post_id;
$result = $this->db->sql_query($sql);
$data = $this->db->sql_fetchrow($result);
$subject = $data['post_subject'];
$this->db->sql_freeresult($result);
return $subject;
}
/**
* Retrieves the title of a topic from the database when given an ID
*
* @param int $topic_id The ID of the topic to query
* @return string|false The name of the topic, or false if not found
*/
public function query_topic_title($topic_id)
{
if (!is_numeric($topic_id))
{
return null;
}
$sql = "SELECT topic_title from " . TOPICS_TABLE . " WHERE topic_id = " . (int) $topic_id;
$result = $this->db->sql_query($sql);
$data = $this->db->sql_fetchfield('topic_title');
$this->db->sql_freeresult($result);
return $data;
}
/**
* Runs a query to fetch useful data about a specific forum topic. The return data includes information on the
* first poster, number of posts, which forum contains the topic, and more.
*
* @param int $topic_id The ID of the topic to query
* @return array containing data about the topic and the forum it is contained in
*/
public function query_topic_details($topic_id)
{
if (!is_numeric($topic_id))
{
return [];
}
$sql = "SELECT
f.forum_id, f.forum_name,
t.topic_id, t.topic_title, t.topic_poster, t.topic_first_post_id, t.topic_first_poster_name, t.topic_posts_approved, t.topic_visibility
FROM " . FORUMS_TABLE . " f, " . TOPICS_TABLE . " t
WHERE t.forum_id = f.forum_id and t.topic_id = " . (int) $topic_id;
$result = $this->db->sql_query($sql);
$data = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
return $data;
}
/**
* Retrieves the name of a user from the database when given an ID
*
* @param int $user_id The ID of the user to query
* @return string|false The name of the user, or false if not found
*/
public function query_user_name($user_id)
{
if (!is_numeric($user_id))
{
return false;
}
$sql = "SELECT username from " . USERS_TABLE . " WHERE user_id = " . (int) $user_id;
$result = $this->db->sql_query($sql);
$data = $this->db->sql_fetchfield('username');
$this->db->sql_freeresult($result);
return $data;
}
/**
* Sends a notification message to Discord. This function checks the master switch configuration for the extension,
* but does no further checks. The caller is responsible for performing full validation of the notification prior
* to calling this function.
*
* @param string $color The color to use in the notification (decimal value of a hexadecimal RGB code)
* @param string $message The message text to send.
* @param null|string $webhook_url
* @param null|string $footer Text to place in the footer of the message. Optional.
*/
public function send_discord_notification($color, $message, $webhook_url = null, $title = null, $preview = null, $footer = null)
{
global $table_prefix;
if ($this->config['discord_notifications_enabled'] == 0 || !isset($message))
{
return;
}
// Note that the value stored in the config table will always be a valid URL when discord_notifications_enabled is set
if (is_null($webhook_url))
{
$default = $this->config['discord_notification_default_webhook'];
if ($default)
{
$sql = "SELECT url FROM {$table_prefix}discord_webhooks WHERE alias = '" . $this->db->sql_escape($default) . "'";
$result = $this->db->sql_query($sql);
$data = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
$webhook_url = $data['url'];
}
}
$this->execute_discord_webhook($webhook_url, $color, $message, $title, $preview, $footer);
}
/**
* Sends a message to Discord, disregarding any configurations that are currently set. This method is primarily
* used by users to test their notifications from the ACP.
*
* @param string $discord_webhook_url The URL of the Discord webhook to transmit the message to. If this is an
* invalid URL, no message will be sent.
* @param string $message The message text to send. Must be a non-empty string.
* @return bool indicating whether the message transmission resulted in success or failure.
*/
public function force_send_discord_notification($discord_webhook_url, $message)
{
if (!filter_var($discord_webhook_url, FILTER_VALIDATE_URL) || !is_string($message) || $message == '')
{
return false;
}
return $this->execute_discord_webhook($discord_webhook_url, self::DEFAULT_COLOR, $message);
}
/**
* Helper function that performs the message transmission. This method checks the inputs to prevent any problematic
* characters in strings. Note that this function checks that the message and footer do not exceed the maximum
* allowable limits by the Discord API, but it does -not- check configuration settings such as the
* post_preview_length. The code invoking this method is responsible for checking those settings.
*
* @param string $discord_webhook_url The URL of the Discord webhook to transmit the message to.
* @param string $color Color to set for the message. Should be a positive non-zero integer
* representing a hex color code.
* @param string $message The message text to send. Must be a non-empty string.
* @param string $title Title of the preview (for example "Preview" or "Reason"). Optional.
* @param string $preview Content of the preview. Optional.
* @param string $footer The text to place in the footer. Optional.
* @return bool indicating whether the message transmission resulted in success or failure.
* @see https://discordapp.com/developers/docs/resources/webhook#execute-webhook
* @see https://discord.com/developers/docs/resources/channel#embed-object
*/
private function execute_discord_webhook($discord_webhook_url, $color, $message, $title = null, $preview = null, $footer = null)
{
if (!isset($discord_webhook_url) || $discord_webhook_url === '')
{
return false;
}
if (!is_integer($color) || $color < 0)
{
// Use the default color if we did not receive a valid color value
$color = self::DEFAULT_COLOR;
}
if (!is_string($message) || $message == '')
{
return false;
}
if (isset($footer) && (!is_string($footer) || $footer == ''))
{
return false;
}
// Clean up the message and footer text before sending by trimming whitespace from the front and end of the message and footer strings.
$message = trim($message);
if (isset($footer))
{
$footer = trim($footer);
}
// Abort if we find that either of our text fields are now empty strings
if ($message === '')
{
return false;
}
if (isset($footer) && $footer === '')
{
return false;
}
// Verify that the message and footer size is within the allowable limit and truncate if necessary. We add "..." as the last three characters
// when we require truncation.
if (mb_strlen($message) > self::MAX_MESSAGE_SIZE)
{
$message = mb_substr($message, 0, self::MAX_MESSAGE_SIZE - 1) . self::ELLIPSIS;
}
if (isset($footer) && mb_strlen($footer) > self::MAX_FOOTER_SIZE)
{
$footer = mb_substr($footer, 0, self::MAX_FOOTER_SIZE - 1) . self::ELLIPSIS;
}
// Place the message inside the JSON structure that Discord expects to receive at the REST endpoint.
$embed = [
'timestamp' => date('c', time()),
'color' => $color,
'description' => $message,
];
if (isset($footer))
{
$embed['footer'] = [
'text' => $footer,
];
}
if (isset($title) && isset($preview))
{
$title = trim($title);
$preview = trim($preview);
if ($title !== '' && $preview !== '')
{
$embed['fields'] = [
[
'name' => $title,
'value' => $preview,
'inline' => false,
],
];
}
}
$payload = [
'embeds' => [$embed],
];
$json = \json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
if ($json === false)
{
$this->log->add('admin', ANONYMOUS, '127.0.0.1', 'ACP_DISCORD_NOTIFICATIONS_JSON_ERROR', time(), [json_last_error_msg()]);
return false;
}
// Use the CURL library to transmit the message via a POST operation to the webhook URL.
$h = curl_init();
curl_setopt($h, CURLOPT_HTTPHEADER, ['Content-type: application/json']);
curl_setopt($h, CURLOPT_URL, $discord_webhook_url);
curl_setopt($h, CURLOPT_POST, 1);
curl_setopt($h, CURLOPT_POSTFIELDS, $json);
curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($h, CURLOPT_CONNECTTIMEOUT, (int) $this->config['discord_notifications_connect_timeout']);
curl_setopt($h, CURLOPT_TIMEOUT, (int) $this->config['discord_notifications_exec_timeout']);
$response = curl_exec($h);
$error = curl_errno($h);
$status = curl_getinfo($h, CURLINFO_HTTP_CODE);
curl_close($h);
// TODO: Retry?
if ($error == 0)
{
if ($status < 200 || $status > 299)
{
$this->log->add('admin', ANONYMOUS, '127.0.0.1', 'ACP_DISCORD_NOTIFICATIONS_WEBHOOK_ERROR', time(), [$status]);
return false;
}
}
else
{
$this->log->add('admin', ANONYMOUS, '127.0.0.1', 'ACP_DISCORD_NOTIFICATIONS_CURL_ERROR', time(), [$error]);
return false;
}
return true;
}
}