From 508c4f02e2af40afed8d984a9e70ebd7053e1aa7 Mon Sep 17 00:00:00 2001 From: StanByes Date: Sun, 31 Jul 2022 16:48:08 +0200 Subject: [PATCH 1/2] Add discord webhook to send msg on new ticket --- Controller/SupportAppController.php | 40 +++++++ Controller/SupportController.php | 70 +++++++++++- SQL/schema.php | 1 + View/Support/admin_config.ctp | 20 ++-- config.json | 2 +- lang/en_US.json | 162 ++++++++++++++-------------- lang/fr_FR.json | 6 +- 7 files changed, 208 insertions(+), 93 deletions(-) create mode 100644 Controller/SupportAppController.php diff --git a/Controller/SupportAppController.php b/Controller/SupportAppController.php new file mode 100644 index 0000000..e4b5235 --- /dev/null +++ b/Controller/SupportAppController.php @@ -0,0 +1,40 @@ +loadModel("Support.SettingsSupport"); + $discordWebhook = $this->SettingsSupport->find("first"); + if (empty($discordWebhook) || empty($discordWebhook["SettingsSupport"]["discord_webhook"])) + return false; + + $discordWebhook = explode("/", $discordWebhook["SettingsSupport"]["discord_webhook"]); + $webhookData = [ + "id" => $discordWebhook[5], + "token" => $discordWebhook[6] + ]; + + $handle = curl_init("https://discord.com/api/webhooks/" . $webhookData["id"] . "/" . $webhookData["token"]); + + $data = json_encode([ + "content" => $msg, + "embeds" => $embedData + ]); + + curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); + curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($handle, CURLOPT_POST, true); + curl_setopt($handle, CURLOPT_HEADER, true); + curl_setopt($handle, CURLOPT_HTTPHEADER, + array( + 'Content-Type:application/json', + 'Content-Length: ' . strlen($data) + ) + ); + curl_setopt($handle, CURLOPT_POSTFIELDS, $data); + + $res = curl_exec($handle); + return true; + } +} + diff --git a/Controller/SupportController.php b/Controller/SupportController.php index 3ce22a8..ea5eefc 100644 --- a/Controller/SupportController.php +++ b/Controller/SupportController.php @@ -1,6 +1,6 @@ layout = 'admin'; - if (!$this->Permissions->can('SETTINGS_SUPPORT')) + if (!$this->Permissions->can('PERMISSIONS__SETTINGS_SUPPORT')) throw new ForbiddenException(); - $this->set('title_for_layout', $this->Lang->get('SUPPORT__SETTINGS_TITLE') . ' - ' . $this->Lang->get('SUPPORT__SUPPORT')); + $this->loadModel('Support.SettingsSupport'); + if ($this->request->is('post')) { + if (!isset($this->request->data["webhook"])) { + return $this->sendJSON(['statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS')]); + } + + $settings = $this->SettingsSupport->find('first'); + if (!$settings) { + $this->SettingsSupport->create([ + "discord_webhook" => $this->request->data["webhook"] + ]); + } else { + $this->SettingsSupport->read(null, $settings["SettingsSupport"]["id"]); + $this->SettingsSupport->set([ + "discord_webhook" => $this->request->data["webhook"] + ]); + } + + $this->SettingsSupport->save(); + $this->Session->setFlash($this->Lang->get('CONFIG__EDIT_SUCCESS'), 'default.success'); + $this->redirect("/admin/support/config"); + } + + $this->set('title_for_layout', $this->Lang->get('SUPPORT__SETTINGS_TITLE') . ' - ' . $this->Lang->get('SUPPORT__SUPPORT')); $settings = $this->SettingsSupport->find('first'); + if ($settings) { + $settings = $settings["SettingsSupport"]; + } + $this->set(compact('settings')); } + function admin_ajax_edit_settings() + { + if (!$this->Permissions->can('PERMISSIONS__SETTINGS_SUPPORT')) + throw new ForbiddenException(); + + if (!isset($this->request->data["webhook"])) { + return $this->sendJSON(['statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS')]); + } + + $this->loadModel('Support.SettingsSupport'); + $settings = $this->SettingsSupport->find('first'); + if (!$settings) { + $this->Ticket->create([ + "discord_webhook" => $this->request->data["webhook"] + ]); + } else { + $this->Ticket->read(null, $settings["SettingsSupport"]["id"]); + $this->Ticket->set([ + "discord_webhook" => $this->request->data["webhook"] + ]); + } + + $this->Ticket->save(); + $this->sendJSON(['statut' => true, 'msg' => $this->Lang->get('SUPPORT__SETTINGS_SUCCESS_MODIFIED')]); + } + function admin_ajax_create_categorie() { if (!$this->Permissions->can('MANAGE_CATEGORIES')) @@ -222,6 +275,17 @@ function ajax_create() )); $this->Ticket->save(); $this->Notification->setToAdmin($this->User->getKey('pseudo') . ' ' . $this->Lang->get('SUPPORT__NOTIF_CREATE')); + + $this->loadModel("Support.CategoriesSupport"); + $categorieName = $this->CategoriesSupport->find("first", ["conditions" => ["id" => $this->request->data['categorie']]])["CategoriesSupport"]["name"]; + $this->sendDiscordMessage("", [array( + "title" => "SUPPORT: Nouveau ticket par " . $this->User->getKey("pseudo"), + "color" => "3447003", + "description" => $this->Lang->get('SUPPORT__DISCORD_EMBED_CREATE_DESCRIPTION', + ['{SUBJECT_NAME}' => $this->request->data['subject'], '{CATEGORIE_NAME}' => $categorieName] + ) + )]); + $this->sendJSON(['statut' => true, 'msg' => $this->Lang->get('SUPPORT__SUCCESS_CREATE')]); } diff --git a/SQL/schema.php b/SQL/schema.php index b46768e..bd26c98 100755 --- a/SQL/schema.php +++ b/SQL/schema.php @@ -35,6 +35,7 @@ public function after($event = array()) { public $support__settings = array( 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => false, 'key' => 'primary'), 'suffix_reply' => array('type' => 'text', 'null' => true, 'default' => null, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'), + 'discord_webhook' => array('type' => 'text', 'null' => true, 'default' => null, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'), 'indexes' => array( 'PRIMARY' => array('column' => 'id', 'unique' => 1) ), diff --git a/View/Support/admin_config.ctp b/View/Support/admin_config.ctp index 41c9cd5..3435ff0 100644 --- a/View/Support/admin_config.ctp +++ b/View/Support/admin_config.ctp @@ -1,12 +1,18 @@ -

get('SUPPORT__SETTINGS_TITLE'); ?>

- Bientôt +
+
+
+ + "> +
+
+ + +
@@ -14,14 +20,14 @@ $Support = new SupportController();