From 52faea518dd525ba8cf218a4a3995b8df90c0230 Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Sat, 4 Nov 2023 01:08:54 +0100 Subject: [PATCH 01/15] Split ACP module into different modes --- acp/discord_notifications_info.php | 30 ++-- acp/discord_notifications_module.php | 137 +++++++++++------- ...=> acp_discord_notifications_mapping.html} | 111 ++------------ .../acp_discord_notifications_settings.html | 43 ++++++ .../acp_discord_notifications_webhooks.html | 81 +++++++++++ .../de/info_acp_discord_notifications.php | 4 +- .../en/info_acp_discord_notifications.php | 4 +- .../fr/info_acp_discord_notifications.php | 4 +- migrations/v308/add_modules.php | 43 ++++++ 9 files changed, 296 insertions(+), 161 deletions(-) rename adm/style/{acp_discord_notifications.html => acp_discord_notifications_mapping.html} (63%) create mode 100644 adm/style/acp_discord_notifications_settings.html create mode 100644 adm/style/acp_discord_notifications_webhooks.html create mode 100644 migrations/v308/add_modules.php 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..1355556 100644 --- a/acp/discord_notifications_module.php +++ b/acp/discord_notifications_module.php @@ -73,32 +73,55 @@ public function main($id, $mode) $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->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')) - { - $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')) + switch ($mode) { - $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(); + } + + $this->generate_webhook_section(); + break; + + case 'mapping': + $this->tpl_name = 'acp_discord_notifications_mapping'; - // Generate the dynamic HTML content for enabling/disabling forum notifications - $this->generate_forum_section(); + if ($this->request->is_set_post('submit')) + { + $this->process_mapping_form_submit(); + } + + $this->generate_webhook_section(); + $this->generate_forum_section(); + break; - $this->generate_webhook_section(); + 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( @@ -159,7 +182,7 @@ private function validate_url($url) */ private function process_send_test_message() { - global $table_prefix; + global $table_prefix, $phpbb_container; $webhook = $this->request->variable('dn_test_webhook', '', true); $test_message = $this->request->variable('dn_test_message', ''); @@ -174,6 +197,8 @@ private function process_send_test_message() trigger_error($this->language->lang('DN_TEST_BAD_WEBHOOK') . adm_back_link($this->u_action), E_USER_WARNING); } + $this->notification_service = $phpbb_container->get('mober.discordnotifications.notification_service'); + $sql = "SELECT url FROM {$table_prefix}discord_webhooks WHERE alias = '" . $this->db->sql_escape($webhook) . "'"; $result = $this->db->sql_query($sql); $data = $this->db->sql_fetchrow($result); @@ -211,13 +236,8 @@ 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); @@ -238,6 +258,31 @@ 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; + + if (!check_form_key(self::PAGE_FORM_NAME)) + { + trigger_error('FORM_INVALID', E_USER_WARNING); + } + // Create new entry $new_alias = $this->request->variable('dn_webhook_new_alias', '', true); $new_url = $this->request->variable('dn_webhook_new_url', ''); @@ -267,36 +312,30 @@ private function process_form_submit() $this->db->sql_query($sql); } - // 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); - } - } + // 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)); + } - $connect_timeout = (int) $this->request->variable('dn_connect_to', 0); - if ($connect_timeout < 1) + private function process_mapping_form_submit() + { + if (!check_form_key(self::PAGE_FORM_NAME)) { - $connect_timeout = 1; + trigger_error('FORM_INVALID', E_USER_WARNING); } - $exec_timeout = (int) $this->request->variable('dn_exec_to', 0); - if ($exec_timeout < 1) + // Update configuration per forum + $forum_configuration = $this->request->variable('dn_forum', [0 => ''], true); + foreach ($forum_configuration as $id => $value) { - $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)); @@ -356,7 +395,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)) 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..5151791 100644 --- a/adm/style/acp_discord_notifications.html +++ b/adm/style/acp_discord_notifications_mapping.html @@ -1,4 +1,4 @@ - +{% include 'overall_header.html' %}

