diff --git a/acp/discord_notifications_info.php b/acp/discord_notifications_info.php index 27cf0e8..0283d96 100644 --- a/acp/discord_notifications_info.php +++ b/acp/discord_notifications_info.php @@ -2,8 +2,8 @@ /** * Discord Notifications 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) + * @copyright (c) 2018, Tyler Olsen, https://github.com/rootslinux + * @license GNU General Public License, version 2 (GPL-2.0) */ namespace mober\discordnotifications\acp; @@ -15,16 +15,26 @@ class discord_notifications_info { public function module() { - return array( + return [ 'filename' => '\mober\discordnotifications\acp\discord_notifications_module', 'title' => 'ACP_DISCORD_NOTIFICATIONS', - 'modes' => array( - 'settings' => array( - 'title' => 'ACP_DISCORD_NOTIFICATIONS_TITLE', + 'modes' => [ + 'settings' => [ + 'title' => 'ACP_DISCORD_NOTIFICATIONS_SETTINGS', 'auth' => 'ext_mober/discordnotifications && acl_a_board', - 'cat' => array('ACP_DISCORD_NOTIFICATIONS') - ), - ), - ); + 'cat' => ['ACP_DISCORD_NOTIFICATIONS'], + ], + 'webhooks' => [ + 'title' => 'ACP_DISCORD_NOTIFICATIONS_WEBHOOKS', + 'auth' => 'ext_mober/discordnotifications && acl_a_board', + 'cat' => ['ACP_DISCORD_NOTIFICATIONS'], + ], + 'mapping' => [ + 'title' => 'ACP_DISCORD_NOTIFICATIONS_MAPPING', + 'auth' => 'ext_mober/discordnotifications && acl_a_board', + 'cat' => ['ACP_DISCORD_NOTIFICATIONS'], + ], + ], + ]; } } diff --git a/acp/discord_notifications_module.php b/acp/discord_notifications_module.php index fb82cbb..53035e5 100644 --- a/acp/discord_notifications_module.php +++ b/acp/discord_notifications_module.php @@ -49,9 +49,6 @@ class discord_notifications_module /** @var \phpbb\log\log */ protected $log; - /** @var \mober\discordnotifications\notification_service */ - protected $notification_service; - /** @var \phpbb\request\request */ protected $request; @@ -61,9 +58,13 @@ class discord_notifications_module /** @var \phpbb\user */ protected $user; + /** @var bool */ + private $curl_available; + public function main($id, $mode) { global $phpbb_container; + $this->cache = $phpbb_container->get('cache.driver'); $this->config = $phpbb_container->get('config'); $this->db = $phpbb_container->get('dbal.conn'); @@ -72,36 +73,59 @@ public function main($id, $mode) $this->request = $phpbb_container->get('request'); $this->template = $phpbb_container->get('template'); $this->user = $phpbb_container->get('user'); - // Used for sending test messages to Discord - $this->notification_service = $phpbb_container->get('mober.discordnotifications.notification_service'); + $this->curl_available = extension_loaded('curl'); $this->language->add_lang('acp_discord_notifications', 'mober/discordnotifications'); - $this->tpl_name = 'acp_discord_notifications'; - $this->page_title = $this->language->lang('ACP_DISCORD_NOTIFICATIONS_TITLE'); + $this->page_title = $this->language->lang('ACP_DISCORD_NOTIFICATIONS'); add_form_key(self::PAGE_FORM_NAME); - // Process submit actions - if ($this->request->is_set_post('action_send_test_message')) + switch ($mode) { - $this->process_send_test_message(); - } - else if ($this->request->is_set_post('action_delete_alias')) - { - $this->process_delete_alias(); - } - else if ($this->request->is_set_post('submit')) - { - $this->process_form_submit(); - } + case 'webhooks': + $this->tpl_name = 'acp_discord_notifications_webhooks'; + + if ($this->request->is_set_post('action_send_test_message')) + { + $this->process_send_test_message(); + } + else if ($this->request->is_set_post('action_delete_alias')) + { + $this->process_delete_alias(); + } + else if ($this->request->is_set_post('submit')) + { + $this->process_webhooks_form_submit(); + } - // Generate the dynamic HTML content for enabling/disabling forum notifications - $this->generate_forum_section(); + $this->generate_webhook_section(); + break; - $this->generate_webhook_section(); + case 'mapping': + $this->tpl_name = 'acp_discord_notifications_mapping'; + + if ($this->request->is_set_post('submit')) + { + $this->process_mapping_form_submit(); + } + + $this->generate_webhook_section(); + $this->generate_forum_section(); + break; + + case 'settings': + default: + $this->tpl_name = 'acp_discord_notifications_settings'; + + if ($this->request->is_set_post('submit')) + { + $this->process_settings_form_submit(); + } + break; + } // Assign template values so that the page reflects the state of the extension settings - $this->template->assign_vars(array( + $this->template->assign_vars([ 'DN_MASTER_ENABLE' => $this->config['discord_notifications_enabled'], 'DN_POST_PREVIEW_LENGTH' => $this->config['discord_notifications_post_preview_length'], 'DN_TEST_MESSAGE_TEXT' => $this->language->lang('DN_TEST_MESSAGE_TEXT'), @@ -127,8 +151,9 @@ public function main($id, $mode) 'DN_DEFAULT_WEBHOOK' => $this->config['discord_notification_default_webhook'], + 'DN_CURL_AVAILABLE' => $this->curl_available, 'U_ACTION' => $this->u_action, - )); + ]); } /** @@ -159,7 +184,9 @@ private function validate_url($url) */ private function process_send_test_message() { - global $table_prefix; + global $table_prefix, $phpbb_container; + + $this->validate_post_request(); $webhook = $this->request->variable('dn_test_webhook', '', true); $test_message = $this->request->variable('dn_test_message', ''); @@ -179,7 +206,8 @@ private function process_send_test_message() $data = $this->db->sql_fetchrow($result); $this->db->sql_freeresult($result); - $result = $this->notification_service->force_send_discord_notification($data['url'], $test_message); + $notification_service = $phpbb_container->get('mober.discordnotifications.notification_service'); + $result = $notification_service->force_send_discord_notification($data['url'], $test_message); if ($result) { trigger_error($this->language->lang('DN_TEST_SUCCESS') . adm_back_link($this->u_action)); @@ -194,10 +222,7 @@ private function process_delete_alias() { global $table_prefix; - if (!check_form_key(self::PAGE_FORM_NAME)) - { - trigger_error('FORM_INVALID', E_USER_WARNING); - } + $this->validate_post_request(); $delete_alias = $this->request->variable('action_delete_alias', ['' => ''], true); foreach ($delete_alias as $alias => $url) @@ -211,17 +236,9 @@ private function process_delete_alias() trigger_error($this->language->lang('DN_SETTINGS_SAVED') . adm_back_link($this->u_action)); } - /** - * Handles all error checking and database changes when the user hits the submit button on the ACP page. - */ - private function process_form_submit() + private function process_settings_form_submit() { - global $table_prefix; - - if (!check_form_key(self::PAGE_FORM_NAME)) - { - trigger_error('FORM_INVALID', E_USER_WARNING); - } + $this->validate_post_request(); // Get form values for the main settings $master_enable = $this->request->variable('dn_master_enable', 0); @@ -238,6 +255,28 @@ private function process_form_submit() trigger_error($this->language->lang('DN_POST_PREVIEW_INVALID') . adm_back_link($this->u_action), E_USER_WARNING); } + $connect_timeout = max(1, (int) $this->request->variable('dn_connect_to', 0)); + $exec_timeout = max(1, (int) $this->request->variable('dn_exec_to', 0)); + + $this->config->set('discord_notifications_enabled', $master_enable); + $this->config->set('discord_notifications_post_preview_length', $preview_length); + $this->config->set('discord_notifications_connect_timeout', $connect_timeout); + $this->config->set('discord_notifications_exec_timeout', $exec_timeout); + + // Log the settings change + $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_DISCORD_NOTIFICATIONS_LOG_UPDATE'); + // Destroy any cached discord notification data + $this->cache->destroy('mober_discord_notifications'); + + trigger_error($this->language->lang('DN_SETTINGS_SAVED') . adm_back_link($this->u_action)); + } + + private function process_webhooks_form_submit() + { + global $table_prefix; + + $this->validate_post_request(); + // Create new entry $new_alias = $this->request->variable('dn_webhook_new_alias', '', true); $new_url = $this->request->variable('dn_webhook_new_url', ''); @@ -267,36 +306,27 @@ private function process_form_submit() $this->db->sql_query($sql); } + // Log the settings change + $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_DISCORD_NOTIFICATIONS_LOG_UPDATE'); + // Destroy any cached discord notification data + $this->cache->destroy('mober_discord_notifications'); + + trigger_error($this->language->lang('DN_SETTINGS_SAVED') . adm_back_link($this->u_action)); + } + + private function process_mapping_form_submit() + { + $this->validate_post_request(); + // Update configuration per forum $forum_configuration = $this->request->variable('dn_forum', [0 => ''], true); foreach ($forum_configuration as $id => $value) { - // Don't update deleted entries - if ($value === '' || $webhook_configuration[$value] !== '') - { - $sql = "UPDATE " . FORUMS_TABLE . " SET discord_notifications = '" . $this->db->sql_escape($value) . - "' WHERE forum_id = " . (int) $id; - $this->db->sql_query($sql); - } - } - - $connect_timeout = (int) $this->request->variable('dn_connect_to', 0); - if ($connect_timeout < 1) - { - $connect_timeout = 1; - } - - $exec_timeout = (int) $this->request->variable('dn_exec_to', 0); - if ($exec_timeout < 1) - { - $exec_timeout = 1; + $sql = "UPDATE " . FORUMS_TABLE . " SET discord_notifications = '" . $this->db->sql_escape($value) . + "' WHERE forum_id = " . (int) $id; + $this->db->sql_query($sql); } - $this->config->set('discord_notifications_enabled', $master_enable); - $this->config->set('discord_notifications_post_preview_length', $preview_length); - $this->config->set('discord_notifications_connect_timeout', $connect_timeout); - $this->config->set('discord_notifications_exec_timeout', $exec_timeout); - $this->config->set('discord_notification_type_post_create', $this->request->variable('dn_post_create', 0)); $this->config->set('discord_notification_type_post_update', $this->request->variable('dn_post_update', 0)); $this->config->set('discord_notification_type_post_delete', $this->request->variable('dn_post_delete', 0)); @@ -333,10 +363,10 @@ private function generate_webhook_section() while ($row = $this->db->sql_fetchrow($result)) { - $tpl_row = array( + $tpl_row = [ 'ALIAS' => $row['alias'], 'URL' => $row['url'], - ); + ]; $this->template->assign_block_vars('webhookrow', $tpl_row); } $this->db->sql_freeresult($result); @@ -356,7 +386,7 @@ private function generate_forum_section() $sql = "SELECT forum_id, forum_type, forum_name, left_id, right_id, parent_id, forum_parents, discord_notifications FROM " . FORUMS_TABLE . " - ORDER BY left_id ASC"; + ORDER BY left_id"; $result = $this->db->sql_query($sql); while ($row = $this->db->sql_fetchrow($result)) @@ -364,23 +394,23 @@ private function generate_forum_section() // Category forums are displayed for organizational purposes, but have no configuration if ($row['forum_type'] == FORUM_CAT) { - $tpl_row = array( + $tpl_row = [ 'S_IS_CAT' => true, 'FORUM_NAME' => $row['forum_name'], - 'PARENTS' => '', - ); + 'PARENTS' => '', + ]; $this->template->assign_block_vars('forumrow', $tpl_row); } else if ($row['forum_type'] == FORUM_POST) { // The labels for all the inputs are constructed based on the forum IDs to make it easy to know which - $tpl_row = array( - 'S_IS_CAT' => false, - 'FORUM_NAME' => $row['forum_name'], - 'FORUM_ID' => $row['forum_id'], - 'ALIAS' => $row['discord_notifications'], - 'PARENTS' => '', - ); + $tpl_row = [ + 'S_IS_CAT' => false, + 'FORUM_NAME' => $row['forum_name'], + 'FORUM_ID' => $row['forum_id'], + 'ALIAS' => $row['discord_notifications'], + 'PARENTS' => '', + ]; $parents = get_forum_parents($row); if (is_array($parents)) { @@ -395,4 +425,12 @@ private function generate_forum_section() } $this->db->sql_freeresult($result); } + + private function validate_post_request() + { + if (!check_form_key(self::PAGE_FORM_NAME)) + { + trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING); + } + } } diff --git a/adm/style/acp_discord_notifications.html b/adm/style/acp_discord_notifications_mapping.html similarity index 63% rename from adm/style/acp_discord_notifications.html rename to adm/style/acp_discord_notifications_mapping.html index 27ee7d8..e059a8f 100644 --- a/adm/style/acp_discord_notifications.html +++ b/adm/style/acp_discord_notifications_mapping.html @@ -1,100 +1,40 @@ - +{% include 'overall_header.html' %}

{{ lang('ACP_DISCORD_NOTIFICATIONS') }}

-

{{ DN_ACP_DESCRIPTION }}

+

{{ lang('DN_ACP_DESCRIPTION') }}

-
+{% if not DN_CURL_AVAILABLE %} +
+

{{ lang('DN_MISSING_CURL') }}

+
+{% endif %} -
- {{ lang('DN_MAIN_SETTINGS') }} -
-
-
-
-
-
-

{{ lang('DN_POST_PREVIEW_DESCRIPTION') }}
-
- -
-
-
-

{{ lang('DN_CONNECT_TO_DESCRIPTION') }}
-
- -
-
-
-

{{ lang('DN_EXEC_TO_DESCRIPTION') }}
-
- -
-
-
- -
- {{ lang('DN_WEBHOOK_SETTINGS') }} -

{{ lang('DN_WEBHOOK_SETTINGS_DESCRIPTION') }}

- {% if loops.webhookrow %} - {% for webhook in loops.webhookrow %} -
-
-
- - -
-
- {% endfor %} - {% else %} -

{{ lang('DN_NO_WEBHOOKS') }}

- {% endif %} -
+
- {{ lang('DN_WEBHOOK_NEW') }} -

{{ lang('DN_WEBHOOK_NEW_DESCRIPTION') }}

-
-

{{ lang('DN_WEBHOOK_NEW_ALIAS_DESCRIPTION') }}
-
- -
-
+ {{ lang('DN_WEBHOOK_DEFAULT') }}
-

{{ lang('DN_WEBHOOK_NEW_URL_DESCRIPTION') }}
-
- -
+
+
+
-
- -
- {{ lang('DN_TEST_SETTINGS') }}
-

{{ lang('DN_TEST_DESCRIPTION') }}
-
- -
+
+
+
- {% if loops.webhookrow %}
-

+

{{ lang('DN_WEBHOOK_DEFAULT_DESCRIPTION') }}
- + {% for webhook in loops.webhookrow %} - + {% endfor %}
-
-

{{ lang('DN_SEND_TEST_DESCRIPTION') }}
-
-
- {% else %} -

{{ lang('DN_NO_WEBHOOKS') }}

- {% endif %}
@@ -160,27 +100,6 @@

{{ lang('ACP_DISCORD_NOTIFICATIONS') }}

-
-
-
-
-
-
-
-
-
-
-
-

{{ lang('DN_WEBHOOK_DEFAULT_DESCRIPTION') }}
-
- -
-
diff --git a/adm/style/acp_discord_notifications_settings.html b/adm/style/acp_discord_notifications_settings.html new file mode 100644 index 0000000..9aa8239 --- /dev/null +++ b/adm/style/acp_discord_notifications_settings.html @@ -0,0 +1,49 @@ +{% include 'overall_header.html' %} + +

{{ lang('ACP_DISCORD_NOTIFICATIONS') }}

+ +

{{ lang('DN_ACP_DESCRIPTION') }}

+ +{% if not DN_CURL_AVAILABLE %} +
+

{{ lang('DN_MISSING_CURL') }}

+
+{% endif %} + + + +
+ {{ lang('DN_MAIN_SETTINGS') }} +
+
+
+
+
+
+

{{ lang('DN_POST_PREVIEW_DESCRIPTION') }}
+
+ +
+
+
+

{{ lang('DN_CONNECT_TO_DESCRIPTION') }}
+
+ +
+
+
+

{{ lang('DN_EXEC_TO_DESCRIPTION') }}
+
+ +
+
+
+ +
+   + {{ S_FORM_TOKEN }} +
+ + + +{% include 'overall_footer.html' %} diff --git a/adm/style/acp_discord_notifications_webhooks.html b/adm/style/acp_discord_notifications_webhooks.html new file mode 100644 index 0000000..a500594 --- /dev/null +++ b/adm/style/acp_discord_notifications_webhooks.html @@ -0,0 +1,86 @@ +{% include 'overall_header.html' %} + +

{{ lang('ACP_DISCORD_NOTIFICATIONS') }}

+ +

{{ lang('DN_ACP_DESCRIPTION') }}

+ +{% if not DN_CURL_AVAILABLE %} +
+

{{ lang('DN_MISSING_CURL') }}

+
+{% endif %} + +
+ +
+ {{ lang('DN_WEBHOOK_SETTINGS') }} +

{{ lang('DN_WEBHOOK_SETTINGS_DESCRIPTION') }}

+ {% if loops.webhookrow %} + {% for webhook in loops.webhookrow %} +
+
+
+ + +
+
+ {% endfor %} + {% else %} +

{{ lang('DN_NO_WEBHOOKS') }}

+ {% endif %} +
+ +
+ {{ lang('DN_WEBHOOK_NEW') }} +

{{ lang('DN_WEBHOOK_NEW_DESCRIPTION') }}

+
+

{{ lang('DN_WEBHOOK_NEW_ALIAS_DESCRIPTION') }}
+
+ +
+
+
+

{{ lang('DN_WEBHOOK_NEW_URL_DESCRIPTION') }}
+
+ +
+
+
+ +
+ {{ lang('DN_TEST_SETTINGS') }} + {% if loops.webhookrow %} +
+

{{ lang('DN_TEST_DESCRIPTION') }}
+
+ +
+
+
+

+
+ +
+
+
+

{{ lang('DN_SEND_TEST_DESCRIPTION') }}
+
+
+ {% else %} +

{{ lang('DN_NO_WEBHOOKS') }}

+ {% endif %} +
+ +
+   + {{ S_FORM_TOKEN }} +
+ +
+ +{% include 'overall_footer.html' %} diff --git a/composer.json b/composer.json index dda7d2a..2bb65ac 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "type": "phpbb-extension", "description": "Publishes forum event notifications to a Discord server channel", "homepage": "https://github.com/m-ober/phpbb-discord-notifications", - "version": "3.0.7", + "version": "3.0.8-RC2", "keywords": [ "phpbb", "extension", diff --git a/event/notification_event_listener.php b/event/notification_event_listener.php index c927595..af5e9d6 100644 --- a/event/notification_event_listener.php +++ b/event/notification_event_listener.php @@ -79,7 +79,7 @@ public function __construct( */ static public function getSubscribedEvents() { - return array( + return [ // This event is used for performing actions directly after a post or topic has been submitted. 'core.submit_post_end' => 'handle_post_submit_action', // This event is used for performing actions directly after a post or topic has been deleted. @@ -100,7 +100,7 @@ static public function getSubscribedEvents() 'core.approve_topics_after' => 'handle_topic_approval_action', // Post approval 'core.approve_posts_after' => 'handle_post_approval_action', - ); + ]; } // ---------------------------------------------------------------------------- @@ -179,7 +179,7 @@ public function handle_post_submit_action($event) } // Build an array of the event data that we may need to pass along to the function that will construct the notification message - $post_data = array( + $post_data = [ 'user_id' => $event['data']['poster_id'], 'user_name' => $event['username'], 'forum_id' => $event['data']['forum_id'], @@ -192,7 +192,7 @@ public function handle_post_submit_action($event) 'edit_user_name' => $this->language->lang('UNKNOWN_USER'), 'edit_reason' => $event['data']['post_edit_reason'], 'content' => $event['data']['message'], - ); + ]; if ($post_data['edit_user_id'] == $post_data['user_id']) { @@ -254,7 +254,7 @@ public function handle_post_delete_action($event) // Build an array of the event data that we may need to pass along to the function that will construct the notification message. // Note that unfortunately, the event data does not give us any information indicating which user deleted the post. - $post_data = array( + $post_data = [ 'user_id' => $event['data']['poster_id'], 'user_name' => $this->language->lang('UNKNOWN_USER'), 'forum_id' => $event['forum_id'], @@ -263,7 +263,7 @@ public function handle_post_delete_action($event) 'topic_title' => $this->language->lang('UNKNOWN_TOPIC'), 'post_id' => $event['post_id'], 'delete_reason' => $event['softdelete_reason'], - ); + ]; // Fetch the forum name, topic title, and user name using the respective IDs. $forum_name = $this->notification_service->query_forum_name($post_data['forum_id']); @@ -319,13 +319,14 @@ public function handle_topic_delete_action($event) } // Copy over the data necessary to generate the notification into a new array - $delete_data = array(); - $delete_data['forum_id'] = $query_data['forum_id']; - $delete_data['forum_name'] = $query_data['forum_name']; - $delete_data['topic_title'] = $query_data['topic_title']; - $delete_data['topic_post_count'] = $query_data['topic_posts_approved']; - $delete_data['user_id'] = $query_data['topic_poster']; - $delete_data['user_name'] = $query_data['topic_first_poster_name']; + $delete_data = [ + 'forum_id' => $query_data['forum_id'], + 'forum_name' => $query_data['forum_name'], + 'topic_title' => $query_data['topic_title'], + 'topic_post_count' => $query_data['topic_posts_approved'], + 'user_id' => $query_data['topic_poster'], + 'user_name' => $query_data['topic_first_poster_name'], + ]; $this->notify_topic_deleted($delete_data, $webhook_url); } @@ -352,22 +353,24 @@ public function handle_lock_action($event) // Get the ID needed to access $event['data'], then extract all relevant data from the event that we need to generate the notification $id = array_slice($event['ids'], -1)[0]; - - $lock_data = array(); - $lock_data['forum_id'] = $event['data'][$id]['forum_id']; - $lock_data['forum_name'] = $event['data'][$id]['forum_name']; - $lock_data['post_id'] = $event['data'][$id]['post_id'] ?? ''; // only used for post_[un]lock events - $lock_data['post_subject'] = $event['data'][$id]['post_subject'] ?? ''; // only used for post_[un]lock events - $lock_data['topic_id'] = $event['data'][$id]['topic_id']; - $lock_data['topic_title'] = $event['data'][$id]['topic_title']; - // Two sets of user data captured: one for the post (if applicable) and one for the user that started the topic - $lock_data['post_user_id'] = $event['data'][$id]['poster_id'] ?? ''; // only used for post_[un]lock events - $lock_data['post_user_name'] = $event['data'][$id]['username'] ?? ''; // only used for post_[un]lock events - $lock_data['topic_user_id'] = $event['data'][$id]['topic_poster']; - $lock_data['topic_user_name'] = $event['data'][$id]['topic_first_poster_name']; + $event_data = $event['data'][$id]; + + $lock_data = [ + 'forum_id' => $event_data['forum_id'], + 'forum_name' => $event_data['forum_name'], + 'topic_id' => $event_data['topic_id'], + 'topic_title' => $event_data['topic_title'], + 'topic_user_id' => $event_data['topic_poster'], + 'topic_user_name' => $event_data['topic_first_poster_name'], + // only used for post_[un]lock events, not always set + 'post_id' => $event_data['post_id'] ?? '', + 'post_subject' => $event_data['post_subject'] ?? '', + 'post_user_id' => $event_data['poster_id'] ?? '', + 'post_user_name' => $event_data['username'] ?? '', + ]; // If the forum the post was made in does not have notifications enabled or the topic/poar is not visible, do nothing more. - $topic_visibile = $event['data'][$id]['topic_visibility'] == 1; + $topic_visibile = $event_data['topic_visibility'] == 1; $webhook_url = $this->notification_service->get_forum_notification_url($lock_data['forum_id']); if (!$webhook_url || !$topic_visibile) { @@ -429,7 +432,7 @@ public function handle_user_add_action($event) */ public function handle_user_activate_action($event) { - $user_data = array(); + $user_data = []; if ($event['user_id']) { $user_data['user_id'] = $event['user_id']; @@ -458,7 +461,7 @@ public function handle_user_activate_action($event) public function handle_user_delete_action($event) { // Extract the IDs and names of all deleted users to pass along in an array of (id => name) - $user_data = array(); + $user_data = []; foreach ($event['user_ids'] as $id) { $user_data[$id] = $event['user_rows'][$id]['username']; diff --git a/ext.php b/ext.php new file mode 100644 index 0000000..63c8480 --- /dev/null +++ b/ext.php @@ -0,0 +1,23 @@ +container->get('language'); + $language->add_lang('acp_discord_notifications', 'mober/discordnotifications'); + + $curl_available = extension_loaded('curl'); + if (!$curl_available) + { + return $language->lang('DN_MISSING_CURL'); + } + + return true; + } +} diff --git a/language/de/acp_discord_notifications.php b/language/de/acp_discord_notifications.php index ded810d..2e8ffd2 100644 --- a/language/de/acp_discord_notifications.php +++ b/language/de/acp_discord_notifications.php @@ -21,6 +21,7 @@ $lang = array_merge($lang, array( // ACP Extension Settings Page 'DN_ACP_DESCRIPTION' => 'Diese Einstellungen erlauben es, bei verschiedenen Ereignissen eine Benachrichtigung an einen Discord-Kanal zu schicken.', + 'DN_MISSING_CURL' => 'Damit diese Erweiterung funktioniert, musst du das ext-curl Modul für PHP installieren.', 'DN_MAIN_SETTINGS' => 'Konfiguration', 'DN_MASTER_ENABLE' => 'Discord Benachrichtigungen aktivieren', @@ -44,7 +45,7 @@ 'DN_WEBHOOK_NEW_ALIAS' => 'Neuer Alias', 'DN_WEBHOOK_NEW_ALIAS_DESCRIPTION' => 'Frei wählbar, z.B. der Names des Channels, auf den der Webhook zeigt (wie "öffentlich" or "marktplatz").', 'DN_WEBHOOK_NEW_URL' => 'Discord Webhook URL', - 'DN_WEBHOOK_NEW_URL_DESCRIPTION'=> 'Webhook für einen Discord-Kanal. Weitere Informationen gibt es in diesem Artikel.', + 'DN_WEBHOOK_NEW_URL_DESCRIPTION'=> 'Webhook für einen Discord-Kanal. Weitere Informationen gibt es in diesem Artikel.', 'DN_WEBHOOK_SELECT' => 'Webhook auswählen', 'DN_WEBHOOK_DEFAULT' => 'Forenübergreifende Ereignisse', 'DN_WEBHOOK_DEFAULT_DESCRIPTION' => 'Webhook für Ereignisse, die nicht zu einem bestimmten Forum gehören (z.B. Benutzer erstellt/gelöscht)', diff --git a/language/de/info_acp_discord_notifications.php b/language/de/info_acp_discord_notifications.php index 4500002..906c645 100644 --- a/language/de/info_acp_discord_notifications.php +++ b/language/de/info_acp_discord_notifications.php @@ -19,7 +19,9 @@ $lang = array_merge($lang, array( // ACP Module 'ACP_DISCORD_NOTIFICATIONS' => 'Discord Benachrichtigungen', - 'ACP_DISCORD_NOTIFICATIONS_TITLE' => 'Discord Konfiguration', + 'ACP_DISCORD_NOTIFICATIONS_SETTINGS' => 'Einstellungen', + 'ACP_DISCORD_NOTIFICATIONS_WEBHOOKS' => 'Webhooks', + 'ACP_DISCORD_NOTIFICATIONS_MAPPING' => 'Zuordnung', // ACP Logs 'ACP_DISCORD_NOTIFICATIONS_LOG_UPDATE' => 'Discord Benachrichtigungen: Einstellungen aktualisiert', diff --git a/language/en/acp_discord_notifications.php b/language/en/acp_discord_notifications.php index b236f0f..83d36b5 100644 --- a/language/en/acp_discord_notifications.php +++ b/language/en/acp_discord_notifications.php @@ -21,6 +21,7 @@ $lang = array_merge($lang, array( // ACP Extension Settings Page 'DN_ACP_DESCRIPTION' => 'These settings allow for various forum events to send notification messages to a Discord server.', + 'DN_MISSING_CURL' => 'Please install the ext-curl module for PHP, otherwise this extension will not work.', 'DN_MAIN_SETTINGS' => 'Main Settings', 'DN_MASTER_ENABLE' => 'Enable Discord Notifications', diff --git a/language/en/info_acp_discord_notifications.php b/language/en/info_acp_discord_notifications.php index d7dee76..e92efb2 100644 --- a/language/en/info_acp_discord_notifications.php +++ b/language/en/info_acp_discord_notifications.php @@ -19,7 +19,9 @@ $lang = array_merge($lang, array( // ACP Module 'ACP_DISCORD_NOTIFICATIONS' => 'Discord Notifications', - 'ACP_DISCORD_NOTIFICATIONS_TITLE' => 'Discord Notification Settings', + 'ACP_DISCORD_NOTIFICATIONS_SETTINGS' => 'Settings', + 'ACP_DISCORD_NOTIFICATIONS_WEBHOOKS' => 'Webhooks', + 'ACP_DISCORD_NOTIFICATIONS_MAPPING' => 'Mapping', // ACP Logs 'ACP_DISCORD_NOTIFICATIONS_LOG_UPDATE' => 'Modified Discord notification settings', diff --git a/language/fr/acp_discord_notifications.php b/language/fr/acp_discord_notifications.php index ab94c25..bc27da6 100644 --- a/language/fr/acp_discord_notifications.php +++ b/language/fr/acp_discord_notifications.php @@ -42,6 +42,7 @@ $lang = array_merge($lang, array( // ACP Extension Settings Page 'DN_ACP_DESCRIPTION' => 'Depuis cette page il est possible de définir les paramètres permettant à différents évènements du forum d’être envoyés comme notification sur un serveur Discord.', + 'DN_MISSING_CURL' => 'Pour que cette extension fonctionne, tu dois installer le module ext-curl pour PHP.', 'DN_MAIN_SETTINGS' => 'Paramètres généraux', 'DN_MASTER_ENABLE' => 'Activer les notifications Discord', diff --git a/language/fr/info_acp_discord_notifications.php b/language/fr/info_acp_discord_notifications.php index 6455ac5..4dedb57 100644 --- a/language/fr/info_acp_discord_notifications.php +++ b/language/fr/info_acp_discord_notifications.php @@ -42,7 +42,9 @@ $lang = array_merge($lang, array( // ACP Module 'ACP_DISCORD_NOTIFICATIONS' => 'Notifications Discord', - 'ACP_DISCORD_NOTIFICATIONS_TITLE' => 'Paramètres', + 'ACP_DISCORD_NOTIFICATIONS_SETTINGS' => 'Paramètres', + 'ACP_DISCORD_NOTIFICATIONS_WEBHOOKS' => 'Webhooks', + 'ACP_DISCORD_NOTIFICATIONS_MAPPING' => 'Mapping', // ACP Logs 'ACP_DISCORD_NOTIFICATIONS_LOG_UPDATE' => 'Paramètres des notifications Discord modifiées', diff --git a/migrations/create_tables.php b/migrations/create_tables.php index 3f145cd..a9f9728 100644 --- a/migrations/create_tables.php +++ b/migrations/create_tables.php @@ -12,7 +12,7 @@ class create_tables extends \phpbb\db\migration\migration { static public function depends_on() { - return array('\mober\discordnotifications\migrations\extension_installation'); + return ['\mober\discordnotifications\migrations\extension_installation']; } /** @@ -21,23 +21,23 @@ static public function depends_on() */ public function update_schema() { - return array( - 'add_tables' => array ( - $this->table_prefix . 'discord_webhooks' => array ( - 'COLUMNS' => array ( - 'alias' => array ( + return [ + 'add_tables' => [ + $this->table_prefix . 'discord_webhooks' => [ + 'COLUMNS' => [ + 'alias' => [ 'VCHAR:255', - '' - ), - 'url' => array ( + '', + ], + 'url' => [ 'VCHAR:255', - '' - ), - ), + '', + ], + ], 'PRIMARY_KEY' => 'alias', - ), - ), - ); + ], + ], + ]; } /** @@ -46,10 +46,10 @@ public function update_schema() */ public function revert_schema() { - return array ( - 'drop_tables' => array ( + return [ + 'drop_tables' => [ $this->table_prefix . 'discord_webhooks', - ) - ); + ], + ]; } } diff --git a/migrations/extension_installation.php b/migrations/extension_installation.php index 676d977..06c9991 100644 --- a/migrations/extension_installation.php +++ b/migrations/extension_installation.php @@ -38,13 +38,13 @@ public function effectively_installed() */ public function update_schema() { - return array( - 'add_columns' => array( - $this->table_prefix . 'forums' => array( - 'discord_notifications' => array('VCHAR:255', ''), - ), - ), - ); + return [ + 'add_columns' => [ + $this->table_prefix . 'forums' => [ + 'discord_notifications' => ['VCHAR:255', ''], + ], + ], + ]; } /** @@ -55,13 +55,13 @@ public function update_schema() */ public function revert_schema() { - return array( - 'drop_columns' => array( - $this->table_prefix . 'forums' => array( + return [ + 'drop_columns' => [ + $this->table_prefix . 'forums' => [ 'discord_notifications', - ), - ), - ); + ], + ], + ]; } /** @@ -74,55 +74,55 @@ public function revert_schema() */ public function update_data() { - return array( + return [ // The "master switch" that enables notifications to be sent. This can only be set to true if the webhook URL is also valid - array('config.add', array('discord_notifications_enabled', 0)), + ['config.add', ['discord_notifications_enabled', 0]], // The maximum number of characters permitted in a discord notification message - array('config.add', array('discord_notifications_post_preview_length', 200)), + ['config.add', ['discord_notifications_post_preview_length', 200]], // Timeout values - array('config.add', array('discord_notifications_connect_timeout', 2)), - array('config.add', array('discord_notifications_exec_timeout', 2)), + ['config.add', ['discord_notifications_connect_timeout', 2]], + ['config.add', ['discord_notifications_exec_timeout', 2]], // These configurations represent the various types of notifications that can be sent, which can be individually enabled or disabled. // Upon installation, every notification type is enabled by default. // Post notifications - array('config.add', array('discord_notification_type_post_create', 1)), - array('config.add', array('discord_notification_type_post_update', 1)), - array('config.add', array('discord_notification_type_post_delete', 1)), - array('config.add', array('discord_notification_type_post_lock', 1)), - array('config.add', array('discord_notification_type_post_unlock', 1)), - array('config.add', array('discord_notification_type_post_approve', 1)), + ['config.add', ['discord_notification_type_post_create', 1]], + ['config.add', ['discord_notification_type_post_update', 1]], + ['config.add', ['discord_notification_type_post_delete', 1]], + ['config.add', ['discord_notification_type_post_lock', 1]], + ['config.add', ['discord_notification_type_post_unlock', 1]], + ['config.add', ['discord_notification_type_post_approve', 1]], // Topic notifications - array('config.add', array('discord_notification_type_topic_create', 1)), - array('config.add', array('discord_notification_type_topic_update', 1)), - array('config.add', array('discord_notification_type_topic_delete', 1)), - array('config.add', array('discord_notification_type_topic_lock', 1)), - array('config.add', array('discord_notification_type_topic_unlock', 1)), - array('config.add', array('discord_notification_type_topic_approve', 1)), + ['config.add', ['discord_notification_type_topic_create', 1]], + ['config.add', ['discord_notification_type_topic_update', 1]], + ['config.add', ['discord_notification_type_topic_delete', 1]], + ['config.add', ['discord_notification_type_topic_lock', 1]], + ['config.add', ['discord_notification_type_topic_unlock', 1]], + ['config.add', ['discord_notification_type_topic_approve', 1]], // User notifications - array('config.add', array('discord_notification_type_user_create', 1)), - array('config.add', array('discord_notification_type_user_delete', 1)), + ['config.add', ['discord_notification_type_user_create', 1]], + ['config.add', ['discord_notification_type_user_delete', 1]], // Default channel - array('config.add', array('discord_notification_default_webhook', '')), + ['config.add', ['discord_notification_default_webhook', '']], // Standard ACP module data - array('module.add', array( + ['module.add', [ 'acp', 'ACP_CAT_DOT_MODS', - 'ACP_DISCORD_NOTIFICATIONS' - )), - array('module.add', array( + 'ACP_DISCORD_NOTIFICATIONS', + ]], + ['module.add', [ 'acp', 'ACP_DISCORD_NOTIFICATIONS', - array( - 'module_basename' => '\mober\discordnotifications\acp\discord_notifications_module', - 'modes' => array('settings'), - ), - )), - ); + [ + 'module_basename' => '\mober\discordnotifications\acp\discord_notifications_module', + 'modes' => ['settings'], + ], + ]], + ]; } } diff --git a/migrations/v308/add_modules.php b/migrations/v308/add_modules.php new file mode 100644 index 0000000..c52f41d --- /dev/null +++ b/migrations/v308/add_modules.php @@ -0,0 +1,39 @@ + '\mober\discordnotifications\acp\discord_notifications_module', + 'modes' => [ + 'webhooks', + 'mapping', + ], + ], + ]], + ['custom', [ + [$this, 'rename_settings_module'] + ]], + ]; + } + + public function rename_settings_module() + { + $sql = 'UPDATE ' . MODULES_TABLE . " + SET module_langname = 'ACP_DISCORD_NOTIFICATIONS_SETTINGS' + WHERE module_langname = 'ACP_DISCORD_NOTIFICATIONS_TITLE'"; + $this->sql_query($sql); + } +} diff --git a/migrations/v308/add_version.php b/migrations/v308/add_version.php new file mode 100644 index 0000000..2b00710 --- /dev/null +++ b/migrations/v308/add_version.php @@ -0,0 +1,24 @@ +config['discord_notification_version']) && + version_compare($this->config['discord_notification_version'], '3.0.8', '>='); + } + + public function update_data() + { + return [ + ['config.add', ['discord_notification_version', '3.0.8']], + ]; + } +} diff --git a/notification_service.php b/notification_service.php index 9c2c7d1..ece99c2 100644 --- a/notification_service.php +++ b/notification_service.php @@ -70,7 +70,7 @@ public function get_forum_notification_url($forum_id) { global $table_prefix; - if (is_numeric($forum_id) == false) + if (!is_numeric($forum_id)) { return false; } @@ -118,7 +118,7 @@ public function get_post_preview_length() */ public function query_forum_name($forum_id) { - if (is_numeric($forum_id) == false) + if (!is_numeric($forum_id)) { return null; } @@ -138,7 +138,7 @@ public function query_forum_name($forum_id) */ public function query_post_subject($post_id) { - if (is_numeric($post_id) == false) + if (!is_numeric($post_id)) { return null; } @@ -159,7 +159,7 @@ public function query_post_subject($post_id) */ public function query_topic_title($topic_id) { - if (is_numeric($topic_id) == false) + if (!is_numeric($topic_id)) { return null; } @@ -180,9 +180,9 @@ public function query_topic_title($topic_id) */ public function query_topic_details($topic_id) { - if (is_numeric($topic_id) == false) + if (!is_numeric($topic_id)) { - return array(); + return []; } $sql = "SELECT @@ -205,7 +205,7 @@ public function query_topic_details($topic_id) */ public function query_user_name($user_id) { - if (is_numeric($user_id) == false) + if (!is_numeric($user_id)) { return false; } @@ -231,7 +231,7 @@ public function send_discord_notification($color, $message, $webhook_url = null, { global $table_prefix; - if ($this->config['discord_notifications_enabled'] == 0 || isset($message) == false) + if ($this->config['discord_notifications_enabled'] == 0 || !isset($message)) { return; } @@ -263,7 +263,7 @@ public function send_discord_notification($color, $message, $webhook_url = null, */ public function force_send_discord_notification($discord_webhook_url, $message) { - if (!filter_var($discord_webhook_url, FILTER_VALIDATE_URL) || is_string($message) == false || $message == '') + if (!filter_var($discord_webhook_url, FILTER_VALIDATE_URL) || !is_string($message) || $message == '') { return false; } @@ -290,20 +290,20 @@ public function force_send_discord_notification($discord_webhook_url, $message) */ private function execute_discord_webhook($discord_webhook_url, $color, $message, $title = null, $preview = null, $footer = null) { - if (isset($discord_webhook_url) == false || $discord_webhook_url === '') + if (!isset($discord_webhook_url) || $discord_webhook_url === '') { return false; } - if (is_integer($color) == false || $color < 0) + 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) == false || $message == '') + if (!is_string($message) || $message == '') { return false; } - if (isset($footer) == true && (is_string($footer) == false || $footer == '')) + if (isset($footer) && (!is_string($footer) || $footer == '')) { return false; } @@ -339,14 +339,16 @@ private function execute_discord_webhook($discord_webhook_url, $color, $message, // 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 + 'timestamp' => date('c', time()), + 'color' => $color, + 'description' => $message, ]; if (isset($footer)) { - $embed["footer"] = ["text" => $footer]; + $embed['footer'] = [ + 'text' => $footer, + ]; } if (isset($title) && isset($preview)) @@ -357,16 +359,16 @@ private function execute_discord_webhook($discord_webhook_url, $color, $message, { $embed['fields'] = [ [ - 'name' => $title, - 'value' => $preview, - 'inline' => false - ] + 'name' => $title, + 'value' => $preview, + 'inline' => false, + ], ]; } } $payload = [ - 'embeds' => [$embed] + 'embeds' => [$embed], ]; $json = \json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); @@ -378,7 +380,7 @@ private function execute_discord_webhook($discord_webhook_url, $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_HTTPHEADER, array('Content-type: application/json')); + 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); @@ -398,7 +400,8 @@ private function execute_discord_webhook($discord_webhook_url, $color, $message, $this->log->add('admin', ANONYMOUS, '127.0.0.1', 'ACP_DISCORD_NOTIFICATIONS_WEBHOOK_ERROR', time(), [$status]); return false; } - } else + } + else { $this->log->add('admin', ANONYMOUS, '127.0.0.1', 'ACP_DISCORD_NOTIFICATIONS_CURL_ERROR', time(), [$error]); return false;