snipe-it/app/Http/Livewire/SlackSettingsForm.php

269 lines
10 KiB
PHP
Raw Normal View History

2023-01-24 09:25:05 -08:00
<?php
namespace App\Http\Livewire;
use GuzzleHttp\Client;
2024-01-24 15:50:36 -08:00
use Illuminate\Support\Facades\Http;
2023-01-24 09:25:05 -08:00
use Livewire\Component;
use App\Models\Setting;
use App\Helpers\Helper;
2023-01-24 09:25:05 -08:00
class SlackSettingsForm extends Component
{
public $webhook_endpoint;
public $webhook_channel;
public $webhook_botname;
public $isDisabled ='disabled' ;
public $webhook_name;
public $webhook_link;
public $webhook_placeholder;
public $webhook_icon;
public $webhook_selected;
2023-03-13 16:13:02 -07:00
public array $webhook_text;
public Setting $setting;
2023-09-19 17:46:29 -07:00
public $webhook_endpoint_rules;
protected $rules = [
2023-09-28 11:29:46 -07:00
'webhook_endpoint' => 'required_with:webhook_channel|starts_with:http://,https://,ftp://,irc://,https://hooks.slack.com/services/|url|nullable',
'webhook_channel' => 'required_with:webhook_endpoint|starts_with:#|nullable',
'webhook_botname' => 'string|nullable',
];
2023-03-13 16:13:02 -07:00
public function mount() {
2023-03-13 16:13:02 -07:00
$this->webhook_text= [
"slack" => array(
2023-03-13 16:13:02 -07:00
"name" => trans('admin/settings/general.slack') ,
"icon" => 'fab fa-slack',
"placeholder" => "https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX",
"link" => 'https://api.slack.com/messaging/webhooks',
2024-01-29 12:58:09 -08:00
"test" => "testWebhook"
),
2023-03-13 16:13:02 -07:00
"general"=> array(
"name" => trans('admin/settings/general.general_webhook'),
"icon" => "fab fa-hashtag",
2024-01-24 10:38:06 -08:00
"placeholder" => trans('general.url'),
2023-03-13 16:13:02 -07:00
"link" => "",
2024-01-29 12:58:09 -08:00
"test" => "testWebhook"
),
"google" => array(
"name" => trans('admin/settings/general.google_workspaces'),
"icon" => "fa-brands fa-google",
"placeholder" => "https://chat.googleapis.com/v1/spaces/xxxxxxxx/messages?key=xxxxxx",
"link" => "https://developers.google.com/chat/how-tos/webhooks#register_the_incoming_webhook",
"test" => "googleWebhookTest"
2023-03-13 16:13:02 -07:00
),
2024-01-17 13:41:45 -08:00
"microsoft" => array(
"name" => trans('admin/settings/general.ms_teams'),
"icon" => "fa-brands fa-microsoft",
"placeholder" => "https://abcd.webhook.office.com/webhookb2/XXXXXXX",
"link" => "https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook?tabs=dotnet#create-incoming-webhooks-1",
2024-01-29 12:58:09 -08:00
"test" => "msTeamTestWebhook"
2024-01-24 11:29:32 -08:00
),
2023-03-13 16:13:02 -07:00
];
2023-01-25 16:38:08 -08:00
$this->setting = Setting::getSettings();
2023-03-21 15:53:09 -07:00
$this->save_button = trans('general.save');
$this->webhook_selected = $this->setting->webhook_selected;
2023-03-13 16:13:02 -07:00
$this->webhook_name = $this->webhook_text[$this->setting->webhook_selected]["name"];
$this->webhook_icon = $this->webhook_text[$this->setting->webhook_selected]["icon"];
$this->webhook_placeholder = $this->webhook_text[$this->setting->webhook_selected]["placeholder"];
$this->webhook_link = $this->webhook_text[$this->setting->webhook_selected]["link"];
2024-01-29 12:58:09 -08:00
$this->webhook_test = $this->webhook_text[$this->setting->webhook_selected]["test"];
$this->webhook_endpoint = $this->setting->webhook_endpoint;
$this->webhook_channel = $this->setting->webhook_channel;
$this->webhook_botname = $this->setting->webhook_botname;
$this->webhook_options = $this->setting->webhook_selected;
2024-01-31 10:48:48 -08:00
if($this->webhook_selected == 'microsoft' || $this->webhook_selected == 'google'){
2024-01-31 12:54:30 -08:00
$this->webhook_channel = '#NA';
}
2023-03-21 15:53:09 -07:00
if($this->setting->webhook_endpoint != null && $this->setting->webhook_channel != null){
2024-01-31 12:54:30 -08:00
$this->isDisabled= '';
2023-03-21 15:53:09 -07:00
}
}
public function updated($field) {
$this->validateOnly($field, $this->rules);
2023-09-19 17:46:29 -07:00
}
public function updatedWebhookSelected() {
2023-03-13 16:13:02 -07:00
$this->webhook_name = $this->webhook_text[$this->webhook_selected]['name'];
$this->webhook_icon = $this->webhook_text[$this->webhook_selected]["icon"]; ;
$this->webhook_placeholder = $this->webhook_text[$this->webhook_selected]["placeholder"];
2024-01-17 13:41:45 -08:00
$this->webhook_endpoint = null;
2023-03-13 16:13:02 -07:00
$this->webhook_link = $this->webhook_text[$this->webhook_selected]["link"];
2024-01-29 12:58:09 -08:00
$this->webhook_test = $this->webhook_text[$this->webhook_selected]["test"];
2023-03-21 15:53:09 -07:00
if($this->webhook_selected != 'slack'){
$this->isDisabled= '';
$this->save_button = trans('general.save');
}
2024-01-31 10:48:23 -08:00
if($this->webhook_selected == 'microsoft' || $this->webhook_selected == 'google'){
2024-01-31 12:53:56 -08:00
$this->webhook_channel = '#NA';
}
}
2023-01-24 09:25:05 -08:00
private function isButtonDisabled() {
2023-03-21 15:53:09 -07:00
if (empty($this->webhook_endpoint)) {
$this->isDisabled = 'disabled';
$this->save_button = trans('admin/settings/general.webhook_presave');
}
if (empty($this->webhook_channel)) {
$this->isDisabled = 'disabled';
$this->save_button = trans('admin/settings/general.webhook_presave');
}
}
2023-01-24 09:25:05 -08:00
public function render()
{
2023-03-21 15:53:09 -07:00
$this->isButtonDisabled();
2023-01-24 09:25:05 -08:00
return view('livewire.slack-settings-form');
}
public function testWebhook(){
$webhook = new Client([
'base_url' => e($this->webhook_endpoint),
'defaults' => [
'exceptions' => false,
],
'allow_redirects' => false,
]);
$payload = json_encode(
[
'channel' => e($this->webhook_channel),
'text' => trans('general.webhook_test_msg', ['app' => $this->webhook_name]),
'username' => e($this->webhook_botname),
'icon_emoji' => ':heart:',
]);
try {
$test = $webhook->post($this->webhook_endpoint, ['body' => $payload]);
2023-09-28 11:29:46 -07:00
if(($test->getStatusCode() == 302)||($test->getStatusCode() == 301)){
2023-09-28 12:15:17 -07:00
return session()->flash('error' , trans('admin/settings/message.webhook.error_redirect', ['endpoint' => $this->webhook_endpoint]));
}
2023-02-15 15:46:05 -08:00
$this->isDisabled='';
2023-03-21 15:53:09 -07:00
$this->save_button = trans('general.save');
2023-09-28 12:15:17 -07:00
return session()->flash('success' , trans('admin/settings/message.webhook.success', ['webhook_name' => $this->webhook_name]));
} catch (\Exception $e) {
$this->isDisabled='disabled';
$this->save_button = trans('admin/settings/general.webhook_presave');
return session()->flash('error' , trans('admin/settings/message.webhook.error', ['error_message' => $e->getMessage(), 'app' => $this->webhook_name]));
}
return session()->flash('error' , trans('admin/settings/message.webhook.error_misc'));
}
2023-03-21 15:53:09 -07:00
public function clearSettings(){
if (Helper::isDemoMode()) {
session()->flash('error',trans('general.feature_disabled'));
} else {
$this->webhook_endpoint = '';
$this->webhook_channel = '';
$this->webhook_botname = '';
$this->setting->webhook_endpoint = '';
$this->setting->webhook_channel = '';
$this->setting->webhook_botname = '';
$this->setting->save();
2023-03-21 15:53:09 -07:00
session()->flash('success', trans('admin/settings/message.update.success'));
}
}
2023-03-21 15:53:09 -07:00
public function submit()
{
if (Helper::isDemoMode()) {
session()->flash('error',trans('general.feature_disabled'));
} else {
$this->validate($this->rules);
$this->setting->webhook_selected = $this->webhook_selected;
$this->setting->webhook_endpoint = $this->webhook_endpoint;
$this->setting->webhook_channel = $this->webhook_channel;
$this->setting->webhook_botname = $this->webhook_botname;
$this->setting->save();
session()->flash('success',trans('admin/settings/message.update.success'));
}
2023-01-31 11:32:05 -08:00
}
2024-01-29 12:58:09 -08:00
public function googleWebhookTest(){
$payload = [
"text" => trans('general.webhook_test_msg', ['app' => $this->webhook_name]),
];
try {
$response = Http::withHeaders([
'content-type' => 'applications/json',
])->post($this->webhook_endpoint,
$payload)->throw();
if (($response->getStatusCode() == 302) || ($response->getStatusCode() == 301)) {
return session()->flash('error', trans('admin/settings/message.webhook.error_redirect', ['endpoint' => $this->webhook_endpoint]));
}
$this->isDisabled='';
$this->save_button = trans('general.save');
return session()->flash('success' , trans('admin/settings/message.webhook.success', ['webhook_name' => $this->webhook_name]));
} catch (\Exception $e) {
$this->isDisabled='disabled';
$this->save_button = trans('admin/settings/general.webhook_presave');
return session()->flash('error' , trans('admin/settings/message.webhook.error', ['error_message' => $e->getMessage(), 'app' => $this->webhook_name]));
}
}
public function msTeamTestWebhook(){
2024-01-17 13:41:45 -08:00
$payload =
[
"@type" => "MessageCard",
"@context" => "http://schema.org/extensions",
2024-01-24 10:38:06 -08:00
"summary" => trans('mail.snipe_webhook_summary'),
"title" => trans('mail.snipe_webhook_test'),
"text" => trans('general.webhook_test_msg', ['app' => $this->webhook_name]),
];
2024-01-17 13:41:45 -08:00
try {
$response = Http::withHeaders([
'content-type' => 'applications/json',
])->post($this->webhook_endpoint,
$payload)->throw();
2024-01-17 13:41:45 -08:00
if(($response->getStatusCode() == 302)||($response->getStatusCode() == 301)){
return session()->flash('error' , trans('admin/settings/message.webhook.error_redirect', ['endpoint' => $this->webhook_endpoint]));
}
$this->isDisabled='';
$this->save_button = trans('general.save');
return session()->flash('success' , trans('admin/settings/message.webhook.success', ['webhook_name' => $this->webhook_name]));
} catch (\Exception $e) {
$this->isDisabled='disabled';
$this->save_button = trans('admin/settings/general.webhook_presave');
return session()->flash('error' , trans('admin/settings/message.webhook.error', ['error_message' => $e->getMessage(), 'app' => $this->webhook_name]));
}
return session()->flash('error' , trans('admin/settings/message.webhook.error_misc'));
}
2023-01-24 09:25:05 -08:00
}