{{ lang('ACP_DISCORD_NOTIFICATIONS') }}

@@ -7,94 +7,28 @@

{{ lang('ACP_DISCORD_NOTIFICATIONS') }}

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

{{ 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_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 +94,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..760a05a --- /dev/null +++ b/adm/style/acp_discord_notifications_settings.html @@ -0,0 +1,43 @@ +{% include 'overall_header.html' %} + +

{{ lang('ACP_DISCORD_NOTIFICATIONS') }}

+ +

{{ DN_ACP_DESCRIPTION }}

+ + + +
+ {{ 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..6094e9a --- /dev/null +++ b/adm/style/acp_discord_notifications_webhooks.html @@ -0,0 +1,81 @@ +{% include 'overall_header.html' %} + +

{{ lang('ACP_DISCORD_NOTIFICATIONS') }}

+ +

{{ DN_ACP_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_NEW_URL_DESCRIPTION') }}
+
+ +
+
+
+ +
+ {{ lang('DN_TEST_SETTINGS') }} +
+

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

+
+ +
+
+
+

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

{{ lang('DN_NO_WEBHOOKS') }}

+ {% endif %} +
+ +
+   + {{ S_FORM_TOKEN }} +
+ +
+ +{% include 'overall_footer.html' %} 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/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/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/v308/add_modules.php b/migrations/v308/add_modules.php new file mode 100644 index 0000000..de1d11b --- /dev/null +++ b/migrations/v308/add_modules.php @@ -0,0 +1,43 @@ +container->get('migrator.tool.module'); + return $module_manager->exists('acp', 'ACP_DISCORD_NOTIFICATIONS', 'ACP_DISCORD_NOTIFICATIONS_SETTINGS'); + } + + public function update_data() + { + return [ + ['module.remove', [ + 'acp', + 'ACP_DISCORD_NOTIFICATIONS', + [ + 'module_langname' => 'ACP_DISCORD_NOTIFICATIONS_TITLE', + ], + ]], + ['module.add', [ + 'acp', + 'ACP_DISCORD_NOTIFICATIONS', + [ + 'module_basename' => '\mober\discordnotifications\acp\discord_notifications_module', + 'modes' => [ + 'settings', + 'webhooks', + 'mapping', + ], + ], + ]], + ]; + } +} From e04374cafad98305d577212cab48aec48a0ae0d5 Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Sat, 4 Nov 2023 13:38:50 +0100 Subject: [PATCH 02/15] Inline `$notification_service` variable --- acp/discord_notifications_module.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/acp/discord_notifications_module.php b/acp/discord_notifications_module.php index 1355556..18bff40 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; @@ -72,7 +69,6 @@ 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->language->add_lang('acp_discord_notifications', 'mober/discordnotifications'); $this->page_title = $this->language->lang('ACP_DISCORD_NOTIFICATIONS'); @@ -197,14 +193,13 @@ private function process_send_test_message() trigger_error($this->language->lang('DN_TEST_BAD_WEBHOOK') . adm_back_link($this->u_action), E_USER_WARNING); } - $this->notification_service = $phpbb_container->get('mober.discordnotifications.notification_service'); - $sql = "SELECT url FROM {$table_prefix}discord_webhooks WHERE alias = '" . $this->db->sql_escape($webhook) . "'"; $result = $this->db->sql_query($sql); $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)); From 9705fad9abf87248d428989afb8cccba9e2aa40a Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Sat, 4 Nov 2023 13:39:24 +0100 Subject: [PATCH 03/15] Extract form key check into function --- acp/discord_notifications_module.php | 30 +++++++++++++--------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/acp/discord_notifications_module.php b/acp/discord_notifications_module.php index 18bff40..55813e3 100644 --- a/acp/discord_notifications_module.php +++ b/acp/discord_notifications_module.php @@ -180,6 +180,8 @@ private function process_send_test_message() { 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', ''); @@ -214,10 +216,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) @@ -233,10 +232,7 @@ private function process_delete_alias() private function process_settings_form_submit() { - 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); @@ -273,10 +269,7 @@ private function process_webhooks_form_submit() { global $table_prefix; - if (!check_form_key(self::PAGE_FORM_NAME)) - { - trigger_error('FORM_INVALID', E_USER_WARNING); - } + $this->validate_post_request(); // Create new entry $new_alias = $this->request->variable('dn_webhook_new_alias', '', true); @@ -317,10 +310,7 @@ private function process_webhooks_form_submit() private function process_mapping_form_submit() { - if (!check_form_key(self::PAGE_FORM_NAME)) - { - trigger_error('FORM_INVALID', E_USER_WARNING); - } + $this->validate_post_request(); // Update configuration per forum $forum_configuration = $this->request->variable('dn_forum', [0 => ''], true); @@ -429,4 +419,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); + } + } } From 9489e625a1d4bddc52303743e6d70a26eb2219e2 Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Sat, 4 Nov 2023 13:43:46 +0100 Subject: [PATCH 04/15] Show warning if `ext-curl` is not installed --- acp/discord_notifications_module.php | 6 ++++++ adm/style/acp_discord_notifications_mapping.html | 6 ++++++ adm/style/acp_discord_notifications_settings.html | 6 ++++++ adm/style/acp_discord_notifications_webhooks.html | 7 ++++++- language/de/acp_discord_notifications.php | 1 + language/en/acp_discord_notifications.php | 1 + language/fr/acp_discord_notifications.php | 1 + 7 files changed, 27 insertions(+), 1 deletion(-) diff --git a/acp/discord_notifications_module.php b/acp/discord_notifications_module.php index 55813e3..90736bb 100644 --- a/acp/discord_notifications_module.php +++ b/acp/discord_notifications_module.php @@ -58,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'); @@ -69,6 +73,7 @@ public function main($id, $mode) $this->request = $phpbb_container->get('request'); $this->template = $phpbb_container->get('template'); $this->user = $phpbb_container->get('user'); + $this->curl_available = extension_loaded('curl'); $this->language->add_lang('acp_discord_notifications', 'mober/discordnotifications'); $this->page_title = $this->language->lang('ACP_DISCORD_NOTIFICATIONS'); @@ -146,6 +151,7 @@ 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, )); } diff --git a/adm/style/acp_discord_notifications_mapping.html b/adm/style/acp_discord_notifications_mapping.html index 5151791..0c5a3bf 100644 --- a/adm/style/acp_discord_notifications_mapping.html +++ b/adm/style/acp_discord_notifications_mapping.html @@ -4,6 +4,12 @@

