2023-01-24 09:25:05 -08:00
< ? php
2024-05-29 12:07:48 -07:00
namespace App\Livewire ;
2023-01-24 09:25:05 -08:00
2023-02-01 16:51:05 -08:00
use GuzzleHttp\Client ;
2024-01-24 15:50:36 -08:00
use Illuminate\Support\Facades\Http ;
2024-10-28 10:57:48 -07:00
use Illuminate\Support\Str ;
2023-01-24 09:25:05 -08:00
use Livewire\Component ;
use App\Models\Setting ;
2023-03-21 20:00:11 -07:00
use App\Helpers\Helper ;
2024-10-24 12:21:51 -07:00
use Osama\LaravelTeamsNotification\TeamsNotification ;
2023-01-24 09:25:05 -08:00
class SlackSettingsForm extends Component
{
2023-03-01 12:30:32 -08:00
public $webhook_endpoint ;
public $webhook_channel ;
public $webhook_botname ;
2023-09-20 12:54:34 -07:00
public $isDisabled = 'disabled' ;
2023-03-07 15:48:21 -08:00
public $webhook_name ;
2023-02-27 09:19:59 -08:00
public $webhook_link ;
2023-03-01 17:10:10 -08:00
public $webhook_placeholder ;
public $webhook_icon ;
2023-02-27 09:19:59 -08:00
public $webhook_selected ;
2024-10-28 10:57:48 -07:00
public $teams_webhook_deprecated ;
2023-03-13 16:13:02 -07:00
public array $webhook_text ;
2023-03-01 17:10:10 -08:00
public Setting $setting ;
2024-06-04 15:49:39 -07:00
public $save_button ;
2024-06-04 16:14:07 -07:00
public $webhook_test ;
2023-09-19 17:46:29 -07:00
2024-06-04 16:14:07 -07:00
public $webhook_endpoint_rules ;
2023-03-01 17:10:10 -08:00
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' ,
2023-03-07 15:48:21 -08:00
'webhook_channel' => 'required_with:webhook_endpoint|starts_with:#|nullable' ,
2023-03-01 17:10:10 -08:00
'webhook_botname' => 'string|nullable' ,
];
2024-06-04 16:14:07 -07:00
2023-03-13 16:13:02 -07:00
2023-03-21 21:00:43 -07:00
public function mount () {
2023-03-13 16:13:02 -07:00
$this -> webhook_text = [
2024-01-30 12:38:17 -08:00
" slack " => array (
2023-03-13 16:13:02 -07:00
" name " => trans ( 'admin/settings/general.slack' ) ,
2024-01-30 12:38:17 -08:00
" 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-01 12:30:32 -08:00
),
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 (
2024-01-17 11:49:31 -08:00
" name " => trans ( 'admin/settings/general.ms_teams' ),
" icon " => " fa-brands fa-microsoft " ,
" placeholder " => " https://abcd.webhook.office.com/webhookb2/XXXXXXX " ,
2024-01-17 16:30:27 -08:00
" 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:22:34 -08: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' );
2023-03-07 15:48:21 -08:00
$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 " ];
2024-01-17 16:30:27 -08:00
$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 " ];
2023-03-01 12:30:32 -08:00
$this -> webhook_endpoint = $this -> setting -> webhook_endpoint ;
$this -> webhook_channel = $this -> setting -> webhook_channel ;
$this -> webhook_botname = $this -> setting -> webhook_botname ;
2023-03-01 17:10:10 -08:00
$this -> webhook_options = $this -> setting -> webhook_selected ;
2024-10-28 14:30:34 -07:00
$this -> teams_webhook_deprecated = ! Str :: contains ( $this -> webhook_endpoint , 'workflows' );
2024-10-28 13:57:42 -07:00
if ( $this -> webhook_selected === 'microsoft' || $this -> webhook_selected === 'google' ){
2024-01-31 12:54:30 -08:00
$this -> webhook_channel = '#NA' ;
2024-01-31 10:47:25 -08:00
}
2023-03-01 12:30:32 -08:00
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
}
2024-10-28 15:03:43 -07:00
if ( $this -> webhook_selected === 'microsoft' && $this -> teams_webhook_deprecated ) {
2024-10-28 13:57:42 -07:00
session () -> flash ( 'warning' , 'The selected Microsoft Teams webhook URL will be deprecated Jan 31st, 2025. Please use a workflow URL. Microsofts Documentation on creating a workflow can be found <a href="https://support.microsoft.com/en-us/office/create-incoming-webhooks-with-workflows-for-microsoft-teams-8ae491c7-0394-4861-ba59-055e33f75498" target="_blank"> here.</a>' );
}
2023-01-25 16:22:34 -08:00
}
2023-03-21 21:00:43 -07:00
public function updated ( $field ) {
2023-03-07 15:48:21 -08:00
$this -> validateOnly ( $field , $this -> rules );
2023-09-19 17:46:29 -07:00
2023-02-06 09:40:57 -08:00
}
2023-03-21 21:00:43 -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' ;
2024-01-31 10:47:25 -08:00
}
2023-03-01 17:10:10 -08:00
}
2023-01-24 09:25:05 -08:00
2023-03-21 21:00:43 -07: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-03-21 21:00:43 -07:00
2023-01-24 09:25:05 -08:00
public function render ()
{
2023-03-21 15:53:09 -07:00
$this -> isButtonDisabled ();
2024-10-28 13:57:42 -07:00
2023-01-24 09:25:05 -08:00
return view ( 'livewire.slack-settings-form' );
2024-10-28 13:57:42 -07:00
2023-01-24 09:25:05 -08:00
}
2023-03-01 12:30:32 -08:00
public function testWebhook (){
2023-02-01 16:51:05 -08:00
2023-03-07 15:48:21 -08:00
$webhook = new Client ([
2023-03-01 12:30:32 -08:00
'base_url' => e ( $this -> webhook_endpoint ),
2023-02-01 16:51:05 -08:00
'defaults' => [
'exceptions' => false ,
],
2023-09-20 12:54:34 -07:00
'allow_redirects' => false ,
2023-02-01 16:51:05 -08:00
]);
$payload = json_encode (
[
2023-03-01 12:30:32 -08:00
'channel' => e ( $this -> webhook_channel ),
2023-03-14 19:02:38 -07:00
'text' => trans ( 'general.webhook_test_msg' , [ 'app' => $this -> webhook_name ]),
2023-03-01 12:30:32 -08:00
'username' => e ( $this -> webhook_botname ),
2023-02-01 16:51:05 -08:00
'icon_emoji' => ':heart:' ,
2023-09-20 12:54:34 -07:00
2023-02-01 16:51:05 -08:00
]);
try {
2023-09-20 12:54:34 -07:00
$test = $webhook -> post ( $this -> webhook_endpoint , [ 'body' => $payload ]);
2023-03-21 21:00:43 -07:00
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-09-20 12:54:34 -07:00
}
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 ]));
2023-02-01 16:51:05 -08:00
} catch ( \Exception $e ) {
2023-03-21 21:00:43 -07:00
2023-09-20 12:54:34 -07:00
$this -> isDisabled = 'disabled' ;
$this -> save_button = trans ( 'admin/settings/general.webhook_presave' );
2023-03-08 10:35:45 -08:00
return session () -> flash ( 'error' , trans ( 'admin/settings/message.webhook.error' , [ 'error_message' => $e -> getMessage (), 'app' => $this -> webhook_name ]));
2023-02-01 16:51:05 -08:00
}
2023-03-21 18:59:18 -07:00
return session () -> flash ( 'error' , trans ( 'admin/settings/message.webhook.error_misc' ));
2023-02-01 16:51:05 -08:00
2024-01-24 14:38:45 -08:00
}
2023-02-01 12:08:13 -08:00
2023-03-21 15:53:09 -07:00
public function clearSettings (){
2023-03-21 21:00:43 -07:00
2023-03-21 19:27:05 -07:00
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 = '' ;
2023-02-01 12:08:13 -08:00
2023-03-21 19:27:05 -07:00
$this -> setting -> save ();
2023-03-21 15:53:09 -07:00
2023-03-21 19:27:05 -07:00
session () -> flash ( 'success' , trans ( 'admin/settings/message.update.success' ));
}
2023-02-01 12:08:13 -08:00
}
2023-03-21 15:53:09 -07:00
2023-01-25 16:22:34 -08:00
public function submit ()
{
2023-03-21 19:27:05 -07:00
if ( Helper :: isDemoMode ()) {
2023-03-21 18:59:18 -07:00
session () -> flash ( 'error' , trans ( 'general.feature_disabled' ));
} else {
2023-09-20 12:54:34 -07:00
$this -> validate ( $this -> rules );
2023-01-25 16:22:34 -08:00
2023-03-21 18:59:18 -07:00
$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 ;
2023-01-25 16:22:34 -08:00
2023-03-21 18:59:18 -07:00
$this -> setting -> save ();
session () -> flash ( 'success' , trans ( 'admin/settings/message.update.success' ));
}
2023-01-31 11:32:05 -08:00
2023-01-25 16:22:34 -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-10-28 13:57:42 -07:00
2024-10-28 10:57:48 -07:00
try {
2024-01-17 13:41:45 -08:00
2024-10-28 10:57:48 -07:00
if ( $this -> teams_webhook_deprecated ){
//will use the deprecated webhook format
$payload =
[
" @type " => " MessageCard " ,
" @context " => " http://schema.org/extensions " ,
" summary " => trans ( 'mail.snipe_webhook_summary' ),
" title " => trans ( 'mail.snipe_webhook_test' ),
" text " => trans ( 'general.webhook_test_msg' , [ 'app' => $this -> webhook_name ]),
];
$response = Http :: withHeaders ([
'content-type' => 'applications/json' ,
]) -> post ( $this -> webhook_endpoint ,
$payload ) -> throw ();
}
else {
$notification = new TeamsNotification ( $this -> webhook_endpoint );
$message = trans ( 'general.webhook_test_msg' , [ 'app' => $this -> webhook_name ]);
$notification -> success () -> sendMessage ( $message );
$response = Http :: withHeaders ([
'content-type' => 'applications/json' ,
]) -> post ( $this -> webhook_endpoint );
}
2024-01-17 13:41:45 -08:00
2024-01-17 16:30:27 -08:00
if (( $response -> getStatusCode () == 302 ) || ( $response -> getStatusCode () == 301 )){
return session () -> flash ( 'error' , trans ( 'admin/settings/message.webhook.error_redirect' , [ 'endpoint' => $this -> webhook_endpoint ]));
2024-01-17 11:49:31 -08:00
}
2024-01-17 16:30:27 -08:00
$this -> isDisabled = '' ;
$this -> save_button = trans ( 'general.save' );
return session () -> flash ( 'success' , trans ( 'admin/settings/message.webhook.success' , [ 'webhook_name' => $this -> webhook_name ]));
2024-01-17 11:49:31 -08:00
2024-01-17 16:30:27 -08:00
} catch ( \Exception $e ) {
2024-01-17 11:49:31 -08:00
2024-01-17 16:30:27 -08:00
$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 ]));
}
2024-01-17 11:49:31 -08:00
2024-01-17 16:30:27 -08:00
return session () -> flash ( 'error' , trans ( 'admin/settings/message.webhook.error_misc' ));
2024-01-17 11:49:31 -08:00
}
2023-01-24 09:25:05 -08:00
}