-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathnotification_service.php
340 lines (302 loc) · 11.6 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
<?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 roots\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 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;
/** @var \phpbb\config\config */
protected $config;
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/**
* 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)
{
$this->config = $config;
$this->db = $db;
}
/**
* Check whether notifications are enabled for a certain type
* @param $notification_type The name of the notification type to check
* @return False if the global notification setting is disabled or this notification type is disabled
*/
public function is_notification_type_enabled($notification_type)
{
// Also check the global extension enabled setting. We don't generate any notifications if this is disabled
if ($this->config['discord_notifications_enabled'] == 1 && $this->config[$notification_type] == 1)
{
return true;
}
return false;
}
/**
* Check whether notifications that occur on a specific forum should be generated
* @param $forum_id The ID of the forum to check
* @return False if the global notification setting is disabled, notifications are disabled for the forum, or no forum exists with this ID
*/
public function is_notification_forum_enabled($forum_id)
{
if (is_numeric($forum_id) == false)
{
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_enabled FROM " . FORUMS_TABLE . " WHERE forum_id = $forum_id";
$result = $this->db->sql_query($sql);
$data = $this->db->sql_fetchrow($result);
$enabled = $data['discord_notifications_enabled'] == 1 ? true : false;
$this->db->sql_freeresult($result);
return $enabled;
}
/**
* Retrieve the value for the ACP settings configuration related to post preview length
* @return 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 $forum_id The ID of the forum to query
* @return The name of the forum, or NULL if not found
*/
public function query_forum_name($forum_id)
{
if (is_numeric($forum_id) == false)
{
return null;
}
$sql = "SELECT forum_name from " . FORUMS_TABLE . " WHERE forum_id = $forum_id";
$result = $this->db->sql_query($sql);
$data = $this->db->sql_fetchrow($result);
$name = $data['forum_name'];
$this->db->sql_freeresult($result);
return $name;
}
/**
* Retrieves the subject of a post from the database when given an ID
* @param $post_id The ID of the post to query
* @return The subject of the post, or NULL if not found
*/
public function query_post_subject($post_id)
{
if (is_numeric($post_id) == false)
{
return null;
}
$sql = "SELECT post_subject from " . POSTS_TABLE . " WHERE post_id = $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 $topic_id The ID of the topic to query
* @return The name of the topic, or NULL if not found
*/
public function query_topic_title($topic_id)
{
if (is_numeric($topic_id) == false)
{
return null;
}
$sql = "SELECT topic_title from " . TOPICS_TABLE . " WHERE topic_id = $topic_id";
$result = $this->db->sql_query($sql);
$data = $this->db->sql_fetchrow($result);
$title = $data['topic_title'];
$this->db->sql_freeresult($result);
return $title;
}
/**
* 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 $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) == false)
{
return array();
}
$topic_table = TOPICS_TABLE;
$forum_table = FORUMS_TABLE;
$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
$forum_table f, $topic_table t
WHERE
t.forum_id = f.forum_id and t.topic_id = $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 $user_id The ID of the user to query
* @return The name of the user, or NULL if not found
*/
public function query_user_name($user_id)
{
if (is_numeric($user_id) == false)
{
return null;
}
$sql = "SELECT username from " . USERS_TABLE . " WHERE user_id = $user_id";
$result = $this->db->sql_query($sql);
$data = $this->db->sql_fetchrow($result);
$name = $data['username'];
$this->db->sql_freeresult($result);
return $name;
}
/**
* 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 $color The color to use in the notification (decimal value of a hexadecimal RGB code)
* @param $message The message text to send.
* @param $footer Text to place in the footer of the message. Optional.
*/
public function send_discord_notification($color, $message, $footer = NULL)
{
if ($this->config['discord_notifications_enabled'] == 0 || isset($message) == false)
{
return;
}
// Note that the value stored in the config table will always be a valid URL when discord_notifications_enabled is set
$discord_webhook_url = $this->config['discord_notifications_webhook_url'];
$this->execute_discord_webhook($discord_webhook_url, $color, $message, $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 $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 $message The message text to send. Must be a non-empty string.
* @return Boolean 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) == false || $message == '')
{
return false;
}
return $this->execute_discord_webhook($discord_webhook_url, self::DEFAULT_COLOR, $message, NULL);
}
/**
* 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 $discord_webhook_url The URL of the Discord webhook to transmit the message to.
* @param $color Color to set for the message. Should be a positive non-zero integer representing a hex color code.
* @param $message The message text to send. Must be a non-empty string.
* @param $footer The text to place in the footer. Optional. Must be a non-empty string.
* @return Boolean indicating whether the message transmission resulted in success or failure.
* @see https://discordapp.com/developers/docs/resources/webhook#execute-webhook
*/
private function execute_discord_webhook($discord_webhook_url, $color, $message, $footer = NULL)
{
if (isset($discord_webhook_url) == false || $discord_webhook_url === '')
{
return false;
}
if (is_integer($color) == false || $color < 0)
{
// Use the default color if we did not receive a valid color value
$color = self::DEFAULT_COLOR;
}
if (is_string($message) == false || $message == '')
{
return false;
}
if (isset($footer) == true && (is_string($footer) == false || $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);
$message = str_replace('"', "'", $message); // Replace " characters that would break the JSON encoding that our message must be wrapped in.
if (isset($footer))
{
$footer = trim($footer);
$footer = str_replace('"', "'", $footer);
// Discord does not appear to allow newline characters in the footer. In fact, the presence of them causes the message POST
// to fail. Hence why we replace all newlines with a space here.
$footer = str_replace(array("\r", "\n"), ' ', $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 (strlen($message) > self::MAX_MESSAGE_SIZE)
{
$message = substr($message, 0, self::MAX_MESSAGE_SIZE - 3) . '...';
}
if (isset($footer))
{
if (strlen($footer) > self::MAX_FOOTER_SIZE)
{
$footer = substr($footer, 0, self::MAX_FOOTER_SIZE - 3) . '...';
}
}
// Place the message inside the JSON structure that Discord expects to receive at the REST endpoint.
$post = '';
if (isset($footer))
{
$post = sprintf('{"embeds": [{"color": "%d", "description" : "%s", "footer": {"text": "%s"}}]}', $color, $message, $footer);
}
else {
$post = sprintf('{"embeds": [{"color": "%d", "description" : "%s"}]}', $color, $message);
}
// Use the CURL library to transmit the message via a POST operation to the webhook URL.
$h = curl_init();
curl_setopt($h, CURLOPT_URL, $discord_webhook_url);
curl_setopt($h, CURLOPT_POST, 1);
curl_setopt($h, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($h);
curl_close($h);
// Check if the response was not successful
if (is_array($response) && $response['message'])
{
// TODO: If the response includes a message then an error has occurred. Determine whether we want to log it, queue it up to try again, etc.
return false;
}
return true;
}
}