{{ lang('ACP_DISCORD_NOTIFICATIONS') }}

{{ DN_ACP_DESCRIPTION }}

+{% if not DN_CURL_AVAILABLE %} +
+

{{ lang('DN_MISSING_CURL') }}

+
+{% endif %} +
diff --git a/adm/style/acp_discord_notifications_settings.html b/adm/style/acp_discord_notifications_settings.html index 760a05a..95e9827 100644 --- a/adm/style/acp_discord_notifications_settings.html +++ b/adm/style/acp_discord_notifications_settings.html @@ -4,6 +4,12 @@

{{ lang('ACP_DISCORD_NOTIFICATIONS') }}

{{ DN_ACP_DESCRIPTION }}

+{% if not DN_CURL_AVAILABLE %} +
+

{{ lang('DN_MISSING_CURL') }}

+
+{% endif %} +
diff --git a/adm/style/acp_discord_notifications_webhooks.html b/adm/style/acp_discord_notifications_webhooks.html index 6094e9a..cb4257d 100644 --- a/adm/style/acp_discord_notifications_webhooks.html +++ b/adm/style/acp_discord_notifications_webhooks.html @@ -4,8 +4,13 @@

{{ lang('ACP_DISCORD_NOTIFICATIONS') }}

{{ DN_ACP_DESCRIPTION }}

- +{% if not DN_CURL_AVAILABLE %} +
+

{{ lang('DN_MISSING_CURL') }}

+
+{% endif %} +
{{ lang('DN_WEBHOOK_SETTINGS') }} diff --git a/language/de/acp_discord_notifications.php b/language/de/acp_discord_notifications.php index ded810d..387f1c7 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', 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/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', From c19c491afcf11c7751acc7b8e6739d9cdbf1710b Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Sat, 4 Nov 2023 13:44:17 +0100 Subject: [PATCH 05/15] Fix variable --- adm/style/acp_discord_notifications_mapping.html | 2 +- adm/style/acp_discord_notifications_settings.html | 2 +- adm/style/acp_discord_notifications_webhooks.html | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/adm/style/acp_discord_notifications_mapping.html b/adm/style/acp_discord_notifications_mapping.html index 0c5a3bf..e059a8f 100644 --- a/adm/style/acp_discord_notifications_mapping.html +++ b/adm/style/acp_discord_notifications_mapping.html @@ -2,7 +2,7 @@

{{ lang('ACP_DISCORD_NOTIFICATIONS') }}

-

{{ DN_ACP_DESCRIPTION }}

+

{{ lang('DN_ACP_DESCRIPTION') }}

{% if not DN_CURL_AVAILABLE %}
diff --git a/adm/style/acp_discord_notifications_settings.html b/adm/style/acp_discord_notifications_settings.html index 95e9827..9aa8239 100644 --- a/adm/style/acp_discord_notifications_settings.html +++ b/adm/style/acp_discord_notifications_settings.html @@ -2,7 +2,7 @@

{{ lang('ACP_DISCORD_NOTIFICATIONS') }}

-

{{ DN_ACP_DESCRIPTION }}

+

{{ lang('DN_ACP_DESCRIPTION') }}

{% if not DN_CURL_AVAILABLE %}
diff --git a/adm/style/acp_discord_notifications_webhooks.html b/adm/style/acp_discord_notifications_webhooks.html index cb4257d..35561b9 100644 --- a/adm/style/acp_discord_notifications_webhooks.html +++ b/adm/style/acp_discord_notifications_webhooks.html @@ -2,7 +2,7 @@

{{ lang('ACP_DISCORD_NOTIFICATIONS') }}

-

{{ DN_ACP_DESCRIPTION }}

+

{{ lang('DN_ACP_DESCRIPTION') }}

{% if not DN_CURL_AVAILABLE %}
From ddb872ae612ae50fdac379885a08bf0563c4fa07 Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Sat, 4 Nov 2023 13:44:48 +0100 Subject: [PATCH 06/15] Simplify boolean expressions --- notification_service.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/notification_service.php b/notification_service.php index 9c2c7d1..0b337db 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,7 +180,7 @@ 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(); } @@ -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; } From 994214ec98c4f214071a0b609c5c6e7d1b568ca8 Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Sat, 4 Nov 2023 14:55:00 +0100 Subject: [PATCH 07/15] Bump version to 3.0.8-RC1 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index dda7d2a..3e6b13e 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-RC1", "keywords": [ "phpbb", "extension", From a666dd1d0580e4ed9549c805f44c4b77cb131fe5 Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Mon, 6 Nov 2023 09:45:12 +0100 Subject: [PATCH 08/15] Use short array syntax --- acp/discord_notifications_module.php | 28 ++++----- event/notification_event_listener.php | 61 ++++++++++--------- migrations/create_tables.php | 38 ++++++------ migrations/extension_installation.php | 86 +++++++++++++-------------- notification_service.php | 24 ++++---- 5 files changed, 121 insertions(+), 116 deletions(-) diff --git a/acp/discord_notifications_module.php b/acp/discord_notifications_module.php index 90736bb..53035e5 100644 --- a/acp/discord_notifications_module.php +++ b/acp/discord_notifications_module.php @@ -125,7 +125,7 @@ public function main($id, $mode) } // 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'), @@ -153,7 +153,7 @@ public function main($id, $mode) 'DN_CURL_AVAILABLE' => $this->curl_available, 'U_ACTION' => $this->u_action, - )); + ]); } /** @@ -363,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); @@ -394,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)) { 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/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/notification_service.php b/notification_service.php index 0b337db..e61a4a9 100644 --- a/notification_service.php +++ b/notification_service.php @@ -182,7 +182,7 @@ public function query_topic_details($topic_id) { if (!is_numeric($topic_id)) { - return array(); + return []; } $sql = "SELECT @@ -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); From 31cc60251a84398962d2dd92f71374727b47601a Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Mon, 6 Nov 2023 09:52:36 +0100 Subject: [PATCH 09/15] Fix code style --- notification_service.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/notification_service.php b/notification_service.php index e61a4a9..ece99c2 100644 --- a/notification_service.php +++ b/notification_service.php @@ -400,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; From c6fc666b364a14cf239973e0c3560affa35705b2 Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Mon, 6 Nov 2023 14:02:37 +0100 Subject: [PATCH 10/15] Update link in German translation --- language/de/acp_discord_notifications.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/language/de/acp_discord_notifications.php b/language/de/acp_discord_notifications.php index 387f1c7..2e8ffd2 100644 --- a/language/de/acp_discord_notifications.php +++ b/language/de/acp_discord_notifications.php @@ -45,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)', From 0a1b54e323070c633beff35b352a4720fec9884c Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Mon, 6 Nov 2023 19:03:43 +0100 Subject: [PATCH 11/15] Fix migration to work with all upgrade/downgrade paths --- migrations/v308/add_modules.php | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/migrations/v308/add_modules.php b/migrations/v308/add_modules.php index de1d11b..c52f41d 100644 --- a/migrations/v308/add_modules.php +++ b/migrations/v308/add_modules.php @@ -2,42 +2,38 @@ namespace mober\discordnotifications\migrations\v308; -class add_modules extends \phpbb\db\migration\container_aware_migration +class add_modules extends \phpbb\db\migration\migration { static public function depends_on() { return ['\mober\discordnotifications\migrations\extension_installation']; } - public function effectively_installed() - { - /** @var \phpbb\db\migration\tool\module $module_manager */ - $module_manager = $this->container->get('migrator.tool.module'); - return $module_manager->exists('acp', 'ACP_DISCORD_NOTIFICATIONS', 'ACP_DISCORD_NOTIFICATIONS_SETTINGS'); - } - public function update_data() { return [ - ['module.remove', [ - 'acp', - 'ACP_DISCORD_NOTIFICATIONS', - [ - 'module_langname' => 'ACP_DISCORD_NOTIFICATIONS_TITLE', - ], - ]], ['module.add', [ 'acp', 'ACP_DISCORD_NOTIFICATIONS', [ 'module_basename' => '\mober\discordnotifications\acp\discord_notifications_module', 'modes' => [ - 'settings', '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); + } } From 99a2782db3c9e504fa60f5b5b64c6544a7e96772 Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Mon, 6 Nov 2023 20:05:49 +0100 Subject: [PATCH 12/15] Add migration to set installed version in config --- migrations/v308/add_version.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 migrations/v308/add_version.php 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']], + ]; + } +} From 230ef3ad0d49f612cb2e4498355ab72e1bebccf0 Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Mon, 6 Nov 2023 20:45:20 +0100 Subject: [PATCH 13/15] Bump version to 3.0.8-RC2 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3e6b13e..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.8-RC1", + "version": "3.0.8-RC2", "keywords": [ "phpbb", "extension", From 93c3b359ea720542056b075027c50fd18da87285 Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Wed, 8 Nov 2023 23:24:37 +0100 Subject: [PATCH 14/15] Check if `curl` extension is available when installing --- ext.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 ext.php 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; + } +} From e16d6f2d68bf0d59ea729e28202da3dd58fa1d7a Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Thu, 9 Nov 2023 11:18:42 +0100 Subject: [PATCH 15/15] Hide test settings if no Webhook is available --- adm/style/acp_discord_notifications_webhooks.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adm/style/acp_discord_notifications_webhooks.html b/adm/style/acp_discord_notifications_webhooks.html index 35561b9..a500594 100644 --- a/adm/style/acp_discord_notifications_webhooks.html +++ b/adm/style/acp_discord_notifications_webhooks.html @@ -49,13 +49,13 @@

{{ lang('ACP_DISCORD_NOTIFICATIONS') }}

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

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